|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237 |
- /*
- * Carla Bridge Plugin
- * Copyright (C) 2011-2014 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 "CarlaEngine.hpp"
-
- #ifndef BUILD_BRIDGE
-
- #include "CarlaBackendUtils.hpp"
- #include "CarlaBase64Utils.hpp"
- #include "CarlaBridgeUtils.hpp"
- #include "CarlaMathUtils.hpp"
- #include "CarlaShmUtils.hpp"
-
- #include "jackbridge/JackBridge.hpp"
-
- #include <cerrno>
- #include <cmath>
- #include <ctime>
-
- #define CARLA_BRIDGE_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
- /* check argument count */ \
- if (argc != argcToCompare) \
- { \
- carla_stderr("BridgePlugin::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
- return 1; \
- } \
- if (argc > 0) \
- { \
- /* check for nullness */ \
- if (! (types && typesToCompare)) \
- { \
- carla_stderr("BridgePlugin::%s() - argument types are null", __FUNCTION__); \
- return 1; \
- } \
- /* check argument types */ \
- if (std::strcmp(types, typesToCompare) != 0) \
- { \
- carla_stderr("BridgePlugin::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
- return 1; \
- } \
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- using juce::File;
- using juce::String;
- using juce::StringArray;
-
- CARLA_BACKEND_START_NAMESPACE
-
- // -------------------------------------------------------------------------------------------------------------------
- // call carla_shm_create with for a XXXXXX temp filename
-
- static shm_t shm_mkstemp(char* const fileBase)
- {
- CARLA_SAFE_ASSERT_RETURN(fileBase != nullptr, gNullCarlaShm);
-
- const size_t fileBaseLen(std::strlen(fileBase));
-
- CARLA_SAFE_ASSERT_RETURN(fileBaseLen > 6, gNullCarlaShm);
- CARLA_SAFE_ASSERT_RETURN(std::strcmp(fileBase + fileBaseLen - 6, "XXXXXX") == 0, gNullCarlaShm);
-
- static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "0123456789";
- static const int charSetLen = static_cast<int>(std::strlen(charSet) - 1); // -1 to avoid trailing '\0'
-
- // try until getting a valid shm or an error occurs
- for (;;)
- {
- for (size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
- fileBase[c] = charSet[std::rand() % charSetLen];
-
- const shm_t shm = carla_shm_create(fileBase);
-
- if (carla_is_shm_valid(shm))
- return shm;
- if (errno != EEXIST)
- return gNullCarlaShm;
- }
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- struct BridgeAudioPool {
- CarlaString filename;
- float* data;
- size_t size;
- shm_t shm;
-
- BridgeAudioPool() noexcept
- : filename(),
- data(nullptr),
- size(0),
- shm(shm_t_INIT) {}
-
- ~BridgeAudioPool() noexcept
- {
- // should be cleared by now
- CARLA_SAFE_ASSERT(data == nullptr);
-
- clear();
- }
-
- void clear() noexcept
- {
- filename.clear();
-
- if (! carla_is_shm_valid(shm))
- return;
-
- if (data != nullptr)
- {
- carla_shm_unmap(shm, data, size);
- data = nullptr;
- }
-
- size = 0;
- carla_shm_close(shm);
- }
-
- void resize(const uint32_t bufferSize, const uint32_t portCount) noexcept
- {
- if (data != nullptr)
- carla_shm_unmap(shm, data, size);
-
- size = portCount*bufferSize*sizeof(float);
-
- if (size == 0)
- size = sizeof(float);
-
- data = (float*)carla_shm_map(shm, size);
- }
-
- CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
-
- struct BridgeRtControl : public CarlaRingBuffer<StackBuffer> {
- CarlaString filename;
- BridgeRtData* data;
- shm_t shm;
-
- BridgeRtControl()
- : filename(),
- data(nullptr),
- shm(shm_t_INIT) {}
-
- ~BridgeRtControl()
- {
- // should be cleared by now
- CARLA_SAFE_ASSERT(data == nullptr);
-
- clear();
- }
-
- void clear() noexcept
- {
- filename.clear();
-
- if (! carla_is_shm_valid(shm))
- return;
-
- if (data != nullptr)
- {
- carla_shm_unmap(shm, data, sizeof(BridgeRtData));
- data = nullptr;
- }
-
- carla_shm_close(shm);
- }
-
- bool mapData() noexcept
- {
- CARLA_SAFE_ASSERT(data == nullptr);
-
- if (carla_shm_map<BridgeRtData>(shm, data))
- {
- setRingBuffer(&data->ringBuffer, true);
- return true;
- }
-
- return false;
- }
-
- void unmapData() noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
-
- carla_shm_unmap(shm, data, sizeof(BridgeRtData));
- data = nullptr;
-
- setRingBuffer(nullptr, false);
- }
-
- bool waitForServer(const int secs) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
-
- jackbridge_sem_post(&data->sem.server);
-
- return jackbridge_sem_timedwait(&data->sem.client, secs);
- }
-
- void writeOpcode(const PluginBridgeRtOpcode opcode) noexcept
- {
- writeInt(static_cast<int32_t>(opcode));
- }
-
- CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtControl)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
-
- struct BridgeNonRtControl : public CarlaRingBuffer<BigStackBuffer> {
- CarlaMutex mutex;
- CarlaString filename;
- BridgeNonRtData* data;
- shm_t shm;
-
- BridgeNonRtControl() noexcept
- : mutex(),
- filename(),
- data(nullptr),
- shm(shm_t_INIT) {}
-
- ~BridgeNonRtControl() noexcept
- {
- // should be cleared by now
- CARLA_SAFE_ASSERT(data == nullptr);
-
- clear();
- }
-
- void clear() noexcept
- {
- filename.clear();
-
- if (! carla_is_shm_valid(shm))
- return;
-
- if (data != nullptr)
- {
- carla_shm_unmap(shm, data, sizeof(BridgeNonRtData));
- data = nullptr;
- }
-
- carla_shm_close(shm);
- }
-
- bool mapData() noexcept
- {
- CARLA_SAFE_ASSERT(data == nullptr);
-
- if (carla_shm_map<BridgeNonRtData>(shm, data))
- {
- setRingBuffer(&data->ringBuffer, true);
- return true;
- }
-
- return false;
- }
-
- void unmapData() noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
-
- carla_shm_unmap(shm, data, sizeof(BridgeNonRtData));
- data = nullptr;
-
- setRingBuffer(nullptr, false);
- }
-
- void writeOpcode(const PluginBridgeNonRtOpcode opcode) noexcept
- {
- writeInt(static_cast<int32_t>(opcode));
- }
-
- CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtControl)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
-
- struct BridgeParamInfo {
- float value;
- CarlaString name;
- CarlaString unit;
-
- BridgeParamInfo() noexcept
- : value(0.0f),
- name(),
- unit() {}
-
- CARLA_DECLARE_NON_COPY_STRUCT(BridgeParamInfo)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
-
- class BridgePlugin : public CarlaPlugin
- {
- public:
- BridgePlugin(CarlaEngine* const engine, const uint id, const BinaryType btype, const PluginType ptype)
- : CarlaPlugin(engine, id),
- fBinaryType(btype),
- fPluginType(ptype),
- fInitiated(false),
- fInitError(false),
- fSaved(false),
- fNeedsSemDestroy(false),
- fTimedOut(false),
- fLastPongCounter(-1),
- fBridgeBinary(),
- fShmAudioPool(),
- fShmRtControl(),
- fShmNonRtControl(),
- fInfo(),
- fParams(nullptr),
- leakDetector_BridgePlugin()
- {
- carla_debug("BridgePlugin::BridgePlugin(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
-
- pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_BRIDGE);
-
- pData->hints |= PLUGIN_IS_BRIDGE;
- }
-
- ~BridgePlugin() override
- {
- carla_debug("BridgePlugin::~BridgePlugin()");
-
- // close UI
- if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
- pData->transientTryCounter = 0;
-
- pData->singleMutex.lock();
- pData->masterMutex.lock();
-
- if (pData->client != nullptr && pData->client->isActive())
- pData->client->deactivate();
-
- if (pData->active)
- {
- deactivate();
- pData->active = false;
- }
-
- if (pData->osc.thread.isThreadRunning())
- {
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtQuit);
- fShmNonRtControl.commitWrite();
-
- if (! fTimedOut)
- fShmRtControl.waitForServer(3);
- }
-
- pData->osc.data.clear();
- pData->osc.thread.stopThread(3000);
-
- if (fNeedsSemDestroy)
- {
- jackbridge_sem_destroy(&fShmRtControl.data->sem.server);
- jackbridge_sem_destroy(&fShmRtControl.data->sem.client);
- }
-
- fShmAudioPool.clear();
- fShmRtControl.clear();
- fShmNonRtControl.clear();
-
- clearBuffers();
-
- fInfo.chunk.clear();
- }
-
- // -------------------------------------------------------------------
- // Information (base)
-
- BinaryType getBinaryType() const noexcept
- {
- return fBinaryType;
- }
-
- PluginType getType() const noexcept override
- {
- return fPluginType;
- }
-
- PluginCategory getCategory() const noexcept override
- {
- return fInfo.category;
- }
-
- int64_t getUniqueId() const noexcept override
- {
- return fInfo.uniqueId;
- }
-
- // -------------------------------------------------------------------
- // 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);
- CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.size() > 0, 0);
-
- *dataPtr = fInfo.chunk.data();
- 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;
- }
-
- void getLabel(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.label, STR_MAX);
- }
-
- void getMaker(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.maker, STR_MAX);
- }
-
- void getCopyright(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.copyright, STR_MAX);
- }
-
- void getRealName(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fInfo.name, STR_MAX);
- }
-
- void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_ASSERT(parameterId < pData->param.count);
-
- std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
- }
-
- void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_ASSERT(parameterId < pData->param.count);
-
- std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
- }
-
- // -------------------------------------------------------------------
- // Set data (state)
-
- void prepareForSave() override
- {
- fSaved = false;
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPrepareForSave);
- fShmNonRtControl.commitWrite();
- }
-
- carla_stdout("BridgePlugin::prepareForSave() - sent, now waiting...");
-
- for (int i=0; i < 200; ++i)
- {
- if (fSaved)
- break;
- carla_msleep(30);
- pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
- pData->engine->idle();
- }
-
- if (! fSaved)
- carla_stderr("BridgePlugin::prepareForSave() - Timeout while requesting save state");
- else
- carla_stdout("BridgePlugin::prepareForSave() - success!");
- }
-
- // -------------------------------------------------------------------
- // Set data (internal stuff)
-
- void setOption(const uint option, const bool yesNo, const bool sendCallback) override
- {
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetOption);
- fShmNonRtControl.writeUInt(option);
- fShmNonRtControl.writeBool(yesNo);
- fShmNonRtControl.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,); // never call this from RT
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetCtrlChannel);
- fShmNonRtControl.writeShort(channel);
- fShmNonRtControl.commitWrite();
- }
-
- CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
- }
-
- // -------------------------------------------------------------------
- // 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,);
-
- const float fixedValue(pData->param.getFixedValue(parameterId, value));
- fParams[parameterId].value = fixedValue;
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
- fShmNonRtControl.writeUInt(parameterId);
- fShmNonRtControl.writeFloat(value);
- fShmNonRtControl.commitWrite();
- }
-
- CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
- }
-
- void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
- CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
- fShmNonRtControl.writeUInt(parameterId);
- fShmNonRtControl.writeByte(channel);
- fShmNonRtControl.commitWrite();
- }
-
- CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
- }
-
- void setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
- CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterMidiCC);
- fShmNonRtControl.writeUInt(parameterId);
- fShmNonRtControl.writeShort(cc);
- fShmNonRtControl.commitWrite();
- }
-
- CarlaPlugin::setParameterMidiCC(parameterId, cc, sendOsc, sendCallback);
- }
-
- void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetProgram);
- fShmNonRtControl.writeInt(index);
- fShmNonRtControl.commitWrite();
- }
-
- CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
- }
-
- void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetMidiProgram);
- fShmNonRtControl.writeInt(index);
- fShmNonRtControl.commitWrite();
- }
-
- CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
- }
-
- #if 0
- void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
- {
- CARLA_ASSERT(type);
- CARLA_ASSERT(key);
- CARLA_ASSERT(value);
-
- if (sendGui)
- {
- // TODO - if type is chunk|binary, store it in a file and send path instead
- QString cData;
- cData = type;
- cData += "·";
- cData += key;
- cData += "·";
- cData += value;
- osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CUSTOM, cData.toUtf8().constData());
- }
-
- CarlaPlugin::setCustomData(type, key, value, sendGui);
- }
- #endif
-
- 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;
- filePath += ".CarlaChunk_";
- filePath += fShmAudioPool.filename.buffer() + 18;
-
- if (File(filePath).replaceWithText(dataBase64.buffer()))
- {
- const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
-
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetChunkDataFile);
- fShmNonRtControl.writeUInt(ulength);
- fShmNonRtControl.writeCustomData(filePath.toRawUTF8(), ulength);
- fShmNonRtControl.commitWrite();
- }
- }
-
- // -------------------------------------------------------------------
- // Set ui stuff
-
- void showCustomUI(const bool yesNo) override
- {
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(yesNo ? kPluginBridgeNonRtShowUI : kPluginBridgeNonRtHideUI);
- fShmNonRtControl.commitWrite();
- }
-
- if (yesNo)
- {
- pData->tryTransient();
- }
- else
- {
- pData->transientTryCounter = 0;
- }
- }
-
- void idle() override
- {
- if (pData->osc.thread.isThreadRunning())
- {
- if (fTimedOut && pData->active)
- setActive(false, true, true);
-
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPing);
- fShmNonRtControl.commitWrite();
- }
- else
- carla_stderr2("TESTING: Bridge has closed!");
-
- CarlaPlugin::idle();
- }
-
- // -------------------------------------------------------------------
- // Plugin state
-
- void reload() override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
- carla_debug("BridgePlugin::reload() - start");
-
- const EngineProcessMode processMode(pData->engine->getProccessMode());
-
- // Safely disable plugin for reload
- const ScopedDisabler sd(this);
-
- 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.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.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);
- 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.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);
- pData->audioOut.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);
- }
-
- 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);
- }
-
- // 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;
-
- if (fInfo.aIns <= 2 && fInfo.aOuts <= 2 && (fInfo.aIns == fInfo.aOuts || fInfo.aIns == 0 || fInfo.aOuts == 0))
- pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
-
- bufferSizeChanged(pData->engine->getBufferSize());
- reloadPrograms(true);
-
- carla_debug("BridgePlugin::reload() - end");
- }
-
- // -------------------------------------------------------------------
- // Plugin processing
-
- void activate() noexcept override
- {
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtActivate);
- fShmNonRtControl.commitWrite();
- }
-
- bool timedOut = true;
-
- try {
- timedOut = waitForServer(1);
- } catch(...) {}
-
- if (! timedOut)
- fTimedOut = false;
- }
-
- void deactivate() noexcept override
- {
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtDeactivate);
- fShmNonRtControl.commitWrite();
- }
-
- bool timedOut = true;
-
- try {
- timedOut = waitForServer(1);
- } catch(...) {}
-
- if (! timedOut)
- fTimedOut = false;
- }
-
- void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
- {
- // --------------------------------------------------------------------------------------------------------
- // Check if active
-
- if (fTimedOut || ! pData->active)
- {
- // disable any output sound
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- FloatVectorOperations::clear(outBuffer[i], static_cast<int>(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.begin(); it.valid(); it.next())
- {
- const ExternalMidiNote& note(it.getValue());
-
- CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
-
- uint8_t data1, data2, data3;
- data1 = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
- data2 = note.note;
- data3 = note.velo;
-
- fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
- fShmRtControl.writeUInt(0); // time
- fShmRtControl.writeByte(0); // port
- fShmRtControl.writeByte(3); // size
- fShmRtControl.writeByte(data1);
- fShmRtControl.writeByte(data2);
- fShmRtControl.writeByte(data3);
- fShmRtControl.commitWrite();
- }
-
- pData->extNotes.data.clear();
- pData->extNotes.mutex.unlock();
-
- } // End of MIDI Input (External)
-
- // ----------------------------------------------------------------------------------------------------
- // Event Input (System)
-
- bool allNotesOffSent = false;
-
- for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
- {
- const EngineEvent& event(pData->event.portIn->getEvent(i));
-
- // Control change
- switch (event.type)
- {
- case kEngineEventTypeNull:
- break;
-
- case kEngineEventTypeControl: {
- const EngineControlEvent& ctrlEvent = event.ctrl;
-
- switch (ctrlEvent.type)
- {
- case kEngineControlEventTypeNull:
- break;
-
- case kEngineControlEventTypeParameter:
- // Control backend stuff
- if (event.channel == pData->ctrlChannel)
- {
- float value;
-
- if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
- {
- value = ctrlEvent.value;
- setDryWet(value, false, false);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
- break;
- }
-
- if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
- {
- value = ctrlEvent.value*127.0f/100.0f;
- setVolume(value, false, false);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
- break;
- }
-
- if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
- {
- float left, right;
- value = ctrlEvent.value/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;
- }
-
- setBalanceLeft(left, false, false);
- setBalanceRight(right, false, false);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
- break;
- }
- }
-
- fShmRtControl.writeOpcode(kPluginBridgeRtControlEventParameter);
- fShmRtControl.writeUInt(event.time);
- fShmRtControl.writeByte(event.channel);
- fShmRtControl.writeUShort(event.ctrl.param);
- fShmRtControl.writeFloat(event.ctrl.value);
- fShmRtControl.commitWrite();
- break;
-
- case kEngineControlEventTypeMidiBank:
- if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
- {
- fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiBank);
- fShmRtControl.writeUInt(event.time);
- fShmRtControl.writeByte(event.channel);
- fShmRtControl.writeUShort(event.ctrl.param);
- fShmRtControl.commitWrite();
- }
- break;
-
- case kEngineControlEventTypeMidiProgram:
- if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
- {
- fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiProgram);
- fShmRtControl.writeUInt(event.time);
- fShmRtControl.writeByte(event.channel);
- fShmRtControl.writeUShort(event.ctrl.param);
- fShmRtControl.commitWrite();
- }
- break;
-
- case kEngineControlEventTypeAllSoundOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllSoundOff);
- fShmRtControl.writeUInt(event.time);
- fShmRtControl.writeByte(event.channel);
- fShmRtControl.commitWrite();
- }
- break;
-
- case kEngineControlEventTypeAllNotesOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
- {
- allNotesOffSent = true;
- sendMidiAllNotesOffToCallback();
- }
-
- fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllNotesOff);
- fShmRtControl.writeUInt(event.time);
- fShmRtControl.writeByte(event.channel);
- fShmRtControl.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;
-
- uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
- uint8_t channel = event.channel;
-
- if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
- status = MIDI_STATUS_NOTE_OFF;
-
- 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;
-
- fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
- fShmRtControl.writeUInt(event.time);
- fShmRtControl.writeByte(midiEvent.port);
- fShmRtControl.writeByte(midiEvent.size);
-
- for (uint8_t j=0; j < midiEvent.size; ++j)
- fShmRtControl.writeByte(midiEvent.data[j]);
-
- fShmRtControl.commitWrite();
-
- if (status == MIDI_STATUS_NOTE_ON)
- pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
- else if (status == MIDI_STATUS_NOTE_OFF)
- pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
- } break;
- }
- }
-
- pData->postRtEvents.trySplice();
-
- } // End of Event Input
-
- processSingle(inBuffer, outBuffer, frames);
- }
-
- bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
- {
- CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
-
- if (pData->audioIn.count > 0)
- {
- CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
- }
- if (pData->audioOut.count > 0)
- {
- CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Try lock, silence otherwise
-
- if (pData->engine->isOffline())
- {
- pData->singleMutex.lock();
- }
- else if (! pData->singleMutex.tryLock())
- {
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
- return false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Reset audio buffers
-
- //std::memset(fShmAudioPool.data, 0, fShmAudioPool.size);
-
- for (uint32_t i=0; i < fInfo.aIns; ++i)
- FloatVectorOperations::copy(fShmAudioPool.data + (i * frames), inBuffer[i], static_cast<int>(frames));
-
- // --------------------------------------------------------------------------------------------------------
- // TimeInfo
-
- const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
- BridgeTimeInfo& bridgeTimeInfo(fShmRtControl.data->timeInfo);
-
- bridgeTimeInfo.playing = timeInfo.playing;
- bridgeTimeInfo.frame = timeInfo.frame;
- bridgeTimeInfo.usecs = timeInfo.usecs;
- bridgeTimeInfo.valid = timeInfo.valid;
-
- if (timeInfo.valid & EngineTimeInfo::kValidBBT)
- {
- 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
-
- {
- fShmRtControl.writeOpcode(kPluginBridgeRtProcess);
- fShmRtControl.commitWrite();
- }
-
- if (! waitForServer(2))
- {
- pData->singleMutex.unlock();
- return true;
- }
-
- for (uint32_t i=0; i < fInfo.aOuts; ++i)
- FloatVectorOperations::copy(outBuffer[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), static_cast<int>(frames));
-
- // --------------------------------------------------------------------------------------------------------
- // Post-processing (dry/wet, volume and balance)
-
- {
- const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && pData->postProc.volume != 1.0f;
- const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
- const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
-
- bool isPair;
- float bufValue, oldBufLeft[doBalance ? frames : 1];
-
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- {
- // Dry/Wet
- if (doDryWet)
- {
- for (uint32_t k=0; k < frames; ++k)
- {
- bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k];
- outBuffer[i][k] = (outBuffer[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);
- FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(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
- outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
- outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
- }
- else
- {
- // right
- outBuffer[i][k] = outBuffer[i][k] * balRangeR;
- outBuffer[i][k] += oldBufLeft[k] * balRangeL;
- }
- }
- }
-
- // Volume (and buffer copy)
- if (doVolume)
- {
- for (uint32_t k=0; k < frames; ++k)
- outBuffer[i][k] *= pData->postProc.volume;
- }
- }
-
- } // End of Post-processing
-
- // --------------------------------------------------------------------------------------------------------
-
- pData->singleMutex.unlock();
- return true;
- }
-
- void bufferSizeChanged(const uint32_t newBufferSize) override
- {
- resizeAudioPool(newBufferSize);
-
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
- fShmNonRtControl.writeUInt(newBufferSize);
- fShmNonRtControl.commitWrite();
- }
-
- fShmRtControl.waitForServer(1);
- }
-
- void sampleRateChanged(const double newSampleRate) override
- {
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
- fShmNonRtControl.writeDouble(newSampleRate);
- fShmNonRtControl.commitWrite();
- }
-
- fShmRtControl.waitForServer(1);
- }
-
- void offlineModeChanged(const bool isOffline) override
- {
- {
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
- fShmNonRtControl.writeOpcode(isOffline ? kPluginBridgeNonRtSetOffline : kPluginBridgeNonRtSetOnline);
- fShmNonRtControl.commitWrite();
- }
-
- fShmRtControl.waitForServer(1);
- }
-
- // -------------------------------------------------------------------
- // Plugin buffers
-
- void clearBuffers() noexcept override
- {
- if (fParams != nullptr)
- {
- delete[] fParams;
- fParams = nullptr;
- }
-
- CarlaPlugin::clearBuffers();
- }
-
- // -------------------------------------------------------------------
- // OSC stuff
-
- void updateOscURL() override
- {
- const String newURL(String(pData->engine->getOscServerPathUDP()) + String("/") + String(pData->id));
- const uint32_t ulength(static_cast<uint32_t>(newURL.length()));
-
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetOscURL);
- fShmNonRtControl.writeUInt(ulength);
- fShmNonRtControl.writeCustomData(newURL.toRawUTF8(), ulength);
- fShmNonRtControl.commitWrite();
- }
-
- // -------------------------------------------------------------------
- // 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(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiParameterChange);
- fShmNonRtControl.writeUInt(index);
- fShmNonRtControl.writeFloat(value);
- fShmNonRtControl.commitWrite();
- }
-
- void uiProgramChange(const uint32_t index) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
-
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiProgramChange);
- fShmNonRtControl.writeUInt(index);
- fShmNonRtControl.commitWrite();
- }
-
- void uiMidiProgramChange(const uint32_t index) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
-
- const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiMidiProgramChange);
- fShmNonRtControl.writeUInt(index);
- fShmNonRtControl.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(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiNoteOn);
- fShmNonRtControl.writeByte(channel);
- fShmNonRtControl.writeByte(note);
- fShmNonRtControl.writeByte(velo);
- fShmNonRtControl.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(fShmNonRtControl.mutex);
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiNoteOff);
- fShmNonRtControl.writeByte(channel);
- fShmNonRtControl.writeByte(note);
- fShmNonRtControl.commitWrite();
- }
-
- // -------------------------------------------------------------------
-
- int setOscPluginBridgeInfo(const PluginBridgeOscInfoType infoType, const int argc, const lo_arg* const* const argv, const char* const types)
- {
- #ifdef DEBUG
- if (infoType != kPluginBridgeOscPong) {
- carla_debug("BridgePlugin::setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeOscInfoType2str(infoType), argc, argv, types);
- }
- #endif
-
- switch (infoType)
- {
- case kPluginBridgeOscNull:
- break;
-
- case kPluginBridgeOscPong:
- if (fLastPongCounter > 0)
- fLastPongCounter = 0;
- break;
-
- case kPluginBridgeOscPluginInfo1: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiih");
-
- const int32_t category = argv[0]->i;
- const int32_t hints = argv[1]->i;
- const int32_t optionAv = argv[2]->i;
- const int32_t optionEn = argv[3]->i;
- const int64_t uniqueId = argv[4]->h;
-
- CARLA_SAFE_ASSERT_BREAK(category >= 0);
- CARLA_SAFE_ASSERT_BREAK(hints >= 0);
- CARLA_SAFE_ASSERT_BREAK(optionAv >= 0);
- CARLA_SAFE_ASSERT_BREAK(optionEn >= 0);
-
- pData->hints = static_cast<uint>(hints);
- pData->hints |= PLUGIN_IS_BRIDGE;
-
- pData->options = static_cast<uint>(optionEn);
-
- fInfo.category = static_cast<PluginCategory>(category);
- fInfo.uniqueId = uniqueId;
- fInfo.optionsAvailable = static_cast<uint>(optionAv);
- break;
- }
-
- case kPluginBridgeOscPluginInfo2: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ssss");
-
- const char* const realName = (const char*)&argv[0]->s;
- const char* const label = (const char*)&argv[1]->s;
- const char* const maker = (const char*)&argv[2]->s;
- const char* const copyright = (const char*)&argv[3]->s;
-
- CARLA_SAFE_ASSERT_BREAK(realName != nullptr);
- CARLA_SAFE_ASSERT_BREAK(label != nullptr);
- CARLA_SAFE_ASSERT_BREAK(maker != nullptr);
- CARLA_SAFE_ASSERT_BREAK(copyright != nullptr);
-
- fInfo.name = realName;
- fInfo.label = label;
- fInfo.maker = maker;
- fInfo.copyright = copyright;
-
- if (pData->name == nullptr)
- pData->name = pData->engine->getUniquePluginName(realName);
- break;
- }
-
- case kPluginBridgeOscAudioCount: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
-
- const int32_t ins = argv[0]->i;
- const int32_t outs = argv[1]->i;
-
- CARLA_SAFE_ASSERT_BREAK(ins >= 0);
- CARLA_SAFE_ASSERT_BREAK(outs >= 0);
-
- fInfo.aIns = static_cast<uint32_t>(ins);
- fInfo.aOuts = static_cast<uint32_t>(outs);
- break;
- }
-
- case kPluginBridgeOscMidiCount: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
-
- const int32_t ins = argv[0]->i;
- const int32_t outs = argv[1]->i;
-
- CARLA_SAFE_ASSERT_BREAK(ins >= 0);
- CARLA_SAFE_ASSERT_BREAK(outs >= 0);
-
- fInfo.mIns = static_cast<uint32_t>(ins);
- fInfo.mOuts = static_cast<uint32_t>(outs);
- break;
- }
-
- case kPluginBridgeOscParameterCount: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
-
- const int32_t ins = argv[0]->i;
- const int32_t outs = argv[1]->i;
-
- CARLA_SAFE_ASSERT_BREAK(ins >= 0);
- CARLA_SAFE_ASSERT_BREAK(outs >= 0);
-
- // delete old data
- pData->param.clear();
-
- if (fParams != nullptr)
- {
- delete[] fParams;
- fParams = nullptr;
- }
-
- if (int32_t count = ins+outs)
- {
- const int32_t maxParams(static_cast<int32_t>(pData->engine->getOptions().maxParameters));
-
- if (count > maxParams)
- {
- count = maxParams;
- carla_safe_assert_int2("count <= pData->engine->getOptions().maxParameters", __FILE__, __LINE__, count, maxParams);
- }
-
- const uint32_t ucount(static_cast<uint32_t>(count));
-
- pData->param.createNew(ucount, false);
- fParams = new BridgeParamInfo[ucount];
- }
- break;
- }
-
- case kPluginBridgeOscProgramCount: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
-
- const int32_t count = argv[0]->i;
-
- CARLA_SAFE_ASSERT_BREAK(count >= 0);
-
- pData->prog.clear();
-
- if (count > 0)
- pData->prog.createNew(static_cast<uint32_t>(count));
-
- break;
- }
-
- case kPluginBridgeOscMidiProgramCount: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
-
- const int32_t count = argv[0]->i;
-
- CARLA_SAFE_ASSERT_BREAK(count >= 0);
-
- pData->midiprog.clear();
-
- if (count > 0)
- pData->midiprog.createNew(static_cast<uint32_t>(count));
- break;
- }
-
- case kPluginBridgeOscParameterData1: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiii");
-
- const int32_t index = argv[0]->i;
- const int32_t rindex = argv[1]->i;
- const int32_t type = argv[2]->i;
- const int32_t hints = argv[3]->i;
- const int32_t midiCC = argv[4]->i;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_BREAK(rindex >= 0);
- CARLA_SAFE_ASSERT_BREAK(type >= 0);
- CARLA_SAFE_ASSERT_BREAK(hints >= 0);
- CARLA_SAFE_ASSERT_BREAK(midiCC >= -1 && midiCC < 0x5F);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
-
- if (index < static_cast<int32_t>(pData->param.count))
- {
- pData->param.data[index].type = static_cast<ParameterType>(type);
- pData->param.data[index].index = index;
- pData->param.data[index].rindex = rindex;
- pData->param.data[index].hints = static_cast<uint>(hints);
- pData->param.data[index].midiCC = static_cast<int16_t>(midiCC);
- }
- break;
- }
-
- case kPluginBridgeOscParameterData2: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iss");
-
- const int32_t index = argv[0]->i;
- const char* const name = (const char*)&argv[1]->s;
- const char* const unit = (const char*)&argv[2]->s;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_BREAK(name != nullptr);
- CARLA_SAFE_ASSERT_BREAK(unit != nullptr);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
-
- if (index < static_cast<int32_t>(pData->param.count))
- {
- fParams[index].name = name;
- fParams[index].unit = unit;
- }
- break;
- }
-
- case kPluginBridgeOscParameterRanges1: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
-
- const int32_t index = argv[0]->i;
- const float def = argv[1]->f;
- const float min = argv[2]->f;
- const float max = argv[3]->f;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_BREAK(min < max);
- CARLA_SAFE_ASSERT_BREAK(def >= min);
- CARLA_SAFE_ASSERT_BREAK(def <= max);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
-
- if (index < static_cast<int32_t>(pData->param.count))
- {
- pData->param.ranges[index].def = def;
- pData->param.ranges[index].min = min;
- pData->param.ranges[index].max = max;
- }
- break;
- }
-
- case kPluginBridgeOscParameterRanges2: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
-
- const int32_t index = argv[0]->i;
- const float step = argv[1]->f;
- const float stepSmall = argv[2]->f;
- const float stepLarge = argv[3]->f;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
-
- if (index < static_cast<int32_t>(pData->param.count))
- {
- pData->param.ranges[index].step = step;
- pData->param.ranges[index].stepSmall = stepSmall;
- pData->param.ranges[index].stepLarge = stepLarge;
- }
- break;
- }
-
- case kPluginBridgeOscParameterValue: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
-
- const int32_t index = argv[0]->i;
- const float value = argv[1]->f;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
-
- if (index < static_cast<int32_t>(pData->param.count))
- {
- const uint32_t uindex(static_cast<uint32_t>(index));
- const float fixedValue(pData->param.getFixedValue(uindex, value));
- fParams[uindex].value = fixedValue;
-
- CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
- }
- break;
- }
-
- case kPluginBridgeOscDefaultValue: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
-
- const int32_t index = argv[0]->i;
- const float value = argv[1]->f;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
-
- if (index < static_cast<int32_t>(pData->param.count))
- pData->param.ranges[index].def = value;
- break;
- }
-
- case kPluginBridgeOscCurrentProgram: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
-
- const int32_t index = argv[0]->i;
-
- 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 kPluginBridgeOscCurrentMidiProgram: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
-
- const int32_t index = argv[0]->i;
-
- 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 kPluginBridgeOscProgramName: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
-
- const int32_t index = argv[0]->i;
- const char* const name = (const char*)&argv[1]->s;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_BREAK(name != nullptr);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
-
- if (index < static_cast<int32_t>(pData->prog.count))
- {
- if (pData->prog.names[index] != nullptr)
- delete[] pData->prog.names[index];
- pData->prog.names[index] = carla_strdup(name);
- }
- break;
- }
-
- case kPluginBridgeOscMidiProgramData: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
-
- const int32_t index = argv[0]->i;
- const int32_t bank = argv[1]->i;
- const int32_t program = argv[2]->i;
- const char* const name = (const char*)&argv[3]->s;
-
- CARLA_SAFE_ASSERT_BREAK(index >= 0);
- CARLA_SAFE_ASSERT_BREAK(bank >= 0);
- CARLA_SAFE_ASSERT_BREAK(program >= 0);
- CARLA_SAFE_ASSERT_BREAK(name != nullptr);
- CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
-
- if (index < static_cast<int32_t>(pData->midiprog.count))
- {
- if (pData->midiprog.data[index].name != nullptr)
- delete[] pData->midiprog.data[index].name;
- pData->midiprog.data[index].bank = static_cast<uint32_t>(bank);
- pData->midiprog.data[index].program = static_cast<uint32_t>(program);
- pData->midiprog.data[index].name = carla_strdup(name);
- }
- break;
- }
-
- case kPluginBridgeOscConfigure: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
-
- const char* const key = (const char*)&argv[0]->s;
- const char* const value = (const char*)&argv[1]->s;
-
- CARLA_SAFE_ASSERT_BREAK(key != nullptr);
- CARLA_SAFE_ASSERT_BREAK(value != nullptr);
-
- if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
- pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
- else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
- fSaved = true;
- break;
- }
-
- case kPluginBridgeOscSetCustomData: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
-
- const char* const type = (const char*)&argv[0]->s;
- const char* const key = (const char*)&argv[1]->s;
- const char* const value = (const char*)&argv[2]->s;
-
- CARLA_SAFE_ASSERT_BREAK(type != nullptr);
- CARLA_SAFE_ASSERT_BREAK(key != nullptr);
- CARLA_SAFE_ASSERT_BREAK(value != nullptr);
-
- CarlaPlugin::setCustomData(type, key, value, false);
- break;
- }
-
- case kPluginBridgeOscSetChunkDataFile: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
-
- const char* const chunkFilePath = (const char*)&argv[0]->s;
-
- CARLA_SAFE_ASSERT_BREAK(chunkFilePath != nullptr);
-
- String realChunkFilePath(chunkFilePath);
- carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
-
- #ifndef CARLA_OS_WIN
- // Using Wine, fix temp dir
- if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
- {
- // Get WINEPREFIX
- String wineDir;
- if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
- wineDir = String(WINEPREFIX);
- else
- wineDir = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
-
- const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
-
- realChunkFilePath = wineDir;
- realChunkFilePath += "/drive_";
- realChunkFilePath += driveLetterSplit[0].toLowerCase();
- realChunkFilePath += "/";
- realChunkFilePath += driveLetterSplit[1];
-
- realChunkFilePath = realChunkFilePath.replace("\\", "/");
- carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
- }
- #endif
-
- File chunkFile(realChunkFilePath);
-
- if (chunkFile.existsAsFile())
- {
- fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
- chunkFile.deleteFile();
- carla_stderr("chunk data final");
- }
- break;
- }
-
- case kPluginBridgeOscLatency:
- // TODO
- break;
-
- case kPluginBridgeOscReady:
- fInitiated = true;
- break;
-
- case kPluginBridgeOscError: {
- CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
-
- const char* const error = (const char*)&argv[0]->s;
-
- CARLA_ASSERT(error != nullptr);
-
- pData->engine->setLastError(error);
-
- fInitError = true;
- fInitiated = true;
- break;
- }
- }
-
- return 0;
- }
-
- // -------------------------------------------------------------------
-
- const void* getExtraStuff() const noexcept override
- {
- return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
- }
-
- bool init(const char* const filename, const char* const name, const char* const label, 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;
- }
-
- // ---------------------------------------------------------------
- // set info
-
- if (name != nullptr && name[0] != '\0')
- pData->name = pData->engine->getUniquePluginName(name);
-
- pData->filename = carla_strdup(filename);
-
- if (bridgeBinary != nullptr)
- fBridgeBinary = bridgeBinary;
-
- std::srand(static_cast<uint>(std::time(nullptr)));
-
- // ---------------------------------------------------------------
- // SHM Audio Pool
-
- {
- char tmpFileBase[64];
-
- std::sprintf(tmpFileBase, "/carla-bridge_shm_ap_XXXXXX");
-
- fShmAudioPool.shm = shm_mkstemp(tmpFileBase);
-
- if (! carla_is_shm_valid(fShmAudioPool.shm))
- {
- carla_stdout("Failed to open or create shared memory file #1");
- return false;
- }
-
- fShmAudioPool.filename = tmpFileBase;
- }
-
- // ---------------------------------------------------------------
- // SHM RT Control
-
- {
- char tmpFileBase[64];
-
- std::sprintf(tmpFileBase, "/carla-bridge_shm_rt_XXXXXX");
-
- fShmRtControl.shm = shm_mkstemp(tmpFileBase);
-
- if (! carla_is_shm_valid(fShmRtControl.shm))
- {
- carla_stdout("Failed to open or create shared memory file #2");
- // clear
- carla_shm_close(fShmAudioPool.shm);
- return false;
- }
-
- fShmRtControl.filename = tmpFileBase;
-
- if (! fShmRtControl.mapData())
- {
- carla_stdout("Failed to map shared memory file #2");
- // clear
- carla_shm_close(fShmRtControl.shm);
- carla_shm_close(fShmAudioPool.shm);
- return false;
- }
-
- CARLA_SAFE_ASSERT(fShmRtControl.data != nullptr);
-
- if (! jackbridge_sem_init(&fShmRtControl.data->sem.server))
- {
- carla_stdout("Failed to initialize shared memory semaphore #1");
- // clear
- fShmRtControl.unmapData();
- carla_shm_close(fShmRtControl.shm);
- carla_shm_close(fShmAudioPool.shm);
- return false;
- }
-
- if (! jackbridge_sem_init(&fShmRtControl.data->sem.client))
- {
- carla_stdout("Failed to initialize shared memory semaphore #2");
- // clear
- jackbridge_sem_destroy(&fShmRtControl.data->sem.server);
- fShmRtControl.unmapData();
- carla_shm_close(fShmRtControl.shm);
- carla_shm_close(fShmAudioPool.shm);
- return false;
- }
-
- fNeedsSemDestroy = true;
- }
-
- // ---------------------------------------------------------------
- // SHM Non-RT Control
- {
- char tmpFileBase[64];
-
- std::sprintf(tmpFileBase, "/carla-bridge_shm_nonrt_XXXXXX");
-
- fShmNonRtControl.shm = shm_mkstemp(tmpFileBase);
-
- if (! carla_is_shm_valid(fShmNonRtControl.shm))
- {
- carla_stdout("Failed to open or create shared memory file #3");
- return false;
- }
-
- fShmNonRtControl.filename = tmpFileBase;
-
- if (! fShmNonRtControl.mapData())
- {
- carla_stdout("Failed to map shared memory file #3");
- // clear
- fShmNonRtControl.unmapData();
- carla_shm_close(fShmNonRtControl.shm);
- carla_shm_close(fShmRtControl.shm);
- carla_shm_close(fShmAudioPool.shm);
- return false;
- }
- }
-
- carla_stdout("Carla Server Info:");
- carla_stdout(" sizeof(BridgeRtData): " P_SIZE, sizeof(BridgeRtData));
- carla_stdout(" sizeof(BridgeNonRtData): " P_SIZE, sizeof(BridgeNonRtData));
-
- // initial values
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtNull);
- fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeRtData)));
- fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeNonRtData)));
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
- fShmNonRtControl.writeInt(static_cast<int32_t>(pData->engine->getBufferSize()));
-
- fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
- fShmNonRtControl.writeDouble(pData->engine->getSampleRate());
-
- fShmNonRtControl.commitWrite();
-
- // register plugin now so we can receive OSC (and wait for it)
- pData->hints |= PLUGIN_IS_BRIDGE;
- pData->engine->registerEnginePlugin(pData->id, this);
-
- // init OSC
- {
- char shmIdStr[18+1] = { 0 };
- std::strncpy(shmIdStr, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
- std::strncat(shmIdStr, &fShmRtControl.filename[fShmRtControl.filename.length()-6], 6);
- std::strncat(shmIdStr, &fShmNonRtControl.filename[fShmNonRtControl.filename.length()-6], 6);
-
- pData->osc.thread.setOscData(bridgeBinary, label, getPluginTypeAsString(fPluginType), shmIdStr);
- pData->osc.thread.startThread();
- }
-
- fInitiated = false;
- fLastPongCounter = 0;
-
- for (; fLastPongCounter++ < 500;)
- {
- if (fInitiated || ! pData->osc.thread.isThreadRunning())
- break;
- carla_msleep(25);
- pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
- pData->engine->idle();
- }
-
- fLastPongCounter = -1;
-
- if (fInitError || ! fInitiated)
- {
- pData->osc.thread.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;
- }
-
- // ---------------------------------------------------------------
- // register client
-
- if (pData->name == nullptr)
- {
- if (name != nullptr && name[0] != '\0')
- pData->name = pData->engine->getUniquePluginName(name);
- else if (label != nullptr && label[0] != '\0')
- pData->name = pData->engine->getUniquePluginName(label);
- else
- pData->name = pData->engine->getUniquePluginName("unknown");
- }
-
- pData->client = pData->engine->addClient(this);
-
- if (pData->client == nullptr || ! pData->client->isOk())
- {
- pData->engine->setLastError("Failed to register plugin client");
- return false;
- }
-
- return true;
- }
-
- private:
- const BinaryType fBinaryType;
- const PluginType fPluginType;
-
- bool fInitiated;
- bool fInitError;
- bool fSaved;
- bool fNeedsSemDestroy;
- bool fTimedOut;
-
- volatile int32_t fLastPongCounter;
-
- CarlaString fBridgeBinary;
-
- BridgeAudioPool fShmAudioPool;
- BridgeRtControl fShmRtControl;
- BridgeNonRtControl fShmNonRtControl;
-
- struct Info {
- uint32_t aIns, aOuts;
- uint32_t mIns, mOuts;
- PluginCategory category;
- uint optionsAvailable;
- int64_t uniqueId;
- CarlaString name;
- CarlaString label;
- CarlaString maker;
- CarlaString copyright;
- std::vector<uint8_t> chunk;
-
- Info()
- : aIns(0),
- aOuts(0),
- mIns(0),
- mOuts(0),
- category(PLUGIN_CATEGORY_NONE),
- optionsAvailable(0),
- uniqueId(0),
- name(),
- label(),
- maker(),
- copyright(),
- chunk() {}
- } fInfo;
-
- BridgeParamInfo* fParams;
-
- void resizeAudioPool(const uint32_t bufferSize)
- {
- fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts);
-
- fShmRtControl.writeOpcode(kPluginBridgeRtSetAudioPool);
- fShmRtControl.writeLong(static_cast<int64_t>(fShmAudioPool.size));
- fShmRtControl.commitWrite();
-
- waitForServer();
- }
-
- bool waitForServer(const int secs = 5)
- {
- CARLA_SAFE_ASSERT_RETURN(! fTimedOut, false);
-
- if (! fShmRtControl.waitForServer(secs))
- {
- carla_stderr("waitForServer() timeout here");
- fTimedOut = true;
- return false;
- }
-
- return true;
- }
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BridgePlugin)
- };
-
- CARLA_BACKEND_END_NAMESPACE
-
- #endif // ! BUILD_BRIDGE
-
- // -------------------------------------------------------------------------------------------------------------------
-
- CARLA_BACKEND_START_NAMESPACE
-
- CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* const bridgeBinary)
- {
- carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
-
- #ifndef BUILD_BRIDGE
- if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
- {
- init.engine->setLastError("Bridge not possible, bridge-binary not found");
- return nullptr;
- }
-
- BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, btype, ptype));
-
- if (! plugin->init(init.filename, init.name, init.label, bridgeBinary))
- {
- init.engine->registerEnginePlugin(init.id, nullptr);
- delete plugin;
- return nullptr;
- }
-
- plugin->reload();
-
- if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
- {
- init.engine->setLastError("Carla's rack mode can only work with Stereo Bridged plugins, sorry!");
- delete plugin;
- return nullptr;
- }
-
- return plugin;
- #else
- init.engine->setLastError("Plugin bridge support not available");
- return nullptr;
-
- // unused
- (void)bridgeBinary;
- #endif
- }
-
- #ifndef BUILD_BRIDGE
- // -------------------------------------------------------------------------------------------------------------------
- // Bridge Helper
-
- #define bridgePlugin ((BridgePlugin*)plugin)
-
- extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
- const int argc, const lo_arg* const* const argv, const char* const types);
-
- int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
- const int argc, const lo_arg* const* const argv, const char* const types)
- {
- CARLA_SAFE_ASSERT(plugin != nullptr && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0);
- return bridgePlugin->setOscPluginBridgeInfo(type, argc, argv, types);
- }
-
- #undef bridgePlugin
-
- #endif
-
- CARLA_BACKEND_END_NAMESPACE
-
- // -------------------------------------------------------------------------------------------------------------------
|