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.

2108 lines
69KB

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