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

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