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.

1664 lines
55KB

  1. /*
  2. * Carla JACK Engine
  3. * Copyright (C) 2012-2013 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 GPL.txt file
  16. */
  17. #if 1//def WANT_JACK
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "jackbridge/jackbridge.h"
  22. #include <cmath>
  23. CARLA_BACKEND_START_NAMESPACE
  24. #if 0
  25. } // Fix editor indentation
  26. #endif
  27. // -------------------------------------------------------------------
  28. // Helpers, defined in CarlaPlugin.cpp
  29. extern CarlaEngine* CarlaPluginGetEngine(CarlaPlugin* const plugin);
  30. extern CarlaEngineAudioPort* CarlaPluginGetAudioInPort(CarlaPlugin* const plugin, uint32_t index);
  31. extern CarlaEngineAudioPort* CarlaPluginGetAudioOutPort(CarlaPlugin* const plugin, uint32_t index);
  32. // -------------------------------------------------------------------------------------------------------------------
  33. // Carla Engine JACK-Audio port
  34. class CarlaEngineJackAudioPort : public CarlaEngineAudioPort
  35. {
  36. public:
  37. CarlaEngineJackAudioPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
  38. : CarlaEngineAudioPort(isInput, processMode),
  39. kClient(client),
  40. kPort(port)
  41. {
  42. carla_debug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
  43. if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  44. {
  45. CARLA_ASSERT(client != nullptr && port != nullptr);
  46. }
  47. else
  48. {
  49. CARLA_ASSERT(client == nullptr && port == nullptr);
  50. }
  51. }
  52. ~CarlaEngineJackAudioPort()
  53. {
  54. carla_debug("CarlaEngineJackAudioPort::~CarlaEngineJackAudioPort()");
  55. if (kClient != nullptr && kPort != nullptr)
  56. jackbridge_port_unregister(kClient, kPort);
  57. }
  58. void initBuffer(CarlaEngine* const engine)
  59. {
  60. CARLA_ASSERT(engine != nullptr);
  61. if (engine == nullptr)
  62. {
  63. fBuffer = nullptr;
  64. return;
  65. }
  66. if (kPort == nullptr)
  67. return CarlaEngineAudioPort::initBuffer(engine);
  68. fBuffer = (float*)jackbridge_port_get_buffer(kPort, engine->getBufferSize());
  69. if (! kIsInput)
  70. carla_zeroFloat(fBuffer, engine->getBufferSize());
  71. }
  72. private:
  73. jack_client_t* const kClient;
  74. jack_port_t* const kPort;
  75. friend class CarlaEngineJack;
  76. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackAudioPort)
  77. };
  78. // -------------------------------------------------------------------------------------------------------------------
  79. // Carla Engine JACK-Event port
  80. static const EngineEvent kFallbackJackEngineEvent;
  81. class CarlaEngineJackEventPort : public CarlaEngineEventPort
  82. {
  83. public:
  84. CarlaEngineJackEventPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
  85. : CarlaEngineEventPort(isInput, processMode),
  86. kClient(client),
  87. kPort(port),
  88. fJackBuffer(nullptr)
  89. {
  90. carla_debug("CarlaEngineJackEventPort::CarlaEngineJackEventPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
  91. if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  92. {
  93. CARLA_ASSERT(client != nullptr && port != nullptr);
  94. }
  95. else
  96. {
  97. CARLA_ASSERT(client == nullptr && port == nullptr);
  98. }
  99. }
  100. ~CarlaEngineJackEventPort()
  101. {
  102. carla_debug("CarlaEngineJackEventPort::~CarlaEngineJackEventPort()");
  103. if (kClient != nullptr && kPort != nullptr)
  104. jackbridge_port_unregister(kClient, kPort);
  105. }
  106. void initBuffer(CarlaEngine* const engine)
  107. {
  108. CARLA_ASSERT(engine != nullptr);
  109. if (engine == nullptr)
  110. {
  111. fJackBuffer = nullptr;
  112. return;
  113. }
  114. if (kPort == nullptr)
  115. return CarlaEngineEventPort::initBuffer(engine);
  116. fJackBuffer = jackbridge_port_get_buffer(kPort, engine->getBufferSize());
  117. if (! kIsInput)
  118. jackbridge_midi_clear_buffer(fJackBuffer);
  119. }
  120. uint32_t getEventCount()
  121. {
  122. if (kPort == nullptr)
  123. return CarlaEngineEventPort::getEventCount();
  124. CARLA_ASSERT(kIsInput);
  125. CARLA_ASSERT(fJackBuffer != nullptr);
  126. if (! kIsInput)
  127. return 0;
  128. if (fJackBuffer == nullptr)
  129. return 0;
  130. return jackbridge_midi_get_event_count(fJackBuffer);
  131. }
  132. const EngineEvent& getEvent(const uint32_t index)
  133. {
  134. if (kPort == nullptr)
  135. return CarlaEngineEventPort::getEvent(index);
  136. CARLA_ASSERT(kIsInput);
  137. CARLA_ASSERT(fJackBuffer != nullptr);
  138. if (! kIsInput)
  139. return kFallbackJackEngineEvent;
  140. if (fJackBuffer == nullptr)
  141. return kFallbackJackEngineEvent;
  142. jack_midi_event_t jackEvent;
  143. if (jackbridge_midi_event_get(&jackEvent, fJackBuffer, index) != 0 || jackEvent.size > 3)
  144. return kFallbackJackEngineEvent;
  145. fRetEvent.clear();
  146. const uint8_t midiStatus = MIDI_GET_STATUS_FROM_DATA(jackEvent.buffer);
  147. const uint8_t midiChannel = MIDI_GET_CHANNEL_FROM_DATA(jackEvent.buffer);
  148. fRetEvent.time = jackEvent.time;
  149. fRetEvent.channel = midiChannel;
  150. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  151. {
  152. const uint8_t midiControl = jackEvent.buffer[1];
  153. fRetEvent.type = kEngineEventTypeControl;
  154. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  155. {
  156. const uint8_t midiBank = jackEvent.buffer[2];
  157. fRetEvent.ctrl.type = kEngineControlEventTypeMidiBank;
  158. fRetEvent.ctrl.param = midiBank;
  159. fRetEvent.ctrl.value = 0.0;
  160. }
  161. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  162. {
  163. fRetEvent.ctrl.type = kEngineControlEventTypeAllSoundOff;
  164. fRetEvent.ctrl.param = 0;
  165. fRetEvent.ctrl.value = 0.0;
  166. }
  167. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  168. {
  169. fRetEvent.ctrl.type = kEngineControlEventTypeAllNotesOff;
  170. fRetEvent.ctrl.param = 0;
  171. fRetEvent.ctrl.value = 0.0;
  172. }
  173. else
  174. {
  175. const uint8_t midiValue = jackEvent.buffer[2];
  176. fRetEvent.ctrl.type = kEngineControlEventTypeParameter;
  177. fRetEvent.ctrl.param = midiControl;
  178. fRetEvent.ctrl.value = double(midiValue)/127.0;
  179. }
  180. }
  181. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  182. {
  183. const uint8_t midiProgram = jackEvent.buffer[1];
  184. fRetEvent.type = kEngineEventTypeControl;
  185. fRetEvent.ctrl.type = kEngineControlEventTypeMidiProgram;
  186. fRetEvent.ctrl.param = midiProgram;
  187. fRetEvent.ctrl.value = 0.0;
  188. }
  189. else
  190. {
  191. fRetEvent.type = kEngineEventTypeMidi;
  192. fRetEvent.midi.data[0] = midiStatus;
  193. fRetEvent.midi.data[1] = jackEvent.buffer[1];
  194. fRetEvent.midi.data[2] = jackEvent.buffer[2];
  195. fRetEvent.midi.size = static_cast<uint8_t>(jackEvent.size);
  196. }
  197. return fRetEvent;
  198. }
  199. void writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const double value)
  200. {
  201. if (kPort == nullptr)
  202. return CarlaEngineEventPort::writeControlEvent(time, channel, type, param, value);
  203. CARLA_ASSERT(! kIsInput);
  204. CARLA_ASSERT(fJackBuffer != nullptr);
  205. CARLA_ASSERT(type != kEngineControlEventTypeNull);
  206. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  207. CARLA_ASSERT(param < MAX_MIDI_VALUE);
  208. CARLA_SAFE_ASSERT(value >= 0.0 && value <= 1.0);
  209. if (kIsInput)
  210. return;
  211. if (fJackBuffer == nullptr)
  212. return;
  213. if (type == kEngineControlEventTypeNull)
  214. return;
  215. if (channel >= MAX_MIDI_CHANNELS)
  216. return;
  217. if (param >= MAX_MIDI_VALUE)
  218. return;
  219. if (type == kEngineControlEventTypeParameter)
  220. {
  221. CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  222. }
  223. const double fixedValue = carla_fixValue<double>(0.0, 1.0, value);
  224. uint8_t data[3] = { 0 };
  225. uint8_t size = 0;
  226. switch (type)
  227. {
  228. case kEngineControlEventTypeNull:
  229. break;
  230. case kEngineControlEventTypeParameter:
  231. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  232. data[1] = static_cast<uint8_t>(param);
  233. data[2] = uint8_t(fixedValue * 127.0);
  234. size = 3;
  235. break;
  236. case kEngineControlEventTypeMidiBank:
  237. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  238. data[1] = MIDI_CONTROL_BANK_SELECT;
  239. data[2] = static_cast<uint8_t>(param);
  240. size = 3;
  241. break;
  242. case kEngineControlEventTypeMidiProgram:
  243. data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel;
  244. data[1] = static_cast<uint8_t>(param);
  245. size = 2;
  246. break;
  247. case kEngineControlEventTypeAllSoundOff:
  248. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  249. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  250. size = 2;
  251. break;
  252. case kEngineControlEventTypeAllNotesOff:
  253. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  254. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  255. size = 2;
  256. break;
  257. }
  258. if (size > 0)
  259. jackbridge_midi_event_write(fJackBuffer, time, data, size);
  260. }
  261. void writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t port, const uint8_t* const data, const uint8_t size)
  262. {
  263. if (kPort == nullptr)
  264. return CarlaEngineEventPort::writeMidiEvent(time, channel, port, data, size);
  265. CARLA_ASSERT(! kIsInput);
  266. CARLA_ASSERT(fJackBuffer != nullptr);
  267. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  268. CARLA_ASSERT(data != nullptr);
  269. CARLA_ASSERT(size > 0);
  270. if (kIsInput)
  271. return;
  272. if (fJackBuffer == nullptr)
  273. return;
  274. if (channel >= MAX_MIDI_CHANNELS)
  275. return;
  276. if (data == nullptr)
  277. return;
  278. if (size == 0)
  279. return;
  280. uint8_t jdata[size];
  281. carla_copy<uint8_t>(jdata, data, size);
  282. jdata[0] = data[0] + channel;
  283. jackbridge_midi_event_write(fJackBuffer, time, jdata, size);
  284. }
  285. private:
  286. jack_client_t* const kClient;
  287. jack_port_t* const kPort;
  288. void* fJackBuffer;
  289. EngineEvent fRetEvent;
  290. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackEventPort)
  291. };
  292. // -------------------------------------------------------------------------------------------------------------------
  293. // Jack Engine client
  294. class CarlaEngineJackClient : public CarlaEngineClient
  295. {
  296. public:
  297. CarlaEngineJackClient(const EngineType engineType, const ProcessMode processMode, jack_client_t* const client)
  298. : CarlaEngineClient(engineType, processMode),
  299. kClient(client),
  300. kUseClient(processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  301. {
  302. carla_debug("CarlaEngineJackClient::CarlaEngineJackClient(%s, %s, %p)", EngineType2Str(engineType), ProcessMode2Str(processMode), client);
  303. if (kUseClient)
  304. {
  305. CARLA_ASSERT(kClient != nullptr);
  306. }
  307. else
  308. {
  309. CARLA_ASSERT(kClient == nullptr);
  310. }
  311. }
  312. ~CarlaEngineJackClient()
  313. {
  314. carla_debug("CarlaEngineClient::~CarlaEngineClient()");
  315. if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  316. {
  317. if (kClient)
  318. jackbridge_client_close(kClient);
  319. }
  320. }
  321. void activate()
  322. {
  323. carla_debug("CarlaEngineJackClient::activate()");
  324. if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  325. {
  326. CARLA_ASSERT(kClient && ! fActive);
  327. if (kClient && ! fActive)
  328. jackbridge_activate(kClient);
  329. }
  330. CarlaEngineClient::activate();
  331. }
  332. void deactivate()
  333. {
  334. carla_debug("CarlaEngineJackClient::deactivate()");
  335. if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  336. {
  337. CARLA_ASSERT(kClient && fActive);
  338. if (kClient && fActive)
  339. jackbridge_deactivate(kClient);
  340. }
  341. CarlaEngineClient::deactivate();
  342. }
  343. bool isOk() const
  344. {
  345. carla_debug("CarlaEngineJackClient::isOk()");
  346. if (kUseClient)
  347. return bool(kClient);
  348. return CarlaEngineClient::isOk();
  349. }
  350. void setLatency(const uint32_t samples)
  351. {
  352. CarlaEngineClient::setLatency(samples);
  353. if (kUseClient)
  354. jackbridge_recompute_total_latencies(kClient);
  355. }
  356. CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput)
  357. {
  358. carla_debug("CarlaEngineJackClient::addPort(%s, \"%s\", %s)", EnginePortType2Str(portType), name, bool2str(isInput));
  359. jack_port_t* port = nullptr;
  360. // Create JACK port first, if needed
  361. if (kUseClient)
  362. {
  363. switch (portType)
  364. {
  365. case kEnginePortTypeNull:
  366. break;
  367. case kEnginePortTypeAudio:
  368. port = jackbridge_port_register(kClient, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  369. break;
  370. case kEnginePortTypeEvent:
  371. port = jackbridge_port_register(kClient, name, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  372. break;
  373. }
  374. }
  375. // Create Engine port
  376. switch (portType)
  377. {
  378. case kEnginePortTypeNull:
  379. break;
  380. case kEnginePortTypeAudio:
  381. return new CarlaEngineJackAudioPort(isInput, kProcessMode, kClient, port);
  382. case kEnginePortTypeEvent:
  383. return new CarlaEngineJackEventPort(isInput, kProcessMode, kClient, port);
  384. }
  385. carla_stderr("CarlaEngineJackClient::addPort(%s, \"%s\", %s) - invalid type", EnginePortType2Str(portType), name, bool2str(isInput));
  386. return nullptr;
  387. }
  388. private:
  389. jack_client_t* const kClient;
  390. const bool kUseClient;
  391. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackClient)
  392. };
  393. // -------------------------------------------------------------------------------------------------------------------
  394. // Jack Engine
  395. class CarlaEngineJack : public CarlaEngine
  396. {
  397. public:
  398. CarlaEngineJack()
  399. : CarlaEngine(),
  400. fClient(nullptr),
  401. fTransportState(JackTransportStopped),
  402. fFreewheel(false),
  403. #ifdef BUILD_BRIDGE
  404. fHasQuit(false)
  405. #else
  406. fRackPorts{nullptr},
  407. fLastGroupId(0),
  408. fLastPortId(0),
  409. fLastConnectionId(0)
  410. #endif
  411. {
  412. carla_debug("CarlaEngineJack::CarlaEngineJack()");
  413. #ifdef BUILD_BRIDGE
  414. fOptions.processMode = PROCESS_MODE_MULTIPLE_CLIENTS;
  415. #endif
  416. carla_zeroStruct<jack_position_t>(fTransportPos);
  417. }
  418. ~CarlaEngineJack()
  419. {
  420. carla_debug("CarlaEngineJack::~CarlaEngineJack()");
  421. CARLA_ASSERT(fClient == nullptr);
  422. #ifndef BUILD_BRIDGE
  423. fUsedGroupNames.clear();
  424. fUsedPortNames.clear();
  425. fUsedConnections.clear();
  426. #endif
  427. }
  428. // -------------------------------------------------------------------
  429. // Maximum values
  430. unsigned int maxClientNameSize()
  431. {
  432. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  433. return static_cast<unsigned int>(jackbridge_client_name_size());
  434. return CarlaEngine::maxClientNameSize();
  435. }
  436. unsigned int maxPortNameSize()
  437. {
  438. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  439. return static_cast<unsigned int>(jackbridge_port_name_size());
  440. return CarlaEngine::maxPortNameSize();
  441. }
  442. // -------------------------------------------------------------------
  443. // Virtual, per-engine type calls
  444. bool init(const char* const clientName)
  445. {
  446. carla_debug("CarlaEngineJack::init(\"%s\")", clientName);
  447. fFreewheel = false;
  448. fTransportState = JackTransportStopped;
  449. carla_zeroStruct<jack_position_t>(fTransportPos);
  450. #ifndef BUILD_BRIDGE
  451. fLastGroupId = 0;
  452. fLastPortId = 0;
  453. fLastConnectionId = 0;
  454. fUsedGroupNames.clear();
  455. fUsedPortNames.clear();
  456. fUsedConnections.clear();
  457. fClient = jackbridge_client_open(clientName, JackNullOption, nullptr);
  458. if (fClient != nullptr)
  459. {
  460. fBufferSize = jackbridge_get_buffer_size(fClient);
  461. fSampleRate = jackbridge_get_sample_rate(fClient);
  462. jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback, this);
  463. jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback, this);
  464. jackbridge_set_freewheel_callback(fClient, carla_jack_freewheel_callback, this);
  465. jackbridge_set_process_callback(fClient, carla_jack_process_callback, this);
  466. jackbridge_set_latency_callback(fClient, carla_jack_latency_callback, this);
  467. jackbridge_on_shutdown(fClient, carla_jack_shutdown_callback, this);
  468. const char* const jackClientName = jackbridge_get_client_name(fClient);
  469. initJackPatchbay(jackClientName);
  470. // TODO - update jackbridge
  471. jack_set_client_registration_callback(fClient, carla_jack_client_registration_callback, this);
  472. jack_set_port_registration_callback(fClient, carla_jack_port_registration_callback, this);
  473. jack_set_port_connect_callback(fClient, carla_jack_port_connect_callback, this);
  474. if (jack_set_port_rename_callback)
  475. jack_set_port_rename_callback(fClient, carla_jack_port_rename_callback, this);
  476. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  477. {
  478. fRackPorts[rackPortAudioIn1] = jackbridge_port_register(fClient, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  479. fRackPorts[rackPortAudioIn2] = jackbridge_port_register(fClient, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  480. fRackPorts[rackPortAudioOut1] = jackbridge_port_register(fClient, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  481. fRackPorts[rackPortAudioOut2] = jackbridge_port_register(fClient, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  482. fRackPorts[rackPortEventIn] = jackbridge_port_register(fClient, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  483. fRackPorts[rackPortEventOut] = jackbridge_port_register(fClient, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  484. }
  485. if (jackbridge_activate(fClient) == 0)
  486. {
  487. return CarlaEngine::init(jackClientName);
  488. }
  489. else
  490. {
  491. setLastError("Failed to activate the JACK client");
  492. jackbridge_client_close(fClient);
  493. fClient = nullptr;
  494. }
  495. }
  496. else
  497. setLastError("Failed to create new JACK client");
  498. return false;
  499. #else
  500. if (fBufferSize == 0 || fSampleRate == 0.0)
  501. {
  502. // open temp client to get initial buffer-size and sample-rate values
  503. if (jack_client_t* tmpClient = jackbridge_client_open(clientName, JackNullOption, nullptr))
  504. {
  505. fBufferSize = jackbridge_get_buffer_size(tmpClient);
  506. fSampleRate = jackbridge_get_sample_rate(tmpClient);
  507. jackbridge_client_close(tmpClient);
  508. }
  509. }
  510. return CarlaEngine::init(clientName);
  511. #endif
  512. }
  513. bool close()
  514. {
  515. carla_debug("CarlaEngineJack::close()");
  516. CarlaEngine::close();
  517. #ifdef BUILD_BRIDGE
  518. fClient = nullptr;
  519. fHasQuit = true;
  520. return true;
  521. #else
  522. if (jackbridge_deactivate(fClient) == 0)
  523. {
  524. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  525. {
  526. jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioIn1]);
  527. jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioIn2]);
  528. jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioOut1]);
  529. jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioOut2]);
  530. jackbridge_port_unregister(fClient, fRackPorts[rackPortEventIn]);
  531. jackbridge_port_unregister(fClient, fRackPorts[rackPortEventOut]);
  532. }
  533. if (jackbridge_client_close(fClient) == 0)
  534. {
  535. fClient = nullptr;
  536. return true;
  537. }
  538. else
  539. setLastError("Failed to close the JACK client");
  540. }
  541. else
  542. setLastError("Failed to deactivate the JACK client");
  543. fClient = nullptr;
  544. fUsedGroupNames.clear();
  545. fUsedPortNames.clear();
  546. fUsedConnections.clear();
  547. #endif
  548. return false;
  549. }
  550. bool isRunning() const
  551. {
  552. #ifdef BUILD_BRIDGE
  553. return (fClient != nullptr || ! fHasQuit);
  554. #else
  555. return (fClient != nullptr);
  556. #endif
  557. }
  558. bool isOffline() const
  559. {
  560. return fFreewheel;
  561. }
  562. EngineType type() const
  563. {
  564. return kEngineTypeJack;
  565. }
  566. CarlaEngineClient* addClient(CarlaPlugin* const plugin)
  567. {
  568. jack_client_t* client = nullptr;
  569. #ifdef BUILD_BRIDGE
  570. client = fClient = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
  571. fBufferSize = jackbridge_get_buffer_size(client);
  572. fSampleRate = jackbridge_get_sample_rate(client);
  573. jackbridge_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  574. jackbridge_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  575. jackbridge_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  576. jackbridge_set_process_callback(client, carla_jack_process_callback, this);
  577. jackbridge_set_latency_callback(client, carla_jack_latency_callback, this);
  578. jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
  579. #else
  580. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
  581. {
  582. client = fClient;
  583. }
  584. else if (fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  585. {
  586. client = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
  587. jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, plugin);
  588. jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, plugin);
  589. }
  590. #endif
  591. return new CarlaEngineJackClient(kEngineTypeJack, fOptions.processMode, client);
  592. }
  593. #ifndef BUILD_BRIDGE
  594. // -------------------------------------------------------------------
  595. // Patchbay
  596. void patchbayConnect(int portA, int portB)
  597. {
  598. if (fClient == nullptr)
  599. return;
  600. const char* const portNameA = getFullPortName(portA).toUtf8().constData();
  601. const char* const portNameB = getFullPortName(portB).toUtf8().constData();
  602. jack_connect(fClient, portNameA, portNameB);
  603. }
  604. void patchbayDisconnect(int connectionId)
  605. {
  606. if (fClient == nullptr)
  607. return;
  608. for (int i=0, count=fUsedConnections.count(); i < count; i++)
  609. {
  610. if (fUsedConnections[i].id == connectionId)
  611. {
  612. const char* const portNameA = getFullPortName(fUsedConnections[i].portOut).toUtf8().constData();
  613. const char* const portNameB = getFullPortName(fUsedConnections[i].portIn).toUtf8().constData();
  614. jack_disconnect(fClient, portNameA, portNameB);
  615. break;
  616. }
  617. }
  618. }
  619. #endif
  620. // -------------------------------------------------------------------
  621. // Transport
  622. void transportPlay()
  623. {
  624. if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
  625. CarlaEngine::transportPlay();
  626. else if (fClient != nullptr)
  627. jackbridge_transport_start(fClient);
  628. }
  629. void transportPause()
  630. {
  631. if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
  632. CarlaEngine::transportPause();
  633. else if (fClient != nullptr)
  634. jackbridge_transport_stop(fClient);
  635. }
  636. void transportRelocate(const uint32_t frame)
  637. {
  638. if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
  639. CarlaEngine::transportRelocate(frame);
  640. else if (fClient != nullptr)
  641. jackbridge_transport_locate(fClient, frame);
  642. }
  643. // -------------------------------------
  644. protected:
  645. void handleJackBufferSizeCallback(const uint32_t newBufferSize)
  646. {
  647. if (fBufferSize != newBufferSize)
  648. {
  649. fBufferSize = newBufferSize;
  650. bufferSizeChanged(newBufferSize);
  651. }
  652. }
  653. void handleJackSampleRateCallback(const double newSampleRate)
  654. {
  655. if (fSampleRate != newSampleRate)
  656. {
  657. fSampleRate = newSampleRate;
  658. sampleRateChanged(newSampleRate);
  659. }
  660. }
  661. void handleJackFreewheelCallback(const bool isFreewheel)
  662. {
  663. fFreewheel = isFreewheel;
  664. }
  665. void saveTransportInfo()
  666. {
  667. if (fOptions.transportMode != TRANSPORT_MODE_JACK)
  668. return;
  669. fTransportPos.unique_1 = fTransportPos.unique_2 + 1; // invalidate
  670. fTransportState = jackbridge_transport_query(fClient, &fTransportPos);
  671. fTimeInfo.playing = (fTransportState == JackTransportRolling);
  672. if (fTransportPos.unique_1 == fTransportPos.unique_2)
  673. {
  674. fTimeInfo.frame = fTransportPos.frame;
  675. fTimeInfo.time = fTransportPos.usecs;
  676. if (fTransportPos.valid & JackPositionBBT)
  677. {
  678. fTimeInfo.valid = EngineTimeInfo::ValidBBT;
  679. fTimeInfo.bbt.bar = fTransportPos.bar;
  680. fTimeInfo.bbt.beat = fTransportPos.beat;
  681. fTimeInfo.bbt.tick = fTransportPos.tick;
  682. fTimeInfo.bbt.barStartTick = fTransportPos.bar_start_tick;
  683. fTimeInfo.bbt.beatsPerBar = fTransportPos.beats_per_bar;
  684. fTimeInfo.bbt.beatType = fTransportPos.beat_type;
  685. fTimeInfo.bbt.ticksPerBeat = fTransportPos.ticks_per_beat;
  686. fTimeInfo.bbt.beatsPerMinute = fTransportPos.beats_per_minute;
  687. }
  688. else
  689. fTimeInfo.valid = 0x0;
  690. }
  691. else
  692. {
  693. fTimeInfo.frame = 0;
  694. fTimeInfo.valid = 0x0;
  695. }
  696. }
  697. void handleJackProcessCallback(const uint32_t nframes)
  698. {
  699. #ifndef BUILD_BRIDGE
  700. if (kData->curPluginCount == 0)
  701. {
  702. // pass-through
  703. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  704. {
  705. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn1], nframes);
  706. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn2], nframes);
  707. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut1], nframes);
  708. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut2], nframes);
  709. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[rackPortEventOut], nframes);
  710. CARLA_ASSERT(audioIn1 != nullptr);
  711. CARLA_ASSERT(audioIn2 != nullptr);
  712. CARLA_ASSERT(audioOut1 != nullptr);
  713. CARLA_ASSERT(audioOut2 != nullptr);
  714. CARLA_ASSERT(eventOut != nullptr);
  715. carla_copyFloat(audioOut1, audioIn1, nframes);
  716. carla_copyFloat(audioOut2, audioIn2, nframes);
  717. jackbridge_midi_clear_buffer(eventOut);
  718. }
  719. return proccessPendingEvents();
  720. }
  721. #endif
  722. saveTransportInfo();
  723. #ifdef BUILD_BRIDGE
  724. CarlaPlugin* const plugin = getPluginUnchecked(0);
  725. if (plugin && plugin->enabled() && plugin->tryLock())
  726. {
  727. plugin->initBuffers();
  728. processPlugin(plugin, nframes);
  729. plugin->unlock();
  730. }
  731. #else
  732. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
  733. {
  734. for (unsigned int i=0; i < kData->curPluginCount; i++)
  735. {
  736. CarlaPlugin* const plugin = getPluginUnchecked(i);
  737. if (plugin && plugin->enabled() && plugin->tryLock())
  738. {
  739. plugin->initBuffers();
  740. processPlugin(plugin, nframes);
  741. plugin->unlock();
  742. }
  743. }
  744. }
  745. else if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  746. {
  747. // get buffers from jack
  748. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn1], nframes);
  749. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn2], nframes);
  750. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut1], nframes);
  751. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut2], nframes);
  752. void* const eventIn = jackbridge_port_get_buffer(fRackPorts[rackPortEventIn], nframes);
  753. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[rackPortEventOut], nframes);
  754. // assert buffers
  755. CARLA_ASSERT(audioIn1 != nullptr);
  756. CARLA_ASSERT(audioIn2 != nullptr);
  757. CARLA_ASSERT(audioOut1 != nullptr);
  758. CARLA_ASSERT(audioOut2 != nullptr);
  759. CARLA_ASSERT(eventIn != nullptr);
  760. CARLA_ASSERT(eventOut != nullptr);
  761. // create audio buffers
  762. float* inBuf[2] = { audioIn1, audioIn2 };
  763. float* outBuf[2] = { audioOut1, audioOut2 };
  764. // initialize input events
  765. carla_zeroMem(kData->rack.in, sizeof(EngineEvent)*RACK_EVENT_COUNT);
  766. {
  767. uint32_t engineEventIndex = 0;
  768. jack_midi_event_t jackEvent;
  769. const uint32_t jackEventCount = jackbridge_midi_get_event_count(eventIn);
  770. for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; jackEventIndex++)
  771. {
  772. if (jackbridge_midi_event_get(&jackEvent, eventIn, jackEventIndex) != 0)
  773. continue;
  774. EngineEvent* const engineEvent = &kData->rack.in[engineEventIndex++];
  775. engineEvent->clear();
  776. const uint8_t midiStatus = MIDI_GET_STATUS_FROM_DATA(jackEvent.buffer);
  777. const uint8_t midiChannel = MIDI_GET_CHANNEL_FROM_DATA(jackEvent.buffer);
  778. engineEvent->time = jackEvent.time;
  779. engineEvent->channel = midiChannel;
  780. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  781. {
  782. const uint8_t midiControl = jackEvent.buffer[1];
  783. engineEvent->type = kEngineEventTypeControl;
  784. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  785. {
  786. const uint8_t midiBank = jackEvent.buffer[2];
  787. engineEvent->ctrl.type = kEngineControlEventTypeMidiBank;
  788. engineEvent->ctrl.param = midiBank;
  789. engineEvent->ctrl.value = 0.0;
  790. }
  791. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  792. {
  793. engineEvent->ctrl.type = kEngineControlEventTypeAllSoundOff;
  794. engineEvent->ctrl.param = 0;
  795. engineEvent->ctrl.value = 0.0;
  796. }
  797. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  798. {
  799. engineEvent->ctrl.type = kEngineControlEventTypeAllNotesOff;
  800. engineEvent->ctrl.param = 0;
  801. engineEvent->ctrl.value = 0.0;
  802. }
  803. else
  804. {
  805. const uint8_t midiValue = jackEvent.buffer[2];
  806. engineEvent->ctrl.type = kEngineControlEventTypeParameter;
  807. engineEvent->ctrl.param = midiControl;
  808. engineEvent->ctrl.value = double(midiValue)/127.0;
  809. }
  810. }
  811. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  812. {
  813. const uint8_t midiProgram = jackEvent.buffer[1];
  814. engineEvent->type = kEngineEventTypeControl;
  815. engineEvent->ctrl.type = kEngineControlEventTypeMidiProgram;
  816. engineEvent->ctrl.param = midiProgram;
  817. engineEvent->ctrl.value = 0.0;
  818. }
  819. else
  820. {
  821. engineEvent->type = kEngineEventTypeMidi;
  822. engineEvent->midi.data[0] = midiStatus;
  823. engineEvent->midi.data[1] = jackEvent.buffer[1];
  824. engineEvent->midi.data[2] = jackEvent.buffer[2];
  825. engineEvent->midi.size = static_cast<uint8_t>(jackEvent.size);
  826. }
  827. if (engineEventIndex >= RACK_EVENT_COUNT)
  828. break;
  829. }
  830. }
  831. // process rack
  832. processRack(inBuf, outBuf, nframes);
  833. // output control
  834. {
  835. jackbridge_midi_clear_buffer(eventOut);
  836. for (unsigned short i=0; i < RACK_EVENT_COUNT; i++)
  837. {
  838. EngineEvent* const engineEvent = &kData->rack.out[i];
  839. uint8_t data[3] = { 0 };
  840. uint8_t size = 0;
  841. switch (engineEvent->type)
  842. {
  843. case kEngineEventTypeNull:
  844. break;
  845. case kEngineEventTypeControl:
  846. {
  847. EngineControlEvent* const ctrlEvent = &engineEvent->ctrl;
  848. if (ctrlEvent->type == kEngineControlEventTypeParameter && MIDI_IS_CONTROL_BANK_SELECT(ctrlEvent->param))
  849. {
  850. // FIXME?
  851. ctrlEvent->type = kEngineControlEventTypeMidiBank;
  852. ctrlEvent->param = ctrlEvent->value;
  853. ctrlEvent->value = 0.0;
  854. }
  855. switch (ctrlEvent->type)
  856. {
  857. case kEngineControlEventTypeNull:
  858. break;
  859. case kEngineControlEventTypeParameter:
  860. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  861. data[1] = static_cast<uint8_t>(ctrlEvent->param);
  862. data[2] = uint8_t(ctrlEvent->value * 127.0);
  863. size = 3;
  864. break;
  865. case kEngineControlEventTypeMidiBank:
  866. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  867. data[1] = MIDI_CONTROL_BANK_SELECT;
  868. data[2] = static_cast<uint8_t>(ctrlEvent->param);
  869. size = 3;
  870. break;
  871. case kEngineControlEventTypeMidiProgram:
  872. data[0] = MIDI_STATUS_PROGRAM_CHANGE + engineEvent->channel;
  873. data[1] = static_cast<uint8_t>(ctrlEvent->param);
  874. size = 2;
  875. break;
  876. case kEngineControlEventTypeAllSoundOff:
  877. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  878. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  879. size = 2;
  880. break;
  881. case kEngineControlEventTypeAllNotesOff:
  882. data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
  883. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  884. size = 2;
  885. break;
  886. }
  887. break;
  888. }
  889. case kEngineEventTypeMidi:
  890. {
  891. EngineMidiEvent* const midiEvent = &engineEvent->midi;
  892. data[0] = midiEvent->data[0];
  893. data[1] = midiEvent->data[1];
  894. data[2] = midiEvent->data[2];
  895. size = midiEvent->size;
  896. break;
  897. }
  898. }
  899. if (size > 0)
  900. jackbridge_midi_event_write(eventOut, engineEvent->time, data, size);
  901. }
  902. }
  903. }
  904. #endif // ! BUILD_BRIDGE
  905. proccessPendingEvents();
  906. }
  907. void handleJackLatencyCallback(const jack_latency_callback_mode_t mode)
  908. {
  909. if (fOptions.processMode != PROCESS_MODE_SINGLE_CLIENT)
  910. return;
  911. for (unsigned int i=0; i < kData->curPluginCount; i++)
  912. {
  913. CarlaPlugin* const plugin = getPluginUnchecked(i);
  914. if (plugin && plugin->enabled())
  915. latencyPlugin(plugin, mode);
  916. }
  917. }
  918. #ifndef BUILD_BRIDGE
  919. void handleJackClientRegistrationCallback(const char* name, bool reg)
  920. {
  921. if (reg)
  922. {
  923. GroupNameToId groupNameToId;
  924. groupNameToId.id = fLastGroupId;
  925. groupNameToId.name = name;
  926. callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, fLastGroupId, 0, 0.0f, name);
  927. fUsedGroupNames.append(groupNameToId);
  928. fLastGroupId++;
  929. }
  930. else
  931. {
  932. for (int i=0, count=fUsedGroupNames.count(); i < count; i++)
  933. {
  934. if (fUsedGroupNames[i].name == name)
  935. {
  936. callback(CALLBACK_PATCHBAY_CLIENT_REMOVED, 0, fUsedGroupNames[i].id, 0, 0.0f, nullptr);
  937. fUsedGroupNames.takeAt(i);
  938. break;
  939. }
  940. }
  941. }
  942. }
  943. void handleJackPortRegistrationCallback(jack_port_id_t port, bool reg)
  944. {
  945. jack_port_t* jackPort = jack_port_by_id(fClient, port);
  946. QString fullName(jack_port_name(jackPort));
  947. QString groupName = fullName.split(":").at(0);
  948. int groupId = getGroupId(groupName);
  949. const char* portName = jack_port_short_name(jackPort);
  950. if (reg)
  951. {
  952. bool portIsInput = (jack_port_flags(jackPort) & JackPortIsInput);
  953. bool portIsAudio = (std::strcmp(jack_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
  954. unsigned int portFlags = 0x0;
  955. portFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
  956. portFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
  957. PortNameToId portNameToId;
  958. portNameToId.groupId = groupId;
  959. portNameToId.portId = fLastPortId;
  960. portNameToId.name = portName;
  961. portNameToId.fullName = fullName;
  962. fUsedPortNames.append(portNameToId);
  963. callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, fLastPortId, portFlags, portName);
  964. fLastPortId++;
  965. }
  966. else
  967. {
  968. for (int i=0, count=fUsedPortNames.count(); i < count; i++)
  969. {
  970. if (fUsedPortNames[i].groupId == groupId && fUsedPortNames[i].name == portName)
  971. {
  972. callback(CALLBACK_PATCHBAY_PORT_REMOVED, 0, fUsedPortNames[i].portId, 0, 0.0f, nullptr);
  973. fUsedPortNames.takeAt(i);
  974. break;
  975. }
  976. }
  977. }
  978. }
  979. void handleJackPortConnectCallback(jack_port_id_t a, jack_port_id_t b, bool connect)
  980. {
  981. jack_port_t* jackPortA = jack_port_by_id(fClient, a);
  982. jack_port_t* jackPortB = jack_port_by_id(fClient, b);
  983. int portIdA = getPortId(QString(jack_port_name(jackPortA)));
  984. int portIdB = getPortId(QString(jack_port_name(jackPortB)));
  985. if (connect)
  986. {
  987. ConnectionToId connectionToId;
  988. connectionToId.id = fLastConnectionId;
  989. connectionToId.portOut = portIdA;
  990. connectionToId.portIn = portIdB;
  991. fUsedConnections.append(connectionToId);
  992. callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, fLastConnectionId, portIdA, portIdB, nullptr);
  993. fLastConnectionId++;
  994. }
  995. else
  996. {
  997. for (int i=0, count=fUsedConnections.count(); i < count; i++)
  998. {
  999. if (fUsedConnections[i].portOut == portIdA && fUsedConnections[i].portIn == portIdB)
  1000. {
  1001. callback(CALLBACK_PATCHBAY_CONNECTION_REMOVED, 0, fUsedConnections[i].id, 0, 0.0f, nullptr);
  1002. fUsedConnections.takeAt(i);
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. }
  1008. void handleJackPortRenameCallback(jack_port_id_t port, const char* oldName, const char* newName)
  1009. {
  1010. jack_port_t* jackPort = jack_port_by_id(fClient, port);
  1011. QString fullName(oldName);
  1012. QString groupName = fullName.split(":").at(0);
  1013. int groupId = getGroupId(groupName);
  1014. const char* portName = jack_port_short_name(jackPort);
  1015. for (int i=0, count=fUsedPortNames.count(); i < count; i++)
  1016. {
  1017. if (fUsedPortNames[i].groupId == groupId && fUsedPortNames[i].name == portName)
  1018. {
  1019. callback(CALLBACK_PATCHBAY_PORT_RENAMED, 0, fUsedPortNames[i].portId, 0, 0.0f, newName);
  1020. fUsedPortNames[i].name = newName;
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. #endif
  1026. void handleJackShutdownCallback()
  1027. {
  1028. for (unsigned int i=0; i < kData->curPluginCount; i++)
  1029. {
  1030. //CarlaPlugin* const plugin = getPluginUnchecked(i);
  1031. //if (plugin)
  1032. // plugin->x_client = nullptr;
  1033. }
  1034. fClient = nullptr;
  1035. callback(CALLBACK_QUIT, 0, 0, 0, 0.0f, nullptr);
  1036. }
  1037. // -------------------------------------
  1038. private:
  1039. jack_client_t* fClient;
  1040. jack_position_t fTransportPos;
  1041. jack_transport_state_t fTransportState;
  1042. bool fFreewheel;
  1043. // -------------------------------------
  1044. #ifdef BUILD_BRIDGE
  1045. bool fHasQuit;
  1046. #else
  1047. enum RackPorts {
  1048. rackPortAudioIn1 = 0,
  1049. rackPortAudioIn2 = 1,
  1050. rackPortAudioOut1 = 2,
  1051. rackPortAudioOut2 = 3,
  1052. rackPortEventIn = 4,
  1053. rackPortEventOut = 5,
  1054. rackPortCount = 8
  1055. };
  1056. jack_port_t* fRackPorts[rackPortCount];
  1057. struct GroupNameToId {
  1058. int id;
  1059. QString name;
  1060. };
  1061. struct PortNameToId {
  1062. int groupId;
  1063. int portId;
  1064. QString name;
  1065. QString fullName;
  1066. };
  1067. struct ConnectionToId {
  1068. int id;
  1069. int portOut;
  1070. int portIn;
  1071. };
  1072. int fLastGroupId;
  1073. int fLastPortId;
  1074. int fLastConnectionId ;
  1075. QList<GroupNameToId> fUsedGroupNames;
  1076. QList<PortNameToId> fUsedPortNames;
  1077. QList<ConnectionToId> fUsedConnections;
  1078. int getGroupId(QString groupName)
  1079. {
  1080. for (int i=0, count=fUsedGroupNames.count(); i < count; i++)
  1081. {
  1082. if (fUsedGroupNames[i].name == groupName)
  1083. {
  1084. return fUsedGroupNames[i].id;
  1085. }
  1086. }
  1087. return -1;
  1088. }
  1089. int getPortId(QString fullPortName)
  1090. {
  1091. QString groupName = fullPortName.split(":").at(0);
  1092. QString portName = fullPortName.replace(groupName+":", "");
  1093. int groupId = getGroupId(groupName);
  1094. for (int i=0, count=fUsedPortNames.count(); i < count; i++)
  1095. {
  1096. if (fUsedPortNames[i].groupId == groupId && fUsedPortNames[i].name == portName)
  1097. {
  1098. return fUsedPortNames[i].portId;
  1099. }
  1100. }
  1101. return -1;
  1102. }
  1103. QString& getFullPortName(int portId)
  1104. {
  1105. static QString fallbackString;
  1106. for (int i=0, count=fUsedPortNames.count(); i < count; i++)
  1107. {
  1108. if (fUsedPortNames[i].portId == portId)
  1109. {
  1110. return fUsedPortNames[i].fullName;
  1111. }
  1112. }
  1113. return fallbackString;
  1114. }
  1115. void initJackPatchbay(const char* const ourName)
  1116. {
  1117. // query initial jack ports
  1118. QList<QString> parsedGroups;
  1119. // our client
  1120. {
  1121. GroupNameToId groupNameToId;
  1122. groupNameToId.id = fLastGroupId;
  1123. groupNameToId.name = ourName;
  1124. callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, fLastGroupId, 0, 0.0f, ourName);
  1125. fUsedGroupNames.append(groupNameToId);
  1126. fLastGroupId++;
  1127. parsedGroups.append(QString(ourName));
  1128. }
  1129. if (const char** ports = jack_get_ports(fClient, nullptr, nullptr, 0))
  1130. {
  1131. for (int i=0; ports[i] != nullptr; i++)
  1132. {
  1133. jack_port_t* jackPort = jack_port_by_name(fClient, ports[i]);
  1134. const char* portName = jack_port_short_name(jackPort);
  1135. QString fullName(ports[i]);
  1136. QString groupName(fullName.split(":").at(0));
  1137. int groupId = -1;
  1138. if (groupName == ourName)
  1139. continue;
  1140. if (parsedGroups.contains(groupName))
  1141. {
  1142. groupId = getGroupId(groupName);
  1143. }
  1144. else
  1145. {
  1146. groupId = fLastGroupId++;
  1147. GroupNameToId groupNameToId;
  1148. groupNameToId.id = groupId;
  1149. groupNameToId.name = groupName;
  1150. fUsedGroupNames.append(groupNameToId);
  1151. parsedGroups.append(groupName);
  1152. callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupId, 0, 0.0f, groupName.toUtf8().constData());
  1153. }
  1154. bool portIsInput = (jack_port_flags(jackPort) & JackPortIsInput);
  1155. bool portIsAudio = (std::strcmp(jack_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
  1156. unsigned int portFlags = 0x0;
  1157. portFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
  1158. portFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
  1159. PortNameToId portNameToId;
  1160. portNameToId.groupId = groupId;
  1161. portNameToId.portId = fLastPortId;
  1162. portNameToId.name = portName;
  1163. portNameToId.fullName = fullName;
  1164. fUsedPortNames.append(portNameToId);
  1165. callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, fLastPortId, portFlags, portName);
  1166. fLastPortId++;
  1167. }
  1168. jack_free(ports);
  1169. }
  1170. // query connections, after all ports are in place
  1171. if (const char** ports = jack_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
  1172. {
  1173. for (int i=0; ports[i] != nullptr; i++)
  1174. {
  1175. jack_port_t* jackPort = jack_port_by_name(fClient, ports[i]);
  1176. int thisPortId = getPortId(QString(ports[i]));
  1177. if (const char** jackConnections = jack_port_get_connections(jackPort))
  1178. {
  1179. for (int j=0; jackConnections[j] != nullptr; j++)
  1180. {
  1181. int targetPortId = getPortId(QString(jackConnections[j]));
  1182. ConnectionToId connectionToId;
  1183. connectionToId.id = fLastConnectionId;
  1184. connectionToId.portOut = thisPortId;
  1185. connectionToId.portIn = targetPortId;
  1186. fUsedConnections.append(connectionToId);
  1187. callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, fLastConnectionId, thisPortId, targetPortId, nullptr);
  1188. fLastConnectionId++;
  1189. }
  1190. jack_free(jackConnections);
  1191. }
  1192. }
  1193. jack_free(ports);
  1194. }
  1195. }
  1196. #endif
  1197. // -------------------------------------
  1198. void processPlugin(CarlaPlugin* const plugin, const uint32_t nframes)
  1199. {
  1200. const uint32_t inCount = plugin->audioInCount();
  1201. const uint32_t outCount = plugin->audioOutCount();
  1202. float* inBuffer[inCount];
  1203. float* outBuffer[outCount];
  1204. float inPeaks[inCount];
  1205. float outPeaks[outCount];
  1206. if (inCount > 0)
  1207. carla_zeroFloat(inPeaks, inCount);
  1208. if (outCount > 0)
  1209. carla_zeroFloat(outPeaks, outCount);
  1210. for (uint32_t i=0; i < inCount; i++)
  1211. {
  1212. CarlaEngineAudioPort* const port = CarlaPluginGetAudioInPort(plugin, i);
  1213. inBuffer[i] = port->getBuffer();
  1214. }
  1215. for (uint32_t i=0; i < outCount; i++)
  1216. {
  1217. CarlaEngineAudioPort* const port = CarlaPluginGetAudioOutPort(plugin, i);
  1218. outBuffer[i] = port->getBuffer();
  1219. }
  1220. for (uint32_t i=0; i < inCount; i++)
  1221. {
  1222. for (uint32_t j=0; j < nframes; j++)
  1223. {
  1224. const float absV = std::fabs(inBuffer[i][j]);
  1225. if (absV > inPeaks[i])
  1226. inPeaks[i] = absV;
  1227. }
  1228. }
  1229. plugin->process(inBuffer, outBuffer, nframes);
  1230. for (uint32_t i=0; i < outCount; i++)
  1231. {
  1232. for (uint32_t j=0; j < nframes; j++)
  1233. {
  1234. const float absV = std::fabs(outBuffer[i][j]);
  1235. if (absV > outPeaks[i])
  1236. outPeaks[i] = absV;
  1237. }
  1238. }
  1239. setPeaks(plugin->id(), inPeaks, outPeaks);
  1240. }
  1241. void latencyPlugin(CarlaPlugin* const plugin, jack_latency_callback_mode_t mode)
  1242. {
  1243. const uint32_t inCount = plugin->audioInCount();
  1244. const uint32_t outCount = plugin->audioOutCount();
  1245. jack_latency_range_t range;
  1246. uint32_t pluginLatency = plugin->latency();
  1247. if (pluginLatency == 0)
  1248. return;
  1249. if (mode == JackCaptureLatency)
  1250. {
  1251. for (uint32_t i=0; i < inCount; i++)
  1252. {
  1253. uint aOutI = (i >= outCount) ? outCount : i;
  1254. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, i))->kPort;
  1255. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, aOutI))->kPort;
  1256. jackbridge_port_get_latency_range(portIn, mode, &range);
  1257. range.min += pluginLatency;
  1258. range.max += pluginLatency;
  1259. jackbridge_port_set_latency_range(portOut, mode, &range);
  1260. }
  1261. }
  1262. else
  1263. {
  1264. for (uint32_t i=0; i < outCount; i++)
  1265. {
  1266. uint aInI = (i >= inCount) ? inCount : i;
  1267. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, aInI))->kPort;
  1268. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, i))->kPort;
  1269. jackbridge_port_get_latency_range(portOut, mode, &range);
  1270. range.min += pluginLatency;
  1271. range.max += pluginLatency;
  1272. jackbridge_port_set_latency_range(portIn, mode, &range);
  1273. }
  1274. }
  1275. }
  1276. // -------------------------------------
  1277. #define handlePtr ((CarlaEngineJack*)arg)
  1278. static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  1279. {
  1280. handlePtr->handleJackSampleRateCallback(newSampleRate);
  1281. return 0;
  1282. }
  1283. static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  1284. {
  1285. handlePtr->handleJackBufferSizeCallback(newBufferSize);
  1286. return 0;
  1287. }
  1288. static void carla_jack_freewheel_callback(int starting, void* arg)
  1289. {
  1290. handlePtr->handleJackFreewheelCallback(bool(starting));
  1291. }
  1292. static int carla_jack_process_callback(jack_nframes_t nframes, void* arg)
  1293. {
  1294. handlePtr->handleJackProcessCallback(nframes);
  1295. return 0;
  1296. }
  1297. static void carla_jack_latency_callback(jack_latency_callback_mode_t mode, void* arg)
  1298. {
  1299. handlePtr->handleJackLatencyCallback(mode);
  1300. }
  1301. #ifndef BUILD_BRIDGE
  1302. static void carla_jack_client_registration_callback(const char* name, int reg, void* arg)
  1303. {
  1304. handlePtr->handleJackClientRegistrationCallback(name, (reg != 0));
  1305. }
  1306. static void carla_jack_port_registration_callback(jack_port_id_t port, int reg, void* arg)
  1307. {
  1308. handlePtr->handleJackPortRegistrationCallback(port, (reg != 0));
  1309. }
  1310. static void carla_jack_port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  1311. {
  1312. handlePtr->handleJackPortConnectCallback(a, b, (connect != 0));
  1313. }
  1314. static int carla_jack_port_rename_callback(jack_port_id_t port, const char* oldName, const char* newName, void* arg)
  1315. {
  1316. handlePtr->handleJackPortRenameCallback(port, oldName, newName);
  1317. return 0;
  1318. }
  1319. #endif
  1320. static void carla_jack_shutdown_callback(void* arg)
  1321. {
  1322. handlePtr->handleJackShutdownCallback();
  1323. }
  1324. #undef handlePtr
  1325. // -------------------------------------
  1326. #ifndef BUILD_BRIDGE
  1327. static int carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg)
  1328. {
  1329. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  1330. if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
  1331. {
  1332. CarlaEngineJack* const engine = (CarlaEngineJack*)CarlaPluginGetEngine(plugin);
  1333. plugin->initBuffers();
  1334. engine->saveTransportInfo();
  1335. engine->processPlugin(plugin, nframes);
  1336. plugin->unlock();
  1337. }
  1338. else
  1339. carla_stdout("Plugin not enabled or locked");
  1340. return 0;
  1341. }
  1342. static void carla_jack_latency_callback_plugin(jack_latency_callback_mode_t mode, void* arg)
  1343. {
  1344. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  1345. if (plugin != nullptr && plugin->enabled())
  1346. {
  1347. CarlaEngineJack* const engine = (CarlaEngineJack*)CarlaPluginGetEngine(plugin);
  1348. engine->latencyPlugin(plugin, mode);
  1349. }
  1350. }
  1351. #endif
  1352. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
  1353. };
  1354. // -----------------------------------------
  1355. CarlaEngine* CarlaEngine::newJack()
  1356. {
  1357. return new CarlaEngineJack();
  1358. }
  1359. // -----------------------------------------
  1360. CARLA_BACKEND_END_NAMESPACE
  1361. #endif // CARLA_ENGINE_JACK