Collection of tools useful for audio production
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.

610 lines
21KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.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.h"
  19. #include "carla_plugin.h"
  20. #include <iostream>
  21. CARLA_BACKEND_START_NAMESPACE
  22. // -------------------------------------------------------------------------------------------------------------------
  23. // static JACK<->Engine calls
  24. static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  25. {
  26. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  27. engine->handleSampleRateCallback(newSampleRate);
  28. return 0;
  29. }
  30. static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  31. {
  32. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  33. engine->handleBufferSizeCallback(newBufferSize);
  34. return 0;
  35. }
  36. static void carla_jack_freewheel_callback(int starting, void* arg)
  37. {
  38. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  39. engine->handleFreewheelCallback(bool(starting));
  40. }
  41. static int carla_jack_process_callback(jack_nframes_t nframes, void* arg)
  42. {
  43. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  44. engine->handleProcessCallback(nframes);
  45. return 0;
  46. }
  47. static int carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg)
  48. {
  49. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  50. if (plugin && plugin->enabled())
  51. {
  52. plugin->engineProcessLock();
  53. plugin->initBuffers();
  54. plugin->process_jack(nframes);
  55. plugin->engineProcessUnlock();
  56. }
  57. return 0;
  58. }
  59. static void carla_jack_shutdown_callback(void* arg)
  60. {
  61. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  62. engine->handleShutdownCallback();
  63. }
  64. // -------------------------------------------------------------------------------------------------------------------
  65. // Carla Engine (JACK)
  66. CarlaEngineJack::CarlaEngineJack()
  67. #ifdef Q_COMPILER_INITIALIZER_LISTS
  68. : CarlaEngine(),
  69. rackJackPorts{nullptr}
  70. #else
  71. : CarlaEngine()
  72. #endif
  73. {
  74. qDebug("CarlaEngineJack::CarlaEngineJack()");
  75. type = CarlaEngineTypeJack;
  76. client = nullptr;
  77. state = JackTransportStopped;
  78. freewheel = false;
  79. procThread = nullptr;
  80. memset(&pos, 0, sizeof(jack_position_t));
  81. #ifndef Q_COMPILER_INITIALIZER_LISTS
  82. for (unsigned short i=0; i < rackPortCount; i++)
  83. rackJackPorts[i] = nullptr;
  84. #endif
  85. }
  86. CarlaEngineJack::~CarlaEngineJack()
  87. {
  88. qDebug("CarlaEngineJack::~CarlaEngineJack()");
  89. }
  90. // -------------------------------------------------------------------------------------------------------------------
  91. bool CarlaEngineJack::init(const char* const clientName)
  92. {
  93. qDebug("CarlaEngineJack::init(\"%s\")", clientName);
  94. client = jack_client_open(clientName, JackNullOption, nullptr);
  95. state = JackTransportStopped;
  96. freewheel = false;
  97. procThread = nullptr;
  98. if (client)
  99. {
  100. sampleRate = jack_get_sample_rate(client);
  101. bufferSize = jack_get_buffer_size(client);
  102. jack_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  103. jack_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  104. jack_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  105. jack_set_process_callback(client, carla_jack_process_callback, this);
  106. jack_on_shutdown(client, carla_jack_shutdown_callback, this);
  107. #ifndef BUILD_BRIDGE
  108. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  109. {
  110. rackJackPorts[rackPortAudioIn1] = jack_port_register(client, "in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  111. rackJackPorts[rackPortAudioIn2] = jack_port_register(client, "in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  112. rackJackPorts[rackPortAudioOut1] = jack_port_register(client, "out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  113. rackJackPorts[rackPortAudioOut2] = jack_port_register(client, "out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  114. rackJackPorts[rackPortControlIn] = jack_port_register(client, "control-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  115. rackJackPorts[rackPortControlOut] = jack_port_register(client, "control-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  116. rackJackPorts[rackPortMidiIn] = jack_port_register(client, "midi-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  117. rackJackPorts[rackPortMidiOut] = jack_port_register(client, "midi-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  118. }
  119. #endif
  120. if (jack_activate(client) == 0)
  121. {
  122. // set client name, fixed for OSC usage
  123. // FIXME - put this in shared?
  124. char* fixedName = strdup(jack_get_client_name(client));
  125. for (size_t i=0; i < strlen(fixedName); i++)
  126. {
  127. if (! (std::isalpha(fixedName[i]) || std::isdigit(fixedName[i])))
  128. fixedName[i] = '_';
  129. }
  130. name = strdup(fixedName);
  131. free((void*)fixedName);
  132. CarlaEngine::init(name);
  133. return true;
  134. }
  135. else
  136. {
  137. setLastError("Failed to activate the JACK client");
  138. client = nullptr;
  139. }
  140. }
  141. else
  142. setLastError("Failed to create new JACK client");
  143. return false;
  144. }
  145. bool CarlaEngineJack::close()
  146. {
  147. qDebug("CarlaEngineJack::close()");
  148. CarlaEngine::close();
  149. if (name)
  150. {
  151. free((void*)name);
  152. name = nullptr;
  153. }
  154. if (jack_deactivate(client) == 0)
  155. {
  156. #ifndef BUILD_BRIDGE
  157. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  158. {
  159. jack_port_unregister(client, rackJackPorts[rackPortAudioIn1]);
  160. jack_port_unregister(client, rackJackPorts[rackPortAudioIn2]);
  161. jack_port_unregister(client, rackJackPorts[rackPortAudioOut1]);
  162. jack_port_unregister(client, rackJackPorts[rackPortAudioOut2]);
  163. jack_port_unregister(client, rackJackPorts[rackPortControlIn]);
  164. jack_port_unregister(client, rackJackPorts[rackPortControlOut]);
  165. jack_port_unregister(client, rackJackPorts[rackPortMidiIn]);
  166. jack_port_unregister(client, rackJackPorts[rackPortMidiOut]);
  167. }
  168. #endif
  169. if (jack_client_close(client) == 0)
  170. {
  171. client = nullptr;
  172. return true;
  173. }
  174. else
  175. setLastError("Failed to close the JACK client");
  176. }
  177. else
  178. setLastError("Failed to deactivate the JACK client");
  179. client = nullptr;
  180. return false;
  181. }
  182. bool CarlaEngineJack::isOnAudioThread()
  183. {
  184. return (QThread::currentThread() == procThread);
  185. }
  186. bool CarlaEngineJack::isOffline()
  187. {
  188. return freewheel;
  189. }
  190. bool CarlaEngineJack::isRunning()
  191. {
  192. return bool(client);
  193. }
  194. CarlaEngineClient* CarlaEngineJack::addClient(CarlaPlugin* const plugin)
  195. {
  196. CarlaEngineClientNativeHandle handle;
  197. #ifndef BUILD_BRIDGE
  198. if (carlaOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
  199. {
  200. handle.jackClient = client;
  201. }
  202. else if (carlaOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
  203. #endif
  204. {
  205. handle.jackClient = jack_client_open(plugin->name(), JackNullOption, nullptr);
  206. jack_set_process_callback(handle.jackClient, carla_jack_process_callback_plugin, plugin);
  207. }
  208. //else if (carla_options.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  209. //{
  210. //}
  211. return new CarlaEngineClient(CarlaEngineTypeJack, handle);
  212. }
  213. // -------------------------------------------------------------------------------------------------------------------
  214. void CarlaEngineJack::handleSampleRateCallback(double newSampleRate)
  215. {
  216. sampleRate = newSampleRate;
  217. }
  218. void CarlaEngineJack::handleBufferSizeCallback(uint32_t newBufferSize)
  219. {
  220. #ifndef BUILD_BRIDGE
  221. if (carlaOptions.processHighPrecision)
  222. return;
  223. #endif
  224. bufferSizeChanged(newBufferSize);
  225. }
  226. void CarlaEngineJack::handleFreewheelCallback(bool isFreewheel)
  227. {
  228. freewheel = isFreewheel;
  229. }
  230. void CarlaEngineJack::handleProcessCallback(uint32_t nframes)
  231. {
  232. if (procThread == nullptr)
  233. procThread = QThread::currentThread();
  234. if (maxPluginNumber == 0)
  235. return;
  236. state = jack_transport_query(client, &pos);
  237. timeInfo.playing = (state != JackTransportStopped);
  238. if (pos.unique_1 == pos.unique_2)
  239. {
  240. timeInfo.frame = pos.frame;
  241. timeInfo.time = pos.usecs;
  242. if (pos.valid & JackPositionBBT)
  243. {
  244. timeInfo.valid = CarlaEngineTimeBBT;
  245. timeInfo.bbt.bar = pos.bar;
  246. timeInfo.bbt.beat = pos.beat;
  247. timeInfo.bbt.tick = pos.tick;
  248. timeInfo.bbt.bar_start_tick = pos.bar_start_tick;
  249. timeInfo.bbt.beats_per_bar = pos.beats_per_bar;
  250. timeInfo.bbt.beat_type = pos.beat_type;
  251. timeInfo.bbt.ticks_per_beat = pos.ticks_per_beat;
  252. timeInfo.bbt.beats_per_minute = pos.beats_per_minute;
  253. }
  254. else
  255. timeInfo.valid = 0;
  256. }
  257. else
  258. {
  259. timeInfo.frame = 0;
  260. timeInfo.valid = 0;
  261. }
  262. #ifndef BUILD_BRIDGE
  263. if (carlaOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
  264. {
  265. for (unsigned short i=0; i < maxPluginNumber; i++)
  266. {
  267. CarlaPlugin* const plugin = getPluginUnchecked(i);
  268. if (plugin && plugin->enabled())
  269. {
  270. plugin->engineProcessLock();
  271. plugin->initBuffers();
  272. plugin->process_jack(nframes);
  273. plugin->engineProcessUnlock();
  274. }
  275. }
  276. }
  277. else if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  278. {
  279. // get buffers from jack
  280. float* audioIn1 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioIn1], nframes);
  281. float* audioIn2 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioIn2], nframes);
  282. float* audioOut1 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioOut1], nframes);
  283. float* audioOut2 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioOut2], nframes);
  284. void* controlIn = jack_port_get_buffer(rackJackPorts[rackPortControlIn], nframes);
  285. void* controlOut = jack_port_get_buffer(rackJackPorts[rackPortControlOut], nframes);
  286. void* midiIn = jack_port_get_buffer(rackJackPorts[rackPortMidiIn], nframes);
  287. void* midiOut = jack_port_get_buffer(rackJackPorts[rackPortMidiOut], nframes);
  288. // assert buffers
  289. Q_ASSERT(audioIn1);
  290. Q_ASSERT(audioIn2);
  291. Q_ASSERT(audioOut1);
  292. Q_ASSERT(audioOut2);
  293. Q_ASSERT(controlIn);
  294. Q_ASSERT(controlOut);
  295. Q_ASSERT(midiIn);
  296. Q_ASSERT(midiOut);
  297. // create temporary audio buffers
  298. float ains_tmp_buf1[nframes];
  299. float ains_tmp_buf2[nframes];
  300. float aouts_tmp_buf1[nframes];
  301. float aouts_tmp_buf2[nframes];
  302. float* ains_tmp[2] = { ains_tmp_buf1, ains_tmp_buf2 };
  303. float* aouts_tmp[2] = { aouts_tmp_buf1, aouts_tmp_buf2 };
  304. // initialize audio input
  305. memcpy(ains_tmp_buf1, audioIn1, sizeof(float)*nframes);
  306. memcpy(ains_tmp_buf2, audioIn2, sizeof(float)*nframes);
  307. // initialize control input
  308. memset(rackControlEventsIn, 0, sizeof(CarlaEngineControlEvent)*MAX_ENGINE_CONTROL_EVENTS);
  309. {
  310. jack_midi_event_t jackEvent;
  311. const uint32_t jackEventCount = jack_midi_get_event_count(controlIn);
  312. uint32_t carlaEventIndex = 0;
  313. for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; jackEventIndex++)
  314. {
  315. if (jack_midi_event_get(&jackEvent, controlIn, jackEventIndex) != 0)
  316. continue;
  317. CarlaEngineControlEvent* const carlaEvent = &rackControlEventsIn[carlaEventIndex++];
  318. uint8_t midiStatus = jackEvent.buffer[0];
  319. uint8_t midiChannel = midiStatus & 0x0F;
  320. carlaEvent->time = jackEvent.time;
  321. carlaEvent->channel = midiChannel;
  322. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  323. {
  324. uint8_t midiControl = jackEvent.buffer[1];
  325. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  326. {
  327. uint8_t midiBank = jackEvent.buffer[2];
  328. carlaEvent->type = CarlaEngineEventMidiBankChange;
  329. carlaEvent->value = midiBank;
  330. }
  331. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  332. {
  333. carlaEvent->type = CarlaEngineEventAllSoundOff;
  334. }
  335. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  336. {
  337. carlaEvent->type = CarlaEngineEventAllNotesOff;
  338. }
  339. else
  340. {
  341. uint8_t midiValue = jackEvent.buffer[2];
  342. carlaEvent->type = CarlaEngineEventControlChange;
  343. carlaEvent->controller = midiControl;
  344. carlaEvent->value = double(midiValue)/127;
  345. }
  346. }
  347. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  348. {
  349. uint8_t midiProgram = jackEvent.buffer[1];
  350. carlaEvent->type = CarlaEngineEventMidiProgramChange;
  351. carlaEvent->value = midiProgram;
  352. }
  353. }
  354. }
  355. // initialize midi input
  356. memset(rackMidiEventsIn, 0, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  357. {
  358. uint32_t i = 0, j = 0;
  359. jack_midi_event_t jackEvent;
  360. while (jack_midi_event_get(&jackEvent, midiIn, j++) == 0)
  361. {
  362. if (i == MAX_ENGINE_MIDI_EVENTS)
  363. break;
  364. if (jackEvent.size < 4)
  365. {
  366. rackMidiEventsIn[i].time = jackEvent.time;
  367. rackMidiEventsIn[i].size = jackEvent.size;
  368. memcpy(rackMidiEventsIn[i].data, jackEvent.buffer, jackEvent.size);
  369. i += 1;
  370. }
  371. }
  372. }
  373. // initialize outputs (zero)
  374. memset(aouts_tmp_buf1, 0, sizeof(float)*nframes);
  375. memset(aouts_tmp_buf2, 0, sizeof(float)*nframes);
  376. memset(rackControlEventsOut, 0, sizeof(CarlaEngineControlEvent)*MAX_ENGINE_CONTROL_EVENTS);
  377. memset(rackMidiEventsOut, 0, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  378. bool processed = false;
  379. // process plugins
  380. for (unsigned short i=0; i < maxPluginNumber; i++)
  381. {
  382. CarlaPlugin* const plugin = getPluginUnchecked(i);
  383. if (plugin && plugin->enabled())
  384. {
  385. if (processed)
  386. {
  387. // initialize inputs (from previous outputs)
  388. memcpy(ains_tmp_buf1, aouts_tmp_buf1, sizeof(float)*nframes);
  389. memcpy(ains_tmp_buf2, aouts_tmp_buf2, sizeof(float)*nframes);
  390. memcpy(rackMidiEventsIn, rackMidiEventsOut, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  391. // initialize outputs (zero)
  392. memset(aouts_tmp_buf1, 0, sizeof(float)*nframes);
  393. memset(aouts_tmp_buf2, 0, sizeof(float)*nframes);
  394. memset(rackMidiEventsOut, 0, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  395. }
  396. // process
  397. plugin->engineProcessLock();
  398. plugin->initBuffers();
  399. if (carlaOptions.processHighPrecision)
  400. {
  401. float* ains_buffer2[2];
  402. float* aouts_buffer2[2];
  403. for (uint32_t j=0; j < nframes; j += 8)
  404. {
  405. ains_buffer2[0] = ains_tmp_buf1 + j;
  406. ains_buffer2[1] = ains_tmp_buf2 + j;
  407. aouts_buffer2[0] = aouts_tmp_buf1 + j;
  408. aouts_buffer2[1] = aouts_tmp_buf2 + j;
  409. plugin->process(ains_buffer2, aouts_buffer2, 8, j);
  410. }
  411. }
  412. else
  413. plugin->process(ains_tmp, aouts_tmp, nframes);
  414. plugin->engineProcessUnlock();
  415. // if plugin has no audio inputs, add previous buffers
  416. if (plugin->audioInCount() == 0)
  417. {
  418. for (uint32_t j=0; j < nframes; j++)
  419. {
  420. aouts_tmp_buf1[j] += ains_tmp_buf1[j];
  421. aouts_tmp_buf2[j] += ains_tmp_buf2[j];
  422. }
  423. }
  424. // if plugin has no midi output, add previous midi input
  425. if (plugin->midiOutCount() == 0)
  426. {
  427. memcpy(rackMidiEventsOut, rackMidiEventsIn, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  428. }
  429. processed = true;
  430. }
  431. }
  432. // if no plugins in the rack, copy inputs over outputs
  433. if (! processed)
  434. {
  435. memcpy(aouts_tmp_buf1, ains_tmp_buf1, sizeof(float)*nframes);
  436. memcpy(aouts_tmp_buf2, ains_tmp_buf2, sizeof(float)*nframes);
  437. memcpy(rackMidiEventsOut, rackMidiEventsIn, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  438. }
  439. // output audio
  440. memcpy(audioOut1, aouts_tmp_buf1, sizeof(float)*nframes);
  441. memcpy(audioOut2, aouts_tmp_buf2, sizeof(float)*nframes);
  442. // output control
  443. {
  444. jack_midi_clear_buffer(controlOut);
  445. for (unsigned short i=0; i < MAX_ENGINE_CONTROL_EVENTS; i++)
  446. {
  447. CarlaEngineControlEvent* const event = &rackControlEventsOut[i];
  448. if (event->type == CarlaEngineEventControlChange && MIDI_IS_CONTROL_BANK_SELECT(event->controller))
  449. event->type = CarlaEngineEventMidiBankChange;
  450. uint8_t data[4] = { 0 };
  451. switch (event->type)
  452. {
  453. case CarlaEngineEventNull:
  454. break;
  455. case CarlaEngineEventControlChange:
  456. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  457. data[1] = event->controller;
  458. data[2] = event->value * 127;
  459. jack_midi_event_write(controlOut, event->time, data, 3);
  460. break;
  461. case CarlaEngineEventMidiBankChange:
  462. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  463. data[1] = MIDI_CONTROL_BANK_SELECT;
  464. data[2] = event->value;
  465. jack_midi_event_write(controlOut, event->time, data, 3);
  466. break;
  467. case CarlaEngineEventMidiProgramChange:
  468. data[0] = MIDI_STATUS_PROGRAM_CHANGE + event->channel;
  469. data[1] = event->value;
  470. jack_midi_event_write(controlOut, event->time, data, 2);
  471. break;
  472. case CarlaEngineEventAllSoundOff:
  473. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  474. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  475. jack_midi_event_write(controlOut, event->time, data, 2);
  476. break;
  477. case CarlaEngineEventAllNotesOff:
  478. data[0] = MIDI_STATUS_CONTROL_CHANGE + event->channel;
  479. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  480. jack_midi_event_write(controlOut, event->time, data, 2);
  481. break;
  482. }
  483. }
  484. }
  485. // output midi
  486. {
  487. jack_midi_clear_buffer(midiOut);
  488. for (unsigned short i=0; i < MAX_ENGINE_MIDI_EVENTS; i++)
  489. {
  490. if (rackMidiEventsOut[i].size == 0)
  491. break;
  492. jack_midi_event_write(midiOut, rackMidiEventsOut[i].time, rackMidiEventsOut[i].data, rackMidiEventsOut[i].size);
  493. }
  494. }
  495. }
  496. #else
  497. Q_UNUSED(nframes);
  498. #endif
  499. }
  500. void CarlaEngineJack::handleShutdownCallback()
  501. {
  502. //for (unsigned short i=0; i < maxPluginNumber; i++)
  503. //{
  504. //CarlaPlugin* const plugin = getPluginUnchecked(i);
  505. //plugin->x_client
  506. //}
  507. client = nullptr;
  508. procThread = nullptr;
  509. callback(CALLBACK_QUIT, 0, 0, 0, 0.0);
  510. }
  511. CARLA_BACKEND_END_NAMESPACE
  512. #endif // CARLA_ENGINE_JACK