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.

4262 lines
149KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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 "CarlaEngineClient.hpp"
  18. #include "CarlaEngineInit.hpp"
  19. #include "CarlaEngineInternal.hpp"
  20. #include "CarlaPlugin.hpp"
  21. #include "CarlaBackendUtils.hpp"
  22. #include "CarlaEngineUtils.hpp"
  23. #include "CarlaMathUtils.hpp"
  24. #include "CarlaMIDI.h"
  25. #include "CarlaPatchbayUtils.hpp"
  26. #include "CarlaStringList.hpp"
  27. #include "jackey.h"
  28. #ifdef USING_JUCE
  29. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  30. # pragma GCC diagnostic push
  31. # pragma GCC diagnostic ignored "-Wconversion"
  32. # pragma GCC diagnostic ignored "-Weffc++"
  33. # pragma GCC diagnostic ignored "-Wsign-conversion"
  34. # pragma GCC diagnostic ignored "-Wundef"
  35. # endif
  36. # include "AppConfig.h"
  37. # include "juce_events/juce_events.h"
  38. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  39. # pragma GCC diagnostic pop
  40. # endif
  41. #endif
  42. #ifdef __SSE2_MATH__
  43. # include <xmmintrin.h>
  44. #endif
  45. // must be last
  46. #include "jackbridge/JackBridge.hpp"
  47. #define URI_CANVAS_ICON "http://kxstudio.sf.net/ns/canvas/icon"
  48. #define URI_MAIN_CLIENT_NAME "https://kx.studio/ns/carla/main-client-name"
  49. #define URI_POSITION "https://kx.studio/ns/carla/position"
  50. #define URI_PLUGIN_ICON "https://kx.studio/ns/carla/plugin-icon"
  51. #define URI_PLUGIN_ID "https://kx.studio/ns/carla/plugin-id"
  52. #define URI_TYPE_INTEGER "http://www.w3.org/2001/XMLSchema#integer"
  53. #define URI_TYPE_STRING "text/plain"
  54. CARLA_BACKEND_START_NAMESPACE
  55. class CarlaEngineJack;
  56. class CarlaEngineJackClient;
  57. struct CarlaJackPortHints {
  58. bool isHardware : 1;
  59. bool isInput : 1;
  60. bool isAudio : 1;
  61. bool isMIDI : 1;
  62. bool isCV : 1;
  63. bool isOSC : 1;
  64. static CarlaJackPortHints fromPort(const jack_port_t* const jackPort)
  65. {
  66. CarlaJackPortHints ph = { false, false, false, false, false, false };
  67. const int portFlags = jackbridge_port_flags(jackPort);
  68. const char* const portType = jackbridge_port_type(jackPort);
  69. ph.isHardware = portFlags & JackPortIsPhysical;
  70. ph.isInput = portFlags & JackPortIsInput;
  71. ph.isAudio = portType != nullptr && std::strcmp(portType, JACK_DEFAULT_AUDIO_TYPE) == 0;
  72. ph.isMIDI = portType != nullptr && std::strcmp(portType, JACK_DEFAULT_MIDI_TYPE) == 0;
  73. ph.isCV = false;
  74. ph.isOSC = false;
  75. if (const jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  76. {
  77. char* value = nullptr;
  78. char* type = nullptr;
  79. if (jackbridge_get_property(uuid, JACKEY_SIGNAL_TYPE, &value, &type)
  80. && value != nullptr
  81. && type != nullptr
  82. && std::strcmp(type, URI_TYPE_STRING) == 0)
  83. {
  84. ph.isCV = (std::strcmp(value, "CV") == 0);
  85. ph.isOSC = (std::strcmp(value, "OSC") == 0);
  86. jackbridge_free(value);
  87. jackbridge_free(type);
  88. }
  89. }
  90. return ph;
  91. }
  92. };
  93. // -----------------------------------------------------------------------
  94. // Fallback data
  95. static const GroupNameToId kGroupNameToIdFallback = { 0, { '\0' } };
  96. static const PortNameToId kPortNameToIdFallback = { 0, 0, { '\0' }, { '\0' } };
  97. #ifndef BUILD_BRIDGE
  98. static /* */ PortNameToId kPortNameToIdFallbackNC = { 0, 0, { '\0' }, { '\0' } };
  99. #endif
  100. static const ConnectionToId kConnectionToIdFallback = { 0, 0, 0, 0, 0 };
  101. static const EngineEvent kFallbackJackEngineEvent = { kEngineEventTypeNull, 0, 0, {{ kEngineControlEventTypeNull, 0, 0.0f }} };
  102. // -----------------------------------------------------------------------
  103. // Carla Engine Port removal helper
  104. class CarlaEngineJackAudioPort;
  105. class CarlaEngineJackCVPort;
  106. class CarlaEngineJackEventPort;
  107. struct JackPortDeletionCallback {
  108. virtual ~JackPortDeletionCallback() noexcept {}
  109. virtual void jackAudioPortDeleted(CarlaEngineJackAudioPort* const) noexcept = 0;
  110. virtual void jackCVPortDeleted(CarlaEngineJackCVPort* const) noexcept = 0;
  111. virtual void jackEventPortDeleted(CarlaEngineJackEventPort* const) noexcept = 0;
  112. };
  113. // -----------------------------------------------------------------------
  114. // Carla Engine JACK-Audio port
  115. class CarlaEngineJackAudioPort : public CarlaEngineAudioPort
  116. {
  117. public:
  118. 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
  119. : CarlaEngineAudioPort(client, isInputPort, indexOffset),
  120. fJackClient(jackClient),
  121. fJackPort(jackPort),
  122. kDeletionCallback(delCallback)
  123. {
  124. carla_debug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %p, %p)", bool2str(isInputPort), jackClient, jackPort);
  125. switch (kClient.getEngine().getProccessMode())
  126. {
  127. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  128. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  129. CARLA_SAFE_ASSERT_RETURN(jackClient != nullptr && jackPort != nullptr,);
  130. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  131. if (const jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  132. jackbridge_set_property(jackClient, uuid, JACKEY_SIGNAL_TYPE, "AUDIO", URI_TYPE_STRING);
  133. #endif
  134. break;
  135. default:
  136. CARLA_SAFE_ASSERT(jackClient == nullptr && jackPort == nullptr);
  137. break;
  138. }
  139. }
  140. ~CarlaEngineJackAudioPort() noexcept override
  141. {
  142. carla_debug("CarlaEngineJackAudioPort::~CarlaEngineJackAudioPort()");
  143. if (fJackClient != nullptr && fJackPort != nullptr)
  144. {
  145. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  146. try {
  147. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  148. jackbridge_remove_property(fJackClient, uuid, JACKEY_SIGNAL_TYPE);
  149. } CARLA_SAFE_EXCEPTION("Audio port remove meta type");
  150. #endif
  151. try {
  152. jackbridge_port_unregister(fJackClient, fJackPort);
  153. } CARLA_SAFE_EXCEPTION("Audio port unregister");
  154. fJackClient = nullptr;
  155. fJackPort = nullptr;
  156. }
  157. if (kDeletionCallback != nullptr)
  158. kDeletionCallback->jackAudioPortDeleted(this);
  159. }
  160. void initBuffer() noexcept override
  161. {
  162. if (fJackPort == nullptr)
  163. return CarlaEngineAudioPort::initBuffer();
  164. const uint32_t bufferSize(kClient.getEngine().getBufferSize());
  165. try {
  166. fBuffer = (float*)jackbridge_port_get_buffer(fJackPort, bufferSize);
  167. }
  168. catch(...) {
  169. fBuffer = nullptr;
  170. return;
  171. }
  172. if (! kIsInput)
  173. carla_zeroFloats(fBuffer, bufferSize);
  174. }
  175. void invalidate() noexcept
  176. {
  177. fJackClient = nullptr;
  178. fJackPort = nullptr;
  179. }
  180. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  181. void setMetaData(const char* const key, const char* const value, const char* const type) override
  182. {
  183. if (fJackPort == nullptr)
  184. return CarlaEngineJackAudioPort::setMetaData(key, value, type);
  185. try {
  186. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  187. jackbridge_set_property(fJackClient, uuid, key, value, type);
  188. } CARLA_SAFE_EXCEPTION("Port setMetaData");
  189. }
  190. #endif
  191. private:
  192. jack_client_t* fJackClient;
  193. jack_port_t* fJackPort;
  194. JackPortDeletionCallback* const kDeletionCallback;
  195. friend class CarlaEngineJackClient;
  196. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackAudioPort)
  197. };
  198. // -----------------------------------------------------------------------
  199. // Carla Engine JACK-CV port
  200. class CarlaEngineJackCVPort : public CarlaEngineCVPort
  201. {
  202. public:
  203. 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
  204. : CarlaEngineCVPort(client, isInputPort, indexOffset),
  205. fJackClient(jackClient),
  206. fJackPort(jackPort),
  207. kDeletionCallback(delCallback)
  208. {
  209. carla_debug("CarlaEngineJackCVPort::CarlaEngineJackCVPort(%s, %p, %p)", bool2str(isInputPort), jackClient, jackPort);
  210. switch (kClient.getEngine().getProccessMode())
  211. {
  212. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  213. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  214. CARLA_SAFE_ASSERT_RETURN(jackClient != nullptr && jackPort != nullptr,);
  215. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  216. if (const jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  217. jackbridge_set_property(jackClient, uuid, JACKEY_SIGNAL_TYPE, "CV", URI_TYPE_STRING);
  218. #endif
  219. break;
  220. default:
  221. CARLA_SAFE_ASSERT(jackClient == nullptr && jackPort == nullptr);
  222. break;
  223. }
  224. }
  225. ~CarlaEngineJackCVPort() noexcept override
  226. {
  227. carla_debug("CarlaEngineJackCVPort::~CarlaEngineJackCVPort()");
  228. if (fJackClient != nullptr && fJackPort != nullptr)
  229. {
  230. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  231. try {
  232. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  233. jackbridge_remove_property(fJackClient, uuid, JACKEY_SIGNAL_TYPE);
  234. } CARLA_SAFE_EXCEPTION("CV port remove meta type");
  235. #endif
  236. try {
  237. jackbridge_port_unregister(fJackClient, fJackPort);
  238. } CARLA_SAFE_EXCEPTION("CV port unregister");
  239. fJackClient = nullptr;
  240. fJackPort = nullptr;
  241. }
  242. if (kDeletionCallback != nullptr)
  243. kDeletionCallback->jackCVPortDeleted(this);
  244. }
  245. void initBuffer() noexcept override
  246. {
  247. if (fJackPort == nullptr)
  248. return CarlaEngineCVPort::initBuffer();
  249. const uint32_t bufferSize(kClient.getEngine().getBufferSize());
  250. try {
  251. fBuffer = (float*)jackbridge_port_get_buffer(fJackPort, bufferSize);
  252. }
  253. catch(...) {
  254. fBuffer = nullptr;
  255. return;
  256. }
  257. if (! kIsInput)
  258. carla_zeroFloats(fBuffer, bufferSize);
  259. }
  260. void invalidate() noexcept
  261. {
  262. fJackClient = nullptr;
  263. fJackPort = nullptr;
  264. }
  265. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  266. void setMetaData(const char* const key, const char* const value, const char* const type) override
  267. {
  268. if (fJackPort == nullptr)
  269. return CarlaEngineCVPort::setMetaData(key, value, type);
  270. try {
  271. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  272. jackbridge_set_property(fJackClient, uuid, key, value, type);
  273. } CARLA_SAFE_EXCEPTION("Port setMetaData");
  274. }
  275. #endif
  276. private:
  277. jack_client_t* fJackClient;
  278. jack_port_t* fJackPort;
  279. JackPortDeletionCallback* const kDeletionCallback;
  280. friend class CarlaEngineJackClient;
  281. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackCVPort)
  282. };
  283. // -----------------------------------------------------------------------
  284. // Carla Engine JACK-Event port
  285. class CarlaEngineJackEventPort : public CarlaEngineEventPort
  286. {
  287. public:
  288. 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
  289. : CarlaEngineEventPort(client, isInputPort, indexOffset),
  290. fJackClient(jackClient),
  291. fJackPort(jackPort),
  292. fJackBuffer(nullptr),
  293. fRetEvent(kFallbackJackEngineEvent),
  294. fCvSourceEvents(nullptr),
  295. fCvSourceEventCount(0),
  296. kDeletionCallback(delCallback)
  297. {
  298. carla_debug("CarlaEngineJackEventPort::CarlaEngineJackEventPort(%s, %p, %p)", bool2str(isInputPort), jackClient, jackPort);
  299. switch (kClient.getEngine().getProccessMode())
  300. {
  301. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  302. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  303. CARLA_SAFE_ASSERT_RETURN(jackClient != nullptr && jackPort != nullptr,);
  304. break;
  305. default:
  306. CARLA_SAFE_ASSERT(jackClient == nullptr && jackPort == nullptr);
  307. break;
  308. }
  309. }
  310. ~CarlaEngineJackEventPort() noexcept override
  311. {
  312. carla_debug("CarlaEngineJackEventPort::~CarlaEngineJackEventPort()");
  313. if (fJackClient != nullptr && fJackPort != nullptr)
  314. {
  315. try {
  316. jackbridge_port_unregister(fJackClient, fJackPort);
  317. } CARLA_SAFE_EXCEPTION("Event port unregister");
  318. fJackClient = nullptr;
  319. fJackPort = nullptr;
  320. }
  321. if (kDeletionCallback != nullptr)
  322. kDeletionCallback->jackEventPortDeleted(this);
  323. }
  324. void initBuffer() noexcept override
  325. {
  326. if (fJackPort == nullptr)
  327. return CarlaEngineEventPort::initBuffer();
  328. fCvSourceEvents = nullptr;
  329. fCvSourceEventCount = 0;
  330. try {
  331. fJackBuffer = jackbridge_port_get_buffer(fJackPort, kClient.getEngine().getBufferSize());
  332. }
  333. catch(...) {
  334. fJackBuffer = nullptr;
  335. return;
  336. }
  337. if (! kIsInput)
  338. jackbridge_midi_clear_buffer(fJackBuffer);
  339. }
  340. void setCvSourceEvents(EngineEvent* const events, const uint32_t eventCount) noexcept
  341. {
  342. fCvSourceEvents = events;
  343. fCvSourceEventCount = eventCount;
  344. }
  345. uint32_t getEventCount() const noexcept override
  346. {
  347. if (fJackPort == nullptr)
  348. return CarlaEngineEventPort::getEventCount();
  349. CARLA_SAFE_ASSERT_RETURN(kIsInput, 0);
  350. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, 0);
  351. try {
  352. return jackbridge_midi_get_event_count(fJackBuffer) + fCvSourceEventCount;
  353. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_get_event_count", 0);
  354. }
  355. const EngineEvent& getEvent(const uint32_t index) const noexcept override
  356. {
  357. if (fJackPort == nullptr)
  358. return CarlaEngineEventPort::getEvent(index);
  359. CARLA_SAFE_ASSERT_RETURN(kIsInput, kFallbackJackEngineEvent);
  360. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, kFallbackJackEngineEvent);
  361. return getEventUnchecked(index);
  362. }
  363. const EngineEvent& getEventUnchecked(uint32_t index) const noexcept override
  364. {
  365. if (index < fCvSourceEventCount)
  366. return fCvSourceEvents[index];
  367. index -= fCvSourceEventCount;
  368. jack_midi_event_t jackEvent;
  369. bool test = false;
  370. try {
  371. test = jackbridge_midi_event_get(&jackEvent, fJackBuffer, index);
  372. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_event_get", kFallbackJackEngineEvent);
  373. if (! test)
  374. return kFallbackJackEngineEvent;
  375. CARLA_SAFE_ASSERT_RETURN(jackEvent.size < 0xFF /* uint8_t max */, kFallbackJackEngineEvent);
  376. uint8_t port;
  377. if (kIndexOffset < 0xFF /* uint8_t max */)
  378. {
  379. port = static_cast<uint8_t>(kIndexOffset);
  380. }
  381. else
  382. {
  383. port = 0;
  384. carla_safe_assert_uint("kIndexOffset < 0xFF", __FILE__, __LINE__, kIndexOffset);
  385. }
  386. fRetEvent.time = jackEvent.time;
  387. fRetEvent.fillFromMidiData(static_cast<uint8_t>(jackEvent.size), jackEvent.buffer, port);
  388. return fRetEvent;
  389. }
  390. bool writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) noexcept override
  391. {
  392. if (fJackPort == nullptr)
  393. return CarlaEngineEventPort::writeControlEvent(time, channel, type, param, value);
  394. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  395. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, false);
  396. CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
  397. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  398. CARLA_SAFE_ASSERT_RETURN(param < MAX_MIDI_VALUE, false);
  399. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  400. if (type == kEngineControlEventTypeParameter) {
  401. CARLA_SAFE_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  402. }
  403. uint8_t data[3] = { 0, 0, 0 };
  404. EngineControlEvent ctrlEvent = { type, param, value };
  405. const uint8_t size = ctrlEvent.convertToMidiData(channel, data);
  406. if (size == 0)
  407. return false;
  408. try {
  409. return jackbridge_midi_event_write(fJackBuffer, time, data, size);
  410. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_event_write", false);
  411. }
  412. bool writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t size, const uint8_t* const data) noexcept override
  413. {
  414. if (fJackPort == nullptr)
  415. return CarlaEngineEventPort::writeMidiEvent(time, channel, size, data);
  416. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  417. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, false);
  418. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  419. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  420. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  421. jack_midi_data_t jdata[size];
  422. jdata[0] = static_cast<jack_midi_data_t>(MIDI_GET_STATUS_FROM_DATA(data) + channel);
  423. for (uint8_t i=1; i < size; ++i)
  424. jdata[i] = data[i];
  425. try {
  426. return jackbridge_midi_event_write(fJackBuffer, time, jdata, size);
  427. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_event_write", false);
  428. }
  429. void invalidate() noexcept
  430. {
  431. fJackClient = nullptr;
  432. fJackPort = nullptr;
  433. }
  434. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  435. void setMetaData(const char* const key, const char* const value, const char* const type) override
  436. {
  437. if (fJackPort == nullptr)
  438. return CarlaEngineJackEventPort::setMetaData(key, value, type);
  439. try {
  440. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  441. jackbridge_set_property(fJackClient, uuid, key, value, type);
  442. } CARLA_SAFE_EXCEPTION("Port setMetaData");
  443. }
  444. #endif
  445. private:
  446. jack_client_t* fJackClient;
  447. jack_port_t* fJackPort;
  448. void* fJackBuffer;
  449. mutable EngineEvent fRetEvent;
  450. EngineEvent* fCvSourceEvents;
  451. uint32_t fCvSourceEventCount;
  452. JackPortDeletionCallback* const kDeletionCallback;
  453. friend class CarlaEngineJackClient;
  454. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackEventPort)
  455. };
  456. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  457. // -----------------------------------------------------------------------
  458. // Jack Engine CV source ports
  459. class CarlaEngineJackCVSourcePorts : public CarlaEngineCVSourcePorts
  460. {
  461. public:
  462. CarlaEngineJackCVSourcePorts(const bool useClient)
  463. : CarlaEngineCVSourcePorts(),
  464. fUseClient(useClient),
  465. fBuffer(nullptr),
  466. fBufferToDeleteLater(nullptr)
  467. {}
  468. ~CarlaEngineJackCVSourcePorts() override
  469. {
  470. if (fBufferToDeleteLater != nullptr)
  471. {
  472. delete[] fBufferToDeleteLater;
  473. fBufferToDeleteLater = nullptr;
  474. }
  475. }
  476. bool addCVSource(CarlaEngineCVPort* const port, const uint32_t portIndexOffset) override
  477. {
  478. if (! fUseClient)
  479. return CarlaEngineCVSourcePorts::addCVSource(port, portIndexOffset);
  480. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  481. if (! CarlaEngineCVSourcePorts::addCVSource(port, portIndexOffset))
  482. return false;
  483. if (pData->cvs.size() == 1 && fBuffer == nullptr)
  484. {
  485. EngineEvent* const buffer = new EngineEvent[kMaxEngineEventInternalCount];
  486. carla_zeroStructs(buffer, kMaxEngineEventInternalCount);
  487. fBuffer = buffer;
  488. }
  489. return true;
  490. }
  491. bool removeCVSource(const uint32_t portIndexOffset) override
  492. {
  493. if (! fUseClient)
  494. return CarlaEngineCVSourcePorts::removeCVSource(portIndexOffset);
  495. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  496. if (! CarlaEngineCVSourcePorts::removeCVSource(portIndexOffset))
  497. return false;
  498. if (pData->cvs.size() == 0 && fBuffer != nullptr)
  499. {
  500. if (fBufferToDeleteLater != nullptr)
  501. delete[] fBufferToDeleteLater;
  502. fBufferToDeleteLater = fBuffer;
  503. fBuffer = nullptr;
  504. }
  505. return true;
  506. }
  507. void initPortBuffers(const float* const* const buffers,
  508. const uint32_t frames,
  509. const bool sampleAccurate,
  510. CarlaEngineEventPort* const eventPort) override
  511. {
  512. if (! fUseClient)
  513. return CarlaEngineCVSourcePorts::initPortBuffers(buffers, frames, sampleAccurate, eventPort);
  514. CARLA_SAFE_ASSERT_RETURN(buffers != nullptr,);
  515. CARLA_SAFE_ASSERT_RETURN(eventPort != nullptr,);
  516. const CarlaRecursiveMutexTryLocker crmtl(pData->rmutex);
  517. if (! crmtl.wasLocked())
  518. return;
  519. const int numCVs = pData->cvs.size();
  520. if (numCVs == 0)
  521. return;
  522. EngineEvent* const buffer = fBuffer;
  523. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  524. uint32_t eventCount = 0;
  525. float v, min, max;
  526. for (int i = 0; i < numCVs && eventCount < kMaxEngineEventInternalCount; ++i)
  527. {
  528. CarlaEngineEventCV& ecv(pData->cvs.getReference(i));
  529. CARLA_SAFE_ASSERT_CONTINUE(ecv.cvPort != nullptr);
  530. CARLA_SAFE_ASSERT_CONTINUE(buffers[i] != nullptr);
  531. float previousValue = ecv.previousValue;
  532. ecv.cvPort->getRange(min, max);
  533. v = buffers[i][0];
  534. if (carla_isNotEqual(v, previousValue))
  535. {
  536. previousValue = v;
  537. EngineEvent& event(buffer[eventCount++]);
  538. event.type = kEngineEventTypeControl;
  539. event.time = 0;
  540. event.channel = kEngineEventNonMidiChannel;
  541. event.ctrl.type = kEngineControlEventTypeParameter;
  542. event.ctrl.param = static_cast<uint16_t>(ecv.indexOffset);
  543. event.ctrl.value = carla_fixedValue(0.0f, 1.0f, (v - min) / (max - min));
  544. }
  545. ecv.previousValue = previousValue;
  546. }
  547. if (eventCount != 0)
  548. if (CarlaEngineJackEventPort* const jackEventPort = dynamic_cast<CarlaEngineJackEventPort*>(eventPort))
  549. jackEventPort->setCvSourceEvents(buffer, eventCount);
  550. }
  551. CarlaRecursiveMutex& getMutex() const noexcept
  552. {
  553. return pData->rmutex;
  554. }
  555. uint32_t getPortCount() const noexcept
  556. {
  557. return static_cast<uint32_t>(pData->cvs.size());
  558. }
  559. CarlaEngineCVPort* getPort(const uint32_t portIndexOffset) const
  560. {
  561. const int ioffset = static_cast<int>(portIndexOffset);
  562. return pData->cvs[ioffset].cvPort;
  563. }
  564. void setGraphAndPlugin(PatchbayGraph* const graph, CarlaPlugin* const plugin) noexcept
  565. {
  566. pData->graph = graph;
  567. pData->plugin = plugin;
  568. }
  569. private:
  570. const bool fUseClient;
  571. EngineEvent* fBuffer;
  572. EngineEvent* fBufferToDeleteLater;
  573. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineJackCVSourcePorts)
  574. };
  575. #endif
  576. // -----------------------------------------------------------------------
  577. // Jack Engine client
  578. class CarlaEngineJackClient : public CarlaEngineClientForSubclassing,
  579. private JackPortDeletionCallback
  580. {
  581. public:
  582. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  583. CarlaEngineJackClient(const CarlaEngine& engine,
  584. EngineInternalGraph& egraph,
  585. CarlaPlugin* const plugin,
  586. const CarlaString& mainClientName,
  587. jack_client_t* const jackClient)
  588. : CarlaEngineClientForSubclassing(engine, egraph, plugin),
  589. #else
  590. CarlaEngineJackClient(const CarlaEngine& engine,
  591. const CarlaString& mainClientName,
  592. jack_client_t* const jackClient)
  593. : CarlaEngineClientForSubclassing(engine),
  594. #endif
  595. fJackClient(jackClient),
  596. fUseClient(engine.getProccessMode() == ENGINE_PROCESS_MODE_SINGLE_CLIENT ||
  597. engine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
  598. fAudioPorts(),
  599. fCVPorts(),
  600. fEventPorts(),
  601. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  602. fCVSourcePorts(fUseClient),
  603. #endif
  604. fMainClientName(mainClientName),
  605. fPreRenameMutex(),
  606. fPreRenameConnections(),
  607. fPreRenamePluginId(),
  608. fPreRenamePluginIcon()
  609. {
  610. carla_debug("CarlaEngineJackClient::CarlaEngineJackClient(%p)", jackClient);
  611. if (fUseClient)
  612. {
  613. CARLA_SAFE_ASSERT(jackClient != nullptr);
  614. }
  615. else
  616. {
  617. CARLA_SAFE_ASSERT(jackClient == nullptr);
  618. }
  619. }
  620. ~CarlaEngineJackClient() noexcept override
  621. {
  622. carla_debug("CarlaEngineJackClient::~CarlaEngineJackClient()");
  623. if (getProcessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS && fJackClient != nullptr) // FIXME
  624. jackbridge_client_close(fJackClient);
  625. // ports must have been deleted by now!
  626. //fAudioPorts.clear();
  627. //fCVPorts.clear();
  628. //fEventPorts.clear();
  629. }
  630. void activate() noexcept override
  631. {
  632. carla_debug("CarlaEngineJackClient::activate()");
  633. if (getProcessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  634. {
  635. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr && ! isActive(),);
  636. try {
  637. jackbridge_activate(fJackClient);
  638. } catch(...) {}
  639. }
  640. CarlaEngineClient::activate();
  641. const CarlaMutexLocker cml(fPreRenameMutex);
  642. if (fJackClient != nullptr)
  643. {
  644. // restore pre-rename connections
  645. const char* portNameA = nullptr;
  646. const char* portNameB = nullptr;
  647. bool doConnection = false;
  648. for (CarlaStringList::Itenerator it = fPreRenameConnections.begin2(); it.valid(); it.next())
  649. {
  650. const bool connectNow = doConnection;
  651. doConnection = !doConnection;
  652. if (connectNow)
  653. portNameB = it.getValue(nullptr);
  654. else
  655. portNameA = it.getValue(nullptr);
  656. if (! connectNow)
  657. continue;
  658. CARLA_SAFE_ASSERT_CONTINUE(portNameA != nullptr && portNameA[0] != '\0');
  659. CARLA_SAFE_ASSERT_CONTINUE(portNameB != nullptr && portNameB[0] != '\0');
  660. jackbridge_connect(fJackClient, portNameA, portNameB);
  661. }
  662. if (fPreRenamePluginId.isNotEmpty())
  663. {
  664. if (char* const uuidstr = jackbridge_client_get_uuid(fJackClient))
  665. {
  666. jack_uuid_t uuid;
  667. if (jackbridge_uuid_parse(uuidstr, &uuid))
  668. {
  669. jackbridge_set_property(fJackClient, uuid,
  670. URI_MAIN_CLIENT_NAME,
  671. fMainClientName,
  672. URI_TYPE_STRING);
  673. jackbridge_set_property(fJackClient, uuid,
  674. URI_PLUGIN_ID,
  675. fPreRenamePluginId,
  676. URI_TYPE_INTEGER);
  677. if (fPreRenamePluginIcon.isNotEmpty())
  678. jackbridge_set_property(fJackClient, uuid,
  679. URI_PLUGIN_ICON,
  680. fPreRenamePluginIcon,
  681. URI_TYPE_STRING);
  682. }
  683. jackbridge_free(uuidstr);
  684. }
  685. }
  686. }
  687. fPreRenameConnections.clear();
  688. fPreRenamePluginId.clear();
  689. fPreRenamePluginIcon.clear();
  690. }
  691. void deactivate() noexcept override
  692. {
  693. carla_debug("CarlaEngineJackClient::deactivate()");
  694. if (getProcessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  695. {
  696. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr && isActive(),);
  697. try {
  698. jackbridge_deactivate(fJackClient);
  699. } catch(...) {}
  700. }
  701. CarlaEngineClient::deactivate();
  702. }
  703. bool isOk() const noexcept override
  704. {
  705. if (fUseClient)
  706. return (fJackClient != nullptr);
  707. return CarlaEngineClient::isOk();
  708. }
  709. CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput, const uint32_t indexOffset) override
  710. {
  711. carla_debug("CarlaEngineJackClient::addPort(%i:%s, \"%s\", %s)", portType, EnginePortType2Str(portType), name, bool2str(isInput));
  712. jack_port_t* jackPort = nullptr;
  713. const char* realName = name;
  714. // Create JACK port first, if needed
  715. if (fUseClient)
  716. {
  717. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr, nullptr);
  718. realName = pData->getUniquePortName(name);
  719. switch (portType)
  720. {
  721. case kEnginePortTypeNull:
  722. break;
  723. case kEnginePortTypeAudio:
  724. jackPort = jackbridge_port_register(fJackClient, realName, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  725. break;
  726. case kEnginePortTypeCV:
  727. jackPort = jackbridge_port_register(fJackClient, realName, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  728. break;
  729. case kEnginePortTypeEvent:
  730. jackPort = jackbridge_port_register(fJackClient, realName, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  731. break;
  732. }
  733. CARLA_SAFE_ASSERT_RETURN(jackPort != nullptr, nullptr);
  734. }
  735. // Create Engine port
  736. switch (portType)
  737. {
  738. case kEnginePortTypeNull:
  739. break;
  740. case kEnginePortTypeAudio: {
  741. pData->addAudioPortName(isInput, realName);
  742. if (realName != name) delete[] realName;
  743. CarlaEngineJackAudioPort* const enginePort(new CarlaEngineJackAudioPort(*this, isInput, indexOffset, fJackClient, jackPort, this));
  744. fAudioPorts.append(enginePort);
  745. return enginePort;
  746. }
  747. case kEnginePortTypeCV: {
  748. pData->addCVPortName(isInput, realName);
  749. if (realName != name) delete[] realName;
  750. CarlaEngineJackCVPort* const enginePort(new CarlaEngineJackCVPort(*this, isInput, indexOffset, fJackClient, jackPort, this));
  751. fCVPorts.append(enginePort);
  752. return enginePort;
  753. }
  754. case kEnginePortTypeEvent: {
  755. pData->addEventPortName(isInput, realName);
  756. if (realName != name) delete[] realName;
  757. CarlaEngineJackEventPort* const enginePort(new CarlaEngineJackEventPort(*this, isInput, indexOffset, fJackClient, jackPort, this));
  758. fEventPorts.append(enginePort);
  759. return enginePort;
  760. }
  761. }
  762. carla_stderr("CarlaEngineJackClient::addPort(%i, \"%s\", %s) - invalid type", portType, name, bool2str(isInput));
  763. return nullptr;
  764. }
  765. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  766. CarlaEngineCVSourcePorts* createCVSourcePorts() override
  767. {
  768. fCVSourcePorts.setGraphAndPlugin(getPatchbayGraphOrNull(), getPlugin());
  769. return &fCVSourcePorts;
  770. }
  771. CarlaEngineJackCVSourcePorts& getCVSourcePorts() noexcept
  772. {
  773. return fCVSourcePorts;
  774. }
  775. #endif
  776. void invalidate() noexcept
  777. {
  778. for (LinkedList<CarlaEngineJackAudioPort*>::Itenerator it = fAudioPorts.begin2(); it.valid(); it.next())
  779. {
  780. CarlaEngineJackAudioPort* const port(it.getValue(nullptr));
  781. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  782. port->invalidate();
  783. }
  784. for (LinkedList<CarlaEngineJackCVPort*>::Itenerator it = fCVPorts.begin2(); it.valid(); it.next())
  785. {
  786. CarlaEngineJackCVPort* const port(it.getValue(nullptr));
  787. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  788. port->invalidate();
  789. }
  790. for (LinkedList<CarlaEngineJackEventPort*>::Itenerator it = fEventPorts.begin2(); it.valid(); it.next())
  791. {
  792. CarlaEngineJackEventPort* const port(it.getValue(nullptr));
  793. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  794. port->invalidate();
  795. }
  796. fJackClient = nullptr;
  797. CarlaEngineClient::deactivate();
  798. }
  799. const char* getJackClientName() const noexcept
  800. {
  801. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr, nullptr);
  802. try {
  803. return jackbridge_get_client_name(fJackClient);
  804. } CARLA_SAFE_EXCEPTION_RETURN("jack_get_client_name", nullptr);
  805. }
  806. void jackAudioPortDeleted(CarlaEngineJackAudioPort* const port) noexcept override
  807. {
  808. fAudioPorts.removeAll(port);
  809. }
  810. void jackCVPortDeleted(CarlaEngineJackCVPort* const port) noexcept override
  811. {
  812. fCVPorts.removeAll(port);
  813. }
  814. void jackEventPortDeleted(CarlaEngineJackEventPort* const port) noexcept override
  815. {
  816. fEventPorts.removeAll(port);
  817. }
  818. bool renameInSingleClient(const CarlaString& newClientName)
  819. {
  820. const CarlaString clientNamePrefix(newClientName + ":");
  821. return _renamePorts(fAudioPorts, clientNamePrefix) &&
  822. _renamePorts(fCVPorts, clientNamePrefix) &&
  823. _renamePorts(fEventPorts, clientNamePrefix);
  824. }
  825. void closeForRename(jack_client_t* const newClient, const CarlaString& newClientName) noexcept
  826. {
  827. if (fJackClient != nullptr)
  828. {
  829. if (isActive())
  830. {
  831. {
  832. const CarlaString clientNamePrefix(newClientName + ":");
  833. // store current client connections
  834. const CarlaMutexLocker cml(fPreRenameMutex);
  835. fPreRenameConnections.clear();
  836. fPreRenamePluginId.clear();
  837. fPreRenamePluginIcon.clear();
  838. _savePortsConnections(fAudioPorts, clientNamePrefix);
  839. _savePortsConnections(fCVPorts, clientNamePrefix);
  840. _savePortsConnections(fEventPorts, clientNamePrefix);
  841. _saveProperties();
  842. }
  843. try {
  844. jackbridge_deactivate(fJackClient);
  845. } catch(...) {}
  846. }
  847. try {
  848. jackbridge_client_close(fJackClient);
  849. } catch(...) {}
  850. invalidate();
  851. }
  852. fAudioPorts.clear();
  853. fCVPorts.clear();
  854. fEventPorts.clear();
  855. pData->clearPorts();
  856. fJackClient = newClient;
  857. }
  858. void setNewPluginId(const uint id) const
  859. {
  860. if (char* const uuidstr = jackbridge_client_get_uuid(fJackClient))
  861. {
  862. jack_uuid_t uuid;
  863. if (jackbridge_uuid_parse(uuidstr, &uuid))
  864. {
  865. char buf[32];
  866. std::snprintf(buf, 31, "%u", id);
  867. buf[31] = '\0';
  868. jackbridge_set_property(fJackClient, uuid,
  869. URI_PLUGIN_ID,
  870. buf,
  871. URI_TYPE_INTEGER);
  872. }
  873. jackbridge_free(uuidstr);
  874. }
  875. }
  876. private:
  877. jack_client_t* fJackClient;
  878. const bool fUseClient;
  879. LinkedList<CarlaEngineJackAudioPort*> fAudioPorts;
  880. LinkedList<CarlaEngineJackCVPort*> fCVPorts;
  881. LinkedList<CarlaEngineJackEventPort*> fEventPorts;
  882. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  883. CarlaEngineJackCVSourcePorts fCVSourcePorts;
  884. #endif
  885. const CarlaString& fMainClientName;
  886. CarlaMutex fPreRenameMutex;
  887. CarlaStringList fPreRenameConnections;
  888. CarlaString fPreRenamePluginId;
  889. CarlaString fPreRenamePluginIcon;
  890. template<typename T>
  891. bool _renamePorts(const LinkedList<T*>& t, const CarlaString& clientNamePrefix)
  892. {
  893. for (typename LinkedList<T*>::Itenerator it = t.begin2(); it.valid(); it.next())
  894. {
  895. T* const port(it.getValue(nullptr));
  896. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  897. CARLA_SAFE_ASSERT_CONTINUE(port->fJackPort != nullptr);
  898. const char* shortPortName(jackbridge_port_short_name(port->fJackPort));
  899. CARLA_SAFE_ASSERT_CONTINUE(shortPortName != nullptr && shortPortName[0] != '\0');
  900. const char* const oldClientNameSep(std::strstr(shortPortName, ":"));
  901. CARLA_SAFE_ASSERT_CONTINUE(oldClientNameSep != nullptr && oldClientNameSep[0] != '\0' && oldClientNameSep[1] != '\0');
  902. shortPortName += oldClientNameSep-shortPortName + 1;
  903. const CarlaString newPortName(clientNamePrefix + shortPortName);
  904. if (! jackbridge_port_rename(fJackClient, port->fJackPort, newPortName))
  905. return false;
  906. }
  907. return true;
  908. }
  909. template<typename T>
  910. void _savePortsConnections(const LinkedList<T*>& t, const CarlaString& clientNamePrefix)
  911. {
  912. for (typename LinkedList<T*>::Itenerator it = t.begin2(); it.valid(); it.next())
  913. {
  914. T* const port(it.getValue(nullptr));
  915. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  916. CARLA_SAFE_ASSERT_CONTINUE(port->fJackPort != nullptr);
  917. const char* const shortPortName(jackbridge_port_short_name(port->fJackPort));
  918. CARLA_SAFE_ASSERT_CONTINUE(shortPortName != nullptr && shortPortName[0] != '\0');
  919. const CarlaString portName(clientNamePrefix + shortPortName);
  920. if (const char** const connections = jackbridge_port_get_all_connections(fJackClient, port->fJackPort))
  921. {
  922. for (int i=0; connections[i] != nullptr; ++i)
  923. {
  924. if (port->kIsInput)
  925. {
  926. fPreRenameConnections.append(connections[i]);
  927. fPreRenameConnections.append(portName);
  928. }
  929. else
  930. {
  931. fPreRenameConnections.append(portName);
  932. fPreRenameConnections.append(connections[i]);
  933. }
  934. }
  935. jackbridge_free(connections);
  936. }
  937. }
  938. }
  939. void _saveProperties()
  940. {
  941. if (char* const uuidstr = jackbridge_client_get_uuid(fJackClient))
  942. {
  943. jack_uuid_t uuid;
  944. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  945. jackbridge_free(uuidstr);
  946. if (parsed)
  947. {
  948. char* value = nullptr;
  949. char* type = nullptr;
  950. CARLA_SAFE_ASSERT_RETURN(jackbridge_get_property(uuid,
  951. URI_PLUGIN_ID,
  952. &value,
  953. &type),);
  954. CARLA_SAFE_ASSERT_RETURN(type != nullptr,);
  955. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_INTEGER) == 0,);
  956. fPreRenamePluginId = value;
  957. jackbridge_free(value);
  958. jackbridge_free(type);
  959. value = type = nullptr;
  960. if (jackbridge_get_property(uuid, URI_PLUGIN_ICON, &value, &type))
  961. {
  962. CARLA_SAFE_ASSERT_RETURN(type != nullptr,);
  963. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_STRING) == 0,);
  964. fPreRenamePluginIcon = value;
  965. jackbridge_free(value);
  966. jackbridge_free(type);
  967. }
  968. }
  969. }
  970. }
  971. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackClient)
  972. };
  973. // -----------------------------------------------------------------------
  974. // Jack Engine
  975. class CarlaEngineJack : public CarlaEngine
  976. #ifndef BUILD_BRIDGE
  977. , private CarlaThread
  978. #endif
  979. {
  980. public:
  981. CarlaEngineJack()
  982. : CarlaEngine(),
  983. #ifndef BUILD_BRIDGE
  984. CarlaThread("CarlaEngineJackCallbacks"),
  985. #endif
  986. fClient(nullptr),
  987. fExternalPatchbayHost(true),
  988. fExternalPatchbayOsc(true),
  989. fFreewheel(false),
  990. fClientName(),
  991. #ifdef BUILD_BRIDGE
  992. fIsRunning(false)
  993. #else
  994. fTimebaseMaster(false),
  995. fTimebaseRolling(false),
  996. fTimebaseUsecs(0),
  997. fUsedGroups(),
  998. fUsedPorts(),
  999. fUsedConnections(),
  1000. fPatchbayProcThreadProtectionMutex(),
  1001. fRetConns(),
  1002. fPostPonedEvents(),
  1003. fPostPonedEventsMutex(),
  1004. fPostPonedUUIDs(),
  1005. fPostPonedUUIDsMutex(),
  1006. fIsInternalClient(false)
  1007. #endif
  1008. {
  1009. carla_debug("CarlaEngineJack::CarlaEngineJack()");
  1010. #ifdef BUILD_BRIDGE
  1011. pData->options.processMode = ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS;
  1012. #else
  1013. carla_zeroPointers(fRackPorts, kRackPortCount);
  1014. #endif
  1015. }
  1016. ~CarlaEngineJack() noexcept override
  1017. {
  1018. carla_debug("CarlaEngineJack::~CarlaEngineJack()");
  1019. CARLA_SAFE_ASSERT(fClient == nullptr);
  1020. #ifndef BUILD_BRIDGE
  1021. fUsedGroups.clear();
  1022. fUsedPorts.clear();
  1023. fUsedConnections.clear();
  1024. CARLA_SAFE_ASSERT(fPostPonedEvents.count() == 0);
  1025. #endif
  1026. }
  1027. // -------------------------------------------------------------------
  1028. // Maximum values
  1029. uint getMaxClientNameSize() const noexcept override
  1030. {
  1031. #ifndef BUILD_BRIDGE
  1032. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT || pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1033. #endif
  1034. {
  1035. try {
  1036. return static_cast<uint>(jackbridge_client_name_size()-1);
  1037. } CARLA_SAFE_EXCEPTION_RETURN("jack_client_name_size", 32);
  1038. }
  1039. return CarlaEngine::getMaxClientNameSize();
  1040. }
  1041. uint getMaxPortNameSize() const noexcept override
  1042. {
  1043. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT || pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1044. {
  1045. try {
  1046. return static_cast<uint>(jackbridge_port_name_size()-1);
  1047. } CARLA_SAFE_EXCEPTION_RETURN("jack_port_name_size", 255);
  1048. }
  1049. return CarlaEngine::getMaxPortNameSize();
  1050. }
  1051. // -------------------------------------------------------------------
  1052. // Virtual, per-engine type calls
  1053. bool init(const char* const clientName) override
  1054. {
  1055. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr || (clientName != nullptr && clientName[0] != '\0'), false);
  1056. CARLA_SAFE_ASSERT_RETURN(jackbridge_is_ok(), false);
  1057. carla_debug("CarlaEngineJack::init(\"%s\")", clientName);
  1058. fFreewheel = false;
  1059. fExternalPatchbayHost = true;
  1060. fExternalPatchbayOsc = true;
  1061. CarlaString truncatedClientName;
  1062. if (fClient == nullptr && clientName != nullptr)
  1063. {
  1064. truncatedClientName = clientName;
  1065. truncatedClientName.truncate(getMaxClientNameSize());
  1066. }
  1067. #ifdef BUILD_BRIDGE
  1068. fIsRunning = true;
  1069. if (! pData->init(truncatedClientName))
  1070. {
  1071. close();
  1072. setLastError("Failed to init internal data");
  1073. return false;
  1074. }
  1075. if (pData->bufferSize == 0 || carla_isEqual(pData->sampleRate, 0.0))
  1076. {
  1077. // open temp client to get initial buffer-size and sample-rate values
  1078. if (jack_client_t* const tmpClient = jackbridge_client_open(truncatedClientName, JackNoStartServer, nullptr))
  1079. {
  1080. pData->bufferSize = jackbridge_get_buffer_size(tmpClient);
  1081. pData->sampleRate = jackbridge_get_sample_rate(tmpClient);
  1082. jackbridge_client_close(tmpClient);
  1083. }
  1084. else
  1085. {
  1086. close();
  1087. setLastError("Failed to init temporary jack client");
  1088. return false;
  1089. }
  1090. }
  1091. return true;
  1092. #else
  1093. if (fClient == nullptr && clientName != nullptr)
  1094. fClient = jackbridge_client_open(truncatedClientName, JackNoStartServer, nullptr);
  1095. if (fClient == nullptr)
  1096. {
  1097. setLastError("Failed to create new JACK client");
  1098. return false;
  1099. }
  1100. const char* const jackClientName = jackbridge_get_client_name(fClient);
  1101. if (! pData->init(jackClientName))
  1102. {
  1103. jackbridge_client_close(fClient);
  1104. fClient = nullptr;
  1105. setLastError("Failed to init internal data");
  1106. return false;
  1107. }
  1108. fClientName = jackClientName;
  1109. const EngineOptions& opts(pData->options);
  1110. pData->bufferSize = jackbridge_get_buffer_size(fClient);
  1111. pData->sampleRate = jackbridge_get_sample_rate(fClient);
  1112. pData->initTime(opts.transportExtra);
  1113. jackbridge_set_thread_init_callback(fClient, carla_jack_thread_init_callback, nullptr);
  1114. jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback, this);
  1115. jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback, this);
  1116. jackbridge_set_freewheel_callback(fClient, carla_jack_freewheel_callback, this);
  1117. jackbridge_set_latency_callback(fClient, carla_jack_latency_callback, this);
  1118. jackbridge_set_process_callback(fClient, carla_jack_process_callback, this);
  1119. jackbridge_on_shutdown(fClient, carla_jack_shutdown_callback, this);
  1120. fTimebaseRolling = false;
  1121. if (opts.transportMode == ENGINE_TRANSPORT_MODE_JACK)
  1122. fTimebaseMaster = jackbridge_set_timebase_callback(fClient, true, carla_jack_timebase_callback, this);
  1123. else
  1124. fTimebaseMaster = false;
  1125. if (opts.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1126. initJackPatchbay(true, false, jackClientName);
  1127. jackbridge_set_client_registration_callback(fClient, carla_jack_client_registration_callback, this);
  1128. jackbridge_set_port_registration_callback(fClient, carla_jack_port_registration_callback, this);
  1129. jackbridge_set_port_connect_callback(fClient, carla_jack_port_connect_callback, this);
  1130. jackbridge_set_port_rename_callback(fClient, carla_jack_port_rename_callback, this);
  1131. jackbridge_set_property_change_callback(fClient, carla_jack_property_change_callback, this);
  1132. jackbridge_set_xrun_callback(fClient, carla_jack_xrun_callback, this);
  1133. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || opts.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1134. {
  1135. fRackPorts[kRackPortAudioIn1] = jackbridge_port_register(fClient, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  1136. fRackPorts[kRackPortAudioIn2] = jackbridge_port_register(fClient, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  1137. fRackPorts[kRackPortAudioOut1] = jackbridge_port_register(fClient, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  1138. fRackPorts[kRackPortAudioOut2] = jackbridge_port_register(fClient, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  1139. fRackPorts[kRackPortEventIn] = jackbridge_port_register(fClient, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  1140. fRackPorts[kRackPortEventOut] = jackbridge_port_register(fClient, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  1141. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1142. {
  1143. // FIXME?
  1144. pData->graph.create(0, 0, 0, 0);
  1145. }
  1146. else
  1147. {
  1148. pData->graph.create(2, 2, 0, 0);
  1149. // pData->graph.setUsingExternalHost(true);
  1150. // pData->graph.setUsingExternalOSC(true);
  1151. patchbayRefresh(true, false, false);
  1152. }
  1153. }
  1154. if (char* const uuidstr = jackbridge_client_get_uuid(fClient))
  1155. {
  1156. jack_uuid_t uuid;
  1157. if (jackbridge_uuid_parse(uuidstr, &uuid))
  1158. {
  1159. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  1160. const CarlaString& tcp(pData->osc.getServerPathTCP());
  1161. const CarlaString& udp(pData->osc.getServerPathUDP());
  1162. if (tcp.isNotEmpty())
  1163. jackbridge_set_property(fClient, uuid,
  1164. "https://kx.studio/ns/carla/osc-tcp", tcp, URI_TYPE_STRING);
  1165. if (tcp.isNotEmpty())
  1166. jackbridge_set_property(fClient, uuid,
  1167. "https://kx.studio/ns/carla/osc-udp", udp, URI_TYPE_STRING);
  1168. #endif
  1169. }
  1170. jackbridge_free(uuidstr);
  1171. }
  1172. if (jackbridge_activate(fClient))
  1173. {
  1174. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1175. opts.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1176. {
  1177. if (pData->options.audioDevice != nullptr &&
  1178. std::strcmp(pData->options.audioDevice, "Auto-Connect ON") == 0 &&
  1179. std::getenv("LADISH_APP_NAME") == nullptr &&
  1180. std::getenv("NSM_URL") == nullptr)
  1181. {
  1182. char strBuf[STR_MAX];
  1183. if (jackbridge_port_by_name(fClient, "system:capture_1") != nullptr)
  1184. {
  1185. std::snprintf(strBuf, STR_MAX, "%s:audio-in1", jackClientName);
  1186. strBuf[STR_MAX-1] = '\0';
  1187. jackbridge_connect(fClient, "system:capture_1", strBuf);
  1188. std::snprintf(strBuf, STR_MAX, "%s:audio-in2", jackClientName);
  1189. strBuf[STR_MAX-1] = '\0';
  1190. if (jackbridge_port_by_name(fClient, "system:capture_2") != nullptr)
  1191. jackbridge_connect(fClient, "system:capture_2", strBuf);
  1192. else
  1193. jackbridge_connect(fClient, "system:capture_1", strBuf);
  1194. }
  1195. if (jackbridge_port_by_name(fClient, "system:playback_1") != nullptr)
  1196. {
  1197. std::snprintf(strBuf, STR_MAX, "%s:audio-out1", jackClientName);
  1198. strBuf[STR_MAX-1] = '\0';
  1199. jackbridge_connect(fClient, strBuf, "system:playback_1");
  1200. std::snprintf(strBuf, STR_MAX, "%s:audio-out2", jackClientName);
  1201. strBuf[STR_MAX-1] = '\0';
  1202. if (jackbridge_port_by_name(fClient, "system:playback_2") != nullptr)
  1203. jackbridge_connect(fClient, strBuf, "system:playback_2");
  1204. else
  1205. jackbridge_connect(fClient, strBuf, "system:playback_1");
  1206. }
  1207. }
  1208. }
  1209. startThread();
  1210. callback(true, true,
  1211. ENGINE_CALLBACK_ENGINE_STARTED, 0,
  1212. opts.processMode,
  1213. opts.transportMode,
  1214. static_cast<int>(pData->bufferSize),
  1215. static_cast<float>(pData->sampleRate),
  1216. getCurrentDriverName());
  1217. return true;
  1218. }
  1219. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1220. opts.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1221. {
  1222. pData->graph.destroy();
  1223. }
  1224. pData->close();
  1225. jackbridge_client_close(fClient);
  1226. fClient = nullptr;
  1227. setLastError("Failed to activate the JACK client");
  1228. return false;
  1229. #endif
  1230. }
  1231. #ifndef BUILD_BRIDGE
  1232. bool initInternal(jack_client_t* const client)
  1233. {
  1234. fClient = client;
  1235. fIsInternalClient = true;
  1236. return init(nullptr);
  1237. }
  1238. #endif
  1239. bool close() override
  1240. {
  1241. carla_debug("CarlaEngineJack::close()");
  1242. #ifdef BUILD_BRIDGE
  1243. fClient = nullptr;
  1244. fIsRunning = false;
  1245. CarlaEngine::close();
  1246. return true;
  1247. #else
  1248. stopThread(-1);
  1249. fClientName.clear();
  1250. fPostPonedEvents.clear();
  1251. CARLA_SAFE_ASSERT_RETURN_ERR(fClient != nullptr, "JACK Client is null");
  1252. // deactivate and close client
  1253. jackbridge_deactivate(fClient);
  1254. jackbridge_client_close(fClient);
  1255. // clear engine data
  1256. CarlaEngine::close();
  1257. fUsedGroups.clear();
  1258. fUsedPorts.clear();
  1259. fUsedConnections.clear();
  1260. fPostPonedEvents.clear();
  1261. // clear rack/patchbay stuff
  1262. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1263. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1264. {
  1265. carla_zeroPointers(fRackPorts, kRackPortCount);
  1266. pData->graph.destroy();
  1267. }
  1268. fClient = nullptr;
  1269. return true;
  1270. #endif
  1271. }
  1272. bool isRunning() const noexcept override
  1273. {
  1274. #ifdef BUILD_BRIDGE
  1275. return (fClient != nullptr || fIsRunning);
  1276. #else
  1277. return (fClient != nullptr);
  1278. #endif
  1279. }
  1280. bool isOffline() const noexcept override
  1281. {
  1282. return fFreewheel;
  1283. }
  1284. EngineType getType() const noexcept override
  1285. {
  1286. return kEngineTypeJack;
  1287. }
  1288. const char* getCurrentDriverName() const noexcept override
  1289. {
  1290. return "JACK";
  1291. }
  1292. #ifndef BUILD_BRIDGE
  1293. float getDSPLoad() const noexcept override
  1294. {
  1295. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 0.0f);
  1296. return jackbridge_cpu_load(fClient);
  1297. }
  1298. void callback(const bool sendHost, const bool sendOsc,
  1299. const EngineCallbackOpcode action, const uint pluginId,
  1300. const int value1, const int value2, const int value3,
  1301. const float valuef, const char* const valueStr) noexcept override
  1302. {
  1303. if (action == ENGINE_CALLBACK_PROJECT_LOAD_FINISHED && fTimebaseMaster)
  1304. {
  1305. // project finished loading, need to set bpm here, so we force an update of timebase master
  1306. transportRelocate(pData->timeInfo.frame);
  1307. }
  1308. CarlaEngine::callback(sendHost, sendOsc, action, pluginId, value1, value2, value3, valuef, valueStr);
  1309. }
  1310. void idle() noexcept override
  1311. {
  1312. water::Array<jack_uuid_t> uuids;
  1313. {
  1314. const CarlaMutexLocker cml(fPostPonedUUIDsMutex);
  1315. fPostPonedUUIDs.swapWith(uuids);
  1316. }
  1317. for (int i=0; i<uuids.size(); ++i)
  1318. {
  1319. jack_uuid_t uuid = uuids.getUnchecked(i);
  1320. char uuidstr[JACK_UUID_STRING_SIZE];
  1321. carla_zeroStruct(uuidstr);
  1322. jackbridge_uuid_unparse(uuid, uuidstr);
  1323. if (char* const clientName = jackbridge_get_client_name_by_uuid(fClient, uuidstr))
  1324. {
  1325. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0',);
  1326. uint groupId;
  1327. {
  1328. const CarlaMutexLocker cml(fUsedGroups.mutex);
  1329. groupId = fUsedGroups.getGroupId(clientName);
  1330. }
  1331. jackbridge_free(clientName);
  1332. CARLA_SAFE_ASSERT_RETURN(groupId != 0,);
  1333. char* value = nullptr;
  1334. char* type = nullptr;
  1335. if (jackbridge_get_property(uuid, URI_POSITION, &value, &type)
  1336. && value != nullptr
  1337. && type != nullptr
  1338. && std::strcmp(type, URI_TYPE_STRING) == 0)
  1339. {
  1340. if (char* sep1 = std::strstr(value, ":"))
  1341. {
  1342. int x1, y1 = 0, x2 = 0, y2 = 0;
  1343. *sep1++ = '\0';
  1344. x1 = std::atoi(value);
  1345. if (char* sep2 = std::strstr(sep1, ":"))
  1346. {
  1347. *sep2++ = '\0';
  1348. y1 = std::atoi(sep1);
  1349. if (char* sep3 = std::strstr(sep2, ":"))
  1350. {
  1351. *sep3++ = '\0';
  1352. x2 = std::atoi(sep2);
  1353. y2 = std::atoi(sep3);
  1354. }
  1355. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  1356. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1357. groupId, x1, y1, x2, static_cast<float>(y2),
  1358. nullptr);
  1359. }
  1360. }
  1361. jackbridge_free(value);
  1362. jackbridge_free(type);
  1363. }
  1364. }
  1365. }
  1366. CarlaEngine::idle();
  1367. }
  1368. #endif
  1369. bool setBufferSizeAndSampleRate(const uint bufferSize, const double sampleRate) override
  1370. {
  1371. CARLA_SAFE_ASSERT_RETURN(carla_isEqual(pData->sampleRate, sampleRate), false);
  1372. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1373. try {
  1374. return jackbridge_set_buffer_size(fClient, bufferSize);
  1375. } CARLA_SAFE_EXCEPTION_RETURN("setBufferSizeAndSampleRate", false);
  1376. }
  1377. EngineTimeInfo getTimeInfo() const noexcept override
  1378. {
  1379. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  1380. return CarlaEngine::getTimeInfo();
  1381. if (pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1382. return CarlaEngine::getTimeInfo();
  1383. jack_position_t jpos;
  1384. // invalidate
  1385. jpos.unique_1 = 1;
  1386. jpos.unique_2 = 2;
  1387. EngineTimeInfo timeInfo;
  1388. const bool playing = jackbridge_transport_query(fClient, &jpos) == JackTransportRolling;
  1389. if (jpos.unique_1 != jpos.unique_2)
  1390. {
  1391. timeInfo.playing = false;
  1392. timeInfo.frame = 0;
  1393. timeInfo.usecs = 0;
  1394. timeInfo.bbt.valid = false;
  1395. return timeInfo;
  1396. }
  1397. timeInfo.playing = playing;
  1398. timeInfo.frame = jpos.frame;
  1399. timeInfo.usecs = jpos.usecs;
  1400. if (jpos.valid & JackPositionBBT)
  1401. {
  1402. timeInfo.bbt.valid = true;
  1403. timeInfo.bbt.bar = jpos.bar;
  1404. timeInfo.bbt.beat = jpos.beat;
  1405. timeInfo.bbt.tick = jpos.tick;
  1406. timeInfo.bbt.barStartTick = jpos.bar_start_tick;
  1407. timeInfo.bbt.beatsPerBar = jpos.beats_per_bar;
  1408. timeInfo.bbt.beatType = jpos.beat_type;
  1409. timeInfo.bbt.ticksPerBeat = jpos.ticks_per_beat;
  1410. timeInfo.bbt.beatsPerMinute = jpos.beats_per_minute;
  1411. }
  1412. else
  1413. {
  1414. timeInfo.bbt.valid = false;
  1415. }
  1416. return timeInfo;
  1417. }
  1418. #ifndef BUILD_BRIDGE
  1419. void setOption(const EngineOption option, const int value, const char* const valueStr) noexcept override
  1420. {
  1421. if (option == ENGINE_OPTION_TRANSPORT_MODE && fClient != nullptr)
  1422. {
  1423. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_DISABLED && value <= ENGINE_TRANSPORT_MODE_JACK,);
  1424. if (value == ENGINE_TRANSPORT_MODE_JACK)
  1425. {
  1426. fTimebaseMaster = jackbridge_set_timebase_callback(fClient, true, carla_jack_timebase_callback, this);
  1427. }
  1428. else
  1429. {
  1430. // jack transport cannot be disabled in multi-client
  1431. callback(true, true,
  1432. ENGINE_CALLBACK_TRANSPORT_MODE_CHANGED, 0,
  1433. ENGINE_TRANSPORT_MODE_JACK,
  1434. 0, 0, 0.0f,
  1435. pData->options.transportExtra);
  1436. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS,);
  1437. jackbridge_release_timebase(fClient);
  1438. fTimebaseMaster = false;
  1439. }
  1440. }
  1441. CarlaEngine::setOption(option, value, valueStr);
  1442. }
  1443. #endif
  1444. CarlaEngineClient* addClient(CarlaPlugin* const plugin) override
  1445. {
  1446. jack_client_t* client = nullptr;
  1447. #ifndef BUILD_BRIDGE
  1448. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1449. {
  1450. client = fClient;
  1451. }
  1452. else if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1453. #endif
  1454. {
  1455. client = jackbridge_client_open(plugin->getName(), JackNoStartServer, nullptr);
  1456. CARLA_SAFE_ASSERT_RETURN(client != nullptr, nullptr);
  1457. jackbridge_set_thread_init_callback(client, carla_jack_thread_init_callback, nullptr);
  1458. #ifndef BUILD_BRIDGE
  1459. /*
  1460. jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback_plugin, plugin);
  1461. jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback_plugin, plugin);
  1462. */
  1463. jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, plugin);
  1464. jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, plugin);
  1465. jackbridge_on_shutdown(client, carla_jack_shutdown_callback_plugin, plugin);
  1466. if (char* const uuidstr = jackbridge_client_get_uuid(client))
  1467. {
  1468. jack_uuid_t uuid;
  1469. if (jackbridge_uuid_parse(uuidstr, &uuid))
  1470. {
  1471. char strBufId[24];
  1472. std::snprintf(strBufId, 24, "%u", plugin->getId());
  1473. strBufId[23] = '\0';
  1474. jackbridge_set_property(client, uuid,
  1475. URI_MAIN_CLIENT_NAME,
  1476. fClientName,
  1477. URI_TYPE_STRING);
  1478. jackbridge_set_property(client, uuid,
  1479. URI_PLUGIN_ID,
  1480. strBufId,
  1481. URI_TYPE_INTEGER);
  1482. if (const char* const pluginIcon = plugin->getIconName())
  1483. jackbridge_set_property(client, uuid,
  1484. URI_PLUGIN_ICON,
  1485. pluginIcon,
  1486. URI_TYPE_STRING);
  1487. }
  1488. jackbridge_free(uuidstr);
  1489. }
  1490. #else
  1491. fClient = client;
  1492. pData->bufferSize = jackbridge_get_buffer_size(client);
  1493. pData->sampleRate = jackbridge_get_sample_rate(client);
  1494. pData->initTime(nullptr);
  1495. jackbridge_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  1496. jackbridge_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  1497. jackbridge_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  1498. jackbridge_set_latency_callback(client, carla_jack_latency_callback, this);
  1499. jackbridge_set_process_callback(client, carla_jack_process_callback, this);
  1500. jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
  1501. fClientName = jackbridge_get_client_name(client);
  1502. #endif
  1503. }
  1504. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1505. return new CarlaEngineJackClient(*this, pData->graph, plugin, fClientName, client);
  1506. #else
  1507. return new CarlaEngineJackClient(*this, fClientName, client);
  1508. #endif
  1509. }
  1510. #ifndef BUILD_BRIDGE
  1511. bool removePlugin(const uint id) override
  1512. {
  1513. if (! CarlaEngine::removePlugin(id))
  1514. return false;
  1515. for (uint i=id; i < pData->curPluginCount; ++i)
  1516. {
  1517. CarlaPlugin* const plugin = pData->plugins[i].plugin;
  1518. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  1519. CarlaEngineJackClient* const client = dynamic_cast<CarlaEngineJackClient*>(plugin->getEngineClient());
  1520. CARLA_SAFE_ASSERT_BREAK(client != nullptr);
  1521. client->setNewPluginId(i);
  1522. }
  1523. return true;
  1524. }
  1525. bool switchPlugins(const uint idA, const uint idB) noexcept override
  1526. {
  1527. if (! CarlaEngine::switchPlugins(idA, idB))
  1528. return false;
  1529. CarlaPlugin* const newPluginA(pData->plugins[idA].plugin);
  1530. CARLA_SAFE_ASSERT_RETURN(newPluginA != nullptr, true);
  1531. CarlaPlugin* const newPluginB(pData->plugins[idB].plugin);
  1532. CARLA_SAFE_ASSERT_RETURN(newPluginB != nullptr, true);
  1533. CarlaEngineJackClient* const clientA = dynamic_cast<CarlaEngineJackClient*>(newPluginA->getEngineClient());
  1534. CARLA_SAFE_ASSERT_RETURN(clientA != nullptr, true);
  1535. CarlaEngineJackClient* const clientB = dynamic_cast<CarlaEngineJackClient*>(newPluginB->getEngineClient());
  1536. CARLA_SAFE_ASSERT_RETURN(clientB != nullptr, true);
  1537. clientA->setNewPluginId(idA);
  1538. clientB->setNewPluginId(idB);
  1539. return true;
  1540. }
  1541. bool renamePlugin(const uint id, const char* const newName) override
  1542. {
  1543. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1544. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1545. {
  1546. return CarlaEngine::renamePlugin(id, newName);
  1547. }
  1548. CARLA_SAFE_ASSERT_RETURN(pData->plugins != nullptr, false);
  1549. CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount != 0, false);
  1550. CARLA_SAFE_ASSERT_RETURN(id < pData->curPluginCount, false);
  1551. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0', false);
  1552. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  1553. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to rename");
  1554. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  1555. // before we stop the engine thread we might need to get the plugin data
  1556. const bool needsReinit = (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS);
  1557. const CarlaStateSave* saveStatePtr = nullptr;
  1558. if (needsReinit)
  1559. {
  1560. const CarlaStateSave& saveState(plugin->getStateSave());
  1561. saveStatePtr = &saveState;
  1562. }
  1563. CarlaString uniqueName;
  1564. try {
  1565. const char* const tmpName = getUniquePluginName(newName);
  1566. uniqueName = tmpName;
  1567. delete[] tmpName;
  1568. } CARLA_SAFE_EXCEPTION("JACK renamePlugin getUniquePluginName");
  1569. if (uniqueName.isEmpty())
  1570. {
  1571. setLastError("Failed to request new unique plugin name");
  1572. return false;
  1573. }
  1574. const ScopedThreadStopper sts(this);
  1575. // rename on client client mode, just rename the ports
  1576. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1577. {
  1578. CarlaEngineJackClient* const client((CarlaEngineJackClient*)plugin->getEngineClient());
  1579. if (! client->renameInSingleClient(uniqueName))
  1580. {
  1581. setLastError("Failed to rename some JACK ports, does your JACK version support proper port renaming?");
  1582. return false;
  1583. }
  1584. }
  1585. // rename in multiple client mode
  1586. else if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1587. {
  1588. CarlaEngineJackClient* const client((CarlaEngineJackClient*)plugin->getEngineClient());
  1589. // we should not be able to do this, jack really needs to allow client rename
  1590. if (jack_client_t* const jackClient = jackbridge_client_open(uniqueName, JackNoStartServer, nullptr))
  1591. {
  1592. // get new client name
  1593. uniqueName = jackbridge_get_client_name(jackClient);
  1594. fClientName = uniqueName;
  1595. // close client
  1596. client->closeForRename(jackClient, uniqueName);
  1597. // disable plugin
  1598. plugin->setEnabled(false);
  1599. // set new client data
  1600. jackbridge_set_latency_callback(jackClient, carla_jack_latency_callback_plugin, plugin);
  1601. jackbridge_set_process_callback(jackClient, carla_jack_process_callback_plugin, plugin);
  1602. jackbridge_on_shutdown(jackClient, carla_jack_shutdown_callback_plugin, plugin);
  1603. // NOTE: jack1 locks up here
  1604. if (jackbridge_get_version_string() != nullptr)
  1605. jackbridge_set_thread_init_callback(jackClient, carla_jack_thread_init_callback, nullptr);
  1606. /* The following code is because of a tricky situation.
  1607. We cannot lock or do jack operations during jack callbacks on jack1. jack2 events are asynchronous.
  1608. When we close the client jack will trigger unregister-port callbacks, which we handle on a separate thread ASAP.
  1609. But before that happens we already registered a new client with the same ports (the "renamed" one),
  1610. and at this point the port we receive during that callback is actually the new one from the new client..
  1611. JACK2 seems to be reusing ports to save space, which is understandable.
  1612. Anyway, this means we have to remove all our port-related data before the new client ports are created.
  1613. (we also stop the separate jack-events thread to avoid any race conditions while modying our port data) */
  1614. stopThread(-1);
  1615. LinkedList<PortNameToId> ports;
  1616. LinkedList<ConnectionToId> conns;
  1617. {
  1618. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  1619. if (const uint groupId = fUsedGroups.getGroupId(plugin->getName()))
  1620. {
  1621. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  1622. for (LinkedList<PortNameToId>::Itenerator it = fUsedPorts.list.begin2(); it.valid(); it.next())
  1623. {
  1624. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  1625. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  1626. if (portNameToId.group != groupId)
  1627. continue;
  1628. ports.append(portNameToId);
  1629. fUsedPorts.list.remove(it);
  1630. }
  1631. const CarlaMutexLocker cml3(fUsedConnections.mutex);
  1632. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.list.begin2(); it.valid(); it.next())
  1633. {
  1634. const ConnectionToId& connectionToId = it.getValue(kConnectionToIdFallback);
  1635. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  1636. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1637. continue;
  1638. conns.append(connectionToId);
  1639. fUsedConnections.list.remove(it);
  1640. }
  1641. }
  1642. }
  1643. for (LinkedList<ConnectionToId>::Itenerator it = conns.begin2(); it.valid(); it.next())
  1644. {
  1645. const ConnectionToId& connectionToId = it.getValue(kConnectionToIdFallback);
  1646. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  1647. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  1648. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1649. connectionToId.id,
  1650. 0, 0, 0, 0.0f, nullptr);
  1651. }
  1652. for (LinkedList<PortNameToId>::Itenerator it = ports.begin2(); it.valid(); it.next())
  1653. {
  1654. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  1655. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  1656. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  1657. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1658. portNameToId.group,
  1659. static_cast<int>(portNameToId.port),
  1660. 0, 0, 0.0f, nullptr);
  1661. }
  1662. ports.clear();
  1663. conns.clear();
  1664. startThread();
  1665. }
  1666. else
  1667. {
  1668. setLastError("Failed to create new JACK client");
  1669. return false;
  1670. }
  1671. }
  1672. // Rename
  1673. plugin->setName(uniqueName);
  1674. if (needsReinit)
  1675. {
  1676. // reload plugin to recreate its ports
  1677. plugin->reload();
  1678. plugin->loadStateSave(*saveStatePtr);
  1679. plugin->setEnabled(true);
  1680. }
  1681. callback(true, true, ENGINE_CALLBACK_PLUGIN_RENAMED, id, 0, 0, 0, 0.0f, uniqueName);
  1682. return true;
  1683. }
  1684. // -------------------------------------------------------------------
  1685. // Patchbay
  1686. bool patchbayConnect(const bool external,
  1687. const uint groupA, const uint portA, const uint groupB, const uint portB) override
  1688. {
  1689. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1690. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1691. return CarlaEngine::patchbayConnect(false, groupA, portA, groupB, portB);
  1692. const CarlaMutexLocker cml(fUsedPorts.mutex);
  1693. const char* const fullPortNameA = fUsedPorts.getFullPortName(groupA, portA);
  1694. CARLA_SAFE_ASSERT_RETURN(fullPortNameA != nullptr && fullPortNameA[0] != '\0', false);
  1695. const char* const fullPortNameB = fUsedPorts.getFullPortName(groupB, portB);
  1696. CARLA_SAFE_ASSERT_RETURN(fullPortNameB != nullptr && fullPortNameB[0] != '\0', false);
  1697. if (! jackbridge_connect(fClient, fullPortNameA, fullPortNameB))
  1698. {
  1699. setLastError("JACK operation failed");
  1700. return false;
  1701. }
  1702. return true;
  1703. }
  1704. bool patchbayDisconnect(const bool external, const uint connectionId) override
  1705. {
  1706. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1707. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1708. return CarlaEngine::patchbayDisconnect(false, connectionId);
  1709. ConnectionToId connectionToId = { 0, 0, 0, 0, 0 };
  1710. {
  1711. const CarlaMutexLocker cml(fUsedConnections.mutex);
  1712. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.list.begin2(); it.valid(); it.next())
  1713. {
  1714. connectionToId = it.getValue(kConnectionToIdFallback);
  1715. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  1716. if (connectionToId.id == connectionId)
  1717. break;
  1718. }
  1719. }
  1720. if (connectionToId.id == 0 || connectionToId.id != connectionId)
  1721. {
  1722. setLastError("Failed to find the requested connection");
  1723. return false;
  1724. }
  1725. const CarlaMutexLocker cml(fUsedPorts.mutex);
  1726. const char* const fullPortNameA = fUsedPorts.getFullPortName(connectionToId.groupA, connectionToId.portA);
  1727. CARLA_SAFE_ASSERT_RETURN(fullPortNameA != nullptr && fullPortNameA[0] != '\0', false);
  1728. const char* const fullPortNameB = fUsedPorts.getFullPortName(connectionToId.groupB, connectionToId.portB);
  1729. CARLA_SAFE_ASSERT_RETURN(fullPortNameB != nullptr && fullPortNameB[0] != '\0', false);
  1730. if (! jackbridge_disconnect(fClient, fullPortNameA, fullPortNameB))
  1731. {
  1732. setLastError("JACK operation failed");
  1733. return false;
  1734. }
  1735. return true;
  1736. }
  1737. bool patchbaySetGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  1738. const uint groupId, const int x1, const int y1, const int x2, const int y2) override
  1739. {
  1740. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1741. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1742. return CarlaEngine::patchbaySetGroupPos(sendHost, sendOSC, false, groupId, x1, y1, x2, y2);
  1743. const char* groupName;
  1744. {
  1745. const CarlaMutexLocker cml(fUsedGroups.mutex);
  1746. groupName = fUsedGroups.getGroupName(groupId);
  1747. CARLA_SAFE_ASSERT_RETURN(groupName != nullptr && groupName[0] != '\0', false);
  1748. }
  1749. jack_uuid_t uuid;
  1750. {
  1751. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, groupName);
  1752. CARLA_SAFE_ASSERT_RETURN(uuidstr != nullptr && uuidstr[0] != '\0', false);
  1753. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  1754. jackbridge_free(uuidstr);
  1755. CARLA_SAFE_ASSERT_RETURN(parsed, false);
  1756. }
  1757. char valueStr[STR_MAX];
  1758. std::snprintf(valueStr, STR_MAX-1, "%i:%i:%i:%i", x1, y1, x2, y2);
  1759. valueStr[STR_MAX-1] = '\0';
  1760. const bool ok = jackbridge_set_property(fClient, uuid, URI_POSITION, valueStr, URI_TYPE_STRING);
  1761. callback(sendHost, sendOSC,
  1762. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1763. groupId, x1, y1, x2, static_cast<float>(y2),
  1764. nullptr);
  1765. return ok;
  1766. }
  1767. bool patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external) override
  1768. {
  1769. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1770. carla_debug("patchbayRefresh(%s, %s, %s)", bool2str(sendHost), bool2str(sendOSC), bool2str(external));
  1771. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1772. {
  1773. if (sendHost)
  1774. {
  1775. fExternalPatchbayHost = external;
  1776. pData->graph.setUsingExternalHost(external);
  1777. }
  1778. if (sendOSC)
  1779. {
  1780. fExternalPatchbayOsc = external;
  1781. pData->graph.setUsingExternalOSC(external);
  1782. }
  1783. if (! external)
  1784. return CarlaEngine::patchbayRefresh(sendHost, sendOSC, false);
  1785. }
  1786. {
  1787. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  1788. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  1789. const CarlaMutexLocker cml3(fUsedConnections.mutex);
  1790. fUsedGroups.clear();
  1791. fUsedPorts.clear();
  1792. fUsedConnections.clear();
  1793. }
  1794. initJackPatchbay(sendHost, sendOSC, jackbridge_get_client_name(fClient));
  1795. return true;
  1796. }
  1797. // -------------------------------------------------------------------
  1798. // Transport
  1799. void transportPlay() noexcept override
  1800. {
  1801. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  1802. return CarlaEngine::transportPlay();
  1803. if (fClient != nullptr)
  1804. {
  1805. if (! pData->timeInfo.bbt.valid)
  1806. {
  1807. // old timebase master no longer active, make ourselves master again
  1808. pData->time.setNeedsReset();
  1809. fTimebaseMaster = jackbridge_set_timebase_callback(fClient, true, carla_jack_timebase_callback, this);
  1810. }
  1811. try {
  1812. jackbridge_transport_start(fClient);
  1813. } catch(...) {}
  1814. }
  1815. }
  1816. void transportPause() noexcept override
  1817. {
  1818. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  1819. return CarlaEngine::transportPause();
  1820. if (fClient != nullptr)
  1821. {
  1822. try {
  1823. jackbridge_transport_stop(fClient);
  1824. } catch(...) {}
  1825. }
  1826. }
  1827. void transportBPM(const double bpm) noexcept override
  1828. {
  1829. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK || fTimebaseMaster)
  1830. return CarlaEngine::transportBPM(bpm);
  1831. if (fClient == nullptr)
  1832. return;
  1833. jack_position_t jpos;
  1834. // invalidate
  1835. jpos.unique_1 = 1;
  1836. jpos.unique_2 = 2;
  1837. jackbridge_transport_query(fClient, &jpos);
  1838. if (jpos.unique_1 == jpos.unique_2 && (jpos.valid & JackPositionBBT) != 0)
  1839. {
  1840. carla_stdout("NOTE: Changing BPM without being JACK timebase master");
  1841. jpos.beats_per_minute = bpm;
  1842. try {
  1843. jackbridge_transport_reposition(fClient, &jpos);
  1844. } catch(...) {}
  1845. }
  1846. }
  1847. void transportRelocate(const uint64_t frame) noexcept override
  1848. {
  1849. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  1850. return CarlaEngine::transportRelocate(frame);
  1851. if (fClient != nullptr)
  1852. {
  1853. try {
  1854. jackbridge_transport_locate(fClient, static_cast<jack_nframes_t>(frame));
  1855. } catch(...) {}
  1856. }
  1857. }
  1858. // -------------------------------------------------------------------
  1859. // Patchbay stuff
  1860. const char* const* getPatchbayConnections(const bool external) const override
  1861. {
  1862. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, nullptr);
  1863. carla_debug("CarlaEngineJack::getPatchbayConnections(%s)", bool2str(external));
  1864. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1865. return CarlaEngine::getPatchbayConnections(external);
  1866. CarlaStringList connList;
  1867. if (const char** const ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
  1868. {
  1869. for (int i=0; ports[i] != nullptr; ++i)
  1870. {
  1871. const jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
  1872. const char* const fullPortName(ports[i]);
  1873. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  1874. if (const char** const connections = jackbridge_port_get_all_connections(fClient, jackPort))
  1875. {
  1876. for (int j=0; connections[j] != nullptr; ++j)
  1877. {
  1878. connList.append(fullPortName);
  1879. connList.append(connections[j]);
  1880. }
  1881. jackbridge_free(connections);
  1882. }
  1883. }
  1884. jackbridge_free(ports);
  1885. }
  1886. if (connList.count() == 0)
  1887. return nullptr;
  1888. fRetConns = connList.toCharStringListPtr();
  1889. return fRetConns;
  1890. }
  1891. const PatchbayPosition* getPatchbayPositions(const bool external, uint& count) const override
  1892. {
  1893. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, nullptr);
  1894. carla_debug("CarlaEngineJack::getPatchbayPositions(%s)", bool2str(external));
  1895. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1896. return CarlaEngine::getPatchbayPositions(external, count);
  1897. const CarlaMutexLocker cml(fUsedGroups.mutex);
  1898. if (const std::size_t maxCount = fUsedGroups.list.count())
  1899. {
  1900. PatchbayPosition* ret;
  1901. try {
  1902. ret = new CarlaEngine::PatchbayPosition[maxCount];
  1903. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1904. count = 0;
  1905. GroupNameToId groupNameToId;
  1906. for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroups.list.begin2(); it.valid(); it.next())
  1907. {
  1908. groupNameToId = it.getValue(kGroupNameToIdFallback);
  1909. CARLA_SAFE_ASSERT_CONTINUE(groupNameToId.group != 0);
  1910. jack_uuid_t uuid;
  1911. {
  1912. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, groupNameToId.name);
  1913. CARLA_SAFE_ASSERT_CONTINUE(uuidstr != nullptr && uuidstr[0] != '\0');
  1914. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  1915. jackbridge_free(uuidstr);
  1916. CARLA_SAFE_ASSERT_CONTINUE(parsed);
  1917. }
  1918. char* value = nullptr;
  1919. char* type = nullptr;
  1920. if (jackbridge_get_property(uuid, URI_POSITION, &value, &type)
  1921. && value != nullptr
  1922. && type != nullptr
  1923. && std::strcmp(type, URI_TYPE_STRING) == 0)
  1924. {
  1925. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1926. ppos.name = carla_strdup_safe(groupNameToId.name);
  1927. ppos.dealloc = true;
  1928. ppos.pluginId = -1;
  1929. if (char* sep1 = std::strstr(value, ":"))
  1930. {
  1931. *sep1++ = '\0';
  1932. ppos.x1 = std::atoi(value);
  1933. if (char* sep2 = std::strstr(sep1, ":"))
  1934. {
  1935. *sep2++ = '\0';
  1936. ppos.y1 = std::atoi(sep1);
  1937. if (char* sep3 = std::strstr(sep2, ":"))
  1938. {
  1939. *sep3++ = '\0';
  1940. ppos.x2 = std::atoi(sep2);
  1941. ppos.y2 = std::atoi(sep3);
  1942. }
  1943. }
  1944. }
  1945. jackbridge_free(value);
  1946. jackbridge_free(type);
  1947. value = type = nullptr;
  1948. const bool clientBelongsToUs = (jackbridge_get_property(uuid, URI_MAIN_CLIENT_NAME, &value, &type)
  1949. && value != nullptr
  1950. && type != nullptr
  1951. && std::strcmp(type, URI_TYPE_STRING) == 0
  1952. && fClientName == value);
  1953. jackbridge_free(value);
  1954. jackbridge_free(type);
  1955. value = type = nullptr;
  1956. if (! clientBelongsToUs)
  1957. continue;
  1958. if (jackbridge_get_property(uuid, URI_PLUGIN_ID, &value, &type)
  1959. && value != nullptr
  1960. && type != nullptr
  1961. && std::strcmp(type, URI_TYPE_INTEGER) == 0)
  1962. {
  1963. ppos.pluginId = std::atoi(value);
  1964. }
  1965. jackbridge_free(value);
  1966. jackbridge_free(type);
  1967. }
  1968. }
  1969. return ret;
  1970. }
  1971. return nullptr;
  1972. }
  1973. void restorePatchbayConnection(const bool external,
  1974. const char* const connSource, const char* const connTarget) override
  1975. {
  1976. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr,);
  1977. CARLA_SAFE_ASSERT_RETURN(connSource != nullptr && connSource[0] != '\0',);
  1978. CARLA_SAFE_ASSERT_RETURN(connTarget != nullptr && connTarget[0] != '\0',);
  1979. carla_debug("CarlaEngineJack::restorePatchbayConnection(%s, \"%s\", \"%s\")",
  1980. bool2str(external), connSource, connTarget);
  1981. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1982. return CarlaEngine::restorePatchbayConnection(external, connSource, connTarget);
  1983. if (const jack_port_t* const port = jackbridge_port_by_name(fClient, connSource))
  1984. {
  1985. if (jackbridge_port_by_name(fClient, connTarget) == nullptr)
  1986. return;
  1987. if (! jackbridge_port_connected_to(port, connTarget))
  1988. jackbridge_connect(fClient, connSource, connTarget);
  1989. }
  1990. }
  1991. void restorePatchbayGroupPosition(const bool external, const PatchbayPosition& ppos) override
  1992. {
  1993. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr,);
  1994. carla_debug("CarlaEngineJack::restorePatchbayGroupPosition(%s, ...)", bool2str(external));
  1995. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1996. return CarlaEngine::restorePatchbayGroupPosition(external, ppos);
  1997. bool hasGroups = true;
  1998. uint groupId = 0;
  1999. /* NOTE: When loading a project, it might take a bit to receive plugins' jack client registration callbacks.
  2000. * We try to wait a little for it, but not too much.
  2001. */
  2002. if (ppos.pluginId >= 0)
  2003. {
  2004. for (int i=10; --i >=0;)
  2005. {
  2006. {
  2007. const CarlaMutexLocker cml(fUsedGroups.mutex);
  2008. if (fUsedGroups.list.count() == 0)
  2009. {
  2010. hasGroups = false;
  2011. break;
  2012. }
  2013. groupId = fUsedGroups.getGroupId(ppos.name);
  2014. }
  2015. if (groupId != 0)
  2016. break;
  2017. carla_msleep(200);
  2018. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2019. }
  2020. }
  2021. else
  2022. {
  2023. const CarlaMutexLocker cml(fUsedGroups.mutex);
  2024. if (fUsedGroups.list.count() != 0)
  2025. groupId = fUsedGroups.getGroupId(ppos.name);
  2026. else
  2027. hasGroups = false;
  2028. }
  2029. if (hasGroups) {
  2030. CARLA_SAFE_ASSERT(groupId != 0);
  2031. }
  2032. jack_uuid_t uuid;
  2033. {
  2034. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, ppos.name);
  2035. CARLA_SAFE_ASSERT_RETURN(uuidstr != nullptr && uuidstr[0] != '\0',);
  2036. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  2037. jackbridge_free(uuidstr);
  2038. CARLA_SAFE_ASSERT_RETURN(parsed,);
  2039. }
  2040. char valueStr[STR_MAX];
  2041. std::snprintf(valueStr, STR_MAX-1, "%i:%i:%i:%i", ppos.x1, ppos.y1, ppos.x2, ppos.y2);
  2042. valueStr[STR_MAX-1] = '\0';
  2043. jackbridge_set_property(fClient, uuid, URI_POSITION, valueStr, URI_TYPE_STRING);
  2044. if (groupId != 0)
  2045. {
  2046. callback(true, true,
  2047. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  2048. groupId, ppos.x1, ppos.y1, ppos.x2, static_cast<float>(ppos.y2),
  2049. nullptr);
  2050. }
  2051. }
  2052. #endif
  2053. // -------------------------------------------------------------------
  2054. protected:
  2055. void handleJackBufferSizeCallback(const uint32_t newBufferSize)
  2056. {
  2057. if (pData->bufferSize == newBufferSize)
  2058. return;
  2059. #ifndef BUILD_BRIDGE
  2060. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2061. #endif
  2062. pData->bufferSize = newBufferSize;
  2063. bufferSizeChanged(newBufferSize);
  2064. }
  2065. void handleJackSampleRateCallback(const double newSampleRate)
  2066. {
  2067. if (carla_isEqual(pData->sampleRate, newSampleRate))
  2068. return;
  2069. #ifndef BUILD_BRIDGE
  2070. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2071. #endif
  2072. pData->sampleRate = newSampleRate;
  2073. sampleRateChanged(newSampleRate);
  2074. }
  2075. void handleJackFreewheelCallback(const bool isFreewheel)
  2076. {
  2077. if (fFreewheel == isFreewheel)
  2078. return;
  2079. #ifndef BUILD_BRIDGE
  2080. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2081. #endif
  2082. fFreewheel = isFreewheel;
  2083. offlineModeChanged(isFreewheel);
  2084. }
  2085. void handleJackProcessCallback(const uint32_t nframes)
  2086. {
  2087. const PendingRtEventsRunner prt(this, nframes);
  2088. CARLA_SAFE_ASSERT_INT2_RETURN(nframes == pData->bufferSize, nframes, pData->bufferSize,);
  2089. #ifdef BUILD_BRIDGE
  2090. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  2091. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(fFreewheel))
  2092. {
  2093. plugin->initBuffers();
  2094. processPlugin(plugin, nframes);
  2095. plugin->unlock();
  2096. }
  2097. #else
  2098. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_JACK && !fTimebaseMaster)
  2099. {
  2100. jack_position_t jpos;
  2101. // invalidate
  2102. jpos.unique_1 = 1;
  2103. jpos.unique_2 = 2;
  2104. EngineTimeInfo timeInfo;
  2105. const bool playing = jackbridge_transport_query(fClient, &jpos) == JackTransportRolling;
  2106. if (jpos.unique_1 != jpos.unique_2)
  2107. {
  2108. timeInfo.playing = false;
  2109. timeInfo.frame = 0;
  2110. timeInfo.usecs = 0;
  2111. timeInfo.bbt.valid = false;
  2112. }
  2113. else
  2114. {
  2115. timeInfo.playing = playing;
  2116. timeInfo.frame = jpos.frame;
  2117. timeInfo.usecs = jpos.usecs;
  2118. if (jpos.valid & JackPositionBBT)
  2119. {
  2120. timeInfo.bbt.valid = true;
  2121. timeInfo.bbt.bar = jpos.bar;
  2122. timeInfo.bbt.beat = jpos.beat;
  2123. timeInfo.bbt.tick = jpos.tick;
  2124. timeInfo.bbt.barStartTick = jpos.bar_start_tick;
  2125. timeInfo.bbt.beatsPerBar = jpos.beats_per_bar;
  2126. timeInfo.bbt.beatType = jpos.beat_type;
  2127. timeInfo.bbt.ticksPerBeat = jpos.ticks_per_beat;
  2128. timeInfo.bbt.beatsPerMinute = jpos.beats_per_minute;
  2129. }
  2130. else
  2131. {
  2132. timeInfo.bbt.valid = false;
  2133. }
  2134. }
  2135. pData->timeInfo = timeInfo;
  2136. }
  2137. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2138. {
  2139. if (pData->aboutToClose)
  2140. {
  2141. if (float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes))
  2142. carla_zeroFloats(audioOut1, nframes);
  2143. if (float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes))
  2144. carla_zeroFloats(audioOut2, nframes);
  2145. }
  2146. else if (pData->curPluginCount == 0)
  2147. {
  2148. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  2149. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  2150. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  2151. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  2152. // assert buffers
  2153. CARLA_SAFE_ASSERT_RETURN(audioIn1 != nullptr,);
  2154. CARLA_SAFE_ASSERT_RETURN(audioIn2 != nullptr,);
  2155. CARLA_SAFE_ASSERT_RETURN(audioOut1 != nullptr,);
  2156. CARLA_SAFE_ASSERT_RETURN(audioOut2 != nullptr,);
  2157. // pass-through
  2158. carla_copyFloats(audioOut1, audioIn1, nframes);
  2159. carla_copyFloats(audioOut2, audioIn2, nframes);
  2160. // TODO pass-through MIDI as well
  2161. if (void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes))
  2162. jackbridge_midi_clear_buffer(eventOut);
  2163. return;
  2164. }
  2165. }
  2166. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  2167. {
  2168. for (uint i=0; i < pData->curPluginCount; ++i)
  2169. {
  2170. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  2171. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(fFreewheel))
  2172. {
  2173. plugin->initBuffers();
  2174. processPlugin(plugin, nframes);
  2175. plugin->unlock();
  2176. }
  2177. }
  2178. }
  2179. else if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  2180. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2181. {
  2182. CARLA_SAFE_ASSERT_RETURN(pData->events.in != nullptr,);
  2183. CARLA_SAFE_ASSERT_RETURN(pData->events.out != nullptr,);
  2184. // get buffers from jack
  2185. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  2186. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  2187. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  2188. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  2189. void* const eventIn = jackbridge_port_get_buffer(fRackPorts[kRackPortEventIn], nframes);
  2190. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes);
  2191. // assert buffers
  2192. CARLA_SAFE_ASSERT_RETURN(audioIn1 != nullptr,);
  2193. CARLA_SAFE_ASSERT_RETURN(audioIn2 != nullptr,);
  2194. CARLA_SAFE_ASSERT_RETURN(audioOut1 != nullptr,);
  2195. CARLA_SAFE_ASSERT_RETURN(audioOut2 != nullptr,);
  2196. // create audio buffers
  2197. const float* inBuf[2] = { audioIn1, audioIn2 };
  2198. /**/ float* outBuf[2] = { audioOut1, audioOut2 };
  2199. // initialize events
  2200. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  2201. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  2202. if (eventIn != nullptr)
  2203. {
  2204. ushort engineEventIndex = 0;
  2205. jack_midi_event_t jackEvent;
  2206. const uint32_t jackEventCount(jackbridge_midi_get_event_count(eventIn));
  2207. for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; ++jackEventIndex)
  2208. {
  2209. if (! jackbridge_midi_event_get(&jackEvent, eventIn, jackEventIndex))
  2210. continue;
  2211. CARLA_SAFE_ASSERT_CONTINUE(jackEvent.size < 0xFF /* uint8_t max */);
  2212. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  2213. engineEvent.time = jackEvent.time;
  2214. engineEvent.fillFromMidiData(static_cast<uint8_t>(jackEvent.size), jackEvent.buffer, 0);
  2215. if (engineEventIndex >= kMaxEngineEventInternalCount)
  2216. break;
  2217. }
  2218. }
  2219. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2220. {
  2221. pData->graph.processRack(pData, inBuf, outBuf, nframes);
  2222. }
  2223. else
  2224. {
  2225. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2226. pData->graph.process(pData, inBuf, outBuf, nframes);
  2227. }
  2228. // output control
  2229. if (eventOut != nullptr)
  2230. {
  2231. jackbridge_midi_clear_buffer(eventOut);
  2232. uint8_t size = 0;
  2233. uint8_t mdata[3] = { 0, 0, 0 };
  2234. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  2235. const uint8_t* mdataPtr;
  2236. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  2237. {
  2238. const EngineEvent& engineEvent(pData->events.out[i]);
  2239. /**/ if (engineEvent.type == kEngineEventTypeNull)
  2240. {
  2241. break;
  2242. }
  2243. else if (engineEvent.type == kEngineEventTypeControl)
  2244. {
  2245. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  2246. size = ctrlEvent.convertToMidiData(engineEvent.channel, mdata);
  2247. mdataPtr = mdata;
  2248. }
  2249. else if (engineEvent.type == kEngineEventTypeMidi)
  2250. {
  2251. const EngineMidiEvent& midiEvent(engineEvent.midi);
  2252. size = midiEvent.size;
  2253. CARLA_SAFE_ASSERT_CONTINUE(size > 0);
  2254. if (size > EngineMidiEvent::kDataSize)
  2255. {
  2256. CARLA_SAFE_ASSERT_CONTINUE(midiEvent.dataExt != nullptr);
  2257. mdataPtr = midiEvent.dataExt;
  2258. }
  2259. else
  2260. {
  2261. // set first byte
  2262. mdataTmp[0] = static_cast<uint8_t>(midiEvent.data[0] | (engineEvent.channel & MIDI_CHANNEL_BIT));
  2263. // copy rest
  2264. carla_copy<uint8_t>(mdataTmp+1, midiEvent.data+1, size-1U);
  2265. // done
  2266. mdataPtr = mdataTmp;
  2267. }
  2268. }
  2269. else
  2270. {
  2271. continue;
  2272. }
  2273. if (size > 0)
  2274. jackbridge_midi_event_write(eventOut, engineEvent.time, mdataPtr, size);
  2275. }
  2276. }
  2277. }
  2278. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_JACK)
  2279. {
  2280. if (fTimebaseMaster)
  2281. {
  2282. const bool playing = jackbridge_transport_query(fClient, nullptr) == JackTransportRolling;
  2283. if (fTimebaseRolling != playing)
  2284. {
  2285. fTimebaseRolling = playing;
  2286. pData->timeInfo.playing = playing;
  2287. }
  2288. // Check if we are no longer timebase master
  2289. if (playing && fTimebaseUsecs != 0 && fTimebaseUsecs == pData->timeInfo.usecs)
  2290. {
  2291. carla_debug("No longer timerbase master");
  2292. fTimebaseMaster = false;
  2293. }
  2294. }
  2295. fTimebaseUsecs = pData->timeInfo.usecs;
  2296. }
  2297. #endif // ! BUILD_BRIDGE
  2298. }
  2299. void handleJackLatencyCallback(const jack_latency_callback_mode_t /*mode*/)
  2300. {
  2301. // TODO
  2302. }
  2303. #ifndef BUILD_BRIDGE
  2304. void handleJackTimebaseCallback(jack_nframes_t nframes, jack_position_t* const pos, const int new_pos)
  2305. {
  2306. if (new_pos)
  2307. pData->time.setNeedsReset();
  2308. pData->timeInfo.playing = fTimebaseRolling;
  2309. pData->timeInfo.frame = pos->frame;
  2310. pData->timeInfo.usecs = pos->usecs;
  2311. pData->time.fillJackTimeInfo(pos, nframes);
  2312. }
  2313. void handleJackClientUnregistrationCallback(const char* const name)
  2314. {
  2315. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  2316. // ignore this if on internal patchbay mode
  2317. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2318. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2319. #else
  2320. if (! fExternalPatchbayHost) return;
  2321. #endif
  2322. uint groupId;
  2323. {
  2324. const CarlaMutexLocker cml(fUsedGroups.mutex);
  2325. groupId = fUsedGroups.getGroupId(name);
  2326. // clients might have been registered without ports
  2327. if (groupId == 0) return;
  2328. GroupNameToId groupNameToId;
  2329. groupNameToId.setData(groupId, name);
  2330. fUsedGroups.list.removeOne(groupNameToId);
  2331. }
  2332. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2333. ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED,
  2334. groupId,
  2335. 0, 0, 0, 0.0f, nullptr);
  2336. }
  2337. void handleJackClientPositionChangeCallback(const jack_uuid_t uuid)
  2338. {
  2339. // ignore this if on internal patchbay mode
  2340. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2341. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2342. #else
  2343. if (! fExternalPatchbayHost) return;
  2344. #endif
  2345. const CarlaMutexLocker cml(fPostPonedUUIDsMutex);
  2346. fPostPonedUUIDs.addIfNotAlreadyThere(uuid);
  2347. }
  2348. void handleJackPortRegistrationCallback(const char* const portName,
  2349. const char* const shortPortName,
  2350. const CarlaJackPortHints& jackPortHints)
  2351. {
  2352. // ignore this if on internal patchbay mode
  2353. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2354. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2355. #else
  2356. if (! fExternalPatchbayHost) return;
  2357. #endif
  2358. bool groupFound;
  2359. CarlaString groupName(portName);
  2360. groupName.truncate(groupName.rfind(shortPortName, &groupFound)-1);
  2361. CARLA_SAFE_ASSERT_RETURN(groupFound,);
  2362. groupFound = false;
  2363. GroupToIdData groupData;
  2364. PortToIdData portData;
  2365. {
  2366. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  2367. groupData.id = fUsedGroups.getGroupId(groupName);
  2368. if (groupData.id == 0)
  2369. {
  2370. groupData.id = ++fUsedGroups.lastId;
  2371. GroupNameToId groupNameToId;
  2372. groupNameToId.setData(groupData.id, groupName);
  2373. int pluginId = -1;
  2374. PatchbayIcon icon = jackPortHints.isHardware ? PATCHBAY_ICON_HARDWARE : PATCHBAY_ICON_APPLICATION;
  2375. findPluginIdAndIcon(groupName, pluginId, icon);
  2376. fUsedGroups.list.append(groupNameToId);
  2377. groupFound = true;
  2378. groupData.icon = icon;
  2379. groupData.pluginId = pluginId;
  2380. std::strncpy(groupData.strVal, groupName, STR_MAX-1);
  2381. groupData.strVal[STR_MAX-1] = '\0';
  2382. }
  2383. uint canvasPortFlags = 0x0;
  2384. canvasPortFlags |= jackPortHints.isInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
  2385. /**/ if (jackPortHints.isCV)
  2386. canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
  2387. else if (jackPortHints.isOSC)
  2388. canvasPortFlags |= PATCHBAY_PORT_TYPE_OSC;
  2389. else if (jackPortHints.isAudio)
  2390. canvasPortFlags |= PATCHBAY_PORT_TYPE_AUDIO;
  2391. else if (jackPortHints.isMIDI)
  2392. canvasPortFlags |= PATCHBAY_PORT_TYPE_MIDI;
  2393. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  2394. portData.group = groupData.id;
  2395. portData.port = ++fUsedPorts.lastId;
  2396. portData.flags = canvasPortFlags;
  2397. std::strncpy(portData.strVal, shortPortName, STR_MAX-1);
  2398. portData.strVal[STR_MAX-1] = '\0';
  2399. PortNameToId portNameToId;
  2400. portNameToId.setData(portData.group, portData.port, shortPortName, portName);
  2401. fUsedPorts.list.append(portNameToId);
  2402. }
  2403. if (groupFound)
  2404. {
  2405. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2406. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  2407. groupData.id,
  2408. groupData.icon,
  2409. groupData.pluginId,
  2410. 0, 0.0f,
  2411. groupData.strVal);
  2412. }
  2413. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2414. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  2415. portData.group,
  2416. static_cast<int>(portData.port),
  2417. static_cast<int>(portData.flags),
  2418. 0, 0.0f,
  2419. portData.strVal);
  2420. }
  2421. void handleJackPortUnregistrationCallback(const char* const portName)
  2422. {
  2423. // ignore this if on internal patchbay mode
  2424. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2425. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2426. #else
  2427. if (! fExternalPatchbayHost) return;
  2428. #endif
  2429. uint groupId, portId;
  2430. {
  2431. const CarlaMutexLocker cml(fUsedPorts.mutex);
  2432. const PortNameToId& portNameToId(fUsedPorts.getPortNameToId(portName));
  2433. /* NOTE: Due to JACK2 async behaviour the port we get here might be the same of a previous rename-plugin request.
  2434. See the comment on CarlaEngineJack::renamePlugin() for more information. */
  2435. if (portNameToId.group <= 0 || portNameToId.port <= 0) return;
  2436. groupId = portNameToId.group;
  2437. portId = portNameToId.port;
  2438. fUsedPorts.list.removeOne(portNameToId);
  2439. }
  2440. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2441. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  2442. groupId,
  2443. static_cast<int>(portId),
  2444. 0, 0, 0.0f, nullptr);
  2445. }
  2446. void handleJackPortConnectCallback(const char* const portNameA, const char* const portNameB)
  2447. {
  2448. // ignore this if on internal patchbay mode
  2449. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2450. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2451. #else
  2452. if (! fExternalPatchbayHost) return;
  2453. #endif
  2454. char strBuf[STR_MAX];
  2455. uint connectionId;
  2456. {
  2457. const CarlaMutexLocker cml1(fUsedPorts.mutex);
  2458. const PortNameToId& portNameToIdA(fUsedPorts.getPortNameToId(portNameA));
  2459. const PortNameToId& portNameToIdB(fUsedPorts.getPortNameToId(portNameB));
  2460. /* NOTE: Due to JACK2 async behaviour the port we get here might be the same of a previous rename-plugin request.
  2461. See the comment on CarlaEngineJack::renamePlugin() for more information. */
  2462. if (portNameToIdA.group <= 0 || portNameToIdA.port <= 0) return;
  2463. if (portNameToIdB.group <= 0 || portNameToIdB.port <= 0) return;
  2464. const CarlaMutexLocker cml2(fUsedConnections.mutex);
  2465. std::snprintf(strBuf, STR_MAX-1, "%i:%i:%i:%i",
  2466. portNameToIdA.group, portNameToIdA.port,
  2467. portNameToIdB.group, portNameToIdB.port);
  2468. strBuf[STR_MAX-1] = '\0';
  2469. connectionId = ++fUsedConnections.lastId;
  2470. ConnectionToId connectionToId;
  2471. connectionToId.setData(connectionId,
  2472. portNameToIdA.group, portNameToIdA.port,
  2473. portNameToIdB.group, portNameToIdB.port);
  2474. fUsedConnections.list.append(connectionToId);
  2475. }
  2476. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2477. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  2478. connectionId,
  2479. 0, 0, 0, 0.0f,
  2480. strBuf);
  2481. }
  2482. void handleJackPortDisconnectCallback(const char* const portNameA, const char* const portNameB)
  2483. {
  2484. // ignore this if on internal patchbay mode
  2485. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2486. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2487. #else
  2488. if (! fExternalPatchbayHost) return;
  2489. #endif
  2490. uint connectionId = 0;
  2491. {
  2492. const CarlaMutexLocker cml1(fUsedPorts.mutex);
  2493. const PortNameToId& portNameToIdA(fUsedPorts.getPortNameToId(portNameA));
  2494. const PortNameToId& portNameToIdB(fUsedPorts.getPortNameToId(portNameB));
  2495. /* NOTE: Due to JACK2 async behaviour the port we get here might be the same of a previous rename-plugin request.
  2496. See the comment on CarlaEngineJack::renamePlugin() for more information. */
  2497. if (portNameToIdA.group <= 0 || portNameToIdA.port <= 0) return;
  2498. if (portNameToIdB.group <= 0 || portNameToIdB.port <= 0) return;
  2499. const CarlaMutexLocker cml2(fUsedConnections.mutex);
  2500. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.list.begin2(); it.valid(); it.next())
  2501. {
  2502. const ConnectionToId& connectionToId = it.getValue(kConnectionToIdFallback);
  2503. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  2504. if (connectionToId.groupA == portNameToIdA.group && connectionToId.portA == portNameToIdA.port &&
  2505. connectionToId.groupB == portNameToIdB.group && connectionToId.portB == portNameToIdB.port)
  2506. {
  2507. connectionId = connectionToId.id;
  2508. fUsedConnections.list.remove(it);
  2509. break;
  2510. }
  2511. }
  2512. }
  2513. if (connectionId != 0) {
  2514. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2515. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  2516. connectionId,
  2517. 0, 0, 0, 0.0f, nullptr);
  2518. }
  2519. }
  2520. void handleJackPortRenameCallback(const char* const oldFullName,
  2521. const char* const newFullName,
  2522. const char* const newShortName)
  2523. {
  2524. // ignore this if on internal patchbay mode
  2525. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2526. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2527. #else
  2528. if (! fExternalPatchbayHost) return;
  2529. #endif
  2530. CARLA_SAFE_ASSERT_RETURN(oldFullName != nullptr && oldFullName[0] != '\0',);
  2531. CARLA_SAFE_ASSERT_RETURN(newFullName != nullptr && newFullName[0] != '\0',);
  2532. bool found;
  2533. CarlaString groupName(newFullName);
  2534. groupName.truncate(groupName.rfind(newShortName, &found)-1);
  2535. CARLA_SAFE_ASSERT_RETURN(found,);
  2536. uint groupId, portId = 0;
  2537. char portName[STR_MAX];
  2538. found = false;
  2539. {
  2540. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  2541. groupId = fUsedGroups.getGroupId(groupName);
  2542. CARLA_SAFE_ASSERT_RETURN(groupId != 0,);
  2543. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  2544. for (LinkedList<PortNameToId>::Itenerator it = fUsedPorts.list.begin2(); it.valid(); it.next())
  2545. {
  2546. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  2547. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  2548. if (std::strncmp(portNameToId.fullName, oldFullName, STR_MAX) == 0)
  2549. {
  2550. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group == groupId);
  2551. found = true;
  2552. portId = portNameToId.port;
  2553. std::strncpy(portName, newShortName, STR_MAX-1);
  2554. portName[STR_MAX-1] = '\0';
  2555. portNameToId.rename(newShortName, newFullName);
  2556. break;
  2557. }
  2558. }
  2559. }
  2560. if (found)
  2561. {
  2562. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2563. ENGINE_CALLBACK_PATCHBAY_PORT_CHANGED,
  2564. groupId,
  2565. static_cast<int>(portId),
  2566. 0, 0, 0.0f,
  2567. portName);
  2568. }
  2569. }
  2570. #endif
  2571. void handleJackShutdownCallback()
  2572. {
  2573. #ifndef BUILD_BRIDGE
  2574. signalThreadShouldExit();
  2575. #endif
  2576. const PendingRtEventsRunner prt(this, pData->bufferSize);
  2577. for (uint i=0; i < pData->curPluginCount; ++i)
  2578. {
  2579. if (CarlaPlugin* const plugin = pData->plugins[i].plugin)
  2580. {
  2581. plugin->tryLock(true);
  2582. if (CarlaEngineJackClient* const client = (CarlaEngineJackClient*)plugin->getEngineClient())
  2583. client->invalidate();
  2584. plugin->unlock();
  2585. }
  2586. }
  2587. fClient = nullptr;
  2588. #ifndef BUILD_BRIDGE
  2589. carla_zeroPointers(fRackPorts, kRackPortCount);
  2590. #endif
  2591. callback(true, true, ENGINE_CALLBACK_QUIT, 0, 0, 0, 0, 0.0f, nullptr);
  2592. }
  2593. // -------------------------------------------------------------------
  2594. void handlePluginJackShutdownCallback(CarlaPlugin* const plugin)
  2595. {
  2596. CarlaEngineJackClient* const engineClient((CarlaEngineJackClient*)plugin->getEngineClient());
  2597. CARLA_SAFE_ASSERT_RETURN(engineClient != nullptr,);
  2598. plugin->tryLock(true);
  2599. engineClient->invalidate();
  2600. plugin->unlock();
  2601. //if (pData->nextAction.pluginId == plugin->getId())
  2602. // pData->nextAction.clearAndReset();
  2603. callback(true, true, ENGINE_CALLBACK_PLUGIN_UNAVAILABLE, plugin->getId(), 0, 0, 0, 0.0f, "Killed by JACK");
  2604. }
  2605. // -------------------------------------------------------------------
  2606. private:
  2607. jack_client_t* fClient;
  2608. bool fExternalPatchbayHost;
  2609. bool fExternalPatchbayOsc;
  2610. bool fFreewheel;
  2611. CarlaString fClientName;
  2612. // -------------------------------------------------------------------
  2613. #ifdef BUILD_BRIDGE
  2614. bool fIsRunning;
  2615. #else
  2616. enum RackPorts {
  2617. kRackPortAudioIn1 = 0,
  2618. kRackPortAudioIn2 = 1,
  2619. kRackPortAudioOut1 = 2,
  2620. kRackPortAudioOut2 = 3,
  2621. kRackPortEventIn = 4,
  2622. kRackPortEventOut = 5,
  2623. kRackPortCount = 6
  2624. };
  2625. jack_port_t* fRackPorts[kRackPortCount];
  2626. bool fTimebaseMaster;
  2627. bool fTimebaseRolling;
  2628. uint64_t fTimebaseUsecs;
  2629. PatchbayGroupList fUsedGroups;
  2630. PatchbayPortList fUsedPorts;
  2631. PatchbayConnectionList fUsedConnections;
  2632. CarlaMutex fPatchbayProcThreadProtectionMutex;
  2633. mutable CharStringListPtr fRetConns;
  2634. void findPluginIdAndIcon(const char* const clientName, int& pluginId, PatchbayIcon& icon) const noexcept
  2635. {
  2636. carla_debug("CarlaEngineJack::findPluginIdAndIcon(\"%s\", ...)", clientName);
  2637. // TODO - this currently only works in multi-client mode
  2638. if (pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  2639. return;
  2640. jack_uuid_t uuid;
  2641. {
  2642. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, clientName);
  2643. CARLA_SAFE_ASSERT_RETURN(uuidstr != nullptr && uuidstr[0] != '\0',);
  2644. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  2645. jackbridge_free(uuidstr);
  2646. CARLA_SAFE_ASSERT_RETURN(parsed,);
  2647. }
  2648. bool clientBelongsToUs;
  2649. {
  2650. char* value = nullptr;
  2651. char* type = nullptr;
  2652. if (! jackbridge_get_property(uuid, URI_MAIN_CLIENT_NAME, &value, &type))
  2653. return;
  2654. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  2655. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  2656. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_STRING) == 0,);
  2657. clientBelongsToUs = fClientName == value;
  2658. }
  2659. {
  2660. char* value = nullptr;
  2661. char* type = nullptr;
  2662. if (! jackbridge_get_property(uuid, URI_PLUGIN_ID, &value, &type))
  2663. return;
  2664. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  2665. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  2666. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_INTEGER) == 0,);
  2667. if (clientBelongsToUs)
  2668. pluginId = std::atoi(value);
  2669. icon = PATCHBAY_ICON_PLUGIN;
  2670. jackbridge_free(value);
  2671. jackbridge_free(type);
  2672. }
  2673. {
  2674. char* value = nullptr;
  2675. char* type = nullptr;
  2676. if (! jackbridge_get_property(uuid, URI_PLUGIN_ICON, &value, &type))
  2677. return;
  2678. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  2679. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  2680. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_STRING) == 0,);
  2681. /**/ if (std::strcmp(value, "app") == 0)
  2682. icon = PATCHBAY_ICON_APPLICATION;
  2683. else if (std::strcmp(value, "application") == 0)
  2684. icon = PATCHBAY_ICON_APPLICATION;
  2685. else if (std::strcmp(value, "plugin") == 0)
  2686. icon = PATCHBAY_ICON_PLUGIN;
  2687. else if (std::strcmp(value, "hardware") == 0)
  2688. icon = PATCHBAY_ICON_HARDWARE;
  2689. else if (std::strcmp(value, "carla") == 0)
  2690. icon = PATCHBAY_ICON_CARLA;
  2691. else if (std::strcmp(value, "distrho") == 0)
  2692. icon = PATCHBAY_ICON_DISTRHO;
  2693. else if (std::strcmp(value, "file") == 0)
  2694. icon = PATCHBAY_ICON_FILE;
  2695. jackbridge_free(value);
  2696. jackbridge_free(type);
  2697. }
  2698. }
  2699. // handy stuff only needed for initJackPatchbay
  2700. struct GroupToIdData {
  2701. uint id;
  2702. PatchbayIcon icon;
  2703. int pluginId;
  2704. char strVal[STR_MAX];
  2705. };
  2706. struct PortToIdData {
  2707. uint group;
  2708. uint port;
  2709. uint flags;
  2710. char strVal[STR_MAX];
  2711. };
  2712. struct ConnectionToIdData {
  2713. uint id;
  2714. char strVal[STR_MAX];
  2715. };
  2716. void initJackPatchbay(const bool sendHost, const bool sendOSC, const char* const ourName)
  2717. {
  2718. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY ||
  2719. (fExternalPatchbayHost && sendHost) || (fExternalPatchbayOsc && sendOSC),);
  2720. CARLA_SAFE_ASSERT_RETURN(ourName != nullptr && ourName[0] != '\0',);
  2721. uint id, carlaId;
  2722. CarlaStringList parsedGroups;
  2723. LinkedList<GroupToIdData> groupCallbackData;
  2724. LinkedList<PortToIdData> portsCallbackData;
  2725. LinkedList<ConnectionToIdData> connCallbackData;
  2726. {
  2727. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  2728. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  2729. const CarlaMutexLocker cml3(fUsedConnections.mutex);
  2730. // add our client first
  2731. {
  2732. carlaId = ++fUsedGroups.lastId;
  2733. parsedGroups.append(ourName);
  2734. GroupNameToId groupNameToId;
  2735. groupNameToId.setData(carlaId, ourName);
  2736. fUsedGroups.list.append(groupNameToId);
  2737. }
  2738. // query all jack ports
  2739. {
  2740. const char** const ports = jackbridge_get_ports(fClient, nullptr, nullptr, 0);
  2741. CARLA_SAFE_ASSERT_RETURN(ports != nullptr,);
  2742. for (int i=0; ports[i] != nullptr; ++i)
  2743. {
  2744. const char* const fullPortName(ports[i]);
  2745. CARLA_SAFE_ASSERT_CONTINUE(fullPortName != nullptr && fullPortName[0] != '\0');
  2746. const jack_port_t* const jackPort(jackbridge_port_by_name(fClient, fullPortName));
  2747. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  2748. const char* const shortPortName(jackbridge_port_short_name(jackPort));
  2749. CARLA_SAFE_ASSERT_CONTINUE(shortPortName != nullptr && shortPortName[0] != '\0');
  2750. const CarlaJackPortHints jackPortHints(CarlaJackPortHints::fromPort(jackPort));
  2751. uint groupId = 0;
  2752. bool found;
  2753. CarlaString groupName(fullPortName);
  2754. groupName.truncate(groupName.rfind(shortPortName, &found)-1);
  2755. CARLA_SAFE_ASSERT_CONTINUE(found);
  2756. if (parsedGroups.contains(groupName))
  2757. {
  2758. groupId = fUsedGroups.getGroupId(groupName);
  2759. CARLA_SAFE_ASSERT_CONTINUE(groupId > 0);
  2760. }
  2761. else
  2762. {
  2763. groupId = ++fUsedGroups.lastId;
  2764. parsedGroups.append(groupName);
  2765. GroupNameToId groupNameToId;
  2766. groupNameToId.setData(groupId, groupName);
  2767. int pluginId = -1;
  2768. PatchbayIcon icon = jackPortHints.isHardware ? PATCHBAY_ICON_HARDWARE : PATCHBAY_ICON_APPLICATION;
  2769. findPluginIdAndIcon(groupName, pluginId, icon);
  2770. fUsedGroups.list.append(groupNameToId);
  2771. GroupToIdData groupData;
  2772. groupData.id = groupId;
  2773. groupData.icon = icon;
  2774. groupData.pluginId = pluginId;
  2775. std::strncpy(groupData.strVal, groupName, STR_MAX-1);
  2776. groupData.strVal[STR_MAX-1] = '\0';
  2777. groupCallbackData.append(groupData);
  2778. }
  2779. uint canvasPortFlags = 0x0;
  2780. canvasPortFlags |= jackPortHints.isInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
  2781. /**/ if (jackPortHints.isCV)
  2782. canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
  2783. else if (jackPortHints.isOSC)
  2784. canvasPortFlags |= PATCHBAY_PORT_TYPE_OSC;
  2785. else if (jackPortHints.isAudio)
  2786. canvasPortFlags |= PATCHBAY_PORT_TYPE_AUDIO;
  2787. else if (jackPortHints.isMIDI)
  2788. canvasPortFlags |= PATCHBAY_PORT_TYPE_MIDI;
  2789. id = ++fUsedPorts.lastId;
  2790. PortNameToId portNameToId;
  2791. portNameToId.setData(groupId, id, shortPortName, fullPortName);
  2792. fUsedPorts.list.append(portNameToId);
  2793. PortToIdData portData;
  2794. portData.group = groupId;
  2795. portData.port = id;
  2796. portData.flags = canvasPortFlags;
  2797. std::strncpy(portData.strVal, shortPortName, STR_MAX-1);
  2798. portData.strVal[STR_MAX-1] = '\0';
  2799. portsCallbackData.append(portData);
  2800. }
  2801. jackbridge_free(ports);
  2802. }
  2803. // query connections, after all ports are in place
  2804. if (const char** const ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
  2805. {
  2806. for (int i=0; ports[i] != nullptr; ++i)
  2807. {
  2808. const char* const fullPortName(ports[i]);
  2809. CARLA_SAFE_ASSERT_CONTINUE(fullPortName != nullptr && fullPortName[0] != '\0');
  2810. const jack_port_t* const jackPort(jackbridge_port_by_name(fClient, fullPortName));
  2811. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  2812. const PortNameToId& thisPort(fUsedPorts.getPortNameToId(fullPortName));
  2813. CARLA_SAFE_ASSERT_CONTINUE(thisPort.group > 0);
  2814. CARLA_SAFE_ASSERT_CONTINUE(thisPort.port > 0);
  2815. if (const char** const connections = jackbridge_port_get_all_connections(fClient, jackPort))
  2816. {
  2817. for (int j=0; connections[j] != nullptr; ++j)
  2818. {
  2819. const char* const connection(connections[j]);
  2820. CARLA_SAFE_ASSERT_CONTINUE(connection != nullptr && connection[0] != '\0');
  2821. const PortNameToId& targetPort(fUsedPorts.getPortNameToId(connection));
  2822. CARLA_SAFE_ASSERT_CONTINUE(targetPort.group > 0);
  2823. CARLA_SAFE_ASSERT_CONTINUE(targetPort.port > 0);
  2824. id = ++fUsedConnections.lastId;
  2825. ConnectionToId connectionToId;
  2826. connectionToId.setData(id, thisPort.group, thisPort.port, targetPort.group, targetPort.port);
  2827. fUsedConnections.list.append(connectionToId);
  2828. ConnectionToIdData connData;
  2829. connData.id = id;
  2830. std::snprintf(connData.strVal, STR_MAX-1, "%i:%i:%i:%i",
  2831. thisPort.group, thisPort.port, targetPort.group, targetPort.port);
  2832. connData.strVal[STR_MAX-1] = '\0';
  2833. connCallbackData.append(connData);
  2834. }
  2835. jackbridge_free(connections);
  2836. }
  2837. }
  2838. jackbridge_free(ports);
  2839. }
  2840. }
  2841. const GroupToIdData groupFallback = { 0, PATCHBAY_ICON_PLUGIN, -1, { '\0' } };
  2842. const PortToIdData portFallback = { 0, 0, 0, { '\0' } };
  2843. const ConnectionToIdData connFallback = { 0, { '\0' } };
  2844. callback(sendHost, sendOSC,
  2845. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  2846. carlaId,
  2847. PATCHBAY_ICON_CARLA,
  2848. MAIN_CARLA_PLUGIN_ID,
  2849. 0, 0.0f,
  2850. ourName);
  2851. for (LinkedList<GroupToIdData>::Itenerator it = groupCallbackData.begin2(); it.valid(); it.next())
  2852. {
  2853. const GroupToIdData& group(it.getValue(groupFallback));
  2854. callback(sendHost, sendOSC,
  2855. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  2856. group.id,
  2857. group.icon,
  2858. group.pluginId,
  2859. 0, 0.0f,
  2860. group.strVal);
  2861. jack_uuid_t uuid;
  2862. {
  2863. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, group.strVal);
  2864. CARLA_SAFE_ASSERT_RETURN(uuidstr != nullptr && uuidstr[0] != '\0',);
  2865. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  2866. jackbridge_free(uuidstr);
  2867. CARLA_SAFE_ASSERT_RETURN(parsed,);
  2868. }
  2869. char* value = nullptr;
  2870. char* type = nullptr;
  2871. if (jackbridge_get_property(uuid, URI_POSITION, &value, &type)
  2872. && value != nullptr
  2873. && type != nullptr
  2874. && std::strcmp(type, URI_TYPE_STRING) == 0)
  2875. {
  2876. if (char* sep1 = std::strstr(value, ":"))
  2877. {
  2878. int x1, y1 = 0, x2 = 0, y2 = 0;
  2879. *sep1++ = '\0';
  2880. x1 = std::atoi(value);
  2881. if (char* sep2 = std::strstr(sep1, ":"))
  2882. {
  2883. *sep2++ = '\0';
  2884. y1 = std::atoi(sep1);
  2885. if (char* sep3 = std::strstr(sep2, ":"))
  2886. {
  2887. *sep3++ = '\0';
  2888. x2 = std::atoi(sep2);
  2889. y2 = std::atoi(sep3);
  2890. }
  2891. }
  2892. jackbridge_free(value);
  2893. jackbridge_free(type);
  2894. callback(sendHost, sendOSC,
  2895. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  2896. group.id, x1, y1, x2, static_cast<float>(y2),
  2897. nullptr);
  2898. }
  2899. }
  2900. }
  2901. for (LinkedList<PortToIdData>::Itenerator it = portsCallbackData.begin2(); it.valid(); it.next())
  2902. {
  2903. const PortToIdData& port(it.getValue(portFallback));
  2904. callback(sendHost, sendOSC,
  2905. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  2906. port.group,
  2907. static_cast<int>(port.port),
  2908. static_cast<int>(port.flags),
  2909. 0, 0.0f,
  2910. port.strVal);
  2911. }
  2912. for (LinkedList<ConnectionToIdData>::Itenerator it = connCallbackData.begin2(); it.valid(); it.next())
  2913. {
  2914. const ConnectionToIdData& conn(it.getValue(connFallback));
  2915. callback(sendHost, sendOSC,
  2916. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  2917. conn.id,
  2918. 0, 0, 0, 0.0f,
  2919. conn.strVal);
  2920. }
  2921. groupCallbackData.clear();
  2922. portsCallbackData.clear();
  2923. connCallbackData.clear();
  2924. }
  2925. #endif
  2926. // -------------------------------------------------------------------
  2927. void processPlugin(CarlaPlugin* const plugin, const uint32_t nframes)
  2928. {
  2929. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2930. CarlaEngineJackClient* const client = (CarlaEngineJackClient*)plugin->getEngineClient();
  2931. CarlaEngineJackCVSourcePorts& cvSourcePorts(client->getCVSourcePorts());
  2932. const CarlaRecursiveMutexTryLocker crmtl(cvSourcePorts.getMutex(), fFreewheel);
  2933. // const CarlaRecursiveMutexLocker crml(cvSourcePorts.getMutex());
  2934. #endif
  2935. /*
  2936. const uint32_t audioInCount = client->getPortCount(kEnginePortTypeAudio, true);
  2937. const uint32_t audioOutCount = client->getPortCount(kEnginePortTypeAudio, false);
  2938. const uint32_t cvInCount = client->getPortCount(kEnginePortTypeCV, true);
  2939. const uint32_t cvOutCount = client->getPortCount(kEnginePortTypeCV, false);
  2940. */
  2941. const uint32_t audioInCount = plugin->getAudioInCount();
  2942. const uint32_t audioOutCount = plugin->getAudioOutCount();
  2943. const uint32_t cvInCount = plugin->getCVInCount();
  2944. const uint32_t cvOutCount = plugin->getCVOutCount();
  2945. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2946. const uint32_t cvsInCount = crmtl.wasLocked() ? cvSourcePorts.getPortCount() : 0;
  2947. #else
  2948. const uint32_t cvsInCount = 0;
  2949. #endif
  2950. const float* audioIn[audioInCount];
  2951. /* */ float* audioOut[audioOutCount];
  2952. const float* cvIn[cvInCount+cvsInCount];
  2953. /* */ float* cvOut[cvOutCount];
  2954. for (uint32_t i=0; i < audioInCount; ++i)
  2955. {
  2956. if (CarlaEngineAudioPort* const port = plugin->getAudioInPort(i))
  2957. audioIn[i] = port->getBuffer();
  2958. else
  2959. audioIn[i] = nullptr;
  2960. }
  2961. for (uint32_t i=0; i < audioOutCount; ++i)
  2962. {
  2963. if (CarlaEngineAudioPort* const port = plugin->getAudioOutPort(i))
  2964. audioOut[i] = port->getBuffer();
  2965. else
  2966. audioOut[i] = nullptr;
  2967. }
  2968. for (uint32_t i=0; i < cvInCount; ++i)
  2969. {
  2970. if (CarlaEngineCVPort* const port = plugin->getCVInPort(i))
  2971. cvIn[i] = port->getBuffer();
  2972. else
  2973. cvIn[i] = nullptr;
  2974. }
  2975. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2976. for (uint32_t i=cvInCount, j=0; j < cvsInCount; ++i, ++j)
  2977. {
  2978. if (CarlaEngineCVPort* const port = cvSourcePorts.getPort(j))
  2979. {
  2980. port->initBuffer();
  2981. cvIn[i] = port->getBuffer();
  2982. }
  2983. else
  2984. {
  2985. cvIn[i] = nullptr;
  2986. }
  2987. }
  2988. #endif
  2989. for (uint32_t i=0; i < cvOutCount; ++i)
  2990. {
  2991. if (CarlaEngineCVPort* const port = plugin->getCVOutPort(i))
  2992. cvOut[i] = port->getBuffer();
  2993. else
  2994. cvOut[i] = nullptr;
  2995. }
  2996. float inPeaks[2] = { 0.0f };
  2997. float outPeaks[2] = { 0.0f };
  2998. for (uint32_t i=0; i < audioInCount && i < 2; ++i)
  2999. {
  3000. for (uint32_t j=0; j < nframes; ++j)
  3001. {
  3002. const float absV(std::abs(audioIn[i][j]));
  3003. if (absV > inPeaks[i])
  3004. inPeaks[i] = absV;
  3005. }
  3006. }
  3007. plugin->process(audioIn, audioOut, cvIn, cvOut, nframes);
  3008. for (uint32_t i=0; i < audioOutCount && i < 2; ++i)
  3009. {
  3010. for (uint32_t j=0; j < nframes; ++j)
  3011. {
  3012. const float absV(std::abs(audioOut[i][j]));
  3013. if (absV > outPeaks[i])
  3014. outPeaks[i] = absV;
  3015. }
  3016. }
  3017. setPluginPeaksRT(plugin->getId(), inPeaks, outPeaks);
  3018. }
  3019. #ifndef BUILD_BRIDGE
  3020. // -------------------------------------------------------------------
  3021. struct PostPonedJackEvent {
  3022. enum Type {
  3023. kTypeNull = 0,
  3024. kTypeClientUnregister,
  3025. kTypeClientPositionChange,
  3026. kTypePortRegister,
  3027. kTypePortUnregister,
  3028. kTypePortConnect,
  3029. kTypePortDisconnect,
  3030. kTypePortRename
  3031. };
  3032. Type type;
  3033. union {
  3034. struct {
  3035. char name[STR_MAX+1];
  3036. } clientUnregister;
  3037. struct {
  3038. jack_uuid_t uuid;
  3039. } clientPositionChange;
  3040. struct {
  3041. char shortName[STR_MAX+1];
  3042. char fullName[STR_MAX+1];
  3043. CarlaJackPortHints hints;
  3044. } portRegister;
  3045. struct {
  3046. char fullName[STR_MAX+1];
  3047. } portUnregister;
  3048. struct {
  3049. char oldFullName[STR_MAX+1];
  3050. char newFullName[STR_MAX+1];
  3051. char newShortName[STR_MAX+1];
  3052. } portRename;
  3053. struct {
  3054. char portNameA[STR_MAX+1];
  3055. char portNameB[STR_MAX+1];
  3056. } portConnect;
  3057. struct {
  3058. char portNameA[STR_MAX+1];
  3059. char portNameB[STR_MAX+1];
  3060. } portDisconnect;
  3061. };
  3062. };
  3063. LinkedList<PostPonedJackEvent> fPostPonedEvents;
  3064. CarlaMutex fPostPonedEventsMutex;
  3065. water::Array<jack_uuid_t> fPostPonedUUIDs;
  3066. CarlaMutex fPostPonedUUIDsMutex;
  3067. bool fIsInternalClient;
  3068. void postPoneJackCallback(PostPonedJackEvent& ev)
  3069. {
  3070. const CarlaMutexLocker cml(fPostPonedEventsMutex);
  3071. fPostPonedEvents.append(ev);
  3072. }
  3073. void run() override
  3074. {
  3075. LinkedList<PostPonedJackEvent> events;
  3076. PostPonedJackEvent nullEvent;
  3077. carla_zeroStruct(nullEvent);
  3078. for (; ! shouldThreadExit();)
  3079. {
  3080. if (fIsInternalClient)
  3081. idle();
  3082. {
  3083. const CarlaMutexLocker cml(fPostPonedEventsMutex);
  3084. if (fPostPonedEvents.count() > 0)
  3085. fPostPonedEvents.moveTo(events);
  3086. }
  3087. if (fClient == nullptr)
  3088. break;
  3089. if (events.count() == 0)
  3090. {
  3091. carla_msleep(fIsInternalClient ? 50 : 200);
  3092. continue;
  3093. }
  3094. for (LinkedList<PostPonedJackEvent>::Itenerator it = events.begin2(); it.valid(); it.next())
  3095. {
  3096. const PostPonedJackEvent& ev(it.getValue(nullEvent));
  3097. CARLA_SAFE_ASSERT_CONTINUE(ev.type != PostPonedJackEvent::kTypeNull);
  3098. switch (ev.type)
  3099. {
  3100. case PostPonedJackEvent::kTypeNull:
  3101. break;
  3102. case PostPonedJackEvent::kTypeClientUnregister:
  3103. handleJackClientUnregistrationCallback(ev.clientUnregister.name);
  3104. break;
  3105. case PostPonedJackEvent::kTypeClientPositionChange:
  3106. handleJackClientPositionChangeCallback(ev.clientPositionChange.uuid);
  3107. break;
  3108. case PostPonedJackEvent::kTypePortRegister:
  3109. handleJackPortRegistrationCallback(ev.portRegister.fullName,
  3110. ev.portRegister.shortName,
  3111. ev.portRegister.hints);
  3112. break;
  3113. case PostPonedJackEvent::kTypePortUnregister:
  3114. handleJackPortUnregistrationCallback(ev.portUnregister.fullName);
  3115. break;
  3116. case PostPonedJackEvent::kTypePortConnect:
  3117. handleJackPortConnectCallback(ev.portConnect.portNameA,
  3118. ev.portConnect.portNameB);
  3119. break;
  3120. case PostPonedJackEvent::kTypePortDisconnect:
  3121. handleJackPortDisconnectCallback(ev.portDisconnect.portNameA,
  3122. ev.portDisconnect.portNameB);
  3123. break;
  3124. case PostPonedJackEvent::kTypePortRename:
  3125. handleJackPortRenameCallback(ev.portRename.oldFullName,
  3126. ev.portRename.newFullName,
  3127. ev.portRename.newShortName);
  3128. break;
  3129. }
  3130. }
  3131. events.clear();
  3132. }
  3133. events.clear();
  3134. }
  3135. #endif // BUILD_BRIDGE
  3136. // -------------------------------------------------------------------
  3137. // disable -Wattributes warnings
  3138. #if defined(__clang__)
  3139. # pragma clang diagnostic push
  3140. # pragma clang diagnostic ignored "-Wattributes"
  3141. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  3142. # pragma GCC diagnostic push
  3143. # pragma GCC diagnostic ignored "-Wattributes"
  3144. #endif
  3145. #define handlePtr ((CarlaEngineJack*)arg)
  3146. static void JACKBRIDGE_API carla_jack_thread_init_callback(void*)
  3147. {
  3148. #ifdef __SSE2_MATH__
  3149. // Set FTZ and DAZ flags
  3150. _mm_setcsr(_mm_getcsr() | 0x8040);
  3151. #endif
  3152. }
  3153. static int JACKBRIDGE_API carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  3154. {
  3155. handlePtr->handleJackBufferSizeCallback(newBufferSize);
  3156. return 0;
  3157. }
  3158. static int JACKBRIDGE_API carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  3159. {
  3160. handlePtr->handleJackSampleRateCallback(newSampleRate);
  3161. return 0;
  3162. }
  3163. static void JACKBRIDGE_API carla_jack_freewheel_callback(int starting, void* arg)
  3164. {
  3165. handlePtr->handleJackFreewheelCallback(bool(starting));
  3166. }
  3167. static void JACKBRIDGE_API carla_jack_latency_callback(jack_latency_callback_mode_t mode, void* arg)
  3168. {
  3169. handlePtr->handleJackLatencyCallback(mode);
  3170. }
  3171. static int JACKBRIDGE_API carla_jack_process_callback(jack_nframes_t nframes, void* arg) __attribute__((annotate("realtime")))
  3172. {
  3173. handlePtr->handleJackProcessCallback(nframes);
  3174. return 0;
  3175. }
  3176. #ifndef BUILD_BRIDGE
  3177. static void JACKBRIDGE_API carla_jack_timebase_callback(jack_transport_state_t, jack_nframes_t nframes, jack_position_t* pos, int new_pos, void* arg) __attribute__((annotate("realtime")))
  3178. {
  3179. handlePtr->handleJackTimebaseCallback(nframes, pos, new_pos);
  3180. }
  3181. static void JACKBRIDGE_API carla_jack_client_registration_callback(const char* name, int reg, void* arg)
  3182. {
  3183. // ignored
  3184. if (reg != 0)
  3185. return;
  3186. PostPonedJackEvent ev;
  3187. carla_zeroStruct(ev);
  3188. ev.type = PostPonedJackEvent::kTypeClientUnregister;
  3189. std::strncpy(ev.clientUnregister.name, name, STR_MAX);
  3190. handlePtr->postPoneJackCallback(ev);
  3191. }
  3192. static void JACKBRIDGE_API carla_jack_port_registration_callback(jack_port_id_t port_id, int reg, void* arg)
  3193. {
  3194. const jack_port_t* const port = jackbridge_port_by_id(handlePtr->fClient, port_id);
  3195. CARLA_SAFE_ASSERT_RETURN(port != nullptr,);
  3196. const char* const fullName = jackbridge_port_name(port);
  3197. CARLA_SAFE_ASSERT_RETURN(fullName != nullptr && fullName[0] != '\0',);
  3198. PostPonedJackEvent ev;
  3199. carla_zeroStruct(ev);
  3200. if (reg != 0)
  3201. {
  3202. const char* const shortName = jackbridge_port_short_name(port);
  3203. CARLA_SAFE_ASSERT_RETURN(shortName != nullptr && shortName[0] != '\0',);
  3204. ev.type = PostPonedJackEvent::kTypePortRegister;
  3205. std::strncpy(ev.portRegister.fullName, fullName, STR_MAX);
  3206. std::strncpy(ev.portRegister.shortName, shortName, STR_MAX);
  3207. ev.portRegister.hints = CarlaJackPortHints::fromPort(port);
  3208. }
  3209. else
  3210. {
  3211. ev.type = PostPonedJackEvent::kTypePortUnregister;
  3212. std::strncpy(ev.portUnregister.fullName, fullName, STR_MAX);
  3213. }
  3214. handlePtr->postPoneJackCallback(ev);
  3215. }
  3216. static void JACKBRIDGE_API carla_jack_port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  3217. {
  3218. const jack_port_t* const portA = jackbridge_port_by_id(handlePtr->fClient, a);
  3219. CARLA_SAFE_ASSERT_RETURN(portA != nullptr,);
  3220. const jack_port_t* const portB = jackbridge_port_by_id(handlePtr->fClient, b);
  3221. CARLA_SAFE_ASSERT_RETURN(portB != nullptr,);
  3222. const char* const fullNameA = jackbridge_port_name(portA);
  3223. CARLA_SAFE_ASSERT_RETURN(fullNameA != nullptr && fullNameA[0] != '\0',);
  3224. const char* const fullNameB = jackbridge_port_name(portB);
  3225. CARLA_SAFE_ASSERT_RETURN(fullNameB != nullptr && fullNameB[0] != '\0',);
  3226. PostPonedJackEvent ev;
  3227. carla_zeroStruct(ev);
  3228. if (connect != 0)
  3229. {
  3230. ev.type = PostPonedJackEvent::kTypePortConnect;
  3231. std::strncpy(ev.portConnect.portNameA, fullNameA, STR_MAX);
  3232. std::strncpy(ev.portConnect.portNameB, fullNameB, STR_MAX);
  3233. }
  3234. else
  3235. {
  3236. ev.type = PostPonedJackEvent::kTypePortDisconnect;
  3237. std::strncpy(ev.portDisconnect.portNameA, fullNameA, STR_MAX);
  3238. std::strncpy(ev.portDisconnect.portNameB, fullNameB, STR_MAX);
  3239. }
  3240. handlePtr->postPoneJackCallback(ev);
  3241. }
  3242. static void JACKBRIDGE_API carla_jack_port_rename_callback(jack_port_id_t port_id, const char* oldName, const char* newName, void* arg)
  3243. {
  3244. const jack_port_t* const port = jackbridge_port_by_id(handlePtr->fClient, port_id);
  3245. CARLA_SAFE_ASSERT_RETURN(port != nullptr,);
  3246. const char* const shortName = jackbridge_port_short_name(port);
  3247. CARLA_SAFE_ASSERT_RETURN(shortName != nullptr && shortName[0] != '\0',);
  3248. PostPonedJackEvent ev;
  3249. carla_zeroStruct(ev);
  3250. ev.type = PostPonedJackEvent::kTypePortRename;
  3251. std::strncpy(ev.portRename.oldFullName, oldName, STR_MAX);
  3252. std::strncpy(ev.portRename.newFullName, newName, STR_MAX);
  3253. std::strncpy(ev.portRename.newShortName, shortName, STR_MAX);
  3254. handlePtr->postPoneJackCallback(ev);
  3255. }
  3256. static void carla_jack_property_change_callback(jack_uuid_t subject, const char* key, jack_property_change_t change, void* arg)
  3257. {
  3258. if (change != PropertyChanged)
  3259. return;
  3260. if (std::strcmp(key, URI_POSITION) != 0)
  3261. return;
  3262. PostPonedJackEvent ev;
  3263. carla_zeroStruct(ev);
  3264. ev.type = PostPonedJackEvent::kTypeClientPositionChange;
  3265. ev.clientPositionChange.uuid = subject;
  3266. handlePtr->postPoneJackCallback(ev);
  3267. }
  3268. static int JACKBRIDGE_API carla_jack_xrun_callback(void* arg)
  3269. {
  3270. ++(handlePtr->pData->xruns);
  3271. return 0;
  3272. }
  3273. #endif
  3274. static void JACKBRIDGE_API carla_jack_shutdown_callback(void* arg)
  3275. {
  3276. handlePtr->handleJackShutdownCallback();
  3277. }
  3278. #undef handlePtr
  3279. // -------------------------------------------------------------------
  3280. #ifndef BUILD_BRIDGE
  3281. static int JACKBRIDGE_API carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg) __attribute__((annotate("realtime")))
  3282. {
  3283. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  3284. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr && plugin->isEnabled(), 0);
  3285. CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
  3286. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, 0);
  3287. if (plugin->tryLock(engine->fFreewheel))
  3288. {
  3289. plugin->initBuffers();
  3290. engine->processPlugin(plugin, nframes);
  3291. plugin->unlock();
  3292. }
  3293. return 0;
  3294. }
  3295. /*
  3296. static int JACKBRIDGE_API carla_jack_bufsize_callback_plugin(jack_nframes_t nframes, void* arg)
  3297. {
  3298. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  3299. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr && plugin->isEnabled(), 0);
  3300. plugin->bufferSizeChanged(nframes);
  3301. return 1;
  3302. }
  3303. static int JACKBRIDGE_API carla_jack_srate_callback_plugin(jack_nframes_t nframes, void* arg)
  3304. {
  3305. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  3306. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr && plugin->isEnabled(), 0);
  3307. plugin->sampleRateChanged(nframes);
  3308. return 1;
  3309. }
  3310. */
  3311. static void JACKBRIDGE_API carla_jack_latency_callback_plugin(jack_latency_callback_mode_t /*mode*/, void* /*arg*/)
  3312. {
  3313. // TODO
  3314. }
  3315. static void JACKBRIDGE_API carla_jack_shutdown_callback_plugin(void* arg)
  3316. {
  3317. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  3318. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  3319. CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
  3320. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  3321. engine->handlePluginJackShutdownCallback(plugin);
  3322. }
  3323. #endif
  3324. // enable -Wattributes again
  3325. #if defined(__clang__)
  3326. # pragma clang diagnostic pop
  3327. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  3328. # pragma GCC diagnostic pop
  3329. #endif
  3330. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
  3331. };
  3332. // -----------------------------------------------------------------------
  3333. namespace EngineInit {
  3334. CarlaEngine* newJack()
  3335. {
  3336. carla_debug("EngineInit::newJack()");
  3337. return new CarlaEngineJack();
  3338. }
  3339. }
  3340. // -----------------------------------------------------------------------
  3341. CARLA_BACKEND_END_NAMESPACE
  3342. #if defined(JACKBRIDGE_DIRECT) && !defined(BUILD_BRIDGE)
  3343. // -----------------------------------------------------------------------
  3344. // internal jack client
  3345. CARLA_EXPORT
  3346. int jack_initialize (jack_client_t *client, const char *load_init);
  3347. CARLA_EXPORT
  3348. void jack_finish(void *arg);
  3349. #ifdef CARLA_OS_UNIX
  3350. # include "ThreadSafeFFTW.hpp"
  3351. static ThreadSafeFFTW sThreadSafeFFTW;
  3352. #endif
  3353. // -----------------------------------------------------------------------
  3354. CARLA_EXPORT
  3355. int jack_initialize(jack_client_t* const client, const char* const load_init)
  3356. {
  3357. CARLA_BACKEND_USE_NAMESPACE
  3358. EngineProcessMode mode;
  3359. if (load_init != nullptr && std::strcmp(load_init, "rack") == 0)
  3360. mode = ENGINE_PROCESS_MODE_CONTINUOUS_RACK;
  3361. else
  3362. mode = ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS;
  3363. #ifdef USING_JUCE
  3364. juce::initialiseJuce_GUI();
  3365. #endif
  3366. CarlaEngineJack* const engine = new CarlaEngineJack();
  3367. engine->setOption(ENGINE_OPTION_FORCE_STEREO, 1, nullptr);
  3368. engine->setOption(ENGINE_OPTION_AUDIO_DRIVER, 0, "JACK");
  3369. engine->setOption(ENGINE_OPTION_AUDIO_DEVICE, 0, "Auto-Connect ON");
  3370. engine->setOption(ENGINE_OPTION_OSC_ENABLED, 1, nullptr);
  3371. engine->setOption(ENGINE_OPTION_OSC_PORT_TCP, 22752, nullptr);
  3372. engine->setOption(ENGINE_OPTION_OSC_PORT_UDP, 22752, nullptr);
  3373. engine->setOption(ENGINE_OPTION_PROCESS_MODE, mode, nullptr);
  3374. engine->setOption(ENGINE_OPTION_TRANSPORT_MODE, ENGINE_TRANSPORT_MODE_JACK, nullptr);
  3375. // FIXME
  3376. engine->setOption(ENGINE_OPTION_PATH_BINARIES, 0, "/usr/lib/carla");
  3377. engine->setOption(ENGINE_OPTION_PATH_RESOURCES, 0, "/usr/share/resources");
  3378. if (engine->initInternal(client))
  3379. {
  3380. #ifdef CARLA_OS_UNIX
  3381. sThreadSafeFFTW.init();
  3382. #endif
  3383. return 0;
  3384. }
  3385. else
  3386. {
  3387. delete engine;
  3388. #ifdef USING_JUCE
  3389. juce::shutdownJuce_GUI();
  3390. #endif
  3391. return 1;
  3392. }
  3393. }
  3394. CARLA_EXPORT
  3395. void jack_finish(void *arg)
  3396. {
  3397. CARLA_BACKEND_USE_NAMESPACE
  3398. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;;
  3399. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  3400. #ifdef CARLA_OS_UNIX
  3401. const ThreadSafeFFTW::Deinitializer tsfftwde(sThreadSafeFFTW);
  3402. #endif
  3403. engine->setAboutToClose();
  3404. engine->removeAllPlugins();
  3405. engine->close();
  3406. delete engine;
  3407. #ifdef USING_JUCE
  3408. juce::shutdownJuce_GUI();
  3409. #endif
  3410. }
  3411. // -----------------------------------------------------------------------
  3412. #endif // defined(JACKBRIDGE_DIRECT) && !defined(BUILD_BRIDGE)