Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CarlaEngineJack.cpp 71KB

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