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