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.

1156 lines
37KB

  1. /*
  2. * Carla JACK Engine
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifdef CARLA_ENGINE_JACK
  18. #include "carla_engine_internal.hpp"
  19. #include "carla_backend_utils.hpp"
  20. #include "carla_midi.h"
  21. #include "jackbridge/jackbridge.h"
  22. CARLA_BACKEND_START_NAMESPACE
  23. #if 0
  24. } // Fix editor indentation
  25. #endif
  26. // -------------------------------------------------------------------------------------------------------------------
  27. // Carla Engine JACK-Audio port
  28. class CarlaEngineJackAudioPort : public CarlaEngineAudioPort
  29. {
  30. public:
  31. CarlaEngineJackAudioPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
  32. : CarlaEngineAudioPort(isInput, processMode),
  33. m_client(client),
  34. m_port(port)
  35. {
  36. qDebug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
  37. if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  38. {
  39. CARLA_ASSERT(m_client && m_port);
  40. }
  41. else
  42. {
  43. CARLA_ASSERT(! (m_client || m_port));
  44. }
  45. }
  46. ~CarlaEngineJackAudioPort()
  47. {
  48. qDebug("CarlaEngineJackAudioPort::~CarlaEngineJackAudioPort()");
  49. if (m_client && m_port)
  50. jackbridge_port_unregister(m_client, m_port);
  51. }
  52. void initBuffer(CarlaEngine* const engine)
  53. {
  54. CARLA_ASSERT(engine);
  55. if (! engine)
  56. {
  57. fBuffer = nullptr;
  58. return;
  59. }
  60. if (! m_port)
  61. return CarlaEngineAudioPort::initBuffer(engine);
  62. fBuffer = (float*)jackbridge_port_get_buffer(m_port, engine->getBufferSize());
  63. }
  64. float* getBuffer() const
  65. {
  66. return fBuffer;
  67. }
  68. private:
  69. jack_client_t* const m_client;
  70. jack_port_t* const m_port;
  71. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackAudioPort)
  72. };
  73. // -------------------------------------------------------------------------------------------------------------------
  74. // Carla Engine JACK-Event port
  75. class CarlaEngineJackEventPort : public CarlaEngineEventPort
  76. {
  77. public:
  78. CarlaEngineJackEventPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
  79. : CarlaEngineEventPort(isInput, processMode),
  80. m_client(client),
  81. m_port(port)
  82. {
  83. qDebug("CarlaEngineJackEventPort::CarlaEngineJackEventPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
  84. if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  85. {
  86. CARLA_ASSERT(m_client && m_port);
  87. }
  88. else
  89. {
  90. CARLA_ASSERT(! (m_client || m_port));
  91. }
  92. }
  93. ~CarlaEngineJackEventPort()
  94. {
  95. qDebug("CarlaEngineJackEventPort::~CarlaEngineJackEventPort()");
  96. if (m_client && m_port)
  97. jackbridge_port_unregister(m_client, m_port);
  98. }
  99. void initBuffer(CarlaEngine* const engine)
  100. {
  101. CARLA_ASSERT(engine);
  102. if (! engine)
  103. {
  104. fBuffer = nullptr;
  105. return;
  106. }
  107. if (! m_port)
  108. return CarlaEngineEventPort::initBuffer(engine);
  109. fBuffer = jackbridge_port_get_buffer(m_port, engine->getBufferSize());
  110. if (! kIsInput)
  111. jackbridge_midi_clear_buffer(fBuffer);
  112. }
  113. uint32_t getEventCount()
  114. {
  115. if (! m_port)
  116. return CarlaEngineEventPort::getEventCount();
  117. if (! kIsInput)
  118. return 0;
  119. CARLA_ASSERT(fBuffer);
  120. if (! fBuffer)
  121. return 0;
  122. return jackbridge_midi_get_event_count(fBuffer);
  123. }
  124. const EngineEvent* getEvent(const uint32_t index)
  125. {
  126. if (! m_port)
  127. return CarlaEngineEventPort::getEvent(index);
  128. if (! kIsInput)
  129. return nullptr;
  130. CARLA_ASSERT(fBuffer);
  131. if (! fBuffer)
  132. return nullptr;
  133. jack_midi_event_t jackEvent;
  134. if (jackbridge_midi_event_get(&jackEvent, fBuffer, index) != 0 || jackEvent.size > 3)
  135. return nullptr;
  136. m_retEvent.clear();
  137. const uint8_t midiStatus = MIDI_GET_STATUS_FROM_DATA(jackEvent.buffer);
  138. const uint8_t midiChannel = MIDI_GET_CHANNEL_FROM_DATA(jackEvent.buffer);
  139. m_retEvent.time = jackEvent.time;
  140. m_retEvent.channel = midiChannel;
  141. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  142. {
  143. const uint8_t midiControl = jackEvent.buffer[1];
  144. m_retEvent.type = kEngineEventTypeControl;
  145. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  146. {
  147. const uint8_t midiBank = jackEvent.buffer[2];
  148. m_retEvent.ctrl.type = kEngineControlEventTypeMidiBank;
  149. m_retEvent.ctrl.parameter = midiBank;
  150. m_retEvent.ctrl.value = 0.0;
  151. }
  152. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  153. {
  154. m_retEvent.ctrl.type = kEngineControlEventTypeAllSoundOff;
  155. m_retEvent.ctrl.parameter = 0;
  156. m_retEvent.ctrl.value = 0.0;
  157. }
  158. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  159. {
  160. m_retEvent.ctrl.type = kEngineControlEventTypeAllNotesOff;
  161. m_retEvent.ctrl.parameter = 0;
  162. m_retEvent.ctrl.value = 0.0;
  163. }
  164. else
  165. {
  166. const uint8_t midiValue = jackEvent.buffer[2];
  167. m_retEvent.ctrl.type = kEngineControlEventTypeParameter;
  168. m_retEvent.ctrl.parameter = midiControl;
  169. m_retEvent.ctrl.value = double(midiValue)/127;
  170. }
  171. }
  172. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  173. {
  174. const uint8_t midiProgram = jackEvent.buffer[1];
  175. m_retEvent.type = kEngineEventTypeControl;
  176. m_retEvent.ctrl.type = kEngineControlEventTypeMidiProgram;
  177. m_retEvent.ctrl.parameter = midiProgram;
  178. m_retEvent.ctrl.value = 0.0;
  179. }
  180. else
  181. {
  182. m_retEvent.type = kEngineEventTypeMidi;
  183. m_retEvent.midi.data[0] = midiStatus;
  184. m_retEvent.midi.data[1] = jackEvent.buffer[1];
  185. m_retEvent.midi.data[2] = jackEvent.buffer[2];
  186. m_retEvent.midi.size = jackEvent.size;
  187. }
  188. return &m_retEvent;
  189. }
  190. void writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t parameter, const double value)
  191. {
  192. if (! m_port)
  193. return CarlaEngineEventPort::writeControlEvent(time, channel, type, parameter, value);
  194. if (kIsInput)
  195. return;
  196. CARLA_ASSERT(fBuffer);
  197. CARLA_ASSERT(type != kEngineControlEventTypeNull);
  198. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  199. if (! fBuffer)
  200. return;
  201. if (type == kEngineControlEventTypeNull)
  202. return;
  203. if (channel >= MAX_MIDI_CHANNELS)
  204. return;
  205. if (type == kEngineControlEventTypeParameter)
  206. {
  207. CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(parameter));
  208. }
  209. uint8_t data[3] = { 0 };
  210. uint8_t size = 0;
  211. switch (type)
  212. {
  213. case kEngineControlEventTypeNull:
  214. break;
  215. case kEngineControlEventTypeParameter:
  216. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  217. data[1] = parameter;
  218. data[2] = value * 127;
  219. size = 3;
  220. break;
  221. case kEngineControlEventTypeMidiBank:
  222. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  223. data[1] = MIDI_CONTROL_BANK_SELECT;
  224. data[2] = value;
  225. size = 3;
  226. break;
  227. case kEngineControlEventTypeMidiProgram:
  228. data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel;
  229. data[1] = value;
  230. size = 2;
  231. break;
  232. case kEngineControlEventTypeAllSoundOff:
  233. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  234. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  235. size = 2;
  236. break;
  237. case kEngineControlEventTypeAllNotesOff:
  238. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  239. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  240. size = 2;
  241. break;
  242. }
  243. if (size > 0)
  244. jackbridge_midi_event_write(fBuffer, time, data, size);
  245. }
  246. void writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t* const data, const uint8_t size)
  247. {
  248. if (! m_port)
  249. return CarlaEngineEventPort::writeMidiEvent(time, channel, data, size);
  250. if (kIsInput)
  251. return;
  252. CARLA_ASSERT(fBuffer);
  253. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  254. CARLA_ASSERT(data);
  255. CARLA_ASSERT(size > 0);
  256. if (! fBuffer)
  257. return;
  258. if (channel >= MAX_MIDI_CHANNELS)
  259. return;
  260. if (! (data && size > 0))
  261. return;
  262. uint8_t jdata[size];
  263. std::memcpy(jdata, data, sizeof(uint8_t)*size);
  264. jdata[0] = data[0] + channel;
  265. jackbridge_midi_event_write(fBuffer, time, jdata, size);
  266. }
  267. private:
  268. void* fBuffer;
  269. jack_client_t* const m_client;
  270. jack_port_t* const m_port;
  271. EngineEvent m_retEvent;
  272. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackEventPort)
  273. };
  274. // -------------------------------------------------------------------------------------------------------------------
  275. // Jack Engine client
  276. class CarlaEngineJackClient : public CarlaEngineClient
  277. {
  278. public:
  279. CarlaEngineJackClient(const EngineType engineType, const ProcessMode processMode, jack_client_t* const client)
  280. : CarlaEngineClient(engineType, processMode),
  281. m_client(client),
  282. m_usesClient(processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  283. {
  284. qDebug("CarlaEngineJackClient::CarlaEngineJackClient(%s, %s, %p)", EngineType2Str(engineType), ProcessMode2Str(processMode), client);
  285. if (m_usesClient)
  286. CARLA_ASSERT(m_client);
  287. else
  288. CARLA_ASSERT(! m_client);
  289. }
  290. ~CarlaEngineJackClient()
  291. {
  292. qDebug("CarlaEngineClient::~CarlaEngineClient()");
  293. if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  294. {
  295. if (m_client)
  296. jackbridge_client_close(m_client);
  297. }
  298. }
  299. void activate()
  300. {
  301. qDebug("CarlaEngineClient::activate()");
  302. if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  303. {
  304. CARLA_ASSERT(m_client && ! isActive());
  305. if (m_client && ! isActive())
  306. jackbridge_activate(m_client);
  307. }
  308. CarlaEngineClient::activate();
  309. }
  310. void deactivate()
  311. {
  312. qDebug("CarlaEngineClient::deactivate()");
  313. if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  314. {
  315. CARLA_ASSERT(m_client && isActive());
  316. if (m_client && isActive())
  317. jackbridge_deactivate(m_client);
  318. }
  319. CarlaEngineClient::deactivate();
  320. }
  321. bool isOk() const
  322. {
  323. qDebug("CarlaEngineClient::isOk()");
  324. if (m_usesClient)
  325. return bool(m_client);
  326. return CarlaEngineClient::isOk();
  327. }
  328. void setLatency(const uint32_t samples)
  329. {
  330. CarlaEngineClient::setLatency(samples);
  331. if (m_usesClient)
  332. jackbridge_recompute_total_latencies(m_client);
  333. }
  334. const CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput)
  335. {
  336. qDebug("CarlaJackEngineClient::addPort(%s, \"%s\", %s)", EnginePortType2Str(portType), name, bool2str(isInput));
  337. jack_port_t* port = nullptr;
  338. // Create Jack port if needed
  339. if (m_usesClient)
  340. {
  341. switch (portType)
  342. {
  343. case kEnginePortTypeNull:
  344. break;
  345. case kEnginePortTypeAudio:
  346. port = jackbridge_port_register(m_client, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  347. break;
  348. case kEnginePortTypeEvent:
  349. port = jackbridge_port_register(m_client, name, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  350. break;
  351. }
  352. }
  353. // Create Engine port
  354. switch (portType)
  355. {
  356. case kEnginePortTypeNull:
  357. break;
  358. case kEnginePortTypeAudio:
  359. return new CarlaEngineJackAudioPort(isInput, kProcessMode, m_client, port);
  360. case kEnginePortTypeEvent:
  361. return new CarlaEngineJackEventPort(isInput, kProcessMode, m_client, port);
  362. }
  363. qCritical("CarlaJackEngineClient::addPort(%s, \"%s\", %s) - invalid type", EnginePortType2Str(portType), name, bool2str(isInput));
  364. return nullptr;
  365. }
  366. private:
  367. jack_client_t* const m_client;
  368. const bool m_usesClient;
  369. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackClient)
  370. };
  371. // -------------------------------------------------------------------------------------------------------------------
  372. // Jack Engine
  373. class CarlaEngineJack : public CarlaEngine
  374. {
  375. public:
  376. CarlaEngineJack()
  377. : CarlaEngine()
  378. #ifndef BUILD_BRIDGE
  379. , fRackPorts{nullptr}
  380. #endif
  381. {
  382. qDebug("CarlaEngineJack::CarlaEngineJack()");
  383. m_client = nullptr;
  384. m_state = JackTransportStopped;
  385. m_freewheel = false;
  386. memset(&m_pos, 0, sizeof(jack_position_t));
  387. #ifdef BUILD_BRIDGE
  388. m_hasQuit = false;
  389. #endif
  390. }
  391. ~CarlaEngineJack()
  392. {
  393. qDebug("CarlaEngineJack::~CarlaEngineJack()");
  394. CARLA_ASSERT(! m_client);
  395. }
  396. // -------------------------------------------------------------------
  397. // Maximum values
  398. unsigned int maxClientNameSize()
  399. {
  400. #ifndef BUILD_BRIDGE
  401. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  402. #endif
  403. return (unsigned int)jackbridge_client_name_size() - 3; // reserve space for "_2" forced-stereo ports
  404. return CarlaEngine::maxClientNameSize();
  405. }
  406. unsigned int maxPortNameSize()
  407. {
  408. #ifndef BUILD_BRIDGE
  409. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  410. #endif
  411. return (unsigned int)jackbridge_port_name_size();
  412. return CarlaEngine::maxPortNameSize();
  413. }
  414. // -------------------------------------------------------------------
  415. // Virtual, per-engine type calls
  416. bool init(const char* const clientName)
  417. {
  418. qDebug("CarlaEngineJack::init(\"%s\")", clientName);
  419. m_state = JackTransportStopped;
  420. m_freewheel = false;
  421. #ifndef BUILD_BRIDGE
  422. m_client = jackbridge_client_open(clientName, JackNullOption, nullptr);
  423. if (m_client)
  424. {
  425. fBufferSize = jackbridge_get_buffer_size(m_client);
  426. fSampleRate = jackbridge_get_sample_rate(m_client);
  427. jackbridge_set_buffer_size_callback(m_client, carla_jack_bufsize_callback, this);
  428. jackbridge_set_sample_rate_callback(m_client, carla_jack_srate_callback, this);
  429. jackbridge_set_freewheel_callback(m_client, carla_jack_freewheel_callback, this);
  430. jackbridge_set_process_callback(m_client, carla_jack_process_callback, this);
  431. jackbridge_set_latency_callback(m_client, carla_jack_latency_callback, this);
  432. jackbridge_on_shutdown(m_client, carla_jack_shutdown_callback, this);
  433. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  434. {
  435. fRackPorts[rackPortAudioIn1] = jackbridge_port_register(m_client, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  436. fRackPorts[rackPortAudioIn2] = jackbridge_port_register(m_client, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  437. fRackPorts[rackPortAudioOut1] = jackbridge_port_register(m_client, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  438. fRackPorts[rackPortAudioOut2] = jackbridge_port_register(m_client, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  439. fRackPorts[rackPortEventIn] = jackbridge_port_register(m_client, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  440. fRackPorts[rackPortEventOut] = jackbridge_port_register(m_client, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  441. }
  442. if (jackbridge_activate(m_client) == 0)
  443. {
  444. fName = jackbridge_get_client_name(m_client);
  445. fName.toBasic();
  446. CarlaEngine::init(fName);
  447. return true;
  448. }
  449. else
  450. {
  451. setLastError("Failed to activate the JACK client");
  452. m_client = nullptr;
  453. }
  454. }
  455. else
  456. setLastError("Failed to create new JACK client");
  457. return false;
  458. #else
  459. // open temp client to get initial buffer-size and sample-rate values
  460. if (bufferSize == 0 || sampleRate == 0.0)
  461. {
  462. m_client = jackbridge_client_open(clientName, JackNullOption, nullptr);
  463. if (m_client)
  464. {
  465. bufferSize = jackbridge_get_buffer_size(m_client);
  466. sampleRate = jackbridge_get_sample_rate(m_client);
  467. jackbridge_client_close(m_client);
  468. m_client = nullptr;
  469. }
  470. }
  471. name = clientName;
  472. name.toBasic();
  473. CarlaEngine::init(name);
  474. return true;
  475. #endif
  476. }
  477. bool close()
  478. {
  479. qDebug("CarlaEngineJack::close()");
  480. CarlaEngine::close();
  481. #ifdef BUILD_BRIDGE
  482. m_client = nullptr;
  483. m_hasQuit = true;
  484. return true;
  485. #else
  486. if (jackbridge_deactivate(m_client) == 0)
  487. {
  488. if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  489. {
  490. jackbridge_port_unregister(m_client, fRackPorts[rackPortAudioIn1]);
  491. jackbridge_port_unregister(m_client, fRackPorts[rackPortAudioIn2]);
  492. jackbridge_port_unregister(m_client, fRackPorts[rackPortAudioOut1]);
  493. jackbridge_port_unregister(m_client, fRackPorts[rackPortAudioOut2]);
  494. jackbridge_port_unregister(m_client, fRackPorts[rackPortEventIn]);
  495. jackbridge_port_unregister(m_client, fRackPorts[rackPortEventOut]);
  496. }
  497. if (jackbridge_client_close(m_client) == 0)
  498. {
  499. m_client = nullptr;
  500. return true;
  501. }
  502. else
  503. setLastError("Failed to close the JACK client");
  504. }
  505. else
  506. setLastError("Failed to deactivate the JACK client");
  507. m_client = nullptr;
  508. #endif
  509. return false;
  510. }
  511. bool isRunning() const
  512. {
  513. #ifdef BUILD_BRIDGE
  514. return bool(m_client || ! hasQuit);
  515. #else
  516. return bool(m_client);
  517. #endif
  518. }
  519. bool isOffline() const
  520. {
  521. return m_freewheel;
  522. }
  523. EngineType type() const
  524. {
  525. return kEngineTypeJack;
  526. }
  527. CarlaEngineClient* addClient(CarlaPlugin* const plugin)
  528. {
  529. jack_client_t* client = nullptr;
  530. #ifdef BUILD_BRIDGE
  531. client = m_client = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
  532. bufferSize = jackbridge_get_buffer_size(client);
  533. sampleRate = jackbridge_get_sample_rate(client);
  534. jackbridge_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  535. jackbridge_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  536. jackbridge_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  537. jackbridge_set_process_callback(client, carla_jack_process_callback, this);
  538. jackbridge_set_latency_callback(client, carla_jack_latency_callback, this);
  539. jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
  540. #else
  541. if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
  542. {
  543. client = m_client;
  544. }
  545. else if (fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  546. {
  547. client = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
  548. jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, plugin);
  549. jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, plugin);
  550. }
  551. #endif
  552. #ifdef BUILD_BRIDGE
  553. return new CarlaEngineJackClient(kEngineTypeJack, PROCESS_MODE_MULTIPLE_CLIENTS, client);
  554. #else
  555. return new CarlaEngineJackClient(kEngineTypeJack, fOptions.processMode, client);
  556. #endif
  557. }
  558. // -------------------------------------
  559. protected:
  560. void handleJackBufferSizeCallback(const uint32_t newBufferSize)
  561. {
  562. fBufferSize = newBufferSize;
  563. #ifndef BUILD_BRIDGE
  564. if (fOptions.processHighPrecision)
  565. return;
  566. #endif
  567. bufferSizeChanged(newBufferSize);
  568. }
  569. void handleJackSampleRateCallback(const double newSampleRate)
  570. {
  571. fSampleRate = newSampleRate;
  572. }
  573. void handleJackFreewheelCallback(const bool isFreewheel)
  574. {
  575. m_freewheel = isFreewheel;
  576. }
  577. void handleJackProcessCallback(const uint32_t nframes)
  578. {
  579. #ifndef BUILD_BRIDGE
  580. if (maxPluginNumber() == 0)
  581. return;
  582. #endif
  583. m_pos.unique_1 = m_pos.unique_2 + 1; // invalidate
  584. m_state = jackbridge_transport_query(m_client, &m_pos);
  585. fTimeInfo.playing = (m_state != JackTransportStopped);
  586. if (m_pos.unique_1 == m_pos.unique_2)
  587. {
  588. fTimeInfo.frame = m_pos.frame;
  589. fTimeInfo.time = m_pos.usecs;
  590. if (m_pos.valid & JackPositionBBT)
  591. {
  592. fTimeInfo.valid = EngineTimeInfo::ValidBBT;
  593. fTimeInfo.bbt.bar = m_pos.bar;
  594. fTimeInfo.bbt.beat = m_pos.beat;
  595. fTimeInfo.bbt.tick = m_pos.tick;
  596. fTimeInfo.bbt.barStartTick = m_pos.bar_start_tick;
  597. fTimeInfo.bbt.beatsPerBar = m_pos.beats_per_bar;
  598. fTimeInfo.bbt.beatType = m_pos.beat_type;
  599. fTimeInfo.bbt.ticksPerBeat = m_pos.ticks_per_beat;
  600. fTimeInfo.bbt.beatsPerMinute = m_pos.beats_per_minute;
  601. }
  602. else
  603. fTimeInfo.valid = 0;
  604. }
  605. else
  606. {
  607. fTimeInfo.frame = 0;
  608. fTimeInfo.valid = 0;
  609. }
  610. #if 0
  611. #ifndef BUILD_BRIDGE
  612. if (options.processMode == PROCESS_MODE_SINGLE_CLIENT)
  613. {
  614. for (unsigned short i=0, max=maxPluginNumber(); i < max; i++)
  615. {
  616. CarlaPlugin* const plugin = getPluginUnchecked(i);
  617. #else
  618. CarlaPlugin* const plugin = getPluginUnchecked(0);
  619. #endif
  620. if (plugin && plugin->enabled())
  621. {
  622. processLock();
  623. plugin->initBuffers();
  624. processPlugin(plugin, nframes);
  625. processUnlock();
  626. }
  627. #ifndef BUILD_BRIDGE
  628. }
  629. }
  630. else if (options.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  631. {
  632. // get buffers from jack
  633. float* audioIn1 = (float*)jackbridge_port_get_buffer(m_rackPorts[rackPortAudioIn1], nframes);
  634. float* audioIn2 = (float*)jackbridge_port_get_buffer(m_rackPorts[rackPortAudioIn2], nframes);
  635. float* audioOut1 = (float*)jackbridge_port_get_buffer(m_rackPorts[rackPortAudioOut1], nframes);
  636. float* audioOut2 = (float*)jackbridge_port_get_buffer(m_rackPorts[rackPortAudioOut2], nframes);
  637. void* controlIn = jackbridge_port_get_buffer(m_rackPorts[rackPortControlIn], nframes);
  638. void* controlOut = jackbridge_port_get_buffer(m_rackPorts[rackPortControlOut], nframes);
  639. void* midiIn = jackbridge_port_get_buffer(m_rackPorts[rackPortMidiIn], nframes);
  640. void* midiOut = jackbridge_port_get_buffer(m_rackPorts[rackPortMidiOut], nframes);
  641. // assert buffers
  642. CARLA_ASSERT(audioIn1);
  643. CARLA_ASSERT(audioIn2);
  644. CARLA_ASSERT(audioOut1);
  645. CARLA_ASSERT(audioOut2);
  646. CARLA_ASSERT(controlIn);
  647. CARLA_ASSERT(controlOut);
  648. CARLA_ASSERT(midiIn);
  649. CARLA_ASSERT(midiOut);
  650. // create audio buffers
  651. float* inBuf[2] = { audioIn1, audioIn2 };
  652. float* outBuf[2] = { audioOut1, audioOut2 };
  653. // initialize control input
  654. memset(rackControlEventsIn, 0, sizeof(CarlaEngineControlEvent)*MAX_CONTROL_EVENTS);
  655. {
  656. jack_midi_event_t jackEvent;
  657. const uint32_t jackEventCount = jackbridge_midi_get_event_count(controlIn);
  658. uint32_t carlaEventIndex = 0;
  659. for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; jackEventIndex++)
  660. {
  661. if (jackbridge_midi_event_get(&jackEvent, controlIn, jackEventIndex) != 0)
  662. continue;
  663. CarlaEngineControlEvent* const carlaEvent = &rackControlEventsIn[carlaEventIndex++];
  664. const uint8_t midiStatus = jackEvent.buffer[0];
  665. const uint8_t midiChannel = midiStatus & 0x0F;
  666. carlaEvent->time = jackEvent.time;
  667. carlaEvent->channel = midiChannel;
  668. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  669. {
  670. const uint8_t midiControl = jackEvent.buffer[1];
  671. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  672. {
  673. const uint8_t midiBank = jackEvent.buffer[2];
  674. carlaEvent->type = CarlaEngineMidiBankChangeEvent;
  675. carlaEvent->value = midiBank;
  676. }
  677. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  678. {
  679. carlaEvent->type = CarlaEngineAllSoundOffEvent;
  680. }
  681. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  682. {
  683. carlaEvent->type = CarlaEngineAllNotesOffEvent;
  684. }
  685. else
  686. {
  687. const uint8_t midiValue = jackEvent.buffer[2];
  688. carlaEvent->type = CarlaEngineParameterChangeEvent;
  689. carlaEvent->parameter = midiControl;
  690. carlaEvent->value = double(midiValue)/127;
  691. }
  692. }
  693. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  694. {
  695. const uint8_t midiProgram = jackEvent.buffer[1];
  696. carlaEvent->type = CarlaEngineMidiProgramChangeEvent;
  697. carlaEvent->value = midiProgram;
  698. }
  699. }
  700. }
  701. // initialize midi input
  702. memset(rackMidiEventsIn, 0, sizeof(CarlaEngineMidiEvent)*MAX_MIDI_EVENTS);
  703. {
  704. uint32_t i = 0, j = 0;
  705. jack_midi_event_t jackEvent;
  706. while (jackbridge_midi_event_get(&jackEvent, midiIn, j++) == 0)
  707. {
  708. if (i == MAX_MIDI_EVENTS)
  709. break;
  710. if (jackEvent.size < 4)
  711. {
  712. rackMidiEventsIn[i].time = jackEvent.time;
  713. rackMidiEventsIn[i].size = jackEvent.size;
  714. memcpy(rackMidiEventsIn[i].data, jackEvent.buffer, jackEvent.size);
  715. i += 1;
  716. }
  717. }
  718. }
  719. // process rack
  720. processRack(inBuf, outBuf, nframes);
  721. // output control
  722. {
  723. jackbridge_midi_clear_buffer(controlOut);
  724. for (unsigned short i=0; i < MAX_CONTROL_EVENTS; i++)
  725. {
  726. CarlaEngineControlEvent* const event = &rackControlEventsOut[i];
  727. if (event->type == CarlaEngineParameterChangeEvent && MIDI_IS_CONTROL_BANK_SELECT(event->parameter))
  728. event->type = CarlaEngineMidiBankChangeEvent;
  729. uint8_t data[4] = { 0 };
  730. switch (event->type)
  731. {
  732. case CarlaEngineNullEvent:
  733. break;
  734. case CarlaEngineParameterChangeEvent:
  735. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  736. data[1] = event->parameter;
  737. data[2] = event->value * 127;
  738. jackbridge_midi_event_write(controlOut, event->time, data, 3);
  739. break;
  740. case CarlaEngineMidiBankChangeEvent:
  741. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  742. data[1] = MIDI_CONTROL_BANK_SELECT;
  743. data[2] = event->value;
  744. jackbridge_midi_event_write(controlOut, event->time, data, 3);
  745. break;
  746. case CarlaEngineMidiProgramChangeEvent:
  747. data[0] = MIDI_STATUS_PROGRAM_CHANGE + event->channel;
  748. data[1] = event->value;
  749. jackbridge_midi_event_write(controlOut, event->time, data, 2);
  750. break;
  751. case CarlaEngineAllSoundOffEvent:
  752. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  753. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  754. jackbridge_midi_event_write(controlOut, event->time, data, 2);
  755. break;
  756. case CarlaEngineAllNotesOffEvent:
  757. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  758. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  759. jackbridge_midi_event_write(controlOut, event->time, data, 2);
  760. break;
  761. }
  762. }
  763. }
  764. // output midi
  765. {
  766. jackbridge_midi_clear_buffer(midiOut);
  767. for (unsigned short i=0; i < MAX_MIDI_EVENTS; i++)
  768. {
  769. if (rackMidiEventsOut[i].size == 0)
  770. break;
  771. jackbridge_midi_event_write(midiOut, rackMidiEventsOut[i].time, rackMidiEventsOut[i].data, rackMidiEventsOut[i].size);
  772. }
  773. }
  774. }
  775. #endif
  776. #endif
  777. }
  778. void handleJackLatencyCallback(const jack_latency_callback_mode_t mode)
  779. {
  780. #ifndef BUILD_BRIDGE
  781. if (fOptions.processMode != PROCESS_MODE_SINGLE_CLIENT)
  782. return;
  783. #endif
  784. #if 0
  785. for (unsigned short i=0, max=maxPluginNumber(); i < max; i++)
  786. {
  787. CarlaPlugin* const plugin = getPluginUnchecked(i);
  788. if (plugin && plugin->enabled())
  789. latencyPlugin(plugin, mode);
  790. }
  791. #endif
  792. }
  793. void handleJackShutdownCallback()
  794. {
  795. for (unsigned short i=0, max=maxPluginNumber(); i < max; i++)
  796. {
  797. //CarlaPlugin* const plugin = getPluginUnchecked(i);
  798. //if (plugin)
  799. // plugin->x_client = nullptr;
  800. }
  801. m_client = nullptr;
  802. callback(CALLBACK_QUIT, 0, 0, 0, 0.0, nullptr);
  803. }
  804. // -------------------------------------
  805. private:
  806. jack_client_t* m_client;
  807. jack_transport_state_t m_state;
  808. jack_position_t m_pos;
  809. bool m_freewheel;
  810. // -------------------------------------
  811. #ifdef BUILD_BRIDGE
  812. bool m_hasQuit;
  813. #else
  814. enum RackPorts {
  815. rackPortAudioIn1 = 0,
  816. rackPortAudioIn2 = 1,
  817. rackPortAudioOut1 = 2,
  818. rackPortAudioOut2 = 3,
  819. rackPortEventIn = 4,
  820. rackPortEventOut = 5,
  821. rackPortCount = 8
  822. };
  823. jack_port_t* fRackPorts[rackPortCount];
  824. #endif
  825. // -------------------------------------
  826. static void processPlugin(CarlaPlugin* const p, const uint32_t nframes)
  827. {
  828. #if 0
  829. float* inBuffer[p->aIn.count];
  830. float* outBuffer[p->aOut.count];
  831. for (uint32_t i=0; i < p->aIn.count; i++)
  832. inBuffer[i] = ((CarlaEngineJackAudioPort*)p->aIn.ports[i])->getBuffer();
  833. for (uint32_t i=0; i < p->aOut.count; i++)
  834. outBuffer[i] = ((CarlaEngineJackAudioPort*)p->aOut.ports[i])->getBuffer();
  835. if (p->m_processHighPrecision)
  836. {
  837. float* inBuffer2[p->aIn.count];
  838. float* outBuffer2[p->aOut.count];
  839. for (uint32_t i=0, j; i < nframes; i += 8)
  840. {
  841. for (j=0; j < p->aIn.count; j++)
  842. inBuffer2[j] = inBuffer[j] + i;
  843. for (j=0; j < p->aOut.count; j++)
  844. outBuffer2[j] = outBuffer[j] + i;
  845. p->process(inBuffer2, outBuffer2, 8, i);
  846. }
  847. }
  848. else
  849. p->process(inBuffer, outBuffer, nframes);
  850. #endif
  851. }
  852. static void latencyPlugin(CarlaPlugin* const p, jack_latency_callback_mode_t mode)
  853. {
  854. #if 0
  855. jack_latency_range_t range;
  856. uint32_t pluginLatency = p->x_client->getLatency();
  857. if (pluginLatency == 0)
  858. return;
  859. if (mode == JackCaptureLatency)
  860. {
  861. for (uint32_t i=0; i < p->aIn.count; i++)
  862. {
  863. uint aOutI = (i >= p->aOut.count) ? p->aOut.count : i;
  864. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)p->aIn.ports[i])->m_port;
  865. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)p->aOut.ports[aOutI])->m_port;
  866. jackbridge_port_get_latency_range(portIn, mode, &range);
  867. range.min += pluginLatency;
  868. range.max += pluginLatency;
  869. jackbridge_port_set_latency_range(portOut, mode, &range);
  870. }
  871. }
  872. else
  873. {
  874. for (uint32_t i=0; i < p->aOut.count; i++)
  875. {
  876. uint aInI = (i >= p->aIn.count) ? p->aIn.count : i;
  877. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)p->aIn.ports[aInI])->m_port;
  878. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)p->aOut.ports[i])->m_port;
  879. jackbridge_port_get_latency_range(portOut, mode, &range);
  880. range.min += pluginLatency;
  881. range.max += pluginLatency;
  882. jackbridge_port_set_latency_range(portIn, mode, &range);
  883. }
  884. }
  885. #endif
  886. }
  887. // -------------------------------------
  888. static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  889. {
  890. if (CarlaEngineJack* const _this_ = (CarlaEngineJack*)arg)
  891. _this_->handleJackSampleRateCallback(newSampleRate);
  892. return 0;
  893. }
  894. static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  895. {
  896. if (CarlaEngineJack* const _this_ = (CarlaEngineJack*)arg)
  897. _this_->handleJackBufferSizeCallback(newBufferSize);
  898. return 0;
  899. }
  900. static void carla_jack_freewheel_callback(int starting, void* arg)
  901. {
  902. if (CarlaEngineJack* const _this_ = (CarlaEngineJack*)arg)
  903. _this_->handleJackFreewheelCallback(bool(starting));
  904. }
  905. static int carla_jack_process_callback(jack_nframes_t nframes, void* arg)
  906. {
  907. if (CarlaEngineJack* const _this_ = (CarlaEngineJack*)arg)
  908. _this_->handleJackProcessCallback(nframes);
  909. return 0;
  910. }
  911. static void carla_jack_latency_callback(jack_latency_callback_mode_t mode, void* arg)
  912. {
  913. if (CarlaEngineJack* const _this_ = (CarlaEngineJack*)arg)
  914. _this_->handleJackLatencyCallback(mode);
  915. }
  916. static void carla_jack_shutdown_callback(void* arg)
  917. {
  918. if (CarlaEngineJack* const _this_ = (CarlaEngineJack*)arg)
  919. _this_->handleJackShutdownCallback();
  920. }
  921. // -------------------------------------
  922. static int carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg)
  923. {
  924. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  925. if (plugin && plugin->enabled())
  926. {
  927. //plugin->engineProcessLock();
  928. plugin->initBuffers();
  929. processPlugin(plugin, nframes);
  930. //plugin->engineProcessUnlock();
  931. }
  932. return 0;
  933. }
  934. static void carla_jack_latency_callback_plugin(jack_latency_callback_mode_t mode, void* arg)
  935. {
  936. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  937. if (plugin && plugin->enabled())
  938. latencyPlugin(plugin, mode);
  939. }
  940. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
  941. };
  942. // -----------------------------------------
  943. CarlaEngine* CarlaEngine::newJack()
  944. {
  945. return new CarlaEngineJack();
  946. }
  947. // -----------------------------------------
  948. CARLA_BACKEND_END_NAMESPACE
  949. #endif // CARLA_ENGINE_JACK