|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602 |
- /*
- * Carla VST Plugin
- * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #include "CarlaPluginInternal.hpp"
- #include "CarlaEngine.hpp"
-
- #include "CarlaVstUtils.hpp"
-
- #include "CarlaMathUtils.hpp"
- #include "CarlaPluginUI.hpp"
-
- #ifdef CARLA_OS_MAC
- # import <Foundation/Foundation.h>
- #endif
-
- #include <pthread.h>
-
- #include "water/memory/ByteOrder.h"
-
- #undef VST_FORCE_DEPRECATED
- #define VST_FORCE_DEPRECATED 0
-
- #undef kEffectMagic
- #define kEffectMagic (CCONST( 'V', 's', 't', 'P' ))
-
- using water::ByteOrder;
-
- CARLA_BACKEND_START_NAMESPACE
-
- // -----------------------------------------------------
-
- const uint PLUGIN_CAN_PROCESS_REPLACING = 0x1000;
- const uint PLUGIN_HAS_COCKOS_EXTENSIONS = 0x2000;
- const uint PLUGIN_USES_OLD_VSTSDK = 0x4000;
- const uint PLUGIN_WANTS_MIDI_INPUT = 0x8000;
-
- static const int32_t kVstMidiEventSize = static_cast<int32_t>(sizeof(VstMidiEvent));
-
- // -----------------------------------------------------
-
- class CarlaPluginVST2 : public CarlaPlugin,
- private CarlaPluginUI::Callback
- {
- public:
- CarlaPluginVST2(CarlaEngine* const engine, const uint id)
- : CarlaPlugin(engine, id),
- fUnique1(1),
- fEffect(nullptr),
- fMidiEventCount(0),
- fTimeInfo(),
- fNeedIdle(false),
- fLastChunk(nullptr),
- fIsProcessing(false),
- fMainThread(pthread_self()),
- #ifdef PTW32_DLLPORT
- fProcThread({nullptr, 0}),
- #else
- fProcThread(0),
- #endif
- #ifdef CARLA_OS_MAC
- fMacBundleRef(nullptr),
- fMacBundleRefNum(0),
- #endif
- fEvents(),
- fUI(),
- fUnique2(2)
- {
- carla_debug("CarlaPluginVST2::CarlaPluginVST2(%p, %i)", engine, id);
-
- carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
- carla_zeroStruct(fTimeInfo);
-
- for (ushort i=0; i < kPluginMaxMidiEvents*2; ++i)
- fEvents.data[i] = (VstEvent*)&fMidiEvents[i];
-
- // make plugin valid
- srand(id);
- fUnique1 = fUnique2 = rand();
- }
-
- ~CarlaPluginVST2() override
- {
- carla_debug("CarlaPluginVST2::~CarlaPluginVST2()");
-
- // 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();
-
- CARLA_ASSERT(! fIsProcessing);
-
- if (pData->active)
- {
- deactivate();
- pData->active = false;
- }
-
- if (fEffect != nullptr)
- {
- dispatcher(effClose, 0, 0, nullptr, 0.0f);
- fEffect = nullptr;
- }
-
- // make plugin invalid
- fUnique2 += 1;
-
- if (fLastChunk != nullptr)
- {
- std::free(fLastChunk);
- fLastChunk = nullptr;
- }
-
- clearBuffers();
-
- #ifdef CARLA_OS_MAC
- if (fMacBundleRef != nullptr)
- {
- CFBundleCloseBundleResourceMap(fMacBundleRef, fMacBundleRefNum);
-
- if (CFGetRetainCount(fMacBundleRef) == 1)
- CFBundleUnloadExecutable(fMacBundleRef);
-
- if (CFGetRetainCount(fMacBundleRef) > 0)
- CFRelease(fMacBundleRef);
-
- fMacBundleRef = nullptr;
- }
- #endif
- }
-
- // -------------------------------------------------------------------
- // Information (base)
-
- PluginType getType() const noexcept override
- {
- return PLUGIN_VST2;
- }
-
- PluginCategory getCategory() const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, CarlaPlugin::getCategory());
-
- const intptr_t category(dispatcher(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 (fEffect->flags & effFlagsIsSynth)
- return PLUGIN_CATEGORY_SYNTH;
-
- return CarlaPlugin::getCategory();
- }
-
- int64_t getUniqueId() const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
-
- return static_cast<int64_t>(fEffect->uniqueID);
- }
-
- // -------------------------------------------------------------------
- // 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(fEffect != nullptr, 0);
- CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
-
- *dataPtr = nullptr;
-
- try {
- const intptr_t ret = dispatcher(effGetChunk, 0 /* bank */, 0, dataPtr, 0.0f);
- CARLA_SAFE_ASSERT_RETURN(ret >= 0, 0);
- return static_cast<std::size_t>(ret);
- } CARLA_SAFE_EXCEPTION_RETURN("CarlaPluginVST2::getChunkData", 0);
- }
-
- // -------------------------------------------------------------------
- // Information (per-plugin data)
-
- uint getOptionsAvailable() const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
-
- uint options = 0x0;
-
- // can't disable fixed buffers if using latency or MIDI output
- if (pData->latency.frames == 0 && ! hasMidiOutput())
- options |= PLUGIN_OPTION_FIXED_BUFFERS;
-
- if (fEffect->numPrograms > 1)
- options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
-
- if (fEffect->flags & effFlagsProgramChunks)
- options |= PLUGIN_OPTION_USE_CHUNKS;
-
- if (hasMidiInput())
- {
- 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;
- }
-
- return options;
- }
-
- float getParameterValue(const uint32_t parameterId) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0.0f);
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
-
- return fEffect->getParameter(fEffect, static_cast<int32_t>(parameterId));
- }
-
- void getLabel(char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
-
- strBuf[0] = '\0';
- dispatcher(effGetProductString, 0, 0, strBuf, 0.0f);
- }
-
- void getMaker(char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
-
- strBuf[0] = '\0';
- dispatcher(effGetVendorString, 0, 0, strBuf, 0.0f);
- }
-
- void getCopyright(char* const strBuf) const noexcept override
- {
- getMaker(strBuf);
- }
-
- void getRealName(char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
-
- strBuf[0] = '\0';
- dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
- }
-
- void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
-
- strBuf[0] = '\0';
-
- VstParameterProperties prop;
- carla_zeroStruct(prop);
-
- if (dispatcher(effGetParameterProperties, static_cast<int32_t>(parameterId), 0, &prop, 0) == 1 && prop.label[0] != '\0')
- {
- std::strncpy(strBuf, prop.label, 64);
- strBuf[64] = '\0';
- return;
- }
-
- strBuf[0] = '\0';
- dispatcher(effGetParamName, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
- }
-
- void getParameterText(const uint32_t parameterId, char* const strBuf) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
-
- strBuf[0] = '\0';
- dispatcher(effGetParamDisplay, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
-
- if (strBuf[0] == '\0')
- std::snprintf(strBuf, STR_MAX, "%f", getParameterValue(parameterId));
- }
-
- void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
-
- strBuf[0] = '\0';
- dispatcher(effGetParamLabel, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
- }
-
- // -------------------------------------------------------------------
- // Set data (state)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Set data (internal stuff)
-
- void setName(const char* const newName) override
- {
- CarlaPlugin::setName(newName);
-
- if (fUI.window != nullptr)
- {
- CarlaString guiTitle(pData->name);
- guiTitle += " (GUI)";
- fUI.window->setTitle(guiTitle.buffer());
- }
- }
-
- // -------------------------------------------------------------------
- // 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(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
-
- const float fixedValue(pData->param.getFixedValue(parameterId, value));
- fEffect->setParameter(fEffect, static_cast<int32_t>(parameterId), fixedValue);
-
- CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
- }
-
- void setParameterValueRT(const uint32_t parameterId, const float value) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
-
- const float fixedValue(pData->param.getFixedValue(parameterId, value));
- fEffect->setParameter(fEffect, static_cast<int32_t>(parameterId), fixedValue);
-
- CarlaPlugin::setParameterValueRT(parameterId, fixedValue);
- }
-
- 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(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
-
- if (loadOldSaveFormat(data, dataSize))
- return;
-
- if (fLastChunk != nullptr)
- std::free(fLastChunk);
-
- fLastChunk = std::malloc(dataSize);
- CARLA_SAFE_ASSERT_RETURN(fLastChunk != nullptr,);
-
- std::memcpy(fLastChunk, data, dataSize);
-
- {
- const ScopedSingleProcessLocker spl(this, true);
- dispatcher(effSetChunk, 0 /* bank */, static_cast<intptr_t>(dataSize), fLastChunk, 0.0f);
- }
-
- // simulate an updateDisplay callback
- handleAudioMasterCallback(audioMasterUpdateDisplay, 0, 0, nullptr, 0.0f);
-
- #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
- const bool sendOsc(pData->engine->isOscControlRegistered());
- #else
- const bool sendOsc(false);
- #endif
- pData->updateParameterValues(this, sendOsc, 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(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
- CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
-
- if (index >= 0)
- {
- try {
- dispatcher(effBeginSetProgram, 0, 0, nullptr, 0.0f);
- } CARLA_SAFE_EXCEPTION_RETURN("effBeginSetProgram",);
-
- {
- const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
-
- try {
- dispatcher(effSetProgram, 0, index, nullptr, 0.0f);
- } CARLA_SAFE_EXCEPTION("effSetProgram");
- }
-
- try {
- dispatcher(effEndSetProgram, 0, 0, nullptr, 0.0f);
- } CARLA_SAFE_EXCEPTION("effEndSetProgram");
- }
-
- CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
- }
-
- void setProgramRT(const uint32_t uindex) noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(uindex < pData->prog.count,);
-
- try {
- dispatcher(effBeginSetProgram, 0, 0, nullptr, 0.0f);
- } CARLA_SAFE_EXCEPTION_RETURN("effBeginSetProgram",);
-
- try {
- dispatcher(effSetProgram, 0, static_cast<intptr_t>(uindex), nullptr, 0.0f);
- } CARLA_SAFE_EXCEPTION("effSetProgram");
-
- try {
- dispatcher(effEndSetProgram, 0, 0, nullptr, 0.0f);
- } CARLA_SAFE_EXCEPTION("effEndSetProgram");
-
- CarlaPlugin::setProgramRT(uindex);
- }
-
- // -------------------------------------------------------------------
- // Set ui stuff
-
- void showCustomUI(const bool yesNo) override
- {
- if (fUI.isVisible == yesNo)
- return;
-
- if (yesNo)
- {
- CarlaString uiTitle(pData->name);
- uiTitle += " (GUI)";
-
- intptr_t value = 0;
- void* vstPtr = nullptr;
- ERect* vstRect = nullptr;
-
- if (fUI.window == nullptr)
- {
- const char* msg = nullptr;
- const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
-
- #if defined(CARLA_OS_MAC) && defined(CARLA_OS_64BIT)
- fUI.window = CarlaPluginUI::newCocoa(this, frontendWinId, false);
- #elif defined(CARLA_OS_WIN)
- fUI.window = CarlaPluginUI::newWindows(this, frontendWinId, false);
- #elif defined(HAVE_X11)
- fUI.window = CarlaPluginUI::newX11(this, frontendWinId, false);
- #else
- msg = "Unknown UI type";
- #endif
-
- if (fUI.window == nullptr)
- return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
-
- fUI.window->setTitle(uiTitle.buffer());
- }
-
- vstPtr = fUI.window->getPtr();
-
- dispatcher(effEditGetRect, 0, 0, &vstRect, 0.0f);
-
- #ifdef HAVE_X11
- value = (intptr_t)fUI.window->getDisplay();
- #endif
-
- if (dispatcher(effEditOpen, 0, value, vstPtr, 0.0f) != 0)
- {
- if (vstRect == nullptr || vstRect->right - vstRect->left < 2)
- dispatcher(effEditGetRect, 0, 0, &vstRect, 0.0f);
-
- if (vstRect != nullptr)
- {
- const int width(vstRect->right - vstRect->left);
- const int height(vstRect->bottom - vstRect->top);
-
- CARLA_SAFE_ASSERT_INT2(width > 1 && height > 1, width, height);
-
- if (width > 1 && height > 1)
- fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
- }
-
- fUI.window->show();
- fUI.isVisible = true;
- }
- else
- {
- delete fUI.window;
- fUI.window = nullptr;
-
- carla_stderr2("Plugin refused to open its own UI");
- return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
- }
- }
- else
- {
- fUI.isVisible = false;
-
- CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
- fUI.window->hide();
-
- dispatcher(effEditClose, 0, 0, nullptr, 0.0f);
- }
- }
-
- void idle() override
- {
- if (fNeedIdle)
- dispatcher(effIdle, 0, 0, nullptr, 0.0f);
-
- CarlaPlugin::idle();
- }
-
- void uiIdle() override
- {
- if (fUI.window != nullptr)
- {
- fUI.window->idle();
-
- if (fUI.isVisible)
- dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
- }
-
- CarlaPlugin::uiIdle();
- }
-
- // -------------------------------------------------------------------
- // Plugin state
-
- void reload() override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
- carla_debug("CarlaPluginVST2::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;
-
- bool needsCtrlIn, needsCtrlOut;
- needsCtrlIn = needsCtrlOut = false;
-
- aIns = (fEffect->numInputs > 0) ? static_cast<uint32_t>(fEffect->numInputs) : 0;
- aOuts = (fEffect->numOutputs > 0) ? static_cast<uint32_t>(fEffect->numOutputs) : 0;
- params = (fEffect->numParams > 0) ? static_cast<uint32_t>(fEffect->numParams) : 0;
-
- if (hasMidiInput())
- {
- mIns = 1;
- needsCtrlIn = true;
- }
- else
- mIns = 0;
-
- if (hasMidiOutput())
- {
- mOuts = 1;
- needsCtrlOut = true;
- }
- else
- mOuts = 0;
-
- 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<int32_t>(j);
- pData->param.data[j].type = PARAMETER_INPUT;
- pData->param.data[j].index = ij;
- pData->param.data[j].rindex = ij;
-
- float min, max, def, step, stepSmall, stepLarge;
-
- VstParameterProperties prop;
- carla_zeroStruct(prop);
-
- if (pData->hints & PLUGIN_HAS_COCKOS_EXTENSIONS)
- {
- double vrange[2] = { 0.0, 1.0 };
- bool isInteger = false;
-
- if (static_cast<uintptr_t>(dispatcher(effVendorSpecific, static_cast<int32_t>(0xdeadbef0), ij, vrange, 0.0f)) >= 0xbeef)
- {
- min = static_cast<float>(vrange[0]);
- max = static_cast<float>(vrange[1]);
-
- if (min > max)
- {
- carla_stderr2("WARNING - Broken plugin parameter min > max (with cockos extensions)");
- min = max - 0.1f;
- }
- else if (carla_isEqual(min, max))
- {
- carla_stderr2("WARNING - Broken plugin parameter min == max (with cockos extensions)");
- max = min + 0.1f;
- }
-
- // only use values as integer if we have a proper range
- if (max - min >= 1.0f)
- isInteger = dispatcher(effVendorSpecific, kVstParameterUsesIntStep, ij, nullptr, 0.0f) >= 0xbeef;
- }
- else
- {
- min = 0.0f;
- max = 1.0f;
- }
-
- if (isInteger)
- {
- step = 1.0f;
- stepSmall = 1.0f;
- stepLarge = 10.0f;
- }
- else
- {
- const float range = max - min;
- step = range/100.0f;
- stepSmall = range/1000.0f;
- stepLarge = range/10.0f;
- }
- }
- else if (dispatcher(effGetParameterProperties, ij, 0, &prop, 0) == 1)
- {
- #if 0
- if (prop.flags & kVstParameterUsesIntegerMinMax)
- {
- min = static_cast<float>(prop.minInteger);
- max = static_cast<float>(prop.maxInteger);
-
- if (min > max)
- {
- carla_stderr2("WARNING - Broken plugin parameter min > max");
- min = max - 0.1f;
- }
- else if (carla_isEqual(min, max))
- {
- carla_stderr2("WARNING - Broken plugin parameter min == max");
- max = min + 0.1f;
- }
- }
- else
- #endif
- {
- min = 0.0f;
- max = 1.0f;
- }
-
- 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);
- pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
- }
- 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 & kVstParameterCanRamp)
- pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
- }
- else
- {
- min = 0.0f;
- max = 1.0f;
- step = 0.001f;
- stepSmall = 0.0001f;
- stepLarge = 0.1f;
- }
-
- pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
- pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
-
- if ((pData->hints & PLUGIN_USES_OLD_VSTSDK) != 0 || dispatcher(effCanBeAutomated, ij, 0, nullptr, 0.0f) == 1)
- pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
-
- // no such thing as VST default parameters
- def = fEffect->getParameter(fEffect, ij);
-
- 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);
- }
-
- 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
- const intptr_t vstCategory = dispatcher(effGetPlugCategory, 0, 0, nullptr, 0.0f);
-
- pData->hints = 0x0;
-
- if (vstCategory == kPlugCategSynth || vstCategory == kPlugCategGenerator)
- pData->hints |= PLUGIN_IS_SYNTH;
-
- if (fEffect->flags & effFlagsHasEditor)
- {
- pData->hints |= PLUGIN_HAS_CUSTOM_UI;
- pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
- }
-
- if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
- pData->hints |= PLUGIN_USES_OLD_VSTSDK;
-
- if ((fEffect->flags & effFlagsCanReplacing) != 0 && fEffect->processReplacing != fEffect->process)
- pData->hints |= PLUGIN_CAN_PROCESS_REPLACING;
-
- if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>("hasCockosExtensions"), 0.0f)) == 0xbeef0000)
- pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
-
- 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;
-
- if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
- pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
-
- // dummy pre-start to get latency and wantEvents() on old plugins
- {
- activate();
- deactivate();
- }
-
- // check initial latency
- char* const empty3Ptr = &fEffect->empty3[0];
- int32_t initialDelay = *(int32_t*)empty3Ptr;
- const uint32_t latency = (initialDelay > 0) ? static_cast<uint32_t>(initialDelay) : 0;
-
- if (latency != 0)
- {
- pData->client->setLatency(latency);
- #ifndef BUILD_BRIDGE
- pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
- #endif
- }
-
- //bufferSizeChanged(pData->engine->getBufferSize());
- reloadPrograms(true);
-
- if (pData->active)
- activate();
-
- carla_debug("CarlaPluginVST2::reload() - end");
- }
-
- void reloadPrograms(const bool doInit) override
- {
- carla_debug("CarlaPluginVST2::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
- uint32_t newCount = (fEffect->numPrograms > 0) ? static_cast<uint32_t>(fEffect->numPrograms) : 0;
-
- if (newCount > 0)
- {
- pData->prog.createNew(newCount);
-
- // Update names
- for (int32_t i=0; i < fEffect->numPrograms; ++i)
- {
- char strBuf[STR_MAX+1] = { '\0' };
- if (dispatcher(effGetProgramNameIndexed, i, 0, strBuf, 0.0f) != 1)
- {
- // program will be [re-]changed later
- dispatcher(effSetProgram, 0, i, nullptr, 0.0f);
- dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
- }
- pData->prog.names[i] = carla_strdup(strBuf);
- }
- }
-
- #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
- // Update OSC Names
- if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
- {
- pData->engine->oscSend_control_set_program_count(pData->id, newCount);
-
- for (uint32_t i=0; i < newCount; ++i)
- pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
- }
- #endif
-
- if (doInit)
- {
- if (newCount > 0)
- setProgram(0, false, false, false, true);
- else
- dispatcher(effSetProgram, 0, 0, nullptr, 0.0f);
- }
- 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)
- dispatcher(effSetProgram, 0, pData->prog.current, nullptr, 0.0f);
- }
-
- pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
- }
- }
-
- // -------------------------------------------------------------------
- // Plugin processing
-
- void activate() noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
-
- dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
-
- dispatcher(effSetBlockSizeAndSampleRate, 0,
- static_cast<int32_t>(pData->engine->getBufferSize()), nullptr,
- static_cast<float>(pData->engine->getSampleRate()));
-
- dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
- dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
-
- try {
- dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
- } catch(...) {}
-
- try {
- dispatcher(effStartProcess, 0, 0, nullptr, 0.0f);
- } catch(...) {}
- }
-
- void deactivate() noexcept override
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
-
- try {
- dispatcher(effStopProcess, 0, 0, nullptr, 0.0f);
- } catch(...) {}
-
- try {
- dispatcher(effMainsChanged, 0, 0, nullptr, 0.0f);
- } catch(...) {}
- }
-
- void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
- {
- fProcThread = pthread_self();
-
- // --------------------------------------------------------------------------------------------------------
- // 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;
- }
-
- fMidiEventCount = 0;
- carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
-
- // --------------------------------------------------------------------------------------------------------
- // Check if needs reset
-
- if (pData->needsReset)
- {
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- fMidiEventCount = MAX_MIDI_CHANNELS*2;
-
- for (uint8_t i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
- {
- fMidiEvents[k].type = kVstMidiType;
- fMidiEvents[k].byteSize = kVstMidiEventSize;
- fMidiEvents[k].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
- fMidiEvents[k].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
-
- fMidiEvents[k+i].type = kVstMidiType;
- fMidiEvents[k+i].byteSize = kVstMidiEventSize;
- fMidiEvents[k+i].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
- fMidiEvents[k+i].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
- }
- }
- else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
- {
- fMidiEventCount = MAX_MIDI_NOTE;
-
- for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
- {
- fMidiEvents[i].type = kVstMidiType;
- fMidiEvents[i].byteSize = kVstMidiEventSize;
- fMidiEvents[i].midiData[0] = char(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
- fMidiEvents[i].midiData[1] = char(i);
- }
- }
-
- pData->needsReset = false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Set TimeInfo
-
- const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
-
- fTimeInfo.flags = kVstTransportChanged;
-
- if (timeInfo.playing)
- fTimeInfo.flags |= kVstTransportPlaying;
-
- fTimeInfo.samplePos = double(timeInfo.frame);
- fTimeInfo.sampleRate = pData->engine->getSampleRate();
-
- if (timeInfo.usecs != 0)
- {
- fTimeInfo.nanoSeconds = double(timeInfo.usecs)/1000.0;
- fTimeInfo.flags |= kVstNanosValid;
- }
-
- 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 = double(timeInfo.bbt.bar - 1) * timeInfo.bbt.beatsPerBar;
- const double ppqBeat = double(timeInfo.bbt.beat - 1);
- const double ppqTick = double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat;
-
- // PPQ Pos
- fTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
- fTimeInfo.flags |= kVstPpqPosValid;
-
- // Tempo
- fTimeInfo.tempo = timeInfo.bbt.beatsPerMinute;
- fTimeInfo.flags |= kVstTempoValid;
-
- // Bars
- fTimeInfo.barStartPos = ppqBar;
- fTimeInfo.flags |= kVstBarsValid;
-
- // Time Signature
- fTimeInfo.timeSigNumerator = static_cast<int32_t>(timeInfo.bbt.beatsPerBar);
- fTimeInfo.timeSigDenominator = static_cast<int32_t>(timeInfo.bbt.beatType);
- fTimeInfo.flags |= kVstTimeSigValid;
- }
- else
- {
- // Tempo
- fTimeInfo.tempo = 120.0;
- fTimeInfo.flags |= kVstTempoValid;
-
- // Time Signature
- fTimeInfo.timeSigNumerator = 4;
- fTimeInfo.timeSigDenominator = 4;
- fTimeInfo.flags |= kVstTimeSigValid;
-
- // Missing info
- fTimeInfo.ppqPos = 0.0;
- fTimeInfo.barStartPos = 0.0;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Event Input and Processing
-
- if (pData->event.portIn != nullptr)
- {
- // ----------------------------------------------------------------------------------------------------
- // MIDI Input (External)
-
- if (pData->extNotes.mutex.tryLock())
- {
- ExternalMidiNote note = { 0, 0, 0 };
-
- for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
- {
- note = pData->extNotes.data.getFirst(note, true);
-
- CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
-
- VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
-
- vstMidiEvent.type = kVstMidiType;
- vstMidiEvent.byteSize = kVstMidiEventSize;
- vstMidiEvent.midiData[0] = char((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
- vstMidiEvent.midiData[1] = char(note.note);
- vstMidiEvent.midiData[2] = char(note.velo);
- }
-
- pData->extNotes.mutex.unlock();
-
- } // End of MIDI Input (External)
-
- // ----------------------------------------------------------------------------------------------------
- // Event Input (System)
-
- #ifndef BUILD_BRIDGE
- bool allNotesOffSent = false;
- #endif
- bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
-
- uint32_t startTime = 0;
- uint32_t timeOffset = 0;
-
- for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
- {
- const EngineEvent& event(pData->event.portIn->getEvent(i));
-
- if (event.time >= frames)
- continue;
-
- CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
-
- if (isSampleAccurate && event.time > timeOffset)
- {
- if (processSingle(audioIn, audioOut, event.time - timeOffset, timeOffset))
- {
- startTime = 0;
- timeOffset = event.time;
-
- if (fMidiEventCount > 0)
- {
- carla_zeroStructs(fMidiEvents, fMidiEventCount);
- fMidiEventCount = 0;
- }
- }
- else
- startTime += timeOffset;
- }
-
- switch (event.type)
- {
- case kEngineEventTypeNull:
- break;
-
- case kEngineEventTypeControl: {
- const EngineControlEvent& ctrlEvent(event.ctrl);
-
- switch (ctrlEvent.type)
- {
- case kEngineControlEventTypeNull:
- break;
-
- case kEngineControlEventTypeParameter: {
- #ifndef BUILD_BRIDGE
- // Control backend stuff
- if (event.channel == pData->ctrlChannel)
- {
- float value;
-
- if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
- {
- value = ctrlEvent.value;
- setDryWetRT(value);
- }
-
- if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
- {
- value = ctrlEvent.value*127.0f/100.0f;
- setVolumeRT(value);
- }
-
- if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
- {
- float left, right;
- value = ctrlEvent.value/0.5f - 1.0f;
-
- if (value < 0.0f)
- {
- left = -1.0f;
- right = (value*2.0f)+1.0f;
- }
- else if (value > 0.0f)
- {
- left = (value*2.0f)-1.0f;
- right = 1.0f;
- }
- else
- {
- left = -1.0f;
- right = 1.0f;
- }
-
- setBalanceLeftRT(left);
- setBalanceRightRT(right);
- }
- }
- #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].midiCC != ctrlEvent.param)
- continue;
- if (pData->param.data[k].type != PARAMETER_INPUT)
- continue;
- if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
- continue;
-
- float value;
-
- if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
- {
- value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
- }
- else
- {
- if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
- value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
- else
- value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
-
- if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
- value = std::rint(value);
- }
-
- setParameterValueRT(k, value);
- }
-
- if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
- {
- if (fMidiEventCount >= kPluginMaxMidiEvents*2)
- continue;
-
- VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
- carla_zeroStruct(vstMidiEvent);
-
- vstMidiEvent.type = kVstMidiType;
- vstMidiEvent.byteSize = kVstMidiEventSize;
- vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
- vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- vstMidiEvent.midiData[1] = char(ctrlEvent.param);
- vstMidiEvent.midiData[2] = char(ctrlEvent.value*127.0f);
- }
-
- break;
- } // case kEngineControlEventTypeParameter
-
- case kEngineControlEventTypeMidiBank:
- if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
- {
- if (fMidiEventCount >= kPluginMaxMidiEvents*2)
- continue;
-
- VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
- carla_zeroStruct(vstMidiEvent);
-
- vstMidiEvent.type = kVstMidiType;
- vstMidiEvent.byteSize = kVstMidiEventSize;
- vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
- vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- vstMidiEvent.midiData[1] = MIDI_CONTROL_BANK_SELECT;
- vstMidiEvent.midiData[2] = char(ctrlEvent.param);
- }
- 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);
- break;
- }
- }
- else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
- {
- if (fMidiEventCount >= kPluginMaxMidiEvents*2)
- continue;
-
- VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
- carla_zeroStruct(vstMidiEvent);
-
- vstMidiEvent.type = kVstMidiType;
- vstMidiEvent.byteSize = kVstMidiEventSize;
- vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
- vstMidiEvent.midiData[0] = char(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- vstMidiEvent.midiData[1] = char(ctrlEvent.param);
- }
- break;
-
- case kEngineControlEventTypeAllSoundOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- if (fMidiEventCount >= kPluginMaxMidiEvents*2)
- continue;
-
- VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
- carla_zeroStruct(vstMidiEvent);
-
- vstMidiEvent.type = kVstMidiType;
- vstMidiEvent.byteSize = kVstMidiEventSize;
- vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
- vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
- }
- break;
-
- case kEngineControlEventTypeAllNotesOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- #ifndef BUILD_BRIDGE
- if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
- {
- allNotesOffSent = true;
- sendMidiAllNotesOffToCallback();
- }
- #endif
-
- if (fMidiEventCount >= kPluginMaxMidiEvents*2)
- continue;
-
- VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
- carla_zeroStruct(vstMidiEvent);
-
- vstMidiEvent.type = kVstMidiType;
- vstMidiEvent.byteSize = kVstMidiEventSize;
- vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
- vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
- vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
- }
- break;
- } // switch (ctrlEvent.type)
- break;
- } // case kEngineEventTypeControl
-
- case kEngineEventTypeMidi: {
- if (fMidiEventCount >= kPluginMaxMidiEvents*2)
- continue;
-
- const EngineMidiEvent& midiEvent(event.midi);
-
- if (midiEvent.size > 3)
- continue;
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- static_assert(3 <= EngineMidiEvent::kDataSize, "Incorrect data");
- #endif
-
- uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
-
- 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 && midiEvent.data[2] == 0)
- status = MIDI_STATUS_NOTE_OFF;
-
- VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
- carla_zeroStruct(vstMidiEvent);
-
- vstMidiEvent.type = kVstMidiType;
- vstMidiEvent.byteSize = kVstMidiEventSize;
- vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
- vstMidiEvent.midiData[0] = char(status | (event.channel & MIDI_CHANNEL_BIT));
- vstMidiEvent.midiData[1] = char(midiEvent.size >= 2 ? midiEvent.data[1] : 0);
- vstMidiEvent.midiData[2] = char(midiEvent.size >= 3 ? midiEvent.data[2] : 0);
-
- if (status == MIDI_STATUS_NOTE_ON)
- pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
- else if (status == MIDI_STATUS_NOTE_OFF)
- pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
- } break;
- } // switch (event.type)
- }
-
- pData->postRtEvents.trySplice();
-
- if (frames > timeOffset)
- processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
-
- } // End of Event Input and Processing
-
- // --------------------------------------------------------------------------------------------------------
- // Plugin processing (no events)
-
- else
- {
- processSingle(audioIn, audioOut, frames, 0);
-
- } // End of Plugin processing (no events)
-
- // --------------------------------------------------------------------------------------------------------
- // MIDI Output
-
- if (pData->event.portOut != nullptr)
- {
- // reverse lookup MIDI events
- for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
- {
- if (fMidiEvents[k].type == 0)
- break;
-
- const VstMidiEvent& vstMidiEvent(fMidiEvents[k]);
-
- CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.deltaFrames >= 0);
- CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.midiData[0] != 0);
-
- uint8_t midiData[3];
- midiData[0] = static_cast<uint8_t>(vstMidiEvent.midiData[0]);
- midiData[1] = static_cast<uint8_t>(vstMidiEvent.midiData[1]);
- midiData[2] = static_cast<uint8_t>(vstMidiEvent.midiData[2]);
-
- if (! pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(vstMidiEvent.deltaFrames), 3, midiData))
- break;
- }
-
- } // End of MIDI Output
- }
-
- bool processSingle(const float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
- {
- 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
-
- #ifndef STOAT_TEST_BUILD
- if (pData->engine->isOffline())
- {
- pData->singleMutex.lock();
- }
- else
- #endif
- if (! pData->singleMutex.tryLock())
- {
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- {
- for (uint32_t k=0; k < frames; ++k)
- outBuffer[i][k+timeOffset] = 0.0f;
- }
-
- return false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Set audio buffers
-
- float* vstInBuffer[pData->audioIn.count];
- float* vstOutBuffer[pData->audioOut.count];
-
- for (uint32_t i=0; i < pData->audioIn.count; ++i)
- vstInBuffer[i] = const_cast<float*>(inBuffer[i]+timeOffset);
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- vstOutBuffer[i] = outBuffer[i]+timeOffset;
-
- // --------------------------------------------------------------------------------------------------------
- // Set MIDI events
-
- if (fMidiEventCount > 0)
- {
- fEvents.numEvents = static_cast<int32_t>(fMidiEventCount);
- fEvents.reserved = 0;
- dispatcher(effProcessEvents, 0, 0, &fEvents, 0.0f);
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Run plugin
-
- fIsProcessing = true;
-
- if (pData->hints & PLUGIN_CAN_PROCESS_REPLACING)
- {
- fEffect->processReplacing(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
- }
- else
- {
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- carla_zeroFloats(vstOutBuffer[i], frames);
-
- #if ! VST_FORCE_DEPRECATED
- fEffect->process(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
- #endif
- }
-
- fIsProcessing = false;
-
- fTimeInfo.samplePos += frames;
-
- #ifndef BUILD_BRIDGE
- // --------------------------------------------------------------------------------------------------------
- // 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));
-
- bool isPair;
- float bufValue, oldBufLeft[doBalance ? frames : 1];
-
- for (uint32_t i=0; i < pData->audioOut.count; ++i)
- {
- // Dry/Wet
- if (doDryWet)
- {
- for (uint32_t k=0; k < frames; ++k)
- {
- bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k+timeOffset];
- outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * 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]+timeOffset, 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+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
- outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
- }
- else
- {
- // right
- outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
- outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
- }
- }
- }
-
- // Volume
- if (doVolume)
- {
- for (uint32_t k=0; k < frames; ++k)
- outBuffer[i][k+timeOffset] *= pData->postProc.volume;
- }
- }
-
- } // End of Post-processing
- #endif
-
- // --------------------------------------------------------------------------------------------------------
-
- pData->singleMutex.unlock();
- return true;
- }
-
- void bufferSizeChanged(const uint32_t newBufferSize) override
- {
- CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
- carla_debug("CarlaPluginVST2::bufferSizeChanged(%i)", newBufferSize);
-
- if (pData->active)
- deactivate();
-
- #if ! VST_FORCE_DEPRECATED
- dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(newBufferSize), nullptr, static_cast<float>(pData->engine->getSampleRate()));
- #endif
- dispatcher(effSetBlockSize, 0, static_cast<int32_t>(newBufferSize), nullptr, 0.0f);
-
- if (pData->active)
- activate();
- }
-
- void sampleRateChanged(const double newSampleRate) override
- {
- CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
- carla_debug("CarlaPluginVST2::sampleRateChanged(%g)", newSampleRate);
-
- if (pData->active)
- deactivate();
-
- #if ! VST_FORCE_DEPRECATED
- dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(newSampleRate));
- #endif
- dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(newSampleRate));
-
- if (pData->active)
- activate();
- }
-
- // -------------------------------------------------------------------
- // Plugin buffers
-
- // nothing
-
- // -------------------------------------------------------------------
- // Post-poned UI Stuff
-
- // nothing
-
- // -------------------------------------------------------------------
-
- protected:
- void handlePluginUIClosed() override
- {
- CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
- carla_debug("CarlaPluginVST2::handlePluginUIClosed()");
-
- showCustomUI(false);
- pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
- }
-
- void handlePluginUIResized(const uint width, const uint height) override
- {
- CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
- carla_debug("CarlaPluginVST2::handlePluginUIResized(%u, %u)", width, height);
-
- return; // unused
- (void)width; (void)height;
- }
-
- // -------------------------------------------------------------------
-
- intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt) const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
- #ifdef DEBUG
- if (opcode != effIdle && opcode != effEditIdle && opcode != effProcessEvents)
- carla_debug("CarlaPluginVST2::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
- #endif
-
- try {
- return fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt);
- } CARLA_SAFE_EXCEPTION_RETURN("Vst dispatcher", 0);
- }
-
- intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
- {
- #ifdef DEBUG
- if (opcode != audioMasterGetTime)
- carla_debug("CarlaPluginVST2::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
- #endif
-
- intptr_t ret = 0;
-
- switch (opcode)
- {
- case audioMasterAutomate: {
- CARLA_SAFE_ASSERT_BREAK(pData->enabled);
-
- // plugins should never do this:
- CARLA_SAFE_ASSERT_INT(index >= 0 && index < static_cast<int32_t>(pData->param.count), index);
-
- if (index < 0 || index >= static_cast<int32_t>(pData->param.count))
- break;
-
- const uint32_t uindex(static_cast<uint32_t>(index));
- const float fixedValue(pData->param.getFixedValue(uindex, opt));
-
- // Called from plugin process thread, nasty!
- if (pthread_equal(pthread_self(), fProcThread))
- {
- CARLA_SAFE_ASSERT(fIsProcessing);
-
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
- }
- // Called from UI
- else if (fUI.isVisible)
- {
- CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
- }
- // Unknown
- // TODO - check if plugin or UI is initializing
- else
- {
- carla_stdout("audioMasterAutomate called from unknown source");
-
- setParameterValue(uindex, fixedValue, true, true, true);
- //pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
- }
- break;
- }
-
- case audioMasterCurrentId:
- ret = fEffect->uniqueID;
- break;
-
- case audioMasterIdle:
- if (pthread_equal(pthread_self(), fMainThread))
- {
- pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
-
- if (pData->engine->getType() != kEngineTypePlugin)
- pData->engine->idle();
- }
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterPinConnected:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
-
- case audioMasterWantMidi:
- // Deprecated in VST SDK 2.4
- pData->hints |= PLUGIN_WANTS_MIDI_INPUT;
- break;
- #endif
-
- case audioMasterGetTime:
- ret = (intptr_t)&fTimeInfo;
- break;
-
- case audioMasterProcessEvents:
- CARLA_SAFE_ASSERT_RETURN(pData->enabled, 0);
- CARLA_SAFE_ASSERT_RETURN(fIsProcessing, 0);
- CARLA_SAFE_ASSERT_RETURN(pData->event.portOut != nullptr, 0);
-
- if (fMidiEventCount >= kPluginMaxMidiEvents*2-1)
- return 0;
-
- if (const VstEvents* const vstEvents = (const VstEvents*)ptr)
- {
- for (int32_t i=0; i < vstEvents->numEvents && i < kPluginMaxMidiEvents*2; ++i)
- {
- if (vstEvents->events[i] == nullptr)
- break;
-
- const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)vstEvents->events[i]);
-
- if (vstMidiEvent->type != kVstMidiType)
- continue;
-
- // reverse-find first free event, and put it there
- for (uint32_t j=(kPluginMaxMidiEvents*2)-1; j >= fMidiEventCount; --j)
- {
- if (fMidiEvents[j].type == 0)
- {
- std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
- break;
- }
- }
- }
- }
- ret = 1;
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterSetTime:
- // Deprecated in VST SDK 2.4
- break;
-
- case audioMasterTempoAt:
- // Deprecated in VST SDK 2.4
- ret = static_cast<intptr_t>(fTimeInfo.tempo * 10000);
- break;
-
- case audioMasterGetNumAutomatableParameters:
- // Deprecated in VST SDK 2.4
- ret = static_cast<intptr_t>(pData->engine->getOptions().maxParameters);
- ret = carla_minPositive<intptr_t>(ret, fEffect->numParams);
- break;
-
- case audioMasterGetParameterQuantization:
- // Deprecated in VST SDK 2.4
- ret = 1; // full single float precision
- break;
- #endif
-
- #if 0
- case audioMasterIOChanged:
- CARLA_ASSERT(pData->enabled);
-
- // TESTING
-
- if (! pData->enabled)
- {
- ret = 1;
- break;
- }
-
- if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
- {
- carla_stderr2("CarlaPluginVST2::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);
-
- ret = 1;
- break;
- #endif
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterNeedIdle:
- // Deprecated in VST SDK 2.4
- fNeedIdle = true;
- ret = 1;
- break;
- #endif
-
- case audioMasterSizeWindow:
- CARLA_SAFE_ASSERT_BREAK(fUI.window != nullptr);
- CARLA_SAFE_ASSERT_BREAK(index > 0);
- CARLA_SAFE_ASSERT_BREAK(value > 0);
- fUI.window->setSize(static_cast<uint>(index), static_cast<uint>(value), true);
- ret = 1;
- break;
-
- case audioMasterGetSampleRate:
- ret = static_cast<intptr_t>(pData->engine->getSampleRate());
- break;
-
- case audioMasterGetBlockSize:
- ret = static_cast<intptr_t>(pData->engine->getBufferSize());
- 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 (pthread_equal(pthread_self(), fProcThread))
- {
- CARLA_SAFE_ASSERT(fIsProcessing);
-
- if (pData->engine->isOffline())
- ret = kVstProcessLevelOffline;
- else
- ret = kVstProcessLevelRealtime;
- }
- else
- {
- ret = kVstProcessLevelUser;
- }
- break;
-
- case audioMasterGetAutomationState:
- ret = pData->active ? kVstAutomationReadWrite : kVstAutomationOff;
- 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 audioMasterVendorSpecific:
- // TODO - cockos extensions
- break;
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterSetIcon:
- // Deprecated in VST SDK 2.4
- break;
- #endif
-
- #if ! VST_FORCE_DEPRECATED
- case audioMasterOpenWindow:
- case audioMasterCloseWindow:
- // Deprecated in VST SDK 2.4
- // TODO
- break;
- #endif
-
- case audioMasterGetDirectory:
- // TODO
- break;
-
- case audioMasterUpdateDisplay:
- // Idle UI if visible
- if (fUI.isVisible)
- dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
-
- // Update current program
- if (pData->prog.count > 0)
- {
- const int32_t current = static_cast<int32_t>(dispatcher(effGetProgram, 0, 0, nullptr, 0.0f));
-
- if (current >= 0 && current < static_cast<int32_t>(pData->prog.count))
- {
- char strBuf[STR_MAX+1] = { '\0' };
- dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
-
- if (pData->prog.names[current] != nullptr)
- delete[] pData->prog.names[current];
-
- pData->prog.names[current] = carla_strdup(strBuf);
-
- if (pData->prog.current != current)
- {
- pData->prog.current = current;
- pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, current, 0, 0.0f, nullptr);
- }
- }
- }
-
- pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
- 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:
- carla_debug("CarlaPluginVST2::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f) UNDEF", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
- break;
- }
-
- return ret;
-
- // unused
- (void)opt;
- }
-
- bool canDo(const char* const feature) const noexcept
- {
- try {
- return (fEffect->dispatcher(fEffect, effCanDo, 0, 0, const_cast<char*>(feature), 0.0f) == 1);
- } CARLA_SAFE_EXCEPTION_RETURN("vstPluginCanDo", false);
- }
-
- bool hasMidiInput() const noexcept
- {
- return (canDo("receiveVstEvents") || canDo("receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (pData->hints & PLUGIN_WANTS_MIDI_INPUT));
- }
-
- bool hasMidiOutput() const noexcept
- {
- return (canDo("sendVstEvents") || canDo("sendVstMidiEvent"));
- }
-
- // -------------------------------------------------------------------
-
- const void* getNativeDescriptor() const noexcept override
- {
- return fEffect;
- }
-
- // -------------------------------------------------------------------
-
- public:
- bool init(const char* const filename, const char* const name, const int64_t uniqueId, const uint options)
- {
- 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 (filename == nullptr || filename[0] == '\0')
- {
- pData->engine->setLastError("null filename");
- return false;
- }
-
- // ---------------------------------------------------------------
-
- VST_Function vstFn;
-
- #ifdef CARLA_OS_MAC
- CarlaString filenameCheck(filename);
- filenameCheck.toLower();
-
- if (filenameCheck.endsWith(".vst") || filenameCheck.endsWith(".vst/"))
- {
- // FIXME assert returns, set engine error
- const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)strlen(filename), true);
- CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
-
- fMacBundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
- CFRelease(urlRef);
- CARLA_SAFE_ASSERT_RETURN(fMacBundleRef != nullptr, false);
-
- if (! CFBundleLoadExecutable(fMacBundleRef))
- {
- CFRelease(fMacBundleRef);
- pData->engine->setLastError("Failed to load VST bundle executable");
- return false;
- }
-
- vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("main_macho"));
-
- if (vstFn == nullptr)
- vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("VSTPluginMain"));
-
- if (vstFn == nullptr)
- {
- CFBundleUnloadExecutable(fMacBundleRef);
- CFRelease(fMacBundleRef);
- pData->engine->setLastError("Not a VST plugin");
- return false;
- }
-
- fMacBundleRefNum = CFBundleOpenBundleResourceMap(fMacBundleRef);
- }
- else
- #endif
- {
- // -----------------------------------------------------------
- // open DLL
-
- if (! pData->libOpen(filename))
- {
- pData->engine->setLastError(pData->libError(filename));
- return false;
- }
-
- // -----------------------------------------------------------
- // get DLL main entry
-
- vstFn = pData->libSymbol<VST_Function>("VSTPluginMain");
-
- if (vstFn == nullptr)
- {
- vstFn = pData->libSymbol<VST_Function>("main");
-
- if (vstFn == nullptr)
- {
- pData->engine->setLastError("Could not find the VST main entry in the plugin library");
- return false;
- }
- }
- }
-
- // ---------------------------------------------------------------
- // initialize plugin (part 1)
-
- sCurrentUniqueId = uniqueId;
- sLastCarlaPluginVST2 = this;
-
- try {
- fEffect = vstFn(carla_vst_audioMasterCallback);
- } CARLA_SAFE_EXCEPTION_RETURN("Vst init", false);
-
- sLastCarlaPluginVST2 = nullptr;
- sCurrentUniqueId = 0;
-
- if (fEffect == nullptr)
- {
- pData->engine->setLastError("Plugin failed to initialize");
- return false;
- }
-
- if (fEffect->magic != kEffectMagic)
- {
- pData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
- return false;
- }
-
- fEffect->ptr1 = this;
-
- dispatcher(effIdentify, 0, 0, 0, 0);
-
- dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
-
- dispatcher(effSetBlockSizeAndSampleRate, 0,
- static_cast<int32_t>(pData->engine->getBufferSize()), nullptr,
- static_cast<float>(pData->engine->getSampleRate()));
-
- dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
- dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
-
- dispatcher(effOpen, 0, 0, nullptr, 0.0f);
-
- // ---------------------------------------------------------------
- // get info
-
- if (name != nullptr && name[0] != '\0')
- {
- pData->name = pData->engine->getUniquePluginName(name);
- }
- else
- {
- char strBuf[STR_MAX+1];
- carla_zeroChars(strBuf, STR_MAX+1);
- dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
-
- if (strBuf[0] != '\0')
- pData->name = pData->engine->getUniquePluginName(strBuf);
- else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
- pData->name = pData->engine->getUniquePluginName(shortname+1);
- else
- pData->name = pData->engine->getUniquePluginName("unknown");
- }
-
- pData->filename = carla_strdup(filename);
-
- // ---------------------------------------------------------------
- // register client
-
- pData->client = pData->engine->addClient(this);
-
- if (pData->client == nullptr || ! pData->client->isOk())
- {
- pData->engine->setLastError("Failed to register plugin client");
- return false;
- }
-
- // ---------------------------------------------------------------
- // initialize plugin (part 2)
-
- for (int i = fEffect->numInputs; --i >= 0;) dispatcher(effConnectInput, i, 1, 0, 0);
- for (int i = fEffect->numOutputs; --i >= 0;) dispatcher(effConnectOutput, i, 1, 0, 0);
-
- if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
- pData->hints |= PLUGIN_USES_OLD_VSTSDK;
-
- static const char kHasCockosExtensions[] = "hasCockosExtensions";
-
- if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>(kHasCockosExtensions), 0.0f)) == 0xbeef0000)
- pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
-
- // ---------------------------------------------------------------
- // set default options
-
- pData->options = 0x0;
-
- if (pData->latency.frames != 0 || hasMidiOutput())
- pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
- else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
- pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
-
- if (fEffect->numPrograms > 1)
- pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
-
- if (fEffect->flags & effFlagsProgramChunks)
- pData->options |= PLUGIN_OPTION_USE_CHUNKS;
-
- if (hasMidiInput())
- {
- pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
- pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
- pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
- pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
-
- if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
- pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
- if (options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
- pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
- }
-
- return true;
-
- // unused
- (void)uniqueId;
- }
-
- private:
- int fUnique1;
-
- AEffect* fEffect;
-
- uint32_t fMidiEventCount;
- VstMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
- VstTimeInfo fTimeInfo;
-
- bool fNeedIdle;
- void* fLastChunk;
-
- bool fIsProcessing;
- pthread_t fMainThread;
- pthread_t fProcThread;
-
- #ifdef CARLA_OS_MAC
- CFBundleRef fMacBundleRef;
- CFBundleRefNum fMacBundleRefNum;
- #endif
-
- struct FixedVstEvents {
- int32_t numEvents;
- intptr_t reserved;
- VstEvent* data[kPluginMaxMidiEvents*2];
-
- FixedVstEvents() noexcept
- : numEvents(0),
- reserved(0)
- {
- carla_zeroPointers(data, kPluginMaxMidiEvents*2);
- }
-
- CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
- } fEvents;
-
- struct UI {
- bool isVisible;
- CarlaPluginUI* window;
-
- UI() noexcept
- : isVisible(false),
- window(nullptr) {}
-
- ~UI()
- {
- CARLA_ASSERT(! isVisible);
-
- if (window != nullptr)
- {
- delete window;
- window = nullptr;
- }
- }
-
- CARLA_DECLARE_NON_COPY_STRUCT(UI);
- } fUI;
-
- int fUnique2;
-
- static intptr_t sCurrentUniqueId;
- static CarlaPluginVST2* sLastCarlaPluginVST2;
-
- // -------------------------------------------------------------------
-
- static bool compareMagic (int32_t magic, const char* name) noexcept
- {
- return magic == (int32_t)ByteOrder::littleEndianInt (name)
- || magic == (int32_t)ByteOrder::bigEndianInt (name);
- }
-
- static int32_t fxbSwap(const int32_t x) noexcept
- {
- return (int32_t)ByteOrder::swapIfLittleEndian ((uint32_t) x);
- }
-
- bool loadOldSaveFormat(const void* const data, const std::size_t dataSize)
- {
- if (dataSize < 28)
- return false;
-
- const int32_t* const set = (const int32_t*)data;
-
- if (! compareMagic(set[0], "CcnK"))
- return false;
- if (! compareMagic(set[2], "FBCh"))
- return false;
- if (fxbSwap(set[3]) > 1)
- return false;
-
- const int32_t chunkSize = fxbSwap(set[39]);
- CARLA_SAFE_ASSERT_RETURN(chunkSize > 0, false);
-
- if (static_cast<const std::size_t>(chunkSize + 160) > dataSize)
- return false;
-
- setChunkData(&set[40], static_cast<const std::size_t>(chunkSize));
- return true;
- }
-
- static intptr_t carla_vst_hostCanDo(const char* const feature)
- {
- carla_debug("carla_vst_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)
- 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;
- if (std::strcmp(feature, "NIMKPIVendorSpecificCallbacks") == 0)
- return -1;
-
- // unimplemented
- carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
- return 0;
- }
-
- static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
- {
- #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
- if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
- carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
- #endif
-
- switch (opcode)
- {
- case audioMasterVersion:
- return kVstVersion;
-
- case audioMasterCurrentId:
- if (sCurrentUniqueId != 0)
- return sCurrentUniqueId;
- break;
-
- case audioMasterGetVendorString:
- CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
- std::strcpy((char*)ptr, "falkTX");
- return 1;
-
- case audioMasterGetProductString:
- CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
- std::strcpy((char*)ptr, "Carla");
- return 1;
-
- case audioMasterGetVendorVersion:
- return CARLA_VERSION_HEX;
-
- case audioMasterCanDo:
- CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
- return carla_vst_hostCanDo((const char*)ptr);
-
- case audioMasterGetLanguage:
- return kVstLangEnglish;
- }
-
- // Check if 'resvd1' points to us, otherwise register ourselfs if possible
- CarlaPluginVST2* self = nullptr;
-
- if (effect != nullptr)
- {
- if (effect->ptr1 != nullptr)
- {
- self = (CarlaPluginVST2*)effect->ptr1;
- if (self->fUnique1 != self->fUnique2)
- self = nullptr;
- }
-
- if (self != nullptr)
- {
- if (self->fEffect == nullptr)
- self->fEffect = effect;
-
- if (self->fEffect != effect)
- {
- carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
- self = nullptr;
- }
- }
- else if (sLastCarlaPluginVST2 != nullptr)
- {
- effect->ptr1 = sLastCarlaPluginVST2;
- self = sLastCarlaPluginVST2;
- }
- }
-
- return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
- }
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginVST2)
- };
-
- intptr_t CarlaPluginVST2::sCurrentUniqueId = 0;
- CarlaPluginVST2* CarlaPluginVST2::sLastCarlaPluginVST2 = nullptr;
-
- CARLA_BACKEND_END_NAMESPACE
-
- // -------------------------------------------------------------------------------------------------------------------
-
- CARLA_BACKEND_START_NAMESPACE
-
- CarlaPlugin* CarlaPlugin::newVST2(const Initializer& init)
- {
- carla_debug("CarlaPlugin::newVST2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.uniqueId);
-
- CarlaPluginVST2* const plugin(new CarlaPluginVST2(init.engine, init.id));
-
- if (! plugin->init(init.filename, init.name, init.uniqueId, init.options))
- {
- delete plugin;
- return nullptr;
- }
-
- return plugin;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- CARLA_BACKEND_END_NAMESPACE
|