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.

CarlaEngineGraph.cpp 76KB

10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
7 years ago
10 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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 "CarlaEngineGraph.hpp"
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaJuceUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. using water::jmax;
  24. using water::jmin;
  25. using water::AudioProcessor;
  26. using water::MidiBuffer;
  27. using water::String;
  28. using water::StringArray;
  29. CARLA_BACKEND_START_NAMESPACE
  30. // -----------------------------------------------------------------------
  31. // Fallback data
  32. static const PortNameToId kPortNameToIdFallback = { 0, 0, { '\0' }, { '\0' } };
  33. static /* */ PortNameToId kPortNameToIdFallbackNC = { 0, 0, { '\0' }, { '\0' } };
  34. // -----------------------------------------------------------------------
  35. // External Graph stuff
  36. static inline
  37. uint getExternalGraphPortIdFromName(const char* const shortname) noexcept
  38. {
  39. if (std::strcmp(shortname, "AudioIn1") == 0 || std::strcmp(shortname, "audio-in1") == 0)
  40. return kExternalGraphCarlaPortAudioIn1;
  41. if (std::strcmp(shortname, "AudioIn2") == 0 || std::strcmp(shortname, "audio-in2") == 0)
  42. return kExternalGraphCarlaPortAudioIn2;
  43. if (std::strcmp(shortname, "AudioOut1") == 0 || std::strcmp(shortname, "audio-out1") == 0)
  44. return kExternalGraphCarlaPortAudioOut1;
  45. if (std::strcmp(shortname, "AudioOut2") == 0 || std::strcmp(shortname, "audio-out2") == 0)
  46. return kExternalGraphCarlaPortAudioOut2;
  47. if (std::strcmp(shortname, "MidiIn") == 0 || std::strcmp(shortname, "midi-in") == 0)
  48. return kExternalGraphCarlaPortMidiIn;
  49. if (std::strcmp(shortname, "MidiOut") == 0 || std::strcmp(shortname, "midi-out") == 0)
  50. return kExternalGraphCarlaPortMidiOut;
  51. carla_stderr("CarlaBackend::getExternalGraphPortIdFromName(%s) - invalid short name", shortname);
  52. return kExternalGraphCarlaPortNull;
  53. }
  54. static inline
  55. const char* getExternalGraphFullPortNameFromId(const /*RackGraphCarlaPortIds*/ uint portId)
  56. {
  57. switch (portId)
  58. {
  59. case kExternalGraphCarlaPortAudioIn1:
  60. return "Carla:AudioIn1";
  61. case kExternalGraphCarlaPortAudioIn2:
  62. return "Carla:AudioIn2";
  63. case kExternalGraphCarlaPortAudioOut1:
  64. return "Carla:AudioOut1";
  65. case kExternalGraphCarlaPortAudioOut2:
  66. return "Carla:AudioOut2";
  67. case kExternalGraphCarlaPortMidiIn:
  68. return "Carla:MidiIn";
  69. case kExternalGraphCarlaPortMidiOut:
  70. return "Carla:MidiOut";
  71. //case kExternalGraphCarlaPortNull:
  72. //case kExternalGraphCarlaPortMax:
  73. // break;
  74. }
  75. carla_stderr("CarlaBackend::getExternalGraphFullPortNameFromId(%i) - invalid port id", portId);
  76. return nullptr;
  77. }
  78. // -----------------------------------------------------------------------
  79. ExternalGraphPorts::ExternalGraphPorts() noexcept
  80. : ins(),
  81. outs() {}
  82. const char* ExternalGraphPorts::getName(const bool isInput, const uint portId) const noexcept
  83. {
  84. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
  85. {
  86. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  87. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  88. if (portNameToId.port == portId)
  89. return portNameToId.name;
  90. }
  91. return nullptr;
  92. }
  93. uint ExternalGraphPorts::getPortId(const bool isInput, const char portName[], bool* const ok) const noexcept
  94. {
  95. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
  96. {
  97. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  98. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  99. if (std::strncmp(portNameToId.name, portName, STR_MAX) == 0)
  100. {
  101. if (ok != nullptr)
  102. *ok = true;
  103. return portNameToId.port;
  104. }
  105. }
  106. if (ok != nullptr)
  107. *ok = false;
  108. return 0;
  109. }
  110. // -----------------------------------------------------------------------
  111. ExternalGraph::ExternalGraph(CarlaEngine* const engine) noexcept
  112. : connections(),
  113. audioPorts(),
  114. midiPorts(),
  115. retCon(),
  116. kEngine(engine) {}
  117. void ExternalGraph::clear() noexcept
  118. {
  119. connections.clear();
  120. audioPorts.ins.clear();
  121. audioPorts.outs.clear();
  122. midiPorts.ins.clear();
  123. midiPorts.outs.clear();
  124. }
  125. bool ExternalGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback) noexcept
  126. {
  127. uint otherGroup, otherPort, carlaPort;
  128. if (groupA == kExternalGraphGroupCarla)
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(groupB != kExternalGraphGroupCarla, false);
  131. carlaPort = portA;
  132. otherGroup = groupB;
  133. otherPort = portB;
  134. }
  135. else
  136. {
  137. CARLA_SAFE_ASSERT_RETURN(groupB == kExternalGraphGroupCarla, false);
  138. carlaPort = portB;
  139. otherGroup = groupA;
  140. otherPort = portA;
  141. }
  142. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  143. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  144. bool makeConnection = false;
  145. switch (carlaPort)
  146. {
  147. case kExternalGraphCarlaPortAudioIn1:
  148. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  149. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  150. break;
  151. case kExternalGraphCarlaPortAudioIn2:
  152. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  153. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  154. break;
  155. case kExternalGraphCarlaPortAudioOut1:
  156. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  157. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  158. break;
  159. case kExternalGraphCarlaPortAudioOut2:
  160. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  161. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  162. break;
  163. case kExternalGraphCarlaPortMidiIn:
  164. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiIn, false);
  165. if (const char* const portName = midiPorts.getName(true, otherPort))
  166. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  167. break;
  168. case kExternalGraphCarlaPortMidiOut:
  169. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiOut, false);
  170. if (const char* const portName = midiPorts.getName(false, otherPort))
  171. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  172. break;
  173. }
  174. if (! makeConnection)
  175. {
  176. kEngine->setLastError("Invalid rack connection");
  177. return false;
  178. }
  179. ConnectionToId connectionToId;
  180. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  181. char strBuf[STR_MAX+1];
  182. strBuf[STR_MAX] = '\0';
  183. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  184. if (sendCallback)
  185. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  186. connections.list.append(connectionToId);
  187. return true;
  188. }
  189. bool ExternalGraph::disconnect(const uint connectionId) noexcept
  190. {
  191. CARLA_SAFE_ASSERT_RETURN(connections.list.count() > 0, false);
  192. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  193. {
  194. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  195. const ConnectionToId& connectionToId(it.getValue(fallback));
  196. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  197. if (connectionToId.id != connectionId)
  198. continue;
  199. uint otherGroup, otherPort, carlaPort;
  200. if (connectionToId.groupA == kExternalGraphGroupCarla)
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB != kExternalGraphGroupCarla, false);
  203. carlaPort = connectionToId.portA;
  204. otherGroup = connectionToId.groupB;
  205. otherPort = connectionToId.portB;
  206. }
  207. else
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB == kExternalGraphGroupCarla, false);
  210. carlaPort = connectionToId.portB;
  211. otherGroup = connectionToId.groupA;
  212. otherPort = connectionToId.portA;
  213. }
  214. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  215. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  216. bool makeDisconnection = false;
  217. switch (carlaPort)
  218. {
  219. case kExternalGraphCarlaPortAudioIn1:
  220. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  221. break;
  222. case kExternalGraphCarlaPortAudioIn2:
  223. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  224. break;
  225. case kExternalGraphCarlaPortAudioOut1:
  226. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  227. break;
  228. case kExternalGraphCarlaPortAudioOut2:
  229. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  230. break;
  231. case kExternalGraphCarlaPortMidiIn:
  232. if (const char* const portName = midiPorts.getName(true, otherPort))
  233. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  234. break;
  235. case kExternalGraphCarlaPortMidiOut:
  236. if (const char* const portName = midiPorts.getName(false, otherPort))
  237. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  238. break;
  239. }
  240. if (! makeDisconnection)
  241. {
  242. kEngine->setLastError("Invalid rack connection");
  243. return false;
  244. }
  245. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  246. connections.list.remove(it);
  247. return true;
  248. }
  249. kEngine->setLastError("Failed to find connection");
  250. return false;
  251. }
  252. void ExternalGraph::refresh(const char* const deviceName)
  253. {
  254. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  255. const bool isRack(kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  256. // Main
  257. {
  258. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupCarla, PATCHBAY_ICON_CARLA, MAIN_CARLA_PLUGIN_ID, 0.0f, kEngine->getName());
  259. if (isRack)
  260. {
  261. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in1");
  262. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in2");
  263. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
  264. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
  265. }
  266. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiIn, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "midi-in");
  267. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiOut, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
  268. }
  269. char strBuf[STR_MAX+1];
  270. strBuf[STR_MAX] = '\0';
  271. if (isRack)
  272. {
  273. // Audio In
  274. if (deviceName[0] != '\0')
  275. std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName);
  276. else
  277. std::strncpy(strBuf, "Capture", STR_MAX);
  278. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  279. const CarlaString groupNameIn(strBuf);
  280. int h = 0;
  281. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.ins.begin2(); it.valid(); it.next())
  282. {
  283. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  284. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  285. portNameToId.setFullName(groupNameIn + portNameToId.name);
  286. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioIn, ++h,
  287. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, portNameToId.name);
  288. }
  289. // Audio Out
  290. if (deviceName[0] != '\0')
  291. std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName);
  292. else
  293. std::strncpy(strBuf, "Playback", STR_MAX);
  294. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  295. const CarlaString groupNameOut(strBuf);
  296. h = 0;
  297. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.outs.begin2(); it.valid(); it.next())
  298. {
  299. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  300. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  301. portNameToId.setFullName(groupNameOut + portNameToId.name);
  302. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioOut, ++h,
  303. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
  304. }
  305. }
  306. // MIDI In
  307. {
  308. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Readable MIDI ports");
  309. const CarlaString groupNamePlus("Readable MIDI ports:");
  310. int h = 0;
  311. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.ins.begin2(); it.valid(); it.next())
  312. {
  313. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  314. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  315. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  316. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiIn, ++h,
  317. PATCHBAY_PORT_TYPE_MIDI, 0.0f, portNameToId.name);
  318. }
  319. }
  320. // MIDI Out
  321. {
  322. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Writable MIDI ports");
  323. const CarlaString groupNamePlus("Writable MIDI ports:");
  324. int h = 0;
  325. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.outs.begin2(); it.valid(); it.next())
  326. {
  327. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  328. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  329. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  330. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiOut, ++h,
  331. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
  332. }
  333. }
  334. }
  335. const char* const* ExternalGraph::getConnections() const noexcept
  336. {
  337. if (connections.list.count() == 0)
  338. return nullptr;
  339. CarlaStringList connList;
  340. char strBuf[STR_MAX+1];
  341. strBuf[STR_MAX] = '\0';
  342. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  343. {
  344. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  345. const ConnectionToId& connectionToId(it.getValue(fallback));
  346. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  347. uint otherGroup, otherPort, carlaPort;
  348. if (connectionToId.groupA == kExternalGraphGroupCarla)
  349. {
  350. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB != kExternalGraphGroupCarla);
  351. carlaPort = connectionToId.portA;
  352. otherGroup = connectionToId.groupB;
  353. otherPort = connectionToId.portB;
  354. }
  355. else
  356. {
  357. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB == kExternalGraphGroupCarla);
  358. carlaPort = connectionToId.portB;
  359. otherGroup = connectionToId.groupA;
  360. otherPort = connectionToId.portA;
  361. }
  362. CARLA_SAFE_ASSERT_CONTINUE(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax);
  363. CARLA_SAFE_ASSERT_CONTINUE(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax);
  364. switch (carlaPort)
  365. {
  366. case kExternalGraphCarlaPortAudioIn1:
  367. case kExternalGraphCarlaPortAudioIn2:
  368. std::snprintf(strBuf, STR_MAX, "AudioIn:%s", audioPorts.getName(true, otherPort));
  369. connList.append(strBuf);
  370. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  371. break;
  372. case kExternalGraphCarlaPortAudioOut1:
  373. case kExternalGraphCarlaPortAudioOut2:
  374. std::snprintf(strBuf, STR_MAX, "AudioOut:%s", audioPorts.getName(false, otherPort));
  375. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  376. connList.append(strBuf);
  377. break;
  378. case kExternalGraphCarlaPortMidiIn:
  379. std::snprintf(strBuf, STR_MAX, "MidiIn:%s", midiPorts.getName(true, otherPort));
  380. connList.append(strBuf);
  381. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  382. break;
  383. case kExternalGraphCarlaPortMidiOut:
  384. std::snprintf(strBuf, STR_MAX, "MidiOut:%s", midiPorts.getName(false, otherPort));
  385. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  386. connList.append(strBuf);
  387. break;
  388. }
  389. }
  390. if (connList.count() == 0)
  391. return nullptr;
  392. retCon = connList.toCharStringListPtr();
  393. return retCon;
  394. }
  395. bool ExternalGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  396. {
  397. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', false);
  398. if (std::strncmp(fullPortName, "Carla:", 6) == 0)
  399. {
  400. groupId = kExternalGraphGroupCarla;
  401. portId = getExternalGraphPortIdFromName(fullPortName+6);
  402. if (portId > kExternalGraphCarlaPortNull && portId < kExternalGraphCarlaPortMax)
  403. return true;
  404. }
  405. else if (std::strncmp(fullPortName, "AudioIn:", 8) == 0)
  406. {
  407. groupId = kExternalGraphGroupAudioIn;
  408. if (const char* const portName = fullPortName+8)
  409. {
  410. bool ok;
  411. portId = audioPorts.getPortId(true, portName, &ok);
  412. return ok;
  413. }
  414. }
  415. else if (std::strncmp(fullPortName, "AudioOut:", 9) == 0)
  416. {
  417. groupId = kExternalGraphGroupAudioOut;
  418. if (const char* const portName = fullPortName+9)
  419. {
  420. bool ok;
  421. portId = audioPorts.getPortId(false, portName, &ok);
  422. return ok;
  423. }
  424. }
  425. else if (std::strncmp(fullPortName, "MidiIn:", 7) == 0)
  426. {
  427. groupId = kExternalGraphGroupMidiIn;
  428. if (const char* const portName = fullPortName+7)
  429. {
  430. bool ok;
  431. portId = midiPorts.getPortId(true, portName, &ok);
  432. return ok;
  433. }
  434. }
  435. else if (std::strncmp(fullPortName, "MidiOut:", 8) == 0)
  436. {
  437. groupId = kExternalGraphGroupMidiOut;
  438. if (const char* const portName = fullPortName+8)
  439. {
  440. bool ok;
  441. portId = midiPorts.getPortId(false, portName, &ok);
  442. return ok;
  443. }
  444. }
  445. return false;
  446. }
  447. // -----------------------------------------------------------------------
  448. // RackGraph Buffers
  449. RackGraph::Buffers::Buffers() noexcept
  450. : mutex(),
  451. connectedIn1(),
  452. connectedIn2(),
  453. connectedOut1(),
  454. connectedOut2(),
  455. #ifdef CARLA_PROPER_CPP11_SUPPORT
  456. inBuf{nullptr, nullptr},
  457. inBufTmp{nullptr, nullptr},
  458. outBuf{nullptr, nullptr},
  459. #endif
  460. unusedBuf(nullptr)
  461. {
  462. #ifndef CARLA_PROPER_CPP11_SUPPORT
  463. inBuf[0] = inBuf[1] = nullptr;
  464. inBufTmp[0] = inBufTmp[1] = nullptr;
  465. outBuf[0] = outBuf[1] = nullptr;
  466. #endif
  467. }
  468. RackGraph::Buffers::~Buffers() noexcept
  469. {
  470. const CarlaRecursiveMutexLocker cml(mutex);
  471. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  472. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  473. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  474. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  475. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  476. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  477. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  478. connectedIn1.clear();
  479. connectedIn2.clear();
  480. connectedOut1.clear();
  481. connectedOut2.clear();
  482. }
  483. void RackGraph::Buffers::setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept
  484. {
  485. const CarlaRecursiveMutexLocker cml(mutex);
  486. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  487. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  488. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  489. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  490. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  491. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  492. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  493. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  494. try {
  495. inBufTmp[0] = new float[bufferSize];
  496. inBufTmp[1] = new float[bufferSize];
  497. unusedBuf = new float[bufferSize];
  498. if (createBuffers)
  499. {
  500. inBuf[0] = new float[bufferSize];
  501. inBuf[1] = new float[bufferSize];
  502. outBuf[0] = new float[bufferSize];
  503. outBuf[1] = new float[bufferSize];
  504. }
  505. }
  506. catch(...) {
  507. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  508. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  509. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  510. if (createBuffers)
  511. {
  512. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  513. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  514. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  515. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  516. }
  517. return;
  518. }
  519. carla_zeroFloats(inBufTmp[0], bufferSize);
  520. carla_zeroFloats(inBufTmp[1], bufferSize);
  521. if (createBuffers)
  522. {
  523. carla_zeroFloats(inBuf[0], bufferSize);
  524. carla_zeroFloats(inBuf[1], bufferSize);
  525. carla_zeroFloats(outBuf[0], bufferSize);
  526. carla_zeroFloats(outBuf[1], bufferSize);
  527. }
  528. }
  529. // -----------------------------------------------------------------------
  530. // RackGraph
  531. RackGraph::RackGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs) noexcept
  532. : extGraph(engine),
  533. inputs(ins),
  534. outputs(outs),
  535. isOffline(false),
  536. audioBuffers(),
  537. kEngine(engine)
  538. {
  539. setBufferSize(engine->getBufferSize());
  540. }
  541. RackGraph::~RackGraph() noexcept
  542. {
  543. extGraph.clear();
  544. }
  545. void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
  546. {
  547. audioBuffers.setBufferSize(bufferSize, (inputs > 0 || outputs > 0));
  548. }
  549. void RackGraph::setOffline(const bool offline) noexcept
  550. {
  551. isOffline = offline;
  552. }
  553. bool RackGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  554. {
  555. return extGraph.connect(groupA, portA, groupB, portB, true);
  556. }
  557. bool RackGraph::disconnect(const uint connectionId) noexcept
  558. {
  559. return extGraph.disconnect(connectionId);
  560. }
  561. void RackGraph::refresh(const char* const deviceName)
  562. {
  563. extGraph.refresh(deviceName);
  564. char strBuf[STR_MAX+1];
  565. strBuf[STR_MAX] = '\0';
  566. // Connections
  567. const CarlaRecursiveMutexLocker cml(audioBuffers.mutex);
  568. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  569. {
  570. const uint& portId(it.getValue(0));
  571. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  572. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  573. ConnectionToId connectionToId;
  574. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1);
  575. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  576. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  577. extGraph.connections.list.append(connectionToId);
  578. }
  579. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  580. {
  581. const uint& portId(it.getValue(0));
  582. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  583. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  584. ConnectionToId connectionToId;
  585. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2);
  586. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  587. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  588. extGraph.connections.list.append(connectionToId);
  589. }
  590. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  591. {
  592. const uint& portId(it.getValue(0));
  593. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  594. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  595. ConnectionToId connectionToId;
  596. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, kExternalGraphGroupAudioOut, portId);
  597. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  598. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  599. extGraph.connections.list.append(connectionToId);
  600. }
  601. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  602. {
  603. const uint& portId(it.getValue(0));
  604. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  605. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  606. ConnectionToId connectionToId;
  607. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, kExternalGraphGroupAudioOut, portId);
  608. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  609. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  610. extGraph.connections.list.append(connectionToId);
  611. }
  612. }
  613. const char* const* RackGraph::getConnections() const noexcept
  614. {
  615. return extGraph.getConnections();
  616. }
  617. bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  618. {
  619. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  620. }
  621. void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBufReal[2], const uint32_t frames)
  622. {
  623. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  624. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  625. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  626. // safe copy
  627. float* const dummyBuf = audioBuffers.unusedBuf;
  628. float* const inBuf0 = audioBuffers.inBufTmp[0];
  629. float* const inBuf1 = audioBuffers.inBufTmp[1];
  630. // initialize audio inputs
  631. carla_copyFloats(inBuf0, inBufReal[0], frames);
  632. carla_copyFloats(inBuf1, inBufReal[1], frames);
  633. // initialize audio outputs (zero)
  634. carla_zeroFloats(outBufReal[0], frames);
  635. carla_zeroFloats(outBufReal[1], frames);
  636. // initialize event outputs (zero)
  637. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  638. uint32_t oldAudioInCount = 0;
  639. uint32_t oldAudioOutCount = 0;
  640. uint32_t oldMidiOutCount = 0;
  641. bool processed = false;
  642. // process plugins
  643. for (uint i=0; i < data->curPluginCount; ++i)
  644. {
  645. CarlaPlugin* const plugin = data->plugins[i].plugin;
  646. if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  647. continue;
  648. if (processed)
  649. {
  650. // initialize audio inputs (from previous outputs)
  651. carla_copyFloats(inBuf0, outBufReal[0], frames);
  652. carla_copyFloats(inBuf1, outBufReal[1], frames);
  653. // initialize audio outputs (zero)
  654. carla_zeroFloats(outBufReal[0], frames);
  655. carla_zeroFloats(outBufReal[1], frames);
  656. // if plugin has no midi out, add previous events
  657. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  658. {
  659. if (data->events.out[0].type != kEngineEventTypeNull)
  660. {
  661. // TODO: carefully add to input, sorted events
  662. //carla_stderr("TODO midi event mixing here %s", plugin->getName());
  663. }
  664. // else nothing needed
  665. }
  666. else
  667. {
  668. // initialize event inputs from previous outputs
  669. carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  670. // initialize event outputs (zero)
  671. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  672. }
  673. }
  674. oldAudioInCount = plugin->getAudioInCount();
  675. oldAudioOutCount = plugin->getAudioOutCount();
  676. oldMidiOutCount = plugin->getMidiOutCount();
  677. const uint32_t numInBufs = std::max(oldAudioInCount, 2U);
  678. const uint32_t numOutBufs = std::max(oldAudioOutCount, 2U);
  679. const float* inBuf[numInBufs];
  680. inBuf[0] = inBuf0;
  681. inBuf[1] = inBuf1;
  682. float* outBuf[numOutBufs];
  683. outBuf[0] = outBufReal[0];
  684. outBuf[1] = outBufReal[1];
  685. if (numInBufs > 2 || numOutBufs > 2)
  686. {
  687. carla_zeroFloats(dummyBuf, frames);
  688. for (uint32_t j=2; j<numInBufs; ++j)
  689. inBuf[j] = dummyBuf;
  690. for (uint32_t j=2; j<numOutBufs; ++j)
  691. outBuf[j] = dummyBuf;
  692. }
  693. // process
  694. plugin->initBuffers();
  695. plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
  696. plugin->unlock();
  697. // if plugin has no audio inputs, add input buffer
  698. if (oldAudioInCount == 0)
  699. {
  700. carla_addFloats(outBufReal[0], inBuf0, frames);
  701. carla_addFloats(outBufReal[1], inBuf1, frames);
  702. }
  703. // if plugin only has 1 output, copy it to the 2nd
  704. if (oldAudioOutCount == 1)
  705. {
  706. carla_copyFloats(outBufReal[1], outBufReal[0], frames);
  707. }
  708. // set peaks
  709. {
  710. EnginePluginData& pluginData(data->plugins[i]);
  711. if (oldAudioInCount > 0)
  712. {
  713. pluginData.peaks[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
  714. pluginData.peaks[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
  715. }
  716. else
  717. {
  718. pluginData.peaks[0] = 0.0f;
  719. pluginData.peaks[1] = 0.0f;
  720. }
  721. if (oldAudioOutCount > 0)
  722. {
  723. pluginData.peaks[2] = carla_findMaxNormalizedFloat(outBufReal[0], frames);
  724. pluginData.peaks[3] = carla_findMaxNormalizedFloat(outBufReal[1], frames);
  725. }
  726. else
  727. {
  728. pluginData.peaks[2] = 0.0f;
  729. pluginData.peaks[3] = 0.0f;
  730. }
  731. }
  732. processed = true;
  733. }
  734. }
  735. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  736. {
  737. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  738. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  739. if (inBuf != nullptr && inputs > 0)
  740. {
  741. bool noConnections = true;
  742. // connect input buffers
  743. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  744. {
  745. const uint& port(it.getValue(0));
  746. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  747. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  748. if (noConnections)
  749. {
  750. carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  751. noConnections = false;
  752. }
  753. else
  754. {
  755. carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  756. }
  757. }
  758. if (noConnections)
  759. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  760. noConnections = true;
  761. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  762. {
  763. const uint& port(it.getValue(0));
  764. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  765. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  766. if (noConnections)
  767. {
  768. carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  769. noConnections = false;
  770. }
  771. else
  772. {
  773. carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  774. }
  775. }
  776. if (noConnections)
  777. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  778. }
  779. else
  780. {
  781. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  782. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  783. }
  784. carla_zeroFloats(audioBuffers.outBuf[0], frames);
  785. carla_zeroFloats(audioBuffers.outBuf[1], frames);
  786. // process
  787. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  788. // connect output buffers
  789. if (audioBuffers.connectedOut1.count() != 0)
  790. {
  791. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  792. {
  793. const uint& port(it.getValue(0));
  794. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  795. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  796. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
  797. }
  798. }
  799. if (audioBuffers.connectedOut2.count() != 0)
  800. {
  801. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  802. {
  803. const uint& port(it.getValue(0));
  804. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  805. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  806. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
  807. }
  808. }
  809. }
  810. // -----------------------------------------------------------------------
  811. // Patchbay Graph stuff
  812. static const uint32_t kAudioInputPortOffset = MAX_PATCHBAY_PLUGINS*1;
  813. static const uint32_t kAudioOutputPortOffset = MAX_PATCHBAY_PLUGINS*2;
  814. static const uint32_t kMidiInputPortOffset = MAX_PATCHBAY_PLUGINS*3;
  815. static const uint32_t kMidiOutputPortOffset = MAX_PATCHBAY_PLUGINS*3+1;
  816. static const uint kMidiChannelIndex = static_cast<uint>(AudioProcessorGraph::midiChannelIndex);
  817. static inline
  818. bool adjustPatchbayPortIdForWater(uint& portId)
  819. {
  820. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  821. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, false);
  822. if (portId == kMidiInputPortOffset)
  823. {
  824. portId = kMidiChannelIndex;
  825. return true;
  826. }
  827. if (portId == kMidiOutputPortOffset)
  828. {
  829. portId = kMidiChannelIndex;
  830. return true;
  831. }
  832. if (portId >= kAudioOutputPortOffset)
  833. {
  834. portId -= kAudioOutputPortOffset;
  835. return true;
  836. }
  837. if (portId >= kAudioInputPortOffset)
  838. {
  839. portId -= kAudioInputPortOffset;
  840. return true;
  841. }
  842. return false;
  843. }
  844. static inline
  845. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  846. {
  847. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  848. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  849. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, String());
  850. String fullPortName(proc->getName());
  851. if (portId == kMidiOutputPortOffset)
  852. {
  853. fullPortName += ":events-out";
  854. }
  855. else if (portId == kMidiInputPortOffset)
  856. {
  857. fullPortName += ":events-in";
  858. }
  859. else if (portId >= kAudioOutputPortOffset)
  860. {
  861. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels() > 0, String());
  862. fullPortName += ":" + proc->getOutputChannelName(static_cast<int>(portId-kAudioOutputPortOffset));
  863. }
  864. else if (portId >= kAudioInputPortOffset)
  865. {
  866. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels() > 0, String());
  867. fullPortName += ":" + proc->getInputChannelName(static_cast<int>(portId-kAudioInputPortOffset));
  868. }
  869. else
  870. {
  871. return String();
  872. }
  873. return fullPortName;
  874. }
  875. static inline
  876. void addNodeToPatchbay(CarlaEngine* const engine, const uint32_t groupId, const int clientId, const AudioProcessor* const proc)
  877. {
  878. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  879. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  880. const int icon((clientId >= 0) ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE);
  881. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, groupId, icon, clientId, 0.0f, proc->getName().toRawUTF8());
  882. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  883. {
  884. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  885. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, proc->getInputChannelName(i).toRawUTF8());
  886. }
  887. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  888. {
  889. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  890. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, proc->getOutputChannelName(i).toRawUTF8());
  891. }
  892. if (proc->acceptsMidi())
  893. {
  894. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiInputPortOffset),
  895. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "events-in");
  896. }
  897. if (proc->producesMidi())
  898. {
  899. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiOutputPortOffset),
  900. PATCHBAY_PORT_TYPE_MIDI, 0.0f, "events-out");
  901. }
  902. }
  903. static inline
  904. void removeNodeFromPatchbay(CarlaEngine* const engine, const uint32_t groupId, const AudioProcessor* const proc)
  905. {
  906. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  907. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  908. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  909. {
  910. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  911. 0, 0.0f, nullptr);
  912. }
  913. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  914. {
  915. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  916. 0, 0.0f, nullptr);
  917. }
  918. if (proc->acceptsMidi())
  919. {
  920. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiInputPortOffset),
  921. 0, 0.0f, nullptr);
  922. }
  923. if (proc->producesMidi())
  924. {
  925. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiOutputPortOffset),
  926. 0, 0.0f, nullptr);
  927. }
  928. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, groupId, 0, 0, 0.0f, nullptr);
  929. }
  930. // -----------------------------------------------------------------------
  931. class CarlaPluginInstance : public AudioProcessor
  932. {
  933. public:
  934. CarlaPluginInstance(CarlaEngine* const engine, CarlaPlugin* const plugin)
  935. : kEngine(engine),
  936. fPlugin(plugin)
  937. {
  938. setPlayConfigDetails(static_cast<int>(fPlugin->getAudioInCount()),
  939. static_cast<int>(fPlugin->getAudioOutCount()),
  940. getSampleRate(), getBlockSize());
  941. }
  942. ~CarlaPluginInstance() override
  943. {
  944. }
  945. void invalidatePlugin() noexcept
  946. {
  947. fPlugin = nullptr;
  948. }
  949. // -------------------------------------------------------------------
  950. const String getName() const override
  951. {
  952. return fPlugin->getName();
  953. }
  954. void processBlock(AudioSampleBuffer& audio, MidiBuffer& midi) override
  955. {
  956. if (fPlugin == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  957. {
  958. audio.clear();
  959. midi.clear();
  960. return;
  961. }
  962. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  963. {
  964. EngineEvent* const engineEvents(port->fBuffer);
  965. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  966. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  967. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  968. }
  969. midi.clear();
  970. fPlugin->initBuffers();
  971. // TODO - CV support
  972. const uint32_t numSamples(static_cast<uint32_t>(audio.getNumSamples()));
  973. if (const uint32_t numChan = audio.getNumChannels())
  974. {
  975. const uint32_t numChanu = jmin(numChan, 2U);
  976. if (fPlugin->getAudioInCount() == 0)
  977. audio.clear();
  978. float* audioBuffers[numChan];
  979. for (uint32_t i=0; i<numChan; ++i)
  980. audioBuffers[i] = audio.getWritePointer(i);
  981. float inPeaks[2] = { 0.0f };
  982. float outPeaks[2] = { 0.0f };
  983. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChanu); i<count; ++i)
  984. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  985. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers, nullptr, nullptr, numSamples);
  986. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChanu); i<count; ++i)
  987. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  988. kEngine->setPluginPeaks(fPlugin->getId(), inPeaks, outPeaks);
  989. }
  990. else
  991. {
  992. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  993. }
  994. midi.clear();
  995. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  996. {
  997. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  998. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  999. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1000. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1001. }
  1002. fPlugin->unlock();
  1003. }
  1004. const String getInputChannelName(int i) const override
  1005. {
  1006. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1007. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1008. return client->getAudioPortName(true, static_cast<uint>(i));
  1009. }
  1010. const String getOutputChannelName(int i) const override
  1011. {
  1012. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1013. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1014. return client->getAudioPortName(false, static_cast<uint>(i));
  1015. }
  1016. void prepareToPlay(double, int) override {}
  1017. void releaseResources() override {}
  1018. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1019. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1020. private:
  1021. CarlaEngine* const kEngine;
  1022. CarlaPlugin* fPlugin;
  1023. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1024. };
  1025. // -----------------------------------------------------------------------
  1026. // Patchbay Graph
  1027. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1028. {
  1029. public:
  1030. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1031. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1032. inputNames(),
  1033. outputNames() {}
  1034. const String getInputChannelName (int index) const override
  1035. {
  1036. if (index < inputNames.size())
  1037. return inputNames[index];
  1038. return String("Playback ") + String(index+1);
  1039. }
  1040. const String getOutputChannelName (int index) const override
  1041. {
  1042. if (index < outputNames.size())
  1043. return outputNames[index];
  1044. return String("Capture ") + String(index+1);
  1045. }
  1046. void setNames(const bool setInputNames, const StringArray& names)
  1047. {
  1048. if (setInputNames)
  1049. inputNames = names;
  1050. else
  1051. outputNames = names;
  1052. }
  1053. private:
  1054. StringArray inputNames;
  1055. StringArray outputNames;
  1056. };
  1057. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs)
  1058. : CarlaThread("PatchbayReorderThread"),
  1059. connections(),
  1060. graph(),
  1061. audioBuffer(),
  1062. midiBuffer(),
  1063. inputs(carla_fixedValue(0U, 32U, ins)),
  1064. outputs(carla_fixedValue(0U, 32U, outs)),
  1065. retCon(),
  1066. usingExternal(false),
  1067. extGraph(engine),
  1068. kEngine(engine)
  1069. {
  1070. const uint32_t bufferSize(engine->getBufferSize());
  1071. const double sampleRate(engine->getSampleRate());
  1072. graph.setPlayConfigDetails(static_cast<int>(inputs), static_cast<int>(outputs), sampleRate, static_cast<int>(bufferSize));
  1073. graph.prepareToPlay(sampleRate, static_cast<int>(bufferSize));
  1074. audioBuffer.setSize(jmax(inputs, outputs), bufferSize);
  1075. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1076. midiBuffer.clear();
  1077. StringArray channelNames;
  1078. switch (inputs)
  1079. {
  1080. case 2:
  1081. channelNames.add("Left");
  1082. channelNames.add("Right");
  1083. break;
  1084. case 3:
  1085. channelNames.add("Left");
  1086. channelNames.add("Right");
  1087. channelNames.add("Sidechain");
  1088. break;
  1089. }
  1090. {
  1091. NamedAudioGraphIOProcessor* const proc(
  1092. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1093. proc->setNames(false, channelNames);
  1094. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1095. node->properties.set("isPlugin", false);
  1096. node->properties.set("isOutput", false);
  1097. node->properties.set("isAudio", true);
  1098. node->properties.set("isCV", false);
  1099. node->properties.set("isMIDI", false);
  1100. node->properties.set("isOSC", false);
  1101. }
  1102. {
  1103. NamedAudioGraphIOProcessor* const proc(
  1104. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1105. proc->setNames(true, channelNames);
  1106. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1107. node->properties.set("isPlugin", false);
  1108. node->properties.set("isOutput", false);
  1109. node->properties.set("isAudio", true);
  1110. node->properties.set("isCV", false);
  1111. node->properties.set("isMIDI", false);
  1112. node->properties.set("isOSC", false);
  1113. }
  1114. {
  1115. NamedAudioGraphIOProcessor* const proc(
  1116. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1117. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1118. node->properties.set("isPlugin", false);
  1119. node->properties.set("isOutput", false);
  1120. node->properties.set("isAudio", false);
  1121. node->properties.set("isCV", false);
  1122. node->properties.set("isMIDI", true);
  1123. node->properties.set("isOSC", false);
  1124. }
  1125. {
  1126. NamedAudioGraphIOProcessor* const proc(
  1127. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1128. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1129. node->properties.set("isPlugin", false);
  1130. node->properties.set("isOutput", true);
  1131. node->properties.set("isAudio", false);
  1132. node->properties.set("isCV", false);
  1133. node->properties.set("isMIDI", true);
  1134. node->properties.set("isOSC", false);
  1135. }
  1136. startThread();
  1137. }
  1138. PatchbayGraph::~PatchbayGraph()
  1139. {
  1140. stopThread(-1);
  1141. connections.clear();
  1142. extGraph.clear();
  1143. graph.releaseResources();
  1144. graph.clear();
  1145. audioBuffer.clear();
  1146. }
  1147. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1148. {
  1149. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1150. graph.releaseResources();
  1151. graph.prepareToPlay(kEngine->getSampleRate(), static_cast<int>(bufferSize));
  1152. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  1153. }
  1154. void PatchbayGraph::setSampleRate(const double sampleRate)
  1155. {
  1156. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1157. graph.releaseResources();
  1158. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1159. }
  1160. void PatchbayGraph::setOffline(const bool offline)
  1161. {
  1162. graph.setNonRealtime(offline);
  1163. }
  1164. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  1165. {
  1166. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1167. carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
  1168. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1169. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1170. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1171. plugin->setPatchbayNodeId(node->nodeId);
  1172. node->properties.set("isPlugin", true);
  1173. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1174. if (! usingExternal)
  1175. addNodeToPatchbay(plugin->getEngine(), node->nodeId, static_cast<int>(plugin->getId()), instance);
  1176. }
  1177. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1178. {
  1179. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  1180. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  1181. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1182. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1183. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1184. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1185. if (! usingExternal)
  1186. {
  1187. disconnectInternalGroup(oldNode->nodeId);
  1188. removeNodeFromPatchbay(kEngine, oldNode->nodeId, oldNode->getProcessor());
  1189. }
  1190. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1191. graph.removeNode(oldNode->nodeId);
  1192. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1193. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1194. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1195. newPlugin->setPatchbayNodeId(node->nodeId);
  1196. node->properties.set("isPlugin", true);
  1197. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1198. if (! usingExternal)
  1199. addNodeToPatchbay(newPlugin->getEngine(), node->nodeId, static_cast<int>(newPlugin->getId()), instance);
  1200. }
  1201. void PatchbayGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1202. {
  1203. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1204. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin, newName);
  1205. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1206. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1207. if (! usingExternal)
  1208. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED, node->nodeId, 0, 0, 0.0f, newName);
  1209. }
  1210. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  1211. {
  1212. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1213. carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
  1214. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1215. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1216. if (! usingExternal)
  1217. {
  1218. disconnectInternalGroup(node->nodeId);
  1219. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1220. }
  1221. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1222. // Fix plugin Ids properties
  1223. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1224. {
  1225. CarlaPlugin* const plugin2(kEngine->getPlugin(i));
  1226. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  1227. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1228. {
  1229. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1230. node2->properties.set("pluginId", static_cast<int>(i-1));
  1231. }
  1232. }
  1233. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1234. }
  1235. void PatchbayGraph::removeAllPlugins()
  1236. {
  1237. carla_debug("PatchbayGraph::removeAllPlugins()");
  1238. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1239. {
  1240. CarlaPlugin* const plugin(kEngine->getPlugin(i));
  1241. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1242. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1243. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1244. if (! usingExternal)
  1245. {
  1246. disconnectInternalGroup(node->nodeId);
  1247. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1248. }
  1249. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1250. graph.removeNode(node->nodeId);
  1251. }
  1252. }
  1253. bool PatchbayGraph::connect(const bool external, const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback)
  1254. {
  1255. if (external)
  1256. return extGraph.connect(groupA, portA, groupB, portB, sendCallback);
  1257. uint adjustedPortA = portA;
  1258. uint adjustedPortB = portB;
  1259. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1260. return false;
  1261. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1262. return false;
  1263. if (! graph.addConnection(groupA, static_cast<int>(adjustedPortA), groupB, static_cast<int>(adjustedPortB)))
  1264. {
  1265. kEngine->setLastError("Failed from water");
  1266. return false;
  1267. }
  1268. ConnectionToId connectionToId;
  1269. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1270. char strBuf[STR_MAX+1];
  1271. strBuf[STR_MAX] = '\0';
  1272. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1273. if (sendCallback)
  1274. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1275. connections.list.append(connectionToId);
  1276. return true;
  1277. }
  1278. bool PatchbayGraph::disconnect(const uint connectionId)
  1279. {
  1280. if (usingExternal)
  1281. return extGraph.disconnect(connectionId);
  1282. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1283. {
  1284. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1285. const ConnectionToId& connectionToId(it.getValue(fallback));
  1286. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1287. if (connectionToId.id != connectionId)
  1288. continue;
  1289. uint adjustedPortA = connectionToId.portA;
  1290. uint adjustedPortB = connectionToId.portB;
  1291. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1292. return false;
  1293. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1294. return false;
  1295. if (! graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1296. connectionToId.groupB, static_cast<int>(adjustedPortB)))
  1297. return false;
  1298. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1299. connections.list.remove(it);
  1300. return true;
  1301. }
  1302. kEngine->setLastError("Failed to find connection");
  1303. return false;
  1304. }
  1305. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1306. {
  1307. CARLA_SAFE_ASSERT(! usingExternal);
  1308. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1309. {
  1310. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1311. const ConnectionToId& connectionToId(it.getValue(fallback));
  1312. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1313. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1314. continue;
  1315. /*
  1316. uint adjustedPortA = connectionToId.portA;
  1317. uint adjustedPortB = connectionToId.portB;
  1318. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1319. return false;
  1320. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1321. return false;
  1322. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1323. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1324. */
  1325. if (! usingExternal)
  1326. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1327. connections.list.remove(it);
  1328. }
  1329. }
  1330. void PatchbayGraph::refresh(const char* const deviceName)
  1331. {
  1332. if (usingExternal)
  1333. return extGraph.refresh(deviceName);
  1334. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1335. connections.clear();
  1336. graph.removeIllegalConnections();
  1337. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1338. {
  1339. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1340. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1341. AudioProcessor* const proc(node->getProcessor());
  1342. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1343. int clientId = -1;
  1344. // plugin node
  1345. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1346. clientId = node->properties.getWithDefault("pluginId", -1);
  1347. addNodeToPatchbay(kEngine, node->nodeId, clientId, proc);
  1348. }
  1349. char strBuf[STR_MAX+1];
  1350. strBuf[STR_MAX] = '\0';
  1351. for (size_t i=0, count=graph.getNumConnections(); i<count; ++i)
  1352. {
  1353. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1354. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1355. CARLA_SAFE_ASSERT_CONTINUE(conn->sourceChannelIndex >= 0);
  1356. CARLA_SAFE_ASSERT_CONTINUE(conn->destChannelIndex >= 0);
  1357. const uint groupA = conn->sourceNodeId;
  1358. const uint groupB = conn->destNodeId;
  1359. uint portA = static_cast<uint>(conn->sourceChannelIndex);
  1360. uint portB = static_cast<uint>(conn->destChannelIndex);
  1361. if (portA == kMidiChannelIndex)
  1362. portA = kMidiOutputPortOffset;
  1363. else
  1364. portA += kAudioOutputPortOffset;
  1365. if (portB == kMidiChannelIndex)
  1366. portB = kMidiInputPortOffset;
  1367. else
  1368. portB += kAudioInputPortOffset;
  1369. ConnectionToId connectionToId;
  1370. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1371. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1372. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1373. connections.list.append(connectionToId);
  1374. }
  1375. }
  1376. const char* const* PatchbayGraph::getConnections(const bool external) const
  1377. {
  1378. if (external)
  1379. return extGraph.getConnections();
  1380. if (connections.list.count() == 0)
  1381. return nullptr;
  1382. CarlaStringList connList;
  1383. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1384. {
  1385. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1386. const ConnectionToId& connectionToId(it.getValue(fallback));
  1387. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1388. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1389. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1390. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1391. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1392. AudioProcessor* const procA(nodeA->getProcessor());
  1393. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1394. AudioProcessor* const procB(nodeB->getProcessor());
  1395. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1396. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1397. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1398. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1399. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1400. connList.append(fullPortNameA.toRawUTF8());
  1401. connList.append(fullPortNameB.toRawUTF8());
  1402. }
  1403. if (connList.count() == 0)
  1404. return nullptr;
  1405. retCon = connList.toCharStringListPtr();
  1406. return retCon;
  1407. }
  1408. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1409. {
  1410. if (external)
  1411. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1412. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1413. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1414. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1415. {
  1416. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1417. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1418. AudioProcessor* const proc(node->getProcessor());
  1419. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1420. if (proc->getName() != groupName)
  1421. continue;
  1422. groupId = node->nodeId;
  1423. if (portName == "events-in")
  1424. {
  1425. portId = kMidiInputPortOffset;
  1426. return true;
  1427. }
  1428. if (portName == "events-out")
  1429. {
  1430. portId = kMidiOutputPortOffset;
  1431. return true;
  1432. }
  1433. for (int j=0, numInputs=proc->getTotalNumInputChannels(); j<numInputs; ++j)
  1434. {
  1435. if (proc->getInputChannelName(j) != portName)
  1436. continue;
  1437. portId = kAudioInputPortOffset+static_cast<uint>(j);
  1438. return true;
  1439. }
  1440. for (int j=0, numOutputs=proc->getTotalNumOutputChannels(); j<numOutputs; ++j)
  1441. {
  1442. if (proc->getOutputChannelName(j) != portName)
  1443. continue;
  1444. portId = kAudioOutputPortOffset+static_cast<uint>(j);
  1445. return true;
  1446. }
  1447. }
  1448. return false;
  1449. }
  1450. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1451. {
  1452. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1453. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  1454. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  1455. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  1456. // put events in water buffer
  1457. {
  1458. midiBuffer.clear();
  1459. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  1460. }
  1461. // set audio buffer size, needed for water internals
  1462. if (! audioBuffer.setSizeRT(frames))
  1463. return;
  1464. // put carla audio in water buffer
  1465. {
  1466. uint32_t i=0;
  1467. for (; i < inputs; ++i)
  1468. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  1469. // clear remaining channels
  1470. for (const uint32_t count=audioBuffer.getNumChannels(); i<count; ++i)
  1471. audioBuffer.clear(i, 0, frames);
  1472. }
  1473. // ready to go!
  1474. graph.processBlock(audioBuffer, midiBuffer);
  1475. // put water audio in carla buffer
  1476. {
  1477. for (uint32_t i=0; i < outputs; ++i)
  1478. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  1479. }
  1480. // put water events in carla buffer
  1481. {
  1482. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  1483. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  1484. midiBuffer.clear();
  1485. }
  1486. }
  1487. void PatchbayGraph::run()
  1488. {
  1489. while (! shouldThreadExit())
  1490. {
  1491. carla_msleep(100);
  1492. graph.reorderNowIfNeeded();
  1493. }
  1494. }
  1495. // -----------------------------------------------------------------------
  1496. // InternalGraph
  1497. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  1498. : fIsRack(false),
  1499. fIsReady(false),
  1500. fRack(nullptr),
  1501. kEngine(engine) {}
  1502. EngineInternalGraph::~EngineInternalGraph() noexcept
  1503. {
  1504. CARLA_SAFE_ASSERT(! fIsReady);
  1505. CARLA_SAFE_ASSERT(fRack == nullptr);
  1506. }
  1507. void EngineInternalGraph::create(const uint32_t inputs, const uint32_t outputs)
  1508. {
  1509. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  1510. if (fIsRack)
  1511. {
  1512. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  1513. fRack = new RackGraph(kEngine, inputs, outputs);
  1514. }
  1515. else
  1516. {
  1517. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  1518. fPatchbay = new PatchbayGraph(kEngine, inputs, outputs);
  1519. }
  1520. fIsReady = true;
  1521. }
  1522. void EngineInternalGraph::destroy() noexcept
  1523. {
  1524. if (! fIsReady)
  1525. {
  1526. CARLA_SAFE_ASSERT(fRack == nullptr);
  1527. return;
  1528. }
  1529. if (fIsRack)
  1530. {
  1531. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1532. delete fRack;
  1533. fRack = nullptr;
  1534. }
  1535. else
  1536. {
  1537. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1538. delete fPatchbay;
  1539. fPatchbay = nullptr;
  1540. }
  1541. fIsReady = false;
  1542. }
  1543. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  1544. {
  1545. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1546. if (fIsRack)
  1547. {
  1548. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1549. fRack->setBufferSize(bufferSize);
  1550. }
  1551. else
  1552. {
  1553. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1554. fPatchbay->setBufferSize(bufferSize);
  1555. }
  1556. }
  1557. void EngineInternalGraph::setSampleRate(const double sampleRate)
  1558. {
  1559. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1560. if (fIsRack)
  1561. {
  1562. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1563. }
  1564. else
  1565. {
  1566. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1567. fPatchbay->setSampleRate(sampleRate);
  1568. }
  1569. }
  1570. void EngineInternalGraph::setOffline(const bool offline)
  1571. {
  1572. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1573. if (fIsRack)
  1574. {
  1575. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1576. fRack->setOffline(offline);
  1577. }
  1578. else
  1579. {
  1580. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1581. fPatchbay->setOffline(offline);
  1582. }
  1583. }
  1584. bool EngineInternalGraph::isReady() const noexcept
  1585. {
  1586. return fIsReady;
  1587. }
  1588. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  1589. {
  1590. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  1591. return fRack;
  1592. }
  1593. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  1594. {
  1595. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  1596. return fPatchbay;
  1597. }
  1598. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1599. {
  1600. if (fIsRack)
  1601. {
  1602. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1603. fRack->processHelper(data, inBuf, outBuf, frames);
  1604. }
  1605. else
  1606. {
  1607. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1608. fPatchbay->process(data, inBuf, outBuf, frames);
  1609. }
  1610. }
  1611. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  1612. {
  1613. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  1614. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1615. fRack->process(data, inBuf, outBuf, frames);
  1616. }
  1617. // -----------------------------------------------------------------------
  1618. // used for internal patchbay mode
  1619. void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
  1620. {
  1621. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1622. fPatchbay->addPlugin(plugin);
  1623. }
  1624. void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1625. {
  1626. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1627. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  1628. }
  1629. void EngineInternalGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1630. {
  1631. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1632. fPatchbay->renamePlugin(plugin, newName);
  1633. }
  1634. void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
  1635. {
  1636. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1637. fPatchbay->removePlugin(plugin);
  1638. }
  1639. void EngineInternalGraph::removeAllPlugins()
  1640. {
  1641. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1642. fPatchbay->removeAllPlugins();
  1643. }
  1644. bool EngineInternalGraph::isUsingExternal() const noexcept
  1645. {
  1646. if (fIsRack)
  1647. return true;
  1648. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  1649. return fPatchbay->usingExternal;
  1650. }
  1651. void EngineInternalGraph::setUsingExternal(const bool usingExternal) noexcept
  1652. {
  1653. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  1654. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1655. fPatchbay->usingExternal = usingExternal;
  1656. }
  1657. // -----------------------------------------------------------------------
  1658. // CarlaEngine Patchbay stuff
  1659. bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
  1660. {
  1661. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1662. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1663. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  1664. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1665. {
  1666. RackGraph* const graph = pData->graph.getRackGraph();
  1667. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1668. return graph->connect(groupA, portA, groupB, portB);
  1669. }
  1670. else
  1671. {
  1672. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1673. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1674. return graph->connect(graph->usingExternal, groupA, portA, groupB, portB, true);
  1675. }
  1676. return false;
  1677. }
  1678. bool CarlaEngine::patchbayDisconnect(const uint connectionId)
  1679. {
  1680. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1681. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1682. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  1683. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1684. {
  1685. RackGraph* const graph = pData->graph.getRackGraph();
  1686. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1687. return graph->disconnect(connectionId);
  1688. }
  1689. else
  1690. {
  1691. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1692. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1693. return graph->disconnect(connectionId);
  1694. }
  1695. return false;
  1696. }
  1697. bool CarlaEngine::patchbayRefresh(const bool external)
  1698. {
  1699. // subclasses should handle this
  1700. CARLA_SAFE_ASSERT_RETURN(! external, false);
  1701. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1702. {
  1703. // This is implemented in engine subclasses
  1704. setLastError("Unsupported operation");
  1705. return false;
  1706. }
  1707. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1708. {
  1709. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1710. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1711. graph->refresh("");
  1712. return true;
  1713. }
  1714. setLastError("Unsupported operation");
  1715. return false;
  1716. }
  1717. // -----------------------------------------------------------------------
  1718. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  1719. {
  1720. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  1721. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  1722. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1723. {
  1724. RackGraph* const graph = pData->graph.getRackGraph();
  1725. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1726. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  1727. return graph->getConnections();
  1728. }
  1729. else
  1730. {
  1731. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1732. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1733. return graph->getConnections(external);
  1734. }
  1735. return nullptr;
  1736. }
  1737. void CarlaEngine::restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback)
  1738. {
  1739. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  1740. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  1741. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  1742. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  1743. uint groupA, portA;
  1744. uint groupB, portB;
  1745. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1746. {
  1747. RackGraph* const graph = pData->graph.getRackGraph();
  1748. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1749. CARLA_SAFE_ASSERT_RETURN(external,);
  1750. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  1751. return;
  1752. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  1753. return;
  1754. graph->connect(groupA, portA, groupB, portB);
  1755. }
  1756. else
  1757. {
  1758. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1759. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1760. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  1761. return;
  1762. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  1763. return;
  1764. graph->connect(external, groupA, portA, groupB, portB, sendCallback);
  1765. }
  1766. }
  1767. // -----------------------------------------------------------------------
  1768. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1769. {
  1770. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1771. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1772. RackGraph* const graph(pData->graph.getRackGraph());
  1773. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1774. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1775. switch (connectionType)
  1776. {
  1777. case kExternalGraphConnectionAudioIn1:
  1778. return graph->audioBuffers.connectedIn1.append(portId);
  1779. case kExternalGraphConnectionAudioIn2:
  1780. return graph->audioBuffers.connectedIn2.append(portId);
  1781. case kExternalGraphConnectionAudioOut1:
  1782. return graph->audioBuffers.connectedOut1.append(portId);
  1783. case kExternalGraphConnectionAudioOut2:
  1784. return graph->audioBuffers.connectedOut2.append(portId);
  1785. }
  1786. return false;
  1787. }
  1788. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1789. {
  1790. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1791. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1792. RackGraph* const graph(pData->graph.getRackGraph());
  1793. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1794. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1795. switch (connectionType)
  1796. {
  1797. case kExternalGraphConnectionAudioIn1:
  1798. return graph->audioBuffers.connectedIn1.removeOne(portId);
  1799. case kExternalGraphConnectionAudioIn2:
  1800. return graph->audioBuffers.connectedIn2.removeOne(portId);
  1801. case kExternalGraphConnectionAudioOut1:
  1802. return graph->audioBuffers.connectedOut1.removeOne(portId);
  1803. case kExternalGraphConnectionAudioOut2:
  1804. return graph->audioBuffers.connectedOut2.removeOne(portId);
  1805. }
  1806. return false;
  1807. }
  1808. // -----------------------------------------------------------------------
  1809. CARLA_BACKEND_END_NAMESPACE