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