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

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