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

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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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. #else
  460. {
  461. inBuf[0] = inBuf[1] = nullptr;
  462. inBufTmp[0] = inBufTmp[1] = nullptr;
  463. outBuf[0] = outBuf[1] = nullptr;
  464. }
  465. #endif
  466. RackGraph::Buffers::~Buffers() noexcept
  467. {
  468. const CarlaRecursiveMutexLocker cml(mutex);
  469. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  470. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  471. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  472. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  473. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  474. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  475. connectedIn1.clear();
  476. connectedIn2.clear();
  477. connectedOut1.clear();
  478. connectedOut2.clear();
  479. }
  480. void RackGraph::Buffers::setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept
  481. {
  482. const CarlaRecursiveMutexLocker cml(mutex);
  483. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  484. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  485. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  486. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  487. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  488. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  489. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  490. try {
  491. inBufTmp[0] = new float[bufferSize];
  492. inBufTmp[1] = new float[bufferSize];
  493. if (createBuffers)
  494. {
  495. inBuf[0] = new float[bufferSize];
  496. inBuf[1] = new float[bufferSize];
  497. outBuf[0] = new float[bufferSize];
  498. outBuf[1] = new float[bufferSize];
  499. }
  500. }
  501. catch(...) {
  502. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  503. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  504. if (createBuffers)
  505. {
  506. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  507. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  508. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  509. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  510. }
  511. return;
  512. }
  513. carla_zeroFloats(inBufTmp[0], bufferSize);
  514. carla_zeroFloats(inBufTmp[1], bufferSize);
  515. if (createBuffers)
  516. {
  517. carla_zeroFloats(inBuf[0], bufferSize);
  518. carla_zeroFloats(inBuf[1], bufferSize);
  519. carla_zeroFloats(outBuf[0], bufferSize);
  520. carla_zeroFloats(outBuf[1], bufferSize);
  521. }
  522. }
  523. // -----------------------------------------------------------------------
  524. // RackGraph
  525. RackGraph::RackGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs) noexcept
  526. : extGraph(engine),
  527. inputs(ins),
  528. outputs(outs),
  529. isOffline(false),
  530. audioBuffers(),
  531. kEngine(engine)
  532. {
  533. setBufferSize(engine->getBufferSize());
  534. }
  535. RackGraph::~RackGraph() noexcept
  536. {
  537. extGraph.clear();
  538. }
  539. void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
  540. {
  541. audioBuffers.setBufferSize(bufferSize, (inputs > 0 || outputs > 0));
  542. }
  543. void RackGraph::setOffline(const bool offline) noexcept
  544. {
  545. isOffline = offline;
  546. }
  547. bool RackGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  548. {
  549. return extGraph.connect(groupA, portA, groupB, portB, true);
  550. }
  551. bool RackGraph::disconnect(const uint connectionId) noexcept
  552. {
  553. return extGraph.disconnect(connectionId);
  554. }
  555. void RackGraph::refresh(const char* const deviceName)
  556. {
  557. extGraph.refresh(deviceName);
  558. char strBuf[STR_MAX+1];
  559. strBuf[STR_MAX] = '\0';
  560. // Connections
  561. const CarlaRecursiveMutexLocker cml(audioBuffers.mutex);
  562. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  563. {
  564. const uint& portId(it.getValue(0));
  565. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  566. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  567. ConnectionToId connectionToId;
  568. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1);
  569. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  570. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  571. extGraph.connections.list.append(connectionToId);
  572. }
  573. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  574. {
  575. const uint& portId(it.getValue(0));
  576. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  577. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  578. ConnectionToId connectionToId;
  579. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2);
  580. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  581. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  582. extGraph.connections.list.append(connectionToId);
  583. }
  584. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  585. {
  586. const uint& portId(it.getValue(0));
  587. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  588. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  589. ConnectionToId connectionToId;
  590. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, kExternalGraphGroupAudioOut, portId);
  591. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  592. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  593. extGraph.connections.list.append(connectionToId);
  594. }
  595. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  596. {
  597. const uint& portId(it.getValue(0));
  598. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  599. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  600. ConnectionToId connectionToId;
  601. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, kExternalGraphGroupAudioOut, portId);
  602. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  603. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  604. extGraph.connections.list.append(connectionToId);
  605. }
  606. }
  607. const char* const* RackGraph::getConnections() const noexcept
  608. {
  609. return extGraph.getConnections();
  610. }
  611. bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  612. {
  613. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  614. }
  615. void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBuf[2], const uint32_t frames)
  616. {
  617. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  618. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  619. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  620. // safe copy
  621. float inBuf0[frames];
  622. float inBuf1[frames];
  623. const float* inBuf[2] = { inBuf0, inBuf1 };
  624. // initialize audio inputs
  625. carla_copyFloats(inBuf0, inBufReal[0], frames);
  626. carla_copyFloats(inBuf1, inBufReal[1], frames);
  627. // initialize audio outputs (zero)
  628. carla_zeroFloats(outBuf[0], frames);
  629. carla_zeroFloats(outBuf[1], frames);
  630. // initialize event outputs (zero)
  631. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  632. uint32_t oldAudioInCount = 0;
  633. uint32_t oldAudioOutCount = 0;
  634. uint32_t oldMidiOutCount = 0;
  635. bool processed = false;
  636. // process plugins
  637. for (uint i=0; i < data->curPluginCount; ++i)
  638. {
  639. CarlaPlugin* const plugin = data->plugins[i].plugin;
  640. if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  641. continue;
  642. if (processed)
  643. {
  644. // initialize audio inputs (from previous outputs)
  645. carla_copyFloats(inBuf0, outBuf[0], frames);
  646. carla_copyFloats(inBuf1, outBuf[1], frames);
  647. // initialize audio outputs (zero)
  648. carla_zeroFloats(outBuf[0], frames);
  649. carla_zeroFloats(outBuf[1], frames);
  650. // if plugin has no midi out, add previous events
  651. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  652. {
  653. if (data->events.out[0].type != kEngineEventTypeNull)
  654. {
  655. // TODO: carefully add to input, sorted events
  656. }
  657. // else nothing needed
  658. }
  659. else
  660. {
  661. // initialize event inputs from previous outputs
  662. carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  663. // initialize event outputs (zero)
  664. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  665. }
  666. }
  667. oldAudioInCount = plugin->getAudioInCount();
  668. oldAudioOutCount = plugin->getAudioOutCount();
  669. oldMidiOutCount = plugin->getMidiOutCount();
  670. // process
  671. plugin->initBuffers();
  672. plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
  673. plugin->unlock();
  674. // if plugin has no audio inputs, add input buffer
  675. if (oldAudioInCount == 0)
  676. {
  677. carla_addFloats(outBuf[0], inBuf0, frames);
  678. carla_addFloats(outBuf[1], inBuf1, frames);
  679. }
  680. // if plugin only has 1 output, copy it to the 2nd
  681. if (oldAudioOutCount == 1)
  682. {
  683. carla_copyFloats(outBuf[1], outBuf[0], frames);
  684. }
  685. // set peaks
  686. {
  687. EnginePluginData& pluginData(data->plugins[i]);
  688. if (oldAudioInCount > 0)
  689. {
  690. pluginData.insPeak[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
  691. pluginData.insPeak[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
  692. }
  693. else
  694. {
  695. pluginData.insPeak[0] = 0.0f;
  696. pluginData.insPeak[1] = 0.0f;
  697. }
  698. if (oldAudioOutCount > 0)
  699. {
  700. pluginData.outsPeak[0] = carla_findMaxNormalizedFloat(outBuf[0], frames);
  701. pluginData.outsPeak[1] = carla_findMaxNormalizedFloat(outBuf[1], frames);
  702. }
  703. else
  704. {
  705. pluginData.outsPeak[0] = 0.0f;
  706. pluginData.outsPeak[1] = 0.0f;
  707. }
  708. }
  709. processed = true;
  710. }
  711. }
  712. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  713. {
  714. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  715. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  716. if (inBuf != nullptr && inputs > 0)
  717. {
  718. bool noConnections = true;
  719. // connect input buffers
  720. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  721. {
  722. const uint& port(it.getValue(0));
  723. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  724. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  725. if (noConnections)
  726. {
  727. carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  728. noConnections = false;
  729. }
  730. else
  731. {
  732. carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  733. }
  734. }
  735. if (noConnections)
  736. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  737. noConnections = true;
  738. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  739. {
  740. const uint& port(it.getValue(0));
  741. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  742. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  743. if (noConnections)
  744. {
  745. carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  746. noConnections = false;
  747. }
  748. else
  749. {
  750. carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  751. }
  752. }
  753. if (noConnections)
  754. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  755. }
  756. else
  757. {
  758. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  759. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  760. }
  761. carla_zeroFloats(audioBuffers.outBuf[0], frames);
  762. carla_zeroFloats(audioBuffers.outBuf[1], frames);
  763. // process
  764. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  765. // connect output buffers
  766. if (audioBuffers.connectedOut1.count() != 0)
  767. {
  768. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  769. {
  770. const uint& port(it.getValue(0));
  771. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  772. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  773. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
  774. }
  775. }
  776. if (audioBuffers.connectedOut2.count() != 0)
  777. {
  778. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  779. {
  780. const uint& port(it.getValue(0));
  781. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  782. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  783. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
  784. }
  785. }
  786. }
  787. // -----------------------------------------------------------------------
  788. // Patchbay Graph stuff
  789. static const uint32_t kAudioInputPortOffset = MAX_PATCHBAY_PLUGINS*1;
  790. static const uint32_t kAudioOutputPortOffset = MAX_PATCHBAY_PLUGINS*2;
  791. static const uint32_t kMidiInputPortOffset = MAX_PATCHBAY_PLUGINS*3;
  792. static const uint32_t kMidiOutputPortOffset = MAX_PATCHBAY_PLUGINS*3+1;
  793. static const uint kMidiChannelIndex = static_cast<uint>(AudioProcessorGraph::midiChannelIndex);
  794. static inline
  795. bool adjustPatchbayPortIdForWater(uint& portId)
  796. {
  797. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  798. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, false);
  799. if (portId == kMidiInputPortOffset)
  800. {
  801. portId = kMidiChannelIndex;
  802. return true;
  803. }
  804. if (portId == kMidiOutputPortOffset)
  805. {
  806. portId = kMidiChannelIndex;
  807. return true;
  808. }
  809. if (portId >= kAudioOutputPortOffset)
  810. {
  811. portId -= kAudioOutputPortOffset;
  812. return true;
  813. }
  814. if (portId >= kAudioInputPortOffset)
  815. {
  816. portId -= kAudioInputPortOffset;
  817. return true;
  818. }
  819. return false;
  820. }
  821. static inline
  822. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  823. {
  824. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  825. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  826. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, String());
  827. String fullPortName(proc->getName());
  828. if (portId == kMidiOutputPortOffset)
  829. {
  830. fullPortName += ":events-out";
  831. }
  832. else if (portId == kMidiInputPortOffset)
  833. {
  834. fullPortName += ":events-in";
  835. }
  836. else if (portId >= kAudioOutputPortOffset)
  837. {
  838. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels() > 0, String());
  839. fullPortName += ":" + proc->getOutputChannelName(static_cast<int>(portId-kAudioOutputPortOffset));
  840. }
  841. else if (portId >= kAudioInputPortOffset)
  842. {
  843. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels() > 0, String());
  844. fullPortName += ":" + proc->getInputChannelName(static_cast<int>(portId-kAudioInputPortOffset));
  845. }
  846. else
  847. {
  848. return String();
  849. }
  850. return fullPortName;
  851. }
  852. static inline
  853. void addNodeToPatchbay(CarlaEngine* const engine, const uint32_t groupId, const int clientId, const AudioProcessor* const proc)
  854. {
  855. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  856. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  857. const int icon((clientId >= 0) ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE);
  858. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, groupId, icon, clientId, 0.0f, proc->getName().toRawUTF8());
  859. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  860. {
  861. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  862. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, proc->getInputChannelName(i).toRawUTF8());
  863. }
  864. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  865. {
  866. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  867. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, proc->getOutputChannelName(i).toRawUTF8());
  868. }
  869. if (proc->acceptsMidi())
  870. {
  871. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiInputPortOffset),
  872. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "events-in");
  873. }
  874. if (proc->producesMidi())
  875. {
  876. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiOutputPortOffset),
  877. PATCHBAY_PORT_TYPE_MIDI, 0.0f, "events-out");
  878. }
  879. }
  880. static inline
  881. void removeNodeFromPatchbay(CarlaEngine* const engine, const uint32_t groupId, const AudioProcessor* const proc)
  882. {
  883. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  884. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  885. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  886. {
  887. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  888. 0, 0.0f, nullptr);
  889. }
  890. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  891. {
  892. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  893. 0, 0.0f, nullptr);
  894. }
  895. if (proc->acceptsMidi())
  896. {
  897. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiInputPortOffset),
  898. 0, 0.0f, nullptr);
  899. }
  900. if (proc->producesMidi())
  901. {
  902. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiOutputPortOffset),
  903. 0, 0.0f, nullptr);
  904. }
  905. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, groupId, 0, 0, 0.0f, nullptr);
  906. }
  907. // -----------------------------------------------------------------------
  908. class CarlaPluginInstance : public AudioProcessor
  909. {
  910. public:
  911. CarlaPluginInstance(CarlaEngine* const engine, CarlaPlugin* const plugin)
  912. : kEngine(engine),
  913. fPlugin(plugin)
  914. {
  915. setPlayConfigDetails(static_cast<int>(fPlugin->getAudioInCount()),
  916. static_cast<int>(fPlugin->getAudioOutCount()),
  917. getSampleRate(), getBlockSize());
  918. }
  919. ~CarlaPluginInstance() override
  920. {
  921. }
  922. void invalidatePlugin() noexcept
  923. {
  924. fPlugin = nullptr;
  925. }
  926. // -------------------------------------------------------------------
  927. const String getName() const override
  928. {
  929. return fPlugin->getName();
  930. }
  931. void processBlock(AudioSampleBuffer& audio, MidiBuffer& midi) override
  932. {
  933. if (fPlugin == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  934. {
  935. audio.clear();
  936. midi.clear();
  937. return;
  938. }
  939. fPlugin->initBuffers();
  940. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  941. {
  942. EngineEvent* const engineEvents(port->fBuffer);
  943. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  944. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  945. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  946. }
  947. midi.clear();
  948. // TODO - CV support
  949. const uint32_t numSamples(static_cast<uint32_t>(audio.getNumSamples()));
  950. if (const int numChan = audio.getNumChannels())
  951. {
  952. const uint numChanu = static_cast<uint>(jmin(numChan, 2));
  953. if (fPlugin->getAudioInCount() == 0)
  954. audio.clear();
  955. float* audioBuffers[numChan];
  956. for (int i=0; i<numChan; ++i)
  957. audioBuffers[i] = audio.getWritePointer(i);
  958. float inPeaks[2] = { 0.0f };
  959. float outPeaks[2] = { 0.0f };
  960. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChanu); i<count; ++i)
  961. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  962. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers, nullptr, nullptr, numSamples);
  963. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChanu); i<count; ++i)
  964. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  965. kEngine->setPluginPeaks(fPlugin->getId(), inPeaks, outPeaks);
  966. }
  967. else
  968. {
  969. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  970. }
  971. midi.clear();
  972. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  973. {
  974. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  975. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  976. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  977. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  978. }
  979. fPlugin->unlock();
  980. }
  981. const String getInputChannelName(int i) const override
  982. {
  983. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  984. CarlaEngineClient* const client(fPlugin->getEngineClient());
  985. return client->getAudioPortName(true, static_cast<uint>(i));
  986. }
  987. const String getOutputChannelName(int i) const override
  988. {
  989. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  990. CarlaEngineClient* const client(fPlugin->getEngineClient());
  991. return client->getAudioPortName(false, static_cast<uint>(i));
  992. }
  993. void prepareToPlay(double, int) override {}
  994. void releaseResources() override {}
  995. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  996. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  997. private:
  998. CarlaEngine* const kEngine;
  999. CarlaPlugin* fPlugin;
  1000. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1001. };
  1002. // -----------------------------------------------------------------------
  1003. // Patchbay Graph
  1004. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1005. {
  1006. public:
  1007. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1008. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1009. inputNames(),
  1010. outputNames() {}
  1011. const String getInputChannelName (int index) const override
  1012. {
  1013. if (index < inputNames.size())
  1014. return inputNames[index];
  1015. return String("Playback ") + String(index+1);
  1016. }
  1017. const String getOutputChannelName (int index) const override
  1018. {
  1019. if (index < outputNames.size())
  1020. return outputNames[index];
  1021. return String("Capture ") + String(index+1);
  1022. }
  1023. void setNames(const bool setInputNames, const StringArray& names)
  1024. {
  1025. if (setInputNames)
  1026. inputNames = names;
  1027. else
  1028. outputNames = names;
  1029. }
  1030. private:
  1031. StringArray inputNames;
  1032. StringArray outputNames;
  1033. };
  1034. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs)
  1035. : CarlaThread("PatchbayReorderThread"),
  1036. connections(),
  1037. graph(),
  1038. audioBuffer(),
  1039. midiBuffer(),
  1040. inputs(carla_fixedValue(0U, 32U, ins)),
  1041. outputs(carla_fixedValue(0U, 32U, outs)),
  1042. retCon(),
  1043. usingExternal(false),
  1044. extGraph(engine),
  1045. kEngine(engine)
  1046. {
  1047. const int bufferSize(static_cast<int>(engine->getBufferSize()));
  1048. const double sampleRate(engine->getSampleRate());
  1049. graph.setPlayConfigDetails(static_cast<int>(inputs), static_cast<int>(outputs), sampleRate, bufferSize);
  1050. graph.prepareToPlay(sampleRate, bufferSize);
  1051. audioBuffer.setSize(static_cast<int>(jmax(inputs, outputs)), bufferSize);
  1052. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1053. midiBuffer.clear();
  1054. StringArray channelNames;
  1055. switch (inputs)
  1056. {
  1057. case 2:
  1058. channelNames.add("Left");
  1059. channelNames.add("Right");
  1060. break;
  1061. case 3:
  1062. channelNames.add("Left");
  1063. channelNames.add("Right");
  1064. channelNames.add("Sidechain");
  1065. break;
  1066. }
  1067. {
  1068. NamedAudioGraphIOProcessor* const proc(
  1069. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1070. proc->setNames(false, channelNames);
  1071. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1072. node->properties.set("isPlugin", false);
  1073. node->properties.set("isOutput", false);
  1074. node->properties.set("isAudio", true);
  1075. node->properties.set("isCV", false);
  1076. node->properties.set("isMIDI", false);
  1077. node->properties.set("isOSC", false);
  1078. }
  1079. {
  1080. NamedAudioGraphIOProcessor* const proc(
  1081. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1082. proc->setNames(true, channelNames);
  1083. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1084. node->properties.set("isPlugin", false);
  1085. node->properties.set("isOutput", false);
  1086. node->properties.set("isAudio", true);
  1087. node->properties.set("isCV", false);
  1088. node->properties.set("isMIDI", false);
  1089. node->properties.set("isOSC", false);
  1090. }
  1091. {
  1092. NamedAudioGraphIOProcessor* const proc(
  1093. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  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", false);
  1098. node->properties.set("isCV", false);
  1099. node->properties.set("isMIDI", true);
  1100. node->properties.set("isOSC", false);
  1101. }
  1102. {
  1103. NamedAudioGraphIOProcessor* const proc(
  1104. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1105. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1106. node->properties.set("isPlugin", false);
  1107. node->properties.set("isOutput", true);
  1108. node->properties.set("isAudio", false);
  1109. node->properties.set("isCV", false);
  1110. node->properties.set("isMIDI", true);
  1111. node->properties.set("isOSC", false);
  1112. }
  1113. startThread();
  1114. }
  1115. PatchbayGraph::~PatchbayGraph()
  1116. {
  1117. stopThread(-1);
  1118. connections.clear();
  1119. extGraph.clear();
  1120. graph.releaseResources();
  1121. graph.clear();
  1122. audioBuffer.clear();
  1123. }
  1124. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1125. {
  1126. const int bufferSizei(static_cast<int>(bufferSize));
  1127. graph.releaseResources();
  1128. graph.prepareToPlay(kEngine->getSampleRate(), bufferSizei);
  1129. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSizei);
  1130. }
  1131. void PatchbayGraph::setSampleRate(const double sampleRate)
  1132. {
  1133. graph.releaseResources();
  1134. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1135. }
  1136. void PatchbayGraph::setOffline(const bool offline)
  1137. {
  1138. graph.setNonRealtime(offline);
  1139. }
  1140. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  1141. {
  1142. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1143. carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
  1144. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1145. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1146. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1147. plugin->setPatchbayNodeId(node->nodeId);
  1148. node->properties.set("isPlugin", true);
  1149. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1150. if (! usingExternal)
  1151. addNodeToPatchbay(plugin->getEngine(), node->nodeId, static_cast<int>(plugin->getId()), instance);
  1152. }
  1153. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1154. {
  1155. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  1156. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  1157. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1158. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1159. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1160. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1161. if (! usingExternal)
  1162. {
  1163. disconnectInternalGroup(oldNode->nodeId);
  1164. removeNodeFromPatchbay(kEngine, oldNode->nodeId, oldNode->getProcessor());
  1165. }
  1166. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1167. graph.removeNode(oldNode->nodeId);
  1168. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1169. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1170. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1171. newPlugin->setPatchbayNodeId(node->nodeId);
  1172. node->properties.set("isPlugin", true);
  1173. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1174. if (! usingExternal)
  1175. addNodeToPatchbay(newPlugin->getEngine(), node->nodeId, static_cast<int>(newPlugin->getId()), instance);
  1176. }
  1177. void PatchbayGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1178. {
  1179. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1180. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin, newName);
  1181. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1182. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1183. if (! usingExternal)
  1184. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED, node->nodeId, 0, 0, 0.0f, newName);
  1185. }
  1186. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  1187. {
  1188. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1189. carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
  1190. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1191. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1192. if (! usingExternal)
  1193. {
  1194. disconnectInternalGroup(node->nodeId);
  1195. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1196. }
  1197. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1198. // Fix plugin Ids properties
  1199. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1200. {
  1201. CarlaPlugin* const plugin2(kEngine->getPlugin(i));
  1202. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  1203. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1204. {
  1205. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1206. node2->properties.set("pluginId", static_cast<int>(i-1));
  1207. }
  1208. }
  1209. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1210. }
  1211. void PatchbayGraph::removeAllPlugins()
  1212. {
  1213. carla_debug("PatchbayGraph::removeAllPlugins()");
  1214. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1215. {
  1216. CarlaPlugin* const plugin(kEngine->getPlugin(i));
  1217. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1218. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1219. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1220. if (! usingExternal)
  1221. {
  1222. disconnectInternalGroup(node->nodeId);
  1223. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1224. }
  1225. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1226. graph.removeNode(node->nodeId);
  1227. }
  1228. }
  1229. bool PatchbayGraph::connect(const bool external, const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback)
  1230. {
  1231. if (external)
  1232. return extGraph.connect(groupA, portA, groupB, portB, sendCallback);
  1233. uint adjustedPortA = portA;
  1234. uint adjustedPortB = portB;
  1235. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1236. return false;
  1237. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1238. return false;
  1239. if (! graph.addConnection(groupA, static_cast<int>(adjustedPortA), groupB, static_cast<int>(adjustedPortB)))
  1240. {
  1241. kEngine->setLastError("Failed from water");
  1242. return false;
  1243. }
  1244. ConnectionToId connectionToId;
  1245. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1246. char strBuf[STR_MAX+1];
  1247. strBuf[STR_MAX] = '\0';
  1248. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1249. if (sendCallback)
  1250. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1251. connections.list.append(connectionToId);
  1252. return true;
  1253. }
  1254. bool PatchbayGraph::disconnect(const uint connectionId)
  1255. {
  1256. if (usingExternal)
  1257. return extGraph.disconnect(connectionId);
  1258. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1259. {
  1260. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1261. const ConnectionToId& connectionToId(it.getValue(fallback));
  1262. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1263. if (connectionToId.id != connectionId)
  1264. continue;
  1265. uint adjustedPortA = connectionToId.portA;
  1266. uint adjustedPortB = connectionToId.portB;
  1267. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1268. return false;
  1269. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1270. return false;
  1271. if (! graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1272. connectionToId.groupB, static_cast<int>(adjustedPortB)))
  1273. return false;
  1274. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1275. connections.list.remove(it);
  1276. return true;
  1277. }
  1278. kEngine->setLastError("Failed to find connection");
  1279. return false;
  1280. }
  1281. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1282. {
  1283. CARLA_SAFE_ASSERT(! usingExternal);
  1284. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1285. {
  1286. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1287. const ConnectionToId& connectionToId(it.getValue(fallback));
  1288. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1289. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1290. continue;
  1291. /*
  1292. uint adjustedPortA = connectionToId.portA;
  1293. uint adjustedPortB = connectionToId.portB;
  1294. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1295. return false;
  1296. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1297. return false;
  1298. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1299. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1300. */
  1301. if (! usingExternal)
  1302. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1303. connections.list.remove(it);
  1304. }
  1305. }
  1306. void PatchbayGraph::refresh(const char* const deviceName)
  1307. {
  1308. if (usingExternal)
  1309. return extGraph.refresh(deviceName);
  1310. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1311. connections.clear();
  1312. graph.removeIllegalConnections();
  1313. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1314. {
  1315. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1316. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1317. AudioProcessor* const proc(node->getProcessor());
  1318. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1319. int clientId = -1;
  1320. // plugin node
  1321. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1322. clientId = node->properties.getWithDefault("pluginId", -1);
  1323. addNodeToPatchbay(kEngine, node->nodeId, clientId, proc);
  1324. }
  1325. char strBuf[STR_MAX+1];
  1326. strBuf[STR_MAX] = '\0';
  1327. for (int i=0, count=graph.getNumConnections(); i<count; ++i)
  1328. {
  1329. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1330. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1331. CARLA_SAFE_ASSERT_CONTINUE(conn->sourceChannelIndex >= 0);
  1332. CARLA_SAFE_ASSERT_CONTINUE(conn->destChannelIndex >= 0);
  1333. const uint groupA = conn->sourceNodeId;
  1334. const uint groupB = conn->destNodeId;
  1335. uint portA = static_cast<uint>(conn->sourceChannelIndex);
  1336. uint portB = static_cast<uint>(conn->destChannelIndex);
  1337. if (portA == kMidiChannelIndex)
  1338. portA = kMidiOutputPortOffset;
  1339. else
  1340. portA += kAudioOutputPortOffset;
  1341. if (portB == kMidiChannelIndex)
  1342. portB = kMidiInputPortOffset;
  1343. else
  1344. portB += kAudioInputPortOffset;
  1345. ConnectionToId connectionToId;
  1346. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1347. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1348. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1349. connections.list.append(connectionToId);
  1350. }
  1351. }
  1352. const char* const* PatchbayGraph::getConnections(const bool external) const
  1353. {
  1354. if (external)
  1355. return extGraph.getConnections();
  1356. if (connections.list.count() == 0)
  1357. return nullptr;
  1358. CarlaStringList connList;
  1359. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1360. {
  1361. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1362. const ConnectionToId& connectionToId(it.getValue(fallback));
  1363. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1364. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1365. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1366. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1367. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1368. AudioProcessor* const procA(nodeA->getProcessor());
  1369. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1370. AudioProcessor* const procB(nodeB->getProcessor());
  1371. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1372. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1373. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1374. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1375. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1376. connList.append(fullPortNameA.toRawUTF8());
  1377. connList.append(fullPortNameB.toRawUTF8());
  1378. }
  1379. if (connList.count() == 0)
  1380. return nullptr;
  1381. retCon = connList.toCharStringListPtr();
  1382. return retCon;
  1383. }
  1384. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1385. {
  1386. if (external)
  1387. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1388. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1389. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1390. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1391. {
  1392. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1393. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1394. AudioProcessor* const proc(node->getProcessor());
  1395. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1396. if (proc->getName() != groupName)
  1397. continue;
  1398. groupId = node->nodeId;
  1399. if (portName == "events-in")
  1400. {
  1401. portId = kMidiInputPortOffset;
  1402. return true;
  1403. }
  1404. if (portName == "events-out")
  1405. {
  1406. portId = kMidiOutputPortOffset;
  1407. return true;
  1408. }
  1409. for (int j=0, numInputs=proc->getTotalNumInputChannels(); j<numInputs; ++j)
  1410. {
  1411. if (proc->getInputChannelName(j) != portName)
  1412. continue;
  1413. portId = kAudioInputPortOffset+static_cast<uint>(j);
  1414. return true;
  1415. }
  1416. for (int j=0, numOutputs=proc->getTotalNumOutputChannels(); j<numOutputs; ++j)
  1417. {
  1418. if (proc->getOutputChannelName(j) != portName)
  1419. continue;
  1420. portId = kAudioOutputPortOffset+static_cast<uint>(j);
  1421. return true;
  1422. }
  1423. }
  1424. return false;
  1425. }
  1426. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const int frames)
  1427. {
  1428. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1429. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  1430. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  1431. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  1432. // put events in water buffer
  1433. {
  1434. midiBuffer.clear();
  1435. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  1436. }
  1437. // set audio buffer size, needed for water internals
  1438. audioBuffer.setSizeRT(frames);
  1439. // put carla audio in water buffer
  1440. {
  1441. int i=0;
  1442. for (; i < static_cast<int>(inputs); ++i)
  1443. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  1444. // clear remaining channels
  1445. for (const int count=audioBuffer.getNumChannels(); i<count; ++i)
  1446. audioBuffer.clear(i, 0, frames);
  1447. }
  1448. // ready to go!
  1449. graph.processBlock(audioBuffer, midiBuffer);
  1450. // put water audio in carla buffer
  1451. {
  1452. for (int i=0; i < static_cast<int>(outputs); ++i)
  1453. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  1454. }
  1455. // put water events in carla buffer
  1456. {
  1457. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  1458. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  1459. midiBuffer.clear();
  1460. }
  1461. }
  1462. void PatchbayGraph::run()
  1463. {
  1464. while (! shouldThreadExit())
  1465. {
  1466. carla_msleep(100);
  1467. graph.reorderNowIfNeeded();
  1468. }
  1469. }
  1470. // -----------------------------------------------------------------------
  1471. // InternalGraph
  1472. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  1473. : fIsReady(false),
  1474. fRack(nullptr),
  1475. kEngine(engine) {}
  1476. EngineInternalGraph::~EngineInternalGraph() noexcept
  1477. {
  1478. CARLA_SAFE_ASSERT(! fIsReady);
  1479. CARLA_SAFE_ASSERT(fRack == nullptr);
  1480. }
  1481. void EngineInternalGraph::create(const uint32_t inputs, const uint32_t outputs)
  1482. {
  1483. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  1484. if (fIsRack)
  1485. {
  1486. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  1487. fRack = new RackGraph(kEngine, inputs, outputs);
  1488. }
  1489. else
  1490. {
  1491. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  1492. fPatchbay = new PatchbayGraph(kEngine, inputs, outputs);
  1493. }
  1494. fIsReady = true;
  1495. }
  1496. void EngineInternalGraph::destroy() noexcept
  1497. {
  1498. if (! fIsReady)
  1499. {
  1500. CARLA_SAFE_ASSERT(fRack == nullptr);
  1501. return;
  1502. }
  1503. if (fIsRack)
  1504. {
  1505. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1506. delete fRack;
  1507. fRack = nullptr;
  1508. }
  1509. else
  1510. {
  1511. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1512. delete fPatchbay;
  1513. fPatchbay = nullptr;
  1514. }
  1515. fIsReady = false;
  1516. }
  1517. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  1518. {
  1519. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1520. if (fIsRack)
  1521. {
  1522. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1523. fRack->setBufferSize(bufferSize);
  1524. }
  1525. else
  1526. {
  1527. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1528. fPatchbay->setBufferSize(bufferSize);
  1529. }
  1530. }
  1531. void EngineInternalGraph::setSampleRate(const double sampleRate)
  1532. {
  1533. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1534. if (fIsRack)
  1535. {
  1536. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1537. }
  1538. else
  1539. {
  1540. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1541. fPatchbay->setSampleRate(sampleRate);
  1542. }
  1543. }
  1544. void EngineInternalGraph::setOffline(const bool offline)
  1545. {
  1546. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1547. if (fIsRack)
  1548. {
  1549. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1550. fRack->setOffline(offline);
  1551. }
  1552. else
  1553. {
  1554. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1555. fPatchbay->setOffline(offline);
  1556. }
  1557. }
  1558. bool EngineInternalGraph::isReady() const noexcept
  1559. {
  1560. return fIsReady;
  1561. }
  1562. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  1563. {
  1564. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  1565. return fRack;
  1566. }
  1567. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  1568. {
  1569. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  1570. return fPatchbay;
  1571. }
  1572. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1573. {
  1574. if (fIsRack)
  1575. {
  1576. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1577. fRack->processHelper(data, inBuf, outBuf, frames);
  1578. }
  1579. else
  1580. {
  1581. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1582. fPatchbay->process(data, inBuf, outBuf, static_cast<int>(frames));
  1583. }
  1584. }
  1585. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  1586. {
  1587. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  1588. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1589. fRack->process(data, inBuf, outBuf, frames);
  1590. }
  1591. // -----------------------------------------------------------------------
  1592. // used for internal patchbay mode
  1593. void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
  1594. {
  1595. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1596. fPatchbay->addPlugin(plugin);
  1597. }
  1598. void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1599. {
  1600. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1601. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  1602. }
  1603. void EngineInternalGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1604. {
  1605. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1606. fPatchbay->renamePlugin(plugin, newName);
  1607. }
  1608. void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
  1609. {
  1610. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1611. fPatchbay->removePlugin(plugin);
  1612. }
  1613. void EngineInternalGraph::removeAllPlugins()
  1614. {
  1615. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1616. fPatchbay->removeAllPlugins();
  1617. }
  1618. bool EngineInternalGraph::isUsingExternal() const noexcept
  1619. {
  1620. if (fIsRack)
  1621. return true;
  1622. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  1623. return fPatchbay->usingExternal;
  1624. }
  1625. void EngineInternalGraph::setUsingExternal(const bool usingExternal) noexcept
  1626. {
  1627. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  1628. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1629. fPatchbay->usingExternal = usingExternal;
  1630. }
  1631. // -----------------------------------------------------------------------
  1632. // CarlaEngine Patchbay stuff
  1633. bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
  1634. {
  1635. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1636. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1637. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  1638. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1639. {
  1640. RackGraph* const graph = pData->graph.getRackGraph();
  1641. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1642. return graph->connect(groupA, portA, groupB, portB);
  1643. }
  1644. else
  1645. {
  1646. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1647. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1648. return graph->connect(graph->usingExternal, groupA, portA, groupB, portB, true);
  1649. }
  1650. return false;
  1651. }
  1652. bool CarlaEngine::patchbayDisconnect(const uint connectionId)
  1653. {
  1654. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1655. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1656. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  1657. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1658. {
  1659. RackGraph* const graph = pData->graph.getRackGraph();
  1660. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1661. return graph->disconnect(connectionId);
  1662. }
  1663. else
  1664. {
  1665. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1666. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1667. return graph->disconnect(connectionId);
  1668. }
  1669. return false;
  1670. }
  1671. bool CarlaEngine::patchbayRefresh(const bool external)
  1672. {
  1673. // subclasses should handle this
  1674. CARLA_SAFE_ASSERT_RETURN(! external, false);
  1675. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1676. {
  1677. // This is implemented in engine subclasses
  1678. setLastError("Unsupported operation");
  1679. return false;
  1680. }
  1681. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1682. {
  1683. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1684. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1685. graph->refresh("");
  1686. return true;
  1687. }
  1688. setLastError("Unsupported operation");
  1689. return false;
  1690. }
  1691. // -----------------------------------------------------------------------
  1692. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  1693. {
  1694. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  1695. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  1696. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1697. {
  1698. RackGraph* const graph = pData->graph.getRackGraph();
  1699. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1700. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  1701. return graph->getConnections();
  1702. }
  1703. else
  1704. {
  1705. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1706. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1707. return graph->getConnections(external);
  1708. }
  1709. return nullptr;
  1710. }
  1711. void CarlaEngine::restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback)
  1712. {
  1713. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  1714. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  1715. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  1716. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  1717. uint groupA, portA;
  1718. uint groupB, portB;
  1719. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1720. {
  1721. RackGraph* const graph = pData->graph.getRackGraph();
  1722. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1723. CARLA_SAFE_ASSERT_RETURN(external,);
  1724. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  1725. return;
  1726. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  1727. return;
  1728. graph->connect(groupA, portA, groupB, portB);
  1729. }
  1730. else
  1731. {
  1732. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1733. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1734. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  1735. return;
  1736. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  1737. return;
  1738. graph->connect(external, groupA, portA, groupB, portB, sendCallback);
  1739. }
  1740. }
  1741. // -----------------------------------------------------------------------
  1742. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1743. {
  1744. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1745. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1746. RackGraph* const graph(pData->graph.getRackGraph());
  1747. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1748. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1749. switch (connectionType)
  1750. {
  1751. case kExternalGraphConnectionAudioIn1:
  1752. return graph->audioBuffers.connectedIn1.append(portId);
  1753. case kExternalGraphConnectionAudioIn2:
  1754. return graph->audioBuffers.connectedIn2.append(portId);
  1755. case kExternalGraphConnectionAudioOut1:
  1756. return graph->audioBuffers.connectedOut1.append(portId);
  1757. case kExternalGraphConnectionAudioOut2:
  1758. return graph->audioBuffers.connectedOut2.append(portId);
  1759. }
  1760. return false;
  1761. }
  1762. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1763. {
  1764. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1765. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1766. RackGraph* const graph(pData->graph.getRackGraph());
  1767. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1768. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1769. switch (connectionType)
  1770. {
  1771. case kExternalGraphConnectionAudioIn1:
  1772. return graph->audioBuffers.connectedIn1.removeOne(portId);
  1773. case kExternalGraphConnectionAudioIn2:
  1774. return graph->audioBuffers.connectedIn2.removeOne(portId);
  1775. case kExternalGraphConnectionAudioOut1:
  1776. return graph->audioBuffers.connectedOut1.removeOne(portId);
  1777. case kExternalGraphConnectionAudioOut2:
  1778. return graph->audioBuffers.connectedOut2.removeOne(portId);
  1779. }
  1780. return false;
  1781. }
  1782. // -----------------------------------------------------------------------
  1783. CARLA_BACKEND_END_NAMESPACE