|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320 |
- /*
- * Carla Plugin Bridge
- * Copyright (C) 2011-2023 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 "CarlaPluginInternal.hpp"
-
- #include "CarlaBackendUtils.hpp"
- #include "CarlaBase64Utils.hpp"
- #include "CarlaBridgeUtils.hpp"
- #include "CarlaEngineUtils.hpp"
- #include "CarlaMathUtils.hpp"
- #include "CarlaPipeUtils.hpp"
- #include "CarlaScopeUtils.hpp"
- #include "CarlaShmUtils.hpp"
- #include "CarlaTimeUtils.hpp"
- #include "CarlaThread.hpp"
-
- #include "jackbridge/JackBridge.hpp"
-
- #include <ctime>
-
- #include "water/files/File.h"
- #include "water/misc/Time.h"
- #include "water/threads/ChildProcess.h"
-
- // --------------------------------------------------------------------------------------------------------------------
-
- using water::ChildProcess;
- using water::File;
- using water::String;
- using water::StringArray;
-
- CARLA_BACKEND_START_NAMESPACE
-
- // --------------------------------------------------------------------------------------------------------------------
- // Fallback data
-
- static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
-
- // --------------------------------------------------------------------------------------------------------------------
-
- static String findWinePrefix(const String filename, const int recursionLimit = 10)
- {
- if (recursionLimit == 0 || filename.length() < 5 || ! filename.contains("/"))
- return "";
-
- const String path(filename.upToLastOccurrenceOf("/", false, false));
-
- if (File(path + "/dosdevices").isDirectory())
- return path;
-
- return findWinePrefix(path, recursionLimit-1);
- }
-
- // --------------------------------------------------------------------------------------------------------------------
-
- struct BridgeParamInfo {
- float value;
- CarlaString name;
- CarlaString symbol;
- CarlaString unit;
-
- BridgeParamInfo() noexcept
- : value(0.0f),
- name(),
- symbol(),
- unit() {}
-
- CARLA_DECLARE_NON_COPYABLE(BridgeParamInfo)
- };
-
- struct BridgeTextReader {
- char* text;
-
- BridgeTextReader(BridgeNonRtServerControl& nonRtServerCtrl)
- : text(nullptr)
- {
- const uint32_t size = nonRtServerCtrl.readUInt();
- CARLA_SAFE_ASSERT_RETURN(size != 0,);
-
- text = new char[size + 1];
- nonRtServerCtrl.readCustomData(text, size);
- text[size] = '\0';
- }
-
- BridgeTextReader(BridgeNonRtServerControl& nonRtServerCtrl, const uint32_t size)
- : text(nullptr)
- {
- text = new char[size + 1];
-
- if (size != 0)
- nonRtServerCtrl.readCustomData(text, size);
-
- text[size] = '\0';
- }
-
- ~BridgeTextReader() noexcept
- {
- delete[] text;
- }
-
- CARLA_DECLARE_NON_COPYABLE(BridgeTextReader)
- };
-
- // --------------------------------------------------------------------------------------------------------------------
-
- class CarlaPluginBridgeThread : public CarlaThread
- {
- public:
- CarlaPluginBridgeThread(CarlaEngine* const engine, CarlaPlugin* const plugin) noexcept
- : CarlaThread("CarlaPluginBridgeThread"),
- kEngine(engine),
- kPlugin(plugin),
- fBinaryArchName(),
- fBridgeBinary(),
- fLabel(),
- fShmIds(),
- #ifndef CARLA_OS_WIN
- fWinePrefix(),
- #endif
- fProcess() {}
-
- void setData(
- #ifndef CARLA_OS_WIN
- const char* const winePrefix,
- #endif
- const char* const binaryArchName,
- const char* const bridgeBinary,
- const char* const label,
- const char* const shmIds) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(bridgeBinary != nullptr && bridgeBinary[0] != '\0',);
- CARLA_SAFE_ASSERT_RETURN(shmIds != nullptr && shmIds[0] != '\0',);
- CARLA_SAFE_ASSERT(! isThreadRunning());
-
- #ifndef CARLA_OS_WIN
- fWinePrefix = winePrefix;
- #endif
- fBinaryArchName = binaryArchName;
- fBridgeBinary = bridgeBinary;
- fShmIds = shmIds;
-
- if (label != nullptr)
- fLabel = label;
- if (fLabel.isEmpty())
- fLabel = "(none)";
- }
-
- uintptr_t getProcessPID() const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
-
- return (uintptr_t)fProcess->getPID();
- }
-
- protected:
- void run()
- {
- if (fProcess == nullptr)
- {
- fProcess = new ChildProcess();
- }
- else if (fProcess->isRunning())
- {
- carla_stderr("CarlaPluginBridgeThread::run() - already running");
- }
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
-
- const EngineOptions& options(kEngine->getOptions());
-
- String filename(kPlugin->getFilename());
-
- if (filename.isEmpty())
- filename = "(none)";
-
- StringArray arguments;
-
- #ifndef CARLA_OS_WIN
- // start with "wine" if needed
- if (fBridgeBinary.endsWithIgnoreCase(".exe"))
- {
- String wineCMD;
-
- if (options.wine.executable != nullptr && options.wine.executable[0] != '\0')
- {
- wineCMD = options.wine.executable;
-
- if (fBridgeBinary.endsWithIgnoreCase("64.exe")
- && options.wine.executable[0] == CARLA_OS_SEP
- && File(wineCMD + "64").existsAsFile())
- wineCMD += "64";
- }
- else
- {
- wineCMD = "wine";
- }
-
- arguments.add(wineCMD);
- }
- #endif
-
- // setup binary arch
- ChildProcess::Type childType;
- #ifdef CARLA_OS_MAC
- if (fBinaryArchName == "arm64")
- childType = ChildProcess::TypeARM;
- else if (fBinaryArchName == "x86_64")
- childType = ChildProcess::TypeIntel;
- else
- #endif
- childType = ChildProcess::TypeAny;
-
- // bridge binary
- arguments.add(fBridgeBinary);
-
- // plugin type
- arguments.add(getPluginTypeAsString(kPlugin->getType()));
-
- // filename
- arguments.add(filename);
-
- // label
- arguments.add(fLabel);
-
- // uniqueId
- arguments.add(String(static_cast<water::int64>(kPlugin->getUniqueId())));
-
- bool started;
-
- {
- const ScopedEngineEnvironmentLocker _seel(kEngine);
-
- #ifdef CARLA_OS_LINUX
- const CarlaScopedEnvVar sev1("LD_LIBRARY_PATH", nullptr);
- const CarlaScopedEnvVar sev2("LD_PRELOAD", nullptr);
- #endif
-
- carla_setenv("ENGINE_OPTION_FORCE_STEREO", bool2str(options.forceStereo));
- carla_setenv("ENGINE_OPTION_PREFER_PLUGIN_BRIDGES", bool2str(options.preferPluginBridges));
- carla_setenv("ENGINE_OPTION_PREFER_UI_BRIDGES", bool2str(options.preferUiBridges));
- carla_setenv("ENGINE_OPTION_UIS_ALWAYS_ON_TOP", bool2str(options.uisAlwaysOnTop));
-
- std::snprintf(strBuf, STR_MAX, "%u", options.maxParameters);
- carla_setenv("ENGINE_OPTION_MAX_PARAMETERS", strBuf);
-
- std::snprintf(strBuf, STR_MAX, "%u", options.uiBridgesTimeout);
- carla_setenv("ENGINE_OPTION_UI_BRIDGES_TIMEOUT",strBuf);
-
- if (options.pathLADSPA != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", options.pathLADSPA);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", "");
-
- if (options.pathDSSI != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", options.pathDSSI);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", "");
-
- if (options.pathLV2 != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", options.pathLV2);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", "");
-
- if (options.pathVST2 != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", options.pathVST2);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", "");
-
- if (options.pathVST3 != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", options.pathVST3);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", "");
-
- if (options.pathSF2 != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", options.pathSF2);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", "");
-
- if (options.pathSFZ != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", options.pathSFZ);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", "");
-
- if (options.pathJSFX != nullptr)
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_JSFX", options.pathJSFX);
- else
- carla_setenv("ENGINE_OPTION_PLUGIN_PATH_JSFX", "");
-
- if (options.binaryDir != nullptr)
- carla_setenv("ENGINE_OPTION_PATH_BINARIES", options.binaryDir);
- else
- carla_setenv("ENGINE_OPTION_PATH_BINARIES", "");
-
- if (options.resourceDir != nullptr)
- carla_setenv("ENGINE_OPTION_PATH_RESOURCES", options.resourceDir);
- else
- carla_setenv("ENGINE_OPTION_PATH_RESOURCES", "");
-
- carla_setenv("ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR", bool2str(options.preventBadBehaviour));
-
- std::snprintf(strBuf, STR_MAX, P_UINTPTR, options.frontendWinId);
- carla_setenv("ENGINE_OPTION_FRONTEND_WIN_ID", strBuf);
-
- carla_setenv("ENGINE_BRIDGE_SHM_IDS", fShmIds.toRawUTF8());
-
- #ifndef CARLA_OS_WIN
- if (fWinePrefix.isNotEmpty())
- {
- carla_setenv("WINEDEBUG", "-all");
- carla_setenv("WINEPREFIX", fWinePrefix.toRawUTF8());
-
- if (options.wine.rtPrio)
- {
- carla_setenv("STAGING_SHARED_MEMORY", "1");
- carla_setenv("WINE_RT_POLICY", "FF");
-
- std::snprintf(strBuf, STR_MAX, "%i", options.wine.baseRtPrio);
- carla_setenv("STAGING_RT_PRIORITY_BASE", strBuf);
- carla_setenv("WINE_RT", strBuf);
- carla_setenv("WINE_RT_PRIO", strBuf);
-
- std::snprintf(strBuf, STR_MAX, "%i", options.wine.serverRtPrio);
- carla_setenv("STAGING_RT_PRIORITY_SERVER", strBuf);
- carla_setenv("WINE_SVR_RT", strBuf);
-
- carla_stdout("Using WINEPREFIX '%s', with base RT prio %i and server RT prio %i",
- fWinePrefix.toRawUTF8(), options.wine.baseRtPrio, options.wine.serverRtPrio);
- }
- else
- {
- carla_unsetenv("STAGING_SHARED_MEMORY");
- carla_unsetenv("WINE_RT_POLICY");
- carla_unsetenv("STAGING_RT_PRIORITY_BASE");
- carla_unsetenv("STAGING_RT_PRIORITY_SERVER");
- carla_unsetenv("WINE_RT");
- carla_unsetenv("WINE_RT_PRIO");
- carla_unsetenv("WINE_SVR_RT");
-
- carla_stdout("Using WINEPREFIX '%s', without RT priorities", fWinePrefix.toRawUTF8());
- }
- }
- #endif
-
- carla_stdout("Starting plugin bridge, command is:\n%s \"%s\" \"%s\" \"%s\" " P_INT64,
- fBridgeBinary.toRawUTF8(), getPluginTypeAsString(kPlugin->getType()), filename.toRawUTF8(), fLabel.toRawUTF8(), kPlugin->getUniqueId());
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- const File projFolder(kEngine->getCurrentProjectFolder());
-
- if (projFolder.isNotNull())
- {
- const File oldFolder(File::getCurrentWorkingDirectory());
- projFolder.setAsCurrentWorkingDirectory();
- started = fProcess->start(arguments, childType);
- oldFolder.setAsCurrentWorkingDirectory();
- }
- else
- #endif
- {
- started = fProcess->start(arguments, childType);
- }
- }
-
- if (! started)
- {
- carla_stdout("failed!");
- fProcess = nullptr;
- return;
- }
-
- for (; fProcess->isRunning() && ! shouldThreadExit();)
- carla_sleep(1);
-
- // we only get here if bridge crashed or thread asked to exit
- if (fProcess->isRunning() && shouldThreadExit())
- {
- fProcess->waitForProcessToFinish(2000);
-
- if (fProcess->isRunning())
- {
- carla_stdout("CarlaPluginBridgeThread::run() - bridge refused to close, force kill now");
- fProcess->kill();
- }
- else
- {
- carla_stdout("CarlaPluginBridgeThread::run() - bridge auto-closed successfully");
- }
- }
- else
- {
- // forced quit, may have crashed
- if (fProcess->getExitCodeAndClearPID() != 0)
- {
- carla_stderr("CarlaPluginBridgeThread::run() - bridge crashed");
-
- CarlaString errorString("Plugin '" + CarlaString(kPlugin->getName()) + "' has crashed!\n"
- "Saving now will lose its current settings.\n"
- "Please remove this plugin, and not rely on it from this point.");
- kEngine->callback(true, true,
- ENGINE_CALLBACK_ERROR, kPlugin->getId(), 0, 0, 0, 0.0f, errorString);
- }
- }
-
- fProcess = nullptr;
- }
-
- private:
- CarlaEngine* const kEngine;
- CarlaPlugin* const kPlugin;
-
- String fBinaryArchName;
- String fBridgeBinary;
- String fLabel;
- String fShmIds;
- #ifndef CARLA_OS_WIN
- String fWinePrefix;
- #endif
-
- CarlaScopedPointer<ChildProcess> fProcess;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridgeThread)
- };
-
- // ---------------------------------------------------------------------------------------------------------------------
-
- class CarlaPluginBridge : public CarlaPlugin
- {
- public:
- CarlaPluginBridge(CarlaEngine* const engine, const uint id, const BinaryType btype, const PluginType ptype)
- : CarlaPlugin(engine, id),
- fBinaryType(btype),
- fPluginType(ptype),
- fBridgeVersion(6), // before kPluginBridgeNonRtServerVersion was a thing, API was at 6
- fInitiated(false),
- fInitError(false),
- fSaved(true),
- fTimedOut(false),
- fTimedError(false),
- fBufferSize(engine->getBufferSize()),
- fProcWaitTime(0),
- fPendingEmbedCustomUI(0),
- fBridgeBinary(),
- fBridgeThread(engine, this),
- fShmAudioPool(),
- fShmRtClientControl(),
- fShmNonRtClientControl(),
- fShmNonRtServerControl(),
- #ifndef CARLA_OS_WIN
- fWinePrefix(),
- #endif
- fReceivingParamText(),
- fInfo(),
- fUniqueId(0),
- fLatency(0),
- fParams(nullptr)
- {
- carla_debug("CarlaPluginBridge::CarlaPluginBridge(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
-
- pData->hints |= PLUGIN_IS_BRIDGE;
- }
-
- ~CarlaPluginBridge() override
- {
- carla_debug("CarlaPluginBridge::~CarlaPluginBridge()");
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- // close UI
- if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
- pData->transientTryCounter = 0;
- #endif
-
- pData->singleMutex.lock();
- pData->masterMutex.lock();
-
- if (pData->client != nullptr && pData->client->isActive())
- pData->client->deactivate(true);
-
- if (pData->active)
- {
- deactivate();
- pData->active = false;
- }
-
- if (fBridgeThread.isThreadRunning())
- {
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientQuit);
- fShmNonRtClientControl.commitWrite();
-
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientQuit);
- fShmRtClientControl.commitWrite();
-
- if (! fTimedOut)
- waitForClient("stopping", 3000);
- }
-
- fBridgeThread.stopThread(3000);
-
- fShmNonRtServerControl.clear();
- fShmNonRtClientControl.clear();
- fShmRtClientControl.clear();
- fShmAudioPool.clear();
-
- clearBuffers();
-
- fInfo.chunk.clear();
- }
-
- // -------------------------------------------------------------------
- // Information (base)
-
- BinaryType getBinaryType() const noexcept override
- {
- return fBinaryType;
- }
-
- PluginType getType() const noexcept override
- {
- return fPluginType;
- }
-
- PluginCategory getCategory() const noexcept override
- {
- return fInfo.category;
- }
-
- int64_t getUniqueId() const noexcept override
- {
- return fUniqueId;
- }
-
- uint32_t getLatencyInFrames() const noexcept override
- {
- return fLatency;
- }
-
- // -------------------------------------------------------------------
- // Information (count)
-
- uint32_t getMidiInCount() const noexcept override
- {
- return fInfo.mIns;
- }
-
- uint32_t getMidiOutCount() const noexcept override
- {
- return fInfo.mOuts;
- }
-
- // -------------------------------------------------------------------
- // Information (current data)
-
- std::size_t getChunkData(void** const dataPtr) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
- CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
-
- waitForSaved();
-
- CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.size() > 0, 0);
-
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- *dataPtr = fInfo.chunk.data();
- #else
- *dataPtr = &fInfo.chunk.front();
- #endif
- return fInfo.chunk.size();
- }
-
- // -------------------------------------------------------------------
- // Information (per-plugin data)
-
- uint getOptionsAvailable() const noexcept override
- {
- return fInfo.optionsAvailable;
- }
-
- float getParameterValue(const uint32_t parameterId) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
-
- return fParams[parameterId].value;
- }
-
- bool getLabel(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.label, STR_MAX);
- return true;
- }
-
- bool getMaker(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.maker, STR_MAX);
- return true;
- }
-
- bool getCopyright(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.copyright, STR_MAX);
- return true;
- }
-
- bool getRealName(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.name, STR_MAX);
- return true;
- }
-
- bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
-
- std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
- return true;
- }
-
- bool getParameterText(const uint32_t parameterId, char* const strBuf) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
- CARLA_SAFE_ASSERT_RETURN(! fReceivingParamText.isCurrentlyWaitingData(), false);
-
- const int32_t parameterIdi = static_cast<int32_t>(parameterId);
- fReceivingParamText.setTargetData(parameterIdi, strBuf);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientGetParameterText);
- fShmNonRtClientControl.writeInt(parameterIdi);
- fShmNonRtClientControl.commitWrite();
- }
-
- if (waitForParameterText())
- return true;
-
- std::snprintf(strBuf, STR_MAX, "%.12g", static_cast<double>(fParams[parameterId].value));
- return false;
- }
-
- bool getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
-
- std::strncpy(strBuf, fParams[parameterId].symbol.buffer(), STR_MAX);
- return true;
- }
-
- bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
-
- std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
- return true;
- }
-
- // -------------------------------------------------------------------
- // Set data (state)
-
- void prepareForSave(bool) noexcept override
- {
- fSaved = false;
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPrepareForSave);
- fShmNonRtClientControl.commitWrite();
- }
- }
-
- bool waitForParameterText()
- {
- bool success;
- if (fReceivingParamText.wasDataReceived(&success))
- return success;
-
- const uint32_t timeoutEnd = carla_gettime_ms() + 500; // 500 ms
- const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
-
- for (; carla_gettime_ms() < timeoutEnd && fBridgeThread.isThreadRunning();)
- {
- if (fReceivingParamText.wasDataReceived(&success))
- return success;
-
- if (needsEngineIdle)
- pData->engine->idle();
-
- carla_msleep(5);
- }
-
- if (! fBridgeThread.isThreadRunning())
- {
- carla_stderr("CarlaPluginBridge::waitForParameterText() - Bridge is not running");
- return false;
- }
-
- carla_stderr("CarlaPluginBridge::waitForParameterText() - Timeout while requesting text");
- return false;
- }
-
- void waitForSaved()
- {
- if (fSaved)
- return;
-
- // TODO: only wait 1 minute for NI plugins
- const uint32_t timeoutEnd = carla_gettime_ms() + 60*1000; // 60 secs, 1 minute
- const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
-
- for (; carla_gettime_ms() < timeoutEnd && fBridgeThread.isThreadRunning();)
- {
- pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
-
- if (needsEngineIdle)
- pData->engine->idle();
-
- if (fSaved)
- break;
-
- carla_msleep(20);
- }
-
- if (! fBridgeThread.isThreadRunning())
- return carla_stderr("CarlaPluginBridge::waitForSaved() - Bridge is not running");
- if (! fSaved)
- return carla_stderr("CarlaPluginBridge::waitForSaved() - Timeout while requesting save state");
- }
-
- // -------------------------------------------------------------------
- // Set data (internal stuff)
-
- void setOption(const uint option, const bool yesNo, const bool sendCallback) override
- {
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOption);
- fShmNonRtClientControl.writeUInt(option);
- fShmNonRtClientControl.writeBool(yesNo);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setOption(option, yesNo, sendCallback);
- }
-
- void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCtrlChannel);
- fShmNonRtClientControl.writeShort(channel);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
- }
-
- void setName(const char* const newName) override
- {
- CarlaPlugin::setName(newName);
-
- if (pData->uiTitle.isEmpty() && fBridgeVersion >= 8)
- _setUiTitleFromName();
- }
-
- // -------------------------------------------------------------------
- // Set data (plugin-specific stuff)
-
- void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
- CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
-
- const float fixedValue(pData->param.getFixedValue(parameterId, value));
- fParams[parameterId].value = fixedValue;
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
- fShmNonRtClientControl.writeUInt(parameterId);
- fShmNonRtClientControl.writeFloat(value);
- fShmNonRtClientControl.commitWrite();
- fShmNonRtClientControl.waitIfDataIsReachingLimit();
- }
-
- CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
- }
-
- void setParameterValueRT(const uint32_t parameterId, const float value, const uint32_t frameOffset, const bool sendCallbackLater) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
-
- const float fixedValue(pData->param.getFixedValue(parameterId, value));
- fParams[parameterId].value = fixedValue;
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
- fShmNonRtClientControl.writeUInt(parameterId);
- fShmNonRtClientControl.writeFloat(value);
- fShmNonRtClientControl.commitWrite();
- fShmNonRtClientControl.waitIfDataIsReachingLimit();
- }
-
- CarlaPlugin::setParameterValueRT(parameterId, fixedValue, frameOffset, sendCallbackLater);
- }
-
- void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
- CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
- CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMidiChannel);
- fShmNonRtClientControl.writeUInt(parameterId);
- fShmNonRtClientControl.writeByte(channel);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
- }
-
- void setParameterMappedControlIndex(const uint32_t parameterId, const int16_t index,
- const bool sendOsc, const bool sendCallback,
- const bool reconfigureNow) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
- CARLA_SAFE_ASSERT_RETURN(index >= CONTROL_INDEX_NONE && index <= CONTROL_INDEX_MAX_ALLOWED,);
- CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedControlIndex);
- fShmNonRtClientControl.writeUInt(parameterId);
- fShmNonRtClientControl.writeShort(index);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setParameterMappedControlIndex(parameterId, index, sendOsc, sendCallback, reconfigureNow);
- }
-
- void setParameterMappedRange(const uint32_t parameterId, const float minimum, const float maximum, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
- CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
-
- // kPluginBridgeNonRtClientSetParameterMappedRange was added in API 7
- if (fBridgeVersion >= 7)
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedRange);
- fShmNonRtClientControl.writeUInt(parameterId);
- fShmNonRtClientControl.writeFloat(minimum);
- fShmNonRtClientControl.writeFloat(maximum);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setParameterMappedRange(parameterId, minimum, maximum, sendOsc, sendCallback);
- }
-
- void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
- CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
- fShmNonRtClientControl.writeInt(index);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
- }
-
- void setProgramRT(const uint32_t index, const bool sendCallbackLater) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
- fShmNonRtClientControl.writeInt(static_cast<int32_t>(index));
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setProgramRT(index, sendCallbackLater);
- }
-
- void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
- CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
- fShmNonRtClientControl.writeInt(index);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
- }
-
- void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
- fShmNonRtClientControl.writeInt(static_cast<int32_t>(uindex));
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
- }
-
- void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
- {
- CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
- CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
- CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
-
- if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
- return CarlaPlugin::setCustomData(type, key, value, sendGui);
-
- if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "__CarlaPingOnOff__") == 0)
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPingOnOff);
- fShmNonRtClientControl.writeBool(std::strcmp(value, "true") == 0);
- fShmNonRtClientControl.commitWrite();
- return;
- }
-
- const uint32_t maxLocalValueLen = fBridgeVersion >= 10 ? 4096 : 16384;
-
- const uint32_t typeLen = static_cast<uint32_t>(std::strlen(type));
- const uint32_t keyLen = static_cast<uint32_t>(std::strlen(key));
- const uint32_t valueLen = static_cast<uint32_t>(std::strlen(value));
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- if (valueLen > maxLocalValueLen)
- fShmNonRtClientControl.waitIfDataIsReachingLimit();
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCustomData);
-
- fShmNonRtClientControl.writeUInt(typeLen);
- fShmNonRtClientControl.writeCustomData(type, typeLen);
-
- fShmNonRtClientControl.writeUInt(keyLen);
- fShmNonRtClientControl.writeCustomData(key, keyLen);
-
- fShmNonRtClientControl.writeUInt(valueLen);
-
- if (valueLen > 0)
- {
- if (valueLen > maxLocalValueLen)
- {
- String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
-
- filePath += CARLA_OS_SEP_STR ".CarlaCustomData_";
- filePath += fShmAudioPool.getFilenameSuffix();
-
- if (File(filePath).replaceWithText(value))
- {
- const uint32_t ulength = static_cast<uint32_t>(filePath.length());
-
- fShmNonRtClientControl.writeUInt(ulength);
- fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
- }
- else
- {
- fShmNonRtClientControl.writeUInt(0);
- }
- }
- else
- {
- fShmNonRtClientControl.writeCustomData(value, valueLen);
- }
- }
-
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setCustomData(type, key, value, sendGui);
- }
-
- void setChunkData(const void* const data, const std::size_t dataSize) override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
-
- CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
- CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0,);
-
- String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
-
- filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
- filePath += fShmAudioPool.getFilenameSuffix();
-
- if (File(filePath).replaceWithText(dataBase64.buffer()))
- {
- const uint32_t ulength = static_cast<uint32_t>(filePath.length());
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
- fShmNonRtClientControl.writeUInt(ulength);
- fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
- fShmNonRtClientControl.commitWrite();
- }
-
- // save data internally as well
- fInfo.chunk.resize(dataSize);
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- std::memcpy(fInfo.chunk.data(), data, dataSize);
- #else
- std::memcpy(&fInfo.chunk.front(), data, dataSize);
- #endif
- }
-
- // -------------------------------------------------------------------
- // Set ui stuff
-
- void setCustomUITitle(const char* const title) noexcept override
- {
- carla_debug("CarlaPluginBridge::setCustomUITitle(%s)", title);
-
- if (fBridgeVersion >= 8)
- {
- const uint32_t size = static_cast<uint32_t>(std::strlen(title));
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetWindowTitle);
- fShmNonRtClientControl.writeUInt(size);
- fShmNonRtClientControl.writeCustomData(title, size);
- fShmNonRtClientControl.commitWrite();
- }
-
- CarlaPlugin::setCustomUITitle(title);
- }
-
- void showCustomUI(const bool yesNo) override
- {
- if (yesNo && pData->uiTitle.isEmpty() && fBridgeVersion >= 8)
- _setUiTitleFromName();
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(yesNo ? kPluginBridgeNonRtClientShowUI : kPluginBridgeNonRtClientHideUI);
- fShmNonRtClientControl.commitWrite();
- }
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- if (yesNo)
- {
- pData->tryTransient();
- }
- else
- {
- pData->transientTryCounter = 0;
- }
- #endif
- }
-
- void* embedCustomUI(void* const ptr) override
- {
- if (fBridgeVersion < 9)
- return nullptr;
-
- fPendingEmbedCustomUI = 0;
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientEmbedUI);
- fShmNonRtClientControl.writeULong(reinterpret_cast<uint64_t>(ptr));
- fShmNonRtClientControl.commitWrite();
- }
-
- const uint32_t timeoutEnd = carla_gettime_ms() + 15*1000; // 15 secs
- const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
-
- for (; carla_gettime_ms() < timeoutEnd && fBridgeThread.isThreadRunning();)
- {
- pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
-
- if (needsEngineIdle)
- pData->engine->idle();
-
- if (fPendingEmbedCustomUI != 0)
- {
- if (fPendingEmbedCustomUI == 1)
- fPendingEmbedCustomUI = 0;
- break;
- }
-
- carla_msleep(20);
- }
-
- return reinterpret_cast<void*>(fPendingEmbedCustomUI);
- }
-
- void idle() override
- {
- if (fBridgeThread.isThreadRunning())
- {
- if (fInitiated && fTimedOut && pData->active)
- setActive(false, true, true);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPing);
- fShmNonRtClientControl.commitWrite();
- }
-
- try {
- handleNonRtData();
- } CARLA_SAFE_EXCEPTION("handleNonRtData");
- }
- else if (fInitiated)
- {
- fTimedOut = true;
- fTimedError = true;
- fInitiated = false;
- handleProcessStopped();
- }
-
- CarlaPlugin::idle();
- }
-
- // -------------------------------------------------------------------
- // Plugin state
-
- void reload() override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
- carla_debug("CarlaPluginBridge::reload() - start");
-
- const EngineProcessMode processMode(pData->engine->getProccessMode());
-
- // Safely disable plugin for reload
- const ScopedDisabler sd(this);
-
- // cleanup of previous data
- pData->audioIn.clear();
- pData->audioOut.clear();
- pData->cvIn.clear();
- pData->cvOut.clear();
- pData->event.clear();
-
- bool needsCtrlIn, needsCtrlOut;
- needsCtrlIn = needsCtrlOut = false;
-
- if (fInfo.aIns > 0)
- {
- pData->audioIn.createNew(fInfo.aIns);
- }
-
- if (fInfo.aOuts > 0)
- {
- pData->audioOut.createNew(fInfo.aOuts);
- needsCtrlIn = true;
- }
-
- if (fInfo.cvIns > 0)
- {
- pData->cvIn.createNew(fInfo.cvIns);
- }
-
- if (fInfo.cvOuts > 0)
- {
- pData->cvOut.createNew(fInfo.cvOuts);
- }
-
- if (fInfo.mIns > 0)
- needsCtrlIn = true;
-
- if (fInfo.mOuts > 0)
- needsCtrlOut = true;
-
- const uint portNameSize(pData->engine->getMaxPortNameSize());
- CarlaString portName;
-
- // Audio Ins
- for (uint32_t j=0; j < fInfo.aIns; ++j)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- if (fInfo.aInNames != nullptr && fInfo.aInNames[j] != nullptr)
- {
- portName += fInfo.aInNames[j];
- }
- else if (fInfo.aIns > 1)
- {
- portName += "input_";
- portName += CarlaString(j+1);
- }
- else
- portName += "input";
-
- portName.truncate(portNameSize);
-
- pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
- pData->audioIn.ports[j].rindex = j;
- }
-
- // Audio Outs
- for (uint32_t j=0; j < fInfo.aOuts; ++j)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- if (fInfo.aOutNames != nullptr && fInfo.aOutNames[j] != nullptr)
- {
- portName += fInfo.aOutNames[j];
- }
- else if (fInfo.aOuts > 1)
- {
- portName += "output_";
- portName += CarlaString(j+1);
- }
- else
- portName += "output";
-
- portName.truncate(portNameSize);
-
- pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
- pData->audioOut.ports[j].rindex = j;
- }
-
- // TODO - MIDI
-
- // CV Ins
- for (uint32_t j=0; j < fInfo.cvIns; ++j)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- if (fInfo.cvInNames != nullptr && fInfo.cvInNames[j] != nullptr)
- {
- portName += fInfo.cvInNames[j];
- }
- else if (fInfo.cvIns > 1)
- {
- portName += "cv_input_";
- portName += CarlaString(j+1);
- }
- else
- portName += "cv_input";
-
- portName.truncate(portNameSize);
-
- pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
- pData->cvIn.ports[j].rindex = j;
- }
-
- // CV Outs
- for (uint32_t j=0; j < fInfo.cvOuts; ++j)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- if (fInfo.cvOutNames != nullptr && fInfo.cvOutNames[j] != nullptr)
- {
- portName += fInfo.cvOutNames[j];
- }
- else if (fInfo.cvOuts > 1)
- {
- portName += "cv_output_";
- portName += CarlaString(j+1);
- }
- else
- portName += "cv_output";
-
- portName.truncate(portNameSize);
-
- pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
- pData->cvOut.ports[j].rindex = j;
- }
-
- if (needsCtrlIn)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- portName += "event-in";
- portName.truncate(portNameSize);
-
- pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
- #endif
- }
-
- if (needsCtrlOut)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- portName += "event-out";
- portName.truncate(portNameSize);
-
- pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
- }
-
- // extra plugin hints
- pData->extraHints = 0x0;
-
- if (fInfo.mIns > 0)
- pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
-
- if (fInfo.mOuts > 0)
- pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
-
- bufferSizeChanged(pData->engine->getBufferSize());
- reloadPrograms(true);
-
- carla_debug("CarlaPluginBridge::reload() - end");
- }
-
- // -------------------------------------------------------------------
- // Plugin processing
-
- void activate() noexcept override
- {
- if (! fBridgeThread.isThreadRunning())
- {
- CARLA_SAFE_ASSERT_RETURN(restartBridgeThread(),);
- }
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientActivate);
- fShmNonRtClientControl.commitWrite();
- }
-
- fTimedOut = false;
-
- try {
- waitForClient("activate", 2000);
- } CARLA_SAFE_EXCEPTION("activate - waitForClient");
- }
-
- void deactivate() noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientDeactivate);
- fShmNonRtClientControl.commitWrite();
- }
-
- fTimedOut = false;
-
- try {
- waitForClient("deactivate", 2000);
- } CARLA_SAFE_EXCEPTION("deactivate - waitForClient");
- }
-
- void process(const float* const* const audioIn,
- float** const audioOut,
- const float* const* const cvIn,
- float** const cvOut,
- const uint32_t frames) override
- {
- // --------------------------------------------------------------------------------------------------------
- // Check if active
-
- if (fTimedOut || fTimedError || ! pData->active)
- {
- // disable any output sound
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- carla_zeroFloats(audioOut[i], frames);
- for (uint32_t i=0; i < pData->cvOut.count; ++i)
- carla_zeroFloats(cvOut[i], frames);
- return;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Check if needs reset
-
- if (pData->needsReset)
- {
- // TODO
-
- pData->needsReset = false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Event Input
-
- if (pData->event.portIn != nullptr)
- {
- // ----------------------------------------------------------------------------------------------------
- // MIDI Input (External)
-
- if (pData->extNotes.mutex.tryLock())
- {
- for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
- {
- const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
- CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
-
- uint8_t data1, data2, data3;
- data1 = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
- data2 = note.note;
- data3 = note.velo;
-
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
- fShmRtClientControl.writeUInt(0); // time
- fShmRtClientControl.writeByte(0); // port
- fShmRtClientControl.writeByte(3); // size
- fShmRtClientControl.writeByte(data1);
- fShmRtClientControl.writeByte(data2);
- fShmRtClientControl.writeByte(data3);
- fShmRtClientControl.commitWrite();
- }
-
- pData->extNotes.data.clear();
- pData->extNotes.mutex.unlock();
-
- } // End of MIDI Input (External)
-
- // ----------------------------------------------------------------------------------------------------
- // Event Input (System)
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- bool allNotesOffSent = false;
-
- if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
- {
- const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0;
- pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
- }
- #endif
-
- for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
- {
- EngineEvent& event(pData->event.portIn->getEvent(i));
-
- // Control change
- switch (event.type)
- {
- case kEngineEventTypeNull:
- break;
-
- case kEngineEventTypeControl: {
- EngineControlEvent& ctrlEvent = event.ctrl;
-
- switch (ctrlEvent.type)
- {
- case kEngineControlEventTypeNull:
- break;
-
- case kEngineControlEventTypeParameter: {
- float value;
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- // non-midi
- if (event.channel == kEngineEventNonMidiChannel)
- {
- const uint32_t k = ctrlEvent.param;
- CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
-
- ctrlEvent.handled = true;
- value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
- setParameterValueRT(k, value, event.time, true);
- continue;
- }
-
- // Control backend stuff
- if (event.channel == pData->ctrlChannel)
- {
-
- if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
- {
- ctrlEvent.handled = true;
- value = ctrlEvent.normalizedValue;
- setDryWetRT(value, true);
- }
- else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
- {
- ctrlEvent.handled = true;
- value = ctrlEvent.normalizedValue*127.0f/100.0f;
- setVolumeRT(value, true);
- }
- else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
- {
- float left, right;
- value = ctrlEvent.normalizedValue/0.5f - 1.0f;
-
- if (value < 0.0f)
- {
- left = -1.0f;
- right = (value*2.0f)+1.0f;
- }
- else if (value > 0.0f)
- {
- left = (value*2.0f)-1.0f;
- right = 1.0f;
- }
- else
- {
- left = -1.0f;
- right = 1.0f;
- }
-
- ctrlEvent.handled = true;
- setBalanceLeftRT(left, true);
- setBalanceRightRT(right, true);
- }
- }
- #endif
- // Control plugin parameters
- for (uint32_t k=0; k < pData->param.count; ++k)
- {
- if (pData->param.data[k].midiChannel != event.channel)
- continue;
- if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
- continue;
- if (pData->param.data[k].type != PARAMETER_INPUT)
- continue;
- if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMATABLE) == 0)
- continue;
-
- ctrlEvent.handled = true;
- value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
- setParameterValueRT(k, value, event.time, true);
- }
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- if (! ctrlEvent.handled)
- checkForMidiLearn(event);
- #endif
-
- if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(0); // port
- fShmRtClientControl.writeByte(3); // size
- fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
- fShmRtClientControl.writeByte(uint8_t(ctrlEvent.param));
- fShmRtClientControl.writeByte(uint8_t(ctrlEvent.normalizedValue*127.0f + 0.5f));
- }
- break;
- }
-
- case kEngineControlEventTypeMidiBank:
- if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiBank);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(event.channel);
- fShmRtClientControl.writeUShort(event.ctrl.param);
- fShmRtClientControl.commitWrite();
- }
- else if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
- {
- // VST2's that use banks usually require both a MSB bank message and a LSB bank message. The MSB bank message can just be 0
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(0); // port
- fShmRtClientControl.writeByte(3); // size
- fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
- fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT);
- fShmRtClientControl.writeByte(0);
- fShmRtClientControl.commitWrite();
-
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(0); // port
- fShmRtClientControl.writeByte(3); // size
- fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
- fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT__LSB);
- fShmRtClientControl.writeByte(uint8_t(event.ctrl.param));
- fShmRtClientControl.commitWrite();
- }
- break;
-
- case kEngineControlEventTypeMidiProgram:
- if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES || pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiProgram);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(event.channel);
- fShmRtClientControl.writeUShort(event.ctrl.param);
- fShmRtClientControl.commitWrite();
- }
- break;
-
- case kEngineControlEventTypeAllSoundOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllSoundOff);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(event.channel);
- fShmRtClientControl.commitWrite();
- }
- break;
-
- case kEngineControlEventTypeAllNotesOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
- {
- allNotesOffSent = true;
- postponeRtAllNotesOff();
- }
- #endif
-
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllNotesOff);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(event.channel);
- fShmRtClientControl.commitWrite();
- }
- break;
- } // switch (ctrlEvent.type)
- break;
- } // case kEngineEventTypeControl
-
- case kEngineEventTypeMidi: {
- const EngineMidiEvent& midiEvent(event.midi);
-
- if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
- continue;
-
- const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
-
- uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
-
- if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
- continue;
- if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
- continue;
- if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
- continue;
- if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
- continue;
- if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
- continue;
-
- // Fix bad note-off
- if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
- status = MIDI_STATUS_NOTE_OFF;
-
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
- fShmRtClientControl.writeUInt(event.time);
- fShmRtClientControl.writeByte(midiEvent.port);
- fShmRtClientControl.writeByte(midiEvent.size);
-
- fShmRtClientControl.writeByte(uint8_t(midiData[0] | (event.channel & MIDI_CHANNEL_BIT)));
-
- for (uint8_t j=1; j < midiEvent.size; ++j)
- fShmRtClientControl.writeByte(midiData[j]);
-
- fShmRtClientControl.commitWrite();
-
- if (status == MIDI_STATUS_NOTE_ON)
- {
- pData->postponeNoteOnRtEvent(true, event.channel, midiData[1], midiData[2]);
- }
- else if (status == MIDI_STATUS_NOTE_OFF)
- {
- pData->postponeNoteOffRtEvent(true, event.channel, midiData[1]);
- }
- } break;
- }
- }
-
- pData->postRtEvents.trySplice();
-
- } // End of Event Input
-
- if (! processSingle(audioIn, audioOut, cvIn, cvOut, frames))
- return;
-
- // --------------------------------------------------------------------------------------------------------
- // Control and MIDI Output
-
- if (pData->event.portOut != nullptr)
- {
- float value;
-
- for (uint32_t k=0; k < pData->param.count; ++k)
- {
- if (pData->param.data[k].type != PARAMETER_OUTPUT)
- continue;
-
- if (pData->param.data[k].mappedControlIndex > 0)
- {
- value = pData->param.ranges[k].getNormalizedValue(fParams[k].value);
- pData->event.portOut->writeControlEvent(0,
- pData->param.data[k].midiChannel,
- kEngineControlEventTypeParameter,
- static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
- -1,
- value);
- }
- }
-
- uint32_t time;
- uint8_t port, size;
- const uint8_t* midiData(fShmRtClientControl.data->midiOut);
-
- for (std::size_t read=0; read<kBridgeRtClientDataMidiOutSize-kBridgeBaseMidiOutHeaderSize;)
- {
- // get time
- time = *(const uint32_t*)midiData;
- midiData += 4;
-
- // get port and size
- port = *midiData++;
- size = *midiData++;
-
- if (size == 0)
- break;
-
- // store midi data advancing as needed
- uint8_t data[4];
-
- {
- uint8_t j=0;
- for (; j<size && j<4; ++j)
- data[j] = *midiData++;
- }
-
- if (size <= 4)
- pData->event.portOut->writeMidiEvent(time, size, data);
-
- read += kBridgeBaseMidiOutHeaderSize + size;
- }
-
- // TODO
- (void)port;
-
- } // End of Control and MIDI Output
- }
-
- bool processSingle(const float* const* const audioIn, float** const audioOut,
- const float* const* const cvIn, float** const cvOut, const uint32_t frames)
- {
- CARLA_SAFE_ASSERT_RETURN(! fTimedError, false);
- CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
- CARLA_SAFE_ASSERT_RETURN(frames <= fBufferSize, false);
-
- if (pData->audioIn.count > 0)
- {
- CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
- }
- if (pData->audioOut.count > 0)
- {
- CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
- }
- if (pData->cvIn.count > 0)
- {
- CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
- }
- if (pData->cvOut.count > 0)
- {
- CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Try lock, silence otherwise
-
- #ifndef STOAT_TEST_BUILD
- if (pData->engine->isOffline())
- {
- pData->singleMutex.lock();
- }
- else
- #endif
- if (! pData->singleMutex.tryLock())
- {
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- carla_zeroFloats(audioOut[i], frames);
- for (uint32_t i=0; i < pData->cvOut.count; ++i)
- carla_zeroFloats(cvOut[i], frames);
- return false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Reset audio buffers
-
- for (uint32_t i=0; i < pData->audioIn.count; ++i)
- carla_copyFloats(fShmAudioPool.data + (i * fBufferSize), audioIn[i], frames);
- for (uint32_t i=0; i < pData->cvIn.count; ++i)
- carla_copyFloats(fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + i) * fBufferSize), cvIn[i], frames);
-
- // --------------------------------------------------------------------------------------------------------
- // TimeInfo
-
- const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
- BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
-
- bridgeTimeInfo.playing = timeInfo.playing;
- bridgeTimeInfo.frame = timeInfo.frame;
- bridgeTimeInfo.usecs = timeInfo.usecs;
- bridgeTimeInfo.validFlags = timeInfo.bbt.valid ? kPluginBridgeTimeInfoValidBBT : 0x0;
-
- if (timeInfo.bbt.valid)
- {
- bridgeTimeInfo.bar = timeInfo.bbt.bar;
- bridgeTimeInfo.beat = timeInfo.bbt.beat;
- bridgeTimeInfo.tick = timeInfo.bbt.tick;
-
- bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
- bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
-
- bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
- bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
- bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Run plugin
-
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientProcess);
- fShmRtClientControl.writeUInt(frames);
- fShmRtClientControl.commitWrite();
- }
-
- waitForClient("process", fProcWaitTime);
-
- if (fTimedOut)
- {
- pData->singleMutex.unlock();
- return false;
- }
-
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- carla_copyFloats(audioOut[i], fShmAudioPool.data + ((pData->audioIn.count + i) * fBufferSize), frames);
- for (uint32_t i=0; i < pData->cvOut.count; ++i)
- carla_copyFloats(cvOut[i], fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + pData->cvIn.count + i) * fBufferSize), frames);
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- // --------------------------------------------------------------------------------------------------------
- // Post-processing (dry/wet, volume and balance)
-
- {
- const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
- const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
- const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
- const bool isMono = (pData->audioIn.count == 1);
-
- bool isPair;
- float bufValue;
- float* const oldBufLeft = pData->postProc.extraBuffer;
-
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- {
- // Dry/Wet
- if (doDryWet)
- {
- const uint32_t c = isMono ? 0 : i;
-
- for (uint32_t k=0; k < frames; ++k)
- {
- # ifndef BUILD_BRIDGE
- if (k < pData->latency.frames && pData->latency.buffers != nullptr)
- bufValue = pData->latency.buffers[c][k];
- else if (pData->latency.frames < frames)
- bufValue = audioIn[c][k-pData->latency.frames];
- else
- # endif
- bufValue = audioIn[c][k];
-
- audioOut[i][k] = (audioOut[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
- }
- }
-
- // Balance
- if (doBalance)
- {
- isPair = (i % 2 == 0);
-
- if (isPair)
- {
- CARLA_ASSERT(i+1 < pData->audioOut.count);
- carla_copyFloats(oldBufLeft, audioOut[i], frames);
- }
-
- float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
- float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
-
- for (uint32_t k=0; k < frames; ++k)
- {
- if (isPair)
- {
- // left
- audioOut[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
- audioOut[i][k] += audioOut[i+1][k] * (1.0f - balRangeR);
- }
- else
- {
- // right
- audioOut[i][k] = audioOut[i][k] * balRangeR;
- audioOut[i][k] += oldBufLeft[k] * balRangeL;
- }
- }
- }
-
- // Volume (and buffer copy)
- if (doVolume)
- {
- for (uint32_t k=0; k < frames; ++k)
- audioOut[i][k] *= pData->postProc.volume;
- }
- }
-
- } // End of Post-processing
-
- # ifndef BUILD_BRIDGE
- // --------------------------------------------------------------------------------------------------------
- // Save latency values for next callback
-
- if (pData->latency.frames != 0 && pData->latency.buffers != nullptr)
- {
- const uint32_t latframes = pData->latency.frames;
-
- if (latframes <= frames)
- {
- for (uint32_t i=0; i < pData->audioIn.count; ++i)
- carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
- }
- else
- {
- const uint32_t diff = latframes - frames;
-
- for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
- {
- // push back buffer by 'frames'
- for (k=0; k < diff; ++k)
- pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
-
- // put current input at the end
- for (uint32_t j=0; k < latframes; ++j, ++k)
- pData->latency.buffers[i][k] = audioIn[i][j];
- }
- }
- }
- # endif
- #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
-
- // --------------------------------------------------------------------------------------------------------
-
- pData->singleMutex.unlock();
- return true;
- }
-
- void bufferSizeChanged(const uint32_t newBufferSize) override
- {
- fBufferSize = newBufferSize;
- resizeAudioPool(newBufferSize);
-
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetBufferSize);
- fShmRtClientControl.writeUInt(newBufferSize);
- fShmRtClientControl.commitWrite();
- }
-
- //fProcWaitTime = newBufferSize*1000/pData->engine->getSampleRate();
- fProcWaitTime = 1000;
-
- waitForClient("buffersize", 1000);
-
- CarlaPlugin::bufferSizeChanged(newBufferSize);
- }
-
- void sampleRateChanged(const double newSampleRate) override
- {
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetSampleRate);
- fShmRtClientControl.writeDouble(newSampleRate);
- fShmRtClientControl.commitWrite();
- }
-
- //fProcWaitTime = pData->engine->getBufferSize()*1000/newSampleRate;
- fProcWaitTime = 1000;
-
- waitForClient("samplerate", 1000);
- }
-
- void offlineModeChanged(const bool isOffline) override
- {
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetOnline);
- fShmRtClientControl.writeBool(isOffline);
- fShmRtClientControl.commitWrite();
- }
-
- waitForClient("offline", 1000);
- }
-
- // -------------------------------------------------------------------
- // Plugin buffers
-
- void clearBuffers() noexcept override
- {
- if (fParams != nullptr)
- {
- delete[] fParams;
- fParams = nullptr;
- }
-
- CarlaPlugin::clearBuffers();
- }
-
- // -------------------------------------------------------------------
- // Post-poned UI Stuff
-
- void uiParameterChange(const uint32_t index, const float value) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiParameterChange);
- fShmNonRtClientControl.writeUInt(index);
- fShmNonRtClientControl.writeFloat(value);
- fShmNonRtClientControl.commitWrite();
- }
-
- void uiProgramChange(const uint32_t index) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiProgramChange);
- fShmNonRtClientControl.writeUInt(index);
- fShmNonRtClientControl.commitWrite();
- }
-
- void uiMidiProgramChange(const uint32_t index) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiMidiProgramChange);
- fShmNonRtClientControl.writeUInt(index);
- fShmNonRtClientControl.commitWrite();
- }
-
- void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
- CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
- CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOn);
- fShmNonRtClientControl.writeByte(channel);
- fShmNonRtClientControl.writeByte(note);
- fShmNonRtClientControl.writeByte(velo);
- fShmNonRtClientControl.commitWrite();
- }
-
- void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
- CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOff);
- fShmNonRtClientControl.writeByte(channel);
- fShmNonRtClientControl.writeByte(note);
- fShmNonRtClientControl.commitWrite();
- }
-
- // -------------------------------------------------------------------
- // Internal helper functions
-
- void restoreLV2State(bool) noexcept override
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientRestoreLV2State);
- fShmNonRtClientControl.commitWrite();
- }
-
- void waitForBridgeSaveSignal() noexcept override
- {
- // VSTs only save chunks, for which we already have a waitForSaved there
- if (fPluginType != PLUGIN_VST2)
- waitForSaved();
- }
-
- // -------------------------------------------------------------------
-
- void handleNonRtData()
- {
- for (; fShmNonRtServerControl.isDataAvailableForReading();)
- {
- const PluginBridgeNonRtServerOpcode opcode(fShmNonRtServerControl.readOpcode());
- #ifdef DEBUG
- if (opcode != kPluginBridgeNonRtServerPong && opcode != kPluginBridgeNonRtServerParameterValue2) {
- carla_debug("CarlaPluginBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtServerOpcode2str(opcode));
- }
- #endif
- switch (opcode)
- {
- case kPluginBridgeNonRtServerNull:
- case kPluginBridgeNonRtServerPong:
- break;
-
- case kPluginBridgeNonRtServerVersion:
- fBridgeVersion = fShmNonRtServerControl.readUInt();
- break;
-
- case kPluginBridgeNonRtServerPluginInfo1: {
- // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
- const uint32_t category = fShmNonRtServerControl.readUInt();
- const uint32_t hints = fShmNonRtServerControl.readUInt();
- const uint32_t optionAv = fShmNonRtServerControl.readUInt();
- const uint32_t optionEn = fShmNonRtServerControl.readUInt();
- const int64_t uniqueId = fShmNonRtServerControl.readLong();
-
- if (fUniqueId != 0) {
- CARLA_SAFE_ASSERT_INT2(fUniqueId == uniqueId, fUniqueId, uniqueId);
- }
-
- pData->hints = hints | PLUGIN_IS_BRIDGE;
- pData->options = optionEn;
-
- #ifdef HAVE_X11
- if (fBridgeVersion < 9 || fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
- #endif
- {
- pData->hints &= ~PLUGIN_HAS_CUSTOM_EMBED_UI;
- }
-
- fInfo.category = static_cast<PluginCategory>(category);
- fInfo.optionsAvailable = optionAv;
- } break;
-
- case kPluginBridgeNonRtServerPluginInfo2: {
- // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
-
- // realName
- BridgeTextReader realName(fShmNonRtServerControl);
-
- // label
- BridgeTextReader label(fShmNonRtServerControl);
-
- // maker
- BridgeTextReader maker(fShmNonRtServerControl);
-
- // copyright
- BridgeTextReader copyright(fShmNonRtServerControl);
-
- fInfo.name = realName.text;
- fInfo.label = label.text;
- fInfo.maker = maker.text;
- fInfo.copyright = copyright.text;
- realName.text = label.text = maker.text = copyright.text = nullptr;
-
- if (pData->name == nullptr)
- pData->name = pData->engine->getUniquePluginName(fInfo.name);
- } break;
-
- case kPluginBridgeNonRtServerAudioCount: {
- // uint/ins, uint/outs
- fInfo.clear();
-
- fInfo.aIns = fShmNonRtServerControl.readUInt();
- fInfo.aOuts = fShmNonRtServerControl.readUInt();
-
- if (fInfo.aIns > 0)
- {
- fInfo.aInNames = new const char*[fInfo.aIns];
- carla_zeroPointers(fInfo.aInNames, fInfo.aIns);
- }
-
- if (fInfo.aOuts > 0)
- {
- fInfo.aOutNames = new const char*[fInfo.aOuts];
- carla_zeroPointers(fInfo.aOutNames, fInfo.aOuts);
- }
-
- } break;
-
- case kPluginBridgeNonRtServerMidiCount: {
- // uint/ins, uint/outs
- fInfo.mIns = fShmNonRtServerControl.readUInt();
- fInfo.mOuts = fShmNonRtServerControl.readUInt();
- } break;
-
- case kPluginBridgeNonRtServerCvCount: {
- // uint/ins, uint/outs
- fInfo.cvIns = fShmNonRtServerControl.readUInt();
- fInfo.cvOuts = fShmNonRtServerControl.readUInt();
- } break;
-
- case kPluginBridgeNonRtServerParameterCount: {
- // uint/count
- const uint32_t count = fShmNonRtServerControl.readUInt();
-
- // delete old data
- pData->param.clear();
-
- if (fParams != nullptr)
- {
- delete[] fParams;
- fParams = nullptr;
- }
-
- if (count > 0)
- {
- pData->param.createNew(count, false);
- fParams = new BridgeParamInfo[count];
-
- // we might not receive all parameter data, so ensure range max is not 0
- for (uint32_t i=0; i<count; ++i)
- {
- pData->param.ranges[i].def = 0.0f;
- pData->param.ranges[i].min = 0.0f;
- pData->param.ranges[i].max = 1.0f;
- pData->param.ranges[i].step = 0.001f;
- pData->param.ranges[i].stepSmall = 0.0001f;
- pData->param.ranges[i].stepLarge = 0.1f;
- }
- }
- } break;
-
- case kPluginBridgeNonRtServerProgramCount: {
- // uint/count
- pData->prog.clear();
-
- if (const uint32_t count = fShmNonRtServerControl.readUInt())
- pData->prog.createNew(count);
-
- } break;
-
- case kPluginBridgeNonRtServerMidiProgramCount: {
- // uint/count
- pData->midiprog.clear();
-
- if (const uint32_t count = fShmNonRtServerControl.readUInt())
- pData->midiprog.createNew(count);
-
- } break;
-
- case kPluginBridgeNonRtServerPortName: {
- // byte/type, uint/index, uint/size, str[] (name)
- const uint8_t portType = fShmNonRtServerControl.readByte();
- const uint32_t index = fShmNonRtServerControl.readUInt();
-
- // name
- BridgeTextReader name(fShmNonRtServerControl);
-
- CARLA_SAFE_ASSERT_BREAK(portType > kPluginBridgePortNull && portType < kPluginBridgePortTypeCount);
-
- switch (portType)
- {
- case kPluginBridgePortAudioInput:
- CARLA_SAFE_ASSERT_BREAK(index < fInfo.aIns);
- fInfo.aInNames[index] = name.text;
- name.text = nullptr;
- break;
- case kPluginBridgePortAudioOutput:
- CARLA_SAFE_ASSERT_BREAK(index < fInfo.aOuts);
- fInfo.aOutNames[index] = name.text;
- name.text = nullptr;
- break;
- }
-
- } break;
-
- case kPluginBridgeNonRtServerParameterData1: {
- // uint/index, int/rindex, uint/type, uint/hints, short/cc
- const uint32_t index = fShmNonRtServerControl.readUInt();
- const int32_t rindex = fShmNonRtServerControl.readInt();
- const uint32_t type = fShmNonRtServerControl.readUInt();
- const uint32_t hints = fShmNonRtServerControl.readUInt();
- const int16_t ctrl = fShmNonRtServerControl.readShort();
-
- CARLA_SAFE_ASSERT_INT_BREAK(ctrl >= CONTROL_INDEX_NONE && ctrl <= CONTROL_INDEX_MAX_ALLOWED, ctrl);
- CARLA_SAFE_ASSERT_UINT2_BREAK(index < pData->param.count, index, pData->param.count);
-
- pData->param.data[index].type = static_cast<ParameterType>(type);
- pData->param.data[index].index = static_cast<int32_t>(index);
- pData->param.data[index].rindex = rindex;
- pData->param.data[index].hints = hints;
- pData->param.data[index].mappedControlIndex = ctrl;
- } break;
-
- case kPluginBridgeNonRtServerParameterData2: {
- // uint/index, uint/size, str[] (name), uint/size, str[] (unit)
- const uint32_t index = fShmNonRtServerControl.readUInt();
-
- // name
- BridgeTextReader name(fShmNonRtServerControl);
-
- // symbol
- BridgeTextReader symbol(fShmNonRtServerControl);
-
- // unit
- BridgeTextReader unit(fShmNonRtServerControl);
-
- CARLA_SAFE_ASSERT_UINT2_BREAK(index < pData->param.count, index, pData->param.count);
-
- fParams[index].name = name.text;
- fParams[index].symbol = symbol.text;
- fParams[index].unit = unit.text;
- name.text = symbol.text = unit.text = nullptr;
- } break;
-
- case kPluginBridgeNonRtServerParameterRanges: {
- // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
- const uint32_t index = fShmNonRtServerControl.readUInt();
- const float def = fShmNonRtServerControl.readFloat();
- const float min = fShmNonRtServerControl.readFloat();
- const float max = fShmNonRtServerControl.readFloat();
- const float step = fShmNonRtServerControl.readFloat();
- const float stepSmall = fShmNonRtServerControl.readFloat();
- const float stepLarge = fShmNonRtServerControl.readFloat();
-
- CARLA_SAFE_ASSERT_BREAK(min < max);
- CARLA_SAFE_ASSERT_BREAK(def >= min);
- CARLA_SAFE_ASSERT_BREAK(def <= max);
- CARLA_SAFE_ASSERT_UINT2_BREAK(index < pData->param.count, index, pData->param.count);
-
- pData->param.ranges[index].def = def;
- pData->param.ranges[index].min = min;
- pData->param.ranges[index].max = max;
- pData->param.ranges[index].step = step;
- pData->param.ranges[index].stepSmall = stepSmall;
- pData->param.ranges[index].stepLarge = stepLarge;
- } break;
-
- case kPluginBridgeNonRtServerParameterValue: {
- // uint/index, float/value
- const uint32_t index = fShmNonRtServerControl.readUInt();
- const float value = fShmNonRtServerControl.readFloat();
-
- if (index < pData->param.count)
- {
- const float fixedValue(pData->param.getFixedValue(index, value));
-
- if (carla_isNotEqual(fParams[index].value, fixedValue))
- {
- fParams[index].value = fixedValue;
- CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
- }
- }
- } break;
-
- case kPluginBridgeNonRtServerParameterValue2: {
- // uint/index, float/value
- const uint32_t index = fShmNonRtServerControl.readUInt();
- const float value = fShmNonRtServerControl.readFloat();
-
- if (index < pData->param.count)
- {
- const float fixedValue(pData->param.getFixedValue(index, value));
- fParams[index].value = fixedValue;
- }
- } break;
-
- case kPluginBridgeNonRtServerParameterTouch: {
- // uint/index, bool/touch
- const uint32_t index = fShmNonRtServerControl.readUInt();
- const bool touch = fShmNonRtServerControl.readBool();
-
- pData->engine->touchPluginParameter(pData->id, index, touch);
- } break;
-
- case kPluginBridgeNonRtServerDefaultValue: {
- // uint/index, float/value
- const uint32_t index = fShmNonRtServerControl.readUInt();
- const float value = fShmNonRtServerControl.readFloat();
-
- if (index < pData->param.count)
- pData->param.ranges[index].def = value;
- } break;
-
- case kPluginBridgeNonRtServerCurrentProgram: {
- // int/index
- const int32_t index = fShmNonRtServerControl.readInt();
-
- CARLA_SAFE_ASSERT_BREAK(index >= -1);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
-
- CarlaPlugin::setProgram(index, false, true, true);
- } break;
-
- case kPluginBridgeNonRtServerCurrentMidiProgram: {
- // int/index
- const int32_t index = fShmNonRtServerControl.readInt();
-
- CARLA_SAFE_ASSERT_BREAK(index >= -1);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
-
- CarlaPlugin::setMidiProgram(index, false, true, true);
- } break;
-
- case kPluginBridgeNonRtServerProgramName: {
- // uint/index, uint/size, str[] (name)
- const uint32_t index = fShmNonRtServerControl.readUInt();
-
- // name
- const BridgeTextReader name(fShmNonRtServerControl);
-
- CARLA_SAFE_ASSERT_INT2(index < pData->prog.count, index, pData->prog.count);
-
- if (index < pData->prog.count)
- {
- if (pData->prog.names[index] != nullptr)
- delete[] pData->prog.names[index];
- pData->prog.names[index] = carla_strdup(name.text);
- }
- } break;
-
- case kPluginBridgeNonRtServerMidiProgramData: {
- // uint/index, uint/bank, uint/program, uint/size, str[] (name)
- const uint32_t index = fShmNonRtServerControl.readUInt();
- const uint32_t bank = fShmNonRtServerControl.readUInt();
- const uint32_t program = fShmNonRtServerControl.readUInt();
-
- // name
- const BridgeTextReader name(fShmNonRtServerControl);
-
- CARLA_SAFE_ASSERT_INT2(index < pData->midiprog.count, index, pData->midiprog.count);
-
- if (index < pData->midiprog.count)
- {
- if (pData->midiprog.data[index].name != nullptr)
- delete[] pData->midiprog.data[index].name;
- pData->midiprog.data[index].bank = bank;
- pData->midiprog.data[index].program = program;
- pData->midiprog.data[index].name = carla_strdup(name.text);
- }
- } break;
-
- case kPluginBridgeNonRtServerSetCustomData: {
- // uint/size, str[], uint/size, str[], uint/size, str[]
- const uint32_t maxLocalValueLen = fBridgeVersion >= 10 ? 4096 : 16384;
-
- // type
- const BridgeTextReader type(fShmNonRtServerControl);
-
- // key
- const BridgeTextReader key(fShmNonRtServerControl);
-
- // value
- const uint32_t valueSize = fShmNonRtServerControl.readUInt();
-
- // special case for big values
- if (valueSize > maxLocalValueLen)
- {
- const BridgeTextReader bigValueFilePath(fShmNonRtServerControl);
-
- String realBigValueFilePath(bigValueFilePath.text);
-
- #ifndef CARLA_OS_WIN
- // Using Wine, fix temp dir
- if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
- {
- const StringArray driveLetterSplit(StringArray::fromTokens(realBigValueFilePath, ":/", ""));
- carla_stdout("big value save path BEFORE => %s", realBigValueFilePath.toRawUTF8());
-
- realBigValueFilePath = fWinePrefix;
- realBigValueFilePath += "/drive_";
- realBigValueFilePath += driveLetterSplit[0].toLowerCase();
- realBigValueFilePath += driveLetterSplit[1];
-
- realBigValueFilePath = realBigValueFilePath.replace("\\", "/");
- carla_stdout("big value save path AFTER => %s", realBigValueFilePath.toRawUTF8());
- }
- #endif
-
- const File bigValueFile(realBigValueFilePath);
- CARLA_SAFE_ASSERT_BREAK(bigValueFile.existsAsFile());
-
- CarlaPlugin::setCustomData(type.text, key.text, bigValueFile.loadFileAsString().toRawUTF8(), false);
-
- bigValueFile.deleteFile();
- }
- else
- {
- const BridgeTextReader value(fShmNonRtServerControl, valueSize);
-
- CarlaPlugin::setCustomData(type.text, key.text, value.text, false);
- }
-
- } break;
-
- case kPluginBridgeNonRtServerSetChunkDataFile: {
- // uint/size, str[] (filename)
-
- // chunkFilePath
- const BridgeTextReader chunkFilePath(fShmNonRtServerControl);
-
- String realChunkFilePath(chunkFilePath.text);
-
- #ifndef CARLA_OS_WIN
- // Using Wine, fix temp dir
- if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
- {
- const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
- carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
-
- realChunkFilePath = fWinePrefix;
- realChunkFilePath += "/drive_";
- realChunkFilePath += driveLetterSplit[0].toLowerCase();
- realChunkFilePath += driveLetterSplit[1];
-
- realChunkFilePath = realChunkFilePath.replace("\\", "/");
- carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
- }
- #endif
-
- const File chunkFile(realChunkFilePath);
- CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
-
- fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
- chunkFile.deleteFile();
- } break;
-
- case kPluginBridgeNonRtServerSetLatency:
- // uint
- fLatency = fShmNonRtServerControl.readUInt();
- #ifndef BUILD_BRIDGE
- if (! fInitiated)
- pData->latency.recreateBuffers(std::max(fInfo.aIns, fInfo.aOuts), fLatency);
- #endif
- break;
-
- case kPluginBridgeNonRtServerSetParameterText: {
- const int32_t index = fShmNonRtServerControl.readInt();
-
- const uint32_t textSize = fShmNonRtServerControl.readUInt();
- const BridgeTextReader text(fShmNonRtServerControl, textSize);
-
- fReceivingParamText.setReceivedData(index, text.text, textSize);
- } break;
-
- case kPluginBridgeNonRtServerReady:
- fInitiated = true;
- break;
-
- case kPluginBridgeNonRtServerSaved:
- fSaved = true;
- break;
-
- case kPluginBridgeNonRtServerRespEmbedUI:
- fPendingEmbedCustomUI = fShmNonRtServerControl.readULong();
- break;
-
- case kPluginBridgeNonRtServerResizeEmbedUI: {
- const uint width = fShmNonRtServerControl.readUInt();
- const uint height = fShmNonRtServerControl.readUInt();
- pData->engine->callback(true, true, ENGINE_CALLBACK_EMBED_UI_RESIZED, pData->id,
- static_cast<int>(width), static_cast<int>(height),
- 0, 0.0f, nullptr);
- } break;
-
- case kPluginBridgeNonRtServerUiClosed:
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- pData->transientTryCounter = 0;
- #endif
- pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id,
- 0, 0, 0, 0.0f, nullptr);
- break;
-
- case kPluginBridgeNonRtServerError: {
- // error
- const BridgeTextReader error(fShmNonRtServerControl);
-
- if (fInitiated)
- {
- pData->engine->callback(true, true, ENGINE_CALLBACK_ERROR, pData->id, 0, 0, 0, 0.0f, error.text);
-
- // just in case
- pData->engine->setLastError(error.text);
- fInitError = true;
- }
- else
- {
- pData->engine->setLastError(error.text);
- fInitError = true;
- fInitiated = true;
- }
- } break;
- }
- }
- }
-
- // -------------------------------------------------------------------
-
- uintptr_t getUiBridgeProcessId() const noexcept override
- {
- return fBridgeThread.getProcessPID();
- }
-
- const void* getExtraStuff() const noexcept override
- {
- return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
- }
-
- // -------------------------------------------------------------------
-
- bool init(CarlaPluginPtr plugin,
- const char* const filename,
- const char* const name,
- const char* const label,
- const int64_t uniqueId,
- const uint options,
- const char* const binaryArchName,
- const char* const bridgeBinary)
- {
- CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
-
- // ---------------------------------------------------------------
- // first checks
-
- if (pData->client != nullptr)
- {
- pData->engine->setLastError("Plugin client is already registered");
- return false;
- }
-
- if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
- {
- pData->engine->setLastError("null bridge binary");
- return false;
- }
-
- // ---------------------------------------------------------------
- // set info
-
- if (name != nullptr && name[0] != '\0')
- pData->name = pData->engine->getUniquePluginName(name);
-
- if (filename != nullptr && filename[0] != '\0')
- pData->filename = carla_strdup(filename);
- else
- pData->filename = carla_strdup("");
-
- fUniqueId = uniqueId;
- fBridgeBinary = bridgeBinary;
-
- std::srand(static_cast<uint>(std::time(nullptr)));
-
- // ---------------------------------------------------------------
- // init sem/shm
-
- if (! fShmAudioPool.initializeServer())
- {
- carla_stderr("Failed to initialize shared memory audio pool");
- return false;
- }
-
- if (! fShmRtClientControl.initializeServer())
- {
- carla_stderr("Failed to initialize RT client control");
- fShmAudioPool.clear();
- return false;
- }
-
- if (! fShmNonRtClientControl.initializeServer())
- {
- carla_stderr("Failed to initialize Non-RT client control");
- fShmRtClientControl.clear();
- fShmAudioPool.clear();
- return false;
- }
-
- if (! fShmNonRtServerControl.initializeServer())
- {
- carla_stderr("Failed to initialize Non-RT server control");
- fShmNonRtClientControl.clear();
- fShmRtClientControl.clear();
- fShmAudioPool.clear();
- return false;
- }
-
- #ifndef CARLA_OS_WIN
- // ---------------------------------------------------------------
- // set wine prefix
-
- if (fBridgeBinary.contains(".exe", true))
- {
- const EngineOptions& engineOptions(pData->engine->getOptions());
-
- if (engineOptions.wine.autoPrefix)
- fWinePrefix = findWinePrefix(pData->filename);
-
- if (fWinePrefix.isEmpty())
- {
- const char* const envWinePrefix(std::getenv("WINEPREFIX"));
-
- if (envWinePrefix != nullptr && envWinePrefix[0] != '\0')
- fWinePrefix = envWinePrefix;
- else if (engineOptions.wine.fallbackPrefix != nullptr && engineOptions.wine.fallbackPrefix[0] != '\0')
- fWinePrefix = engineOptions.wine.fallbackPrefix;
- else
- fWinePrefix = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
- }
- }
- #endif
-
- // ---------------------------------------------------------------
- // init bridge thread
-
- {
- char shmIdsStr[6*4+1];
- carla_zeroChars(shmIdsStr, 6*4+1);
-
- std::strncpy(shmIdsStr+6*0, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
- std::strncpy(shmIdsStr+6*1, &fShmRtClientControl.filename[fShmRtClientControl.filename.length()-6], 6);
- std::strncpy(shmIdsStr+6*2, &fShmNonRtClientControl.filename[fShmNonRtClientControl.filename.length()-6], 6);
- std::strncpy(shmIdsStr+6*3, &fShmNonRtServerControl.filename[fShmNonRtServerControl.filename.length()-6], 6);
-
- fBridgeThread.setData(
- #ifndef CARLA_OS_WIN
- fWinePrefix.toRawUTF8(),
- #endif
- binaryArchName, bridgeBinary, label, shmIdsStr);
- }
-
- if (! restartBridgeThread())
- return false;
-
- // ---------------------------------------------------------------
- // register client
-
- if (pData->name == nullptr)
- {
- if (label != nullptr && label[0] != '\0')
- pData->name = pData->engine->getUniquePluginName(label);
- else
- pData->name = pData->engine->getUniquePluginName("unknown");
- }
-
- pData->client = pData->engine->addClient(plugin);
-
- if (pData->client == nullptr || ! pData->client->isOk())
- {
- pData->engine->setLastError("Failed to register plugin client");
- return false;
- }
-
- // ---------------------------------------------------------------
- // set options
-
- pData->options = 0x0;
-
- if ((fInfo.optionsAvailable & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0)
- pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
- else if (isPluginOptionEnabled(options, PLUGIN_OPTION_FIXED_BUFFERS))
- pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
-
- if (pData->engine->getOptions().forceStereo)
- {
- pData->options |= PLUGIN_OPTION_FORCE_STEREO;
- }
- else if (fInfo.optionsAvailable & PLUGIN_OPTION_FORCE_STEREO)
- {
- if (options & PLUGIN_OPTION_FORCE_STEREO)
- pData->options |= PLUGIN_OPTION_FORCE_STEREO;
- }
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_USE_CHUNKS)
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_USE_CHUNKS))
- pData->options |= PLUGIN_OPTION_USE_CHUNKS;
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
- pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
- pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
- pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PITCHBEND)
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
- pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
- pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_SKIP_SENDING_NOTES)
- if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
- pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
-
- if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
- {
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
- pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
- }
- else if (fInfo.optionsAvailable & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
- {
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
- pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
- }
-
- // kPluginBridgeNonRtClientSetOptions was added in API 7
- if (fBridgeVersion >= 7)
- {
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOptions);
- fShmNonRtClientControl.writeUInt(pData->options);
- fShmNonRtClientControl.commitWrite();
- }
-
- return true;
- }
-
- private:
- const BinaryType fBinaryType;
- const PluginType fPluginType;
- uint fBridgeVersion;
-
- bool fInitiated;
- bool fInitError;
- bool fSaved;
- bool fTimedOut;
- bool fTimedError;
- uint fBufferSize;
- uint fProcWaitTime;
- uint64_t fPendingEmbedCustomUI;
-
- CarlaString fBridgeBinary;
- CarlaPluginBridgeThread fBridgeThread;
-
- BridgeAudioPool fShmAudioPool;
- BridgeRtClientControl fShmRtClientControl;
- BridgeNonRtClientControl fShmNonRtClientControl;
- BridgeNonRtServerControl fShmNonRtServerControl;
-
- #ifndef CARLA_OS_WIN
- String fWinePrefix;
- #endif
-
- class ReceivingParamText {
- public:
- ReceivingParamText() noexcept
- : dataRecv(false),
- dataOk(false),
- index(-1),
- strBuf(nullptr),
- mutex() {}
-
- bool isCurrentlyWaitingData() const noexcept
- {
- return index >= 0;
- }
-
- bool wasDataReceived(bool* const success) const noexcept
- {
- *success = dataOk;
- return dataRecv;
- }
-
- void setTargetData(const int32_t i, char* const b) noexcept
- {
- const CarlaMutexLocker cml(mutex);
-
- dataOk = false;
- dataRecv = false;
- index = i;
- strBuf = b;
- }
-
- void setReceivedData(const int32_t i, const char* const b, const uint blen) noexcept
- {
- CarlaScopedValueSetter<bool> svs(dataRecv, false, true);
-
- const CarlaMutexLocker cml(mutex);
-
- // make backup and reset data
- const int32_t indexCopy = index;
- char* const strBufCopy = strBuf;
- index = -1;
- strBuf = nullptr;
-
- CARLA_SAFE_ASSERT_RETURN(indexCopy == i,);
- CARLA_SAFE_ASSERT_RETURN(strBufCopy != nullptr,);
-
- std::strncpy(strBufCopy, b, std::min(blen, STR_MAX-1U));
- dataOk = true;
- }
-
- private:
- bool dataRecv;
- bool dataOk;
- int32_t index;
- char* strBuf;
- CarlaMutex mutex;
-
- CARLA_DECLARE_NON_COPYABLE(ReceivingParamText)
- } fReceivingParamText;
-
- struct Info {
- uint32_t aIns, aOuts;
- uint32_t cvIns, cvOuts;
- uint32_t mIns, mOuts;
- PluginCategory category;
- uint optionsAvailable;
- CarlaString name;
- CarlaString label;
- CarlaString maker;
- CarlaString copyright;
- const char** aInNames;
- const char** aOutNames;
- const char** cvInNames;
- const char** cvOutNames;
- std::vector<uint8_t> chunk;
-
- Info()
- : aIns(0),
- aOuts(0),
- cvIns(0),
- cvOuts(0),
- mIns(0),
- mOuts(0),
- category(PLUGIN_CATEGORY_NONE),
- optionsAvailable(0),
- name(),
- label(),
- maker(),
- copyright(),
- aInNames(nullptr),
- aOutNames(nullptr),
- cvInNames(nullptr),
- cvOutNames(nullptr),
- chunk() {}
-
- ~Info()
- {
- clear();
- }
-
- void clear()
- {
- if (aInNames != nullptr)
- {
- CARLA_SAFE_ASSERT_INT(aIns > 0, aIns);
-
- for (uint32_t i=0; i<aIns; ++i)
- delete[] aInNames[i];
-
- delete[] aInNames;
- aInNames = nullptr;
- }
-
- if (aOutNames != nullptr)
- {
- CARLA_SAFE_ASSERT_INT(aOuts > 0, aOuts);
-
- for (uint32_t i=0; i<aOuts; ++i)
- delete[] aOutNames[i];
-
- delete[] aOutNames;
- aOutNames = nullptr;
- }
-
- if (cvInNames != nullptr)
- {
- CARLA_SAFE_ASSERT_INT(cvIns > 0, cvIns);
-
- for (uint32_t i=0; i<cvIns; ++i)
- delete[] cvInNames[i];
-
- delete[] cvInNames;
- cvInNames = nullptr;
- }
-
- if (cvOutNames != nullptr)
- {
- CARLA_SAFE_ASSERT_INT(cvOuts > 0, cvOuts);
-
- for (uint32_t i=0; i<cvOuts; ++i)
- delete[] cvOutNames[i];
-
- delete[] cvOutNames;
- cvOutNames = nullptr;
- }
-
- aIns = aOuts = cvIns = cvOuts = 0;
- }
-
- CARLA_DECLARE_NON_COPYABLE(Info)
- } fInfo;
-
- int64_t fUniqueId;
- uint32_t fLatency;
-
- BridgeParamInfo* fParams;
-
- void handleProcessStopped() noexcept
- {
- const bool wasActive = pData->active;
- pData->active = false;
-
- if (wasActive)
- {
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- pData->engine->callback(true, true,
- ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
- pData->id,
- PARAMETER_ACTIVE,
- 0, 0,
- 0.0f,
- nullptr);
- #endif
- }
-
- if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
- {
- pData->engine->callback(true, true,
- ENGINE_CALLBACK_UI_STATE_CHANGED,
- pData->id,
- 0,
- 0, 0, 0.0f, nullptr);
- }
- }
-
- void resizeAudioPool(const uint32_t bufferSize)
- {
- fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts, fInfo.cvIns+fInfo.cvOuts);
-
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
- fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
- fShmRtClientControl.commitWrite();
-
- waitForClient("resize-pool", 5000);
- }
-
- void waitForClient(const char* const action, const uint msecs)
- {
- CARLA_SAFE_ASSERT_RETURN(! fTimedOut,);
- CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
-
- if (fShmRtClientControl.waitForClient(msecs))
- return;
-
- fTimedOut = true;
- carla_stderr2("waitForClient(%s) timed out", action);
- }
-
- bool restartBridgeThread()
- {
- fInitiated = false;
- fInitError = false;
- fTimedError = false;
-
- // reset memory
- fShmRtClientControl.data->procFlags = 0;
- carla_zeroStruct(fShmRtClientControl.data->timeInfo);
- carla_zeroBytes(fShmRtClientControl.data->midiOut, kBridgeRtClientDataMidiOutSize);
-
- fShmRtClientControl.clearData();
- fShmNonRtClientControl.clearData();
- fShmNonRtServerControl.clearData();
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientVersion);
- fShmNonRtClientControl.writeUInt(CARLA_PLUGIN_BRIDGE_API_VERSION_CURRENT);
-
- fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeRtClientData)));
- fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtClientData)));
- fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtServerData)));
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientInitialSetup);
- fShmNonRtClientControl.writeUInt(pData->engine->getBufferSize());
- fShmNonRtClientControl.writeDouble(pData->engine->getSampleRate());
-
- fShmNonRtClientControl.commitWrite();
-
- if (fShmAudioPool.dataSize != 0)
- {
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
- fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
- fShmRtClientControl.commitWrite();
- }
- else
- {
- // testing dummy message
- fShmRtClientControl.writeOpcode(kPluginBridgeRtClientNull);
- fShmRtClientControl.commitWrite();
- }
-
- fBridgeThread.startThread();
-
- const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- const bool needsCancelableAction = ! pData->engine->isLoadingProject();
-
- if (needsCancelableAction)
- {
- pData->engine->setActionCanceled(false);
- pData->engine->callback(true, true,
- ENGINE_CALLBACK_CANCELABLE_ACTION,
- pData->id,
- 1,
- 0, 0, 0.0f,
- "Loading plugin bridge");
- }
- #endif
-
- for (;fBridgeThread.isThreadRunning();)
- {
- pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
-
- if (needsEngineIdle)
- pData->engine->idle();
-
- idle();
-
- if (fInitiated)
- break;
- if (pData->engine->isAboutToClose() || pData->engine->wasActionCanceled())
- break;
-
- carla_msleep(5);
- }
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- if (needsCancelableAction)
- {
- pData->engine->callback(true, true,
- ENGINE_CALLBACK_CANCELABLE_ACTION,
- pData->id,
- 0,
- 0, 0, 0.0f,
- "Loading plugin bridge");
- }
- #endif
-
- if (fInitError || ! fInitiated)
- {
- fBridgeThread.stopThread(6000);
-
- if (! fInitError)
- pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n"
- "(or the plugin crashed on initialization?)");
-
- return false;
- }
-
- if (const size_t dataSize = fInfo.chunk.size())
- {
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- void* data = fInfo.chunk.data();
- #else
- void* data = &fInfo.chunk.front();
- #endif
- CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
- CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0, true);
-
- String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
-
- filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
- filePath += fShmAudioPool.getFilenameSuffix();
-
- if (File(filePath).replaceWithText(dataBase64.buffer()))
- {
- const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
- fShmNonRtClientControl.writeUInt(ulength);
- fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
- fShmNonRtClientControl.commitWrite();
- }
- }
-
- return true;
- }
-
- void _setUiTitleFromName()
- {
- CarlaString uiName(pData->name);
- uiName += " (GUI)";
-
- const uint32_t size = static_cast<uint32_t>(uiName.length());
-
- const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
-
- fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetWindowTitle);
- fShmNonRtClientControl.writeUInt(size);
- fShmNonRtClientControl.writeCustomData(uiName.buffer(), size);
- fShmNonRtClientControl.commitWrite();
- }
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridge)
- };
-
- CARLA_BACKEND_END_NAMESPACE
-
- // ---------------------------------------------------------------------------------------------------------------------
-
- CARLA_BACKEND_START_NAMESPACE
-
- CarlaPluginPtr CarlaPlugin::newBridge(const Initializer& init,
- const BinaryType btype,
- const PluginType ptype,
- const char* const binaryArchName,
- const char* bridgeBinary)
- {
- carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\", \"%s\")",
- init.engine, init.filename, init.name, init.label,
- BinaryType2Str(btype), PluginType2Str(ptype), binaryArchName, bridgeBinary);
-
- if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
- {
- init.engine->setLastError("Bridge not possible, bridge-binary not found");
- return nullptr;
- }
-
- #ifndef CARLA_OS_WIN
- // FIXME: somewhere, somehow, we end up with double slashes, wine doesn't like that.
- if (std::strncmp(bridgeBinary, "//", 2) == 0)
- ++bridgeBinary;
- #endif
-
- std::shared_ptr<CarlaPluginBridge> plugin(new CarlaPluginBridge(init.engine, init.id, btype, ptype));
-
- if (! plugin->init(plugin, init.filename, init.name, init.label, init.uniqueId, init.options, binaryArchName, bridgeBinary))
- return nullptr;
-
- return plugin;
- }
-
- CARLA_BACKEND_END_NAMESPACE
-
- // ---------------------------------------------------------------------------------------------------------------------
-
- #ifndef BUILD_BRIDGE
- # include "CarlaBridgeUtils.cpp"
- #endif
-
- // ---------------------------------------------------------------------------------------------------------------------
|