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.

2446 lines
85KB

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