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 71KB

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