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

10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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 "CarlaScopeUtils.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. positions(),
  116. retCon(),
  117. kEngine(engine)
  118. {
  119. carla_zeroStruct(positions);
  120. }
  121. void ExternalGraph::clear() noexcept
  122. {
  123. connections.clear();
  124. audioPorts.ins.clear();
  125. audioPorts.outs.clear();
  126. midiPorts.ins.clear();
  127. midiPorts.outs.clear();
  128. }
  129. bool ExternalGraph::connect(const bool sendHost, const bool sendOSC,
  130. const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  131. {
  132. uint otherGroup, otherPort, carlaPort;
  133. if (groupA == kExternalGraphGroupCarla)
  134. {
  135. CARLA_SAFE_ASSERT_RETURN(groupB != kExternalGraphGroupCarla, false);
  136. carlaPort = portA;
  137. otherGroup = groupB;
  138. otherPort = portB;
  139. }
  140. else
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(groupB == kExternalGraphGroupCarla, false);
  143. carlaPort = portB;
  144. otherGroup = groupA;
  145. otherPort = portA;
  146. }
  147. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  148. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  149. bool makeConnection = false;
  150. switch (carlaPort)
  151. {
  152. case kExternalGraphCarlaPortAudioIn1:
  153. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  154. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  155. break;
  156. case kExternalGraphCarlaPortAudioIn2:
  157. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  158. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  159. break;
  160. case kExternalGraphCarlaPortAudioOut1:
  161. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  162. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  163. break;
  164. case kExternalGraphCarlaPortAudioOut2:
  165. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  166. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  167. break;
  168. case kExternalGraphCarlaPortMidiIn:
  169. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiIn, false);
  170. if (const char* const portName = midiPorts.getName(true, otherPort))
  171. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  172. break;
  173. case kExternalGraphCarlaPortMidiOut:
  174. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiOut, false);
  175. if (const char* const portName = midiPorts.getName(false, otherPort))
  176. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  177. break;
  178. }
  179. if (! makeConnection)
  180. {
  181. kEngine->setLastError("Invalid rack connection");
  182. return false;
  183. }
  184. ConnectionToId connectionToId;
  185. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  186. char strBuf[STR_MAX+1];
  187. strBuf[STR_MAX] = '\0';
  188. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  189. kEngine->callback(sendHost, sendOSC,
  190. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0, 0.0f, strBuf);
  191. connections.list.append(connectionToId);
  192. return true;
  193. }
  194. bool ExternalGraph::disconnect(const bool sendHost, const bool sendOSC,
  195. const uint connectionId) noexcept
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(connections.list.count() > 0, false);
  198. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  199. {
  200. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  201. const ConnectionToId& connectionToId(it.getValue(fallback));
  202. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  203. if (connectionToId.id != connectionId)
  204. continue;
  205. uint otherGroup, otherPort, carlaPort;
  206. if (connectionToId.groupA == kExternalGraphGroupCarla)
  207. {
  208. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB != kExternalGraphGroupCarla, false);
  209. carlaPort = connectionToId.portA;
  210. otherGroup = connectionToId.groupB;
  211. otherPort = connectionToId.portB;
  212. }
  213. else
  214. {
  215. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB == kExternalGraphGroupCarla, false);
  216. carlaPort = connectionToId.portB;
  217. otherGroup = connectionToId.groupA;
  218. otherPort = connectionToId.portA;
  219. }
  220. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  221. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  222. bool makeDisconnection = false;
  223. switch (carlaPort)
  224. {
  225. case kExternalGraphCarlaPortAudioIn1:
  226. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  227. break;
  228. case kExternalGraphCarlaPortAudioIn2:
  229. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  230. break;
  231. case kExternalGraphCarlaPortAudioOut1:
  232. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  233. break;
  234. case kExternalGraphCarlaPortAudioOut2:
  235. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  236. break;
  237. case kExternalGraphCarlaPortMidiIn:
  238. if (const char* const portName = midiPorts.getName(true, otherPort))
  239. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  240. break;
  241. case kExternalGraphCarlaPortMidiOut:
  242. if (const char* const portName = midiPorts.getName(false, otherPort))
  243. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  244. break;
  245. }
  246. if (! makeDisconnection)
  247. {
  248. kEngine->setLastError("Invalid rack connection");
  249. return false;
  250. }
  251. kEngine->callback(sendHost, sendOSC,
  252. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0, 0.0f, nullptr);
  253. connections.list.remove(it);
  254. return true;
  255. }
  256. kEngine->setLastError("Failed to find connection");
  257. return false;
  258. }
  259. void ExternalGraph::setGroupPos(const bool sendHost, const bool sendOSC,
  260. const uint groupId, const int x1, const int y1, const int x2, const int y2)
  261. {
  262. CARLA_SAFE_ASSERT_UINT_RETURN(groupId >= kExternalGraphGroupCarla && groupId < kExternalGraphGroupMax, groupId,);
  263. const PatchbayPosition ppos = { true, x1, y1, x2, y2 };
  264. positions[groupId] = ppos;
  265. kEngine->callback(sendHost, sendOSC,
  266. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  267. groupId, x1, y1, x2, static_cast<float>(y2),
  268. nullptr);
  269. }
  270. void ExternalGraph::refresh(const bool sendHost, const bool sendOSC, const char* const deviceName)
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  273. const bool isRack = kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK;
  274. // Main
  275. {
  276. kEngine->callback(sendHost, sendOSC,
  277. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  278. kExternalGraphGroupCarla,
  279. PATCHBAY_ICON_CARLA,
  280. MAIN_CARLA_PLUGIN_ID,
  281. 0, 0.0f,
  282. kEngine->getName());
  283. if (isRack)
  284. {
  285. kEngine->callback(sendHost, sendOSC,
  286. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  287. kExternalGraphGroupCarla,
  288. kExternalGraphCarlaPortAudioIn1,
  289. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  290. 0, 0.0f,
  291. "audio-in1");
  292. kEngine->callback(sendHost, sendOSC,
  293. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  294. kExternalGraphGroupCarla,
  295. kExternalGraphCarlaPortAudioIn2,
  296. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  297. 0, 0.0f,
  298. "audio-in2");
  299. kEngine->callback(sendHost, sendOSC,
  300. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  301. kExternalGraphGroupCarla,
  302. kExternalGraphCarlaPortAudioOut1,
  303. PATCHBAY_PORT_TYPE_AUDIO,
  304. 0, 0.0f,
  305. "audio-out1");
  306. kEngine->callback(sendHost, sendOSC,
  307. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  308. kExternalGraphGroupCarla,
  309. kExternalGraphCarlaPortAudioOut2,
  310. PATCHBAY_PORT_TYPE_AUDIO,
  311. 0, 0.0f,
  312. "audio-out2");
  313. }
  314. kEngine->callback(sendHost, sendOSC,
  315. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  316. kExternalGraphGroupCarla,
  317. kExternalGraphCarlaPortMidiIn,
  318. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT,
  319. 0, 0.0f,
  320. "midi-in");
  321. kEngine->callback(sendHost, sendOSC,
  322. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  323. kExternalGraphGroupCarla,
  324. kExternalGraphCarlaPortMidiOut,
  325. PATCHBAY_PORT_TYPE_MIDI,
  326. 0, 0.0f,
  327. "midi-out");
  328. }
  329. char strBuf[STR_MAX+1];
  330. strBuf[STR_MAX] = '\0';
  331. if (isRack)
  332. {
  333. // Audio In
  334. if (deviceName[0] != '\0')
  335. std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName);
  336. else
  337. std::strncpy(strBuf, "Capture", STR_MAX);
  338. kEngine->callback(sendHost, sendOSC,
  339. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  340. kExternalGraphGroupAudioIn,
  341. PATCHBAY_ICON_HARDWARE,
  342. -1,
  343. 0, 0.0f,
  344. strBuf);
  345. const CarlaString groupNameIn(strBuf);
  346. int h = 0;
  347. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.ins.begin2(); it.valid(); it.next())
  348. {
  349. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  350. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  351. portNameToId.setFullName(groupNameIn + portNameToId.name);
  352. kEngine->callback(sendHost, sendOSC,
  353. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  354. kExternalGraphGroupAudioIn,
  355. ++h,
  356. PATCHBAY_PORT_TYPE_AUDIO,
  357. 0, 0.0f,
  358. portNameToId.name);
  359. }
  360. // Audio Out
  361. if (deviceName[0] != '\0')
  362. std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName);
  363. else
  364. std::strncpy(strBuf, "Playback", STR_MAX);
  365. kEngine->callback(sendHost, sendOSC,
  366. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  367. kExternalGraphGroupAudioOut,
  368. PATCHBAY_ICON_HARDWARE,
  369. -1,
  370. 0, 0.0f,
  371. strBuf);
  372. const CarlaString groupNameOut(strBuf);
  373. h = 0;
  374. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.outs.begin2(); it.valid(); it.next())
  375. {
  376. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  377. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  378. portNameToId.setFullName(groupNameOut + portNameToId.name);
  379. kEngine->callback(sendHost, sendOSC,
  380. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  381. kExternalGraphGroupAudioOut,
  382. ++h,
  383. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  384. 0, 0.0f,
  385. portNameToId.name);
  386. }
  387. }
  388. // MIDI In
  389. {
  390. kEngine->callback(sendHost, sendOSC,
  391. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  392. kExternalGraphGroupMidiIn,
  393. PATCHBAY_ICON_HARDWARE,
  394. -1,
  395. 0, 0.0f,
  396. "Readable MIDI ports");
  397. const CarlaString groupNamePlus("Readable MIDI ports:");
  398. int h = 0;
  399. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.ins.begin2(); it.valid(); it.next())
  400. {
  401. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  402. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  403. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  404. kEngine->callback(sendHost, sendOSC,
  405. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  406. kExternalGraphGroupMidiIn,
  407. ++h,
  408. PATCHBAY_PORT_TYPE_MIDI,
  409. 0, 0.0f,
  410. portNameToId.name);
  411. }
  412. }
  413. // MIDI Out
  414. {
  415. kEngine->callback(sendHost, sendOSC,
  416. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  417. kExternalGraphGroupMidiOut,
  418. PATCHBAY_ICON_HARDWARE,
  419. -1,
  420. 0, 0.0f,
  421. "Writable MIDI ports");
  422. const CarlaString groupNamePlus("Writable MIDI ports:");
  423. int h = 0;
  424. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.outs.begin2(); it.valid(); it.next())
  425. {
  426. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  427. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  428. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  429. kEngine->callback(sendHost, sendOSC,
  430. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  431. kExternalGraphGroupMidiOut,
  432. ++h,
  433. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT,
  434. 0, 0.0f,
  435. portNameToId.name);
  436. }
  437. }
  438. }
  439. const char* const* ExternalGraph::getConnections() const noexcept
  440. {
  441. if (connections.list.count() == 0)
  442. return nullptr;
  443. CarlaStringList connList;
  444. char strBuf[STR_MAX+1];
  445. strBuf[STR_MAX] = '\0';
  446. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  447. {
  448. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  449. const ConnectionToId& connectionToId(it.getValue(fallback));
  450. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  451. uint otherGroup, otherPort, carlaPort;
  452. if (connectionToId.groupA == kExternalGraphGroupCarla)
  453. {
  454. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB != kExternalGraphGroupCarla);
  455. carlaPort = connectionToId.portA;
  456. otherGroup = connectionToId.groupB;
  457. otherPort = connectionToId.portB;
  458. }
  459. else
  460. {
  461. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB == kExternalGraphGroupCarla);
  462. carlaPort = connectionToId.portB;
  463. otherGroup = connectionToId.groupA;
  464. otherPort = connectionToId.portA;
  465. }
  466. CARLA_SAFE_ASSERT_CONTINUE(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax);
  467. CARLA_SAFE_ASSERT_CONTINUE(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax);
  468. switch (carlaPort)
  469. {
  470. case kExternalGraphCarlaPortAudioIn1:
  471. case kExternalGraphCarlaPortAudioIn2:
  472. std::snprintf(strBuf, STR_MAX, "AudioIn:%s", audioPorts.getName(true, otherPort));
  473. connList.append(strBuf);
  474. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  475. break;
  476. case kExternalGraphCarlaPortAudioOut1:
  477. case kExternalGraphCarlaPortAudioOut2:
  478. std::snprintf(strBuf, STR_MAX, "AudioOut:%s", audioPorts.getName(false, otherPort));
  479. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  480. connList.append(strBuf);
  481. break;
  482. case kExternalGraphCarlaPortMidiIn:
  483. std::snprintf(strBuf, STR_MAX, "MidiIn:%s", midiPorts.getName(true, otherPort));
  484. connList.append(strBuf);
  485. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  486. break;
  487. case kExternalGraphCarlaPortMidiOut:
  488. std::snprintf(strBuf, STR_MAX, "MidiOut:%s", midiPorts.getName(false, otherPort));
  489. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  490. connList.append(strBuf);
  491. break;
  492. }
  493. }
  494. if (connList.count() == 0)
  495. return nullptr;
  496. retCon = connList.toCharStringListPtr();
  497. return retCon;
  498. }
  499. bool ExternalGraph::getGroupFromName(const char* groupName, uint& groupId) const noexcept
  500. {
  501. CARLA_SAFE_ASSERT_RETURN(groupName != nullptr && groupName[0] != '\0', false);
  502. if (std::strcmp(groupName, "Carla") == 0)
  503. {
  504. groupId = kExternalGraphGroupCarla;
  505. return true;
  506. }
  507. if (std::strcmp(groupName, "AudioIn") == 0)
  508. {
  509. groupId = kExternalGraphGroupAudioIn;
  510. return true;
  511. }
  512. if (std::strcmp(groupName, "AudioOut") == 0)
  513. {
  514. groupId = kExternalGraphGroupAudioOut;
  515. return true;
  516. }
  517. if (std::strcmp(groupName, "MidiIn") == 0)
  518. {
  519. groupId = kExternalGraphGroupMidiIn;
  520. return true;
  521. }
  522. if (std::strcmp(groupName, "MidiOut") == 0)
  523. {
  524. groupId = kExternalGraphGroupMidiOut;
  525. return true;
  526. }
  527. return false;
  528. }
  529. bool ExternalGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  530. {
  531. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', false);
  532. if (std::strncmp(fullPortName, "Carla:", 6) == 0)
  533. {
  534. groupId = kExternalGraphGroupCarla;
  535. portId = getExternalGraphPortIdFromName(fullPortName+6);
  536. if (portId > kExternalGraphCarlaPortNull && portId < kExternalGraphCarlaPortMax)
  537. return true;
  538. }
  539. else if (std::strncmp(fullPortName, "AudioIn:", 8) == 0)
  540. {
  541. groupId = kExternalGraphGroupAudioIn;
  542. if (const char* const portName = fullPortName+8)
  543. {
  544. bool ok;
  545. portId = audioPorts.getPortId(true, portName, &ok);
  546. return ok;
  547. }
  548. }
  549. else if (std::strncmp(fullPortName, "AudioOut:", 9) == 0)
  550. {
  551. groupId = kExternalGraphGroupAudioOut;
  552. if (const char* const portName = fullPortName+9)
  553. {
  554. bool ok;
  555. portId = audioPorts.getPortId(false, portName, &ok);
  556. return ok;
  557. }
  558. }
  559. else if (std::strncmp(fullPortName, "MidiIn:", 7) == 0)
  560. {
  561. groupId = kExternalGraphGroupMidiIn;
  562. if (const char* const portName = fullPortName+7)
  563. {
  564. bool ok;
  565. portId = midiPorts.getPortId(true, portName, &ok);
  566. return ok;
  567. }
  568. }
  569. else if (std::strncmp(fullPortName, "MidiOut:", 8) == 0)
  570. {
  571. groupId = kExternalGraphGroupMidiOut;
  572. if (const char* const portName = fullPortName+8)
  573. {
  574. bool ok;
  575. portId = midiPorts.getPortId(false, portName, &ok);
  576. return ok;
  577. }
  578. }
  579. return false;
  580. }
  581. // -----------------------------------------------------------------------
  582. // RackGraph Buffers
  583. RackGraph::Buffers::Buffers() noexcept
  584. : mutex(),
  585. connectedIn1(),
  586. connectedIn2(),
  587. connectedOut1(),
  588. connectedOut2(),
  589. #ifdef CARLA_PROPER_CPP11_SUPPORT
  590. inBuf{nullptr, nullptr},
  591. inBufTmp{nullptr, nullptr},
  592. outBuf{nullptr, nullptr},
  593. #endif
  594. unusedBuf(nullptr)
  595. {
  596. #ifndef CARLA_PROPER_CPP11_SUPPORT
  597. inBuf[0] = inBuf[1] = nullptr;
  598. inBufTmp[0] = inBufTmp[1] = nullptr;
  599. outBuf[0] = outBuf[1] = nullptr;
  600. #endif
  601. }
  602. RackGraph::Buffers::~Buffers() noexcept
  603. {
  604. const CarlaRecursiveMutexLocker cml(mutex);
  605. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  606. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  607. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  608. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  609. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  610. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  611. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  612. connectedIn1.clear();
  613. connectedIn2.clear();
  614. connectedOut1.clear();
  615. connectedOut2.clear();
  616. }
  617. void RackGraph::Buffers::setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept
  618. {
  619. const CarlaRecursiveMutexLocker cml(mutex);
  620. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  621. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  622. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  623. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  624. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  625. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  626. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  627. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  628. try {
  629. inBufTmp[0] = new float[bufferSize];
  630. inBufTmp[1] = new float[bufferSize];
  631. unusedBuf = new float[bufferSize];
  632. if (createBuffers)
  633. {
  634. inBuf[0] = new float[bufferSize];
  635. inBuf[1] = new float[bufferSize];
  636. outBuf[0] = new float[bufferSize];
  637. outBuf[1] = new float[bufferSize];
  638. }
  639. }
  640. catch(...) {
  641. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  642. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  643. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  644. if (createBuffers)
  645. {
  646. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  647. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  648. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  649. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  650. }
  651. return;
  652. }
  653. carla_zeroFloats(inBufTmp[0], bufferSize);
  654. carla_zeroFloats(inBufTmp[1], bufferSize);
  655. if (createBuffers)
  656. {
  657. carla_zeroFloats(inBuf[0], bufferSize);
  658. carla_zeroFloats(inBuf[1], bufferSize);
  659. carla_zeroFloats(outBuf[0], bufferSize);
  660. carla_zeroFloats(outBuf[1], bufferSize);
  661. }
  662. }
  663. // -----------------------------------------------------------------------
  664. // RackGraph
  665. RackGraph::RackGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs) noexcept
  666. : extGraph(engine),
  667. inputs(ins),
  668. outputs(outs),
  669. isOffline(false),
  670. audioBuffers(),
  671. kEngine(engine)
  672. {
  673. setBufferSize(engine->getBufferSize());
  674. }
  675. RackGraph::~RackGraph() noexcept
  676. {
  677. extGraph.clear();
  678. }
  679. void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
  680. {
  681. audioBuffers.setBufferSize(bufferSize, (inputs > 0 || outputs > 0));
  682. }
  683. void RackGraph::setOffline(const bool offline) noexcept
  684. {
  685. isOffline = offline;
  686. }
  687. bool RackGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  688. {
  689. return extGraph.connect(true, true, groupA, portA, groupB, portB);
  690. }
  691. bool RackGraph::disconnect(const uint connectionId) noexcept
  692. {
  693. return extGraph.disconnect(true, true, connectionId);
  694. }
  695. void RackGraph::refresh(const bool sendHost, const bool sendOSC, const bool, const char* const deviceName)
  696. {
  697. extGraph.refresh(sendHost, sendOSC, deviceName);
  698. char strBuf[STR_MAX+1];
  699. strBuf[STR_MAX] = '\0';
  700. // Connections
  701. const CarlaRecursiveMutexLocker cml(audioBuffers.mutex);
  702. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  703. {
  704. const uint& portId(it.getValue(0));
  705. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  706. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  707. ConnectionToId connectionToId;
  708. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1);
  709. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  710. kEngine->callback(sendHost, sendOSC,
  711. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  712. connectionToId.id,
  713. 0, 0, 0, 0.0f,
  714. strBuf);
  715. extGraph.connections.list.append(connectionToId);
  716. }
  717. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  718. {
  719. const uint& portId(it.getValue(0));
  720. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  721. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  722. ConnectionToId connectionToId;
  723. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2);
  724. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  725. kEngine->callback(sendHost, sendOSC,
  726. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  727. connectionToId.id,
  728. 0, 0, 0, 0.0f,
  729. strBuf);
  730. extGraph.connections.list.append(connectionToId);
  731. }
  732. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  733. {
  734. const uint& portId(it.getValue(0));
  735. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  736. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  737. ConnectionToId connectionToId;
  738. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, kExternalGraphGroupAudioOut, portId);
  739. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  740. kEngine->callback(sendHost, sendOSC,
  741. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  742. connectionToId.id,
  743. 0, 0, 0, 0.0f,
  744. strBuf);
  745. extGraph.connections.list.append(connectionToId);
  746. }
  747. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  748. {
  749. const uint& portId(it.getValue(0));
  750. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  751. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  752. ConnectionToId connectionToId;
  753. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, kExternalGraphGroupAudioOut, portId);
  754. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  755. kEngine->callback(sendHost, sendOSC,
  756. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  757. connectionToId.id,
  758. 0, 0, 0, 0.0f,
  759. strBuf);
  760. extGraph.connections.list.append(connectionToId);
  761. }
  762. }
  763. const char* const* RackGraph::getConnections() const noexcept
  764. {
  765. return extGraph.getConnections();
  766. }
  767. bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  768. {
  769. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  770. }
  771. void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBufReal[2], const uint32_t frames)
  772. {
  773. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  774. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  775. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  776. // safe copy
  777. float* const dummyBuf = audioBuffers.unusedBuf;
  778. float* const inBuf0 = audioBuffers.inBufTmp[0];
  779. float* const inBuf1 = audioBuffers.inBufTmp[1];
  780. // initialize audio inputs
  781. carla_copyFloats(inBuf0, inBufReal[0], frames);
  782. carla_copyFloats(inBuf1, inBufReal[1], frames);
  783. // initialize audio outputs (zero)
  784. carla_zeroFloats(outBufReal[0], frames);
  785. carla_zeroFloats(outBufReal[1], frames);
  786. // initialize event outputs (zero)
  787. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  788. uint32_t oldAudioInCount = 0;
  789. uint32_t oldAudioOutCount = 0;
  790. uint32_t oldMidiOutCount = 0;
  791. bool processed = false;
  792. // process plugins
  793. for (uint i=0; i < data->curPluginCount; ++i)
  794. {
  795. const CarlaPluginPtr plugin = data->plugins[i].plugin;
  796. if (plugin.get() == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  797. continue;
  798. if (processed)
  799. {
  800. // initialize audio inputs (from previous outputs)
  801. carla_copyFloats(inBuf0, outBufReal[0], frames);
  802. carla_copyFloats(inBuf1, outBufReal[1], frames);
  803. // initialize audio outputs (zero)
  804. carla_zeroFloats(outBufReal[0], frames);
  805. carla_zeroFloats(outBufReal[1], frames);
  806. // if plugin has no midi out, add previous events
  807. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  808. {
  809. if (data->events.out[0].type != kEngineEventTypeNull)
  810. {
  811. // TODO: carefully add to input, sorted events
  812. //carla_stderr("TODO midi event mixing here %s", plugin->getName());
  813. }
  814. // else nothing needed
  815. }
  816. else
  817. {
  818. // initialize event inputs from previous outputs
  819. carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  820. // initialize event outputs (zero)
  821. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  822. }
  823. }
  824. oldAudioInCount = plugin->getAudioInCount();
  825. oldAudioOutCount = plugin->getAudioOutCount();
  826. oldMidiOutCount = plugin->getMidiOutCount();
  827. const uint32_t numInBufs = std::max(oldAudioInCount, 2U);
  828. const uint32_t numOutBufs = std::max(oldAudioOutCount, 2U);
  829. const float* inBuf[numInBufs];
  830. inBuf[0] = inBuf0;
  831. inBuf[1] = inBuf1;
  832. float* outBuf[numOutBufs];
  833. outBuf[0] = outBufReal[0];
  834. outBuf[1] = outBufReal[1];
  835. if (numInBufs > 2 || numOutBufs > 2)
  836. {
  837. carla_zeroFloats(dummyBuf, frames);
  838. for (uint32_t j=2; j<numInBufs; ++j)
  839. inBuf[j] = dummyBuf;
  840. for (uint32_t j=2; j<numOutBufs; ++j)
  841. outBuf[j] = dummyBuf;
  842. }
  843. // process
  844. plugin->initBuffers();
  845. plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
  846. plugin->unlock();
  847. // if plugin has no audio inputs, add input buffer
  848. if (oldAudioInCount == 0)
  849. {
  850. carla_addFloats(outBufReal[0], inBuf0, frames);
  851. carla_addFloats(outBufReal[1], inBuf1, frames);
  852. }
  853. // if plugin only has 1 output, copy it to the 2nd
  854. if (oldAudioOutCount == 1)
  855. {
  856. carla_copyFloats(outBufReal[1], outBufReal[0], frames);
  857. }
  858. // set peaks
  859. {
  860. EnginePluginData& pluginData(data->plugins[i]);
  861. if (oldAudioInCount > 0)
  862. {
  863. pluginData.peaks[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
  864. pluginData.peaks[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
  865. }
  866. else
  867. {
  868. pluginData.peaks[0] = 0.0f;
  869. pluginData.peaks[1] = 0.0f;
  870. }
  871. if (oldAudioOutCount > 0)
  872. {
  873. pluginData.peaks[2] = carla_findMaxNormalizedFloat(outBufReal[0], frames);
  874. pluginData.peaks[3] = carla_findMaxNormalizedFloat(outBufReal[1], frames);
  875. }
  876. else
  877. {
  878. pluginData.peaks[2] = 0.0f;
  879. pluginData.peaks[3] = 0.0f;
  880. }
  881. }
  882. processed = true;
  883. }
  884. }
  885. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  886. {
  887. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  888. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  889. if (inBuf != nullptr && inputs > 0)
  890. {
  891. bool noConnections = true;
  892. // connect input buffers
  893. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  894. {
  895. const uint& port(it.getValue(0));
  896. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  897. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  898. if (noConnections)
  899. {
  900. carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  901. noConnections = false;
  902. }
  903. else
  904. {
  905. carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  906. }
  907. }
  908. if (noConnections)
  909. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  910. noConnections = true;
  911. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  912. {
  913. const uint& port(it.getValue(0));
  914. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  915. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  916. if (noConnections)
  917. {
  918. carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  919. noConnections = false;
  920. }
  921. else
  922. {
  923. carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  924. }
  925. }
  926. if (noConnections)
  927. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  928. }
  929. else
  930. {
  931. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  932. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  933. }
  934. carla_zeroFloats(audioBuffers.outBuf[0], frames);
  935. carla_zeroFloats(audioBuffers.outBuf[1], frames);
  936. // process
  937. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  938. // connect output buffers
  939. if (audioBuffers.connectedOut1.count() != 0)
  940. {
  941. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  942. {
  943. const uint& port(it.getValue(0));
  944. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  945. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  946. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
  947. }
  948. }
  949. if (audioBuffers.connectedOut2.count() != 0)
  950. {
  951. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  952. {
  953. const uint& port(it.getValue(0));
  954. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  955. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  956. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
  957. }
  958. }
  959. }
  960. // -----------------------------------------------------------------------
  961. // Patchbay Graph stuff
  962. static const uint32_t kMaxPortsPerPlugin = 255;
  963. static const uint32_t kAudioInputPortOffset = kMaxPortsPerPlugin*1;
  964. static const uint32_t kAudioOutputPortOffset = kMaxPortsPerPlugin*2;
  965. static const uint32_t kCVInputPortOffset = kMaxPortsPerPlugin*3;
  966. static const uint32_t kCVOutputPortOffset = kMaxPortsPerPlugin*4;
  967. static const uint32_t kMidiInputPortOffset = kMaxPortsPerPlugin*5;
  968. static const uint32_t kMidiOutputPortOffset = kMaxPortsPerPlugin*6;
  969. static const uint32_t kMaxPortOffset = kMaxPortsPerPlugin*7;
  970. static inline
  971. bool adjustPatchbayPortIdForWater(AudioProcessor::ChannelType& channelType, uint& portId)
  972. {
  973. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  974. CARLA_SAFE_ASSERT_RETURN(portId < kMaxPortOffset, false);
  975. if (portId >= kMidiOutputPortOffset)
  976. {
  977. portId -= kMidiOutputPortOffset;
  978. channelType = AudioProcessor::ChannelTypeMIDI;
  979. return true;
  980. }
  981. if (portId >= kMidiInputPortOffset)
  982. {
  983. portId -= kMidiInputPortOffset;
  984. channelType = AudioProcessor::ChannelTypeMIDI;
  985. return true;
  986. }
  987. if (portId >= kCVOutputPortOffset)
  988. {
  989. portId -= kCVOutputPortOffset;
  990. channelType = AudioProcessor::ChannelTypeCV;
  991. return true;
  992. }
  993. if (portId >= kCVInputPortOffset)
  994. {
  995. portId -= kCVInputPortOffset;
  996. channelType = AudioProcessor::ChannelTypeCV;
  997. return true;
  998. }
  999. if (portId >= kAudioOutputPortOffset)
  1000. {
  1001. portId -= kAudioOutputPortOffset;
  1002. channelType = AudioProcessor::ChannelTypeAudio;
  1003. return true;
  1004. }
  1005. if (portId >= kAudioInputPortOffset)
  1006. {
  1007. portId -= kAudioInputPortOffset;
  1008. channelType = AudioProcessor::ChannelTypeAudio;
  1009. return true;
  1010. }
  1011. return false;
  1012. }
  1013. static inline
  1014. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  1015. {
  1016. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  1017. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  1018. CARLA_SAFE_ASSERT_RETURN(portId < kMaxPortOffset, String());
  1019. String fullPortName(proc->getName());
  1020. /**/ if (portId >= kMidiOutputPortOffset)
  1021. {
  1022. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI) > 0, String());
  1023. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI,
  1024. portId-kMidiOutputPortOffset);
  1025. }
  1026. else if (portId >= kMidiInputPortOffset)
  1027. {
  1028. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI) > 0, String());
  1029. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI,
  1030. portId-kMidiInputPortOffset);
  1031. }
  1032. else if (portId >= kCVOutputPortOffset)
  1033. {
  1034. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV) > 0, String());
  1035. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeCV,
  1036. portId-kCVOutputPortOffset);
  1037. }
  1038. else if (portId >= kCVInputPortOffset)
  1039. {
  1040. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV) > 0, String());
  1041. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeCV,
  1042. portId-kCVInputPortOffset);
  1043. }
  1044. else if (portId >= kAudioOutputPortOffset)
  1045. {
  1046. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio) > 0, String());
  1047. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio,
  1048. portId-kAudioOutputPortOffset);
  1049. }
  1050. else if (portId >= kAudioInputPortOffset)
  1051. {
  1052. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio) > 0, String());
  1053. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeAudio,
  1054. portId-kAudioInputPortOffset);
  1055. }
  1056. else
  1057. {
  1058. return String();
  1059. }
  1060. return fullPortName;
  1061. }
  1062. static inline
  1063. void addNodeToPatchbay(const bool sendHost, const bool sendOSC, CarlaEngine* const engine,
  1064. AudioProcessorGraph::Node* const node, const int pluginId, const AudioProcessor* const proc)
  1065. {
  1066. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1067. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1068. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1069. const uint groupId = node->nodeId;
  1070. engine->callback(sendHost, sendOSC,
  1071. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  1072. groupId,
  1073. pluginId >= 0 ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE,
  1074. pluginId,
  1075. 0, 0.0f,
  1076. proc->getName().toRawUTF8());
  1077. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); i<numInputs; ++i)
  1078. {
  1079. engine->callback(sendHost, sendOSC,
  1080. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1081. groupId,
  1082. static_cast<int>(kAudioInputPortOffset+i),
  1083. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  1084. 0, 0.0f,
  1085. proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, i).toRawUTF8());
  1086. }
  1087. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); i<numOutputs; ++i)
  1088. {
  1089. engine->callback(sendHost, sendOSC,
  1090. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1091. groupId,
  1092. static_cast<int>(kAudioOutputPortOffset+i),
  1093. PATCHBAY_PORT_TYPE_AUDIO,
  1094. 0, 0.0f,
  1095. proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, i).toRawUTF8());
  1096. }
  1097. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); i<numInputs; ++i)
  1098. {
  1099. engine->callback(sendHost, sendOSC,
  1100. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1101. groupId,
  1102. static_cast<int>(kCVInputPortOffset+i),
  1103. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1104. 0, 0.0f,
  1105. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, i).toRawUTF8());
  1106. }
  1107. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); i<numOutputs; ++i)
  1108. {
  1109. engine->callback(sendHost, sendOSC,
  1110. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1111. groupId,
  1112. static_cast<int>(kCVOutputPortOffset+i),
  1113. PATCHBAY_PORT_TYPE_CV,
  1114. 0, 0.0f,
  1115. proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, i).toRawUTF8());
  1116. }
  1117. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); i<numInputs; ++i)
  1118. {
  1119. engine->callback(sendHost, sendOSC,
  1120. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1121. groupId,
  1122. static_cast<int>(kMidiInputPortOffset+i),
  1123. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT,
  1124. 0, 0.0f,
  1125. proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, i).toRawUTF8());
  1126. }
  1127. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); i<numOutputs; ++i)
  1128. {
  1129. engine->callback(sendHost, sendOSC,
  1130. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1131. groupId,
  1132. static_cast<int>(kMidiOutputPortOffset+i),
  1133. PATCHBAY_PORT_TYPE_MIDI,
  1134. 0, 0.0f,
  1135. proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, i).toRawUTF8());
  1136. }
  1137. if (node->properties.contains("x1"))
  1138. {
  1139. engine->callback(sendHost, sendOSC,
  1140. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1141. groupId,
  1142. node->properties.getWithDefault("x1", 0),
  1143. node->properties.getWithDefault("y1", 0),
  1144. node->properties.getWithDefault("x2", 0),
  1145. static_cast<float>(node->properties.getWithDefault("y2", 0)),
  1146. nullptr);
  1147. }
  1148. }
  1149. static inline
  1150. void removeNodeFromPatchbay(const bool sendHost, const bool sendOSC, CarlaEngine* const engine,
  1151. const uint32_t groupId, const AudioProcessor* const proc)
  1152. {
  1153. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1154. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1155. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); i<numInputs; ++i)
  1156. {
  1157. engine->callback(sendHost, sendOSC,
  1158. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1159. groupId,
  1160. static_cast<int>(kAudioInputPortOffset+i),
  1161. 0, 0, 0.0f, nullptr);
  1162. }
  1163. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); i<numOutputs; ++i)
  1164. {
  1165. engine->callback(sendHost, sendOSC,
  1166. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1167. groupId,
  1168. static_cast<int>(kAudioOutputPortOffset+i),
  1169. 0, 0, 0.0f,
  1170. nullptr);
  1171. }
  1172. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); i<numInputs; ++i)
  1173. {
  1174. engine->callback(sendHost, sendOSC,
  1175. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1176. groupId,
  1177. static_cast<int>(kCVInputPortOffset+i),
  1178. 0, 0, 0.0f, nullptr);
  1179. }
  1180. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); i<numOutputs; ++i)
  1181. {
  1182. engine->callback(sendHost, sendOSC,
  1183. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1184. groupId,
  1185. static_cast<int>(kCVOutputPortOffset+i),
  1186. 0, 0, 0.0f,
  1187. nullptr);
  1188. }
  1189. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); i<numInputs; ++i)
  1190. {
  1191. engine->callback(sendHost, sendOSC,
  1192. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1193. groupId,
  1194. static_cast<int>(kMidiInputPortOffset+i),
  1195. 0, 0, 0.0f, nullptr);
  1196. }
  1197. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); i<numOutputs; ++i)
  1198. {
  1199. engine->callback(sendHost, sendOSC,
  1200. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1201. groupId,
  1202. static_cast<int>(kMidiOutputPortOffset+i),
  1203. 0, 0, 0.0f, nullptr);
  1204. }
  1205. engine->callback(sendHost, sendOSC,
  1206. ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED,
  1207. groupId,
  1208. 0, 0, 0, 0.0f, nullptr);
  1209. }
  1210. // -----------------------------------------------------------------------
  1211. class CarlaPluginInstance : public AudioProcessor
  1212. {
  1213. public:
  1214. CarlaPluginInstance(CarlaEngine* const engine, const CarlaPluginPtr plugin)
  1215. : kEngine(engine),
  1216. fPlugin(plugin)
  1217. {
  1218. CarlaEngineClient* const client = plugin->getEngineClient();
  1219. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1220. client->getPortCount(kEnginePortTypeAudio, false),
  1221. client->getPortCount(kEnginePortTypeCV, true),
  1222. client->getPortCount(kEnginePortTypeCV, false),
  1223. client->getPortCount(kEnginePortTypeEvent, true),
  1224. client->getPortCount(kEnginePortTypeEvent, false),
  1225. getSampleRate(), getBlockSize());
  1226. }
  1227. ~CarlaPluginInstance() override
  1228. {
  1229. }
  1230. void reconfigure() override
  1231. {
  1232. CARLA_SAFE_ASSERT_RETURN(fPlugin.get() != nullptr,);
  1233. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1234. CARLA_SAFE_ASSERT_RETURN(client != nullptr,);
  1235. carla_stdout("reconfigure called");
  1236. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1237. client->getPortCount(kEnginePortTypeAudio, false),
  1238. client->getPortCount(kEnginePortTypeCV, true),
  1239. client->getPortCount(kEnginePortTypeCV, false),
  1240. client->getPortCount(kEnginePortTypeEvent, true),
  1241. client->getPortCount(kEnginePortTypeEvent, false),
  1242. getSampleRate(), getBlockSize());
  1243. }
  1244. void invalidatePlugin() noexcept
  1245. {
  1246. fPlugin.reset();
  1247. }
  1248. // -------------------------------------------------------------------
  1249. const String getName() const override
  1250. {
  1251. return fPlugin->getName();
  1252. }
  1253. void processBlockWithCV(AudioSampleBuffer& audio,
  1254. const AudioSampleBuffer& cvIn,
  1255. AudioSampleBuffer& cvOut,
  1256. MidiBuffer& midi) override
  1257. {
  1258. if (fPlugin.get() == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  1259. {
  1260. audio.clear();
  1261. cvOut.clear();
  1262. midi.clear();
  1263. return;
  1264. }
  1265. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  1266. {
  1267. EngineEvent* const engineEvents(port->fBuffer);
  1268. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1269. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1270. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  1271. }
  1272. midi.clear();
  1273. fPlugin->initBuffers();
  1274. const uint32_t numSamples = audio.getNumSamples();
  1275. const uint32_t numAudioChan = audio.getNumChannels();
  1276. const uint32_t numCVInChan = cvIn.getNumChannels();
  1277. const uint32_t numCVOutChan = cvOut.getNumChannels();
  1278. if (numAudioChan+numCVInChan+numCVOutChan == 0)
  1279. {
  1280. // nothing to process
  1281. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  1282. }
  1283. else if (numAudioChan != 0)
  1284. {
  1285. // processing audio, include code for peaks
  1286. const uint32_t numChan2 = jmin(numAudioChan, 2U);
  1287. if (fPlugin->getAudioInCount() == 0)
  1288. audio.clear();
  1289. float* audioBuffers[numAudioChan];
  1290. float* cvOutBuffers[numCVOutChan];
  1291. const float* cvInBuffers[numCVInChan];
  1292. for (uint32_t i=0; i<numAudioChan; ++i)
  1293. audioBuffers[i] = audio.getWritePointer(i);
  1294. for (uint32_t i=0; i<numCVOutChan; ++i)
  1295. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1296. for (uint32_t i=0; i<numCVInChan; ++i)
  1297. cvInBuffers[i] = cvIn.getReadPointer(i);
  1298. float inPeaks[2] = { 0.0f };
  1299. float outPeaks[2] = { 0.0f };
  1300. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChan2); i<count; ++i)
  1301. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1302. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers,
  1303. cvInBuffers, cvOutBuffers,
  1304. numSamples);
  1305. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChan2); i<count; ++i)
  1306. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1307. kEngine->setPluginPeaksRT(fPlugin->getId(), inPeaks, outPeaks);
  1308. }
  1309. else
  1310. {
  1311. // processing CV only, skip audiopeaks
  1312. float* cvOutBuffers[numCVOutChan];
  1313. const float* cvInBuffers[numCVInChan];
  1314. for (uint32_t i=0; i<numCVOutChan; ++i)
  1315. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1316. for (uint32_t i=0; i<numCVInChan; ++i)
  1317. cvInBuffers[i] = cvIn.getReadPointer(i);
  1318. fPlugin->process(nullptr, nullptr,
  1319. cvInBuffers, cvOutBuffers,
  1320. numSamples);
  1321. }
  1322. midi.clear();
  1323. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  1324. {
  1325. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  1326. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1327. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1328. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1329. }
  1330. fPlugin->unlock();
  1331. }
  1332. const String getInputChannelName(ChannelType t, uint i) const override
  1333. {
  1334. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1335. switch (t)
  1336. {
  1337. case ChannelTypeAudio:
  1338. return client->getAudioPortName(true, i);
  1339. case ChannelTypeCV:
  1340. return client->getCVPortName(true, i);
  1341. case ChannelTypeMIDI:
  1342. return client->getEventPortName(true, i);
  1343. }
  1344. return String();
  1345. }
  1346. const String getOutputChannelName(ChannelType t, uint i) const override
  1347. {
  1348. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1349. switch (t)
  1350. {
  1351. case ChannelTypeAudio:
  1352. return client->getAudioPortName(false, i);
  1353. case ChannelTypeCV:
  1354. return client->getCVPortName(false, i);
  1355. case ChannelTypeMIDI:
  1356. return client->getEventPortName(false, i);
  1357. }
  1358. return String();
  1359. }
  1360. void prepareToPlay(double, int) override {}
  1361. void releaseResources() override {}
  1362. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1363. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1364. private:
  1365. CarlaEngine* const kEngine;
  1366. CarlaPluginPtr fPlugin;
  1367. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1368. };
  1369. // -----------------------------------------------------------------------
  1370. // Patchbay Graph
  1371. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1372. {
  1373. public:
  1374. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1375. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1376. inputNames(),
  1377. outputNames() {}
  1378. const String getInputChannelName (ChannelType, uint _index) const override
  1379. {
  1380. const int index = static_cast<int>(_index); // FIXME
  1381. if (index < inputNames.size())
  1382. return inputNames[index];
  1383. return String("Playback ") + String(index+1);
  1384. }
  1385. const String getOutputChannelName (ChannelType, uint _index) const override
  1386. {
  1387. const int index = static_cast<int>(_index); // FIXME
  1388. if (index < outputNames.size())
  1389. return outputNames[index];
  1390. return String("Capture ") + String(index+1);
  1391. }
  1392. void setNames(const bool setInputNames, const StringArray& names)
  1393. {
  1394. if (setInputNames)
  1395. inputNames = names;
  1396. else
  1397. outputNames = names;
  1398. }
  1399. private:
  1400. StringArray inputNames;
  1401. StringArray outputNames;
  1402. };
  1403. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine,
  1404. const uint32_t audioIns, const uint32_t audioOuts,
  1405. const uint32_t cvIns, const uint32_t cvOuts)
  1406. : CarlaThread("PatchbayReorderThread"),
  1407. connections(),
  1408. graph(),
  1409. audioBuffer(),
  1410. cvInBuffer(),
  1411. cvOutBuffer(),
  1412. midiBuffer(),
  1413. numAudioIns(carla_fixedValue(0U, 64U, audioIns)),
  1414. numAudioOuts(carla_fixedValue(0U, 64U, audioOuts)),
  1415. numCVIns(carla_fixedValue(0U, 8U, cvIns)),
  1416. numCVOuts(carla_fixedValue(0U, 8U, cvOuts)),
  1417. retCon(),
  1418. usingExternalHost(false),
  1419. usingExternalOSC(false),
  1420. extGraph(engine),
  1421. kEngine(engine)
  1422. {
  1423. const uint32_t bufferSize(engine->getBufferSize());
  1424. const double sampleRate(engine->getSampleRate());
  1425. graph.setPlayConfigDetails(numAudioIns, numAudioOuts,
  1426. numCVIns, numCVOuts,
  1427. 1, 1,
  1428. sampleRate, static_cast<int>(bufferSize));
  1429. graph.prepareToPlay(sampleRate, static_cast<int>(bufferSize));
  1430. audioBuffer.setSize(jmax(numAudioIns, numAudioOuts), bufferSize);
  1431. cvInBuffer.setSize(numCVIns, bufferSize);
  1432. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1433. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1434. midiBuffer.clear();
  1435. StringArray channelNames;
  1436. switch (numAudioIns)
  1437. {
  1438. case 2:
  1439. channelNames.add("Left");
  1440. channelNames.add("Right");
  1441. break;
  1442. case 3:
  1443. channelNames.add("Left");
  1444. channelNames.add("Right");
  1445. channelNames.add("Sidechain");
  1446. break;
  1447. }
  1448. if (numAudioIns != 0)
  1449. {
  1450. NamedAudioGraphIOProcessor* const proc(
  1451. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1452. proc->setNames(false, channelNames);
  1453. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1454. node->properties.set("isPlugin", false);
  1455. node->properties.set("isOutput", false);
  1456. node->properties.set("isAudio", true);
  1457. node->properties.set("isCV", false);
  1458. node->properties.set("isMIDI", false);
  1459. node->properties.set("isOSC", false);
  1460. }
  1461. if (numAudioOuts != 0)
  1462. {
  1463. NamedAudioGraphIOProcessor* const proc(
  1464. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1465. proc->setNames(true, channelNames);
  1466. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1467. node->properties.set("isPlugin", false);
  1468. node->properties.set("isOutput", false);
  1469. node->properties.set("isAudio", true);
  1470. node->properties.set("isCV", false);
  1471. node->properties.set("isMIDI", false);
  1472. node->properties.set("isOSC", false);
  1473. }
  1474. if (numCVIns != 0)
  1475. {
  1476. NamedAudioGraphIOProcessor* const proc(
  1477. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvInputNode));
  1478. // proc->setNames(false, channelNames);
  1479. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1480. node->properties.set("isPlugin", false);
  1481. node->properties.set("isOutput", false);
  1482. node->properties.set("isAudio", false);
  1483. node->properties.set("isCV", true);
  1484. node->properties.set("isMIDI", false);
  1485. node->properties.set("isOSC", false);
  1486. }
  1487. if (numCVOuts != 0)
  1488. {
  1489. NamedAudioGraphIOProcessor* const proc(
  1490. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvOutputNode));
  1491. // proc->setNames(true, channelNames);
  1492. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1493. node->properties.set("isPlugin", false);
  1494. node->properties.set("isOutput", false);
  1495. node->properties.set("isAudio", false);
  1496. node->properties.set("isCV", true);
  1497. node->properties.set("isMIDI", false);
  1498. node->properties.set("isOSC", false);
  1499. }
  1500. {
  1501. NamedAudioGraphIOProcessor* const proc(
  1502. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1503. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1504. node->properties.set("isPlugin", false);
  1505. node->properties.set("isOutput", false);
  1506. node->properties.set("isAudio", false);
  1507. node->properties.set("isCV", false);
  1508. node->properties.set("isMIDI", true);
  1509. node->properties.set("isOSC", false);
  1510. }
  1511. {
  1512. NamedAudioGraphIOProcessor* const proc(
  1513. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1514. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1515. node->properties.set("isPlugin", false);
  1516. node->properties.set("isOutput", true);
  1517. node->properties.set("isAudio", false);
  1518. node->properties.set("isCV", false);
  1519. node->properties.set("isMIDI", true);
  1520. node->properties.set("isOSC", false);
  1521. }
  1522. startThread();
  1523. }
  1524. PatchbayGraph::~PatchbayGraph()
  1525. {
  1526. stopThread(-1);
  1527. connections.clear();
  1528. extGraph.clear();
  1529. graph.releaseResources();
  1530. graph.clear();
  1531. audioBuffer.clear();
  1532. cvInBuffer.clear();
  1533. cvOutBuffer.clear();
  1534. }
  1535. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1536. {
  1537. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1538. graph.releaseResources();
  1539. graph.prepareToPlay(kEngine->getSampleRate(), static_cast<int>(bufferSize));
  1540. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  1541. cvInBuffer.setSize(numCVIns, bufferSize);
  1542. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1543. }
  1544. void PatchbayGraph::setSampleRate(const double sampleRate)
  1545. {
  1546. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1547. graph.releaseResources();
  1548. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1549. }
  1550. void PatchbayGraph::setOffline(const bool offline)
  1551. {
  1552. graph.setNonRealtime(offline);
  1553. }
  1554. void PatchbayGraph::addPlugin(const CarlaPluginPtr plugin)
  1555. {
  1556. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1557. carla_debug("PatchbayGraph::addPlugin(%p)", plugin.get());
  1558. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1559. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1560. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1561. const bool sendHost = !usingExternalHost;
  1562. const bool sendOSC = !usingExternalOSC;
  1563. plugin->setPatchbayNodeId(node->nodeId);
  1564. node->properties.set("isPlugin", true);
  1565. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1566. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(plugin->getId()), instance);
  1567. }
  1568. void PatchbayGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  1569. {
  1570. CARLA_SAFE_ASSERT_RETURN(oldPlugin.get() != nullptr,);
  1571. CARLA_SAFE_ASSERT_RETURN(newPlugin.get() != nullptr,);
  1572. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1573. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1574. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1575. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1576. const bool sendHost = !usingExternalHost;
  1577. const bool sendOSC = !usingExternalOSC;
  1578. disconnectInternalGroup(oldNode->nodeId);
  1579. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, oldNode->nodeId, oldNode->getProcessor());
  1580. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1581. graph.removeNode(oldNode->nodeId);
  1582. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1583. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1584. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1585. newPlugin->setPatchbayNodeId(node->nodeId);
  1586. node->properties.set("isPlugin", true);
  1587. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1588. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(newPlugin->getId()), instance);
  1589. }
  1590. void PatchbayGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  1591. {
  1592. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1593. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin.get(), newName);
  1594. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1595. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1596. const bool sendHost = !usingExternalHost;
  1597. const bool sendOSC = !usingExternalOSC;
  1598. kEngine->callback(sendHost, sendOSC,
  1599. ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED,
  1600. node->nodeId,
  1601. 0, 0, 0, 0.0f,
  1602. newName);
  1603. }
  1604. void PatchbayGraph::switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB)
  1605. {
  1606. CARLA_SAFE_ASSERT_RETURN(pluginA.get() != nullptr,);
  1607. CARLA_SAFE_ASSERT_RETURN(pluginB.get() != nullptr,);
  1608. CARLA_SAFE_ASSERT_RETURN(pluginA != pluginB,);
  1609. CARLA_SAFE_ASSERT_RETURN(pluginA->getId() != pluginB->getId(),);
  1610. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(pluginA->getPatchbayNodeId()));
  1611. CARLA_SAFE_ASSERT_RETURN(nodeA != nullptr,);
  1612. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(pluginB->getPatchbayNodeId()));
  1613. CARLA_SAFE_ASSERT_RETURN(nodeB != nullptr,);
  1614. nodeA->properties.set("pluginId", static_cast<int>(pluginB->getId()));
  1615. nodeB->properties.set("pluginId", static_cast<int>(pluginA->getId()));
  1616. }
  1617. void PatchbayGraph::reconfigureForCV(const CarlaPluginPtr plugin, const uint portIndex, bool added)
  1618. {
  1619. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1620. carla_debug("PatchbayGraph::reconfigureForCV(%p, %u, %s)", plugin.get(), portIndex, bool2str(added));
  1621. AudioProcessorGraph::Node* const node = graph.getNodeForId(plugin->getPatchbayNodeId());
  1622. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1623. CarlaPluginInstance* const proc = dynamic_cast<CarlaPluginInstance*>(node->getProcessor());
  1624. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1625. const bool sendHost = !usingExternalHost;
  1626. const bool sendOSC = !usingExternalOSC;
  1627. const uint oldCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1628. // const uint oldCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1629. {
  1630. const CarlaRecursiveMutexLocker crml(graph.getCallbackLock());
  1631. proc->reconfigure();
  1632. graph.buildRenderingSequence();
  1633. }
  1634. const uint newCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1635. // const uint newCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1636. if (added)
  1637. {
  1638. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn > oldCvIn, newCvIn, oldCvIn,);
  1639. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut >= oldCvOut, newCvOut, oldCvOut,);
  1640. kEngine->callback(sendHost, sendOSC,
  1641. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1642. node->nodeId,
  1643. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1644. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1645. 0, 0.0f,
  1646. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, portIndex).toRawUTF8());
  1647. }
  1648. else
  1649. {
  1650. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn < oldCvIn, newCvIn, oldCvIn,);
  1651. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut <= oldCvOut, newCvOut, oldCvOut,);
  1652. kEngine->callback(sendHost, sendOSC,
  1653. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1654. node->nodeId,
  1655. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1656. 0, 0, 0.0f, nullptr);
  1657. }
  1658. }
  1659. void PatchbayGraph::removePlugin(const CarlaPluginPtr plugin)
  1660. {
  1661. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1662. carla_debug("PatchbayGraph::removePlugin(%p)", plugin.get());
  1663. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1664. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1665. const bool sendHost = !usingExternalHost;
  1666. const bool sendOSC = !usingExternalOSC;
  1667. disconnectInternalGroup(node->nodeId);
  1668. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1669. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1670. // Fix plugin Ids properties
  1671. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1672. {
  1673. const CarlaPluginPtr plugin2 = kEngine->getPlugin(i);
  1674. CARLA_SAFE_ASSERT_BREAK(plugin2.get() != nullptr);
  1675. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1676. {
  1677. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1678. node2->properties.set("pluginId", static_cast<int>(i-1));
  1679. }
  1680. }
  1681. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1682. }
  1683. void PatchbayGraph::removeAllPlugins()
  1684. {
  1685. carla_debug("PatchbayGraph::removeAllPlugins()");
  1686. const bool sendHost = !usingExternalHost;
  1687. const bool sendOSC = !usingExternalOSC;
  1688. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1689. {
  1690. const CarlaPluginPtr plugin = kEngine->getPlugin(i);
  1691. CARLA_SAFE_ASSERT_CONTINUE(plugin.get() != nullptr);
  1692. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1693. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1694. disconnectInternalGroup(node->nodeId);
  1695. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1696. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1697. graph.removeNode(node->nodeId);
  1698. }
  1699. }
  1700. bool PatchbayGraph::connect(const bool external,
  1701. const uint groupA, const uint portA, const uint groupB, const uint portB)
  1702. {
  1703. if (external)
  1704. return extGraph.connect(usingExternalHost, usingExternalOSC, groupA, portA, groupB, portB);
  1705. uint adjustedPortA = portA;
  1706. uint adjustedPortB = portB;
  1707. AudioProcessor::ChannelType channelType;
  1708. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1709. return false;
  1710. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1711. return false;
  1712. if (! graph.addConnection(channelType, groupA, adjustedPortA, groupB, adjustedPortB))
  1713. {
  1714. kEngine->setLastError("Failed from water");
  1715. return false;
  1716. }
  1717. ConnectionToId connectionToId;
  1718. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1719. char strBuf[STR_MAX+1];
  1720. strBuf[STR_MAX] = '\0';
  1721. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1722. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1723. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1724. connectionToId.id,
  1725. 0, 0, 0, 0.0f,
  1726. strBuf);
  1727. connections.list.append(connectionToId);
  1728. return true;
  1729. }
  1730. bool PatchbayGraph::disconnect(const bool external, const uint connectionId)
  1731. {
  1732. if (external)
  1733. return extGraph.disconnect(usingExternalHost, usingExternalOSC, connectionId);
  1734. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1735. {
  1736. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1737. const ConnectionToId& connectionToId(it.getValue(fallback));
  1738. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1739. if (connectionToId.id != connectionId)
  1740. continue;
  1741. uint adjustedPortA = connectionToId.portA;
  1742. uint adjustedPortB = connectionToId.portB;
  1743. AudioProcessor::ChannelType channelType;
  1744. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1745. return false;
  1746. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1747. return false;
  1748. if (! graph.removeConnection(channelType,
  1749. connectionToId.groupA, adjustedPortA,
  1750. connectionToId.groupB, adjustedPortB))
  1751. return false;
  1752. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1753. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1754. connectionToId.id,
  1755. 0, 0, 0, 0.0f,
  1756. nullptr);
  1757. connections.list.remove(it);
  1758. return true;
  1759. }
  1760. kEngine->setLastError("Failed to find connection");
  1761. return false;
  1762. }
  1763. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1764. {
  1765. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1766. {
  1767. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1768. const ConnectionToId& connectionToId(it.getValue(fallback));
  1769. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1770. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1771. continue;
  1772. /*
  1773. uint adjustedPortA = connectionToId.portA;
  1774. uint adjustedPortB = connectionToId.portB;
  1775. AudioProcessor::ChannelType channelType;
  1776. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1777. return false;
  1778. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1779. return false;
  1780. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1781. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1782. */
  1783. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1784. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1785. connectionToId.id,
  1786. 0, 0, 0, 0.0f,
  1787. nullptr);
  1788. connections.list.remove(it);
  1789. }
  1790. }
  1791. void PatchbayGraph::setGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  1792. uint groupId, int x1, int y1, int x2, int y2)
  1793. {
  1794. if (external)
  1795. return extGraph.setGroupPos(sendHost, sendOSC, groupId, x1, y1, x2, y2);
  1796. AudioProcessorGraph::Node* const node(graph.getNodeForId(groupId));
  1797. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1798. node->properties.set("x1", x1);
  1799. node->properties.set("y1", y1);
  1800. node->properties.set("x2", x2);
  1801. node->properties.set("y2", y2);
  1802. kEngine->callback(sendHost, sendOSC,
  1803. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1804. groupId, x1, y1, x2, static_cast<float>(y2),
  1805. nullptr);
  1806. }
  1807. void PatchbayGraph::refresh(const bool sendHost, const bool sendOSC, const bool external, const char* const deviceName)
  1808. {
  1809. if (external)
  1810. return extGraph.refresh(sendHost, sendOSC, deviceName);
  1811. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1812. connections.clear();
  1813. graph.removeIllegalConnections();
  1814. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1815. {
  1816. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1817. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1818. AudioProcessor* const proc(node->getProcessor());
  1819. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1820. int pluginId = -1;
  1821. // plugin node
  1822. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1823. pluginId = node->properties.getWithDefault("pluginId", -1);
  1824. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, pluginId, proc);
  1825. }
  1826. char strBuf[STR_MAX+1];
  1827. strBuf[STR_MAX] = '\0';
  1828. for (size_t i=0, count=graph.getNumConnections(); i<count; ++i)
  1829. {
  1830. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1831. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1832. const uint groupA = conn->sourceNodeId;
  1833. const uint groupB = conn->destNodeId;
  1834. uint portA = conn->sourceChannelIndex;
  1835. uint portB = conn->destChannelIndex;
  1836. switch (conn->channelType)
  1837. {
  1838. case AudioProcessor::ChannelTypeAudio:
  1839. portA += kAudioOutputPortOffset;
  1840. portB += kAudioInputPortOffset;
  1841. break;
  1842. case AudioProcessor::ChannelTypeCV:
  1843. portA += kCVOutputPortOffset;
  1844. portB += kCVInputPortOffset;
  1845. break;
  1846. case AudioProcessor::ChannelTypeMIDI:
  1847. portA += kMidiOutputPortOffset;
  1848. portB += kMidiInputPortOffset;
  1849. break;
  1850. }
  1851. ConnectionToId connectionToId;
  1852. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1853. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1854. kEngine->callback(sendHost, sendOSC,
  1855. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1856. connectionToId.id,
  1857. 0, 0, 0, 0.0f,
  1858. strBuf);
  1859. connections.list.append(connectionToId);
  1860. }
  1861. }
  1862. const char* const* PatchbayGraph::getConnections(const bool external) const
  1863. {
  1864. if (external)
  1865. return extGraph.getConnections();
  1866. if (connections.list.count() == 0)
  1867. return nullptr;
  1868. CarlaStringList connList;
  1869. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1870. {
  1871. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1872. const ConnectionToId& connectionToId(it.getValue(fallback));
  1873. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1874. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1875. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1876. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1877. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1878. AudioProcessor* const procA(nodeA->getProcessor());
  1879. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1880. AudioProcessor* const procB(nodeB->getProcessor());
  1881. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1882. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1883. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1884. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1885. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1886. connList.append(fullPortNameA.toRawUTF8());
  1887. connList.append(fullPortNameB.toRawUTF8());
  1888. }
  1889. if (connList.count() == 0)
  1890. return nullptr;
  1891. retCon = connList.toCharStringListPtr();
  1892. return retCon;
  1893. }
  1894. const CarlaEngine::PatchbayPosition* PatchbayGraph::getPositions(bool external, uint& count) const
  1895. {
  1896. CarlaEngine::PatchbayPosition* ret;
  1897. if (external)
  1898. {
  1899. try {
  1900. ret = new CarlaEngine::PatchbayPosition[kExternalGraphGroupMax];
  1901. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1902. count = 0;
  1903. for (uint i=kExternalGraphGroupCarla; i<kExternalGraphGroupMax; ++i)
  1904. {
  1905. const PatchbayPosition& eppos(extGraph.positions[i]);
  1906. if (! eppos.active)
  1907. continue;
  1908. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1909. switch (i)
  1910. {
  1911. case kExternalGraphGroupCarla:
  1912. ppos.name = "Carla";
  1913. break;
  1914. case kExternalGraphGroupAudioIn:
  1915. ppos.name = "AudioIn";
  1916. break;
  1917. case kExternalGraphGroupAudioOut:
  1918. ppos.name = "AudioOut";
  1919. break;
  1920. case kExternalGraphGroupMidiIn:
  1921. ppos.name = "MidiIn";
  1922. break;
  1923. case kExternalGraphGroupMidiOut:
  1924. ppos.name = "MidiOut";
  1925. break;
  1926. }
  1927. ppos.dealloc = false;
  1928. ppos.pluginId = -1;
  1929. ppos.x1 = eppos.x1;
  1930. ppos.y1 = eppos.y1;
  1931. ppos.x2 = eppos.x2;
  1932. ppos.y2 = eppos.y2;
  1933. }
  1934. return ret;
  1935. }
  1936. else
  1937. {
  1938. const int numNodes = graph.getNumNodes();
  1939. CARLA_SAFE_ASSERT_RETURN(numNodes > 0, nullptr);
  1940. try {
  1941. ret = new CarlaEngine::PatchbayPosition[numNodes];
  1942. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1943. count = 0;
  1944. for (int i=numNodes; --i >= 0;)
  1945. {
  1946. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1947. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1948. if (! node->properties.contains("x1"))
  1949. continue;
  1950. AudioProcessor* const proc(node->getProcessor());
  1951. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1952. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1953. ppos.name = carla_strdup(proc->getName().toRawUTF8());
  1954. ppos.dealloc = true;
  1955. ppos.pluginId = node->properties.getWithDefault("pluginId", -1);
  1956. ppos.x1 = node->properties.getWithDefault("x1", 0);
  1957. ppos.y1 = node->properties.getWithDefault("y1", 0);
  1958. ppos.x2 = node->properties.getWithDefault("x2", 0);
  1959. ppos.y2 = node->properties.getWithDefault("y2", 0);
  1960. }
  1961. return ret;
  1962. }
  1963. }
  1964. bool PatchbayGraph::getGroupFromName(bool external, const char* groupName, uint& groupId) const
  1965. {
  1966. if (external)
  1967. return extGraph.getGroupFromName(groupName, groupId);
  1968. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1969. {
  1970. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1971. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1972. AudioProcessor* const proc(node->getProcessor());
  1973. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1974. if (proc->getName() != groupName)
  1975. continue;
  1976. groupId = node->nodeId;
  1977. return true;
  1978. }
  1979. return false;
  1980. }
  1981. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1982. {
  1983. if (external)
  1984. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1985. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1986. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1987. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1988. {
  1989. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1990. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1991. AudioProcessor* const proc(node->getProcessor());
  1992. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1993. if (proc->getName() != groupName)
  1994. continue;
  1995. groupId = node->nodeId;
  1996. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); j<numInputs; ++j)
  1997. {
  1998. if (proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  1999. continue;
  2000. portId = kAudioInputPortOffset+j;
  2001. return true;
  2002. }
  2003. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); j<numOutputs; ++j)
  2004. {
  2005. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  2006. continue;
  2007. portId = kAudioOutputPortOffset+j;
  2008. return true;
  2009. }
  2010. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); j<numInputs; ++j)
  2011. {
  2012. if (proc->getInputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2013. continue;
  2014. portId = kCVInputPortOffset+j;
  2015. return true;
  2016. }
  2017. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); j<numOutputs; ++j)
  2018. {
  2019. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2020. continue;
  2021. portId = kCVOutputPortOffset+j;
  2022. return true;
  2023. }
  2024. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); j<numInputs; ++j)
  2025. {
  2026. if (proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2027. continue;
  2028. portId = kMidiInputPortOffset+j;
  2029. return true;
  2030. }
  2031. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); j<numOutputs; ++j)
  2032. {
  2033. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2034. continue;
  2035. portId = kMidiOutputPortOffset+j;
  2036. return true;
  2037. }
  2038. }
  2039. return false;
  2040. }
  2041. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data,
  2042. const float* const* const inBuf,
  2043. float* const* const outBuf,
  2044. const uint32_t frames)
  2045. {
  2046. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  2047. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  2048. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  2049. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  2050. // put events in water buffer
  2051. {
  2052. midiBuffer.clear();
  2053. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  2054. }
  2055. // set audio and cv buffer size, needed for water internals
  2056. if (! audioBuffer.setSizeRT(frames))
  2057. return;
  2058. if (! cvInBuffer.setSizeRT(frames))
  2059. return;
  2060. if (! cvOutBuffer.setSizeRT(frames))
  2061. return;
  2062. // put carla audio and cv in water buffer
  2063. {
  2064. uint32_t i=0;
  2065. for (; i < numAudioIns; ++i) {
  2066. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2067. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  2068. }
  2069. for (uint32_t j=0; j < numCVIns; ++j, ++i) {
  2070. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2071. cvInBuffer.copyFrom(j, 0, inBuf[i], frames);
  2072. }
  2073. // clear remaining channels
  2074. for (uint32_t j=numAudioIns, count=audioBuffer.getNumChannels(); j < count; ++j)
  2075. audioBuffer.clear(j, 0, frames);
  2076. for (uint32_t j=0; j < numCVOuts; ++j)
  2077. cvOutBuffer.clear(j, 0, frames);
  2078. }
  2079. // ready to go!
  2080. graph.processBlockWithCV(audioBuffer, cvInBuffer, cvOutBuffer, midiBuffer);
  2081. // put water audio and cv in carla buffer
  2082. {
  2083. uint32_t i=0;
  2084. for (; i < numAudioOuts; ++i)
  2085. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  2086. for (uint32_t j=0; j < numCVOuts; ++j, ++i)
  2087. carla_copyFloats(outBuf[i], cvOutBuffer.getReadPointer(j), frames);
  2088. }
  2089. // put water events in carla buffer
  2090. {
  2091. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  2092. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  2093. midiBuffer.clear();
  2094. }
  2095. }
  2096. void PatchbayGraph::run()
  2097. {
  2098. while (! shouldThreadExit())
  2099. {
  2100. carla_msleep(100);
  2101. graph.reorderNowIfNeeded();
  2102. }
  2103. }
  2104. // -----------------------------------------------------------------------
  2105. // InternalGraph
  2106. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  2107. : fIsRack(false),
  2108. fNumAudioOuts(0),
  2109. fIsReady(false),
  2110. fRack(nullptr),
  2111. kEngine(engine) {}
  2112. EngineInternalGraph::~EngineInternalGraph() noexcept
  2113. {
  2114. CARLA_SAFE_ASSERT(! fIsReady);
  2115. CARLA_SAFE_ASSERT(fRack == nullptr);
  2116. }
  2117. void EngineInternalGraph::create(const uint32_t audioIns, const uint32_t audioOuts,
  2118. const uint32_t cvIns, const uint32_t cvOuts)
  2119. {
  2120. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  2121. if (fIsRack)
  2122. {
  2123. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  2124. fRack = new RackGraph(kEngine, audioIns, audioOuts);
  2125. }
  2126. else
  2127. {
  2128. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  2129. fPatchbay = new PatchbayGraph(kEngine, audioIns, audioOuts, cvIns, cvOuts);
  2130. }
  2131. fNumAudioOuts = audioOuts;
  2132. fIsReady = true;
  2133. }
  2134. void EngineInternalGraph::destroy() noexcept
  2135. {
  2136. if (! fIsReady)
  2137. {
  2138. CARLA_SAFE_ASSERT(fRack == nullptr);
  2139. return;
  2140. }
  2141. if (fIsRack)
  2142. {
  2143. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2144. delete fRack;
  2145. fRack = nullptr;
  2146. }
  2147. else
  2148. {
  2149. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2150. delete fPatchbay;
  2151. fPatchbay = nullptr;
  2152. }
  2153. fIsReady = false;
  2154. fNumAudioOuts = 0;
  2155. }
  2156. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  2157. {
  2158. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2159. if (fIsRack)
  2160. {
  2161. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2162. fRack->setBufferSize(bufferSize);
  2163. }
  2164. else
  2165. {
  2166. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2167. fPatchbay->setBufferSize(bufferSize);
  2168. }
  2169. }
  2170. void EngineInternalGraph::setSampleRate(const double sampleRate)
  2171. {
  2172. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2173. if (fIsRack)
  2174. {
  2175. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2176. }
  2177. else
  2178. {
  2179. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2180. fPatchbay->setSampleRate(sampleRate);
  2181. }
  2182. }
  2183. void EngineInternalGraph::setOffline(const bool offline)
  2184. {
  2185. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2186. if (fIsRack)
  2187. {
  2188. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2189. fRack->setOffline(offline);
  2190. }
  2191. else
  2192. {
  2193. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2194. fPatchbay->setOffline(offline);
  2195. }
  2196. }
  2197. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  2198. {
  2199. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  2200. return fRack;
  2201. }
  2202. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  2203. {
  2204. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  2205. return fPatchbay;
  2206. }
  2207. PatchbayGraph* EngineInternalGraph::getPatchbayGraphOrNull() const noexcept
  2208. {
  2209. return fIsRack ? nullptr : fPatchbay;
  2210. }
  2211. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  2212. {
  2213. if (fIsRack)
  2214. {
  2215. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2216. fRack->processHelper(data, inBuf, outBuf, frames);
  2217. }
  2218. else
  2219. {
  2220. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2221. fPatchbay->process(data, inBuf, outBuf, frames);
  2222. }
  2223. }
  2224. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  2225. {
  2226. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  2227. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2228. fRack->process(data, inBuf, outBuf, frames);
  2229. }
  2230. // -----------------------------------------------------------------------
  2231. // used for internal patchbay mode
  2232. void EngineInternalGraph::addPlugin(const CarlaPluginPtr plugin)
  2233. {
  2234. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2235. fPatchbay->addPlugin(plugin);
  2236. }
  2237. void EngineInternalGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  2238. {
  2239. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2240. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  2241. }
  2242. void EngineInternalGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  2243. {
  2244. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2245. fPatchbay->renamePlugin(plugin, newName);
  2246. }
  2247. void EngineInternalGraph::switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB)
  2248. {
  2249. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2250. fPatchbay->switchPlugins(pluginA, pluginB);
  2251. }
  2252. void EngineInternalGraph::removePlugin(const CarlaPluginPtr plugin)
  2253. {
  2254. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2255. fPatchbay->removePlugin(plugin);
  2256. }
  2257. void EngineInternalGraph::removeAllPlugins()
  2258. {
  2259. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2260. fPatchbay->removeAllPlugins();
  2261. }
  2262. bool EngineInternalGraph::isUsingExternalHost() const noexcept
  2263. {
  2264. if (fIsRack)
  2265. return true;
  2266. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2267. return fPatchbay->usingExternalHost;
  2268. }
  2269. bool EngineInternalGraph::isUsingExternalOSC() const noexcept
  2270. {
  2271. if (fIsRack)
  2272. return true;
  2273. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2274. return fPatchbay->usingExternalOSC;
  2275. }
  2276. void EngineInternalGraph::setUsingExternalHost(const bool usingExternal) noexcept
  2277. {
  2278. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2279. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2280. fPatchbay->usingExternalHost = usingExternal;
  2281. }
  2282. void EngineInternalGraph::setUsingExternalOSC(const bool usingExternal) noexcept
  2283. {
  2284. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2285. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2286. fPatchbay->usingExternalOSC = usingExternal;
  2287. }
  2288. // -----------------------------------------------------------------------
  2289. // CarlaEngine Patchbay stuff
  2290. bool CarlaEngine::patchbayConnect(const bool external,
  2291. const uint groupA, const uint portA,
  2292. const uint groupB, const uint portB)
  2293. {
  2294. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2295. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2296. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  2297. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2298. {
  2299. RackGraph* const graph = pData->graph.getRackGraph();
  2300. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2301. return graph->connect(groupA, portA, groupB, portB);
  2302. }
  2303. else
  2304. {
  2305. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2306. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2307. return graph->connect(external, groupA, portA, groupB, portB);
  2308. }
  2309. }
  2310. bool CarlaEngine::patchbayDisconnect(const bool external, const uint connectionId)
  2311. {
  2312. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2313. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2314. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  2315. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2316. {
  2317. RackGraph* const graph = pData->graph.getRackGraph();
  2318. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2319. return graph->disconnect(connectionId);
  2320. }
  2321. else
  2322. {
  2323. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2324. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2325. return graph->disconnect(external, connectionId);
  2326. }
  2327. }
  2328. bool CarlaEngine::patchbaySetGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  2329. const uint groupId, const int x1, const int y1, const int x2, const int y2)
  2330. {
  2331. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  2332. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2333. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2334. carla_debug("CarlaEngine::patchbaySetGroupPos(%u, %i, %i, %i, %i)", groupId, x1, y1, x2, y2);
  2335. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2336. {
  2337. // we don't bother to save position in this case, there is only midi in/out
  2338. return true;
  2339. }
  2340. else
  2341. {
  2342. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2343. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2344. graph->setGroupPos(sendHost, sendOSC, external, groupId, x1, y1, x2, y2);
  2345. return true;
  2346. }
  2347. }
  2348. bool CarlaEngine::patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external)
  2349. {
  2350. // subclasses should handle this
  2351. CARLA_SAFE_ASSERT_RETURN(! external, false);
  2352. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2353. {
  2354. // This is implemented in engine subclasses
  2355. setLastError("Unsupported operation");
  2356. return false;
  2357. }
  2358. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2359. {
  2360. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2361. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2362. graph->refresh(sendHost, sendOSC, external, "");
  2363. return true;
  2364. }
  2365. setLastError("Unsupported operation");
  2366. return false;
  2367. }
  2368. // -----------------------------------------------------------------------
  2369. // Patchbay stuff
  2370. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  2371. {
  2372. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2373. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  2374. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2375. {
  2376. RackGraph* const graph = pData->graph.getRackGraph();
  2377. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2378. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  2379. return graph->getConnections();
  2380. }
  2381. else
  2382. {
  2383. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2384. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2385. return graph->getConnections(external);
  2386. }
  2387. return nullptr;
  2388. }
  2389. const CarlaEngine::PatchbayPosition* CarlaEngine::getPatchbayPositions(bool external, uint& count) const
  2390. {
  2391. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2392. carla_debug("CarlaEngine::getPatchbayPositions(%s)", bool2str(external));
  2393. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2394. {
  2395. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2396. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2397. return graph->getPositions(external, count);
  2398. }
  2399. return nullptr;
  2400. }
  2401. void CarlaEngine::restorePatchbayConnection(const bool external,
  2402. const char* const sourcePort,
  2403. const char* const targetPort)
  2404. {
  2405. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  2406. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  2407. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  2408. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  2409. uint groupA, portA;
  2410. uint groupB, portB;
  2411. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2412. {
  2413. RackGraph* const graph = pData->graph.getRackGraph();
  2414. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2415. CARLA_SAFE_ASSERT_RETURN(external,);
  2416. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  2417. return;
  2418. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  2419. return;
  2420. graph->connect(groupA, portA, groupB, portB);
  2421. }
  2422. else
  2423. {
  2424. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2425. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2426. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  2427. return;
  2428. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  2429. return;
  2430. graph->connect(external, groupA, portA, groupB, portB);
  2431. }
  2432. }
  2433. bool CarlaEngine::restorePatchbayGroupPosition(const bool external, PatchbayPosition& ppos)
  2434. {
  2435. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2436. CARLA_SAFE_ASSERT_RETURN(ppos.name != nullptr && ppos.name[0] != '\0', false);
  2437. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2438. {
  2439. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2440. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2441. const char* const orig_name = ppos.name;
  2442. // strip client name prefix if present
  2443. if (ppos.pluginId >= 0)
  2444. {
  2445. if (const char* const rname2 = std::strstr(ppos.name, "."))
  2446. if (const char* const rname3 = std::strstr(rname2 + 1, "/"))
  2447. ppos.name = rname3 + 1;
  2448. }
  2449. uint groupId;
  2450. CARLA_SAFE_ASSERT_INT_RETURN(graph->getGroupFromName(external, ppos.name, groupId), external, false);
  2451. graph->setGroupPos(true, true, external, groupId, ppos.x1, ppos.y1, ppos.x2, ppos.y2);
  2452. return ppos.name != orig_name;
  2453. }
  2454. return false;
  2455. }
  2456. // -----------------------------------------------------------------------
  2457. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2458. {
  2459. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2460. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2461. RackGraph* const graph(pData->graph.getRackGraph());
  2462. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2463. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2464. switch (connectionType)
  2465. {
  2466. case kExternalGraphConnectionAudioIn1:
  2467. return graph->audioBuffers.connectedIn1.append(portId);
  2468. case kExternalGraphConnectionAudioIn2:
  2469. return graph->audioBuffers.connectedIn2.append(portId);
  2470. case kExternalGraphConnectionAudioOut1:
  2471. return graph->audioBuffers.connectedOut1.append(portId);
  2472. case kExternalGraphConnectionAudioOut2:
  2473. return graph->audioBuffers.connectedOut2.append(portId);
  2474. }
  2475. return false;
  2476. }
  2477. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2478. {
  2479. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2480. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2481. RackGraph* const graph(pData->graph.getRackGraph());
  2482. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2483. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2484. switch (connectionType)
  2485. {
  2486. case kExternalGraphConnectionAudioIn1:
  2487. return graph->audioBuffers.connectedIn1.removeOne(portId);
  2488. case kExternalGraphConnectionAudioIn2:
  2489. return graph->audioBuffers.connectedIn2.removeOne(portId);
  2490. case kExternalGraphConnectionAudioOut1:
  2491. return graph->audioBuffers.connectedOut1.removeOne(portId);
  2492. case kExternalGraphConnectionAudioOut2:
  2493. return graph->audioBuffers.connectedOut2.removeOne(portId);
  2494. }
  2495. return false;
  2496. }
  2497. // -----------------------------------------------------------------------
  2498. CARLA_BACKEND_END_NAMESPACE