Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2136 lines
71KB

  1. /*
  2. * Carla JACK Engine
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "CarlaEngineInternal.hpp"
  18. #include "CarlaBackendUtils.hpp"
  19. #include "CarlaMIDI.h"
  20. #ifdef JACKBRIDGE_EXPORT
  21. # include "jackbridge/JackBridge.hpp"
  22. #else
  23. # include "jackbridge/JackBridge1.cpp"
  24. # include "jackbridge/JackBridge2.cpp"
  25. #endif
  26. #include <cmath>
  27. #include <QtCore/QStringList>
  28. #define URI_CANVAS_ICON "http://kxstudio.sf.net/ns/canvas/icon"
  29. CARLA_BACKEND_START_NAMESPACE
  30. #if 0
  31. } // Fix editor indentation
  32. #endif
  33. // -------------------------------------------------------------------------------------------------------------------
  34. // Fallback data
  35. static const EngineEvent kFallbackJackEngineEvent;
  36. // -------------------------------------------------------------------------------------------------------------------
  37. // Plugin Helpers, defined in CarlaPlugin.cpp
  38. extern CarlaEngine* CarlaPluginGetEngine(CarlaPlugin* const plugin);
  39. extern CarlaEngineClient* CarlaPluginGetEngineClient(CarlaPlugin* const plugin);
  40. extern CarlaEngineAudioPort* CarlaPluginGetAudioInPort(CarlaPlugin* const plugin, uint32_t index);
  41. extern CarlaEngineAudioPort* CarlaPluginGetAudioOutPort(CarlaPlugin* const plugin, uint32_t index);
  42. // -------------------------------------------------------------------------------------------------------------------
  43. // Carla Engine JACK-Audio port
  44. class CarlaEngineJackAudioPort : public CarlaEngineAudioPort
  45. {
  46. public:
  47. CarlaEngineJackAudioPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
  48. : CarlaEngineAudioPort(isInput, processMode),
  49. kClient(client),
  50. kPort(port)
  51. {
  52. carla_debug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
  53. if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  54. {
  55. CARLA_ASSERT(client != nullptr && port != nullptr);
  56. }
  57. else
  58. {
  59. CARLA_ASSERT(client == nullptr && port == nullptr);
  60. }
  61. }
  62. ~CarlaEngineJackAudioPort() override
  63. {
  64. carla_debug("CarlaEngineJackAudioPort::~CarlaEngineJackAudioPort()");
  65. if (kClient != nullptr && kPort != nullptr)
  66. jackbridge_port_unregister(kClient, kPort);
  67. }
  68. void initBuffer(CarlaEngine* const engine) override
  69. {
  70. CARLA_ASSERT(engine != nullptr);
  71. if (engine == nullptr)
  72. return;
  73. if (kPort == nullptr)
  74. return CarlaEngineAudioPort::initBuffer(engine);
  75. const uint32_t bufferSize(engine->getBufferSize());
  76. fBuffer = (float*)jackbridge_port_get_buffer(kPort, bufferSize);
  77. if (! kIsInput)
  78. carla_zeroFloat(fBuffer, bufferSize);
  79. }
  80. private:
  81. jack_client_t* const kClient;
  82. jack_port_t* const kPort;
  83. friend class CarlaEngineJack;
  84. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackAudioPort)
  85. };
  86. // -------------------------------------------------------------------------------------------------------------------
  87. // Carla Engine JACK-CV port
  88. class CarlaEngineJackCVPort : public CarlaEngineCVPort
  89. {
  90. public:
  91. CarlaEngineJackCVPort(const bool isInput, const ProcessMode processMode, const uint32_t bufferSize, jack_client_t* const client, jack_port_t* const port)
  92. : CarlaEngineCVPort(isInput, processMode, bufferSize),
  93. kClient(client),
  94. kPort(port)
  95. {
  96. carla_debug("CarlaEngineJackCVPort::CarlaEngineJackCVPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
  97. if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  98. {
  99. CARLA_ASSERT(client != nullptr && port != nullptr);
  100. }
  101. else
  102. {
  103. CARLA_ASSERT(client == nullptr && port == nullptr);
  104. }
  105. }
  106. ~CarlaEngineJackCVPort() override
  107. {
  108. carla_debug("CarlaEngineJackCVPort::~CarlaEngineJackCVPort()");
  109. if (kClient != nullptr && kPort != nullptr)
  110. jackbridge_port_unregister(kClient, kPort);
  111. }
  112. void initBuffer(CarlaEngine* const engine) override
  113. {
  114. CARLA_ASSERT(engine != nullptr);
  115. if (engine == nullptr)
  116. return;
  117. if (kPort == nullptr)
  118. return CarlaEngineCVPort::initBuffer(engine);
  119. CARLA_ASSERT(engine->getBufferSize() == fBufferSize);
  120. if (kIsInput)
  121. {
  122. float* const jackBuffer = (float*)jackbridge_port_get_buffer(kPort, fBufferSize);
  123. carla_copyFloat(fBuffer, jackBuffer, fBufferSize);
  124. }
  125. else
  126. {
  127. carla_zeroFloat(fBuffer, fBufferSize);
  128. }
  129. }
  130. void writeBuffer(CarlaEngine* const engine) override
  131. {
  132. CARLA_ASSERT(! kIsInput);
  133. CARLA_ASSERT(engine != nullptr);
  134. if (kIsInput)
  135. return;
  136. if (engine == nullptr)
  137. return;
  138. CARLA_ASSERT(engine->getBufferSize() == fBufferSize);
  139. float* const jackBuffer = (float*)jackbridge_port_get_buffer(kPort, fBufferSize);
  140. carla_copyFloat(jackBuffer, fBuffer, fBufferSize);
  141. }
  142. private:
  143. jack_client_t* const kClient;
  144. jack_port_t* const kPort;
  145. friend class CarlaEngineJack;
  146. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackCVPort)
  147. };
  148. // -------------------------------------------------------------------------------------------------------------------
  149. // Carla Engine JACK-Event port
  150. class CarlaEngineJackEventPort : public CarlaEngineEventPort
  151. {
  152. public:
  153. CarlaEngineJackEventPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
  154. : CarlaEngineEventPort(isInput, processMode),
  155. kClient(client),
  156. kPort(port),
  157. fJackBuffer(nullptr)
  158. {
  159. carla_debug("CarlaEngineJackEventPort::CarlaEngineJackEventPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
  160. if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  161. {
  162. CARLA_ASSERT(client != nullptr && port != nullptr);
  163. }
  164. else
  165. {
  166. CARLA_ASSERT(client == nullptr && port == nullptr);
  167. }
  168. }
  169. ~CarlaEngineJackEventPort() override
  170. {
  171. carla_debug("CarlaEngineJackEventPort::~CarlaEngineJackEventPort()");
  172. if (kClient != nullptr && kPort != nullptr)
  173. jackbridge_port_unregister(kClient, kPort);
  174. }
  175. void initBuffer(CarlaEngine* const engine) override
  176. {
  177. CARLA_ASSERT(engine != nullptr);
  178. if (engine == nullptr)
  179. return;
  180. if (kPort == nullptr)
  181. return CarlaEngineEventPort::initBuffer(engine);
  182. fJackBuffer = jackbridge_port_get_buffer(kPort, engine->getBufferSize());
  183. if (! kIsInput)
  184. jackbridge_midi_clear_buffer(fJackBuffer);
  185. }
  186. uint32_t getEventCount() const override
  187. {
  188. if (kPort == nullptr)
  189. return CarlaEngineEventPort::getEventCount();
  190. CARLA_ASSERT(kIsInput);
  191. CARLA_ASSERT(fJackBuffer != nullptr);
  192. if (! kIsInput)
  193. return 0;
  194. if (fJackBuffer == nullptr)
  195. return 0;
  196. return jackbridge_midi_get_event_count(fJackBuffer);
  197. }
  198. const EngineEvent& getEvent(const uint32_t index) override
  199. {
  200. if (kPort == nullptr)
  201. return CarlaEngineEventPort::getEvent(index);
  202. CARLA_ASSERT(kIsInput);
  203. CARLA_ASSERT(fJackBuffer != nullptr);
  204. if (! kIsInput)
  205. return kFallbackJackEngineEvent;
  206. if (fJackBuffer == nullptr)
  207. return kFallbackJackEngineEvent;
  208. jack_midi_event_t jackEvent;
  209. carla_zeroStruct<jack_midi_event_t>(jackEvent);
  210. if (! jackbridge_midi_event_get(&jackEvent, fJackBuffer, index))
  211. return kFallbackJackEngineEvent;
  212. if (jackEvent.size == 0 || jackEvent.size > 4)
  213. return kFallbackJackEngineEvent;
  214. fRetEvent.clear();
  215. const uint8_t midiStatus = MIDI_GET_STATUS_FROM_DATA(jackEvent.buffer);
  216. const uint8_t midiChannel = MIDI_GET_CHANNEL_FROM_DATA(jackEvent.buffer);
  217. fRetEvent.time = jackEvent.time;
  218. fRetEvent.channel = midiChannel;
  219. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  220. {
  221. CARLA_ASSERT(jackEvent.size == 2 || jackEvent.size == 3);
  222. const uint8_t midiControl = jackEvent.buffer[1];
  223. fRetEvent.type = kEngineEventTypeControl;
  224. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  225. {
  226. const uint8_t midiBank = jackEvent.buffer[2];
  227. fRetEvent.ctrl.type = kEngineControlEventTypeMidiBank;
  228. fRetEvent.ctrl.param = midiBank;
  229. fRetEvent.ctrl.value = 0.0f;
  230. }
  231. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  232. {
  233. fRetEvent.ctrl.type = kEngineControlEventTypeAllSoundOff;
  234. fRetEvent.ctrl.param = 0;
  235. fRetEvent.ctrl.value = 0.0f;
  236. }
  237. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  238. {
  239. fRetEvent.ctrl.type = kEngineControlEventTypeAllNotesOff;
  240. fRetEvent.ctrl.param = 0;
  241. fRetEvent.ctrl.value = 0.0f;
  242. }
  243. else
  244. {
  245. CARLA_ASSERT(jackEvent.size == 3);
  246. const uint8_t midiValue = jackEvent.buffer[2];
  247. fRetEvent.ctrl.type = kEngineControlEventTypeParameter;
  248. fRetEvent.ctrl.param = midiControl;
  249. fRetEvent.ctrl.value = float(midiValue)/127.0f;
  250. }
  251. }
  252. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  253. {
  254. CARLA_ASSERT(jackEvent.size == 2);
  255. const uint8_t midiProgram = jackEvent.buffer[1];
  256. fRetEvent.type = kEngineEventTypeControl;
  257. fRetEvent.ctrl.type = kEngineControlEventTypeMidiProgram;
  258. fRetEvent.ctrl.param = midiProgram;
  259. fRetEvent.ctrl.value = 0.0f;
  260. }
  261. else
  262. {
  263. fRetEvent.type = kEngineEventTypeMidi;
  264. fRetEvent.midi.data[0] = midiStatus;
  265. fRetEvent.midi.size = static_cast<uint8_t>(jackEvent.size);
  266. if (jackEvent.size > 1)
  267. carla_copy<uint8_t>(fRetEvent.midi.data+1, jackEvent.buffer+1, jackEvent.size-1);
  268. }
  269. return fRetEvent;
  270. }
  271. void writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) override
  272. {
  273. if (kPort == nullptr)
  274. return CarlaEngineEventPort::writeControlEvent(time, channel, type, param, value);
  275. CARLA_ASSERT(! kIsInput);
  276. CARLA_ASSERT(fJackBuffer != nullptr);
  277. CARLA_ASSERT(type != kEngineControlEventTypeNull);
  278. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  279. CARLA_ASSERT(param < MAX_MIDI_VALUE);
  280. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  281. if (kIsInput)
  282. return;
  283. if (fJackBuffer == nullptr)
  284. return;
  285. if (type == kEngineControlEventTypeNull)
  286. return;
  287. if (channel >= MAX_MIDI_CHANNELS)
  288. return;
  289. if (param >= MAX_MIDI_VALUE)
  290. return;
  291. if (type == kEngineControlEventTypeParameter)
  292. {
  293. CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  294. }
  295. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  296. size_t size = 0;
  297. jack_midi_data_t data[4] = { 0 };
  298. switch (type)
  299. {
  300. case kEngineControlEventTypeNull:
  301. break;
  302. case kEngineControlEventTypeParameter:
  303. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  304. data[1] = param;
  305. data[2] = fixedValue * 127.0f;
  306. size = 3;
  307. break;
  308. case kEngineControlEventTypeMidiBank:
  309. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  310. data[1] = MIDI_CONTROL_BANK_SELECT;
  311. data[2] = param;
  312. size = 3;
  313. break;
  314. case kEngineControlEventTypeMidiProgram:
  315. data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel;
  316. data[1] = param;
  317. size = 2;
  318. break;
  319. case kEngineControlEventTypeAllSoundOff:
  320. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  321. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  322. data[2] = 0;
  323. size = 3;
  324. break;
  325. case kEngineControlEventTypeAllNotesOff:
  326. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  327. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  328. data[2] = 0;
  329. size = 3;
  330. break;
  331. }
  332. if (size > 0)
  333. jackbridge_midi_event_write(fJackBuffer, time, data, size);
  334. }
  335. void writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t port, const uint8_t* const data, const uint8_t size) override
  336. {
  337. if (kPort == nullptr)
  338. return CarlaEngineEventPort::writeMidiEvent(time, channel, port, data, size);
  339. CARLA_ASSERT(! kIsInput);
  340. CARLA_ASSERT(fJackBuffer != nullptr);
  341. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  342. CARLA_ASSERT(data != nullptr);
  343. CARLA_ASSERT(size != 0);
  344. if (kIsInput)
  345. return;
  346. if (fJackBuffer == nullptr)
  347. return;
  348. if (channel >= MAX_MIDI_CHANNELS)
  349. return;
  350. if (data == nullptr)
  351. return;
  352. if (size == 0)
  353. return;
  354. uint8_t jdata[size];
  355. carla_copy<uint8_t>(jdata, data, size);
  356. jdata[0] = MIDI_GET_STATUS_FROM_DATA(data) + channel;
  357. jackbridge_midi_event_write(fJackBuffer, time, jdata, size);
  358. }
  359. private:
  360. jack_client_t* const kClient;
  361. jack_port_t* const kPort;
  362. void* fJackBuffer;
  363. EngineEvent fRetEvent;
  364. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackEventPort)
  365. };
  366. // -------------------------------------------------------------------------------------------------------------------
  367. // Jack Engine client
  368. class CarlaEngineJackClient : public CarlaEngineClient
  369. {
  370. public:
  371. CarlaEngineJackClient(const CarlaEngine& engine, jack_client_t* const client)
  372. : CarlaEngineClient(engine),
  373. kClient(client),
  374. kUseClient(engine.getProccessMode() == PROCESS_MODE_SINGLE_CLIENT || engine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS)
  375. {
  376. carla_debug("CarlaEngineJackClient::CarlaEngineJackClient(name:\"%s\", %p)", engine.getName(), client);
  377. if (kUseClient)
  378. {
  379. CARLA_ASSERT(kClient != nullptr);
  380. }
  381. else
  382. {
  383. CARLA_ASSERT(kClient == nullptr);
  384. }
  385. }
  386. ~CarlaEngineJackClient() override
  387. {
  388. carla_debug("CarlaEngineClient::~CarlaEngineClient()");
  389. if (kEngine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS && kClient != nullptr)
  390. jackbridge_client_close(kClient);
  391. }
  392. void activate() override
  393. {
  394. carla_debug("CarlaEngineJackClient::activate()");
  395. if (kEngine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS)
  396. {
  397. CARLA_ASSERT(kClient != nullptr && ! fActive);
  398. if (kClient != nullptr && ! fActive)
  399. jackbridge_activate(kClient);
  400. }
  401. CarlaEngineClient::activate();
  402. }
  403. void deactivate() override
  404. {
  405. carla_debug("CarlaEngineJackClient::deactivate()");
  406. if (kEngine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS)
  407. {
  408. CARLA_ASSERT(kClient != nullptr && fActive);
  409. if (kClient != nullptr && fActive)
  410. jackbridge_deactivate(kClient);
  411. }
  412. CarlaEngineClient::deactivate();
  413. }
  414. bool isOk() const override
  415. {
  416. carla_debug("CarlaEngineJackClient::isOk()");
  417. if (kUseClient)
  418. return (kClient != nullptr);
  419. return CarlaEngineClient::isOk();
  420. }
  421. #if 0
  422. void setLatency(const uint32_t samples) override
  423. {
  424. CarlaEngineClient::setLatency(samples);
  425. if (kUseClient)
  426. jackbridge_recompute_total_latencies(kClient);
  427. }
  428. #endif
  429. CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput) override
  430. {
  431. carla_debug("CarlaEngineJackClient::addPort(%s, \"%s\", %s)", EnginePortType2Str(portType), name, bool2str(isInput));
  432. jack_port_t* port = nullptr;
  433. // Create JACK port first, if needed
  434. if (kUseClient)
  435. {
  436. switch (portType)
  437. {
  438. case kEnginePortTypeNull:
  439. break;
  440. case kEnginePortTypeAudio:
  441. port = jackbridge_port_register(kClient, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  442. break;
  443. case kEnginePortTypeCV:
  444. port = jackbridge_port_register(kClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsControlVoltage | (isInput ? JackPortIsInput : JackPortIsOutput), 0);
  445. break;
  446. case kEnginePortTypeEvent:
  447. port = jackbridge_port_register(kClient, name, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  448. break;
  449. }
  450. }
  451. // Create Engine port
  452. switch (portType)
  453. {
  454. case kEnginePortTypeNull:
  455. break;
  456. case kEnginePortTypeAudio:
  457. return new CarlaEngineJackAudioPort(isInput, kEngine.getProccessMode(), kClient, port);
  458. case kEnginePortTypeCV:
  459. return new CarlaEngineJackCVPort(isInput, kEngine.getProccessMode(), kEngine.getBufferSize(), kClient, port);
  460. case kEnginePortTypeEvent:
  461. return new CarlaEngineJackEventPort(isInput, kEngine.getProccessMode(), kClient, port);
  462. }
  463. carla_stderr("CarlaEngineJackClient::addPort(%s, \"%s\", %s) - invalid type", EnginePortType2Str(portType), name, bool2str(isInput));
  464. return nullptr;
  465. }
  466. private:
  467. jack_client_t* const kClient;
  468. const bool kUseClient;
  469. friend class CarlaEngineJack;
  470. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackClient)
  471. };
  472. // -------------------------------------------------------------------------------------------------------------------
  473. // Jack Engine
  474. class CarlaEngineJack : public CarlaEngine
  475. {
  476. public:
  477. CarlaEngineJack()
  478. : CarlaEngine(),
  479. fClient(nullptr),
  480. fTransportState(JackTransportStopped),
  481. fFreewheel(false),
  482. #ifdef BUILD_BRIDGE
  483. fHasQuit(false)
  484. #else
  485. # ifdef CARLA_PROPER_CPP11_SUPPORT
  486. fRackPorts{nullptr},
  487. # endif
  488. fLastGroupId(0),
  489. fLastPortId(0),
  490. fLastConnectionId(0)
  491. #endif
  492. {
  493. carla_debug("CarlaEngineJack::CarlaEngineJack()");
  494. #ifdef BUILD_BRIDGE
  495. fOptions.processMode = PROCESS_MODE_MULTIPLE_CLIENTS;
  496. #else
  497. # ifndef CARLA_PROPER_CPP11_SUPPORT
  498. carla_fill<jack_port_t*>(fRackPorts, kRackPortCount, nullptr);
  499. # endif
  500. #endif
  501. // FIXME: Always enable JACK transport for now
  502. fOptions.transportMode = TRANSPORT_MODE_JACK;
  503. carla_zeroStruct<jack_position_t>(fTransportPos);
  504. }
  505. ~CarlaEngineJack() override
  506. {
  507. carla_debug("CarlaEngineJack::~CarlaEngineJack()");
  508. CARLA_ASSERT(fClient == nullptr);
  509. #ifndef BUILD_BRIDGE
  510. fUsedGroupNames.clear();
  511. fUsedPortNames.clear();
  512. fUsedConnections.clear();
  513. #endif
  514. }
  515. // -------------------------------------------------------------------
  516. // Maximum values
  517. unsigned int maxClientNameSize() const override
  518. {
  519. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  520. return static_cast<unsigned int>(jackbridge_client_name_size());
  521. return CarlaEngine::maxClientNameSize();
  522. }
  523. unsigned int maxPortNameSize() const override
  524. {
  525. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  526. return static_cast<unsigned int>(jackbridge_port_name_size());
  527. return CarlaEngine::maxPortNameSize();
  528. }
  529. // -------------------------------------------------------------------
  530. // Virtual, per-engine type calls
  531. bool init(const char* const clientName) override
  532. {
  533. carla_debug("CarlaEngineJack::init(\"%s\")", clientName);
  534. fFreewheel = false;
  535. fTransportState = JackTransportStopped;
  536. carla_zeroStruct<jack_position_t>(fTransportPos);
  537. #ifndef BUILD_BRIDGE
  538. fLastGroupId = 0;
  539. fLastPortId = 0;
  540. fLastConnectionId = 0;
  541. fUsedGroupNames.clear();
  542. fUsedPortNames.clear();
  543. fUsedConnections.clear();
  544. fClient = jackbridge_client_open(clientName, JackNullOption, nullptr);
  545. if (fClient != nullptr)
  546. {
  547. fBufferSize = jackbridge_get_buffer_size(fClient);
  548. fSampleRate = jackbridge_get_sample_rate(fClient);
  549. jackbridge_custom_publish_data(fClient, URI_CANVAS_ICON, "carla", 6);
  550. jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback, this);
  551. jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback, this);
  552. jackbridge_set_freewheel_callback(fClient, carla_jack_freewheel_callback, this);
  553. jackbridge_set_process_callback(fClient, carla_jack_process_callback, this);
  554. jackbridge_on_shutdown(fClient, carla_jack_shutdown_callback, this);
  555. # if 0
  556. jackbridge_set_latency_callback(fClient, carla_jack_latency_callback, this);
  557. # endif
  558. const char* const jackClientName(jackbridge_get_client_name(fClient));
  559. initJackPatchbay(jackClientName);
  560. jackbridge_set_client_registration_callback(fClient, carla_jack_client_registration_callback, this);
  561. jackbridge_set_port_registration_callback(fClient, carla_jack_port_registration_callback, this);
  562. jackbridge_set_port_connect_callback(fClient, carla_jack_port_connect_callback, this);
  563. jackbridge_set_port_rename_callback(fClient, carla_jack_port_rename_callback, this);
  564. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  565. {
  566. fRackPorts[kRackPortAudioIn1] = jackbridge_port_register(fClient, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  567. fRackPorts[kRackPortAudioIn2] = jackbridge_port_register(fClient, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  568. fRackPorts[kRackPortAudioOut1] = jackbridge_port_register(fClient, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  569. fRackPorts[kRackPortAudioOut2] = jackbridge_port_register(fClient, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  570. fRackPorts[kRackPortEventIn] = jackbridge_port_register(fClient, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  571. fRackPorts[kRackPortEventOut] = jackbridge_port_register(fClient, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  572. }
  573. if (jackbridge_activate(fClient))
  574. {
  575. CarlaEngine::init(jackClientName);
  576. return true;
  577. }
  578. else
  579. {
  580. setLastError("Failed to activate the JACK client");
  581. jackbridge_client_close(fClient);
  582. fClient = nullptr;
  583. }
  584. }
  585. else
  586. setLastError("Failed to create new JACK client");
  587. return false;
  588. #else
  589. if (fBufferSize == 0 || fSampleRate == 0.0)
  590. {
  591. // open temp client to get initial buffer-size and sample-rate values
  592. if (jack_client_t* tmpClient = jackbridge_client_open(clientName, JackNullOption, nullptr))
  593. {
  594. fBufferSize = jackbridge_get_buffer_size(tmpClient);
  595. fSampleRate = jackbridge_get_sample_rate(tmpClient);
  596. jackbridge_client_close(tmpClient);
  597. }
  598. }
  599. return CarlaEngine::init(clientName);
  600. #endif
  601. }
  602. bool close() override
  603. {
  604. carla_debug("CarlaEngineJack::close()");
  605. CarlaEngine::close();
  606. #ifdef BUILD_BRIDGE
  607. fClient = nullptr;
  608. fHasQuit = true;
  609. return true;
  610. #else
  611. if (jackbridge_deactivate(fClient))
  612. {
  613. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  614. {
  615. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioIn1]);
  616. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioIn2]);
  617. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioOut1]);
  618. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioOut2]);
  619. jackbridge_port_unregister(fClient, fRackPorts[kRackPortEventIn]);
  620. jackbridge_port_unregister(fClient, fRackPorts[kRackPortEventOut]);
  621. }
  622. if (jackbridge_client_close(fClient))
  623. {
  624. fClient = nullptr;
  625. return true;
  626. }
  627. else
  628. setLastError("Failed to close the JACK client");
  629. }
  630. else
  631. setLastError("Failed to deactivate the JACK client");
  632. fClient = nullptr;
  633. fUsedGroupNames.clear();
  634. fUsedPortNames.clear();
  635. fUsedConnections.clear();
  636. #endif
  637. return false;
  638. }
  639. bool isRunning() const override
  640. {
  641. #ifdef BUILD_BRIDGE
  642. return (fClient != nullptr || ! fHasQuit);
  643. #else
  644. return (fClient != nullptr);
  645. #endif
  646. }
  647. bool isOffline() const override
  648. {
  649. return fFreewheel;
  650. }
  651. EngineType type() const override
  652. {
  653. return kEngineTypeJack;
  654. }
  655. CarlaEngineClient* addClient(CarlaPlugin* const plugin) override
  656. {
  657. const char* const iconName(plugin->iconName());
  658. jack_client_t* client = nullptr;
  659. #ifdef BUILD_BRIDGE
  660. client = fClient = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
  661. fBufferSize = jackbridge_get_buffer_size(client);
  662. fSampleRate = jackbridge_get_sample_rate(client);
  663. jackbridge_custom_publish_data(client, URI_CANVAS_ICON, iconName, std::strlen(iconName)+1);
  664. jackbridge_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  665. jackbridge_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  666. jackbridge_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  667. jackbridge_set_process_callback(client, carla_jack_process_callback, this);
  668. jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
  669. # if 0
  670. jackbridge_set_latency_callback(client, carla_jack_latency_callback, this);
  671. # endif
  672. #else
  673. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
  674. {
  675. client = fClient;
  676. }
  677. else if (fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  678. {
  679. client = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
  680. CARLA_ASSERT(client != nullptr);
  681. if (client == nullptr)
  682. return nullptr;
  683. jackbridge_custom_publish_data(client, URI_CANVAS_ICON, iconName, std::strlen(iconName)+1);
  684. jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, plugin);
  685. # if 0
  686. jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, plugin);
  687. # endif
  688. }
  689. #endif
  690. return new CarlaEngineJackClient(*this, client);
  691. }
  692. #ifndef BUILD_BRIDGE
  693. const char* renamePlugin(const unsigned int id, const char* const newName) override
  694. {
  695. CARLA_ASSERT(kData->curPluginCount > 0);
  696. CARLA_ASSERT(id < kData->curPluginCount);
  697. CARLA_ASSERT(kData->plugins != nullptr);
  698. CARLA_ASSERT(newName != nullptr);
  699. if (kData->plugins == nullptr)
  700. {
  701. setLastError("Critical error: no plugins are currently loaded!");
  702. return nullptr;
  703. }
  704. CarlaPlugin* const plugin(kData->plugins[id].plugin);
  705. if (plugin == nullptr)
  706. {
  707. carla_stderr("CarlaEngine::clonePlugin(%i) - could not find plugin", id);
  708. return nullptr;
  709. }
  710. CARLA_ASSERT(plugin->id() == id);
  711. bool needsReinit = (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT);
  712. const char* name = getUniquePluginName(newName);
  713. // TODO - use rename port if single-client
  714. // JACK client rename
  715. if (fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  716. {
  717. CarlaEngineJackClient* const client((CarlaEngineJackClient*)CarlaPluginGetEngineClient(plugin));
  718. if (bridge.client_rename_ptr != nullptr)
  719. {
  720. name = bridge.client_rename_ptr(client->kClient, name);
  721. }
  722. else
  723. {
  724. // we should not be able to do this, jack really needs to allow client rename
  725. needsReinit = true;
  726. if (jack_client_t* jclient = jackbridge_client_open(name, JackNullOption, nullptr))
  727. {
  728. const char* const iconName(plugin->iconName());
  729. jackbridge_custom_publish_data(jclient, URI_CANVAS_ICON, iconName, std::strlen(iconName)+1);
  730. // close old client
  731. plugin->setEnabled(false);
  732. if (client->isActive())
  733. client->deactivate();
  734. plugin->clearBuffers();
  735. jackbridge_client_close(client->kClient);
  736. // set new client data
  737. name = jackbridge_get_client_name(jclient);
  738. jackbridge_set_process_callback(jclient, carla_jack_process_callback_plugin, plugin);
  739. # if 0
  740. jackbridge_set_latency_callback(jclient, carla_jack_latency_callback_plugin, plugin);
  741. # endif
  742. // this is supposed to be constant...
  743. std::memcpy((jack_client_t**)&client->kClient, &jclient, sizeof(jack_client_t**));
  744. }
  745. }
  746. }
  747. if (name == nullptr)
  748. return nullptr;
  749. // Rename
  750. plugin->setName(name);
  751. if (needsReinit)
  752. {
  753. // reload plugin to recreate its ports
  754. const SaveState& saveState(plugin->getSaveState());
  755. plugin->reload();
  756. plugin->loadSaveState(saveState);
  757. }
  758. return name;
  759. }
  760. // -------------------------------------------------------------------
  761. // Patchbay
  762. bool patchbayConnect(int portA, int portB) override
  763. {
  764. CARLA_ASSERT(fClient != nullptr);
  765. if (fClient == nullptr)
  766. {
  767. setLastError("Invalid JACK client");
  768. return false;
  769. }
  770. char portNameA[STR_MAX+1];
  771. char portNameB[STR_MAX+1];
  772. getFullPortName(portA, portNameA);
  773. getFullPortName(portB, portNameB);
  774. if (! jackbridge_connect(fClient, portNameA, portNameB))
  775. {
  776. setLastError("JACK operation failed");
  777. return false;
  778. }
  779. return true;
  780. }
  781. bool patchbayDisconnect(int connectionId) override
  782. {
  783. CARLA_ASSERT(fClient != nullptr);
  784. if (fClient == nullptr)
  785. {
  786. setLastError("Invalid JACK client");
  787. return false;
  788. }
  789. foreach (const ConnectionToId& connectionToId, fUsedConnections)
  790. {
  791. if (connectionToId.id == connectionId)
  792. {
  793. char portNameOut[STR_MAX+1];
  794. char portNameIn[STR_MAX+1];
  795. getFullPortName(connectionToId.portOut, portNameOut);
  796. getFullPortName(connectionToId.portIn, portNameIn);
  797. if (! jackbridge_disconnect(fClient, portNameOut, portNameIn))
  798. {
  799. setLastError("JACK operation failed");
  800. return false;
  801. }
  802. return true;
  803. }
  804. }
  805. setLastError("Failed to find the requested connection");
  806. return false;
  807. }
  808. void patchbayRefresh() override
  809. {
  810. CARLA_ASSERT(fClient != nullptr);
  811. if (fClient == nullptr)
  812. return;
  813. fLastGroupId = 0;
  814. fLastPortId = 0;
  815. fLastConnectionId = 0;
  816. fUsedGroupNames.clear();
  817. fUsedPortNames.clear();
  818. fUsedConnections.clear();
  819. initJackPatchbay(jackbridge_get_client_name(fClient));
  820. }
  821. #endif
  822. // -------------------------------------------------------------------
  823. // Transport
  824. void transportPlay() override
  825. {
  826. if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
  827. CarlaEngine::transportPlay();
  828. else if (fClient != nullptr)
  829. jackbridge_transport_start(fClient);
  830. }
  831. void transportPause() override
  832. {
  833. if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
  834. CarlaEngine::transportPause();
  835. else if (fClient != nullptr)
  836. jackbridge_transport_stop(fClient);
  837. }
  838. void transportRelocate(const uint32_t frame) override
  839. {
  840. if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
  841. CarlaEngine::transportRelocate(frame);
  842. else if (fClient != nullptr)
  843. jackbridge_transport_locate(fClient, frame);
  844. }
  845. // -------------------------------------
  846. protected:
  847. void handleJackBufferSizeCallback(const uint32_t newBufferSize)
  848. {
  849. if (fBufferSize == newBufferSize)
  850. return;
  851. fBufferSize = newBufferSize;
  852. bufferSizeChanged(newBufferSize);
  853. }
  854. void handleJackSampleRateCallback(const double newSampleRate)
  855. {
  856. if (fSampleRate == newSampleRate)
  857. return;
  858. fSampleRate = newSampleRate;
  859. sampleRateChanged(newSampleRate);
  860. }
  861. void handleJackFreewheelCallback(const bool isFreewheel)
  862. {
  863. fFreewheel = isFreewheel;
  864. }
  865. void saveTransportInfo()
  866. {
  867. if (fOptions.transportMode != TRANSPORT_MODE_JACK)
  868. return;
  869. fTransportPos.unique_1 = fTransportPos.unique_2 + 1; // invalidate
  870. fTransportState = jackbridge_transport_query(fClient, &fTransportPos);
  871. fTimeInfo.playing = (fTransportState == JackTransportRolling);
  872. if (fTransportPos.unique_1 == fTransportPos.unique_2)
  873. {
  874. fTimeInfo.frame = fTransportPos.frame;
  875. fTimeInfo.usecs = fTransportPos.usecs;
  876. if (fTransportPos.valid & JackPositionBBT)
  877. {
  878. fTimeInfo.valid = EngineTimeInfo::ValidBBT;
  879. fTimeInfo.bbt.bar = fTransportPos.bar;
  880. fTimeInfo.bbt.beat = fTransportPos.beat;
  881. fTimeInfo.bbt.tick = fTransportPos.tick;
  882. fTimeInfo.bbt.barStartTick = fTransportPos.bar_start_tick;
  883. fTimeInfo.bbt.beatsPerBar = fTransportPos.beats_per_bar;
  884. fTimeInfo.bbt.beatType = fTransportPos.beat_type;
  885. fTimeInfo.bbt.ticksPerBeat = fTransportPos.ticks_per_beat;
  886. fTimeInfo.bbt.beatsPerMinute = fTransportPos.beats_per_minute;
  887. }
  888. else
  889. fTimeInfo.valid = 0x0;
  890. }
  891. else
  892. {
  893. fTimeInfo.frame = 0;
  894. fTimeInfo.valid = 0x0;
  895. }
  896. }
  897. void handleJackProcessCallback(const uint32_t nframes)
  898. {
  899. saveTransportInfo();
  900. #ifndef BUILD_BRIDGE
  901. if (kData->curPluginCount == 0)
  902. {
  903. // pass-through
  904. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  905. {
  906. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  907. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  908. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  909. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  910. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes);
  911. CARLA_ASSERT(audioIn1 != nullptr);
  912. CARLA_ASSERT(audioIn2 != nullptr);
  913. CARLA_ASSERT(audioOut1 != nullptr);
  914. CARLA_ASSERT(audioOut2 != nullptr);
  915. CARLA_ASSERT(eventOut != nullptr);
  916. carla_copyFloat(audioOut1, audioIn1, nframes);
  917. carla_copyFloat(audioOut2, audioIn2, nframes);
  918. jackbridge_midi_clear_buffer(eventOut);
  919. }
  920. return proccessPendingEvents();
  921. }
  922. #endif
  923. #ifdef BUILD_BRIDGE
  924. CarlaPlugin* const plugin(getPluginUnchecked(0));
  925. if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
  926. {
  927. plugin->initBuffers();
  928. processPlugin(plugin, nframes);
  929. plugin->unlock();
  930. }
  931. #else
  932. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
  933. {
  934. for (unsigned int i=0; i < kData->curPluginCount; ++i)
  935. {
  936. CarlaPlugin* const plugin(getPluginUnchecked(i));
  937. if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
  938. {
  939. plugin->initBuffers();
  940. processPlugin(plugin, nframes);
  941. plugin->unlock();
  942. }
  943. }
  944. }
  945. else if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  946. {
  947. // get buffers from jack
  948. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  949. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  950. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  951. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  952. void* const eventIn = jackbridge_port_get_buffer(fRackPorts[kRackPortEventIn], nframes);
  953. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes);
  954. // assert buffers
  955. CARLA_ASSERT(audioIn1 != nullptr);
  956. CARLA_ASSERT(audioIn2 != nullptr);
  957. CARLA_ASSERT(audioOut1 != nullptr);
  958. CARLA_ASSERT(audioOut2 != nullptr);
  959. CARLA_ASSERT(eventIn != nullptr);
  960. CARLA_ASSERT(eventOut != nullptr);
  961. // create audio buffers
  962. float* inBuf[2] = { audioIn1, audioIn2 };
  963. float* outBuf[2] = { audioOut1, audioOut2 };
  964. // initialize input events
  965. carla_zeroStruct<EngineEvent>(kData->bufEvents.in, INTERNAL_EVENT_COUNT);
  966. {
  967. uint32_t engineEventIndex = 0;
  968. jack_midi_event_t jackEvent;
  969. const uint32_t jackEventCount(jackbridge_midi_get_event_count(eventIn));
  970. for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; ++jackEventIndex)
  971. {
  972. if (jackbridge_midi_event_get(&jackEvent, eventIn, jackEventIndex) != 0)
  973. continue;
  974. EngineEvent* const engineEvent(&kData->bufEvents.in[engineEventIndex++]);
  975. engineEvent->clear();
  976. const uint8_t midiStatus = MIDI_GET_STATUS_FROM_DATA(jackEvent.buffer);
  977. const uint8_t midiChannel = MIDI_GET_CHANNEL_FROM_DATA(jackEvent.buffer);
  978. engineEvent->time = jackEvent.time;
  979. engineEvent->channel = midiChannel;
  980. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  981. {
  982. const uint8_t midiControl = jackEvent.buffer[1];
  983. engineEvent->type = kEngineEventTypeControl;
  984. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  985. {
  986. const uint8_t midiBank = jackEvent.buffer[2];
  987. engineEvent->ctrl.type = kEngineControlEventTypeMidiBank;
  988. engineEvent->ctrl.param = midiBank;
  989. engineEvent->ctrl.value = 0.0f;
  990. }
  991. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  992. {
  993. engineEvent->ctrl.type = kEngineControlEventTypeAllSoundOff;
  994. engineEvent->ctrl.param = 0;
  995. engineEvent->ctrl.value = 0.0f;
  996. }
  997. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  998. {
  999. engineEvent->ctrl.type = kEngineControlEventTypeAllNotesOff;
  1000. engineEvent->ctrl.param = 0;
  1001. engineEvent->ctrl.value = 0.0f;
  1002. }
  1003. else
  1004. {
  1005. const uint8_t midiValue = jackEvent.buffer[2];
  1006. engineEvent->ctrl.type = kEngineControlEventTypeParameter;
  1007. engineEvent->ctrl.param = midiControl;
  1008. engineEvent->ctrl.value = float(midiValue)/127.0f;
  1009. }
  1010. }
  1011. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  1012. {
  1013. const uint8_t midiProgram = jackEvent.buffer[1];
  1014. engineEvent->type = kEngineEventTypeControl;
  1015. engineEvent->ctrl.type = kEngineControlEventTypeMidiProgram;
  1016. engineEvent->ctrl.param = midiProgram;
  1017. engineEvent->ctrl.value = 0.0f;
  1018. }
  1019. else if (jackEvent.size <= 4)
  1020. {
  1021. engineEvent->type = kEngineEventTypeMidi;
  1022. carla_copy<uint8_t>(engineEvent->midi.data, jackEvent.buffer, jackEvent.size);
  1023. engineEvent->midi.data[0] = midiStatus;
  1024. engineEvent->midi.size = static_cast<uint8_t>(jackEvent.size);
  1025. }
  1026. if (engineEventIndex >= INTERNAL_EVENT_COUNT)
  1027. break;
  1028. }
  1029. }
  1030. // process rack
  1031. processRack(inBuf, outBuf, nframes);
  1032. // output control
  1033. {
  1034. jackbridge_midi_clear_buffer(eventOut);
  1035. for (unsigned short i=0; i < INTERNAL_EVENT_COUNT; ++i)
  1036. {
  1037. EngineEvent* const engineEvent = &kData->bufEvents.out[i];
  1038. uint8_t data[3] = { 0 };
  1039. uint8_t size = 0;
  1040. switch (engineEvent->type)
  1041. {
  1042. case kEngineEventTypeNull:
  1043. break;
  1044. case kEngineEventTypeControl:
  1045. {
  1046. EngineControlEvent* const ctrlEvent = &engineEvent->ctrl;
  1047. if (ctrlEvent->type == kEngineControlEventTypeParameter && MIDI_IS_CONTROL_BANK_SELECT(ctrlEvent->param))
  1048. {
  1049. // FIXME?
  1050. ctrlEvent->type = kEngineControlEventTypeMidiBank;
  1051. ctrlEvent->param = ctrlEvent->value;
  1052. ctrlEvent->value = 0.0f;
  1053. }
  1054. switch (ctrlEvent->type)
  1055. {
  1056. case kEngineControlEventTypeNull:
  1057. break;
  1058. case kEngineControlEventTypeParameter:
  1059. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  1060. data[1] = static_cast<uint8_t>(ctrlEvent->param);
  1061. data[2] = uint8_t(ctrlEvent->value * 127.0f);
  1062. size = 3;
  1063. break;
  1064. case kEngineControlEventTypeMidiBank:
  1065. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  1066. data[1] = MIDI_CONTROL_BANK_SELECT;
  1067. data[2] = static_cast<uint8_t>(ctrlEvent->param);
  1068. size = 3;
  1069. break;
  1070. case kEngineControlEventTypeMidiProgram:
  1071. data[0] = MIDI_STATUS_PROGRAM_CHANGE + engineEvent->channel;
  1072. data[1] = static_cast<uint8_t>(ctrlEvent->param);
  1073. size = 2;
  1074. break;
  1075. case kEngineControlEventTypeAllSoundOff:
  1076. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  1077. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1078. size = 2;
  1079. break;
  1080. case kEngineControlEventTypeAllNotesOff:
  1081. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  1082. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1083. size = 2;
  1084. break;
  1085. }
  1086. break;
  1087. }
  1088. case kEngineEventTypeMidi:
  1089. {
  1090. EngineMidiEvent* const midiEvent = &engineEvent->midi;
  1091. data[0] = midiEvent->data[0];
  1092. data[1] = midiEvent->data[1];
  1093. data[2] = midiEvent->data[2];
  1094. size = midiEvent->size;
  1095. break;
  1096. }
  1097. }
  1098. if (size > 0)
  1099. jackbridge_midi_event_write(eventOut, engineEvent->time, data, size);
  1100. }
  1101. }
  1102. }
  1103. #endif // ! BUILD_BRIDGE
  1104. proccessPendingEvents();
  1105. }
  1106. #if 0
  1107. void handleJackLatencyCallback(const jack_latency_callback_mode_t mode)
  1108. {
  1109. if (fOptions.processMode != PROCESS_MODE_SINGLE_CLIENT)
  1110. return;
  1111. for (unsigned int i=0; i < kData->curPluginCount; ++i)
  1112. {
  1113. CarlaPlugin* const plugin = getPluginUnchecked(i);
  1114. if (plugin && plugin->enabled())
  1115. latencyPlugin(plugin, mode);
  1116. }
  1117. }
  1118. #endif
  1119. #ifndef BUILD_BRIDGE
  1120. void handleJackClientRegistrationCallback(const char* name, bool reg)
  1121. {
  1122. // do nothing on client registration, wait for first port
  1123. if (reg) return;
  1124. const int id(getGroupId(name)); // also checks name nullness
  1125. if (id == -1)
  1126. return;
  1127. GroupNameToId groupNameId(id, name);
  1128. fUsedGroupNames.removeAll(groupNameId);
  1129. callback(CALLBACK_PATCHBAY_CLIENT_REMOVED, 0, id, 0, 0.0f, nullptr);
  1130. }
  1131. void handleJackPortRegistrationCallback(jack_port_id_t port, bool reg)
  1132. {
  1133. jack_port_t* const jackPort(jackbridge_port_by_id(fClient, port));
  1134. const char* const portName(jackbridge_port_short_name(jackPort));
  1135. const char* const fullPortName(jackbridge_port_name(jackPort));
  1136. CARLA_ASSERT(jackPort != nullptr);
  1137. CARLA_ASSERT(portName != nullptr);
  1138. CARLA_ASSERT(fullPortName != nullptr);
  1139. if (jackPort == nullptr)
  1140. return;
  1141. if (portName == nullptr)
  1142. return;
  1143. if (fullPortName == nullptr)
  1144. return;
  1145. const size_t groupNameSize(std::strstr(fullPortName, portName) - fullPortName - 1);
  1146. char groupName[groupNameSize+1];
  1147. carla_copy<char>(groupName, fullPortName, groupNameSize);
  1148. groupName[groupNameSize] = '\0';
  1149. int groupId = getGroupId(groupName);
  1150. if (reg)
  1151. {
  1152. const int jackPortFlags(jackbridge_port_flags(jackPort));
  1153. if (groupId == -1)
  1154. {
  1155. groupId = fLastGroupId++;
  1156. GroupNameToId groupNameToId(groupId, groupName);
  1157. fUsedGroupNames.append(groupNameToId);
  1158. PatchbayIconType groupIcon = PATCHBAY_ICON_APPLICATION;
  1159. void* data = nullptr;
  1160. size_t dataSize = 0;
  1161. if (jackbridge_custom_get_data(fClient, groupName, URI_CANVAS_ICON, &data, &dataSize) && data != nullptr && dataSize != 0)
  1162. {
  1163. const char* const icon((const char*)data);
  1164. CARLA_ASSERT(std::strlen(icon)+1 == dataSize);
  1165. if (std::strcmp(icon, "app") == 0 || std::strcmp(icon, "application") == 0)
  1166. groupIcon = PATCHBAY_ICON_APPLICATION;
  1167. else if (std::strcmp(icon, "hardware") == 0)
  1168. groupIcon = PATCHBAY_ICON_HARDWARE;
  1169. else if (std::strcmp(icon, "carla") == 0)
  1170. groupIcon = PATCHBAY_ICON_CARLA;
  1171. else if (std::strcmp(icon, "distrho") == 0)
  1172. groupIcon = PATCHBAY_ICON_DISTRHO;
  1173. else if (std::strcmp(icon, "file") == 0)
  1174. groupIcon = PATCHBAY_ICON_FILE;
  1175. else if (std::strcmp(icon, "plugin") == 0)
  1176. groupIcon = PATCHBAY_ICON_PLUGIN;
  1177. }
  1178. else if (jackPortFlags & JackPortIsPhysical)
  1179. groupIcon = PATCHBAY_ICON_HARDWARE;
  1180. callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupId, groupIcon, 0.0f, groupName);
  1181. }
  1182. bool portIsInput = (jackPortFlags & JackPortIsInput);
  1183. bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
  1184. bool portIsCV = (jackPortFlags & JackPortIsControlVoltage);
  1185. unsigned int canvasPortFlags = 0x0;
  1186. canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
  1187. canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
  1188. if (portIsAudio && portIsCV)
  1189. canvasPortFlags |= PATCHBAY_PORT_IS_CV;
  1190. PortNameToId portNameToId(groupId, fLastPortId++, portName, fullPortName);
  1191. fUsedPortNames.append(portNameToId);
  1192. callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, portNameToId.portId, canvasPortFlags, portName);
  1193. }
  1194. else
  1195. {
  1196. const int portId(getPortId(fullPortName));
  1197. CARLA_ASSERT(groupId != -1);
  1198. CARLA_ASSERT(portId != -1);
  1199. if (groupId == -1 || portId == -1)
  1200. return;
  1201. PortNameToId portNameId(groupId, portId, portName, fullPortName);
  1202. fUsedPortNames.removeOne(portNameId);
  1203. callback(CALLBACK_PATCHBAY_PORT_REMOVED, 0, portId, 0, 0.0f, nullptr);
  1204. }
  1205. }
  1206. void handleJackPortConnectCallback(jack_port_id_t a, jack_port_id_t b, bool connect)
  1207. {
  1208. jack_port_t* const jackPortA(jackbridge_port_by_id(fClient, a));
  1209. jack_port_t* const jackPortB(jackbridge_port_by_id(fClient, b));
  1210. const char* const fullPortNameA(jackbridge_port_name(jackPortA));
  1211. const char* const fullPortNameB(jackbridge_port_name(jackPortB));
  1212. CARLA_ASSERT(jackPortA != nullptr);
  1213. CARLA_ASSERT(jackPortB != nullptr);
  1214. CARLA_ASSERT(fullPortNameA != nullptr);
  1215. CARLA_ASSERT(fullPortNameB != nullptr);
  1216. if (jackPortA == nullptr)
  1217. return;
  1218. if (jackPortB == nullptr)
  1219. return;
  1220. if (fullPortNameA == nullptr)
  1221. return;
  1222. if (fullPortNameB == nullptr)
  1223. return;
  1224. const int portIdA(getPortId(fullPortNameA));
  1225. const int portIdB(getPortId(fullPortNameB));
  1226. if (portIdA == -1 || portIdB == -1)
  1227. return;
  1228. if (connect)
  1229. {
  1230. ConnectionToId connectionToId(fLastConnectionId++, portIdA, portIdB);
  1231. fUsedConnections.append(connectionToId);
  1232. callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, connectionToId.id, portIdA, portIdB, nullptr);
  1233. }
  1234. else
  1235. {
  1236. for (int i=0, count=fUsedConnections.count(); i < count; ++i)
  1237. {
  1238. const ConnectionToId& connectionToId(fUsedConnections[i]);
  1239. if (connectionToId.portOut == portIdA && connectionToId.portIn == portIdB)
  1240. {
  1241. callback(CALLBACK_PATCHBAY_CONNECTION_REMOVED, 0, connectionToId.id, 0, 0.0f, nullptr);
  1242. fUsedConnections.takeAt(i);
  1243. break;
  1244. }
  1245. }
  1246. }
  1247. }
  1248. void handleJackPortRenameCallback(jack_port_id_t port, const char* oldName, const char* newName)
  1249. {
  1250. jack_port_t* const jackPort(jackbridge_port_by_id(fClient, port));
  1251. const char* const portName(jackbridge_port_short_name(jackPort));
  1252. CARLA_ASSERT(jackPort != nullptr);
  1253. CARLA_ASSERT(portName != nullptr);
  1254. if (jackPort == nullptr)
  1255. return;
  1256. if (portName == nullptr)
  1257. return;
  1258. const size_t groupNameSize(std::strstr(newName, portName) - newName - 1);
  1259. char groupName[groupNameSize+1];
  1260. carla_copy<char>(groupName, newName, groupNameSize);
  1261. groupName[groupNameSize] = '\0';
  1262. const int groupId(getGroupId(groupName));
  1263. CARLA_ASSERT(groupId != -1);
  1264. if (groupId == -1)
  1265. return;
  1266. for (int i=0, count=fUsedPortNames.count(); i < count; ++i)
  1267. {
  1268. PortNameToId& portNameId(fUsedPortNames[i]);
  1269. if (/*portNameId.groupId == groupId &&*/ std::strcmp(portNameId.fullName, oldName) == 0)
  1270. {
  1271. CARLA_ASSERT(portNameId.groupId == groupId);
  1272. portNameId.rename(portName, newName);
  1273. callback(CALLBACK_PATCHBAY_PORT_RENAMED, 0, portNameId.portId, 0, 0.0f, newName);
  1274. break;
  1275. }
  1276. }
  1277. }
  1278. #endif
  1279. void handleJackShutdownCallback()
  1280. {
  1281. for (unsigned int i=0; i < kData->curPluginCount; ++i)
  1282. {
  1283. //CarlaPlugin* const plugin = getPluginUnchecked(i);
  1284. //if (plugin)
  1285. // plugin->x_client = nullptr;
  1286. }
  1287. fClient = nullptr;
  1288. callback(CALLBACK_QUIT, 0, 0, 0, 0.0f, nullptr);
  1289. }
  1290. // -------------------------------------
  1291. private:
  1292. jack_client_t* fClient;
  1293. jack_position_t fTransportPos;
  1294. jack_transport_state_t fTransportState;
  1295. bool fFreewheel;
  1296. // -------------------------------------
  1297. #ifdef BUILD_BRIDGE
  1298. bool fHasQuit;
  1299. #else
  1300. enum RackPorts {
  1301. kRackPortAudioIn1 = 0,
  1302. kRackPortAudioIn2 = 1,
  1303. kRackPortAudioOut1 = 2,
  1304. kRackPortAudioOut2 = 3,
  1305. kRackPortEventIn = 4,
  1306. kRackPortEventOut = 5,
  1307. kRackPortCount = 6
  1308. };
  1309. jack_port_t* fRackPorts[kRackPortCount];
  1310. struct GroupNameToId {
  1311. int id;
  1312. char name[STR_MAX+1];
  1313. GroupNameToId()
  1314. {
  1315. id = -1;
  1316. name[0] = '\0';
  1317. }
  1318. GroupNameToId(const int id, const char name[])
  1319. {
  1320. this->id = id;
  1321. std::strncpy(this->name, name, STR_MAX);
  1322. this->name[STR_MAX] = '\0';
  1323. }
  1324. bool operator==(const GroupNameToId& groupNameId)
  1325. {
  1326. if (groupNameId.id != id)
  1327. return false;
  1328. if (std::strcmp(groupNameId.name, name) != 0)
  1329. return false;
  1330. return true;
  1331. }
  1332. };
  1333. struct PortNameToId {
  1334. int groupId;
  1335. int portId;
  1336. char name[STR_MAX+1];
  1337. char fullName[STR_MAX+1]; // unique
  1338. PortNameToId()
  1339. {
  1340. groupId = -1;
  1341. portId = -1;
  1342. name[0] = '\0';
  1343. fullName[0] = '\0';
  1344. }
  1345. PortNameToId(const int groupId, const int portId, const char name[], const char fullName[])
  1346. {
  1347. this->groupId = groupId;
  1348. this->portId = portId;
  1349. std::strncpy(this->name, name, STR_MAX);
  1350. this->name[STR_MAX] = '\0';
  1351. std::strncpy(this->fullName, fullName, STR_MAX);
  1352. this->fullName[STR_MAX] = '\0';
  1353. }
  1354. void rename(const char name[], const char fullName[])
  1355. {
  1356. std::strncpy(this->name, name, STR_MAX);
  1357. this->name[STR_MAX] = '\0';
  1358. std::strncpy(this->fullName, fullName, STR_MAX);
  1359. this->fullName[STR_MAX] = '\0';
  1360. }
  1361. bool operator==(const PortNameToId& portNameId)
  1362. {
  1363. if (portNameId.groupId != groupId)
  1364. return false;
  1365. if (portNameId.portId != portId)
  1366. return false;
  1367. if (std::strcmp(portNameId.name, name) != 0)
  1368. return false;
  1369. if (std::strcmp(portNameId.fullName, fullName) != 0)
  1370. return false;
  1371. return true;
  1372. }
  1373. };
  1374. struct ConnectionToId {
  1375. int id;
  1376. int portOut;
  1377. int portIn;
  1378. ConnectionToId()
  1379. {
  1380. id = -1;
  1381. portOut = -1;
  1382. portIn = -1;
  1383. }
  1384. ConnectionToId(const int id, const int portOut, const int portIn)
  1385. {
  1386. this->id = id;
  1387. this->portOut = portOut;
  1388. this->portIn = portIn;
  1389. }
  1390. bool operator==(const ConnectionToId& connectionId)
  1391. {
  1392. if (connectionId.id != id)
  1393. return false;
  1394. if (connectionId.portOut != portOut)
  1395. return false;
  1396. if (connectionId.portIn != portIn)
  1397. return false;
  1398. return true;
  1399. }
  1400. };
  1401. int fLastGroupId;
  1402. int fLastPortId;
  1403. int fLastConnectionId;
  1404. QList<GroupNameToId> fUsedGroupNames;
  1405. QList<PortNameToId> fUsedPortNames;
  1406. QList<ConnectionToId> fUsedConnections;
  1407. int getGroupId(const char* const name)
  1408. {
  1409. CARLA_ASSERT(name != nullptr);
  1410. if (name == nullptr)
  1411. return -1;
  1412. foreach (const GroupNameToId& groupNameId, fUsedGroupNames)
  1413. {
  1414. if (std::strcmp(groupNameId.name, name) == 0)
  1415. return groupNameId.id;
  1416. }
  1417. return -1;
  1418. }
  1419. int getPortId(const char* const fullName)
  1420. {
  1421. CARLA_ASSERT(fullName != nullptr);
  1422. if (fullName == nullptr)
  1423. return -1;
  1424. foreach (const PortNameToId& portNameId, fUsedPortNames)
  1425. {
  1426. if (std::strcmp(portNameId.fullName, fullName) == 0)
  1427. return portNameId.portId;
  1428. }
  1429. return -1;
  1430. }
  1431. void getFullPortName(const int portId, char nameBuf[STR_MAX+1])
  1432. {
  1433. foreach (const PortNameToId& portNameId, fUsedPortNames)
  1434. {
  1435. if (portNameId.portId == portId)
  1436. {
  1437. std::strncpy(nameBuf, portNameId.fullName, STR_MAX);
  1438. nameBuf[STR_MAX] = '\0';
  1439. return;
  1440. }
  1441. }
  1442. nameBuf[0] = '\0';
  1443. }
  1444. void initJackPatchbay(const char* const ourName)
  1445. {
  1446. CARLA_ASSERT(fLastGroupId == 0);
  1447. CARLA_ASSERT(fLastPortId == 0);
  1448. CARLA_ASSERT(fLastConnectionId == 0);
  1449. CARLA_ASSERT(ourName != nullptr);
  1450. // query initial jack ports
  1451. QStringList parsedGroups;
  1452. // our client
  1453. if (ourName != nullptr)
  1454. {
  1455. parsedGroups.append(QString(ourName));
  1456. GroupNameToId groupNameToId(fLastGroupId++, ourName);
  1457. fUsedGroupNames.append(groupNameToId);
  1458. callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupNameToId.id, PATCHBAY_ICON_CARLA, 0.0f, ourName);
  1459. }
  1460. if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, 0))
  1461. {
  1462. for (int i=0; ports[i] != nullptr; ++i)
  1463. {
  1464. jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
  1465. const char* const portName(jackbridge_port_short_name(jackPort));
  1466. const char* const fullPortName(ports[i]);
  1467. CARLA_ASSERT(jackPort != nullptr);
  1468. CARLA_ASSERT(portName != nullptr);
  1469. if (jackPort == nullptr)
  1470. continue;
  1471. if (portName == nullptr)
  1472. continue;
  1473. const int jackPortFlags(jackbridge_port_flags(jackPort));
  1474. const size_t groupNameSize(std::strstr(fullPortName, portName) - fullPortName - 1);
  1475. char groupName[groupNameSize+1];
  1476. int groupId = -1;
  1477. carla_copy<char>(groupName, fullPortName, groupNameSize);
  1478. groupName[groupNameSize] = '\0';
  1479. if (parsedGroups.contains(QString(groupName)))
  1480. {
  1481. groupId = getGroupId(groupName);
  1482. CARLA_ASSERT(groupId != -1);
  1483. }
  1484. else
  1485. {
  1486. groupId = fLastGroupId++;
  1487. parsedGroups.append(groupName);
  1488. GroupNameToId groupNameToId(groupId, groupName);
  1489. fUsedGroupNames.append(groupNameToId);
  1490. PatchbayIconType groupIcon = PATCHBAY_ICON_APPLICATION;
  1491. void* data = nullptr;
  1492. size_t dataSize = 0;
  1493. if (jackbridge_custom_get_data(fClient, groupName, URI_CANVAS_ICON, &data, &dataSize) && data != nullptr && dataSize != 0)
  1494. {
  1495. const char* const icon((const char*)data);
  1496. CARLA_ASSERT(std::strlen(icon)+1 == dataSize);
  1497. if (std::strcmp(icon, "app") == 0 || std::strcmp(icon, "application") == 0)
  1498. groupIcon = PATCHBAY_ICON_APPLICATION;
  1499. else if (std::strcmp(icon, "hardware") == 0)
  1500. groupIcon = PATCHBAY_ICON_HARDWARE;
  1501. else if (std::strcmp(icon, "carla") == 0)
  1502. groupIcon = PATCHBAY_ICON_CARLA;
  1503. else if (std::strcmp(icon, "distrho") == 0)
  1504. groupIcon = PATCHBAY_ICON_DISTRHO;
  1505. else if (std::strcmp(icon, "file") == 0)
  1506. groupIcon = PATCHBAY_ICON_FILE;
  1507. else if (std::strcmp(icon, "plugin") == 0)
  1508. groupIcon = PATCHBAY_ICON_PLUGIN;
  1509. }
  1510. else if (jackPortFlags & JackPortIsPhysical)
  1511. groupIcon = PATCHBAY_ICON_HARDWARE;
  1512. callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupId, groupIcon, 0.0f, groupName);
  1513. }
  1514. bool portIsInput = (jackPortFlags & JackPortIsInput);
  1515. bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
  1516. bool portIsCV = (jackPortFlags & JackPortIsControlVoltage);
  1517. unsigned int canvasPortFlags = 0x0;
  1518. canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
  1519. canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
  1520. if (portIsAudio && portIsCV)
  1521. canvasPortFlags |= PATCHBAY_PORT_IS_CV;
  1522. PortNameToId portNameToId(groupId, fLastPortId++, portName, fullPortName);
  1523. fUsedPortNames.append(portNameToId);
  1524. callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, portNameToId.portId, canvasPortFlags, portName);
  1525. }
  1526. jackbridge_free(ports);
  1527. }
  1528. // query connections, after all ports are in place
  1529. if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
  1530. {
  1531. for (int i=0; ports[i] != nullptr; ++i)
  1532. {
  1533. jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
  1534. const char* const fullPortName(ports[i]);
  1535. CARLA_ASSERT(jackPort != nullptr);
  1536. if (jackPort == nullptr)
  1537. continue;
  1538. const int thisPortId(getPortId(fullPortName));
  1539. if (thisPortId == -1)
  1540. continue;
  1541. if (const char** connections = jackbridge_port_get_connections(jackPort))
  1542. {
  1543. for (int j=0; connections[j] != nullptr; ++j)
  1544. {
  1545. const int targetPortId(getPortId(connections[j]));
  1546. ConnectionToId connectionToId(fLastConnectionId++, thisPortId, targetPortId);
  1547. fUsedConnections.append(connectionToId);
  1548. callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, connectionToId.id, thisPortId, targetPortId, nullptr);
  1549. }
  1550. jackbridge_free(connections);
  1551. }
  1552. }
  1553. jackbridge_free(ports);
  1554. }
  1555. }
  1556. #endif
  1557. // -------------------------------------
  1558. void processPlugin(CarlaPlugin* const plugin, const uint32_t nframes)
  1559. {
  1560. const uint32_t inCount(plugin->audioInCount());
  1561. const uint32_t outCount(plugin->audioOutCount());
  1562. float* inBuffer[inCount];
  1563. float* outBuffer[outCount];
  1564. float inPeaks[2] = { 0.0f };
  1565. float outPeaks[2] = { 0.0f };
  1566. for (uint32_t i=0; i < inCount; ++i)
  1567. {
  1568. CarlaEngineAudioPort* const port(CarlaPluginGetAudioInPort(plugin, i));
  1569. inBuffer[i] = port->getBuffer();
  1570. }
  1571. for (uint32_t i=0; i < outCount; ++i)
  1572. {
  1573. CarlaEngineAudioPort* const port(CarlaPluginGetAudioOutPort(plugin, i));
  1574. outBuffer[i] = port->getBuffer();
  1575. }
  1576. for (uint32_t i=0; i < inCount && i < 2; ++i)
  1577. {
  1578. for (uint32_t j=0; j < nframes; ++j)
  1579. {
  1580. const float absV(std::abs(inBuffer[i][j]));
  1581. if (absV > inPeaks[i])
  1582. inPeaks[i] = absV;
  1583. }
  1584. }
  1585. plugin->process(inBuffer, outBuffer, nframes);
  1586. for (uint32_t i=0; i < outCount && i < 2; ++i)
  1587. {
  1588. for (uint32_t j=0; j < nframes; ++j)
  1589. {
  1590. const float absV(std::abs(outBuffer[i][j]));
  1591. if (absV > outPeaks[i])
  1592. outPeaks[i] = absV;
  1593. }
  1594. }
  1595. setPeaks(plugin->id(), inPeaks, outPeaks);
  1596. }
  1597. #if 0
  1598. void latencyPlugin(CarlaPlugin* const plugin, jack_latency_callback_mode_t mode)
  1599. {
  1600. const uint32_t inCount = plugin->audioInCount();
  1601. const uint32_t outCount = plugin->audioOutCount();
  1602. jack_latency_range_t range;
  1603. uint32_t pluginLatency = plugin->latency();
  1604. if (pluginLatency == 0)
  1605. return;
  1606. if (mode == JackCaptureLatency)
  1607. {
  1608. for (uint32_t i=0; i < inCount; ++i)
  1609. {
  1610. uint32_t aOutI = (i >= outCount) ? outCount : i;
  1611. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, i))->kPort;
  1612. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, aOutI))->kPort;
  1613. jackbridge_port_get_latency_range(portIn, mode, &range);
  1614. range.min += pluginLatency;
  1615. range.max += pluginLatency;
  1616. jackbridge_port_set_latency_range(portOut, mode, &range);
  1617. }
  1618. }
  1619. else
  1620. {
  1621. for (uint32_t i=0; i < outCount; ++i)
  1622. {
  1623. uint32_t aInI = (i >= inCount) ? inCount : i;
  1624. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, aInI))->kPort;
  1625. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, i))->kPort;
  1626. jackbridge_port_get_latency_range(portOut, mode, &range);
  1627. range.min += pluginLatency;
  1628. range.max += pluginLatency;
  1629. jackbridge_port_set_latency_range(portIn, mode, &range);
  1630. }
  1631. }
  1632. }
  1633. #endif
  1634. // -------------------------------------
  1635. #define handlePtr ((CarlaEngineJack*)arg)
  1636. static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  1637. {
  1638. handlePtr->handleJackSampleRateCallback(newSampleRate);
  1639. return 0;
  1640. }
  1641. static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  1642. {
  1643. handlePtr->handleJackBufferSizeCallback(newBufferSize);
  1644. return 0;
  1645. }
  1646. static void carla_jack_freewheel_callback(int starting, void* arg)
  1647. {
  1648. handlePtr->handleJackFreewheelCallback(bool(starting));
  1649. }
  1650. static int carla_jack_process_callback(jack_nframes_t nframes, void* arg)
  1651. {
  1652. handlePtr->handleJackProcessCallback(nframes);
  1653. return 0;
  1654. }
  1655. #if 0
  1656. static void carla_jack_latency_callback(jack_latency_callback_mode_t mode, void* arg)
  1657. {
  1658. handlePtr->handleJackLatencyCallback(mode);
  1659. }
  1660. #endif
  1661. #ifndef BUILD_BRIDGE
  1662. static void carla_jack_client_registration_callback(const char* name, int reg, void* arg)
  1663. {
  1664. handlePtr->handleJackClientRegistrationCallback(name, (reg != 0));
  1665. }
  1666. static void carla_jack_port_registration_callback(jack_port_id_t port, int reg, void* arg)
  1667. {
  1668. handlePtr->handleJackPortRegistrationCallback(port, (reg != 0));
  1669. }
  1670. static void carla_jack_port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  1671. {
  1672. handlePtr->handleJackPortConnectCallback(a, b, (connect != 0));
  1673. }
  1674. static int carla_jack_port_rename_callback(jack_port_id_t port, const char* oldName, const char* newName, void* arg)
  1675. {
  1676. handlePtr->handleJackPortRenameCallback(port, oldName, newName);
  1677. return 0;
  1678. }
  1679. #endif
  1680. static void carla_jack_shutdown_callback(void* arg)
  1681. {
  1682. handlePtr->handleJackShutdownCallback();
  1683. }
  1684. #undef handlePtr
  1685. // -------------------------------------
  1686. #ifndef BUILD_BRIDGE
  1687. static int carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg)
  1688. {
  1689. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  1690. if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
  1691. {
  1692. CarlaEngineJack* const engine = (CarlaEngineJack*)CarlaPluginGetEngine(plugin);
  1693. plugin->initBuffers();
  1694. engine->saveTransportInfo();
  1695. engine->processPlugin(plugin, nframes);
  1696. plugin->unlock();
  1697. }
  1698. else
  1699. carla_stdout("Plugin not enabled or locked");
  1700. return 0;
  1701. }
  1702. # if 0
  1703. static void carla_jack_latency_callback_plugin(jack_latency_callback_mode_t mode, void* arg)
  1704. {
  1705. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  1706. if (plugin != nullptr && plugin->enabled())
  1707. {
  1708. CarlaEngineJack* const engine = (CarlaEngineJack*)CarlaPluginGetEngine(plugin);
  1709. engine->latencyPlugin(plugin, mode);
  1710. }
  1711. }
  1712. # endif
  1713. #endif
  1714. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
  1715. };
  1716. // -----------------------------------------
  1717. CarlaEngine* CarlaEngine::newJack()
  1718. {
  1719. return new CarlaEngineJack();
  1720. }
  1721. // -----------------------------------------
  1722. CARLA_BACKEND_END_NAMESPACE