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

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