|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271 |
- /*
- * Carla Plugin Host
- * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #include "CarlaEngineGraph.hpp"
- #include "CarlaEngineInternal.hpp"
- #include "CarlaPlugin.hpp"
-
- #include "CarlaMathUtils.hpp"
- #include "CarlaJuceUtils.hpp"
-
- #include "CarlaMIDI.h"
-
- using water::jmax;
- using water::jmin;
- using water::AudioProcessor;
- using water::MidiBuffer;
- using water::String;
- using water::StringArray;
-
- CARLA_BACKEND_START_NAMESPACE
-
- // -----------------------------------------------------------------------
- // Fallback data
-
- static const PortNameToId kPortNameToIdFallback = { 0, 0, { '\0' }, { '\0' } };
- static /* */ PortNameToId kPortNameToIdFallbackNC = { 0, 0, { '\0' }, { '\0' } };
-
- // -----------------------------------------------------------------------
- // External Graph stuff
-
- static inline
- uint getExternalGraphPortIdFromName(const char* const shortname) noexcept
- {
- if (std::strcmp(shortname, "AudioIn1") == 0 || std::strcmp(shortname, "audio-in1") == 0)
- return kExternalGraphCarlaPortAudioIn1;
- if (std::strcmp(shortname, "AudioIn2") == 0 || std::strcmp(shortname, "audio-in2") == 0)
- return kExternalGraphCarlaPortAudioIn2;
- if (std::strcmp(shortname, "AudioOut1") == 0 || std::strcmp(shortname, "audio-out1") == 0)
- return kExternalGraphCarlaPortAudioOut1;
- if (std::strcmp(shortname, "AudioOut2") == 0 || std::strcmp(shortname, "audio-out2") == 0)
- return kExternalGraphCarlaPortAudioOut2;
- if (std::strcmp(shortname, "MidiIn") == 0 || std::strcmp(shortname, "midi-in") == 0)
- return kExternalGraphCarlaPortMidiIn;
- if (std::strcmp(shortname, "MidiOut") == 0 || std::strcmp(shortname, "midi-out") == 0)
- return kExternalGraphCarlaPortMidiOut;
-
- carla_stderr("CarlaBackend::getExternalGraphPortIdFromName(%s) - invalid short name", shortname);
- return kExternalGraphCarlaPortNull;
- }
-
- static inline
- const char* getExternalGraphFullPortNameFromId(const /*RackGraphCarlaPortIds*/ uint portId)
- {
- switch (portId)
- {
- case kExternalGraphCarlaPortAudioIn1:
- return "Carla:AudioIn1";
- case kExternalGraphCarlaPortAudioIn2:
- return "Carla:AudioIn2";
- case kExternalGraphCarlaPortAudioOut1:
- return "Carla:AudioOut1";
- case kExternalGraphCarlaPortAudioOut2:
- return "Carla:AudioOut2";
- case kExternalGraphCarlaPortMidiIn:
- return "Carla:MidiIn";
- case kExternalGraphCarlaPortMidiOut:
- return "Carla:MidiOut";
- //case kExternalGraphCarlaPortNull:
- //case kExternalGraphCarlaPortMax:
- // break;
- }
-
- carla_stderr("CarlaBackend::getExternalGraphFullPortNameFromId(%i) - invalid port id", portId);
- return nullptr;
- }
-
- // -----------------------------------------------------------------------
-
- ExternalGraphPorts::ExternalGraphPorts() noexcept
- : ins(),
- outs() {}
-
- const char* ExternalGraphPorts::getName(const bool isInput, const uint portId) const noexcept
- {
- for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
- {
- const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
- CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
-
- if (portNameToId.port == portId)
- return portNameToId.name;
- }
-
- return nullptr;
- }
-
- uint ExternalGraphPorts::getPortId(const bool isInput, const char portName[], bool* const ok) const noexcept
- {
- for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
- {
- const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
- CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
-
- if (std::strncmp(portNameToId.name, portName, STR_MAX) == 0)
- {
- if (ok != nullptr)
- *ok = true;
- return portNameToId.port;
- }
- }
-
- if (ok != nullptr)
- *ok = false;
- return 0;
- }
-
- // -----------------------------------------------------------------------
-
- ExternalGraph::ExternalGraph(CarlaEngine* const engine) noexcept
- : connections(),
- audioPorts(),
- midiPorts(),
- retCon(),
- kEngine(engine) {}
-
- void ExternalGraph::clear() noexcept
- {
- connections.clear();
- audioPorts.ins.clear();
- audioPorts.outs.clear();
- midiPorts.ins.clear();
- midiPorts.outs.clear();
- }
-
- bool ExternalGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback) noexcept
- {
- uint otherGroup, otherPort, carlaPort;
-
- if (groupA == kExternalGraphGroupCarla)
- {
- CARLA_SAFE_ASSERT_RETURN(groupB != kExternalGraphGroupCarla, false);
-
- carlaPort = portA;
- otherGroup = groupB;
- otherPort = portB;
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(groupB == kExternalGraphGroupCarla, false);
-
- carlaPort = portB;
- otherGroup = groupA;
- otherPort = portA;
- }
-
- CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
- CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
-
- bool makeConnection = false;
-
- switch (carlaPort)
- {
- case kExternalGraphCarlaPortAudioIn1:
- CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
- makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortAudioIn2:
- CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
- makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortAudioOut1:
- CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
- makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortAudioOut2:
- CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
- makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortMidiIn:
- CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiIn, false);
- if (const char* const portName = midiPorts.getName(true, otherPort))
- makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
- break;
-
- case kExternalGraphCarlaPortMidiOut:
- CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiOut, false);
- if (const char* const portName = midiPorts.getName(false, otherPort))
- makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
- break;
- }
-
- if (! makeConnection)
- {
- kEngine->setLastError("Invalid rack connection");
- return false;
- }
-
- ConnectionToId connectionToId;
- connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
- std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
-
- if (sendCallback)
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- connections.list.append(connectionToId);
- return true;
- }
-
- bool ExternalGraph::disconnect(const uint connectionId) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(connections.list.count() > 0, false);
-
- for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
- {
- static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
-
- const ConnectionToId& connectionToId(it.getValue(fallback));
- CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
-
- if (connectionToId.id != connectionId)
- continue;
-
- uint otherGroup, otherPort, carlaPort;
-
- if (connectionToId.groupA == kExternalGraphGroupCarla)
- {
- CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB != kExternalGraphGroupCarla, false);
-
- carlaPort = connectionToId.portA;
- otherGroup = connectionToId.groupB;
- otherPort = connectionToId.portB;
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB == kExternalGraphGroupCarla, false);
-
- carlaPort = connectionToId.portB;
- otherGroup = connectionToId.groupA;
- otherPort = connectionToId.portA;
- }
-
- CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
- CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
-
- bool makeDisconnection = false;
-
- switch (carlaPort)
- {
- case kExternalGraphCarlaPortAudioIn1:
- makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortAudioIn2:
- makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortAudioOut1:
- makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortAudioOut2:
- makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
- break;
-
- case kExternalGraphCarlaPortMidiIn:
- if (const char* const portName = midiPorts.getName(true, otherPort))
- makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
- break;
-
- case kExternalGraphCarlaPortMidiOut:
- if (const char* const portName = midiPorts.getName(false, otherPort))
- makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
- break;
- }
-
- if (! makeDisconnection)
- {
- kEngine->setLastError("Invalid rack connection");
- return false;
- }
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
-
- connections.list.remove(it);
- return true;
- }
-
- kEngine->setLastError("Failed to find connection");
- return false;
- }
-
- void ExternalGraph::refresh(const char* const deviceName)
- {
- CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
-
- const bool isRack(kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
-
- // Main
- {
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupCarla, PATCHBAY_ICON_CARLA, MAIN_CARLA_PLUGIN_ID, 0.0f, kEngine->getName());
-
- if (isRack)
- {
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in1");
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in2");
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
- }
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiIn, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "midi-in");
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiOut, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
- }
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
-
- if (isRack)
- {
- // Audio In
- if (deviceName[0] != '\0')
- std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName);
- else
- std::strncpy(strBuf, "Capture", STR_MAX);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
-
- const CarlaString groupNameIn(strBuf);
-
- int h = 0;
- for (LinkedList<PortNameToId>::Itenerator it = audioPorts.ins.begin2(); it.valid(); it.next())
- {
- PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
- CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
-
- portNameToId.setFullName(groupNameIn + portNameToId.name);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioIn, ++h,
- PATCHBAY_PORT_TYPE_AUDIO, 0.0f, portNameToId.name);
- }
-
- // Audio Out
- if (deviceName[0] != '\0')
- std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName);
- else
- std::strncpy(strBuf, "Playback", STR_MAX);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
-
- const CarlaString groupNameOut(strBuf);
-
- h = 0;
- for (LinkedList<PortNameToId>::Itenerator it = audioPorts.outs.begin2(); it.valid(); it.next())
- {
- PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
- CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
-
- portNameToId.setFullName(groupNameOut + portNameToId.name);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioOut, ++h,
- PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
- }
- }
-
- // MIDI In
- {
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Readable MIDI ports");
-
- const CarlaString groupNamePlus("Readable MIDI ports:");
-
- int h = 0;
- for (LinkedList<PortNameToId>::Itenerator it = midiPorts.ins.begin2(); it.valid(); it.next())
- {
- PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
- CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
-
- portNameToId.setFullName(groupNamePlus + portNameToId.name);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiIn, ++h,
- PATCHBAY_PORT_TYPE_MIDI, 0.0f, portNameToId.name);
- }
- }
-
- // MIDI Out
- {
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Writable MIDI ports");
-
- const CarlaString groupNamePlus("Writable MIDI ports:");
-
- int h = 0;
- for (LinkedList<PortNameToId>::Itenerator it = midiPorts.outs.begin2(); it.valid(); it.next())
- {
- PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
- CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
-
- portNameToId.setFullName(groupNamePlus + portNameToId.name);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiOut, ++h,
- PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
- }
- }
- }
-
- const char* const* ExternalGraph::getConnections() const noexcept
- {
- if (connections.list.count() == 0)
- return nullptr;
-
- CarlaStringList connList;
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
-
- for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
- {
- static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
-
- const ConnectionToId& connectionToId(it.getValue(fallback));
- CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
-
- uint otherGroup, otherPort, carlaPort;
-
- if (connectionToId.groupA == kExternalGraphGroupCarla)
- {
- CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB != kExternalGraphGroupCarla);
-
- carlaPort = connectionToId.portA;
- otherGroup = connectionToId.groupB;
- otherPort = connectionToId.portB;
- }
- else
- {
- CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB == kExternalGraphGroupCarla);
-
- carlaPort = connectionToId.portB;
- otherGroup = connectionToId.groupA;
- otherPort = connectionToId.portA;
- }
-
- CARLA_SAFE_ASSERT_CONTINUE(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax);
- CARLA_SAFE_ASSERT_CONTINUE(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax);
-
- switch (carlaPort)
- {
- case kExternalGraphCarlaPortAudioIn1:
- case kExternalGraphCarlaPortAudioIn2:
- std::snprintf(strBuf, STR_MAX, "AudioIn:%s", audioPorts.getName(true, otherPort));
- connList.append(strBuf);
- connList.append(getExternalGraphFullPortNameFromId(carlaPort));
- break;
-
- case kExternalGraphCarlaPortAudioOut1:
- case kExternalGraphCarlaPortAudioOut2:
- std::snprintf(strBuf, STR_MAX, "AudioOut:%s", audioPorts.getName(false, otherPort));
- connList.append(getExternalGraphFullPortNameFromId(carlaPort));
- connList.append(strBuf);
- break;
-
- case kExternalGraphCarlaPortMidiIn:
- std::snprintf(strBuf, STR_MAX, "MidiIn:%s", midiPorts.getName(true, otherPort));
- connList.append(strBuf);
- connList.append(getExternalGraphFullPortNameFromId(carlaPort));
- break;
-
- case kExternalGraphCarlaPortMidiOut:
- std::snprintf(strBuf, STR_MAX, "MidiOut:%s", midiPorts.getName(false, otherPort));
- connList.append(getExternalGraphFullPortNameFromId(carlaPort));
- connList.append(strBuf);
- break;
- }
- }
-
- if (connList.count() == 0)
- return nullptr;
-
- retCon = connList.toCharStringListPtr();
-
- return retCon;
- }
-
- bool ExternalGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', false);
-
- if (std::strncmp(fullPortName, "Carla:", 6) == 0)
- {
- groupId = kExternalGraphGroupCarla;
- portId = getExternalGraphPortIdFromName(fullPortName+6);
-
- if (portId > kExternalGraphCarlaPortNull && portId < kExternalGraphCarlaPortMax)
- return true;
- }
- else if (std::strncmp(fullPortName, "AudioIn:", 8) == 0)
- {
- groupId = kExternalGraphGroupAudioIn;
-
- if (const char* const portName = fullPortName+8)
- {
- bool ok;
- portId = audioPorts.getPortId(true, portName, &ok);
- return ok;
- }
- }
- else if (std::strncmp(fullPortName, "AudioOut:", 9) == 0)
- {
- groupId = kExternalGraphGroupAudioOut;
-
- if (const char* const portName = fullPortName+9)
- {
- bool ok;
- portId = audioPorts.getPortId(false, portName, &ok);
- return ok;
- }
- }
- else if (std::strncmp(fullPortName, "MidiIn:", 7) == 0)
- {
- groupId = kExternalGraphGroupMidiIn;
-
- if (const char* const portName = fullPortName+7)
- {
- bool ok;
- portId = midiPorts.getPortId(true, portName, &ok);
- return ok;
- }
- }
- else if (std::strncmp(fullPortName, "MidiOut:", 8) == 0)
- {
- groupId = kExternalGraphGroupMidiOut;
-
- if (const char* const portName = fullPortName+8)
- {
- bool ok;
- portId = midiPorts.getPortId(false, portName, &ok);
- return ok;
- }
- }
-
- return false;
- }
-
- // -----------------------------------------------------------------------
- // RackGraph Buffers
-
- RackGraph::Buffers::Buffers() noexcept
- : mutex(),
- connectedIn1(),
- connectedIn2(),
- connectedOut1(),
- connectedOut2(),
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- inBuf{nullptr, nullptr},
- inBufTmp{nullptr, nullptr},
- outBuf{nullptr, nullptr},
- #endif
- unusedBuf(nullptr)
- {
- #ifndef CARLA_PROPER_CPP11_SUPPORT
- inBuf[0] = inBuf[1] = nullptr;
- inBufTmp[0] = inBufTmp[1] = nullptr;
- outBuf[0] = outBuf[1] = nullptr;
- #endif
- }
-
- RackGraph::Buffers::~Buffers() noexcept
- {
- const CarlaRecursiveMutexLocker cml(mutex);
-
- if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
- if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
- if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
- if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
- if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
- if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
- if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
-
- connectedIn1.clear();
- connectedIn2.clear();
- connectedOut1.clear();
- connectedOut2.clear();
- }
-
- void RackGraph::Buffers::setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept
- {
- const CarlaRecursiveMutexLocker cml(mutex);
-
- if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
- if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
- if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
- if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
- if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
- if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
- if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
-
- CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
-
- try {
- inBufTmp[0] = new float[bufferSize];
- inBufTmp[1] = new float[bufferSize];
- unusedBuf = new float[bufferSize];
-
- if (createBuffers)
- {
- inBuf[0] = new float[bufferSize];
- inBuf[1] = new float[bufferSize];
- outBuf[0] = new float[bufferSize];
- outBuf[1] = new float[bufferSize];
- }
- }
- catch(...) {
- if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
- if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
- if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
-
- if (createBuffers)
- {
- if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
- if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
- if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
- if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
- }
- return;
- }
-
- carla_zeroFloats(inBufTmp[0], bufferSize);
- carla_zeroFloats(inBufTmp[1], bufferSize);
-
- if (createBuffers)
- {
- carla_zeroFloats(inBuf[0], bufferSize);
- carla_zeroFloats(inBuf[1], bufferSize);
- carla_zeroFloats(outBuf[0], bufferSize);
- carla_zeroFloats(outBuf[1], bufferSize);
- }
- }
-
- // -----------------------------------------------------------------------
- // RackGraph
-
- RackGraph::RackGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs) noexcept
- : extGraph(engine),
- inputs(ins),
- outputs(outs),
- isOffline(false),
- audioBuffers(),
- kEngine(engine)
- {
- setBufferSize(engine->getBufferSize());
- }
-
- RackGraph::~RackGraph() noexcept
- {
- extGraph.clear();
- }
-
- void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
- {
- audioBuffers.setBufferSize(bufferSize, (inputs > 0 || outputs > 0));
- }
-
- void RackGraph::setOffline(const bool offline) noexcept
- {
- isOffline = offline;
- }
-
- bool RackGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
- {
- return extGraph.connect(groupA, portA, groupB, portB, true);
- }
-
- bool RackGraph::disconnect(const uint connectionId) noexcept
- {
- return extGraph.disconnect(connectionId);
- }
-
- void RackGraph::refresh(const char* const deviceName)
- {
- extGraph.refresh(deviceName);
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
-
- // Connections
- const CarlaRecursiveMutexLocker cml(audioBuffers.mutex);
-
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
- {
- const uint& portId(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
- CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- extGraph.connections.list.append(connectionToId);
- }
-
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
- {
- const uint& portId(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
- CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- extGraph.connections.list.append(connectionToId);
- }
-
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
- {
- const uint& portId(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
- CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, kExternalGraphGroupAudioOut, portId);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- extGraph.connections.list.append(connectionToId);
- }
-
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
- {
- const uint& portId(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
- CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, kExternalGraphGroupAudioOut, portId);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- extGraph.connections.list.append(connectionToId);
- }
- }
-
- const char* const* RackGraph::getConnections() const noexcept
- {
- return extGraph.getConnections();
- }
-
- bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
- {
- return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
- }
-
- void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBufReal[2], const uint32_t frames)
- {
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
-
- // safe copy
- float* const dummyBuf = audioBuffers.unusedBuf;
- float* const inBuf0 = audioBuffers.inBufTmp[0];
- float* const inBuf1 = audioBuffers.inBufTmp[1];
-
- // initialize audio inputs
- carla_copyFloats(inBuf0, inBufReal[0], frames);
- carla_copyFloats(inBuf1, inBufReal[1], frames);
-
- // initialize audio outputs (zero)
- carla_zeroFloats(outBufReal[0], frames);
- carla_zeroFloats(outBufReal[1], frames);
-
- // initialize event outputs (zero)
- carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
-
- uint32_t oldAudioInCount = 0;
- uint32_t oldAudioOutCount = 0;
- uint32_t oldMidiOutCount = 0;
- bool processed = false;
-
- // process plugins
- for (uint i=0; i < data->curPluginCount; ++i)
- {
- CarlaPlugin* const plugin = data->plugins[i].plugin;
-
- if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
- continue;
-
- if (processed)
- {
- // initialize audio inputs (from previous outputs)
- carla_copyFloats(inBuf0, outBufReal[0], frames);
- carla_copyFloats(inBuf1, outBufReal[1], frames);
-
- // initialize audio outputs (zero)
- carla_zeroFloats(outBufReal[0], frames);
- carla_zeroFloats(outBufReal[1], frames);
-
- // if plugin has no midi out, add previous events
- if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
- {
- if (data->events.out[0].type != kEngineEventTypeNull)
- {
- // TODO: carefully add to input, sorted events
- //carla_stderr("TODO midi event mixing here %s", plugin->getName());
- }
- // else nothing needed
- }
- else
- {
- // initialize event inputs from previous outputs
- carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
-
- // initialize event outputs (zero)
- carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
- }
- }
-
- oldAudioInCount = plugin->getAudioInCount();
- oldAudioOutCount = plugin->getAudioOutCount();
- oldMidiOutCount = plugin->getMidiOutCount();
-
- const uint32_t numInBufs = std::max(oldAudioInCount, 2U);
- const uint32_t numOutBufs = std::max(oldAudioOutCount, 2U);
-
- const float* inBuf[numInBufs];
- inBuf[0] = inBuf0;
- inBuf[1] = inBuf1;
-
- float* outBuf[numOutBufs];
- outBuf[0] = outBufReal[0];
- outBuf[1] = outBufReal[1];
-
- if (numInBufs > 2 || numOutBufs > 2)
- {
- carla_zeroFloats(dummyBuf, frames);
-
- for (uint32_t i=2; i<numInBufs; ++i)
- inBuf[i] = dummyBuf;
-
- for (uint32_t i=2; i<numOutBufs; ++i)
- outBuf[i] = dummyBuf;
- }
-
- // process
- plugin->initBuffers();
- plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
- plugin->unlock();
-
- // if plugin has no audio inputs, add input buffer
- if (oldAudioInCount == 0)
- {
- carla_addFloats(outBufReal[0], inBuf0, frames);
- carla_addFloats(outBufReal[1], inBuf1, frames);
- }
-
- // if plugin only has 1 output, copy it to the 2nd
- if (oldAudioOutCount == 1)
- {
- carla_copyFloats(outBufReal[1], outBufReal[0], frames);
- }
-
- // set peaks
- {
- EnginePluginData& pluginData(data->plugins[i]);
-
- if (oldAudioInCount > 0)
- {
- pluginData.insPeak[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
- pluginData.insPeak[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
- }
- else
- {
- pluginData.insPeak[0] = 0.0f;
- pluginData.insPeak[1] = 0.0f;
- }
-
- if (oldAudioOutCount > 0)
- {
- pluginData.outsPeak[0] = carla_findMaxNormalizedFloat(outBufReal[0], frames);
- pluginData.outsPeak[1] = carla_findMaxNormalizedFloat(outBufReal[1], frames);
- }
- else
- {
- pluginData.outsPeak[0] = 0.0f;
- pluginData.outsPeak[1] = 0.0f;
- }
- }
-
- processed = true;
- }
- }
-
- void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
- {
- CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
-
- const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
-
- if (inBuf != nullptr && inputs > 0)
- {
- bool noConnections = true;
-
- // connect input buffers
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
- {
- const uint& port(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(port > 0);
- CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
-
- if (noConnections)
- {
- carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
- noConnections = false;
- }
- else
- {
- carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
- }
- }
-
- if (noConnections)
- carla_zeroFloats(audioBuffers.inBuf[0], frames);
-
- noConnections = true;
-
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
- {
- const uint& port(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(port > 0);
- CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
-
- if (noConnections)
- {
- carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
- noConnections = false;
- }
- else
- {
- carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
- }
- }
-
- if (noConnections)
- carla_zeroFloats(audioBuffers.inBuf[1], frames);
- }
- else
- {
- carla_zeroFloats(audioBuffers.inBuf[0], frames);
- carla_zeroFloats(audioBuffers.inBuf[1], frames);
- }
-
- carla_zeroFloats(audioBuffers.outBuf[0], frames);
- carla_zeroFloats(audioBuffers.outBuf[1], frames);
-
- // process
- process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
-
- // connect output buffers
- if (audioBuffers.connectedOut1.count() != 0)
- {
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
- {
- const uint& port(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(port > 0);
- CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
-
- carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
- }
- }
-
- if (audioBuffers.connectedOut2.count() != 0)
- {
- for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
- {
- const uint& port(it.getValue(0));
- CARLA_SAFE_ASSERT_CONTINUE(port > 0);
- CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
-
- carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
- }
- }
- }
-
- // -----------------------------------------------------------------------
- // Patchbay Graph stuff
-
- static const uint32_t kAudioInputPortOffset = MAX_PATCHBAY_PLUGINS*1;
- static const uint32_t kAudioOutputPortOffset = MAX_PATCHBAY_PLUGINS*2;
- static const uint32_t kMidiInputPortOffset = MAX_PATCHBAY_PLUGINS*3;
- static const uint32_t kMidiOutputPortOffset = MAX_PATCHBAY_PLUGINS*3+1;
-
- static const uint kMidiChannelIndex = static_cast<uint>(AudioProcessorGraph::midiChannelIndex);
-
- static inline
- bool adjustPatchbayPortIdForWater(uint& portId)
- {
- CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
- CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, false);
-
- if (portId == kMidiInputPortOffset)
- {
- portId = kMidiChannelIndex;
- return true;
- }
- if (portId == kMidiOutputPortOffset)
- {
- portId = kMidiChannelIndex;
- return true;
- }
- if (portId >= kAudioOutputPortOffset)
- {
- portId -= kAudioOutputPortOffset;
- return true;
- }
- if (portId >= kAudioInputPortOffset)
- {
- portId -= kAudioInputPortOffset;
- return true;
- }
-
- return false;
- }
-
- static inline
- const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
- {
- CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
- CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
- CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, String());
-
- String fullPortName(proc->getName());
-
- if (portId == kMidiOutputPortOffset)
- {
- fullPortName += ":events-out";
- }
- else if (portId == kMidiInputPortOffset)
- {
- fullPortName += ":events-in";
- }
- else if (portId >= kAudioOutputPortOffset)
- {
- CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels() > 0, String());
- fullPortName += ":" + proc->getOutputChannelName(static_cast<int>(portId-kAudioOutputPortOffset));
- }
- else if (portId >= kAudioInputPortOffset)
- {
- CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels() > 0, String());
- fullPortName += ":" + proc->getInputChannelName(static_cast<int>(portId-kAudioInputPortOffset));
- }
- else
- {
- return String();
- }
-
- return fullPortName;
- }
-
- static inline
- void addNodeToPatchbay(CarlaEngine* const engine, const uint32_t groupId, const int clientId, const AudioProcessor* const proc)
- {
- CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
-
- const int icon((clientId >= 0) ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE);
- engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, groupId, icon, clientId, 0.0f, proc->getName().toRawUTF8());
-
- for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
- PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, proc->getInputChannelName(i).toRawUTF8());
- }
-
- for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
- PATCHBAY_PORT_TYPE_AUDIO, 0.0f, proc->getOutputChannelName(i).toRawUTF8());
- }
-
- if (proc->acceptsMidi())
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiInputPortOffset),
- PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "events-in");
- }
-
- if (proc->producesMidi())
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiOutputPortOffset),
- PATCHBAY_PORT_TYPE_MIDI, 0.0f, "events-out");
- }
- }
-
- static inline
- void removeNodeFromPatchbay(CarlaEngine* const engine, const uint32_t groupId, const AudioProcessor* const proc)
- {
- CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
-
- for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
- 0, 0.0f, nullptr);
- }
-
- for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
- 0, 0.0f, nullptr);
- }
-
- if (proc->acceptsMidi())
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiInputPortOffset),
- 0, 0.0f, nullptr);
- }
-
- if (proc->producesMidi())
- {
- engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiOutputPortOffset),
- 0, 0.0f, nullptr);
- }
-
- engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, groupId, 0, 0, 0.0f, nullptr);
- }
-
- // -----------------------------------------------------------------------
-
- class CarlaPluginInstance : public AudioProcessor
- {
- public:
- CarlaPluginInstance(CarlaEngine* const engine, CarlaPlugin* const plugin)
- : kEngine(engine),
- fPlugin(plugin)
- {
- setPlayConfigDetails(static_cast<int>(fPlugin->getAudioInCount()),
- static_cast<int>(fPlugin->getAudioOutCount()),
- getSampleRate(), getBlockSize());
- }
-
- ~CarlaPluginInstance() override
- {
- }
-
- void invalidatePlugin() noexcept
- {
- fPlugin = nullptr;
- }
-
- // -------------------------------------------------------------------
-
- const String getName() const override
- {
- return fPlugin->getName();
- }
-
- void processBlock(AudioSampleBuffer& audio, MidiBuffer& midi) override
- {
- if (fPlugin == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
- {
- audio.clear();
- midi.clear();
- return;
- }
-
- fPlugin->initBuffers();
-
- if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
- {
- EngineEvent* const engineEvents(port->fBuffer);
- CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
-
- carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
- fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
- }
-
- midi.clear();
-
- // TODO - CV support
-
- const uint32_t numSamples(static_cast<uint32_t>(audio.getNumSamples()));
-
- if (const int numChan = audio.getNumChannels())
- {
- const uint numChanu = static_cast<uint>(jmin(numChan, 2));
-
- if (fPlugin->getAudioInCount() == 0)
- audio.clear();
-
- float* audioBuffers[numChan];
-
- for (int i=0; i<numChan; ++i)
- audioBuffers[i] = audio.getWritePointer(i);
-
- float inPeaks[2] = { 0.0f };
- float outPeaks[2] = { 0.0f };
-
- for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChanu); i<count; ++i)
- inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
-
- fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers, nullptr, nullptr, numSamples);
-
- for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChanu); i<count; ++i)
- outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
-
- kEngine->setPluginPeaks(fPlugin->getId(), inPeaks, outPeaks);
- }
- else
- {
- fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
- }
-
- midi.clear();
-
- if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
- {
- /*const*/ EngineEvent* const engineEvents(port->fBuffer);
- CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
-
- fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
- carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
- }
-
- fPlugin->unlock();
- }
-
- const String getInputChannelName(int i) const override
- {
- CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
- CarlaEngineClient* const client(fPlugin->getEngineClient());
- return client->getAudioPortName(true, static_cast<uint>(i));
- }
-
- const String getOutputChannelName(int i) const override
- {
- CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
- CarlaEngineClient* const client(fPlugin->getEngineClient());
- return client->getAudioPortName(false, static_cast<uint>(i));
- }
-
- void prepareToPlay(double, int) override {}
- void releaseResources() override {}
-
- bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
- bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
-
- private:
- CarlaEngine* const kEngine;
- CarlaPlugin* fPlugin;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
- };
-
- // -----------------------------------------------------------------------
- // Patchbay Graph
-
- class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
- {
- public:
- NamedAudioGraphIOProcessor(const IODeviceType iotype)
- : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
- inputNames(),
- outputNames() {}
-
- const String getInputChannelName (int index) const override
- {
- if (index < inputNames.size())
- return inputNames[index];
- return String("Playback ") + String(index+1);
- }
-
- const String getOutputChannelName (int index) const override
- {
- if (index < outputNames.size())
- return outputNames[index];
- return String("Capture ") + String(index+1);
- }
-
- void setNames(const bool setInputNames, const StringArray& names)
- {
- if (setInputNames)
- inputNames = names;
- else
- outputNames = names;
- }
-
- private:
- StringArray inputNames;
- StringArray outputNames;
- };
-
- PatchbayGraph::PatchbayGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs)
- : CarlaThread("PatchbayReorderThread"),
- connections(),
- graph(),
- audioBuffer(),
- midiBuffer(),
- inputs(carla_fixedValue(0U, 32U, ins)),
- outputs(carla_fixedValue(0U, 32U, outs)),
- retCon(),
- usingExternal(false),
- extGraph(engine),
- kEngine(engine)
- {
- const int bufferSize(static_cast<int>(engine->getBufferSize()));
- const double sampleRate(engine->getSampleRate());
-
- graph.setPlayConfigDetails(static_cast<int>(inputs), static_cast<int>(outputs), sampleRate, bufferSize);
- graph.prepareToPlay(sampleRate, bufferSize);
-
- audioBuffer.setSize(static_cast<int>(jmax(inputs, outputs)), bufferSize);
-
- midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
- midiBuffer.clear();
-
- StringArray channelNames;
-
- switch (inputs)
- {
- case 2:
- channelNames.add("Left");
- channelNames.add("Right");
- break;
- case 3:
- channelNames.add("Left");
- channelNames.add("Right");
- channelNames.add("Sidechain");
- break;
- }
-
- {
- NamedAudioGraphIOProcessor* const proc(
- new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
- proc->setNames(false, channelNames);
-
- AudioProcessorGraph::Node* const node(graph.addNode(proc));
- node->properties.set("isPlugin", false);
- node->properties.set("isOutput", false);
- node->properties.set("isAudio", true);
- node->properties.set("isCV", false);
- node->properties.set("isMIDI", false);
- node->properties.set("isOSC", false);
- }
-
- {
- NamedAudioGraphIOProcessor* const proc(
- new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
- proc->setNames(true, channelNames);
-
- AudioProcessorGraph::Node* const node(graph.addNode(proc));
- node->properties.set("isPlugin", false);
- node->properties.set("isOutput", false);
- node->properties.set("isAudio", true);
- node->properties.set("isCV", false);
- node->properties.set("isMIDI", false);
- node->properties.set("isOSC", false);
- }
-
- {
- NamedAudioGraphIOProcessor* const proc(
- new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
- AudioProcessorGraph::Node* const node(graph.addNode(proc));
- node->properties.set("isPlugin", false);
- node->properties.set("isOutput", false);
- node->properties.set("isAudio", false);
- node->properties.set("isCV", false);
- node->properties.set("isMIDI", true);
- node->properties.set("isOSC", false);
- }
-
- {
- NamedAudioGraphIOProcessor* const proc(
- new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
- AudioProcessorGraph::Node* const node(graph.addNode(proc));
- node->properties.set("isPlugin", false);
- node->properties.set("isOutput", true);
- node->properties.set("isAudio", false);
- node->properties.set("isCV", false);
- node->properties.set("isMIDI", true);
- node->properties.set("isOSC", false);
- }
-
- startThread();
- }
-
- PatchbayGraph::~PatchbayGraph()
- {
- stopThread(-1);
-
- connections.clear();
- extGraph.clear();
-
- graph.releaseResources();
- graph.clear();
- audioBuffer.clear();
- }
-
- void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
- {
- const int bufferSizei(static_cast<int>(bufferSize));
-
- graph.releaseResources();
- graph.prepareToPlay(kEngine->getSampleRate(), bufferSizei);
- audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSizei);
- }
-
- void PatchbayGraph::setSampleRate(const double sampleRate)
- {
- graph.releaseResources();
- graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
- }
-
- void PatchbayGraph::setOffline(const bool offline)
- {
- graph.setNonRealtime(offline);
- }
-
- void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
- {
- CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
- carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
-
- CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
- AudioProcessorGraph::Node* const node(graph.addNode(instance));
- CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
-
- plugin->setPatchbayNodeId(node->nodeId);
-
- node->properties.set("isPlugin", true);
- node->properties.set("pluginId", static_cast<int>(plugin->getId()));
-
- if (! usingExternal)
- addNodeToPatchbay(plugin->getEngine(), node->nodeId, static_cast<int>(plugin->getId()), instance);
- }
-
- void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
- {
- CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
- CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
-
- AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
- CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
-
- if (! usingExternal)
- {
- disconnectInternalGroup(oldNode->nodeId);
- removeNodeFromPatchbay(kEngine, oldNode->nodeId, oldNode->getProcessor());
- }
-
- ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
-
- graph.removeNode(oldNode->nodeId);
-
- CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
- AudioProcessorGraph::Node* const node(graph.addNode(instance));
- CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
-
- newPlugin->setPatchbayNodeId(node->nodeId);
-
- node->properties.set("isPlugin", true);
- node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
-
- if (! usingExternal)
- addNodeToPatchbay(newPlugin->getEngine(), node->nodeId, static_cast<int>(newPlugin->getId()), instance);
- }
-
- void PatchbayGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
- {
- CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
- carla_debug("PatchbayGraph::renamePlugin(%p)", plugin, newName);
-
- AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
- CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
-
- if (! usingExternal)
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED, node->nodeId, 0, 0, 0.0f, newName);
- }
-
- void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
- {
- CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
- carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
-
- AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
- CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
-
- if (! usingExternal)
- {
- disconnectInternalGroup(node->nodeId);
- removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
- }
-
- ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
-
- // Fix plugin Ids properties
- for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
- {
- CarlaPlugin* const plugin2(kEngine->getPlugin(i));
- CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
-
- if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
- {
- CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
- node2->properties.set("pluginId", static_cast<int>(i-1));
- }
- }
-
- CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
- }
-
- void PatchbayGraph::removeAllPlugins()
- {
- carla_debug("PatchbayGraph::removeAllPlugins()");
-
- for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
- {
- CarlaPlugin* const plugin(kEngine->getPlugin(i));
- CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
-
- AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
- CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
-
- if (! usingExternal)
- {
- disconnectInternalGroup(node->nodeId);
- removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
- }
-
- ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
-
- graph.removeNode(node->nodeId);
- }
- }
-
- bool PatchbayGraph::connect(const bool external, const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback)
- {
- if (external)
- return extGraph.connect(groupA, portA, groupB, portB, sendCallback);
-
- uint adjustedPortA = portA;
- uint adjustedPortB = portB;
-
- if (! adjustPatchbayPortIdForWater(adjustedPortA))
- return false;
- if (! adjustPatchbayPortIdForWater(adjustedPortB))
- return false;
-
- if (! graph.addConnection(groupA, static_cast<int>(adjustedPortA), groupB, static_cast<int>(adjustedPortB)))
- {
- kEngine->setLastError("Failed from water");
- return false;
- }
-
- ConnectionToId connectionToId;
- connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
- std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
-
- if (sendCallback)
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- connections.list.append(connectionToId);
- return true;
- }
-
- bool PatchbayGraph::disconnect(const uint connectionId)
- {
- if (usingExternal)
- return extGraph.disconnect(connectionId);
-
- for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
- {
- static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
-
- const ConnectionToId& connectionToId(it.getValue(fallback));
- CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
-
- if (connectionToId.id != connectionId)
- continue;
-
- uint adjustedPortA = connectionToId.portA;
- uint adjustedPortB = connectionToId.portB;
-
- if (! adjustPatchbayPortIdForWater(adjustedPortA))
- return false;
- if (! adjustPatchbayPortIdForWater(adjustedPortB))
- return false;
-
- if (! graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
- connectionToId.groupB, static_cast<int>(adjustedPortB)))
- return false;
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
-
- connections.list.remove(it);
- return true;
- }
-
- kEngine->setLastError("Failed to find connection");
- return false;
- }
-
- void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
- {
- CARLA_SAFE_ASSERT(! usingExternal);
-
- for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
- {
- static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
-
- const ConnectionToId& connectionToId(it.getValue(fallback));
- CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
-
- if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
- continue;
-
- /*
- uint adjustedPortA = connectionToId.portA;
- uint adjustedPortB = connectionToId.portB;
-
- if (! adjustPatchbayPortIdForWater(adjustedPortA))
- return false;
- if (! adjustPatchbayPortIdForWater(adjustedPortB))
- return false;
-
- graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
- connectionToId.groupB, static_cast<int>(adjustedPortB));
- */
-
- if (! usingExternal)
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
-
- connections.list.remove(it);
- }
- }
-
- void PatchbayGraph::refresh(const char* const deviceName)
- {
- if (usingExternal)
- return extGraph.refresh(deviceName);
-
- CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
-
- connections.clear();
- graph.removeIllegalConnections();
-
- for (int i=0, count=graph.getNumNodes(); i<count; ++i)
- {
- AudioProcessorGraph::Node* const node(graph.getNode(i));
- CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
-
- AudioProcessor* const proc(node->getProcessor());
- CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
-
- int clientId = -1;
-
- // plugin node
- if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
- clientId = node->properties.getWithDefault("pluginId", -1);
-
- addNodeToPatchbay(kEngine, node->nodeId, clientId, proc);
- }
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
-
- for (int i=0, count=graph.getNumConnections(); i<count; ++i)
- {
- const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
- CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
- CARLA_SAFE_ASSERT_CONTINUE(conn->sourceChannelIndex >= 0);
- CARLA_SAFE_ASSERT_CONTINUE(conn->destChannelIndex >= 0);
-
- const uint groupA = conn->sourceNodeId;
- const uint groupB = conn->destNodeId;
-
- uint portA = static_cast<uint>(conn->sourceChannelIndex);
- uint portB = static_cast<uint>(conn->destChannelIndex);
-
- if (portA == kMidiChannelIndex)
- portA = kMidiOutputPortOffset;
- else
- portA += kAudioOutputPortOffset;
-
- if (portB == kMidiChannelIndex)
- portB = kMidiInputPortOffset;
- else
- portB += kAudioInputPortOffset;
-
- ConnectionToId connectionToId;
- connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
-
- kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- connections.list.append(connectionToId);
- }
- }
-
- const char* const* PatchbayGraph::getConnections(const bool external) const
- {
- if (external)
- return extGraph.getConnections();
-
- if (connections.list.count() == 0)
- return nullptr;
-
- CarlaStringList connList;
-
- for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
- {
- static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
-
- const ConnectionToId& connectionToId(it.getValue(fallback));
- CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
-
- AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
- CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
-
- AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
- CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
-
- AudioProcessor* const procA(nodeA->getProcessor());
- CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
-
- AudioProcessor* const procB(nodeB->getProcessor());
- CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
-
- String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
- CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
-
- String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
- CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
-
- connList.append(fullPortNameA.toRawUTF8());
- connList.append(fullPortNameB.toRawUTF8());
- }
-
- if (connList.count() == 0)
- return nullptr;
-
- retCon = connList.toCharStringListPtr();
-
- return retCon;
- }
-
- bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
- {
- if (external)
- return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
-
- String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
- String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
-
- for (int i=0, count=graph.getNumNodes(); i<count; ++i)
- {
- AudioProcessorGraph::Node* const node(graph.getNode(i));
- CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
-
- AudioProcessor* const proc(node->getProcessor());
- CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
-
- if (proc->getName() != groupName)
- continue;
-
- groupId = node->nodeId;
-
- if (portName == "events-in")
- {
- portId = kMidiInputPortOffset;
- return true;
- }
-
- if (portName == "events-out")
- {
- portId = kMidiOutputPortOffset;
- return true;
- }
-
- for (int j=0, numInputs=proc->getTotalNumInputChannels(); j<numInputs; ++j)
- {
- if (proc->getInputChannelName(j) != portName)
- continue;
-
- portId = kAudioInputPortOffset+static_cast<uint>(j);
- return true;
- }
-
- for (int j=0, numOutputs=proc->getTotalNumOutputChannels(); j<numOutputs; ++j)
- {
- if (proc->getOutputChannelName(j) != portName)
- continue;
-
- portId = kAudioOutputPortOffset+static_cast<uint>(j);
- return true;
- }
- }
-
- return false;
- }
-
- void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const int frames)
- {
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(frames > 0,);
-
- // put events in water buffer
- {
- midiBuffer.clear();
- fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
- }
-
- // set audio buffer size, needed for water internals
- audioBuffer.setSizeRT(frames);
-
- // put carla audio in water buffer
- {
- int i=0;
-
- for (; i < static_cast<int>(inputs); ++i)
- audioBuffer.copyFrom(i, 0, inBuf[i], frames);
-
- // clear remaining channels
- for (const int count=audioBuffer.getNumChannels(); i<count; ++i)
- audioBuffer.clear(i, 0, frames);
- }
-
- // ready to go!
- graph.processBlock(audioBuffer, midiBuffer);
-
- // put water audio in carla buffer
- {
- for (int i=0; i < static_cast<int>(outputs); ++i)
- carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
- }
-
- // put water events in carla buffer
- {
- carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
- fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
- midiBuffer.clear();
- }
- }
-
- void PatchbayGraph::run()
- {
- while (! shouldThreadExit())
- {
- carla_msleep(100);
- graph.reorderNowIfNeeded();
- }
- }
-
- // -----------------------------------------------------------------------
- // InternalGraph
-
- EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
- : fIsReady(false),
- fRack(nullptr),
- kEngine(engine) {}
-
- EngineInternalGraph::~EngineInternalGraph() noexcept
- {
- CARLA_SAFE_ASSERT(! fIsReady);
- CARLA_SAFE_ASSERT(fRack == nullptr);
- }
-
- void EngineInternalGraph::create(const uint32_t inputs, const uint32_t outputs)
- {
- fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
-
- if (fIsRack)
- {
- CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
- fRack = new RackGraph(kEngine, inputs, outputs);
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
- fPatchbay = new PatchbayGraph(kEngine, inputs, outputs);
- }
-
- fIsReady = true;
- }
-
- void EngineInternalGraph::destroy() noexcept
- {
- if (! fIsReady)
- {
- CARLA_SAFE_ASSERT(fRack == nullptr);
- return;
- }
-
- if (fIsRack)
- {
- CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
- delete fRack;
- fRack = nullptr;
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- delete fPatchbay;
- fPatchbay = nullptr;
- }
-
- fIsReady = false;
- }
-
- void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
- {
- ScopedValueSetter<bool> svs(fIsReady, false, true);
-
- if (fIsRack)
- {
- CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
- fRack->setBufferSize(bufferSize);
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->setBufferSize(bufferSize);
- }
- }
-
- void EngineInternalGraph::setSampleRate(const double sampleRate)
- {
- ScopedValueSetter<bool> svs(fIsReady, false, true);
-
- if (fIsRack)
- {
- CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->setSampleRate(sampleRate);
- }
- }
-
- void EngineInternalGraph::setOffline(const bool offline)
- {
- ScopedValueSetter<bool> svs(fIsReady, false, true);
-
- if (fIsRack)
- {
- CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
- fRack->setOffline(offline);
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->setOffline(offline);
- }
- }
-
- bool EngineInternalGraph::isReady() const noexcept
- {
- return fIsReady;
- }
-
- RackGraph* EngineInternalGraph::getRackGraph() const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
- return fRack;
- }
-
- PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
- return fPatchbay;
- }
-
- void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
- {
- if (fIsRack)
- {
- CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
- fRack->processHelper(data, inBuf, outBuf, frames);
- }
- else
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->process(data, inBuf, outBuf, static_cast<int>(frames));
- }
- }
-
- void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
- {
- CARLA_SAFE_ASSERT_RETURN(fIsRack,);
- CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
-
- fRack->process(data, inBuf, outBuf, frames);
- }
-
- // -----------------------------------------------------------------------
- // used for internal patchbay mode
-
- void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->addPlugin(plugin);
- }
-
- void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->replacePlugin(oldPlugin, newPlugin);
- }
-
- void EngineInternalGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->renamePlugin(plugin, newName);
- }
-
- void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->removePlugin(plugin);
- }
-
- void EngineInternalGraph::removeAllPlugins()
- {
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->removeAllPlugins();
- }
-
- bool EngineInternalGraph::isUsingExternal() const noexcept
- {
- if (fIsRack)
- return true;
-
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
- return fPatchbay->usingExternal;
- }
-
- void EngineInternalGraph::setUsingExternal(const bool usingExternal) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
- CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
- fPatchbay->usingExternal = usingExternal;
- }
-
- // -----------------------------------------------------------------------
- // CarlaEngine Patchbay stuff
-
- bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
- {
- CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
- CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
- carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- RackGraph* const graph = pData->graph.getRackGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
-
- return graph->connect(groupA, portA, groupB, portB);
- }
- else
- {
- PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
-
- return graph->connect(graph->usingExternal, groupA, portA, groupB, portB, true);
- }
-
- return false;
- }
-
- bool CarlaEngine::patchbayDisconnect(const uint connectionId)
- {
- CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
- CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
- carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- RackGraph* const graph = pData->graph.getRackGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
-
- return graph->disconnect(connectionId);
- }
- else
- {
- PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
-
- return graph->disconnect(connectionId);
- }
-
- return false;
- }
-
- bool CarlaEngine::patchbayRefresh(const bool external)
- {
- // subclasses should handle this
- CARLA_SAFE_ASSERT_RETURN(! external, false);
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- // This is implemented in engine subclasses
- setLastError("Unsupported operation");
- return false;
- }
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
- {
- PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
-
- graph->refresh("");
- return true;
- }
-
- setLastError("Unsupported operation");
- return false;
- }
-
- // -----------------------------------------------------------------------
-
- const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
- {
- CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
- carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- RackGraph* const graph = pData->graph.getRackGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
- CARLA_SAFE_ASSERT_RETURN(external, nullptr);
-
- return graph->getConnections();
- }
- else
- {
- PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
-
- return graph->getConnections(external);
- }
-
- return nullptr;
- }
-
- void CarlaEngine::restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback)
- {
- CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
- CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
- CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
- carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
-
- uint groupA, portA;
- uint groupB, portB;
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- RackGraph* const graph = pData->graph.getRackGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(external,);
-
- if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
- return;
- if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
- return;
-
- graph->connect(groupA, portA, groupB, portB);
- }
- else
- {
- PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
-
- if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
- return;
- if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
- return;
-
- graph->connect(external, groupA, portA, groupB, portB, sendCallback);
- }
- }
-
- // -----------------------------------------------------------------------
-
- bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
- {
- CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
- CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
-
- RackGraph* const graph(pData->graph.getRackGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
-
- const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
-
- switch (connectionType)
- {
- case kExternalGraphConnectionAudioIn1:
- return graph->audioBuffers.connectedIn1.append(portId);
- case kExternalGraphConnectionAudioIn2:
- return graph->audioBuffers.connectedIn2.append(portId);
- case kExternalGraphConnectionAudioOut1:
- return graph->audioBuffers.connectedOut1.append(portId);
- case kExternalGraphConnectionAudioOut2:
- return graph->audioBuffers.connectedOut2.append(portId);
- }
-
- return false;
- }
-
- bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
- {
- CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
- CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
-
- RackGraph* const graph(pData->graph.getRackGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
-
- const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
-
- switch (connectionType)
- {
- case kExternalGraphConnectionAudioIn1:
- return graph->audioBuffers.connectedIn1.removeOne(portId);
- case kExternalGraphConnectionAudioIn2:
- return graph->audioBuffers.connectedIn2.removeOne(portId);
- case kExternalGraphConnectionAudioOut1:
- return graph->audioBuffers.connectedOut1.removeOne(portId);
- case kExternalGraphConnectionAudioOut2:
- return graph->audioBuffers.connectedOut2.removeOne(portId);
- }
-
- return false;
- }
-
- // -----------------------------------------------------------------------
-
- CARLA_BACKEND_END_NAMESPACE
|