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

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