|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916 |
- /*
- * Carla Juce Plugin
- * Copyright (C) 2013-2022 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"
-
- #ifdef USING_JUCE
-
- #include "CarlaBackendUtils.hpp"
- #include "CarlaMathUtils.hpp"
- #include "CarlaProcessUtils.hpp"
- #include "CarlaScopeUtils.hpp"
- #include "CarlaVst2Utils.hpp"
- #include "CarlaVst3Utils.hpp"
-
- #define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
-
- #include "carla_juce/carla_juce.h"
-
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wdouble-promotion"
- #pragma GCC diagnostic ignored "-Wduplicated-branches"
- #pragma GCC diagnostic ignored "-Weffc++"
- #pragma GCC diagnostic ignored "-Wfloat-equal"
- #include "juce_audio_processors/juce_audio_processors.h"
- #include "juce_gui_basics/juce_gui_basics.h"
- #pragma GCC diagnostic pop
-
- #include "JucePluginWindow.hpp"
-
- CARLA_BACKEND_START_NAMESPACE
-
- // -------------------------------------------------------------------------------------------------------------------
- // Fallback data
-
- static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
-
- // -------------------------------------------------------------------------------------------------------------------
- // find all available plugin audio ports
-
- static void findMaxTotalChannels(juce::AudioProcessor* const filter,
- const bool isAU, uint32_t& maxTotalIns, uint32_t& maxTotalOuts)
- {
- filter->enableAllBuses();
-
- if (isAU)
- {
- maxTotalIns = static_cast<uint32_t>(juce::jmax(0, filter->getTotalNumInputChannels()));
- maxTotalOuts = static_cast<uint32_t>(juce::jmax(0, filter->getTotalNumOutputChannels()));
- return;
- }
-
- const int numInputBuses = filter->getBusCount(true);
- const int numOutputBuses = filter->getBusCount(false);
-
- if (numInputBuses > 1 || numOutputBuses > 1)
- {
- maxTotalIns = maxTotalOuts = 0;
-
- for (int i = 0; i < numInputBuses; ++i)
- maxTotalIns += static_cast<uint32_t>(juce::jmax(0, filter->getChannelCountOfBus(true, i)));
-
- for (int i = 0; i < numOutputBuses; ++i)
- maxTotalOuts += static_cast<uint32_t>(juce::jmax(0, filter->getChannelCountOfBus(false, i)));
- }
- else
- {
- maxTotalIns = numInputBuses > 0
- ? static_cast<uint32_t>(juce::jmax(0, filter->getBus(true, 0)->getMaxSupportedChannels(64)))
- : 0;
-
- maxTotalOuts = numOutputBuses > 0
- ? static_cast<uint32_t>(juce::jmax(0, filter->getBus(false, 0)->getMaxSupportedChannels(64)))
- : 0;
- }
- }
-
- // -------------------------------------------------------------------------------------------------------------------
- // ExtensionsVisitor for various formats
-
- static void* getPlatformSpecificDataForAll(const std::unique_ptr<juce::AudioPluginInstance>& instance)
- {
- struct ExtensionsVisitorForVST2 : public juce::ExtensionsVisitor
- {
- void* ptr = nullptr;
- void visitVST3Client (const VST3Client& c) override { ptr = c.getIComponentPtr(); }
- void visitVSTClient (const VSTClient& c) override { ptr = c.getAEffectPtr(); }
- void visitAudioUnitClient(const AudioUnitClient& c) override { ptr = c.getAudioUnitHandle(); }
- } visitor;
- instance->getExtensions(visitor);
- return visitor.ptr;
- }
-
- static AEffect* getPlatformSpecificDataForVST2(const std::unique_ptr<juce::AudioPluginInstance>& instance)
- {
- struct ExtensionsVisitorForVST2 : public juce::ExtensionsVisitor
- {
- AEffect* aeffect = nullptr;
- void visitVSTClient(const VSTClient& c) override { aeffect = c.getAEffectPtr(); }
- } visitor;
- instance->getExtensions(visitor);
- return visitor.aeffect;
- }
-
- static v3_component** getPlatformSpecificDataForVST3(const std::unique_ptr<juce::AudioPluginInstance>& instance)
- {
- struct ExtensionsVisitorForVST3 : public juce::ExtensionsVisitor
- {
- v3_component** component = nullptr;
- void visitVST3Client(const VST3Client& c) override { component = (v3_component**)c.getIComponentPtr(); }
- } visitor;
- instance->getExtensions(visitor);
- return visitor.component;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- class CarlaPluginJuce : public CarlaPlugin,
- private juce::AudioPlayHead,
- private juce::AudioProcessorListener
- {
- public:
- CarlaPluginJuce(CarlaEngine* const engine, const uint id)
- : CarlaPlugin(engine, id),
- fDesc(),
- fFormatManager(),
- fInstance(),
- fAudioBuffer(),
- fMidiBuffer(),
- fPosInfo(),
- fChunk(),
- fFormatName(),
- fWindow(),
- fNeedsUpdate(false)
- {
- carla_debug("CarlaPluginJuce::CarlaPluginJuce(%p, %i)", engine, id);
-
- fMidiBuffer.ensureSize(2048);
- fMidiBuffer.clear();
- }
-
- ~CarlaPluginJuce() override
- {
- carla_debug("CarlaPluginJuce::~CarlaPluginJuce()");
-
- // close UI
- if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
- showCustomUI(false);
-
- pData->singleMutex.lock();
- pData->masterMutex.lock();
-
- if (pData->client != nullptr && pData->client->isActive())
- pData->client->deactivate(true);
-
- if (pData->active)
- {
- deactivate();
- pData->active = false;
- }
-
- fInstance = nullptr;
- clearBuffers();
- }
-
- // -------------------------------------------------------------------
- // Information (base)
-
- PluginType getType() const noexcept override
- {
- return getPluginTypeFromString(fDesc.pluginFormatName.toRawUTF8());
- }
-
- PluginCategory getCategory() const noexcept override
- {
- if (fDesc.isInstrument)
- return PLUGIN_CATEGORY_SYNTH;
-
- return getPluginCategoryFromName(fDesc.category.isNotEmpty()
- ? fDesc.category.toRawUTF8()
- : fDesc.name.toRawUTF8());
- }
-
- int64_t getUniqueId() const noexcept override
- {
- return fDesc.uniqueId;
- }
-
- uint32_t getLatencyInFrames() const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0);
-
- const int latency = fInstance->getLatencySamples();
- CARLA_SAFE_ASSERT_RETURN(latency >= 0, 0);
-
- return static_cast<uint32_t>(latency);
- }
-
- // -------------------------------------------------------------------
- // Information (count)
-
- // nothing
-
- // -------------------------------------------------------------------
- // 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(fInstance != nullptr, 0);
- CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
-
- *dataPtr = nullptr;
-
- try {
- fChunk.reset();
- fInstance->getStateInformation(fChunk);
- } CARLA_SAFE_EXCEPTION_RETURN("CarlaPluginJuce::getChunkData", 0);
-
- if (const std::size_t size = fChunk.getSize())
- {
- *dataPtr = fChunk.getData();
- return size;
- }
-
- return 0;
- }
-
- // -------------------------------------------------------------------
- // Information (per-plugin data)
-
- uint getOptionsAvailable() const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0x0);
-
- uint options = 0x0;
-
- options |= PLUGIN_OPTION_USE_CHUNKS;
-
- if (fInstance->getNumPrograms() > 1)
- options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
-
- if (fInstance->acceptsMidi())
- {
- options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
- options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
- options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
- options |= PLUGIN_OPTION_SEND_PITCHBEND;
- options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
- options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
- options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
- }
-
- return options;
- }
-
- float getParameterValue(const uint32_t parameterId) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0.0f);
-
- juce::AudioProcessorParameter* const parameter = fInstance->getParameters()[static_cast<int>(parameterId)];
- CARLA_SAFE_ASSERT_RETURN(parameter != nullptr, 0.0f);
-
- return parameter->getValue();
- }
-
- bool getLabel(char* const strBuf) const noexcept override
- {
- if (fDesc.pluginFormatName == "AU" || fDesc.pluginFormatName == "AudioUnit")
- std::strncpy(strBuf, fDesc.fileOrIdentifier.toRawUTF8(), STR_MAX);
- else
- std::strncpy(strBuf, fDesc.name.toRawUTF8(), STR_MAX);
-
- return true;
- }
-
- bool getMaker(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fDesc.manufacturerName.toRawUTF8(), STR_MAX);
- return true;
- }
-
- bool getCopyright(char* const strBuf) const noexcept override
- {
- return getMaker(strBuf);
- }
-
- bool getRealName(char* const strBuf) const noexcept override
- {
- std::strncpy(strBuf, fDesc.descriptiveName.toRawUTF8(), 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);
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, false);
-
- juce::AudioProcessorParameter* const parameter = fInstance->getParameters()[static_cast<int>(parameterId)];
- CARLA_SAFE_ASSERT_RETURN(parameter != nullptr, false);
-
- std::strncpy(strBuf, parameter->getName(STR_MAX).toRawUTF8(), 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(fInstance != nullptr, false);
-
- juce::AudioProcessorParameter* const parameter = fInstance->getParameters()[static_cast<int>(parameterId)];
- CARLA_SAFE_ASSERT_RETURN(parameter != nullptr, false);
-
- std::strncpy(strBuf, parameter->getCurrentValueAsText().toRawUTF8(), 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);
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, false);
-
- juce::AudioProcessorParameter* const parameter = fInstance->getParameters()[static_cast<int>(parameterId)];
- CARLA_SAFE_ASSERT_RETURN(parameter != nullptr, false);
-
- std::strncpy(strBuf, parameter->getLabel().toRawUTF8(), STR_MAX);
- return true;
- }
-
- bool getParameterGroupName(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, false);
-
- if (fDesc.pluginFormatName != "VST" && fDesc.pluginFormatName != "VST2")
- return false;
-
- AEffect* const effect = getPlatformSpecificDataForVST2(fInstance);
-
- if (effect == nullptr)
- return false;
-
- VstParameterProperties prop;
- carla_zeroStruct(prop);
-
- if (effect->dispatcher(effect, effGetParameterProperties, static_cast<int32_t>(parameterId), 0, &prop, 0.0f) == 1
- && prop.category != 0 && prop.categoryLabel[0] != '\0')
- {
- std::snprintf(strBuf, STR_MAX, "%d:%s", prop.category, prop.categoryLabel);
- return true;
- }
-
- return false;
- }
-
- // -------------------------------------------------------------------
- // Set data (state)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Set data (internal stuff)
-
- void setName(const char* const newName) override
- {
- CarlaPlugin::setName(newName);
-
- if (fWindow == nullptr || pData->uiTitle.isNotEmpty())
- return;
-
- juce::String uiName(pData->name);
- uiName += " (GUI)";
- fWindow->setName(uiName);
- }
-
- // -------------------------------------------------------------------
- // 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(fInstance != nullptr,);
-
- juce::AudioProcessorParameter* const parameter = fInstance->getParameters()[static_cast<int>(parameterId)];
- CARLA_SAFE_ASSERT_RETURN(parameter != nullptr,);
-
- const float fixedValue = pData->param.getFixedValue(parameterId, value);
-
- try {
- parameter->setValue(value);
- } CARLA_SAFE_EXCEPTION("setValue");
-
- 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,);
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
-
- juce::AudioProcessorParameter* const parameter = fInstance->getParameters()[static_cast<int>(parameterId)];
- CARLA_SAFE_ASSERT_RETURN(parameter != nullptr,);
-
- const float fixedValue = pData->param.getFixedValue(parameterId, value);
-
- try {
- parameter->setValue(value);
- } CARLA_SAFE_EXCEPTION("setValue(RT)");
-
- CarlaPlugin::setParameterValueRT(parameterId, fixedValue, frameOffset, sendCallbackLater);
- }
-
- 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(fInstance != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
-
- if (isJuceSaveFormat(data, dataSize))
- {
- const ScopedSingleProcessLocker spl(this, true);
- fInstance->setStateInformation(data, static_cast<int>(dataSize));
- }
- else
- {
- uint8_t* const dataCompat = (uint8_t*)std::malloc(dataSize + 160);
- CARLA_SAFE_ASSERT_RETURN(dataCompat != nullptr,);
-
- carla_stdout("NOTE: Loading plugin state in Carla JUCE/VST2 compatibility mode");
- std::memset(dataCompat, 0, 160);
- std::memcpy(dataCompat+160, data, dataSize);
-
- int32_t* const set = (int32_t*)dataCompat;
-
- set[0] = (int32_t)juce::ByteOrder::littleEndianInt("CcnK");
- set[2] = (int32_t)juce::ByteOrder::littleEndianInt("FBCh");
- set[3] = fxbSwap(1);
- set[39] = fxbSwap(static_cast<int32_t>(dataSize));
-
- {
- const ScopedSingleProcessLocker spl(this, true);
- fInstance->setStateInformation(dataCompat, static_cast<int>(dataSize+160));
- }
-
- std::free(dataCompat);
- }
-
- pData->updateParameterValues(this, true, true, false);
- }
-
- void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
-
- if (index >= 0)
- {
- const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
-
- try {
- fInstance->setCurrentProgram(index);
- } CARLA_SAFE_EXCEPTION("setCurrentProgram");
- }
-
- CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
- }
-
- void setProgramRT(const uint32_t index, const bool sendCallbackLater) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
-
- try {
- fInstance->setCurrentProgram(static_cast<int32_t>(index));
- } CARLA_SAFE_EXCEPTION("setCurrentProgram");
-
- CarlaPlugin::setProgramRT(index, sendCallbackLater);
- }
-
- // -------------------------------------------------------------------
- // Set ui stuff
-
- void setCustomUITitle(const char* const title) noexcept override
- {
- if (fWindow != nullptr)
- {
- try {
- fWindow->setName(title);
- } CARLA_SAFE_EXCEPTION("set custom ui title");
- }
-
- CarlaPlugin::setCustomUITitle(title);
- }
-
- void showCustomUI(const bool yesNo) override
- {
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
- carla_debug("CarlaPluginJuce::showCustomUI(%s)", bool2str(yesNo));
-
- if (yesNo)
- {
- if (juce::AudioProcessorEditor* const editor = fInstance->createEditorIfNeeded())
- {
- const EngineOptions& opts(pData->engine->getOptions());
- #ifndef CARLA_OS_MAC
- editor->setScaleFactor(opts.uiScale);
- #endif
-
- if (fWindow == nullptr)
- {
- juce::String uiName;
-
- if (pData->uiTitle.isNotEmpty())
- {
- uiName = pData->uiTitle.buffer();
- }
- else
- {
- uiName = pData->name;
- uiName += " (GUI)";
- }
-
- AEffect* const vst2effect = fDesc.pluginFormatName == "VST" || fDesc.pluginFormatName == "VST2"
- ? getPlatformSpecificDataForVST2(fInstance)
- : nullptr;
-
- /* TODO update to juce7 APIs
- v3_plugin_view** const vst3view = fDesc.pluginFormatName == "VST3"
- ? (v3_plugin_view**)editor->getPlatformSpecificData()
- : nullptr;
- */
- v3_plugin_view** const vst3view = nullptr;
-
- fWindow = new JucePluginWindow(opts.frontendWinId, opts.pluginsAreStandalone,
- vst2effect, vst3view);
- fWindow->setName(uiName);
- }
-
- fWindow->show(editor);
- fWindow->toFront(true);
- }
- }
- else
- {
- if (juce::AudioProcessorEditor* const editor = fInstance->getActiveEditor())
- delete editor;
-
- fWindow = nullptr;
- }
- }
-
- void uiIdle() override
- {
- if (fWindow != nullptr)
- {
- if (fWindow->wasClosedByUser())
- {
- showCustomUI(false);
- pData->engine->callback(true, true,
- ENGINE_CALLBACK_UI_STATE_CHANGED,
- pData->id,
- 0,
- 0, 0, 0.0f, nullptr);
- }
- }
-
- CarlaPlugin::uiIdle();
- }
-
- // -------------------------------------------------------------------
- // Plugin state
-
- void reload() override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
- carla_debug("CarlaPluginJuce::reload() - start");
-
- const EngineProcessMode processMode(pData->engine->getProccessMode());
-
- // Safely disable plugin for reload
- const ScopedDisabler sd(this);
-
- if (pData->active)
- deactivate();
-
- clearBuffers();
-
- uint32_t aIns, aOuts, mIns, mOuts, params;
- mIns = mOuts = 0;
-
- bool needsCtrlIn, needsCtrlOut;
- needsCtrlIn = needsCtrlOut = false;
-
- const bool isAU = fDesc.pluginFormatName == "AU" || fDesc.pluginFormatName == "AudioUnit";
- const bool isVST2 = fDesc.pluginFormatName == "VST" || fDesc.pluginFormatName == "VST2";
- const bool isVST3 = fDesc.pluginFormatName == "VST3";
- findMaxTotalChannels(fInstance.get(), isAU, aIns, aOuts);
- fInstance->refreshParameterList();
-
- const juce::Array<juce::AudioProcessorParameter*>& parameters(fInstance->getParameters());
- params = static_cast<uint32_t>(std::max(parameters.size(), 0));
-
- if (fInstance->acceptsMidi())
- {
- mIns = 1;
- needsCtrlIn = true;
- }
-
- if (fInstance->producesMidi())
- {
- mOuts = 1;
- needsCtrlOut = true;
- }
-
- if (aIns > 0)
- {
- pData->audioIn.createNew(aIns);
- }
-
- if (aOuts > 0)
- {
- pData->audioOut.createNew(aOuts);
- needsCtrlIn = true;
- }
-
- if (params > 0)
- {
- pData->param.createNew(params, false);
- needsCtrlIn = true;
- }
-
- const uint portNameSize(pData->engine->getMaxPortNameSize());
- CarlaString portName;
-
- // Audio Ins
- for (uint32_t j=0; j < aIns; ++j)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- if (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 < aOuts; ++j)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- if (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;
- }
-
- for (uint32_t j=0; j < params; ++j)
- {
- const int32_t ij = static_cast<int>(j);
- juce::AudioProcessorParameter* const parameter = parameters[ij];
- CARLA_SAFE_ASSERT_CONTINUE(parameter != nullptr);
-
- pData->param.data[j].type = PARAMETER_INPUT;
- pData->param.data[j].index = static_cast<int32_t>(j);
- pData->param.data[j].rindex = static_cast<int32_t>(j);
-
- pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
- pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
-
- if (parameter->isAutomatable())
- pData->param.data[j].hints |= PARAMETER_IS_AUTOMATABLE;
-
- const float min = 0.0f;
- const float max = 1.0f;
- float def, step, stepSmall, stepLarge;
- bool hasDetails = false;
-
- if (isVST2)
- {
- AEffect* const effect = getPlatformSpecificDataForVST2(fInstance);
-
- VstParameterProperties prop;
- carla_zeroStruct(prop);
-
- if (effect != nullptr && effect->dispatcher(effect, effGetParameterProperties, ij, 0, &prop, 0.0f) == 1)
- {
- hasDetails = true;
-
- /**/ if (prop.flags & kVstParameterIsSwitch)
- {
- step = max - min;
- stepSmall = step;
- stepLarge = step;
- pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
- }
- else if (prop.flags & kVstParameterUsesIntStep)
- {
- step = static_cast<float>(prop.stepInteger);
- stepSmall = static_cast<float>(prop.stepInteger)/10.0f;
- stepLarge = static_cast<float>(prop.largeStepInteger);
- }
- else if (prop.flags & kVstParameterUsesFloatStep)
- {
- step = prop.stepFloat;
- stepSmall = prop.smallStepFloat;
- stepLarge = prop.largeStepFloat;
- }
- else
- {
- const float range = max - min;
- step = range/100.0f;
- stepSmall = range/1000.0f;
- stepLarge = range/10.0f;
- }
-
- if ((prop.flags & (kVstParameterIsSwitch|kVstParameterUsesIntStep)) == 0x0)
- pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
- if (prop.flags & kVstParameterCanRamp)
- pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
- }
- }
- else if (isVST3)
- {
- v3_component** const component = getPlatformSpecificDataForVST3(fInstance);
-
- /* FIXME query edit controller the long way too */
- v3_edit_controller** controller = nullptr;
- v3_cpp_obj_query_interface(component, v3_edit_controller_iid, &controller);
-
- if (controller != nullptr)
- {
- v3_param_info info;
- if (v3_cpp_obj(controller)->get_parameter_info(controller, static_cast<int32_t>(j), &info) == V3_OK)
- {
- hasDetails = true;
-
- if (info.step_count == 1)
- {
- step = stepSmall = stepLarge = 1;
- pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
- }
- else if (info.step_count != 0)
- {
- step = 1.0f / static_cast<float>(info.step_count);
- stepSmall = step/10.0f;
- stepLarge = std::min(1.0f, step*10.0f);
- }
- else
- {
- const float range = max - min;
- step = range/100.0f;
- stepSmall = range/1000.0f;
- stepLarge = range/10.0f;
- }
-
- if (info.flags & V3_PARAM_READ_ONLY)
- pData->param.data[j].type = PARAMETER_OUTPUT;
- // TODO V3_PARAM_IS_LIST
- if (info.flags & (V3_PARAM_IS_HIDDEN|V3_PARAM_PROGRAM_CHANGE))
- pData->param.data[j].hints &= ~PARAMETER_IS_ENABLED;
-
- if ((info.flags & (V3_PARAM_IS_LIST|V3_PARAM_IS_HIDDEN|V3_PARAM_PROGRAM_CHANGE)) == 0x0)
- if (info.flags & V3_PARAM_CAN_AUTOMATE)
- pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
- }
-
- v3_cpp_obj_unref(controller);
- }
- }
-
- if (! hasDetails)
- {
- if (parameter->isBoolean())
- {
- step = max - min;
- stepSmall = step;
- stepLarge = step;
- pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
- }
- else
- {
- const float range = max - min;
- step = range/100.0f;
- stepSmall = range/1000.0f;
- stepLarge = range/10.0f;
-
- if (! parameter->isMetaParameter())
- pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
- }
- }
-
- def = parameter->getDefaultValue();
-
- if (def < min)
- def = min;
- else if (def > max)
- def = max;
-
- pData->param.ranges[j].min = min;
- pData->param.ranges[j].max = max;
- pData->param.ranges[j].def = def;
- pData->param.ranges[j].step = step;
- pData->param.ranges[j].stepSmall = stepSmall;
- pData->param.ranges[j].stepLarge = stepLarge;
- }
-
- if (needsCtrlIn)
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- portName += "events-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 += "events-out";
- portName.truncate(portNameSize);
-
- pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
- }
-
- // plugin hints
- pData->hints = 0x0;
- pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
-
- if (fDesc.isInstrument)
- pData->hints |= PLUGIN_IS_SYNTH;
-
- if (fInstance->hasEditor())
- {
- pData->hints |= PLUGIN_HAS_CUSTOM_UI;
- pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
- }
-
- if (aOuts > 0 && (aIns == aOuts || aIns == 1))
- pData->hints |= PLUGIN_CAN_DRYWET;
-
- if (aOuts > 0)
- pData->hints |= PLUGIN_CAN_VOLUME;
-
- if (aOuts >= 2 && aOuts % 2 == 0)
- pData->hints |= PLUGIN_CAN_BALANCE;
-
- // extra plugin hints
- pData->extraHints = 0x0;
-
- if (mIns > 0)
- pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
-
- if (mOuts > 0)
- pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
-
- fInstance->setRateAndBufferSizeDetails(pData->engine->getSampleRate(),
- static_cast<int>(pData->engine->getBufferSize()));
-
- bufferSizeChanged(pData->engine->getBufferSize());
- reloadPrograms(true);
-
- if (pData->active)
- activate();
-
- carla_debug("CarlaPluginJuce::reload() - end");
- }
-
- void reloadPrograms(const bool doInit) override
- {
- carla_debug("CarlaPluginJuce::reloadPrograms(%s)", bool2str(doInit));
- const uint32_t oldCount = pData->prog.count;
- const int32_t current = pData->prog.current;
-
- // Delete old programs
- pData->prog.clear();
-
- // Query new programs
- const uint32_t newCount = (fInstance->getNumPrograms() > 0)
- ? static_cast<uint32_t>(fInstance->getNumPrograms())
- : 0;
-
- if (newCount > 0)
- {
- pData->prog.createNew(newCount);
-
- // Update names
- for (uint32_t i=0; i < newCount; ++i)
- pData->prog.names[i] = carla_strdup(fInstance->getProgramName(static_cast<int>(i)).toRawUTF8());
- }
-
- if (doInit)
- {
- if (newCount > 0)
- setProgram(0, false, false, false, true);
- }
- else
- {
- // Check if current program is invalid
- bool programChanged = false;
-
- if (newCount == oldCount+1)
- {
- // one program added, probably created by user
- pData->prog.current = static_cast<int32_t>(oldCount);
- programChanged = true;
- }
- else if (current < 0 && newCount > 0)
- {
- // programs exist now, but not before
- pData->prog.current = 0;
- programChanged = true;
- }
- else if (current >= 0 && newCount == 0)
- {
- // programs existed before, but not anymore
- pData->prog.current = -1;
- programChanged = true;
- }
- else if (current >= static_cast<int32_t>(newCount))
- {
- // current program > count
- pData->prog.current = 0;
- programChanged = true;
- }
- else
- {
- // no change
- pData->prog.current = current;
- }
-
- if (programChanged)
- {
- setProgram(pData->prog.current, true, true, true, false);
- }
- else
- {
- // Program was changed during update, re-set it
- if (pData->prog.current >= 0)
- fInstance->setCurrentProgram(pData->prog.current);
- }
-
- pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
- }
- }
-
- // -------------------------------------------------------------------
- // Plugin processing
-
- void activate() noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
-
- try {
- fInstance->prepareToPlay(pData->engine->getSampleRate(), static_cast<int>(pData->engine->getBufferSize()));
- } catch(...) {}
- }
-
- void deactivate() noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
-
- try {
- fInstance->releaseResources();
- } catch(...) {}
- }
-
- void process(const float* const* const audioIn,
- float** const audioOut,
- const float* const* const cvIn,
- float**,
- const uint32_t frames) override
- {
- // --------------------------------------------------------------------------------------------------------
- // Check if active
-
- if (! pData->active)
- {
- // disable any output sound
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- carla_zeroFloats(audioOut[i], frames);
- return;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Check if needs reset
-
- if (pData->needsReset)
- {
- fInstance->reset();
- pData->needsReset = false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Event Input
-
- fMidiBuffer.clear();
-
- 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 midiEvent[3];
- midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
- midiEvent[1] = note.note;
- midiEvent[2] = note.velo;
-
- fMidiBuffer.addEvent(midiEvent, 3, 0);
- }
-
- 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)
- pData->event.cvSourcePorts->initPortBuffers(cvIn, frames, false, pData->event.portIn);
- #endif
-
- for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
- {
- EngineEvent& event(pData->event.portIn->getEvent(i));
-
- if (event.time >= frames)
- continue;
-
- 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
- uint32_t k;
- for (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);
- }
-
- if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
- {
- uint8_t midiData[3];
- midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- midiData[1] = uint8_t(ctrlEvent.param);
- midiData[2] = uint8_t(ctrlEvent.normalizedValue*127.0f + 0.5f);
-
- fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
- }
-
- #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
- if (! ctrlEvent.handled)
- checkForMidiLearn(event);
- #endif
- break;
- } // case kEngineControlEventTypeParameter
-
- case kEngineControlEventTypeMidiBank:
- if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
- {
- uint8_t midiData[3];
- midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- midiData[1] = MIDI_CONTROL_BANK_SELECT;
- midiData[2] = 0;
- fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
-
- midiData[1] = MIDI_CONTROL_BANK_SELECT__LSB;
- midiData[2] = uint8_t(ctrlEvent.normalizedValue*127.0f + 0.5f);
- fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
- }
- break;
-
- case kEngineControlEventTypeMidiProgram:
- if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
- {
- if (ctrlEvent.param < pData->prog.count)
- {
- setProgramRT(ctrlEvent.param, true);
- }
- }
- else if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
- {
- uint8_t midiData[2];
- midiData[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- midiData[1] = uint8_t(ctrlEvent.normalizedValue*127.0f + 0.5f);
- fMidiBuffer.addEvent(midiData, 2, static_cast<int>(event.time));
- }
- break;
-
- case kEngineControlEventTypeAllSoundOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- uint8_t midiData[3];
- midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
- midiData[2] = 0;
-
- fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
- }
- 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
-
- uint8_t midiData[3];
- midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
- midiData[2] = 0;
-
- fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
- }
- break;
- } // switch (ctrlEvent.type)
- break;
- } // case kEngineEventTypeControl
-
- case kEngineEventTypeMidi: {
- const EngineMidiEvent& midiEvent(event.midi);
-
- 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;
-
- // put back channel in data
- uint8_t midiData2[midiEvent.size];
- midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
- std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
-
- fMidiBuffer.addEvent(midiData2, midiEvent.size, static_cast<int>(event.time));
-
- 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;
- } // switch (event.type)
- }
-
- pData->postRtEvents.trySplice();
-
- } // End of Event Input
-
- // --------------------------------------------------------------------------------------------------------
- // Set TimeInfo
-
- const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
-
- fPosInfo.setIsPlaying(timeInfo.playing);
-
- if (timeInfo.bbt.valid)
- {
- CARLA_SAFE_ASSERT_INT(timeInfo.bbt.bar > 0, timeInfo.bbt.bar);
- CARLA_SAFE_ASSERT_INT(timeInfo.bbt.beat > 0, timeInfo.bbt.beat);
-
- const double ppqBar = static_cast<double>(timeInfo.bbt.beatsPerBar) * (timeInfo.bbt.bar - 1);
- const double ppqBeat = static_cast<double>(timeInfo.bbt.beat - 1);
- const double ppqTick = timeInfo.bbt.tick / timeInfo.bbt.ticksPerBeat;
-
- fPosInfo.setBarCount(timeInfo.bbt.bar);
- fPosInfo.setBpm(timeInfo.bbt.beatsPerMinute);
-
- fPosInfo.setTimeSignature(TimeSignature{static_cast<int>(timeInfo.bbt.beatsPerBar+0.5f),
- static_cast<int>(timeInfo.bbt.beatType+0.5f)});
-
- fPosInfo.setTimeInSamples(static_cast<int64_t>(timeInfo.frame));
- fPosInfo.setTimeInSeconds(static_cast<double>(timeInfo.frame)/pData->engine->getSampleRate());
-
- fPosInfo.setPpqPosition(ppqBar + ppqBeat + ppqTick);
- fPosInfo.setPpqPositionOfLastBarStart(ppqBar);
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Process
-
- processSingle(audioIn, audioOut, frames);
-
- // --------------------------------------------------------------------------------------------------------
-
- #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
- return;
-
- // unused
- (void)cvIn;
- #endif
- }
-
- bool processSingle(const float* const* 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)
- carla_zeroFloats(outBuffer[i], frames);
- return false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Set audio in buffers
-
- for (uint32_t i=0; i < pData->audioIn.count; ++i)
- fAudioBuffer.copyFrom(static_cast<int>(i), 0, inBuffer[i], static_cast<int>(frames));
-
- // --------------------------------------------------------------------------------------------------------
- // Run plugin
-
- fInstance->processBlock(fAudioBuffer, fMidiBuffer);
-
- // --------------------------------------------------------------------------------------------------------
- // Set audio out buffers
-
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- carla_copyFloats(outBuffer[i], fAudioBuffer.getReadPointer(static_cast<int>(i)), 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, oldBufLeft[doBalance ? frames : 1];
-
- 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)
- {
- bufValue = inBuffer[c][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);
- carla_copyFloats(oldBufLeft, outBuffer[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
- 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
- if (doVolume)
- {
- for (uint32_t k=0; k < frames; ++k)
- outBuffer[i][k] *= pData->postProc.volume;
- }
- }
-
- } // End of Post-processing
- #endif
-
- // --------------------------------------------------------------------------------------------------------
- // Midi out
-
- if (! fMidiBuffer.isEmpty())
- {
- if (pData->event.portOut != nullptr)
- {
- for (juce::MidiBufferIterator it = fMidiBuffer.cbegin(), end = fMidiBuffer.cend(); it != end; ++it)
- {
- const juce::MidiMessageMetadata metadata(*it);
- CARLA_SAFE_ASSERT_BREAK(metadata.samplePosition >= 0);
- CARLA_SAFE_ASSERT_BREAK(metadata.samplePosition < static_cast<int>(frames));
- CARLA_SAFE_ASSERT_BREAK(metadata.numBytes > 0);
- CARLA_SAFE_ASSERT_CONTINUE(metadata.numBytes <= 0xff);
-
- if (! pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(metadata.samplePosition),
- static_cast<uint8_t>(metadata.numBytes),
- metadata.data))
- break;
- }
- }
-
- fMidiBuffer.clear();
- }
-
- // --------------------------------------------------------------------------------------------------------
-
- pData->singleMutex.unlock();
- return true;
- }
-
- void bufferSizeChanged(const uint32_t newBufferSize) override
- {
- CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
- carla_debug("CarlaPluginJuce::bufferSizeChanged(%i)", newBufferSize);
-
- fAudioBuffer.setSize(static_cast<int>(std::max<uint32_t>(pData->audioIn.count, pData->audioOut.count)), static_cast<int>(newBufferSize));
-
- if (pData->active)
- {
- deactivate();
- activate();
- }
- }
-
- void sampleRateChanged(const double newSampleRate) override
- {
- CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
- carla_debug("CarlaPluginJuce::sampleRateChanged(%g)", newSampleRate);
-
- if (pData->active)
- {
- deactivate();
- activate();
- }
- }
-
- // -------------------------------------------------------------------
- // Misc
-
- void idle() override
- {
- if (fNeedsUpdate)
- {
- fNeedsUpdate = false;
- pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0f, nullptr);
- }
-
- CarlaPlugin::idle();
- }
-
- // -------------------------------------------------------------------
- // Plugin buffers
-
- // nothing
-
- // -------------------------------------------------------------------
- // Post-poned UI Stuff
-
- // nothing
-
- // -------------------------------------------------------------------
-
- void* getNativeHandle() const noexcept override
- {
- return (fInstance != nullptr) ? getPlatformSpecificDataForAll(fInstance) : nullptr;
- }
-
- // -------------------------------------------------------------------
-
- protected:
- void audioProcessorParameterChanged(juce::AudioProcessor*, int index, float value) override
- {
- CARLA_SAFE_ASSERT_RETURN(index >= 0,);
-
- const uint32_t uindex(static_cast<uint32_t>(index));
- const float fixedValue(pData->param.getFixedValue(uindex, value));
-
- CarlaPlugin::setParameterValue(static_cast<uint32_t>(index), fixedValue, false, true, true);
- }
-
- void audioProcessorChanged(juce::AudioProcessor*, const ChangeDetails& details) override
- {
- if (details.parameterInfoChanged || details.programChanged)
- fNeedsUpdate = true;
- }
-
- void audioProcessorParameterChangeGestureBegin(juce::AudioProcessor*, int index) override
- {
- CARLA_SAFE_ASSERT_RETURN(index >= 0,);
-
- pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), true);
- }
-
- void audioProcessorParameterChangeGestureEnd(juce::AudioProcessor*, int index) override
- {
- CARLA_SAFE_ASSERT_RETURN(index >= 0,);
-
- pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), false);
- }
-
- juce::Optional<juce::AudioPlayHead::PositionInfo> getPosition() const override
- {
- return fPosInfo;
- }
-
- // -------------------------------------------------------------------
-
- public:
- bool init(const CarlaPluginPtr plugin,
- const char* const filename, const char* const name, const char* const label,
- const int64_t uniqueId, const uint options, const char* const format)
- {
- 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 (format == nullptr || format[0] == '\0')
- {
- pData->engine->setLastError("null format");
- return false;
- }
-
- // AU requires label
- if (std::strcmp(format, "AU") == 0)
- {
- if (label == nullptr || label[0] == '\0')
- {
- pData->engine->setLastError("null label");
- return false;
- }
- }
-
- juce::String fileOrIdentifier;
-
- if (std::strcmp(format, "AU") == 0)
- {
- fileOrIdentifier = label;
- }
- else
- {
- // VST2 and VST3 require filename
- if (filename == nullptr || filename[0] == '\0')
- {
- pData->engine->setLastError("null filename");
- return false;
- }
-
- juce::String jfilename(filename);
-
- #ifdef CARLA_OS_WIN
- // Fix for wine usage
- if (juce::File("Z:\\usr\\").isDirectory() && filename[0] == '/')
- {
- jfilename.replace("/", "\\");
- jfilename = "Z:" + jfilename;
- }
- #endif
-
- fileOrIdentifier = jfilename;
-
- if (label != nullptr && label[0] != '\0')
- fDesc.name = label;
- }
-
- /**/ if (std::strcmp(format, "AU") == 0)
- {
- #if JUCE_PLUGINHOST_AU
- fFormatManager.addFormat(new juce::AudioUnitPluginFormat());
- #endif
- }
- else if (std::strcmp(format, "VST2") == 0)
- {
- #if JUCE_PLUGINHOST_VST
- fFormatManager.addFormat(new juce::VSTPluginFormat());
- #endif
- }
- else if (std::strcmp(format, "VST3") == 0)
- {
- #if JUCE_PLUGINHOST_VST3
- fFormatManager.addFormat(new juce::VST3PluginFormat());
- #endif
- }
- else
- {
- fFormatManager.addDefaultFormats();
- }
-
- {
- juce::OwnedArray<juce::PluginDescription> pluginDescriptions;
- juce::KnownPluginList plist;
-
- {
- #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
- const ScopedAbortCatcher sac;
- #endif
-
- for (int i = 0; i < fFormatManager.getNumFormats(); ++i)
- {
- juce::AudioPluginFormat* const apformat = fFormatManager.getFormat(i);
- CARLA_SAFE_ASSERT_CONTINUE(apformat != nullptr);
-
- carla_debug("Trying to load '%s' plugin with format '%s'", fileOrIdentifier.toRawUTF8(), apformat->getName().toRawUTF8());
-
- try {
- plist.scanAndAddFile(fileOrIdentifier, true, pluginDescriptions, *apformat);
- } CARLA_SAFE_EXCEPTION_CONTINUE("scanAndAddFile")
-
- #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
- if (sac.wasTriggered())
- {
- carla_stderr("WARNING: Caught exception while scanning file, will not load this plugin");
- pluginDescriptions.clearQuick(false);
- break;
- }
- #endif
- }
- }
-
- if (pluginDescriptions.size() == 0)
- {
- pData->engine->setLastError("Failed to get plugin description");
- return false;
- }
-
- fDesc = *pluginDescriptions[0];
- }
-
- if (uniqueId != 0)
- fDesc.uniqueId = static_cast<int>(uniqueId);
-
- juce::String error;
-
- {
- #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
- const ScopedAbortCatcher sac;
- #endif
-
- try {
- fInstance = fFormatManager.createPluginInstance(fDesc,
- pData->engine->getSampleRate(),
- static_cast<int>(pData->engine->getBufferSize()),
- error);
- } CARLA_SAFE_EXCEPTION("createPluginInstance")
-
- #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
- if (sac.wasTriggered())
- {
- fInstance = nullptr;
- carla_stderr("WARNING: Caught exception while instantiating, will not load this plugin");
- }
- #endif
- }
-
- if (fInstance == nullptr)
- {
- pData->engine->setLastError(error.toRawUTF8());
- return false;
- }
-
- fInstance->fillInPluginDescription(fDesc);
- fInstance->setPlayHead(this);
- fInstance->addListener(this);
-
- fFormatName = format;
-
- // ---------------------------------------------------------------
- // get info
-
- if (name != nullptr && name[0] != '\0')
- pData->name = pData->engine->getUniquePluginName(name);
- else
- pData->name = pData->engine->getUniquePluginName(fInstance->getName().toRawUTF8());
-
- if (filename != nullptr && filename[0] != '\0')
- pData->filename = carla_strdup(filename);
-
- // ---------------------------------------------------------------
- // register client
-
- 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;
- pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
- pData->options |= PLUGIN_OPTION_USE_CHUNKS;
-
- if (fInstance->acceptsMidi())
- {
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
- pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
- pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
- pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
- pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
- pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
- pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
- if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
- pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
- }
-
- if (fInstance->getNumPrograms() > 1 && (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) == 0)
- if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
- pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
-
- return true;
- }
-
- private:
- juce::PluginDescription fDesc;
- juce::AudioPluginFormatManager fFormatManager;
- std::unique_ptr<juce::AudioPluginInstance> fInstance;
-
- juce::AudioSampleBuffer fAudioBuffer;
- juce::MidiBuffer fMidiBuffer;
- PositionInfo fPosInfo;
- juce::MemoryBlock fChunk;
- juce::String fFormatName;
-
- CarlaScopedPointer<JucePluginWindow> fWindow;
-
- bool fNeedsUpdate;
-
- bool isJuceSaveFormat(const void* const data, const std::size_t dataSize)
- {
- if (fFormatName != "VST2")
- return true;
- if (dataSize < 160)
- return false;
-
- const int32_t* const set = (const int32_t*)data;
-
- // chunkMagic
- if (! compareMagic(set[0], "CcnK"))
- return false;
-
- // version
- if (fxbSwap(set[3]) > 1)
- return false;
-
- // fxMagic, data contents depend on this value
- if (compareMagic(set[2], "FBCh") || compareMagic(set[2], "FJuc"))
- {
- const int32_t chunkSize = fxbSwap(set[39]);
- return static_cast<std::size_t>(chunkSize + 160) == dataSize;
- }
- if (compareMagic(set[2], "FxBk"))
- {
- const int32_t numPrograms = fxbSwap(set[6]);
- return numPrograms >= 1;
- }
-
- return false;
- }
-
- static bool compareMagic(int32_t magic, const char* name) noexcept
- {
- return magic == (int32_t)juce::ByteOrder::littleEndianInt (name)
- || magic == (int32_t)juce::ByteOrder::bigEndianInt (name);
- }
-
- static int32_t fxbSwap(const int32_t x) noexcept
- {
- return (int32_t)juce::ByteOrder::swapIfLittleEndian ((uint32_t) x);
- }
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginJuce)
- };
-
- CARLA_BACKEND_END_NAMESPACE
-
- #endif // USING_JUCE
-
- // -------------------------------------------------------------------------------------------------------------------
-
- CARLA_BACKEND_START_NAMESPACE
-
- CarlaPluginPtr CarlaPlugin::newJuce(const Initializer& init, const char* const format)
- {
- carla_debug("CarlaPlugin::newJuce({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)",
- init.engine, init.filename, init.name, init.label, init.uniqueId, format);
-
- #ifdef USING_JUCE
- std::shared_ptr<CarlaPluginJuce> plugin(new CarlaPluginJuce(init.engine, init.id));
-
- if (! plugin->init(plugin, init.filename, init.name, init.label, init.uniqueId, init.options, format))
- return nullptr;
-
- return plugin;
- #else
- init.engine->setLastError("Juce-based plugin not available");
- return nullptr;
-
- // unused
- (void)format;
- #endif
- }
-
- CARLA_BACKEND_END_NAMESPACE
-
- // -------------------------------------------------------------------------------------------------------------------
|