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.

2065 lines
71KB

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