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.

2071 lines
72KB

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