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.

2321 lines
81KB

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