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

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
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
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
11 years ago
11 years ago
11 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
11 years ago
10 years ago
10 years ago
10 years ago
11 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
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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. fFreewheel = false;
  425. fTransportState = JackTransportStopped;
  426. carla_zeroStruct<jack_position_t>(fTransportPos);
  427. #ifdef BUILD_BRIDGE
  428. if (pData->bufferSize == 0 || pData->sampleRate == 0.0)
  429. {
  430. // open temp client to get initial buffer-size and sample-rate values
  431. if (jack_client_t* tmpClient = jackbridge_client_open(clientName, JackNullOption, nullptr))
  432. {
  433. pData->bufferSize = jackbridge_get_buffer_size(tmpClient);
  434. pData->sampleRate = jackbridge_get_sample_rate(tmpClient);
  435. jackbridge_client_close(tmpClient);
  436. }
  437. }
  438. fIsRunning = true;
  439. return CarlaEngine::init(clientName);
  440. #else
  441. fLastGroupId = 0;
  442. fLastPortId = 0;
  443. fLastConnectionId = 0;
  444. fUsedGroupNames.clear();
  445. fUsedPortNames.clear();
  446. fUsedConnections.clear();
  447. fNewGroups.clear();
  448. fClient = jackbridge_client_open(clientName, JackNullOption, nullptr);
  449. if (fClient != nullptr)
  450. {
  451. pData->bufferSize = jackbridge_get_buffer_size(fClient);
  452. pData->sampleRate = jackbridge_get_sample_rate(fClient);
  453. jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback, this);
  454. jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback, this);
  455. jackbridge_set_freewheel_callback(fClient, carla_jack_freewheel_callback, this);
  456. //jackbridge_set_latency_callback(fClient, carla_jack_latency_callback, this);
  457. jackbridge_set_process_callback(fClient, carla_jack_process_callback, this);
  458. jackbridge_on_shutdown(fClient, carla_jack_shutdown_callback, this);
  459. const char* const jackClientName(jackbridge_get_client_name(fClient));
  460. initJackPatchbay(jackClientName);
  461. jackbridge_set_client_registration_callback(fClient, carla_jack_client_registration_callback, this);
  462. jackbridge_set_port_registration_callback(fClient, carla_jack_port_registration_callback, this);
  463. jackbridge_set_port_connect_callback(fClient, carla_jack_port_connect_callback, this);
  464. jackbridge_set_port_rename_callback(fClient, carla_jack_port_rename_callback, this);
  465. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  466. {
  467. fRackPorts[kRackPortAudioIn1] = jackbridge_port_register(fClient, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  468. fRackPorts[kRackPortAudioIn2] = jackbridge_port_register(fClient, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  469. fRackPorts[kRackPortAudioOut1] = jackbridge_port_register(fClient, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  470. fRackPorts[kRackPortAudioOut2] = jackbridge_port_register(fClient, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  471. fRackPorts[kRackPortEventIn] = jackbridge_port_register(fClient, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  472. fRackPorts[kRackPortEventOut] = jackbridge_port_register(fClient, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  473. }
  474. if (jackbridge_activate(fClient))
  475. {
  476. CarlaEngine::init(jackClientName);
  477. return true;
  478. }
  479. else
  480. {
  481. setLastError("Failed to activate the JACK client");
  482. jackbridge_client_close(fClient);
  483. fClient = nullptr;
  484. }
  485. }
  486. else
  487. setLastError("Failed to create new JACK client");
  488. return false;
  489. #endif
  490. }
  491. bool close() override
  492. {
  493. carla_debug("CarlaEngineJack::close()");
  494. CarlaEngine::close();
  495. #ifdef BUILD_BRIDGE
  496. fClient = nullptr;
  497. fIsRunning = false;
  498. return true;
  499. #else
  500. if (jackbridge_deactivate(fClient))
  501. {
  502. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  503. {
  504. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioIn1]);
  505. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioIn2]);
  506. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioOut1]);
  507. jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioOut2]);
  508. jackbridge_port_unregister(fClient, fRackPorts[kRackPortEventIn]);
  509. jackbridge_port_unregister(fClient, fRackPorts[kRackPortEventOut]);
  510. carla_fill<jack_port_t*>(fRackPorts, kRackPortCount, nullptr);
  511. }
  512. if (jackbridge_client_close(fClient))
  513. {
  514. fClient = nullptr;
  515. return true;
  516. }
  517. else
  518. setLastError("Failed to close the JACK client");
  519. }
  520. else
  521. setLastError("Failed to deactivate the JACK client");
  522. fClient = nullptr;
  523. fUsedGroupNames.clear();
  524. fUsedPortNames.clear();
  525. fUsedConnections.clear();
  526. fNewGroups.clear();
  527. return false;
  528. #endif
  529. }
  530. void idle() override
  531. {
  532. CarlaEngine::idle();
  533. if (fNewGroups.count() == 0)
  534. return;
  535. LinkedList<int> newPlugins;
  536. fNewGroups.spliceInsert(newPlugins, true);
  537. for (LinkedList<int>::Itenerator it = newPlugins.begin(); it.valid(); it.next())
  538. {
  539. const int groupId(it.getValue());
  540. const char* const groupName(getGroupName(groupId));
  541. CARLA_SAFE_ASSERT_CONTINUE(groupId > 0 && groupName != nullptr && groupName[0] != '\0');
  542. int pluginId = -1;
  543. PatchbayIcon icon = PATCHBAY_ICON_PLUGIN;
  544. if (findPluginIdAndIcon(groupName, pluginId, icon))
  545. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_DATA_CHANGED, static_cast<uint>(groupId), icon, pluginId, 0.0f, nullptr);
  546. }
  547. newPlugins.clear();
  548. }
  549. bool isRunning() const noexcept override
  550. {
  551. #ifdef BUILD_BRIDGE
  552. return (fClient != nullptr || fIsRunning);
  553. #else
  554. return (fClient != nullptr);
  555. #endif
  556. }
  557. bool isOffline() const noexcept override
  558. {
  559. return fFreewheel;
  560. }
  561. EngineType getType() const noexcept override
  562. {
  563. return kEngineTypeJack;
  564. }
  565. const char* getCurrentDriverName() const noexcept override
  566. {
  567. return "JACK";
  568. }
  569. CarlaEngineClient* addClient(CarlaPlugin* const plugin) override
  570. {
  571. jack_client_t* client = nullptr;
  572. #ifdef BUILD_BRIDGE
  573. client = fClient = jackbridge_client_open(plugin->getName(), JackNullOption, nullptr);
  574. CARLA_SAFE_ASSERT_RETURN(client != nullptr, nullptr);
  575. pData->bufferSize = jackbridge_get_buffer_size(client);
  576. pData->sampleRate = jackbridge_get_sample_rate(client);
  577. jackbridge_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  578. jackbridge_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  579. jackbridge_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  580. //jackbridge_set_latency_callback(client, carla_jack_latency_callback, this);
  581. jackbridge_set_process_callback(client, carla_jack_process_callback, this);
  582. jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
  583. #else
  584. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  585. {
  586. client = fClient;
  587. }
  588. else if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  589. {
  590. client = jackbridge_client_open(plugin->getName(), JackNullOption, nullptr);
  591. CARLA_SAFE_ASSERT_RETURN(client != nullptr, nullptr);
  592. //jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, plugin);
  593. jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, plugin);
  594. }
  595. #endif
  596. return new CarlaEngineJackClient(*this, client);
  597. }
  598. #ifndef BUILD_BRIDGE
  599. const char* renamePlugin(const unsigned int id, const char* const newName) override
  600. {
  601. CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount > 0, nullptr);
  602. CARLA_SAFE_ASSERT_RETURN(id < pData->curPluginCount, nullptr);
  603. CARLA_SAFE_ASSERT_RETURN(pData->plugins != nullptr, nullptr);
  604. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0', nullptr);
  605. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  606. if (plugin == nullptr)
  607. {
  608. carla_stderr("CarlaEngine::clonePlugin(%i) - could not find plugin", id);
  609. return nullptr;
  610. }
  611. CARLA_SAFE_ASSERT(plugin->getId() == id);
  612. bool needsReinit = (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT);
  613. const char* name = getUniquePluginName(newName);
  614. // TODO - use rename port if single-client and JACK2
  615. // JACK client rename
  616. if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  617. {
  618. CarlaEngineJackClient* const client((CarlaEngineJackClient*)plugin->getEngineClient());
  619. #if 0
  620. if (bridge.client_rename_ptr != nullptr)
  621. {
  622. name = bridge.client_rename_ptr(client->fClient, name);
  623. }
  624. else
  625. #endif
  626. {
  627. // we should not be able to do this, jack really needs to allow client rename
  628. needsReinit = true;
  629. if (jack_client_t* const jclient = jackbridge_client_open(name, JackNullOption, nullptr))
  630. {
  631. // close old client
  632. plugin->setEnabled(false);
  633. if (client->isActive())
  634. client->deactivate();
  635. plugin->clearBuffers();
  636. jackbridge_client_close(client->fClient);
  637. // set new client data
  638. name = jackbridge_get_client_name(jclient);
  639. jackbridge_set_process_callback(jclient, carla_jack_process_callback_plugin, plugin);
  640. //jackbridge_set_latency_callback(jclient, carla_jack_latency_callback_plugin, plugin);
  641. client->fClient = jclient;
  642. }
  643. }
  644. }
  645. if (name == nullptr)
  646. return nullptr;
  647. // Rename
  648. plugin->setName(name);
  649. if (needsReinit)
  650. {
  651. // reload plugin to recreate its ports
  652. const SaveState& saveState(plugin->getSaveState());
  653. plugin->reload();
  654. plugin->loadSaveState(saveState);
  655. }
  656. return name;
  657. }
  658. // -------------------------------------------------------------------
  659. // Patchbay
  660. bool patchbayConnect(int portA, int portB) override
  661. {
  662. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  663. if (fClient == nullptr)
  664. {
  665. setLastError("Invalid JACK client");
  666. return false;
  667. }
  668. char portNameA[STR_MAX+1];
  669. char portNameB[STR_MAX+1];
  670. getFullPortName(portA, portNameA);
  671. getFullPortName(portB, portNameB);
  672. if (! jackbridge_connect(fClient, portNameA, portNameB))
  673. {
  674. setLastError("JACK operation failed");
  675. return false;
  676. }
  677. return true;
  678. }
  679. bool patchbayDisconnect(uint connectionId) override
  680. {
  681. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  682. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.begin(); it.valid(); it.next())
  683. {
  684. const ConnectionToId& connectionToId(it.getValue());
  685. if (connectionToId.id == connectionId)
  686. {
  687. char portNameOut[STR_MAX+1];
  688. char portNameIn[STR_MAX+1];
  689. getFullPortName(connectionToId.portOut, portNameOut);
  690. getFullPortName(connectionToId.portIn, portNameIn);
  691. if (! jackbridge_disconnect(fClient, portNameOut, portNameIn))
  692. {
  693. setLastError("JACK operation failed");
  694. return false;
  695. }
  696. return true;
  697. }
  698. }
  699. setLastError("Failed to find the requested connection");
  700. return false;
  701. }
  702. bool patchbayRefresh() override
  703. {
  704. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  705. fLastGroupId = 0;
  706. fLastPortId = 0;
  707. fLastConnectionId = 0;
  708. fUsedGroupNames.clear();
  709. fUsedPortNames.clear();
  710. fUsedConnections.clear();
  711. fNewGroups.clear();
  712. initJackPatchbay(jackbridge_get_client_name(fClient));
  713. return true;
  714. }
  715. // -------------------------------------------------------------------
  716. // Transport
  717. void transportPlay() noexcept override
  718. {
  719. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  720. return CarlaEngine::transportPlay();
  721. if (fClient != nullptr)
  722. {
  723. try {
  724. jackbridge_transport_start(fClient);
  725. } catch(...) {}
  726. }
  727. }
  728. void transportPause() noexcept override
  729. {
  730. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  731. return CarlaEngine::transportPause();
  732. if (fClient != nullptr)
  733. {
  734. try {
  735. jackbridge_transport_stop(fClient);
  736. } catch(...) {}
  737. }
  738. }
  739. void transportRelocate(const uint64_t frame) noexcept override
  740. {
  741. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  742. return CarlaEngine::transportRelocate(frame);
  743. if (fClient != nullptr)
  744. {
  745. try {
  746. jackbridge_transport_locate(fClient, static_cast<jack_nframes_t>(frame));
  747. } catch(...) {}
  748. }
  749. }
  750. // -------------------------------------------------------------------
  751. // Patchbay stuff
  752. const char* const* getPatchbayConnections() const override
  753. {
  754. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, nullptr);
  755. carla_debug("CarlaEngineJack::getPatchbayConnections()");
  756. LinkedList<const char*> connList;
  757. if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
  758. {
  759. for (int i=0; ports[i] != nullptr; ++i)
  760. {
  761. jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
  762. const char* const fullPortName(ports[i]);
  763. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  764. if (const char** connections = jackbridge_port_get_all_connections(fClient, jackPort))
  765. {
  766. for (int j=0; connections[j] != nullptr; ++j)
  767. {
  768. connList.append(carla_strdup(fullPortName));
  769. connList.append(carla_strdup(connections[j]));
  770. }
  771. jackbridge_free(connections);
  772. }
  773. }
  774. jackbridge_free(ports);
  775. }
  776. const size_t connCount(connList.count());
  777. if (connCount == 0)
  778. return nullptr;
  779. const char** const retConns = new const char*[connCount+1];
  780. for (size_t i=0; i < connCount; ++i)
  781. retConns[i] = connList.getAt(i);
  782. retConns[connCount] = nullptr;
  783. connList.clear();
  784. return retConns;
  785. }
  786. void restorePatchbayConnection(const char* const connSource, const char* const connTarget) override
  787. {
  788. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr,);
  789. CARLA_SAFE_ASSERT_RETURN(connSource != nullptr && connSource[0] != '\0',);
  790. CARLA_SAFE_ASSERT_RETURN(connTarget != nullptr && connTarget[0] != '\0',);
  791. carla_debug("CarlaEngineJack::restorePatchbayConnection(\"%s\", \"%s\")", connSource, connTarget);
  792. if (jack_port_t* const port = jackbridge_port_by_name(fClient, connSource))
  793. {
  794. if (jackbridge_port_by_name(fClient, connTarget) == nullptr)
  795. return;
  796. if (! jackbridge_port_connected_to(port, connTarget))
  797. jackbridge_connect(fClient, connSource, connTarget);
  798. }
  799. }
  800. #endif
  801. // -------------------------------------------------------------------
  802. protected:
  803. void handleJackBufferSizeCallback(const uint32_t newBufferSize)
  804. {
  805. if (pData->bufferSize == newBufferSize)
  806. return;
  807. pData->bufferSize = newBufferSize;
  808. bufferSizeChanged(newBufferSize);
  809. }
  810. void handleJackSampleRateCallback(const double newSampleRate)
  811. {
  812. if (pData->sampleRate == newSampleRate)
  813. return;
  814. pData->sampleRate = newSampleRate;
  815. sampleRateChanged(newSampleRate);
  816. }
  817. void handleJackFreewheelCallback(const bool isFreewheel)
  818. {
  819. if (fFreewheel == isFreewheel)
  820. return;
  821. fFreewheel = isFreewheel;
  822. offlineModeChanged(isFreewheel);
  823. }
  824. void saveTransportInfo()
  825. {
  826. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  827. return;
  828. fTransportPos.unique_1 = fTransportPos.unique_2 + 1; // invalidate
  829. fTransportState = jackbridge_transport_query(fClient, &fTransportPos);
  830. pData->timeInfo.playing = (fTransportState == JackTransportRolling);
  831. if (fTransportPos.unique_1 == fTransportPos.unique_2)
  832. {
  833. pData->timeInfo.frame = fTransportPos.frame;
  834. pData->timeInfo.usecs = fTransportPos.usecs;
  835. if (fTransportPos.valid & JackPositionBBT)
  836. {
  837. pData->timeInfo.valid = EngineTimeInfo::kValidBBT;
  838. pData->timeInfo.bbt.bar = fTransportPos.bar;
  839. pData->timeInfo.bbt.beat = fTransportPos.beat;
  840. pData->timeInfo.bbt.tick = fTransportPos.tick;
  841. pData->timeInfo.bbt.barStartTick = fTransportPos.bar_start_tick;
  842. pData->timeInfo.bbt.beatsPerBar = fTransportPos.beats_per_bar;
  843. pData->timeInfo.bbt.beatType = fTransportPos.beat_type;
  844. pData->timeInfo.bbt.ticksPerBeat = fTransportPos.ticks_per_beat;
  845. pData->timeInfo.bbt.beatsPerMinute = fTransportPos.beats_per_minute;
  846. }
  847. else
  848. pData->timeInfo.valid = 0x0;
  849. }
  850. else
  851. {
  852. pData->timeInfo.frame = 0;
  853. pData->timeInfo.valid = 0x0;
  854. }
  855. }
  856. void handleJackProcessCallback(const uint32_t nframes)
  857. {
  858. saveTransportInfo();
  859. if (pData->curPluginCount == 0)
  860. {
  861. #ifndef BUILD_BRIDGE
  862. // pass-through
  863. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  864. {
  865. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  866. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  867. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  868. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  869. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes);
  870. FLOAT_COPY(audioOut1, audioIn1, nframes);
  871. FLOAT_COPY(audioOut2, audioIn2, nframes);
  872. jackbridge_midi_clear_buffer(eventOut);
  873. }
  874. #endif
  875. return runPendingRtEvents();
  876. }
  877. #ifdef BUILD_BRIDGE
  878. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  879. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(fFreewheel))
  880. {
  881. plugin->initBuffers();
  882. processPlugin(plugin, nframes);
  883. plugin->unlock();
  884. }
  885. return runPendingRtEvents();
  886. #else
  887. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  888. {
  889. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  890. {
  891. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  892. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(fFreewheel))
  893. {
  894. plugin->initBuffers();
  895. processPlugin(plugin, nframes);
  896. plugin->unlock();
  897. }
  898. }
  899. return runPendingRtEvents();
  900. }
  901. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  902. {
  903. // get buffers from jack
  904. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  905. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  906. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  907. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  908. void* const eventIn = jackbridge_port_get_buffer(fRackPorts[kRackPortEventIn], nframes);
  909. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes);
  910. // assert buffers
  911. CARLA_ASSERT(audioIn1 != nullptr);
  912. CARLA_ASSERT(audioIn2 != nullptr);
  913. CARLA_ASSERT(audioOut1 != nullptr);
  914. CARLA_ASSERT(audioOut2 != nullptr);
  915. CARLA_ASSERT(eventIn != nullptr);
  916. CARLA_ASSERT(eventOut != nullptr);
  917. // create audio buffers
  918. float* inBuf[2] = { audioIn1, audioIn2 };
  919. float* outBuf[2] = { audioOut1, audioOut2 };
  920. // initialize input events
  921. carla_zeroStruct<EngineEvent>(pData->bufEvents.in, kMaxEngineEventInternalCount);
  922. {
  923. uint32_t engineEventIndex = 0;
  924. jack_midi_event_t jackEvent;
  925. const uint32_t jackEventCount(jackbridge_midi_get_event_count(eventIn));
  926. for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; ++jackEventIndex)
  927. {
  928. if (! jackbridge_midi_event_get(&jackEvent, eventIn, jackEventIndex))
  929. continue;
  930. CARLA_SAFE_ASSERT_CONTINUE(jackEvent.size <= 0xFF /* uint8_t max */);
  931. EngineEvent& engineEvent(pData->bufEvents.in[engineEventIndex++]);
  932. engineEvent.time = jackEvent.time;
  933. engineEvent.fillFromMidiData(static_cast<uint8_t>(jackEvent.size), jackEvent.buffer);
  934. if (engineEventIndex >= kMaxEngineEventInternalCount)
  935. break;
  936. }
  937. }
  938. // process rack
  939. pData->processRack(inBuf, outBuf, nframes, fFreewheel);
  940. // output control
  941. {
  942. jackbridge_midi_clear_buffer(eventOut);
  943. for (unsigned short i=0; i < kMaxEngineEventInternalCount; ++i)
  944. {
  945. const EngineEvent& engineEvent(pData->bufEvents.out[i]);
  946. uint8_t size = 0;
  947. uint8_t data[3] = { 0, 0, 0 };
  948. const uint8_t* dataPtr = data;
  949. switch (engineEvent.type)
  950. {
  951. case kEngineEventTypeNull:
  952. break;
  953. case kEngineEventTypeControl:
  954. {
  955. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  956. ctrlEvent.dumpToMidiData(engineEvent.channel, size, data);
  957. break;
  958. }
  959. case kEngineEventTypeMidi:
  960. {
  961. const EngineMidiEvent& midiEvent(engineEvent.midi);
  962. size = midiEvent.size;
  963. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  964. dataPtr = midiEvent.dataExt;
  965. else
  966. dataPtr = midiEvent.dataExt;
  967. break;
  968. }
  969. }
  970. if (size > 0)
  971. jackbridge_midi_event_write(eventOut, engineEvent.time, dataPtr, size);
  972. }
  973. }
  974. return runPendingRtEvents();
  975. }
  976. #endif // ! BUILD_BRIDGE
  977. runPendingRtEvents();
  978. }
  979. #if 0
  980. void handleJackLatencyCallback(const jack_latency_callback_mode_t mode)
  981. {
  982. if (pData->options.processMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  983. return;
  984. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  985. {
  986. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  987. if (plugin != nullptr && plugin->isEnabled())
  988. latencyPlugin(plugin, mode);
  989. }
  990. }
  991. #endif
  992. #ifndef BUILD_BRIDGE
  993. void handleJackClientRegistrationCallback(const char* const name, const bool reg)
  994. {
  995. // do nothing on client registration, wait for first port
  996. if (reg) return;
  997. const int id(getGroupId(name)); // also checks name nullness
  998. if (id < 0)
  999. return;
  1000. GroupNameToId groupNameToId;
  1001. groupNameToId.setData(id, name);
  1002. fUsedGroupNames.removeAll(groupNameToId);
  1003. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, static_cast<uint>(id), 0, 0, 0.0f, nullptr);
  1004. }
  1005. void handleJackPortRegistrationCallback(const jack_port_id_t port, const bool reg)
  1006. {
  1007. jack_port_t* const jackPort(jackbridge_port_by_id(fClient, port));
  1008. const char* const portName(jackbridge_port_short_name(jackPort));
  1009. const char* const fullPortName(jackbridge_port_name(jackPort));
  1010. CARLA_SAFE_ASSERT_RETURN(jackPort != nullptr,);
  1011. CARLA_SAFE_ASSERT_RETURN(portName != nullptr,);
  1012. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr,);
  1013. CarlaString groupName(fullPortName);
  1014. groupName.truncate(groupName.rfind(portName)-1);
  1015. int groupId = getGroupId(groupName);
  1016. if (reg)
  1017. {
  1018. const int jackPortFlags(jackbridge_port_flags(jackPort));
  1019. if (groupId < 0)
  1020. {
  1021. groupId = fLastGroupId++;
  1022. CARLA_SAFE_ASSERT_RETURN(groupId >= 0,);
  1023. GroupNameToId groupNameToId;
  1024. groupNameToId.setData(groupId, groupName);
  1025. fUsedGroupNames.append(groupNameToId);
  1026. PatchbayIcon icon = (jackPortFlags & JackPortIsPhysical) ? PATCHBAY_ICON_HARDWARE : PATCHBAY_ICON_APPLICATION;
  1027. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, static_cast<uint>(groupId), icon, -1, 0.0f, groupName);
  1028. fNewGroups.append(groupId);
  1029. }
  1030. bool portIsInput = (jackPortFlags & JackPortIsInput);
  1031. bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
  1032. bool portIsCV = false;
  1033. if (jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  1034. {
  1035. char* value = nullptr;
  1036. char* type = nullptr;
  1037. if (jackbridge_get_property(uuid, "urn:jack:IsControlVoltage", &value, &type) && value != nullptr && type != nullptr && std::strcmp(type, "text/plain") == 0)
  1038. portIsCV = (std::strcmp(value, "YES") == 0);
  1039. }
  1040. unsigned int canvasPortFlags = 0x0;
  1041. canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
  1042. canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_TYPE_AUDIO : PATCHBAY_PORT_TYPE_MIDI;
  1043. if (portIsAudio && portIsCV)
  1044. canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
  1045. PortNameToId portNameToId;
  1046. portNameToId.setData(groupId, fLastPortId++, portName, fullPortName);
  1047. fUsedPortNames.append(portNameToId);
  1048. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, static_cast<uint>(groupId), portNameToId.portId, static_cast<int>(canvasPortFlags), 0.0f, portName);
  1049. }
  1050. else
  1051. {
  1052. const int portId(getPortId(fullPortName));
  1053. CARLA_SAFE_ASSERT_RETURN(groupId >= 0,);
  1054. CARLA_SAFE_ASSERT_RETURN(portId >= 0,);
  1055. PortNameToId portNameToId;
  1056. portNameToId.setData(groupId, portId, portName, fullPortName);
  1057. fUsedPortNames.removeOne(portNameToId);
  1058. callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, static_cast<uint>(groupId), portId, 0, 0.0f, nullptr);
  1059. }
  1060. }
  1061. void handleJackPortConnectCallback(const jack_port_id_t a, const jack_port_id_t b, const bool connect)
  1062. {
  1063. jack_port_t* const jackPortA(jackbridge_port_by_id(fClient, a));
  1064. jack_port_t* const jackPortB(jackbridge_port_by_id(fClient, b));
  1065. const char* const fullPortNameA(jackbridge_port_name(jackPortA));
  1066. const char* const fullPortNameB(jackbridge_port_name(jackPortB));
  1067. CARLA_SAFE_ASSERT_RETURN(jackPortA != nullptr,);
  1068. CARLA_SAFE_ASSERT_RETURN(jackPortB != nullptr,);
  1069. CARLA_SAFE_ASSERT_RETURN(fullPortNameA != nullptr,);
  1070. CARLA_SAFE_ASSERT_RETURN(fullPortNameB != nullptr,);
  1071. const int portIdA(getPortId(fullPortNameA));
  1072. const int portIdB(getPortId(fullPortNameB));
  1073. if (portIdA == -1 || portIdB == -1)
  1074. return;
  1075. if (connect)
  1076. {
  1077. ConnectionToId connectionToId;
  1078. connectionToId.setData(fLastConnectionId++, portIdA, portIdB);
  1079. fUsedConnections.append(connectionToId);
  1080. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  1081. }
  1082. else
  1083. {
  1084. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.begin(); it.valid(); it.next())
  1085. {
  1086. const ConnectionToId& connectionToId(it.getValue());
  1087. if (connectionToId.portOut == portIdA && connectionToId.portIn == portIdB)
  1088. {
  1089. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, static_cast<uint>(connectionToId.id), connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  1090. fUsedConnections.remove(it);
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. }
  1096. void handleJackClientRenameCallback(const char* const oldName, const char* const newName)
  1097. {
  1098. for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroupNames.begin(); it.valid(); it.next())
  1099. {
  1100. GroupNameToId& groupNameToId(it.getValue());
  1101. if (std::strcmp(groupNameToId.name, oldName) == 0)
  1102. {
  1103. groupNameToId.rename(newName);
  1104. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED, static_cast<uint>(groupNameToId.id), 0, 0, 0.0f, newName);
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. void handleJackPortRenameCallback(const jack_port_id_t port, const char* const oldName, const char* const newName)
  1110. {
  1111. jack_port_t* const jackPort(jackbridge_port_by_id(fClient, port));
  1112. const char* const portName(jackbridge_port_short_name(jackPort));
  1113. CARLA_SAFE_ASSERT_RETURN(jackPort != nullptr,);
  1114. CARLA_SAFE_ASSERT_RETURN(portName != nullptr,);
  1115. CarlaString groupName(newName);
  1116. groupName.truncate(groupName.rfind(portName)-1);
  1117. const int groupId(getGroupId(groupName));
  1118. CARLA_SAFE_ASSERT_RETURN(groupId >= 0,);
  1119. for (LinkedList<PortNameToId>::Itenerator it = fUsedPortNames.begin(); it.valid(); it.next())
  1120. {
  1121. PortNameToId& portNameId(it.getValue());
  1122. if (std::strcmp(portNameId.fullName, oldName) == 0)
  1123. {
  1124. CARLA_SAFE_ASSERT(portNameId.groupId == groupId);
  1125. portNameId.rename(portName, newName);
  1126. callback(ENGINE_CALLBACK_PATCHBAY_PORT_RENAMED, static_cast<uint>(groupId), portNameId.portId, 0, 0.0f, newName);
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. #endif
  1132. void handleJackShutdownCallback()
  1133. {
  1134. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  1135. {
  1136. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1137. if (plugin != nullptr)
  1138. {
  1139. if (CarlaEngineJackClient* const client = (CarlaEngineJackClient*)plugin->getEngineClient())
  1140. client->fClient = nullptr;
  1141. }
  1142. }
  1143. fClient = nullptr;
  1144. callback(ENGINE_CALLBACK_QUIT, 0, 0, 0, 0.0f, nullptr);
  1145. }
  1146. // -------------------------------------------------------------------
  1147. private:
  1148. jack_client_t* fClient;
  1149. jack_position_t fTransportPos;
  1150. jack_transport_state_t fTransportState;
  1151. bool fFreewheel;
  1152. // -------------------------------------------------------------------
  1153. #ifdef BUILD_BRIDGE
  1154. bool fIsRunning;
  1155. #else
  1156. enum RackPorts {
  1157. kRackPortAudioIn1 = 0,
  1158. kRackPortAudioIn2 = 1,
  1159. kRackPortAudioOut1 = 2,
  1160. kRackPortAudioOut2 = 3,
  1161. kRackPortEventIn = 4,
  1162. kRackPortEventOut = 5,
  1163. kRackPortCount = 6
  1164. };
  1165. jack_port_t* fRackPorts[kRackPortCount];
  1166. struct GroupNameToId {
  1167. int id;
  1168. char name[STR_MAX+1];
  1169. void clear() noexcept
  1170. {
  1171. id = -1;
  1172. name[0] = '\0';
  1173. }
  1174. void setData(const int i, const char n[]) noexcept
  1175. {
  1176. id = i;
  1177. rename(n);
  1178. }
  1179. void rename(const char n[]) noexcept
  1180. {
  1181. std::strncpy(name, n, STR_MAX);
  1182. name[STR_MAX] = '\0';
  1183. }
  1184. bool operator==(const GroupNameToId& groupNameId) const noexcept
  1185. {
  1186. if (groupNameId.id != id)
  1187. return false;
  1188. if (std::strcmp(groupNameId.name, name) != 0)
  1189. return false;
  1190. return true;
  1191. }
  1192. bool operator!=(const GroupNameToId& groupNameId) const noexcept
  1193. {
  1194. return !operator==(groupNameId);
  1195. }
  1196. };
  1197. struct PortNameToId {
  1198. int groupId;
  1199. int portId;
  1200. char name[STR_MAX+1];
  1201. char fullName[STR_MAX+1]; // unique
  1202. void clear() noexcept
  1203. {
  1204. groupId = -1;
  1205. portId = -1;
  1206. name[0] = '\0';
  1207. fullName[0] = '\0';
  1208. }
  1209. void setData(const int gId, const int pId, const char n[], const char fn[]) noexcept
  1210. {
  1211. groupId = gId;
  1212. portId = pId;
  1213. rename(n, fn);
  1214. }
  1215. void rename(const char n[], const char fn[]) noexcept
  1216. {
  1217. std::strncpy(name, n, STR_MAX);
  1218. name[STR_MAX] = '\0';
  1219. std::strncpy(fullName, fn, STR_MAX);
  1220. fullName[STR_MAX] = '\0';
  1221. }
  1222. bool operator==(const PortNameToId& portNameId) noexcept
  1223. {
  1224. if (portNameId.groupId != groupId)
  1225. return false;
  1226. if (portNameId.portId != portId)
  1227. return false;
  1228. if (std::strcmp(portNameId.name, name) != 0)
  1229. return false;
  1230. if (std::strcmp(portNameId.fullName, fullName) != 0)
  1231. return false;
  1232. return true;
  1233. }
  1234. bool operator!=(const PortNameToId& portNameId) noexcept
  1235. {
  1236. return !operator==(portNameId);
  1237. }
  1238. };
  1239. struct ConnectionToId {
  1240. uint id;
  1241. int portOut;
  1242. int portIn;
  1243. void clear() noexcept
  1244. {
  1245. id = 0;
  1246. portOut = -1;
  1247. portIn = -1;
  1248. }
  1249. void setData(const uint i, const int out, const int in) noexcept
  1250. {
  1251. id = i;
  1252. portOut = out;
  1253. portIn = in;
  1254. }
  1255. bool operator==(const ConnectionToId& connectionId) const noexcept
  1256. {
  1257. if (connectionId.id != id)
  1258. return false;
  1259. if (connectionId.portOut != portOut)
  1260. return false;
  1261. if (connectionId.portIn != portIn)
  1262. return false;
  1263. return true;
  1264. }
  1265. bool operator!=(const ConnectionToId& connectionId) const noexcept
  1266. {
  1267. return !operator==(connectionId);
  1268. }
  1269. };
  1270. int fLastGroupId;
  1271. int fLastPortId;
  1272. uint fLastConnectionId;
  1273. LinkedList<GroupNameToId> fUsedGroupNames;
  1274. LinkedList<PortNameToId> fUsedPortNames;
  1275. LinkedList<ConnectionToId> fUsedConnections;
  1276. LinkedList<int> fNewGroups;
  1277. int getGroupId(const char* const name)
  1278. {
  1279. CARLA_SAFE_ASSERT_RETURN(name != nullptr, -1);
  1280. for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroupNames.begin(); it.valid(); it.next())
  1281. {
  1282. const GroupNameToId& groupNameId(it.getValue());
  1283. if (std::strcmp(groupNameId.name, name) == 0)
  1284. return groupNameId.id;
  1285. }
  1286. return -1;
  1287. }
  1288. const char* getGroupName(const int groupId)
  1289. {
  1290. static const char fallback[1] = { '\0' };
  1291. CARLA_SAFE_ASSERT_RETURN(groupId >= 0, fallback);
  1292. for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroupNames.begin(); it.valid(); it.next())
  1293. {
  1294. const GroupNameToId& groupNameId(it.getValue());
  1295. if (groupNameId.id == groupId)
  1296. return groupNameId.name;
  1297. }
  1298. return fallback;
  1299. }
  1300. int getPortId(const char* const fullName)
  1301. {
  1302. CARLA_SAFE_ASSERT_RETURN(fullName != nullptr, -1);
  1303. for (LinkedList<PortNameToId>::Itenerator it = fUsedPortNames.begin(); it.valid(); it.next())
  1304. {
  1305. const PortNameToId& portNameId(it.getValue());
  1306. if (std::strcmp(portNameId.fullName, fullName) == 0)
  1307. return portNameId.portId;
  1308. }
  1309. return -1;
  1310. }
  1311. void getFullPortName(const int portId, char nameBuf[STR_MAX+1])
  1312. {
  1313. for (LinkedList<PortNameToId>::Itenerator it = fUsedPortNames.begin(); it.valid(); it.next())
  1314. {
  1315. const PortNameToId& portNameId(it.getValue());
  1316. if (portNameId.portId == portId)
  1317. {
  1318. std::strncpy(nameBuf, portNameId.fullName, STR_MAX);
  1319. nameBuf[STR_MAX] = '\0';
  1320. return;
  1321. }
  1322. }
  1323. nameBuf[0] = '\0';
  1324. }
  1325. bool findPluginIdAndIcon(const char* const clientName, int& pluginId, PatchbayIcon& icon)
  1326. {
  1327. carla_debug("CarlaEngineJack::findPluginIdAndIcon(\"%s\", ...)", clientName);
  1328. for (uint i=0; i < pData->curPluginCount; ++i)
  1329. {
  1330. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1331. if (plugin == nullptr || ! plugin->isEnabled())
  1332. continue;
  1333. const CarlaEngineJackClient* const engClient((const CarlaEngineJackClient*)plugin->getEngineClient());
  1334. CARLA_SAFE_ASSERT_CONTINUE(engClient != nullptr && engClient->fClient != nullptr);
  1335. const char* const thisClientName(jackbridge_get_client_name(engClient->fClient));
  1336. CARLA_SAFE_ASSERT_CONTINUE(thisClientName != nullptr && thisClientName[0] != '\0');
  1337. if (std::strcmp(clientName, thisClientName) == 0)
  1338. {
  1339. carla_stdout("CarlaEngineJack::findPluginIdAndIcon(\"%s\", ...) - found plugin, yes!!!", clientName);
  1340. pluginId = static_cast<int>(i);
  1341. icon = PATCHBAY_ICON_PLUGIN;
  1342. if (const char* const pluginIcon = plugin->getIconName())
  1343. {
  1344. if (std::strcmp(pluginIcon, "app") == 0 || std::strcmp(pluginIcon, "application") == 0)
  1345. icon = PATCHBAY_ICON_APPLICATION;
  1346. else if (std::strcmp(pluginIcon, "plugin") == 0)
  1347. icon = PATCHBAY_ICON_PLUGIN;
  1348. else if (std::strcmp(pluginIcon, "hardware") == 0)
  1349. icon = PATCHBAY_ICON_HARDWARE;
  1350. else if (std::strcmp(pluginIcon, "carla") == 0)
  1351. icon = PATCHBAY_ICON_CARLA;
  1352. else if (std::strcmp(pluginIcon, "distrho") == 0)
  1353. icon = PATCHBAY_ICON_DISTRHO;
  1354. else if (std::strcmp(pluginIcon, "file") == 0)
  1355. icon = PATCHBAY_ICON_FILE;
  1356. }
  1357. return true;
  1358. }
  1359. }
  1360. carla_stdout("CarlaEngineJack::findPluginIdAndIcon(\"%s\", ...) - nothing here...", clientName);
  1361. return false;
  1362. }
  1363. void initJackPatchbay(const char* const ourName)
  1364. {
  1365. CARLA_SAFE_ASSERT_RETURN(fLastGroupId == 0,);
  1366. CARLA_SAFE_ASSERT_RETURN(fLastPortId == 0,);
  1367. CARLA_SAFE_ASSERT_RETURN(fLastConnectionId == 0,);
  1368. CARLA_SAFE_ASSERT_RETURN(ourName != nullptr,);
  1369. // query initial jack ports
  1370. QStringList parsedGroups;
  1371. // our client
  1372. {
  1373. parsedGroups.append(QString(ourName));
  1374. GroupNameToId groupNameToId;
  1375. groupNameToId.setData(fLastGroupId++, ourName);
  1376. fUsedGroupNames.append(groupNameToId);
  1377. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, 0 /* our client */, PATCHBAY_ICON_CARLA, -1, 0.0f, ourName);
  1378. }
  1379. if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, 0))
  1380. {
  1381. for (int i=0; ports[i] != nullptr; ++i)
  1382. {
  1383. jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
  1384. const char* const portName(jackbridge_port_short_name(jackPort));
  1385. const char* const fullPortName(ports[i]);
  1386. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  1387. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr);
  1388. const int jackPortFlags(jackbridge_port_flags(jackPort));
  1389. int groupId = -1;
  1390. bool found;
  1391. CarlaString groupName(fullPortName);
  1392. groupName.truncate(groupName.rfind(portName, &found)-1);
  1393. CARLA_SAFE_ASSERT(found);
  1394. QString qGroupName((const char*)groupName);
  1395. if (parsedGroups.contains(qGroupName))
  1396. {
  1397. groupId = getGroupId(groupName);
  1398. CARLA_SAFE_ASSERT(groupId >= 0);
  1399. }
  1400. else
  1401. {
  1402. groupId = fLastGroupId++;
  1403. parsedGroups.append(qGroupName);
  1404. GroupNameToId groupNameToId;
  1405. groupNameToId.setData(groupId, groupName);
  1406. fUsedGroupNames.append(groupNameToId);
  1407. int pluginId = -1;
  1408. PatchbayIcon icon = (jackPortFlags & JackPortIsPhysical) ? PATCHBAY_ICON_HARDWARE : PATCHBAY_ICON_APPLICATION;
  1409. findPluginIdAndIcon(groupName.getBuffer(), pluginId, icon);
  1410. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, static_cast<uint>(groupId), icon, pluginId, 0.0f, groupName);
  1411. }
  1412. bool portIsInput = (jackPortFlags & JackPortIsInput);
  1413. bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
  1414. bool portIsCV = false;
  1415. if (jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  1416. {
  1417. char* value = nullptr;
  1418. char* type = nullptr;
  1419. if (jackbridge_get_property(uuid, "urn:jack:IsControlVoltage", &value, &type) && value != nullptr && type != nullptr && std::strcmp(type, "text/plain") == 0)
  1420. portIsCV = (std::strcmp(value, "YES") == 0);
  1421. }
  1422. unsigned int canvasPortFlags = 0x0;
  1423. canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
  1424. canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_TYPE_AUDIO : PATCHBAY_PORT_TYPE_MIDI;
  1425. if (portIsCV)
  1426. canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
  1427. PortNameToId portNameToId;
  1428. portNameToId.setData(groupId, fLastPortId++, portName, fullPortName);
  1429. fUsedPortNames.append(portNameToId);
  1430. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, static_cast<uint>(groupId), portNameToId.portId, static_cast<int>(canvasPortFlags), 0.0f, portName);
  1431. }
  1432. // query connections, after all ports are in place
  1433. for (int i=0; ports[i] != nullptr; ++i)
  1434. {
  1435. jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
  1436. const char* const fullPortName(ports[i]);
  1437. const int thisPortId(getPortId(fullPortName));
  1438. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  1439. CARLA_SAFE_ASSERT_CONTINUE(thisPortId != -1);
  1440. if (const char** connections = jackbridge_port_get_all_connections(fClient, jackPort))
  1441. {
  1442. for (int j=0; connections[j] != nullptr; ++j)
  1443. {
  1444. const int targetPortId(getPortId(connections[j]));
  1445. ConnectionToId connectionToId;
  1446. connectionToId.setData(fLastConnectionId++, thisPortId, targetPortId);
  1447. fUsedConnections.append(connectionToId);
  1448. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  1449. }
  1450. jackbridge_free(connections);
  1451. }
  1452. }
  1453. jackbridge_free(ports);
  1454. }
  1455. }
  1456. #endif
  1457. // -------------------------------------
  1458. void processPlugin(CarlaPlugin* const plugin, const uint32_t nframes)
  1459. {
  1460. const uint32_t inCount(plugin->getAudioInCount());
  1461. const uint32_t outCount(plugin->getAudioOutCount());
  1462. float* inBuffer[inCount];
  1463. float* outBuffer[outCount];
  1464. float inPeaks[2] = { 0.0f };
  1465. float outPeaks[2] = { 0.0f };
  1466. for (uint32_t i=0; i < inCount; ++i)
  1467. {
  1468. CarlaEngineAudioPort* const port(plugin->getAudioInPort(i));
  1469. inBuffer[i] = port->getBuffer();
  1470. }
  1471. for (uint32_t i=0; i < outCount; ++i)
  1472. {
  1473. CarlaEngineAudioPort* const port(plugin->getAudioOutPort(i));
  1474. outBuffer[i] = port->getBuffer();
  1475. }
  1476. for (uint32_t i=0; i < inCount && i < 2; ++i)
  1477. {
  1478. for (uint32_t j=0; j < nframes; ++j)
  1479. {
  1480. const float absV(std::abs(inBuffer[i][j]));
  1481. if (absV > inPeaks[i])
  1482. inPeaks[i] = absV;
  1483. }
  1484. }
  1485. plugin->process(inBuffer, outBuffer, nframes);
  1486. for (uint32_t i=0; i < outCount && i < 2; ++i)
  1487. {
  1488. for (uint32_t j=0; j < nframes; ++j)
  1489. {
  1490. const float absV(std::abs(outBuffer[i][j]));
  1491. if (absV > outPeaks[i])
  1492. outPeaks[i] = absV;
  1493. }
  1494. }
  1495. setPluginPeaks(plugin->getId(), inPeaks, outPeaks);
  1496. }
  1497. #if 0
  1498. void latencyPlugin(CarlaPlugin* const plugin, jack_latency_callback_mode_t mode)
  1499. {
  1500. //const uint32_t inCount(plugin->audioInCount());
  1501. //const uint32_t outCount(plugin->audioOutCount());
  1502. const uint32_t latency(plugin->getLatencyInFrames());
  1503. if (latency == 0)
  1504. return;
  1505. //jack_latency_range_t range;
  1506. // TODO
  1507. if (mode == JackCaptureLatency)
  1508. {
  1509. for (uint32_t i=0; i < inCount; ++i)
  1510. {
  1511. uint32_t aOutI = (i >= outCount) ? outCount : i;
  1512. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, i))->kPort;
  1513. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, aOutI))->kPort;
  1514. jackbridge_port_get_latency_range(portIn, mode, &range);
  1515. range.min += latency;
  1516. range.max += latency;
  1517. jackbridge_port_set_latency_range(portOut, mode, &range);
  1518. }
  1519. }
  1520. else
  1521. {
  1522. for (uint32_t i=0; i < outCount; ++i)
  1523. {
  1524. uint32_t aInI = (i >= inCount) ? inCount : i;
  1525. jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, aInI))->kPort;
  1526. jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, i))->kPort;
  1527. jackbridge_port_get_latency_range(portOut, mode, &range);
  1528. range.min += latency;
  1529. range.max += latency;
  1530. jackbridge_port_set_latency_range(portIn, mode, &range);
  1531. }
  1532. }
  1533. }
  1534. #endif
  1535. // -------------------------------------
  1536. #define handlePtr ((CarlaEngineJack*)arg)
  1537. static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  1538. {
  1539. handlePtr->handleJackBufferSizeCallback(newBufferSize);
  1540. return 0;
  1541. }
  1542. static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  1543. {
  1544. handlePtr->handleJackSampleRateCallback(newSampleRate);
  1545. return 0;
  1546. }
  1547. static void carla_jack_freewheel_callback(int starting, void* arg)
  1548. {
  1549. handlePtr->handleJackFreewheelCallback(bool(starting));
  1550. }
  1551. static int carla_jack_process_callback(jack_nframes_t nframes, void* arg)
  1552. {
  1553. handlePtr->handleJackProcessCallback(nframes);
  1554. return 0;
  1555. }
  1556. #if 0
  1557. static void carla_jack_latency_callback(jack_latency_callback_mode_t mode, void* arg)
  1558. {
  1559. handlePtr->handleJackLatencyCallback(mode);
  1560. }
  1561. #endif
  1562. #ifndef BUILD_BRIDGE
  1563. static void carla_jack_client_registration_callback(const char* name, int reg, void* arg)
  1564. {
  1565. handlePtr->handleJackClientRegistrationCallback(name, (reg != 0));
  1566. }
  1567. static void carla_jack_port_registration_callback(jack_port_id_t port, int reg, void* arg)
  1568. {
  1569. handlePtr->handleJackPortRegistrationCallback(port, (reg != 0));
  1570. }
  1571. static void carla_jack_port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  1572. {
  1573. handlePtr->handleJackPortConnectCallback(a, b, (connect != 0));
  1574. }
  1575. static int carla_jack_client_rename_callback(const char* oldName, const char* newName, void* arg)
  1576. {
  1577. handlePtr->handleJackClientRenameCallback(oldName, newName);
  1578. return 0;
  1579. }
  1580. static int carla_jack_port_rename_callback(jack_port_id_t port, const char* oldName, const char* newName, void* arg)
  1581. {
  1582. handlePtr->handleJackPortRenameCallback(port, oldName, newName);
  1583. return 0;
  1584. }
  1585. #endif
  1586. static void carla_jack_shutdown_callback(void* arg)
  1587. {
  1588. handlePtr->handleJackShutdownCallback();
  1589. }
  1590. #undef handlePtr
  1591. // -------------------------------------------------------------------
  1592. #ifndef BUILD_BRIDGE
  1593. static int carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg)
  1594. {
  1595. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  1596. if (plugin != nullptr && plugin->isEnabled())
  1597. {
  1598. CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
  1599. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,0);
  1600. if (plugin->tryLock(engine->fFreewheel))
  1601. {
  1602. plugin->initBuffers();
  1603. engine->saveTransportInfo();
  1604. engine->processPlugin(plugin, nframes);
  1605. plugin->unlock();
  1606. }
  1607. }
  1608. return 0;
  1609. }
  1610. # if 0
  1611. static void carla_jack_latency_callback_plugin(jack_latency_callback_mode_t mode, void* arg)
  1612. {
  1613. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  1614. if (plugin != nullptr && plugin->isEnabled())
  1615. {
  1616. CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
  1617. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1618. engine->latencyPlugin(plugin, mode);
  1619. }
  1620. }
  1621. # endif
  1622. #endif
  1623. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
  1624. };
  1625. // -----------------------------------------------------------------------
  1626. CarlaEngine* CarlaEngine::newJack()
  1627. {
  1628. carla_debug("CarlaEngine::newJack()");
  1629. return new CarlaEngineJack();
  1630. }
  1631. // -----------------------------------------------------------------------
  1632. CARLA_BACKEND_END_NAMESPACE