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.

504 lines
16KB

  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. #if 0
  23. } /* adjust editor indent */
  24. #endif
  25. // -------------------------------------------------------------------------------------------------------------------
  26. // static JACK<->Engine calls
  27. static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  28. {
  29. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  30. engine->handleSampleRateCallback(newSampleRate);
  31. return 0;
  32. }
  33. static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  34. {
  35. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  36. engine->handleBufferSizeCallback(newBufferSize);
  37. return 0;
  38. }
  39. static void carla_jack_freewheel_callback(int starting, void* arg)
  40. {
  41. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  42. engine->handleFreewheelCallback(bool(starting));
  43. }
  44. static int carla_jack_process_callback(jack_nframes_t nframes, void* arg)
  45. {
  46. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  47. engine->handleProcessCallback(nframes);
  48. return 0;
  49. }
  50. static int carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg)
  51. {
  52. CarlaPlugin* const plugin = (CarlaPlugin*)arg;
  53. if (plugin && plugin->enabled())
  54. {
  55. plugin->engineProcessLock();
  56. plugin->initBuffers();
  57. plugin->process_jack(nframes);
  58. plugin->engineProcessUnlock();
  59. }
  60. return 0;
  61. }
  62. static void carla_jack_shutdown_callback(void* arg)
  63. {
  64. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;
  65. engine->handleShutdownCallback();
  66. }
  67. // -------------------------------------------------------------------------------------------------------------------
  68. // Carla Engine (JACK)
  69. CarlaEngineJack::CarlaEngineJack() :
  70. CarlaEngine(),
  71. rackJackPorts{nullptr}
  72. {
  73. qDebug("CarlaEngineJack::CarlaEngineJack()");
  74. client = nullptr;
  75. state = JackTransportStopped;
  76. freewheel = false;
  77. procThread = nullptr;
  78. memset(&pos, 0, sizeof(jack_position_t));
  79. }
  80. CarlaEngineJack::~CarlaEngineJack()
  81. {
  82. qDebug("CarlaEngineJack::~CarlaEngineJack()");
  83. }
  84. // -------------------------------------------------------------------------------------------------------------------
  85. bool CarlaEngineJack::init(const char* const clientName)
  86. {
  87. qDebug("CarlaEngineJack::init(\"%s\")", clientName);
  88. client = jack_client_open(clientName, JackNullOption, nullptr);
  89. state = JackTransportStopped;
  90. freewheel = false;
  91. procThread = nullptr;
  92. if (client)
  93. {
  94. sampleRate = jack_get_sample_rate(client);
  95. bufferSize = jack_get_buffer_size(client);
  96. jack_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  97. jack_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  98. jack_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  99. jack_set_process_callback(client, carla_jack_process_callback, this);
  100. jack_on_shutdown(client, carla_jack_shutdown_callback, this);
  101. #ifndef BUILD_BRIDGE
  102. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  103. {
  104. rackJackPorts[rackPortAudioIn1] = jack_port_register(client, "in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  105. rackJackPorts[rackPortAudioIn2] = jack_port_register(client, "in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  106. rackJackPorts[rackPortAudioOut1] = jack_port_register(client, "out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  107. rackJackPorts[rackPortAudioOut2] = jack_port_register(client, "out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  108. rackJackPorts[rackPortControlIn] = jack_port_register(client, "control-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  109. rackJackPorts[rackPortControlOut] = jack_port_register(client, "control-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  110. rackJackPorts[rackPortMidiIn] = jack_port_register(client, "midi-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  111. rackJackPorts[rackPortMidiOut] = jack_port_register(client, "midi-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  112. }
  113. #endif
  114. if (jack_activate(client) == 0)
  115. {
  116. // set client name, fixed for OSC usage
  117. // FIXME - put this in shared?
  118. char* fixedName = strdup(jack_get_client_name(client));
  119. for (size_t i=0; i < strlen(fixedName); i++)
  120. {
  121. if (! (std::isalpha(fixedName[i]) || std::isdigit(fixedName[i])))
  122. fixedName[i] = '_';
  123. }
  124. name = strdup(fixedName);
  125. free((void*)fixedName);
  126. CarlaEngine::init(name);
  127. return true;
  128. }
  129. else
  130. {
  131. setLastError("Failed to activate the JACK client");
  132. client = nullptr;
  133. }
  134. }
  135. else
  136. setLastError("Failed to create new JACK client");
  137. return false;
  138. }
  139. bool CarlaEngineJack::close()
  140. {
  141. qDebug("CarlaEngineJack::close()");
  142. CarlaEngine::close();
  143. if (name)
  144. {
  145. free((void*)name);
  146. name = nullptr;
  147. }
  148. if (jack_deactivate(client) == 0)
  149. {
  150. #ifndef BUILD_BRIDGE
  151. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  152. {
  153. jack_port_unregister(client, rackJackPorts[rackPortAudioIn1]);
  154. jack_port_unregister(client, rackJackPorts[rackPortAudioIn2]);
  155. jack_port_unregister(client, rackJackPorts[rackPortAudioOut1]);
  156. jack_port_unregister(client, rackJackPorts[rackPortAudioOut2]);
  157. jack_port_unregister(client, rackJackPorts[rackPortControlIn]);
  158. jack_port_unregister(client, rackJackPorts[rackPortControlOut]);
  159. jack_port_unregister(client, rackJackPorts[rackPortMidiIn]);
  160. jack_port_unregister(client, rackJackPorts[rackPortMidiOut]);
  161. }
  162. #endif
  163. if (jack_client_close(client) == 0)
  164. {
  165. client = nullptr;
  166. return true;
  167. }
  168. else
  169. setLastError("Failed to close the JACK client");
  170. }
  171. else
  172. setLastError("Failed to deactivate the JACK client");
  173. client = nullptr;
  174. return false;
  175. }
  176. bool CarlaEngineJack::isOnAudioThread()
  177. {
  178. return (QThread::currentThread() == procThread);
  179. }
  180. bool CarlaEngineJack::isOffline()
  181. {
  182. return freewheel;
  183. }
  184. bool CarlaEngineJack::isRunning()
  185. {
  186. return bool(client);
  187. }
  188. CarlaEngineClient* CarlaEngineJack::addClient(CarlaPlugin* const plugin)
  189. {
  190. CarlaEngineClientNativeHandle handle = {
  191. nullptr
  192. };
  193. #ifndef BUILD_BRIDGE
  194. if (carlaOptions.process_mode == PROCESS_MODE_SINGLE_CLIENT)
  195. {
  196. handle.client = client;
  197. }
  198. else if (carlaOptions.process_mode == PROCESS_MODE_MULTIPLE_CLIENTS)
  199. #endif
  200. {
  201. handle.client = jack_client_open(plugin->name(), JackNullOption, nullptr);
  202. jack_set_process_callback(handle.client, carla_jack_process_callback_plugin, plugin);
  203. }
  204. //else if (carla_options.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  205. //{
  206. //}
  207. return new CarlaEngineClient(handle);
  208. }
  209. // -------------------------------------------------------------------------------------------------------------------
  210. void CarlaEngineJack::handleSampleRateCallback(double newSampleRate)
  211. {
  212. sampleRate = newSampleRate;
  213. }
  214. void CarlaEngineJack::handleBufferSizeCallback(uint32_t newBufferSize)
  215. {
  216. #ifndef BUILD_BRIDGE
  217. if (carlaOptions.proccess_hp)
  218. return;
  219. #endif
  220. bufferSizeChanged(newBufferSize);
  221. }
  222. void CarlaEngineJack::handleFreewheelCallback(bool isFreewheel)
  223. {
  224. freewheel = isFreewheel;
  225. }
  226. void CarlaEngineJack::handleProcessCallback(uint32_t nframes)
  227. {
  228. if (procThread == nullptr)
  229. procThread = QThread::currentThread();
  230. state = jack_transport_query(client, &pos);
  231. timeInfo.playing = (state != JackTransportStopped);
  232. if (pos.unique_1 == pos.unique_2)
  233. {
  234. timeInfo.frame = pos.frame;
  235. timeInfo.time = pos.usecs;
  236. if (pos.valid & JackPositionBBT)
  237. {
  238. timeInfo.valid = CarlaEngineTimeBBT;
  239. timeInfo.bbt.bar = pos.bar;
  240. timeInfo.bbt.beat = pos.beat;
  241. timeInfo.bbt.tick = pos.tick;
  242. timeInfo.bbt.bar_start_tick = pos.bar_start_tick;
  243. timeInfo.bbt.beats_per_bar = pos.beats_per_bar;
  244. timeInfo.bbt.beat_type = pos.beat_type;
  245. timeInfo.bbt.ticks_per_beat = pos.ticks_per_beat;
  246. timeInfo.bbt.beats_per_minute = pos.beats_per_minute;
  247. }
  248. else
  249. timeInfo.valid = 0;
  250. }
  251. else
  252. {
  253. timeInfo.frame = 0;
  254. timeInfo.valid = 0;
  255. }
  256. #ifndef BUILD_BRIDGE
  257. if (carlaOptions.process_mode == PROCESS_MODE_SINGLE_CLIENT)
  258. {
  259. for (unsigned short i=0; i < MAX_PLUGINS; i++)
  260. {
  261. CarlaPlugin* const plugin = getPlugin(i);
  262. if (plugin && plugin->enabled())
  263. {
  264. plugin->engineProcessLock();
  265. plugin->initBuffers();
  266. plugin->process_jack(nframes);
  267. plugin->engineProcessUnlock();
  268. }
  269. }
  270. }
  271. else if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  272. {
  273. // get buffers from jack
  274. float* audioIn1 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioIn1], nframes);
  275. float* audioIn2 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioIn2], nframes);
  276. float* audioOut1 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioOut1], nframes);
  277. float* audioOut2 = (float*)jack_port_get_buffer(rackJackPorts[rackPortAudioOut2], nframes);
  278. void* controlIn = jack_port_get_buffer(rackJackPorts[rackPortControlIn], nframes);
  279. void* controlOut = jack_port_get_buffer(rackJackPorts[rackPortControlOut], nframes);
  280. void* midiIn = jack_port_get_buffer(rackJackPorts[rackPortMidiIn], nframes);
  281. void* midiOut = jack_port_get_buffer(rackJackPorts[rackPortMidiOut], nframes);
  282. // assert buffers
  283. Q_ASSERT(audioIn1);
  284. Q_ASSERT(audioIn2);
  285. Q_ASSERT(audioOut1);
  286. Q_ASSERT(audioOut2);
  287. Q_ASSERT(controlIn);
  288. Q_ASSERT(controlOut);
  289. Q_ASSERT(midiIn);
  290. Q_ASSERT(midiOut);
  291. // create temporary audio buffers
  292. float ains_tmp_buf1[nframes];
  293. float ains_tmp_buf2[nframes];
  294. float aouts_tmp_buf1[nframes];
  295. float aouts_tmp_buf2[nframes];
  296. float* ains_tmp[2] = { ains_tmp_buf1, ains_tmp_buf2 };
  297. float* aouts_tmp[2] = { aouts_tmp_buf1, aouts_tmp_buf2 };
  298. // initialize audio input
  299. memcpy(ains_tmp_buf1, audioIn1, sizeof(float)*nframes);
  300. memcpy(ains_tmp_buf2, audioIn2, sizeof(float)*nframes);
  301. // initialize control input
  302. memset(rackControlEventsIn, 0, sizeof(CarlaEngineControlEvent)*MAX_ENGINE_CONTROL_EVENTS);
  303. {
  304. // TODO
  305. }
  306. // initialize midi input
  307. memset(rackMidiEventsIn, 0, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  308. {
  309. uint32_t i = 0, j = 0;
  310. jack_midi_event_t jackEvent;
  311. while (jack_midi_event_get(&jackEvent, midiIn, j++) == 0)
  312. {
  313. if (i == MAX_ENGINE_MIDI_EVENTS)
  314. break;
  315. if (jackEvent.size < 4)
  316. {
  317. rackMidiEventsIn[i].time = jackEvent.time;
  318. rackMidiEventsIn[i].size = jackEvent.size;
  319. memcpy(rackMidiEventsIn[i].data, jackEvent.buffer, jackEvent.size);
  320. i += 1;
  321. }
  322. }
  323. }
  324. // initialize outputs (zero)
  325. memset(aouts_tmp_buf1, 0, sizeof(float)*nframes);
  326. memset(aouts_tmp_buf2, 0, sizeof(float)*nframes);
  327. memset(rackControlEventsOut, 0, sizeof(CarlaEngineControlEvent)*MAX_ENGINE_CONTROL_EVENTS);
  328. memset(rackMidiEventsOut, 0, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  329. bool processed = false;
  330. // process plugins
  331. for (unsigned short i=0; i < MAX_PLUGINS; i++)
  332. {
  333. CarlaPlugin* const plugin = getPlugin(i);
  334. if (plugin && plugin->enabled())
  335. {
  336. if (processed)
  337. {
  338. // initialize inputs (from previous outputs)
  339. memcpy(ains_tmp_buf1, aouts_tmp_buf1, sizeof(float)*nframes);
  340. memcpy(ains_tmp_buf2, aouts_tmp_buf2, sizeof(float)*nframes);
  341. memcpy(rackMidiEventsIn, rackMidiEventsOut, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  342. // initialize outputs (zero)
  343. memset(aouts_tmp_buf1, 0, sizeof(float)*nframes);
  344. memset(aouts_tmp_buf2, 0, sizeof(float)*nframes);
  345. memset(rackMidiEventsOut, 0, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  346. }
  347. // process
  348. plugin->engineProcessLock();
  349. plugin->initBuffers();
  350. if (carlaOptions.proccess_hp)
  351. {
  352. float* ains_buffer2[2];
  353. float* aouts_buffer2[2];
  354. for (uint32_t j=0; j < nframes; j += 8)
  355. {
  356. ains_buffer2[0] = ains_tmp_buf1 + j;
  357. ains_buffer2[1] = ains_tmp_buf2 + j;
  358. aouts_buffer2[0] = aouts_tmp_buf1 + j;
  359. aouts_buffer2[1] = aouts_tmp_buf2 + j;
  360. plugin->process(ains_buffer2, aouts_buffer2, 8, j);
  361. }
  362. }
  363. else
  364. plugin->process(ains_tmp, aouts_tmp, nframes);
  365. plugin->engineProcessUnlock();
  366. // if plugin has no audio inputs, add previous buffers
  367. if (plugin->audioInCount() == 0)
  368. {
  369. for (uint32_t j=0; j < nframes; j++)
  370. {
  371. aouts_tmp_buf1[j] += ains_tmp_buf1[j];
  372. aouts_tmp_buf2[j] += ains_tmp_buf2[j];
  373. }
  374. }
  375. processed = true;
  376. }
  377. }
  378. // if no plugins in the rack, copy inputs over outputs
  379. if (! processed)
  380. {
  381. memcpy(aouts_tmp_buf1, ains_tmp_buf1, sizeof(float)*nframes);
  382. memcpy(aouts_tmp_buf2, ains_tmp_buf2, sizeof(float)*nframes);
  383. memcpy(rackMidiEventsOut, rackMidiEventsIn, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  384. }
  385. // output audio
  386. memcpy(audioOut1, aouts_tmp_buf1, sizeof(float)*nframes);
  387. memcpy(audioOut2, aouts_tmp_buf2, sizeof(float)*nframes);
  388. // output control
  389. {
  390. // TODO
  391. }
  392. // output midi
  393. {
  394. jack_midi_clear_buffer(midiOut);
  395. for (unsigned short i=0; i < MAX_ENGINE_MIDI_EVENTS; i++)
  396. {
  397. if (rackMidiEventsOut[i].size == 0)
  398. break;
  399. jack_midi_event_write(midiOut, rackMidiEventsOut[i].time, rackMidiEventsOut[i].data, rackMidiEventsOut[i].size);
  400. }
  401. }
  402. }
  403. #else
  404. Q_UNUSED(nframes);
  405. #endif
  406. }
  407. void CarlaEngineJack::handleShutdownCallback()
  408. {
  409. //for (unsigned short i=0; i < MAX_PLUGINS; i++)
  410. //{
  411. // FIXME
  412. //CarlaPlugin* plugin = CarlaPlugins[i];
  413. //if (plugin && plugin->id() == plugin_id)
  414. // plugin->jack_client = nullptr;
  415. //}
  416. client = nullptr;
  417. procThread = nullptr;
  418. callback(CALLBACK_QUIT, 0, 0, 0, 0.0);
  419. }
  420. CARLA_BACKEND_END_NAMESPACE
  421. #endif // CARLA_ENGINE_JACK