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.

2386 lines
79KB

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