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.

2295 lines
76KB

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