|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415 |
- /*
- * Carla VST Plugin
- * Copyright (C) 2011-2013 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 GPL.txt file
- */
-
- #include "CarlaPluginInternal.hpp"
-
- #ifdef WANT_VST
-
- #include "CarlaVstUtils.hpp"
-
- #ifdef Q_WS_X11
- # include <QtGui/QX11Info>
- #endif
-
- CARLA_BACKEND_START_NAMESPACE
-
- /*!
- * @defgroup PluginHints Plugin Hints
- * @{
- */
- const unsigned int PLUGIN_CAN_PROCESS_REPLACING = 0x1000; //!< VST Plugin cas use processReplacing()
- const unsigned int PLUGIN_HAS_COCKOS_EXTENSIONS = 0x2000; //!< VST Plugin has Cockos extensions
- const unsigned int PLUGIN_USES_OLD_VSTSDK = 0x4000; //!< VST Plugin uses an old VST SDK
- const unsigned int PLUGIN_WANTS_MIDI_INPUT = 0x8000; //!< VST Plugin wants MIDI input
- /**@}*/
-
- class VstPlugin : public CarlaPlugin
- {
- public:
- VstPlugin(CarlaEngine* const engine, const unsigned short id)
- : CarlaPlugin(engine, id)
- {
- carla_debug("VstPlugin::VstPlugin(%p, %i)", engine, id);
-
- #if 0
- effect = nullptr;
- events.numEvents = 0;
- events.reserved = 0;
-
- gui.type = GUI_NONE;
- gui.visible = false;
- gui.width = 0;
- gui.height = 0;
-
- isProcessing = false;
- needIdle = false;
-
- vstTimeOffset = 0;
-
- memset(midiEvents, 0, sizeof(VstMidiEvent)*MAX_MIDI_EVENTS*2);
-
- for (unsigned short i=0; i < MAX_MIDI_EVENTS*2; i++)
- events.data[i] = (VstEvent*)&midiEvents[i];
-
- // make plugin valid
- srand(id);
- unique1 = unique2 = rand();
- #endif
-
- kData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_VST_GUI);
- }
-
- ~VstPlugin()
- {
- carla_debug("VstPlugin::~VstPlugin()");
-
- #if 0
- // make plugin invalid
- unique2 += 1;
-
- if (effect)
- {
- // close UI
- if (m_hints & PLUGIN_HAS_GUI)
- {
- showGui(false);
-
- if (gui.type == GUI_EXTERNAL_OSC)
- {
- if (osc.thread)
- {
- // Wait a bit first, try safe quit, then force kill
- if (osc.thread->isRunning() && ! osc.thread->wait(x_engine->getOptions().oscUiTimeout))
- {
- carla_stderr("Failed to properly stop VST OSC GUI thread");
- osc.thread->terminate();
- }
-
- delete osc.thread;
- }
- }
- else
- effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0.0f);
- }
-
- if (m_activeBefore)
- effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
-
- effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
- }
- #endif
- }
-
- // -------------------------------------------------------------------
- // Information (base)
-
- PluginType type() const
- {
- return PLUGIN_VST;
- }
-
- #if 0
- PluginCategory category()
- {
- CARLA_ASSERT(effect);
-
- if (effect)
- {
- intptr_t category = effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f);
-
- switch (category)
- {
- case kPlugCategSynth:
- return PLUGIN_CATEGORY_SYNTH;
- case kPlugCategAnalysis:
- return PLUGIN_CATEGORY_UTILITY;
- case kPlugCategMastering:
- return PLUGIN_CATEGORY_DYNAMICS;
- case kPlugCategRoomFx:
- return PLUGIN_CATEGORY_DELAY;
- case kPlugCategRestoration:
- return PLUGIN_CATEGORY_UTILITY;
- case kPlugCategGenerator:
- return PLUGIN_CATEGORY_SYNTH;
- }
-
- if (effect->flags & effFlagsIsSynth)
- return PLUGIN_CATEGORY_SYNTH;
- }
-
- return getPluginCategoryFromName(m_name);
- }
-
- long uniqueId()
- {
- CARLA_ASSERT(effect);
-
- return effect ? effect->uniqueID : 0;
- }
-
- // -------------------------------------------------------------------
- // Information (current data)
-
- int32_t chunkData(void** const dataPtr)
- {
- CARLA_ASSERT(dataPtr);
- CARLA_ASSERT(effect);
-
- if (effect)
- return effect->dispatcher(effect, effGetChunk, 0 /* bank */, 0, dataPtr, 0.0f);
-
- return 0;
- }
-
- // -------------------------------------------------------------------
- // Information (per-plugin data)
-
- double getParameterValue(const uint32_t parameterId)
- {
- CARLA_ASSERT(effect);
- CARLA_ASSERT(parameterId < param.count);
-
- if (effect)
- return effect->getParameter(effect, parameterId);
-
- return 0.0;
- }
-
- void getLabel(char* const strBuf)
- {
- CARLA_ASSERT(effect);
-
- if (effect)
- effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f);
- else
- CarlaPlugin::getLabel(strBuf);
- }
-
- void getMaker(char* const strBuf)
- {
- CARLA_ASSERT(effect);
-
- if (effect)
- effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f);
- else
- CarlaPlugin::getMaker(strBuf);
- }
-
- void getCopyright(char* const strBuf)
- {
- CARLA_ASSERT(effect);
-
- if (effect)
- effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f);
- else
- CarlaPlugin::getCopyright(strBuf);
- }
-
- void getRealName(char* const strBuf)
- {
- CARLA_ASSERT(effect);
-
- if (effect)
- effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
- else
- CarlaPlugin::getRealName(strBuf);
- }
-
- void getParameterName(const uint32_t parameterId, char* const strBuf)
- {
- CARLA_ASSERT(effect);
- CARLA_ASSERT(parameterId < param.count);
-
- if (effect)
- effect->dispatcher(effect, effGetParamName, parameterId, 0, strBuf, 0.0f);
- else
- CarlaPlugin::getParameterName(parameterId, strBuf);
- }
-
- void getParameterText(const uint32_t parameterId, char* const strBuf)
- {
- CARLA_ASSERT(effect);
- CARLA_ASSERT(parameterId < param.count);
-
- if (effect)
- {
- effect->dispatcher(effect, effGetParamDisplay, parameterId, 0, strBuf, 0.0f);
-
- if (*strBuf == 0)
- sprintf(strBuf, "%f", getParameterValue(parameterId));
- }
- else
- CarlaPlugin::getParameterText(parameterId, strBuf);
- }
-
- void getParameterUnit(const uint32_t parameterId, char* const strBuf)
- {
- CARLA_ASSERT(effect);
- CARLA_ASSERT(parameterId < param.count);
-
- if (effect)
- effect->dispatcher(effect, effGetParamLabel, parameterId, 0, strBuf, 0.0f);
- else
- CarlaPlugin::getParameterUnit(parameterId, strBuf);
- }
-
- void getGuiInfo(GuiType* const type, bool* const resizable)
- {
- *type = gui.type;
- *resizable = false;
- }
-
- // -------------------------------------------------------------------
- // Set data (plugin-specific stuff)
-
- void setParameterValue(const uint32_t parameterId, double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
- {
- CARLA_ASSERT(parameterId < param.count);
-
- effect->setParameter(effect, parameterId, fixParameterValue(value, param.ranges[parameterId]));
-
- CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
- }
-
- void setChunkData(const char* const stringData)
- {
- CARLA_ASSERT(m_hints & PLUGIN_USES_CHUNKS);
- CARLA_ASSERT(stringData);
-
- static QByteArray chunk;
- chunk = QByteArray::fromBase64(stringData);
-
- if (x_engine->isOffline())
- {
- const CarlaEngine::ScopedLocker m(x_engine);
- effect->dispatcher(effect, effSetChunk, 0 /* bank */, chunk.size(), chunk.data(), 0.0f);
- }
- else
- {
- const CarlaPlugin::ScopedDisabler m(this);
- effect->dispatcher(effect, effSetChunk, 0 /* bank */, chunk.size(), chunk.data(), 0.0f);
- }
- }
-
- void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
- {
- CARLA_ASSERT(index >= -1 && index < (int32_t)prog.count);
-
- if (index < -1)
- index = -1;
- else if (index > (int32_t)prog.count)
- return;
-
- if (index >= 0)
- {
- if (x_engine->isOffline())
- {
- const CarlaEngine::ScopedLocker m(x_engine, block);
- effect->dispatcher(effect, effBeginSetProgram, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effSetProgram, 0, index, nullptr, 0.0f);
- effect->dispatcher(effect, effEndSetProgram, 0, 0, nullptr, 0.0f);
- }
- else
- {
- const ScopedDisabler m(this, block);
- effect->dispatcher(effect, effBeginSetProgram, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effSetProgram, 0, index, nullptr, 0.0f);
- effect->dispatcher(effect, effEndSetProgram, 0, 0, nullptr, 0.0f);
- }
- }
-
- CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, block);
- }
-
- // -------------------------------------------------------------------
- // Set gui stuff
-
- void setGuiContainer(GuiContainer* const container)
- {
- carla_debug("VstPlugin::setGuiContainer(%p)", container);
- CARLA_ASSERT(container);
-
- if (gui.type == GUI_EXTERNAL_OSC)
- return;
-
- int32_t value = 0;
- void* const ptr = (void*)container->winId();
- ERect* vstRect = nullptr;
-
- #ifdef Q_WS_X11
- value = (intptr_t)QX11Info::display();
- #endif
-
- // get UI size before opening UI, plugin may refuse this
- effect->dispatcher(effect, effEditGetRect, 0, 0, &vstRect, 0.0f);
-
- if (vstRect)
- {
- int width = vstRect->right - vstRect->left;
- int height = vstRect->bottom - vstRect->top;
-
- if (width > 0 || height > 0)
- {
- container->setFixedSize(width, height);
- #ifdef BUILD_BRIDGE
- x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 1.0, nullptr);
- #endif
- }
- }
-
- // open UI
- if (effect->dispatcher(effect, effEditOpen, 0, value, ptr, 0.0f) == 1)
- {
- // get UI size again, can't fail now
- vstRect = nullptr;
- effect->dispatcher(effect, effEditGetRect, 0, 0, &vstRect, 0.0f);
-
- if (vstRect)
- {
- int width = vstRect->right - vstRect->left;
- int height = vstRect->bottom - vstRect->top;
-
- if (width <= 0 || height <= 0)
- {
- carla_stderr2("VstPlugin::setGuiContainer(%p) - failed to get proper editor size", container);
- return;
- }
-
- gui.width = width;
- gui.height = height;
-
- container->setFixedSize(width, height);
- carla_debug("VstPlugin::setGuiContainer(%p) -> setFixedSize(%i, %i)", container, width, height);
- }
- else
- carla_stderr2("VstPlugin::setGuiContainer(%p) - failed to get plugin editor size", container);
- }
- else
- {
- // failed to open UI
- carla_stderr("VstPlugin::setGuiContainer(%p) - failed to open UI", container);
-
- m_hints &= ~PLUGIN_HAS_GUI;
- x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0, nullptr);
-
- effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0.0f);
- }
- }
-
- void showGui(const bool yesNo)
- {
- if (gui.type == GUI_EXTERNAL_OSC)
- {
- CARLA_ASSERT(osc.thread);
-
- if (! osc.thread)
- {
- carla_stderr2("VstPlugin::showGui(%s) - attempt to show gui, but it does not exist!", bool2str(yesNo));
- return;
- }
-
- if (yesNo)
- {
- osc.thread->start();
- }
- else
- {
- if (osc.data.target)
- {
- osc_send_hide(&osc.data);
- osc_send_quit(&osc.data);
- osc.data.free();
- }
-
- if (! osc.thread->wait(500))
- osc.thread->quit();
- }
- }
- else
- {
- if (yesNo && gui.width > 0 && gui.height > 0)
- x_engine->callback(CALLBACK_RESIZE_GUI, m_id, gui.width, gui.height, 0.0, nullptr);
- }
-
- gui.visible = yesNo;
- }
-
- void idleGui()
- {
- #ifdef VESTIGE_HEADER
- if (effect /*&& effect->ptr1*/)
- #else
- if (effect /*&& effect->resvd1*/)
- #endif
- {
- if (needIdle)
- effect->dispatcher(effect, effIdle, 0, 0, nullptr, 0.0f);
-
- if (gui.type != GUI_EXTERNAL_OSC)
- effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
- }
-
- CarlaPlugin::idleGui();
- }
-
- // -------------------------------------------------------------------
- // Plugin state
-
- void reload()
- {
- carla_debug("VstPlugin::reload() - start");
- CARLA_ASSERT(effect);
-
- const ProcessMode processMode(x_engine->getOptions().processMode);
-
- // Safely disable plugin for reload
- const ScopedDisabler m(this);
-
- // Remove client ports
- removeClientPorts();
-
- // Delete old data
- deleteBuffers();
-
- uint32_t aIns, aOuts, mIns, mOuts, params, j;
-
- aIns = effect->numInputs;
- aOuts = effect->numOutputs;
- params = effect->numParams;
-
- if (vstPluginCanDo(effect, "receiveVstEvents") || vstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) > 0 || (m_hints & PLUGIN_WANTS_MIDI_INPUT))
- mIns = 1;
- else
- mIns = 0;
-
- if (vstPluginCanDo(effect, "sendVstEvents") || vstPluginCanDo(effect, "sendVstMidiEvent"))
- mOuts = 1;
- else
- mOuts = 0;
-
- if (aIns > 0)
- {
- aIn.ports = new CarlaEngineAudioPort*[aIns];
- aIn.rindexes = new uint32_t[aIns];
- }
-
- if (aOuts > 0)
- {
- aOut.ports = new CarlaEngineAudioPort*[aOuts];
- aOut.rindexes = new uint32_t[aOuts];
- }
-
- if (params > 0)
- {
- param.data = new ParameterData[params];
- param.ranges = new ParameterRanges[params];
- }
-
- bool needsCtrlIn = (aOuts > 0 || params > 0);
-
- const int portNameSize = x_engine->maxPortNameSize();
- CarlaString portName;
-
- // Audio Ins
- for (j=0; j < aIns; j++)
- {
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = m_name;
- portName += ":";
- }
-
- char tmp[12] = { 0 };
- sprintf(tmp, "input_%02i", j+1);
- portName += tmp;
-
- aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
- aIn.rindexes[j] = j;
- }
-
- // Audio Outs
- for (j=0; j < aOuts; j++)
- {
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = m_name;
- portName += ":";
- }
-
- char tmp[12] = { 0 };
- sprintf(tmp, "output_%02i", j+1);
- portName += tmp;
-
- aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
- aOut.rindexes[j] = j;
- }
-
- for (j=0; j < params; j++)
- {
- param.data[j].type = PARAMETER_INPUT;
- param.data[j].index = j;
- param.data[j].rindex = j;
- param.data[j].hints = 0;
- param.data[j].midiChannel = 0;
- param.data[j].midiCC = -1;
-
- double min, max, def, step, stepSmall, stepLarge;
-
- VstParameterProperties prop;
- prop.flags = 0;
-
- if (effect->dispatcher(effect, effGetParameterProperties, j, 0, &prop, 0))
- {
- double range[2] = { 0.0, 1.0 };
-
- if ((m_hints & PLUGIN_HAS_COCKOS_EXTENSIONS) > 0 && effect->dispatcher(effect, effVendorSpecific, 0xdeadbef0, j, range, 0.0) >= 0xbeef)
- {
- min = range[0];
- max = range[1];
- }
- else if (prop.flags & kVstParameterUsesIntegerMinMax)
- {
- min = prop.minInteger;
- max = prop.maxInteger;
- }
- else
- {
- min = 0.0;
- max = 1.0;
- }
-
- if (min > max)
- max = min;
- else if (max < min)
- min = max;
-
- if (max - min == 0.0)
- {
- carla_stderr("Broken plugin parameter: max - min == 0");
- max = min + 0.1;
- }
-
- if ((m_hints & PLUGIN_HAS_COCKOS_EXTENSIONS) > 0 && effect->dispatcher(effect, effVendorSpecific, kVstParameterUsesIntStep, j, nullptr, 0.0f) >= 0xbeef)
- {
- step = 1.0;
- stepSmall = 1.0;
- stepLarge = 10.0;
- }
- else if (prop.flags & kVstParameterIsSwitch)
- {
- step = max - min;
- stepSmall = step;
- stepLarge = step;
- param.data[j].hints |= PARAMETER_IS_BOOLEAN;
- }
- else if (prop.flags & kVstParameterUsesIntStep)
- {
- step = prop.stepInteger;
- stepSmall = prop.stepInteger;
- stepLarge = prop.largeStepInteger;
- param.data[j].hints |= PARAMETER_IS_INTEGER;
- }
- else if (prop.flags & kVstParameterUsesFloatStep)
- {
- step = prop.stepFloat;
- stepSmall = prop.smallStepFloat;
- stepLarge = prop.largeStepFloat;
- }
- else
- {
- double range = max - min;
- step = range/100.0;
- stepSmall = range/1000.0;
- stepLarge = range/10.0;
- }
-
- if (prop.flags & kVstParameterCanRamp)
- param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
- }
- else
- {
- min = 0.0;
- max = 1.0;
- step = 0.001;
- stepSmall = 0.0001;
- stepLarge = 0.1;
- }
-
- // no such thing as VST default parameters
- def = effect->getParameter(effect, j);
-
- if (def < min)
- def = min;
- else if (def > max)
- def = max;
-
- param.ranges[j].min = min;
- param.ranges[j].max = max;
- param.ranges[j].def = def;
- param.ranges[j].step = step;
- param.ranges[j].stepSmall = stepSmall;
- param.ranges[j].stepLarge = stepLarge;
-
- param.data[j].hints |= PARAMETER_IS_ENABLED;
- #ifndef BUILD_BRIDGE
- param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
- #endif
-
- if ((m_hints & PLUGIN_USES_OLD_VSTSDK) > 0 || effect->dispatcher(effect, effCanBeAutomated, j, 0, nullptr, 0.0f) == 1)
- param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
- }
-
- if (needsCtrlIn)
- {
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = m_name;
- portName += ":";
- }
-
- portName += "control-in";
- portName.truncate(portNameSize);
-
- param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
- }
-
- if (mIns == 1)
- {
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = m_name;
- portName += ":";
- }
-
- portName += "midi-in";
- portName.truncate(portNameSize);
-
- midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
- }
-
- if (mOuts == 1)
- {
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = m_name;
- portName += ":";
- }
-
- portName += "midi-out";
- portName.truncate(portNameSize);
-
- midi.portMout = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
- }
-
- aIn.count = aIns;
- aOut.count = aOuts;
- param.count = params;
-
- // plugin checks
- m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
-
- intptr_t vstCategory = effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f);
-
- if (vstCategory == kPlugCategSynth || vstCategory == kPlugCategGenerator)
- m_hints |= PLUGIN_IS_SYNTH;
-
- if (effect->flags & effFlagsProgramChunks)
- m_hints |= PLUGIN_USES_CHUNKS;
-
- if (aOuts > 0 && (aIns == aOuts || aIns == 1))
- m_hints |= PLUGIN_CAN_DRYWET;
-
- if (aOuts > 0)
- m_hints |= PLUGIN_CAN_VOLUME;
-
- if (aOuts >= 2 && aOuts%2 == 0)
- m_hints |= PLUGIN_CAN_BALANCE;
-
- if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2))
- m_hints |= PLUGIN_CAN_FORCE_STEREO;
-
- // check latency
- if (m_hints & PLUGIN_CAN_DRYWET)
- {
- #ifdef VESTIGE_HEADER
- char* const empty3Ptr = &effect->empty3[0];
- int32_t* initialDelayPtr = (int32_t*)empty3Ptr;
- m_latency = *initialDelayPtr;
- #else
- m_latency = effect->initialDelay;
- #endif
-
- x_client->setLatency(m_latency);
- recreateLatencyBuffers();
- }
-
- // special plugin fixes
- #ifdef __WINE__
- // 1. IL Harmless - disable threaded processing
- if (effect->uniqueID == 1229484653)
- {
- char strBuf[255] = { 0 };
- getLabel(strBuf);
-
- if (std::strcmp(strBuf, "IL Harmless") == 0)
- {
- // TODO - disable threaded processing
- }
- }
- #endif
-
- reloadPrograms(true);
-
- carla_debug("VstPlugin::reload() - end");
- }
-
- void reloadPrograms(const bool init)
- {
- carla_debug("VstPlugin::reloadPrograms(%s)", bool2str(init));
- uint32_t i, oldCount = prog.count;
-
- // Delete old programs
- if (prog.count > 0)
- {
- for (i=0; i < prog.count; i++)
- {
- if (prog.names[i])
- free((void*)prog.names[i]);
- }
-
- delete[] prog.names;
- }
-
- prog.count = 0;
- prog.names = nullptr;
-
- // Query new programs
- prog.count = effect->numPrograms;
-
- if (prog.count > 0)
- prog.names = new const char* [prog.count];
-
- // Update names
- for (i=0; i < prog.count; i++)
- {
- char strBuf[STR_MAX] = { 0 };
- if (effect->dispatcher(effect, effGetProgramNameIndexed, i, 0, strBuf, 0.0f) != 1)
- {
- // program will be [re-]changed later
- effect->dispatcher(effect, effSetProgram, 0, i, nullptr, 0.0f);
- effect->dispatcher(effect, effGetProgramName, 0, 0, strBuf, 0.0f);
- }
- prog.names[i] = strdup(strBuf);
- }
-
- #ifndef BUILD_BRIDGE
- // Update OSC Names
- if (x_engine->isOscControlRegistered())
- {
- x_engine->osc_send_control_set_program_count(m_id, prog.count);
-
- for (i=0; i < prog.count; i++)
- x_engine->osc_send_control_set_program_name(m_id, i, prog.names[i]);
- }
- #endif
-
- if (init)
- {
- if (prog.count > 0)
- setProgram(0, false, false, false, true);
- }
- else
- {
- x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0, nullptr);
-
- // Check if current program is invalid
- bool programChanged = false;
-
- if (prog.count == oldCount+1)
- {
- // one program added, probably created by user
- prog.current = oldCount;
- programChanged = true;
- }
- else if (prog.current >= (int32_t)prog.count)
- {
- // current program > count
- prog.current = 0;
- programChanged = true;
- }
- else if (prog.current < 0 && prog.count > 0)
- {
- // programs exist now, but not before
- prog.current = 0;
- programChanged = true;
- }
- else if (prog.current >= 0 && prog.count == 0)
- {
- // programs existed before, but not anymore
- prog.current = -1;
- programChanged = true;
- }
-
- if (programChanged)
- {
- setProgram(prog.current, true, true, true, true);
- }
- else
- {
- // Program was changed during update, re-set it
- if (prog.current >= 0)
- effect->dispatcher(effect, effSetProgram, 0, prog.current, nullptr, 0.0f);
- }
- }
- }
-
- // -------------------------------------------------------------------
- // Plugin processing
-
- void process(float** const inBuffer, float** const outBuffer, const uint32_t frames)
- {
- uint32_t i, k;
- uint32_t midiEventCount = 0;
-
- vstTimeOffset = 0;
-
- double aInsPeak[2] = { 0.0 };
- double aOutsPeak[2] = { 0.0 };
-
- // reset MIDI
- events.numEvents = 0;
- midiEvents[0].type = 0;
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // --------------------------------------------------------------------------------------------------------
- // Input VU
-
- if (aIn.count > 0 && x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
- {
- if (aIn.count == 1)
- {
- for (k=0; k < frames; k++)
- {
- if (std::abs(inBuffer[0][k]) > aInsPeak[0])
- aInsPeak[0] = std::abs(inBuffer[0][k]);
- }
- }
- else if (aIn.count > 1)
- {
- for (k=0; k < frames; k++)
- {
- if (std::abs(inBuffer[0][k]) > aInsPeak[0])
- aInsPeak[0] = std::abs(inBuffer[0][k]);
-
- if (std::abs(inBuffer[1][k]) > aInsPeak[1])
- aInsPeak[1] = std::abs(inBuffer[1][k]);
- }
- }
- }
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // --------------------------------------------------------------------------------------------------------
- // Parameters Input [Automation]
-
- if (param.portCin && m_active && m_activeBefore)
- {
- bool allNotesOffSent = false;
-
- const CarlaEngineControlEvent* cinEvent;
- uint32_t time, nEvents = param.portCin->getEventCount();
-
- for (i=0; i < nEvents; i++)
- {
- cinEvent = param.portCin->getEvent(i);
-
- if (! cinEvent)
- continue;
-
- time = cinEvent->time;
-
- if (time >= frames)
- continue;
-
- // Control change
- switch (cinEvent->type)
- {
- case CarlaEngineNullEvent:
- break;
-
- case CarlaEngineParameterChangeEvent:
- {
- double value;
-
- // Control backend stuff
- if (cinEvent->channel == m_ctrlInChannel)
- {
- if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
- {
- value = cinEvent->value;
- setDryWet(value, false, false);
- postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
- continue;
- }
-
- if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
- {
- value = cinEvent->value*127/100;
- setVolume(value, false, false);
- postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
- continue;
- }
-
- if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
- {
- double left, right;
- value = cinEvent->value/0.5 - 1.0;
-
- if (value < 0.0)
- {
- left = -1.0;
- right = (value*2)+1.0;
- }
- else if (value > 0.0)
- {
- left = (value*2)-1.0;
- right = 1.0;
- }
- else
- {
- left = -1.0;
- right = 1.0;
- }
-
- setBalanceLeft(left, false, false);
- setBalanceRight(right, false, false);
- postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
- postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
- continue;
- }
- }
-
- // Control plugin parameters
- for (k=0; k < param.count; k++)
- {
- if (param.data[k].midiChannel != cinEvent->channel)
- continue;
- if (param.data[k].midiCC != cinEvent->parameter)
- continue;
- if (param.data[k].type != PARAMETER_INPUT)
- continue;
-
- if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
- {
- if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
- {
- value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
- }
- else
- {
- value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
-
- if (param.data[k].hints & PARAMETER_IS_INTEGER)
- value = rint(value);
- }
-
- setParameterValue(k, value, false, false, false);
- postponeEvent(PluginPostEventParameterChange, k, 0, value);
- }
- }
-
- break;
- }
-
- case CarlaEngineMidiBankChangeEvent:
- break;
-
- case CarlaEngineMidiProgramChangeEvent:
- if (cinEvent->channel == m_ctrlInChannel)
- {
- uint32_t progId = rint(cinEvent->value);
-
- if (progId < prog.count)
- {
- setProgram(progId, false, false, false, false);
- postponeEvent(PluginPostEventProgramChange, progId, 0, 0.0);
- }
- }
- break;
-
- case CarlaEngineAllSoundOffEvent:
- if (cinEvent->channel == m_ctrlInChannel)
- {
- if (midi.portMin && ! allNotesOffSent)
- sendMidiAllNotesOff();
-
- effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
- effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
-
- postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
- postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
-
- allNotesOffSent = true;
- }
- break;
-
- case CarlaEngineAllNotesOffEvent:
- if (cinEvent->channel == m_ctrlInChannel)
- {
- if (midi.portMin && ! allNotesOffSent)
- sendMidiAllNotesOff();
-
- allNotesOffSent = true;
- }
- break;
- }
- }
- } // End of Parameters Input
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // --------------------------------------------------------------------------------------------------------
- // MIDI Input
-
- if (midi.portMin && m_active && m_activeBefore)
- {
- // ----------------------------------------------------------------------------------------------------
- // MIDI Input (External)
-
- {
- engineMidiLock();
-
- for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
- {
- if (extMidiNotes[i].channel < 0)
- break;
-
- VstMidiEvent* const midiEvent = &midiEvents[midiEventCount];
- memset(midiEvent, 0, sizeof(VstMidiEvent));
-
- midiEvent->type = kVstMidiType;
- midiEvent->byteSize = sizeof(VstMidiEvent);
- midiEvent->midiData[0] = uint8_t(extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) + extMidiNotes[i].channel;
- midiEvent->midiData[1] = extMidiNotes[i].note;
- midiEvent->midiData[2] = extMidiNotes[i].velo;
-
- extMidiNotes[i].channel = -1; // mark as invalid
- midiEventCount += 1;
- }
-
- engineMidiUnlock();
-
- } // End of MIDI Input (External)
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // ----------------------------------------------------------------------------------------------------
- // MIDI Input (System)
-
- {
- const CarlaEngineMidiEvent* minEvent;
- uint32_t time, nEvents = midi.portMin->getEventCount();
-
- for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
- {
- minEvent = midi.portMin->getEvent(i);
-
- if (! minEvent)
- continue;
-
- time = minEvent->time;
-
- if (time >= frames)
- continue;
-
- uint8_t status = minEvent->data[0];
- uint8_t channel = status & 0x0F;
-
- // Fix bad note-off
- if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
- status -= 0x10;
-
- VstMidiEvent* const midiEvent = &midiEvents[midiEventCount];
- memset(midiEvent, 0, sizeof(VstMidiEvent));
-
- midiEvent->type = kVstMidiType;
- midiEvent->byteSize = sizeof(VstMidiEvent);
- midiEvent->deltaFrames = minEvent->time;
-
- if (MIDI_IS_STATUS_NOTE_OFF(status))
- {
- uint8_t note = minEvent->data[1];
-
- midiEvent->midiData[0] = status;
- midiEvent->midiData[1] = note;
-
- postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
- }
- else if (MIDI_IS_STATUS_NOTE_ON(status))
- {
- uint8_t note = minEvent->data[1];
- uint8_t velo = minEvent->data[2];
-
- midiEvent->midiData[0] = status;
- midiEvent->midiData[1] = note;
- midiEvent->midiData[2] = velo;
-
- postponeEvent(PluginPostEventNoteOn, channel, note, velo);
- }
- else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
- {
- uint8_t note = minEvent->data[1];
- uint8_t pressure = minEvent->data[2];
-
- midiEvent->midiData[0] = status;
- midiEvent->midiData[1] = note;
- midiEvent->midiData[2] = pressure;
- }
- else if (MIDI_IS_STATUS_AFTERTOUCH(status))
- {
- uint8_t pressure = minEvent->data[1];
-
- midiEvent->midiData[0] = status;
- midiEvent->midiData[1] = pressure;
- }
- else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
- {
- uint8_t lsb = minEvent->data[1];
- uint8_t msb = minEvent->data[2];
-
- midiEvent->midiData[0] = status;
- midiEvent->midiData[1] = lsb;
- midiEvent->midiData[2] = msb;
- }
- else
- continue;
-
- midiEventCount += 1;
- }
- } // End of MIDI Input (System)
-
- } // End of MIDI Input
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // --------------------------------------------------------------------------------------------------------
- // Plugin processing
-
- if (m_active)
- {
- if (! m_activeBefore)
- {
- if (midi.portMin)
- {
- for (k=0; k < MAX_MIDI_CHANNELS; k++)
- {
- memset(&midiEvents[k], 0, sizeof(VstMidiEvent));
- midiEvents[k].type = kVstMidiType;
- midiEvents[k].byteSize = sizeof(VstMidiEvent);
- midiEvents[k].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + k;
- midiEvents[k].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
-
- memset(&midiEvents[k*2], 0, sizeof(VstMidiEvent));
- midiEvents[k*2].type = kVstMidiType;
- midiEvents[k*2].byteSize = sizeof(VstMidiEvent);
- midiEvents[k*2].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + k;
- midiEvents[k*2].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
- }
-
- midiEventCount = MAX_MIDI_CHANNELS*2;
- }
-
- if (m_latency > 0)
- {
- for (i=0; i < aIn.count; i++)
- memset(m_latencyBuffers[i], 0, sizeof(float)*m_latency);
- }
-
- effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
- effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
- }
-
- if (midiEventCount > 0)
- {
- events.numEvents = midiEventCount;
- events.reserved = 0;
- effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
- }
-
- // FIXME - make this a global option
- // don't process if not needed
- //if ((effect->flags & effFlagsNoSoundInStop) > 0 && aInsPeak[0] == 0.0 && aInsPeak[1] == 0.0 && midiEventCount == 0 && ! midi.portMout)
- //{
- if (m_hints & PLUGIN_CAN_PROCESS_REPLACING)
- {
- isProcessing = true;
- effect->processReplacing(effect, inBuffer, outBuffer, frames);
- isProcessing = false;
- }
- else
- {
- for (i=0; i < aOut.count; i++)
- carla_zeroF(outBuffer[i], frames);
-
- #if ! VST_FORCE_DEPRECATED
- isProcessing = true;
- effect->process(effect, inBuffer, outBuffer, frames);
- isProcessing = false;
- #endif
- }
- //}
- }
- else
- {
- if (m_activeBefore)
- {
- effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
- }
- }
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // --------------------------------------------------------------------------------------------------------
- // Post-processing (dry/wet, volume and balance)
-
- if (m_active)
- {
- bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
- bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
- bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
-
- double bal_rangeL, bal_rangeR;
- float bufValue, oldBufLeft[do_balance ? frames : 1];
-
- for (i=0; i < aOut.count; i++)
- {
- // Dry/Wet
- if (do_drywet)
- {
- for (k=0; k < frames; k++)
- {
- if (k < m_latency && m_latency < frames)
- bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
- else
- bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
-
- outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
- }
- }
-
- // Balance
- if (do_balance)
- {
- if (i%2 == 0)
- memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
-
- bal_rangeL = (x_balanceLeft+1.0)/2;
- bal_rangeR = (x_balanceRight+1.0)/2;
-
- for (k=0; k < frames; k++)
- {
- if (i%2 == 0)
- {
- // left output
- outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
- outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
- }
- else
- {
- // right
- outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
- outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
- }
- }
- }
-
- // Volume
- if (do_volume)
- {
- for (k=0; k < frames; k++)
- outBuffer[i][k] *= x_volume;
- }
-
- // Output VU
- if (x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
- {
- for (k=0; i < 2 && k < frames; k++)
- {
- if (std::abs(outBuffer[i][k]) > aOutsPeak[i])
- aOutsPeak[i] = std::abs(outBuffer[i][k]);
- }
- }
- }
-
- // Latency, save values for next callback
- if (m_latency > 0 && m_latency < frames)
- {
- for (i=0; i < aIn.count; i++)
- memcpy(m_latencyBuffers[i], inBuffer[i] + (frames - m_latency), sizeof(float)*m_latency);
- }
- }
- else
- {
- // disable any output sound if not active
- for (i=0; i < aOut.count; i++)
- carla_zeroF(outBuffer[i], frames);
-
- aOutsPeak[0] = 0.0;
- aOutsPeak[1] = 0.0;
-
- } // End of Post-processing
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // --------------------------------------------------------------------------------------------------------
- // MIDI Output
-
- if (midi.portMout && m_active)
- {
- uint8_t data[3] = { 0 };
-
- for (int32_t i = midiEventCount; i < events.numEvents; i++)
- {
- data[0] = midiEvents[i].midiData[0];
- data[1] = midiEvents[i].midiData[1];
- data[2] = midiEvents[i].midiData[2];
-
- // Fix bad note-off
- if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
- data[0] -= 0x10;
-
- midi.portMout->writeEvent(midiEvents[i].deltaFrames, data, 3);
- }
- } // End of MIDI Output
-
- CARLA_PROCESS_CONTINUE_CHECK;
-
- // --------------------------------------------------------------------------------------------------------
- // Peak Values
-
- x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
- x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
- x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
- x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
-
- m_activeBefore = m_active;
- }
-
- void bufferSizeChanged(uint32_t newBufferSize)
- {
- if (m_active)
- {
- effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
- }
-
- #if ! VST_FORCE_DEPRECATED
- effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, newBufferSize, nullptr, x_engine->getSampleRate());
- #endif
- effect->dispatcher(effect, effSetBlockSize, 0, newBufferSize, nullptr, 0.0f);
-
- if (m_active)
- {
- effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
- effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
- }
- }
-
- // -------------------------------------------------------------------
- // Post-poned events
-
- void uiParameterChange(const uint32_t index, const double value)
- {
- CARLA_ASSERT(index < param.count);
-
- if (index >= param.count)
- return;
-
- if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
- osc_send_control(&osc.data, param.data[index].rindex, value);
- }
-
- void uiProgramChange(const uint32_t index)
- {
- CARLA_ASSERT(index < prog.count);
-
- if (index >= prog.count)
- return;
-
- if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
- osc_send_program(&osc.data, index);
- }
-
- void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
- {
- CARLA_ASSERT(channel < 16);
- CARLA_ASSERT(note < 128);
- CARLA_ASSERT(velo > 0 && velo < 128);
-
- if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
- {
- uint8_t midiData[4] = { 0 };
- midiData[1] = MIDI_STATUS_NOTE_ON + channel;
- midiData[2] = note;
- midiData[3] = velo;
- osc_send_midi(&osc.data, midiData);
- }
- }
-
- void uiNoteOff(const uint8_t channel, const uint8_t note)
- {
- CARLA_ASSERT(channel < 16);
- CARLA_ASSERT(note < 128);
-
- if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
- {
- uint8_t midiData[4] = { 0 };
- midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
- midiData[2] = note;
- osc_send_midi(&osc.data, midiData);
- }
- }
-
- // -------------------------------------------------------------------
-
- void handleAudioMasterAutomate(const uint32_t index, const double value)
- {
- //CARLA_ASSERT(m_enabled);
- CARLA_ASSERT_INT(index < param.count, index);
-
- if (index >= param.count /*|| ! m_enabled*/)
- return;
-
- if (isProcessing && ! x_engine->isOffline())
- {
- setParameterValue(index, value, false, false, false);
- postponeEvent(PluginPostEventParameterChange, index, 0, value);
- }
- else
- setParameterValue(index, value, isProcessing, true, true);
- }
-
- intptr_t handleAudioMasterGetCurrentProcessLevel()
- {
- if (x_engine->isOffline())
- return kVstProcessLevelOffline;
- if (isProcessing)
- return kVstProcessLevelRealtime;
- return kVstProcessLevelUser;
- }
-
- intptr_t handleAudioMasterGetBlockSize()
- {
- const uint32_t bufferSize = x_engine->getBufferSize();
- effect->dispatcher(effect, effSetBlockSize, 0, bufferSize, nullptr, 0.0f);
- return bufferSize;
- }
-
- intptr_t handleAudioMasterGetSampleRate()
- {
- const double sampleRate = x_engine->getSampleRate();
- effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, sampleRate);
- return sampleRate;
- }
-
- intptr_t handleAudioMasterGetTime()
- {
- memset(&vstTimeInfo, 0, sizeof(VstTimeInfo_R));
-
- const CarlaEngineTimeInfo* const timeInfo = x_engine->getTimeInfo();
-
- vstTimeInfo.flags |= kVstTransportChanged;
-
- if (timeInfo->playing)
- vstTimeInfo.flags |= kVstTransportPlaying;
-
- vstTimeInfo.samplePos = timeInfo->frame + vstTimeOffset;
- vstTimeInfo.sampleRate = x_engine->getSampleRate();
-
- vstTimeInfo.nanoSeconds = timeInfo->time;
- vstTimeInfo.flags |= kVstNanosValid;
-
- if (timeInfo->valid & CarlaEngineTimeBBT)
- {
- double ppqBar = double(timeInfo->bbt.bar - 1) * timeInfo->bbt.beats_per_bar;
- double ppqBeat = double(timeInfo->bbt.beat - 1);
- double ppqTick = double(timeInfo->bbt.tick) / timeInfo->bbt.ticks_per_beat;
-
- // Bars
- vstTimeInfo.barStartPos = ppqBar;
- vstTimeInfo.flags |= kVstBarsValid;
-
- // PPQ Pos
- vstTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
- vstTimeInfo.flags |= kVstPpqPosValid;
-
- // Tempo
- vstTimeInfo.tempo = timeInfo->bbt.beats_per_minute;
- vstTimeInfo.flags |= kVstTempoValid;
-
- // Time Signature
- vstTimeInfo.timeSigNumerator = timeInfo->bbt.beats_per_bar;
- vstTimeInfo.timeSigDenominator = timeInfo->bbt.beat_type;
- vstTimeInfo.flags |= kVstTimeSigValid;
- }
- else
- {
- // Tempo
- vstTimeInfo.tempo = 120.0;
- vstTimeInfo.flags |= kVstTempoValid;
-
- // Time Signature
- vstTimeInfo.timeSigNumerator = 4;
- vstTimeInfo.timeSigDenominator = 4;
- vstTimeInfo.flags |= kVstTimeSigValid;
- }
-
- return (intptr_t)&vstTimeInfo;
- }
-
- intptr_t handleAudioMasterTempoAt()
- {
- const CarlaEngineTimeInfo* const timeInfo = x_engine->getTimeInfo();
-
- if (timeInfo->valid & CarlaEngineTimeBBT)
- return timeInfo->bbt.beats_per_minute * 10000;
-
- return 0;
- }
-
- intptr_t handleAudioMasterIOChanged()
- {
- carla_debug("VstPlugin::handleAudioMasterIOChanged()");
- CARLA_ASSERT(m_enabled);
-
- // TESTING
-
- if (! m_enabled)
- return 1;
-
- if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
- {
- carla_stderr2("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
- return 0;
- }
-
- engineProcessLock();
- m_enabled = false;
- engineProcessUnlock();
-
- if (m_active)
- {
- effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
- }
-
- reload();
-
- if (m_active)
- {
- effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
- effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
- }
-
- x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
-
- return 1;
- }
-
- void handleAudioMasterNeedIdle()
- {
- carla_debug("VstPlugin::handleAudioMasterNeedIdle()");
- needIdle = true;
- }
-
- intptr_t handleAudioMasterProcessEvents(const VstEvents* const vstEvents)
- {
- CARLA_ASSERT(m_enabled);
- CARLA_ASSERT(midi.portMout);
- CARLA_ASSERT(isProcessing);
-
- if (! m_enabled)
- return 0;
-
- if (! midi.portMout)
- return 0;
-
- if (! isProcessing)
- {
- carla_stderr2("VstPlugin::handleAudioMasterProcessEvents(%p) - received MIDI out events outside audio thread, ignoring", vstEvents);
- return 0;
- }
-
- for (int32_t i=0; i < vstEvents->numEvents && events.numEvents < MAX_MIDI_EVENTS*2; i++)
- {
- if (! vstEvents->events[i])
- break;
-
- const VstMidiEvent* const vstMidiEvent = (const VstMidiEvent*)vstEvents->events[i];
-
- if (vstMidiEvent->type == kVstMidiType)
- memcpy(&midiEvents[events.numEvents++], vstMidiEvent, sizeof(VstMidiEvent));
- }
-
- return 1;
- }
-
- intptr_t handleAdioMasterSizeWindow(int32_t width, int32_t height)
- {
- carla_debug("VstPlugin::handleAudioMasterSizeWindow(%i, %i)", width, height);
-
- gui.width = width;
- gui.height = height;
-
- x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0, nullptr);
-
- return 1;
- }
-
- void handleAudioMasterUpdateDisplay()
- {
- carla_debug("VstPlugin::handleAudioMasterUpdateDisplay()");
-
- // Update current program name
- if (prog.count > 0 && prog.current >= 0)
- {
- const int32_t index = prog.current;
- char strBuf[STR_MAX] = { 0 };
-
- effect->dispatcher(effect, effGetProgramName, 0, 0, strBuf, 0.0f);
-
- if (! prog.names[index])
- {
- prog.names[index] = strdup(strBuf);
- }
- else if (strBuf[0] != 0 && std::strcmp(strBuf, prog.names[index]) != 0)
- {
- free((void*)prog.names[index]);
- prog.names[index] = strdup(strBuf);
- }
- }
-
- // Tell backend to update
- x_engine->callback(CALLBACK_UPDATE, m_id, 0, 0, 0.0, nullptr);
- }
-
- void handleAudioMasterWantMidi()
- {
- carla_debug("VstPlugin::handleAudioMasterWantMidi()");
-
- m_hints |= PLUGIN_WANTS_MIDI_INPUT;
- }
-
- // -------------------------------------------------------------------
-
- static intptr_t hostCanDo(const char* const feature)
- {
- carla_debug("VstPlugin::hostCanDo(\"%s\")", feature);
-
- if (std::strcmp(feature, "supplyIdle") == 0)
- return 1;
- if (std::strcmp(feature, "sendVstEvents") == 0)
- return 1;
- if (std::strcmp(feature, "sendVstMidiEvent") == 0)
- return 1;
- if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
- return -1;
- if (std::strcmp(feature, "sendVstTimeInfo") == 0)
- return 1;
- if (std::strcmp(feature, "receiveVstEvents") == 0)
- return 1;
- if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
- return 1;
- if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
- return -1;
- if (std::strcmp(feature, "reportConnectionChanges") == 0)
- return -1;
- if (std::strcmp(feature, "acceptIOChanges") == 0)
- {
- //if (CarlaEngine::processMode == PROCESS_MODE_CONTINUOUS_RACK)
- // return -1;
- return 1;
- }
- if (std::strcmp(feature, "sizeWindow") == 0)
- return 1;
- if (std::strcmp(feature, "offline") == 0)
- return -1;
- if (std::strcmp(feature, "openFileSelector") == 0)
- return -1;
- if (std::strcmp(feature, "closeFileSelector") == 0)
- return -1;
- if (std::strcmp(feature, "startStopProcess") == 0)
- return 1;
- if (std::strcmp(feature, "supportShell") == 0)
- return -1;
- if (std::strcmp(feature, "shellCategory") == 0)
- return -1;
-
- // unimplemented
- carla_stderr("VstPlugin::hostCanDo(\"%s\") - unknown feature", feature);
- return 0;
- }
-
- static intptr_t VSTCALLBACK hostCallback(AEffect* const effect, const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
- {
- #ifdef DEBUG
- if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
- carla_debug("VstPlugin::hostCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
- #endif
-
- #if 0
- // Cockos VST extensions
- if (/*effect &&*/ ptr && (uint32_t)opcode == 0xdeadbeef && (uint32_t)index == 0xdeadf00d)
- {
- const char* const func = (char*)ptr;
-
- if (std::strcmp(func, "GetPlayPosition") == 0)
- return 0;
- if (std::strcmp(func, "GetPlayPosition2") == 0)
- return 0;
- if (std::strcmp(func, "GetCursorPosition") == 0)
- return 0;
- if (std::strcmp(func, "GetPlayState") == 0)
- return 0;
- if (std::strcmp(func, "SetEditCurPos") == 0)
- return 0;
- if (std::strcmp(func, "GetSetRepeat") == 0)
- return 0;
- if (std::strcmp(func, "GetProjectPath") == 0)
- return 0;
- if (std::strcmp(func, "OnPlayButton") == 0)
- return 0;
- if (std::strcmp(func, "OnStopButton") == 0)
- return 0;
- if (std::strcmp(func, "OnPauseButton") == 0)
- return 0;
- if (std::strcmp(func, "IsInRealTimeAudio") == 0)
- return 0;
- if (std::strcmp(func, "Audio_IsRunning") == 0)
- return 0;
- }
- #endif
-
- // Check if 'resvd1' points to this plugin, or register ourselfs if possible
- VstPlugin* self = nullptr;
-
- if (effect)
- {
- #ifdef VESTIGE_HEADER
- if (effect->ptr1)
- {
- self = (VstPlugin*)effect->ptr1;
- #else
- if (effect->resvd1)
- {
- self = (VstPlugin*)getPointerFromAddress(effect->resvd1);
- #endif
- if (self->unique1 != self->unique2)
- self = nullptr;
- }
-
- if (self)
- {
- if (! self->effect)
- self->effect = effect;
-
- CARLA_ASSERT(self->effect == effect);
-
- if (self->effect != effect)
- {
- carla_stderr("VstPlugin::hostCallback() - host pointer mismatch: %p != %p", self->effect, effect);
- self = nullptr;
- }
- }
- else if (lastVstPlugin)
- {
- #ifdef VESTIGE_HEADER
- effect->ptr1 = lastVstPlugin;
- #else
- effect->resvd1 = getAddressFromPointer(lastVstPlugin);
- #endif
- self = lastVstPlugin;
- }
- }
-
- intptr_t ret = 0;
-
- switch (opcode)
- {
- case audioMasterAutomate:
- CARLA_ASSERT(self);
- if (self)
- self->handleAudioMasterAutomate(index, opt);
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterAutomate called without valid object");
- break;
-
- case audioMasterVersion:
- ret = kVstVersion;
- break;
-
- case audioMasterCurrentId:
- // TODO
- // if using old sdk, return effect->uniqueID
- break;
-
- case audioMasterIdle:
- CARLA_ASSERT(effect);
- if (effect)
- effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterIdle called without valid effect");
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterPinConnected:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
-
- case audioMasterWantMidi:
- // Deprecated in VST SDK 2.4
- CARLA_ASSERT(self);
- if (self)
- self->handleAudioMasterWantMidi();
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterWantMidi called without valid object");
- break;
- #endif
-
- case audioMasterGetTime:
- CARLA_ASSERT(self);
- if (self)
- {
- ret = self->handleAudioMasterGetTime();
- }
- else
- {
- static VstTimeInfo_R vstTimeInfo;
- memset(&vstTimeInfo, 0, sizeof(VstTimeInfo_R));
- vstTimeInfo.sampleRate = 44100.0;
-
- // Tempo
- vstTimeInfo.tempo = 120.0;
- vstTimeInfo.flags |= kVstTempoValid;
-
- // Time Signature
- vstTimeInfo.timeSigNumerator = 4;
- vstTimeInfo.timeSigDenominator = 4;
- vstTimeInfo.flags |= kVstTimeSigValid;
-
- ret = (intptr_t)&vstTimeInfo;
- }
- break;
-
- case audioMasterProcessEvents:
- CARLA_ASSERT(self && ptr);
- if (self)
- {
- if (ptr)
- ret = self->handleAudioMasterProcessEvents((const VstEvents*)ptr);
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterProcessEvents called with invalid pointer");
- }
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterProcessEvents called without valid object");
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterSetTime:
- // Deprecated in VST SDK 2.4
- break;
-
- case audioMasterTempoAt:
- // Deprecated in VST SDK 2.4
- CARLA_ASSERT(self);
- if (self)
- ret = self->handleAudioMasterTempoAt();
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterTempoAt called without valid object");
- if (ret == 0)
- ret = 120 * 10000;
- break;
-
- case audioMasterGetNumAutomatableParameters:
- // Deprecated in VST SDK 2.4
- ret = 0; //x_engine->options.maxParameters;
-
- if (effect && ret > effect->numParams)
- ret = effect->numParams;
- // FIXME
- //ret = carla_minPositiveI(effect->numParams, MAX_PARAMETERS);
- break;
-
- case audioMasterGetParameterQuantization:
- // Deprecated in VST SDK 2.4
- ret = 1; // full single float precision
- break;
- #endif
-
- case audioMasterIOChanged:
- CARLA_ASSERT(self);
- if (self)
- ret = self->handleAudioMasterIOChanged();
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterIOChanged called without valid object");
- break;
-
- case audioMasterNeedIdle:
- // Deprecated in VST SDK 2.4
- CARLA_ASSERT(self);
- if (self)
- self->handleAudioMasterNeedIdle();
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterNeedIdle called without valid object");
- break;
-
- case audioMasterSizeWindow:
- CARLA_ASSERT(self);
- if (self)
- {
- if (index > 0 && value > 0)
- ret = self->handleAdioMasterSizeWindow(index, value);
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterSizeWindow called with invalid size");
- }
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterSizeWindow called without valid object");
- break;
-
- case audioMasterGetSampleRate:
- CARLA_ASSERT(self);
- if (self)
- ret = self->handleAudioMasterGetSampleRate();
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterGetSampleRate called without valid object");
- if (ret == 0)
- ret = 44100;
- break;
-
- case audioMasterGetBlockSize:
- CARLA_ASSERT(self);
- if (self)
- ret = self->handleAudioMasterGetBlockSize();
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterGetBlockSize called without valid object");
- if (ret == 0)
- // ret = CarlaEngine::processHighPrecision ? 8 : 512;
- ret = 512;
- break;
-
- case audioMasterGetInputLatency:
- ret = 0;
- break;
-
- case audioMasterGetOutputLatency:
- ret = 0;
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterGetPreviousPlug:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
-
- case audioMasterGetNextPlug:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
-
- case audioMasterWillReplaceOrAccumulate:
- // Deprecated in VST SDK 2.4
- ret = 1; // replace
- break;
- #endif
-
- case audioMasterGetCurrentProcessLevel:
- if (self)
- {
- ret = self->handleAudioMasterGetCurrentProcessLevel();
- }
- else
- {
- carla_stderr("VstPlugin::hostCallback::audioMasterGetCurrentProcessLevel called without valid object");
- ret = kVstProcessLevelUnknown;
- }
- break;
-
- case audioMasterGetAutomationState:
- ret = kVstAutomationReadWrite;
- break;
-
- case audioMasterOfflineStart:
- case audioMasterOfflineRead:
- case audioMasterOfflineWrite:
- case audioMasterOfflineGetCurrentPass:
- case audioMasterOfflineGetCurrentMetaPass:
- // TODO
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterSetOutputSampleRate:
- // Deprecated in VST SDK 2.4
- break;
-
- case audioMasterGetOutputSpeakerArrangement:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
- #endif
-
- case audioMasterGetVendorString:
- CARLA_ASSERT(ptr);
- if (ptr)
- {
- std::strcpy((char*)ptr, "Cadence");
- ret = 1;
- }
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterGetVendorString called with invalid pointer");
- break;
-
- case audioMasterGetProductString:
- CARLA_ASSERT(ptr);
- if (ptr)
- {
- std::strcpy((char*)ptr, "Carla");
- ret = 1;
- }
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterGetProductString called with invalid pointer");
- break;
-
- case audioMasterGetVendorVersion:
- ret = 0x050; // 0.5.0
- break;
-
- case audioMasterVendorSpecific:
- // TODO - cockos extensions
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterSetIcon:
- // Deprecated in VST SDK 2.4
- break;
- #endif
-
- case audioMasterCanDo:
- CARLA_ASSERT(ptr);
- if (ptr)
- ret = hostCanDo((const char*)ptr);
- else
- carla_stderr("VstPlugin::hostCallback::audioMasterCanDo called with invalid pointer");
- break;
-
- case audioMasterGetLanguage:
- ret = kVstLangEnglish;
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterOpenWindow:
- case audioMasterCloseWindow:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
- #endif
-
- case audioMasterGetDirectory:
- // TODO
- //if (ptr)
- // std::strcpy((char*)ptr, "stuff");
- //else
- // carla_stderr("VstPlugin::hostCallback::audioMasterGetDirectory called with invalid pointer");
- break;
-
- case audioMasterUpdateDisplay:
- CARLA_ASSERT(effect);
- if (self)
- self->handleAudioMasterUpdateDisplay();
- if (effect)
- effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
- ret = 1;
- break;
-
- case audioMasterBeginEdit:
- case audioMasterEndEdit:
- // TODO
- break;
-
- case audioMasterOpenFileSelector:
- case audioMasterCloseFileSelector:
- // TODO
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterEditFile:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
-
- case audioMasterGetChunkFile:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
-
- case audioMasterGetInputSpeakerArrangement:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
- #endif
-
- default:
- #ifdef DEBUG
- carla_debug("VstPlugin::hostCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
- #endif
- break;
- }
-
- return ret;
- }
-
- // -------------------------------------------------------------------
-
- bool init(const char* const filename, const char* const name, const char* const label)
- {
- // ---------------------------------------------------------------
- // open DLL
-
- if (! libOpen(filename))
- {
- x_engine->setLastError(libError(filename));
- return false;
- }
-
- // ---------------------------------------------------------------
- // get DLL main entry
-
- VST_Function vstFn = (VST_Function)libSymbol("VSTPluginMain");
-
- if (! vstFn)
- {
- vstFn = (VST_Function)libSymbol("main");
-
- if (! vstFn)
- {
- x_engine->setLastError("Could not find the VST main entry in the plugin library");
- return false;
- }
- }
-
- // ---------------------------------------------------------------
- // initialize plugin (part 1)
-
- lastVstPlugin = this;
- effect = vstFn(hostCallback);
- lastVstPlugin = nullptr;
-
- if ((! effect) || effect->magic != kEffectMagic)
- {
- x_engine->setLastError("Plugin failed to initialize");
- return false;
- }
-
- #ifdef VESTIGE_HEADER
- effect->ptr1 = this;
- #else
- effect->resvd1 = getAddressFromPointer(this);
- #endif
-
- effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
-
- // ---------------------------------------------------------------
- // get info
-
- m_filename = strdup(filename);
-
- if (name)
- {
- m_name = x_engine->getUniquePluginName(name);
- }
- else
- {
- char strBuf[STR_MAX] = { 0 };
- effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
-
- if (strBuf[0] != 0)
- m_name = x_engine->getUniquePluginName(strBuf);
- else
- m_name = x_engine->getUniquePluginName(label);
- }
-
- // ---------------------------------------------------------------
- // register client
-
- x_client = x_engine->addClient(this);
-
- if (! x_client->isOk())
- {
- x_engine->setLastError("Failed to register plugin client");
- return false;
- }
-
- // ---------------------------------------------------------------
- // initialize plugin (part 2)
-
- #if ! VST_FORCE_DEPRECATED
- effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, x_engine->getBufferSize(), nullptr, x_engine->getSampleRate());
- #endif
- effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, x_engine->getSampleRate());
- effect->dispatcher(effect, effSetBlockSize, 0, x_engine->getBufferSize(), nullptr, 0.0f);
- effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
-
- #if ! VST_FORCE_DEPRECATED
- // dummy pre-start to catch possible wantEvents() call on old plugins
- effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
- effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
- effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
- #endif
-
- // special checks
- if ((uintptr_t)effect->dispatcher(effect, effCanDo, 0, 0, (void*)"hasCockosExtensions", 0.0f) == 0xbeef0000)
- {
- carla_debug("Plugin has Cockos extensions!");
- m_hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
- }
-
- if (effect->dispatcher(effect, effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
- m_hints |= PLUGIN_USES_OLD_VSTSDK;
-
- if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != effect->process)
- m_hints |= PLUGIN_CAN_PROCESS_REPLACING;
-
- // ---------------------------------------------------------------
- // gui stuff
-
- if (effect->flags & effFlagsHasEditor)
- {
- m_hints |= PLUGIN_HAS_GUI;
-
- #if defined(Q_OS_LINUX) && 0 // FIXME
- if (x_engine->options.bridge_vstx11 && x_engine->preferUiBridges() && ! (effect->flags & effFlagsProgramChunks))
- {
- osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_VST_GUI);
- osc.thread->setOscData(x_engine->options.bridge_vstx11, label);
- gui.type = GUI_EXTERNAL_OSC;
- }
- else
- #endif
- {
- m_hints |= PLUGIN_USES_SINGLE_THREAD;
- #if defined(Q_OS_WIN)
- gui.type = GUI_INTERNAL_HWND;
- #elif defined(Q_OS_MACOS)
- gui.type = GUI_INTERNAL_COCOA;
- #elif defined(Q_OS_LINUX)
- gui.type = GUI_INTERNAL_X11;
- #else
- m_hints &= ~PLUGIN_HAS_GUI;
- #endif
- }
- }
-
- return true;
- }
- #endif
-
- private:
- int unique1;
-
- AEffect* effect;
-
- struct {
- int32_t numEvents;
- intptr_t reserved;
- VstEvent* data[MAX_MIDI_EVENTS*2];
- } events;
- VstMidiEvent midiEvents[MAX_MIDI_EVENTS*2];
-
- uint32_t vstTimeOffset;
- VstTimeInfo_R vstTimeInfo;
-
- struct {
- //GuiType type;
- bool visible;
- int width;
- int height;
- } gui;
-
- bool isProcessing;
- bool needIdle;
- static VstPlugin* lastVstPlugin;
-
- int unique2;
- };
-
- VstPlugin* VstPlugin::lastVstPlugin = nullptr;
-
- CARLA_BACKEND_END_NAMESPACE
-
- #else // WANT_VST
- # warning Building without VST support
- #endif
-
- CARLA_BACKEND_START_NAMESPACE
-
- CarlaPlugin* CarlaPlugin::newVST(const Initializer& init)
- {
- carla_debug("CarlaPlugin::newVST(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
-
- #ifdef WANT_VST
- VstPlugin* const plugin = new VstPlugin(init.engine, init.id);
-
- //if (! plugin->init(init.filename, init.name, init.label))
- {
- delete plugin;
- return nullptr;
- }
-
- plugin->reload();
-
- if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
- {
- init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
- delete plugin;
- return nullptr;
- }
-
- return plugin;
- #else
- init.engine->setLastError("VST support not available");
- return nullptr;
- #endif
- }
-
- CARLA_BACKEND_END_NAMESPACE
|