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.

2187 lines
77KB

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