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.

CarlaEngineJack.cpp 59KB

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