Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VstPlugin.cpp 87KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  1. /*
  2. * Carla VST Plugin
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  20. # define USE_JUCE_FOR_VST
  21. #endif
  22. #ifndef USE_JUCE_FOR_VST
  23. #include "CarlaVstUtils.hpp"
  24. #include "CarlaMathUtils.hpp"
  25. #include "CarlaPluginUI.hpp"
  26. #include "juce_core.h"
  27. #include <pthread.h>
  28. #undef VST_FORCE_DEPRECATED
  29. #define VST_FORCE_DEPRECATED 0
  30. using juce::File;
  31. CARLA_BACKEND_START_NAMESPACE
  32. // -----------------------------------------------------
  33. const uint PLUGIN_CAN_PROCESS_REPLACING = 0x1000;
  34. const uint PLUGIN_HAS_COCKOS_EXTENSIONS = 0x2000;
  35. const uint PLUGIN_USES_OLD_VSTSDK = 0x4000;
  36. const uint PLUGIN_WANTS_MIDI_INPUT = 0x8000;
  37. static const int32_t kVstMidiEventSize = static_cast<int32_t>(sizeof(VstMidiEvent));
  38. // -----------------------------------------------------
  39. class VstPlugin : public CarlaPlugin,
  40. CarlaPluginUI::CloseCallback
  41. {
  42. public:
  43. VstPlugin(CarlaEngine* const engine, const uint id)
  44. : CarlaPlugin(engine, id),
  45. fUnique1(1),
  46. fEffect(nullptr),
  47. fMidiEventCount(0),
  48. fTimeInfo(),
  49. fNeedIdle(false),
  50. fLastChunk(nullptr),
  51. fIsProcessing(false),
  52. #ifdef PTW32_DLLPORT
  53. fProcThread({nullptr, 0}),
  54. #else
  55. fProcThread(0),
  56. #endif
  57. fEvents(),
  58. fUI(),
  59. fUnique2(2),
  60. leakDetector_VstPlugin()
  61. {
  62. carla_debug("VstPlugin::VstPlugin(%p, %i)", engine, id);
  63. carla_zeroStruct<VstMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  64. carla_zeroStruct<VstTimeInfo>(fTimeInfo);
  65. for (ushort i=0; i < kPluginMaxMidiEvents*2; ++i)
  66. fEvents.data[i] = (VstEvent*)&fMidiEvents[i];
  67. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_VST_GUI);
  68. #ifdef CARLA_OS_WIN
  69. fProcThread.p = nullptr;
  70. fProcThread.x = 0;
  71. #else
  72. fProcThread = 0;
  73. #endif
  74. // make plugin valid
  75. srand(id);
  76. fUnique1 = fUnique2 = rand();
  77. }
  78. ~VstPlugin() override
  79. {
  80. carla_debug("VstPlugin::~VstPlugin()");
  81. // close UI
  82. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  83. {
  84. showCustomUI(false);
  85. if (fUI.type == UI::UI_OSC)
  86. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  87. }
  88. pData->singleMutex.lock();
  89. pData->masterMutex.lock();
  90. if (pData->client != nullptr && pData->client->isActive())
  91. pData->client->deactivate();
  92. CARLA_ASSERT(! fIsProcessing);
  93. if (pData->active)
  94. {
  95. deactivate();
  96. pData->active = false;
  97. }
  98. if (fEffect != nullptr)
  99. {
  100. dispatcher(effClose, 0, 0, nullptr, 0.0f);
  101. fEffect = nullptr;
  102. }
  103. // make plugin invalid
  104. fUnique2 += 1;
  105. if (fLastChunk != nullptr)
  106. {
  107. std::free(fLastChunk);
  108. fLastChunk = nullptr;
  109. }
  110. clearBuffers();
  111. }
  112. // -------------------------------------------------------------------
  113. // Information (base)
  114. PluginType getType() const noexcept override
  115. {
  116. return PLUGIN_VST;
  117. }
  118. PluginCategory getCategory() const noexcept override
  119. {
  120. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, CarlaPlugin::getCategory());
  121. const intptr_t category(dispatcher(effGetPlugCategory, 0, 0, nullptr, 0.0f));
  122. switch (category)
  123. {
  124. case kPlugCategSynth:
  125. return PLUGIN_CATEGORY_SYNTH;
  126. case kPlugCategAnalysis:
  127. return PLUGIN_CATEGORY_UTILITY;
  128. case kPlugCategMastering:
  129. return PLUGIN_CATEGORY_DYNAMICS;
  130. case kPlugCategRoomFx:
  131. return PLUGIN_CATEGORY_DELAY;
  132. case kPlugCategRestoration:
  133. return PLUGIN_CATEGORY_UTILITY;
  134. case kPlugCategGenerator:
  135. return PLUGIN_CATEGORY_SYNTH;
  136. }
  137. if (fEffect->flags & effFlagsIsSynth)
  138. return PLUGIN_CATEGORY_SYNTH;
  139. return CarlaPlugin::getCategory();
  140. }
  141. int64_t getUniqueId() const noexcept override
  142. {
  143. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  144. return static_cast<int64_t>(fEffect->uniqueID);
  145. }
  146. // -------------------------------------------------------------------
  147. // Information (count)
  148. // nothing
  149. // -------------------------------------------------------------------
  150. // Information (current data)
  151. std::size_t getChunkData(void** const dataPtr) noexcept override
  152. {
  153. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  154. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  155. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  156. *dataPtr = nullptr;
  157. try {
  158. const intptr_t ret = dispatcher(effGetChunk, 0 /* bank */, 0, dataPtr, 0.0f);
  159. CARLA_SAFE_ASSERT_RETURN(ret >= 0, 0);
  160. return static_cast<std::size_t>(ret);
  161. } CARLA_SAFE_EXCEPTION_RETURN("VstPlugin::getChunkData", 0);
  162. }
  163. // -------------------------------------------------------------------
  164. // Information (per-plugin data)
  165. uint getOptionsAvailable() const noexcept override
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  168. uint options = 0x0;
  169. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  170. if (fEffect->flags & effFlagsProgramChunks)
  171. options |= PLUGIN_OPTION_USE_CHUNKS;
  172. if (getMidiInCount() == 0)
  173. {
  174. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  175. }
  176. else
  177. {
  178. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  179. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  180. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  181. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  182. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  183. }
  184. return options;
  185. }
  186. float getParameterValue(const uint32_t parameterId) const noexcept override
  187. {
  188. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0.0f);
  189. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  190. return fEffect->getParameter(fEffect, static_cast<int32_t>(parameterId));
  191. }
  192. void getLabel(char* const strBuf) const noexcept override
  193. {
  194. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  195. strBuf[0] = '\0';
  196. dispatcher(effGetProductString, 0, 0, strBuf, 0.0f);
  197. }
  198. void getMaker(char* const strBuf) const noexcept override
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  201. strBuf[0] = '\0';
  202. dispatcher(effGetVendorString, 0, 0, strBuf, 0.0f);
  203. }
  204. void getCopyright(char* const strBuf) const noexcept override
  205. {
  206. getMaker(strBuf);
  207. }
  208. void getRealName(char* const strBuf) const noexcept override
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  211. strBuf[0] = '\0';
  212. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  213. }
  214. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  217. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  218. strBuf[0] = '\0';
  219. dispatcher(effGetParamName, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  220. }
  221. void getParameterText(const uint32_t parameterId, char* const strBuf) const noexcept override
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  224. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  225. strBuf[0] = '\0';
  226. dispatcher(effGetParamDisplay, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  227. if (strBuf[0] == '\0')
  228. std::snprintf(strBuf, STR_MAX, "%f", getParameterValue(parameterId));
  229. }
  230. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  233. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  234. strBuf[0] = '\0';
  235. dispatcher(effGetParamLabel, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  236. }
  237. // -------------------------------------------------------------------
  238. // Set data (state)
  239. // nothing
  240. // -------------------------------------------------------------------
  241. // Set data (internal stuff)
  242. void setName(const char* const newName) override
  243. {
  244. CarlaPlugin::setName(newName);
  245. if (fUI.window != nullptr)
  246. {
  247. CarlaString guiTitle(pData->name);
  248. guiTitle += " (GUI)";
  249. fUI.window->setTitle(guiTitle.buffer());
  250. }
  251. }
  252. // -------------------------------------------------------------------
  253. // Set data (plugin-specific stuff)
  254. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  257. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  258. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  259. fEffect->setParameter(fEffect, static_cast<int32_t>(parameterId), fixedValue);
  260. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  261. }
  262. void setChunkData(const void* const data, const std::size_t dataSize) override
  263. {
  264. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  265. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  266. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  267. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  268. if (fLastChunk != nullptr)
  269. std::free(fLastChunk);
  270. fLastChunk = std::malloc(dataSize);
  271. CARLA_SAFE_ASSERT_RETURN(fLastChunk != nullptr,);
  272. std::memcpy(fLastChunk, data, dataSize);
  273. {
  274. const ScopedSingleProcessLocker spl(this, true);
  275. dispatcher(effSetChunk, 0 /* bank */, static_cast<intptr_t>(dataSize), fLastChunk, 0.0f);
  276. }
  277. // simulate an updateDisplay callback
  278. handleAudioMasterCallback(audioMasterUpdateDisplay, 0, 0, nullptr, 0.0f);
  279. #ifdef BUILD_BRIDGE
  280. const bool sendOsc(false);
  281. #else
  282. const bool sendOsc(pData->engine->isOscControlRegistered());
  283. #endif
  284. pData->updateParameterValues(this, sendOsc, true, false);
  285. }
  286. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  287. {
  288. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  289. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  290. if (index >= 0)
  291. {
  292. try {
  293. dispatcher(effBeginSetProgram, 0, 0, nullptr, 0.0f);
  294. } catch (...) {
  295. return;
  296. }
  297. {
  298. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  299. try {
  300. dispatcher(effSetProgram, 0, index, nullptr, 0.0f);
  301. } catch(...) {}
  302. }
  303. try {
  304. dispatcher(effEndSetProgram, 0, 0, nullptr, 0.0f);
  305. } catch(...) {}
  306. }
  307. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  308. }
  309. // -------------------------------------------------------------------
  310. // Set ui stuff
  311. void showCustomUI(const bool yesNo) override
  312. {
  313. if (fUI.type == UI::UI_OSC)
  314. {
  315. if (yesNo)
  316. {
  317. pData->osc.data.clear();
  318. pData->osc.thread.startThread();
  319. }
  320. else
  321. {
  322. pData->transientTryCounter = 0;
  323. if (pData->osc.data.target != nullptr)
  324. {
  325. osc_send_hide(pData->osc.data);
  326. osc_send_quit(pData->osc.data);
  327. pData->osc.data.clear();
  328. }
  329. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  330. }
  331. return;
  332. }
  333. if (fUI.isVisible == yesNo)
  334. return;
  335. if (yesNo)
  336. {
  337. CarlaString uiTitle(pData->name);
  338. uiTitle += " (GUI)";
  339. intptr_t value = 0;
  340. void* vstPtr = nullptr;
  341. ERect* vstRect = nullptr;
  342. if (fUI.window == nullptr && fUI.type == UI::UI_EMBED)
  343. {
  344. const char* msg = nullptr;
  345. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  346. #if defined(CARLA_OS_LINUX)
  347. # ifdef HAVE_X11
  348. fUI.window = CarlaPluginUI::newX11(this, frontendWinId, false);
  349. # else
  350. msg = "UI is only for systems with X11";
  351. (void)frontendWinId; // unused
  352. # endif
  353. #elif defined(CARLA_OS_MAC)
  354. # ifdef __LP64__
  355. fUI.window = CarlaPluginUI::newCocoa(this, frontendWinId);
  356. # endif
  357. #elif defined(CARLA_OS_WIN)
  358. fUI.window = CarlaPluginUI::newWindows(this, frontendWinId);
  359. #else
  360. msg = "Unknown UI type";
  361. #endif
  362. if (fUI.window == nullptr)
  363. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  364. fUI.window->setTitle(uiTitle.buffer());
  365. }
  366. if (fUI.type == UI::UI_EMBED)
  367. vstPtr = fUI.window->getPtr();
  368. else
  369. vstPtr = const_cast<char*>(uiTitle.buffer());
  370. dispatcher(effEditGetRect, 0, 0, &vstRect, 0.0f);
  371. #ifdef HAVE_X11
  372. value = (intptr_t)fUI.window->getDisplay();
  373. #endif
  374. if (dispatcher(effEditOpen, 0, value, vstPtr, 0.0f) != 0)
  375. {
  376. if (fUI.type == UI::UI_EMBED)
  377. {
  378. if (vstRect == nullptr || vstRect->right - vstRect->left < 2)
  379. dispatcher(effEditGetRect, 0, 0, &vstRect, 0.0f);
  380. if (vstRect != nullptr)
  381. {
  382. const int width(vstRect->right - vstRect->left);
  383. const int height(vstRect->bottom - vstRect->top);
  384. CARLA_SAFE_ASSERT_INT2(width > 1 && height > 1, width, height);
  385. if (width > 1 && height > 1)
  386. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), false);
  387. }
  388. fUI.window->show();
  389. }
  390. else
  391. {
  392. if (pData->engine->getOptions().frontendWinId)
  393. pData->transientTryCounter = 1;
  394. }
  395. fUI.isVisible = true;
  396. }
  397. else
  398. {
  399. if (fUI.type == UI::UI_EMBED)
  400. {
  401. delete fUI.window;
  402. fUI.window = nullptr;
  403. }
  404. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  405. }
  406. }
  407. else
  408. {
  409. fUI.isVisible = false;
  410. if (fUI.type == UI::UI_EMBED)
  411. {
  412. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  413. fUI.window->hide();
  414. }
  415. else
  416. {
  417. pData->transientTryCounter = 0;
  418. }
  419. dispatcher(effEditClose, 0, 0, nullptr, 0.0f);
  420. }
  421. }
  422. void idle() override
  423. {
  424. if (fNeedIdle)
  425. dispatcher(effIdle, 0, 0, nullptr, 0.0f);
  426. if (fUI.window != nullptr)
  427. {
  428. fUI.window->idle();
  429. if (fUI.isVisible)
  430. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  431. }
  432. CarlaPlugin::idle();
  433. }
  434. // -------------------------------------------------------------------
  435. // Plugin state
  436. void reload() override
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  439. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  440. carla_debug("VstPlugin::reload() - start");
  441. const EngineProcessMode processMode(pData->engine->getProccessMode());
  442. // Safely disable plugin for reload
  443. const ScopedDisabler sd(this);
  444. if (pData->active)
  445. deactivate();
  446. clearBuffers();
  447. uint32_t aIns, aOuts, mIns, mOuts, params;
  448. bool needsCtrlIn, needsCtrlOut;
  449. needsCtrlIn = needsCtrlOut = false;
  450. aIns = (fEffect->numInputs > 0) ? static_cast<uint32_t>(fEffect->numInputs) : 0;
  451. aOuts = (fEffect->numOutputs > 0) ? static_cast<uint32_t>(fEffect->numOutputs) : 0;
  452. params = (fEffect->numParams > 0) ? static_cast<uint32_t>(fEffect->numParams) : 0;
  453. if (hasMidiInput())
  454. {
  455. mIns = 1;
  456. needsCtrlIn = true;
  457. }
  458. else
  459. mIns = 0;
  460. if (hasMidiOutput())
  461. {
  462. mOuts = 1;
  463. needsCtrlOut = true;
  464. }
  465. else
  466. mOuts = 0;
  467. if (aIns > 0)
  468. {
  469. pData->audioIn.createNew(aIns);
  470. }
  471. if (aOuts > 0)
  472. {
  473. pData->audioOut.createNew(aOuts);
  474. needsCtrlIn = true;
  475. }
  476. if (params > 0)
  477. {
  478. pData->param.createNew(params, false);
  479. needsCtrlIn = true;
  480. }
  481. const uint portNameSize(pData->engine->getMaxPortNameSize());
  482. CarlaString portName;
  483. // Audio Ins
  484. for (uint32_t j=0; j < aIns; ++j)
  485. {
  486. portName.clear();
  487. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  488. {
  489. portName = pData->name;
  490. portName += ":";
  491. }
  492. if (aIns > 1)
  493. {
  494. portName += "input_";
  495. portName += CarlaString(j+1);
  496. }
  497. else
  498. portName += "input";
  499. portName.truncate(portNameSize);
  500. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  501. pData->audioIn.ports[j].rindex = j;
  502. }
  503. // Audio Outs
  504. for (uint32_t j=0; j < aOuts; ++j)
  505. {
  506. portName.clear();
  507. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  508. {
  509. portName = pData->name;
  510. portName += ":";
  511. }
  512. if (aOuts > 1)
  513. {
  514. portName += "output_";
  515. portName += CarlaString(j+1);
  516. }
  517. else
  518. portName += "output";
  519. portName.truncate(portNameSize);
  520. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  521. pData->audioOut.ports[j].rindex = j;
  522. }
  523. for (uint32_t j=0; j < params; ++j)
  524. {
  525. pData->param.data[j].type = PARAMETER_INPUT;
  526. pData->param.data[j].index = static_cast<int32_t>(j);
  527. pData->param.data[j].rindex = static_cast<int32_t>(j);
  528. float min, max, def, step, stepSmall, stepLarge;
  529. VstParameterProperties prop;
  530. carla_zeroStruct<VstParameterProperties>(prop);
  531. if (pData->hints & PLUGIN_HAS_COCKOS_EXTENSIONS)
  532. {
  533. double vrange[2] = { 0.0, 1.0 };
  534. bool isInteger = false;
  535. if (static_cast<uintptr_t>(dispatcher(effVendorSpecific, static_cast<int32_t>(0xdeadbef0), static_cast<int32_t>(j), vrange, 0.0f)) >= 0xbeef)
  536. {
  537. min = static_cast<float>(vrange[0]);
  538. max = static_cast<float>(vrange[1]);
  539. if (min > max)
  540. {
  541. carla_stderr2("WARNING - Broken plugin parameter min > max (with cockos extensions)");
  542. min = max - 0.1f;
  543. }
  544. else if (carla_compareFloats(min, max))
  545. {
  546. carla_stderr2("WARNING - Broken plugin parameter min == max (with cockos extensions)");
  547. max = min + 0.1f;
  548. }
  549. // only use values as integer if we have a proper range
  550. if (max - min >= 1.0f)
  551. isInteger = dispatcher(effVendorSpecific, kVstParameterUsesIntStep, static_cast<int32_t>(j), nullptr, 0.0f) >= 0xbeef;
  552. }
  553. else
  554. {
  555. min = 0.0f;
  556. max = 1.0f;
  557. }
  558. if (isInteger)
  559. {
  560. step = 1.0f;
  561. stepSmall = 1.0f;
  562. stepLarge = 10.0f;
  563. }
  564. else
  565. {
  566. const float range = max - min;
  567. step = range/100.0f;
  568. stepSmall = range/1000.0f;
  569. stepLarge = range/10.0f;
  570. }
  571. }
  572. else if (dispatcher(effGetParameterProperties, static_cast<int32_t>(j), 0, &prop, 0) == 1)
  573. {
  574. if (prop.flags & kVstParameterUsesIntegerMinMax)
  575. {
  576. min = static_cast<float>(prop.minInteger);
  577. max = static_cast<float>(prop.maxInteger);
  578. if (min > max)
  579. {
  580. carla_stderr2("WARNING - Broken plugin parameter min > max");
  581. min = max - 0.1f;
  582. }
  583. else if (carla_compareFloats(min, max))
  584. {
  585. carla_stderr2("WARNING - Broken plugin parameter min == max");
  586. max = min + 0.1f;
  587. }
  588. }
  589. else
  590. {
  591. min = 0.0f;
  592. max = 1.0f;
  593. }
  594. if (prop.flags & kVstParameterIsSwitch)
  595. {
  596. step = max - min;
  597. stepSmall = step;
  598. stepLarge = step;
  599. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  600. }
  601. else if (prop.flags & kVstParameterUsesIntStep)
  602. {
  603. step = static_cast<float>(prop.stepInteger);
  604. stepSmall = static_cast<float>(prop.stepInteger)/10.0f;
  605. stepLarge = static_cast<float>(prop.largeStepInteger);
  606. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  607. }
  608. else if (prop.flags & kVstParameterUsesFloatStep)
  609. {
  610. step = prop.stepFloat;
  611. stepSmall = prop.smallStepFloat;
  612. stepLarge = prop.largeStepFloat;
  613. }
  614. else
  615. {
  616. const float range = max - min;
  617. step = range/100.0f;
  618. stepSmall = range/1000.0f;
  619. stepLarge = range/10.0f;
  620. }
  621. if (prop.flags & kVstParameterCanRamp)
  622. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  623. }
  624. else
  625. {
  626. min = 0.0f;
  627. max = 1.0f;
  628. step = 0.001f;
  629. stepSmall = 0.0001f;
  630. stepLarge = 0.1f;
  631. }
  632. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  633. #ifndef BUILD_BRIDGE
  634. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  635. #endif
  636. if ((pData->hints & PLUGIN_USES_OLD_VSTSDK) != 0 || dispatcher(effCanBeAutomated, static_cast<int32_t>(j), 0, nullptr, 0.0f) == 1)
  637. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  638. // no such thing as VST default parameters
  639. def = fEffect->getParameter(fEffect, static_cast<int32_t>(j));
  640. if (def < min)
  641. def = min;
  642. else if (def > max)
  643. def = max;
  644. pData->param.ranges[j].min = min;
  645. pData->param.ranges[j].max = max;
  646. pData->param.ranges[j].def = def;
  647. pData->param.ranges[j].step = step;
  648. pData->param.ranges[j].stepSmall = stepSmall;
  649. pData->param.ranges[j].stepLarge = stepLarge;
  650. }
  651. if (needsCtrlIn)
  652. {
  653. portName.clear();
  654. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  655. {
  656. portName = pData->name;
  657. portName += ":";
  658. }
  659. portName += "events-in";
  660. portName.truncate(portNameSize);
  661. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  662. }
  663. if (needsCtrlOut)
  664. {
  665. portName.clear();
  666. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  667. {
  668. portName = pData->name;
  669. portName += ":";
  670. }
  671. portName += "events-out";
  672. portName.truncate(portNameSize);
  673. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  674. }
  675. // plugin hints
  676. const intptr_t vstCategory = dispatcher(effGetPlugCategory, 0, 0, nullptr, 0.0f);
  677. pData->hints = 0x0;
  678. if (vstCategory == kPlugCategSynth || vstCategory == kPlugCategGenerator)
  679. pData->hints |= PLUGIN_IS_SYNTH;
  680. if (fEffect->flags & effFlagsHasEditor)
  681. {
  682. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  683. if (fUI.type == UI::UI_EMBED)
  684. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  685. }
  686. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  687. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  688. if ((fEffect->flags & effFlagsCanReplacing) != 0 && fEffect->processReplacing != fEffect->process)
  689. pData->hints |= PLUGIN_CAN_PROCESS_REPLACING;
  690. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>("hasCockosExtensions"), 0.0f)) == 0xbeef0000)
  691. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  692. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  693. pData->hints |= PLUGIN_CAN_DRYWET;
  694. if (aOuts > 0)
  695. pData->hints |= PLUGIN_CAN_VOLUME;
  696. if (aOuts >= 2 && aOuts % 2 == 0)
  697. pData->hints |= PLUGIN_CAN_BALANCE;
  698. // extra plugin hints
  699. pData->extraHints = 0x0;
  700. if (mIns > 0)
  701. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  702. if (mOuts > 0)
  703. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  704. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  705. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  706. // dummy pre-start to get latency and wantEvents() on old plugins
  707. {
  708. activate();
  709. deactivate();
  710. }
  711. // check latency
  712. if (pData->hints & PLUGIN_CAN_DRYWET)
  713. {
  714. #ifdef VESTIGE_HEADER
  715. char* const empty3Ptr = &fEffect->empty3[0];
  716. int32_t initialDelay = *(int32_t*)empty3Ptr;
  717. pData->latency = (initialDelay > 0) ? static_cast<uint32_t>(initialDelay) : 0;
  718. #else
  719. pData->latency = (fEffect->initialDelay > 0) ? static_cast<uint32_t>(fEffect->initialDelay) : 0;
  720. #endif
  721. pData->client->setLatency(pData->latency);
  722. #ifndef BUILD_BRIDGE
  723. pData->recreateLatencyBuffers();
  724. #endif
  725. }
  726. // special plugin fixes
  727. // 1. IL Harmless - disable threaded processing
  728. if (fEffect->uniqueID == 1229484653)
  729. {
  730. char strBuf[STR_MAX+1] = { '\0' };
  731. getLabel(strBuf);
  732. if (std::strcmp(strBuf, "IL Harmless") == 0)
  733. {
  734. // TODO - disable threaded processing
  735. }
  736. }
  737. //bufferSizeChanged(pData->engine->getBufferSize());
  738. reloadPrograms(true);
  739. if (pData->active)
  740. activate();
  741. carla_debug("VstPlugin::reload() - end");
  742. }
  743. void reloadPrograms(const bool doInit) override
  744. {
  745. carla_debug("VstPlugin::reloadPrograms(%s)", bool2str(doInit));
  746. const uint32_t oldCount = pData->prog.count;
  747. const int32_t current = pData->prog.current;
  748. // Delete old programs
  749. pData->prog.clear();
  750. // Query new programs
  751. uint32_t newCount = (fEffect->numPrograms > 0) ? static_cast<uint32_t>(fEffect->numPrograms) : 0;
  752. if (newCount > 0)
  753. {
  754. pData->prog.createNew(newCount);
  755. // Update names
  756. for (int32_t i=0; i < fEffect->numPrograms; ++i)
  757. {
  758. char strBuf[STR_MAX+1] = { '\0' };
  759. if (dispatcher(effGetProgramNameIndexed, i, 0, strBuf, 0.0f) != 1)
  760. {
  761. // program will be [re-]changed later
  762. dispatcher(effSetProgram, 0, i, nullptr, 0.0f);
  763. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  764. }
  765. pData->prog.names[i] = carla_strdup(strBuf);
  766. }
  767. }
  768. #ifndef BUILD_BRIDGE
  769. // Update OSC Names
  770. if (pData->engine->isOscControlRegistered())
  771. {
  772. pData->engine->oscSend_control_set_program_count(pData->id, newCount);
  773. for (uint32_t i=0; i < newCount; ++i)
  774. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  775. }
  776. #endif
  777. if (doInit)
  778. {
  779. if (newCount > 0)
  780. setProgram(0, false, false, false);
  781. }
  782. else
  783. {
  784. // Check if current program is invalid
  785. bool programChanged = false;
  786. if (newCount == oldCount+1)
  787. {
  788. // one program added, probably created by user
  789. pData->prog.current = static_cast<int32_t>(oldCount);
  790. programChanged = true;
  791. }
  792. else if (current < 0 && newCount > 0)
  793. {
  794. // programs exist now, but not before
  795. pData->prog.current = 0;
  796. programChanged = true;
  797. }
  798. else if (current >= 0 && newCount == 0)
  799. {
  800. // programs existed before, but not anymore
  801. pData->prog.current = -1;
  802. programChanged = true;
  803. }
  804. else if (current >= static_cast<int32_t>(newCount))
  805. {
  806. // current program > count
  807. pData->prog.current = 0;
  808. programChanged = true;
  809. }
  810. else
  811. {
  812. // no change
  813. pData->prog.current = current;
  814. }
  815. if (programChanged)
  816. {
  817. setProgram(pData->prog.current, true, true, true);
  818. }
  819. else
  820. {
  821. // Program was changed during update, re-set it
  822. if (pData->prog.current >= 0)
  823. dispatcher(effSetProgram, 0, pData->prog.current, nullptr, 0.0f);
  824. }
  825. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  826. }
  827. }
  828. // -------------------------------------------------------------------
  829. // Plugin processing
  830. void activate() noexcept override
  831. {
  832. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  833. try {
  834. dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  835. } catch(...) {}
  836. try {
  837. dispatcher(effStartProcess, 0, 0, nullptr, 0.0f);
  838. } catch(...) {}
  839. }
  840. void deactivate() noexcept override
  841. {
  842. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  843. try {
  844. dispatcher(effStopProcess, 0, 0, nullptr, 0.0f);
  845. } catch(...) {}
  846. try {
  847. dispatcher(effMainsChanged, 0, 0, nullptr, 0.0f);
  848. } catch(...) {}
  849. }
  850. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  851. {
  852. fProcThread = pthread_self();
  853. // --------------------------------------------------------------------------------------------------------
  854. // Check if active
  855. if (! pData->active)
  856. {
  857. // disable any output sound
  858. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  859. FloatVectorOperations::clear(audioOut[i], static_cast<int>(frames));
  860. return;
  861. }
  862. fMidiEventCount = 0;
  863. carla_zeroStruct<VstMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  864. // --------------------------------------------------------------------------------------------------------
  865. // Check if needs reset
  866. if (pData->needsReset)
  867. {
  868. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  869. {
  870. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  871. for (uint8_t i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  872. {
  873. fMidiEvents[k].type = kVstMidiType;
  874. fMidiEvents[k].byteSize = kVstMidiEventSize;
  875. fMidiEvents[k].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  876. fMidiEvents[k].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  877. fMidiEvents[k+i].type = kVstMidiType;
  878. fMidiEvents[k+i].byteSize = kVstMidiEventSize;
  879. fMidiEvents[k+i].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  880. fMidiEvents[k+i].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  881. }
  882. }
  883. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  884. {
  885. fMidiEventCount = MAX_MIDI_NOTE;
  886. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  887. {
  888. fMidiEvents[i].type = kVstMidiType;
  889. fMidiEvents[i].byteSize = kVstMidiEventSize;
  890. fMidiEvents[i].midiData[0] = char(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  891. fMidiEvents[i].midiData[1] = char(i);
  892. }
  893. }
  894. #ifndef BUILD_BRIDGE
  895. if (pData->latency > 0)
  896. {
  897. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  898. FloatVectorOperations::clear(pData->latencyBuffers[i], static_cast<int>(pData->latency));
  899. }
  900. #endif
  901. pData->needsReset = false;
  902. }
  903. // --------------------------------------------------------------------------------------------------------
  904. // Set TimeInfo
  905. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  906. fTimeInfo.flags = kVstTransportChanged;
  907. if (timeInfo.playing)
  908. fTimeInfo.flags |= kVstTransportPlaying;
  909. fTimeInfo.samplePos = double(timeInfo.frame);
  910. fTimeInfo.sampleRate = pData->engine->getSampleRate();
  911. if (timeInfo.usecs != 0)
  912. {
  913. fTimeInfo.nanoSeconds = double(timeInfo.usecs)/1000.0;
  914. fTimeInfo.flags |= kVstNanosValid;
  915. }
  916. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  917. {
  918. double ppqBar = double(timeInfo.bbt.bar - 1) * timeInfo.bbt.beatsPerBar;
  919. double ppqBeat = double(timeInfo.bbt.beat - 1);
  920. double ppqTick = double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat;
  921. // PPQ Pos
  922. fTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
  923. fTimeInfo.flags |= kVstPpqPosValid;
  924. // Tempo
  925. fTimeInfo.tempo = timeInfo.bbt.beatsPerMinute;
  926. fTimeInfo.flags |= kVstTempoValid;
  927. // Bars
  928. fTimeInfo.barStartPos = ppqBar;
  929. fTimeInfo.flags |= kVstBarsValid;
  930. // Time Signature
  931. fTimeInfo.timeSigNumerator = static_cast<int32_t>(timeInfo.bbt.beatsPerBar);
  932. fTimeInfo.timeSigDenominator = static_cast<int32_t>(timeInfo.bbt.beatType);
  933. fTimeInfo.flags |= kVstTimeSigValid;
  934. }
  935. else
  936. {
  937. // Tempo
  938. fTimeInfo.tempo = 120.0;
  939. fTimeInfo.flags |= kVstTempoValid;
  940. // Time Signature
  941. fTimeInfo.timeSigNumerator = 4;
  942. fTimeInfo.timeSigDenominator = 4;
  943. fTimeInfo.flags |= kVstTimeSigValid;
  944. // Missing info
  945. fTimeInfo.ppqPos = 0.0;
  946. fTimeInfo.barStartPos = 0.0;
  947. }
  948. // --------------------------------------------------------------------------------------------------------
  949. // Event Input and Processing
  950. if (pData->event.portIn != nullptr)
  951. {
  952. // ----------------------------------------------------------------------------------------------------
  953. // MIDI Input (External)
  954. if (pData->extNotes.mutex.tryLock())
  955. {
  956. ExternalMidiNote note = { 0, 0, 0 };
  957. for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
  958. {
  959. note = pData->extNotes.data.getFirst(note, true);
  960. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  961. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  962. vstMidiEvent.type = kVstMidiType;
  963. vstMidiEvent.byteSize = kVstMidiEventSize;
  964. vstMidiEvent.midiData[0] = char((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  965. vstMidiEvent.midiData[1] = char(note.note);
  966. vstMidiEvent.midiData[2] = char(note.velo);
  967. }
  968. pData->extNotes.mutex.unlock();
  969. } // End of MIDI Input (External)
  970. // ----------------------------------------------------------------------------------------------------
  971. // Event Input (System)
  972. #ifndef BUILD_BRIDGE
  973. bool allNotesOffSent = false;
  974. #endif
  975. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  976. uint32_t startTime = 0;
  977. uint32_t timeOffset = 0;
  978. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  979. {
  980. const EngineEvent& event(pData->event.portIn->getEvent(i));
  981. if (event.time >= frames)
  982. continue;
  983. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  984. if (isSampleAccurate && event.time > timeOffset)
  985. {
  986. if (processSingle(audioIn, audioOut, event.time - timeOffset, timeOffset))
  987. {
  988. startTime = 0;
  989. timeOffset = event.time;
  990. if (fMidiEventCount > 0)
  991. {
  992. carla_zeroStruct<VstMidiEvent>(fMidiEvents, fMidiEventCount);
  993. fMidiEventCount = 0;
  994. }
  995. }
  996. else
  997. startTime += timeOffset;
  998. }
  999. switch (event.type)
  1000. {
  1001. case kEngineEventTypeNull:
  1002. break;
  1003. case kEngineEventTypeControl: {
  1004. const EngineControlEvent& ctrlEvent(event.ctrl);
  1005. switch (ctrlEvent.type)
  1006. {
  1007. case kEngineControlEventTypeNull:
  1008. break;
  1009. case kEngineControlEventTypeParameter: {
  1010. #ifndef BUILD_BRIDGE
  1011. // Control backend stuff
  1012. if (event.channel == pData->ctrlChannel)
  1013. {
  1014. float value;
  1015. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1016. {
  1017. value = ctrlEvent.value;
  1018. setDryWet(value, false, false);
  1019. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1020. break;
  1021. }
  1022. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1023. {
  1024. value = ctrlEvent.value*127.0f/100.0f;
  1025. setVolume(value, false, false);
  1026. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1027. break;
  1028. }
  1029. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1030. {
  1031. float left, right;
  1032. value = ctrlEvent.value/0.5f - 1.0f;
  1033. if (value < 0.0f)
  1034. {
  1035. left = -1.0f;
  1036. right = (value*2.0f)+1.0f;
  1037. }
  1038. else if (value > 0.0f)
  1039. {
  1040. left = (value*2.0f)-1.0f;
  1041. right = 1.0f;
  1042. }
  1043. else
  1044. {
  1045. left = -1.0f;
  1046. right = 1.0f;
  1047. }
  1048. setBalanceLeft(left, false, false);
  1049. setBalanceRight(right, false, false);
  1050. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1051. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1052. break;
  1053. }
  1054. }
  1055. #endif
  1056. // Control plugin parameters
  1057. uint32_t k;
  1058. for (k=0; k < pData->param.count; ++k)
  1059. {
  1060. if (pData->param.data[k].midiChannel != event.channel)
  1061. continue;
  1062. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1063. continue;
  1064. if (pData->param.data[k].type != PARAMETER_INPUT)
  1065. continue;
  1066. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1067. continue;
  1068. float value;
  1069. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1070. {
  1071. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1072. }
  1073. else
  1074. {
  1075. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1076. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1077. value = std::rint(value);
  1078. }
  1079. setParameterValue(k, value, false, false, false);
  1080. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1081. break;
  1082. }
  1083. // check if event is already handled
  1084. if (k != pData->param.count)
  1085. break;
  1086. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1087. {
  1088. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1089. continue;
  1090. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1091. carla_zeroStruct(vstMidiEvent);
  1092. vstMidiEvent.type = kVstMidiType;
  1093. vstMidiEvent.byteSize = kVstMidiEventSize;
  1094. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1095. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1096. vstMidiEvent.midiData[1] = char(ctrlEvent.param);
  1097. vstMidiEvent.midiData[2] = char(ctrlEvent.value*127.0f);
  1098. }
  1099. break;
  1100. } // case kEngineControlEventTypeParameter
  1101. case kEngineControlEventTypeMidiBank:
  1102. break;
  1103. case kEngineControlEventTypeMidiProgram:
  1104. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1105. {
  1106. if (ctrlEvent.param < pData->prog.count)
  1107. {
  1108. setProgram(ctrlEvent.param, false, false, false);
  1109. pData->postponeRtEvent(kPluginPostRtEventProgramChange, ctrlEvent.param, 0, 0.0f);
  1110. break;
  1111. }
  1112. }
  1113. break;
  1114. case kEngineControlEventTypeAllSoundOff:
  1115. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1116. {
  1117. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1118. continue;
  1119. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1120. carla_zeroStruct(vstMidiEvent);
  1121. vstMidiEvent.type = kVstMidiType;
  1122. vstMidiEvent.byteSize = kVstMidiEventSize;
  1123. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1124. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1125. vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1126. }
  1127. break;
  1128. case kEngineControlEventTypeAllNotesOff:
  1129. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1130. {
  1131. #ifndef BUILD_BRIDGE
  1132. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1133. {
  1134. allNotesOffSent = true;
  1135. sendMidiAllNotesOffToCallback();
  1136. }
  1137. #endif
  1138. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1139. continue;
  1140. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1141. carla_zeroStruct(vstMidiEvent);
  1142. vstMidiEvent.type = kVstMidiType;
  1143. vstMidiEvent.byteSize = kVstMidiEventSize;
  1144. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1145. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1146. vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1147. }
  1148. break;
  1149. } // switch (ctrlEvent.type)
  1150. break;
  1151. } // case kEngineEventTypeControl
  1152. case kEngineEventTypeMidi: {
  1153. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1154. continue;
  1155. const EngineMidiEvent& midiEvent(event.midi);
  1156. if (midiEvent.size > 3)
  1157. continue;
  1158. static_assert(3 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1159. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1160. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1161. continue;
  1162. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1163. continue;
  1164. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1165. continue;
  1166. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1167. continue;
  1168. // Fix bad note-off
  1169. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1170. status = MIDI_STATUS_NOTE_OFF;
  1171. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1172. carla_zeroStruct(vstMidiEvent);
  1173. vstMidiEvent.type = kVstMidiType;
  1174. vstMidiEvent.byteSize = kVstMidiEventSize;
  1175. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1176. vstMidiEvent.midiData[0] = char(status | (event.channel & MIDI_CHANNEL_BIT));
  1177. vstMidiEvent.midiData[1] = char(midiEvent.size >= 2 ? midiEvent.data[1] : 0);
  1178. vstMidiEvent.midiData[2] = char(midiEvent.size >= 3 ? midiEvent.data[2] : 0);
  1179. if (status == MIDI_STATUS_NOTE_ON)
  1180. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1181. else if (status == MIDI_STATUS_NOTE_OFF)
  1182. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1183. } break;
  1184. } // switch (event.type)
  1185. }
  1186. pData->postRtEvents.trySplice();
  1187. if (frames > timeOffset)
  1188. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1189. } // End of Event Input and Processing
  1190. // --------------------------------------------------------------------------------------------------------
  1191. // Plugin processing (no events)
  1192. else
  1193. {
  1194. processSingle(audioIn, audioOut, frames, 0);
  1195. } // End of Plugin processing (no events)
  1196. // --------------------------------------------------------------------------------------------------------
  1197. // MIDI Output
  1198. if (pData->event.portOut != nullptr)
  1199. {
  1200. // reverse lookup MIDI events
  1201. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1202. {
  1203. if (fMidiEvents[k].type == 0)
  1204. break;
  1205. const VstMidiEvent& vstMidiEvent(fMidiEvents[k]);
  1206. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.deltaFrames >= 0);
  1207. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.midiData[0] != 0);
  1208. uint8_t midiData[3];
  1209. midiData[0] = static_cast<uint8_t>(vstMidiEvent.midiData[0]);
  1210. midiData[1] = static_cast<uint8_t>(vstMidiEvent.midiData[1]);
  1211. midiData[2] = static_cast<uint8_t>(vstMidiEvent.midiData[2]);
  1212. pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(vstMidiEvent.deltaFrames), MIDI_GET_CHANNEL_FROM_DATA(midiData), 0, 3, midiData);
  1213. }
  1214. } // End of MIDI Output
  1215. }
  1216. bool processSingle(const float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1217. {
  1218. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1219. if (pData->audioIn.count > 0)
  1220. {
  1221. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1222. }
  1223. if (pData->audioOut.count > 0)
  1224. {
  1225. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1226. }
  1227. // --------------------------------------------------------------------------------------------------------
  1228. // Try lock, silence otherwise
  1229. if (pData->engine->isOffline())
  1230. {
  1231. pData->singleMutex.lock();
  1232. }
  1233. else if (! pData->singleMutex.tryLock())
  1234. {
  1235. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1236. {
  1237. for (uint32_t k=0; k < frames; ++k)
  1238. outBuffer[i][k+timeOffset] = 0.0f;
  1239. }
  1240. return false;
  1241. }
  1242. // --------------------------------------------------------------------------------------------------------
  1243. // Set audio buffers
  1244. float* vstInBuffer[pData->audioIn.count];
  1245. float* vstOutBuffer[pData->audioOut.count];
  1246. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1247. vstInBuffer[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1248. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1249. vstOutBuffer[i] = outBuffer[i]+timeOffset;
  1250. // --------------------------------------------------------------------------------------------------------
  1251. // Set MIDI events
  1252. if (fMidiEventCount > 0)
  1253. {
  1254. fEvents.numEvents = static_cast<int32_t>(fMidiEventCount);
  1255. fEvents.reserved = 0;
  1256. dispatcher(effProcessEvents, 0, 0, &fEvents, 0.0f);
  1257. }
  1258. // --------------------------------------------------------------------------------------------------------
  1259. // Run plugin
  1260. fIsProcessing = true;
  1261. if (pData->hints & PLUGIN_CAN_PROCESS_REPLACING)
  1262. {
  1263. fEffect->processReplacing(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1264. }
  1265. else
  1266. {
  1267. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1268. FloatVectorOperations::clear(vstOutBuffer[i], static_cast<int>(frames));
  1269. #if ! VST_FORCE_DEPRECATED
  1270. fEffect->process(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1271. #endif
  1272. }
  1273. fIsProcessing = false;
  1274. fTimeInfo.samplePos += frames;
  1275. #ifndef BUILD_BRIDGE
  1276. // --------------------------------------------------------------------------------------------------------
  1277. // Post-processing (dry/wet, volume and balance)
  1278. {
  1279. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && ! carla_compareFloats(pData->postProc.volume, 1.0f);
  1280. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && ! carla_compareFloats(pData->postProc.dryWet, 1.0f);
  1281. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_compareFloats(pData->postProc.balanceLeft, -1.0f) && carla_compareFloats(pData->postProc.balanceRight, 1.0f));
  1282. bool isPair;
  1283. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1284. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1285. {
  1286. // Dry/Wet
  1287. if (doDryWet)
  1288. {
  1289. for (uint32_t k=0; k < frames; ++k)
  1290. {
  1291. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k+timeOffset];
  1292. outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1293. }
  1294. }
  1295. // Balance
  1296. if (doBalance)
  1297. {
  1298. isPair = (i % 2 == 0);
  1299. if (isPair)
  1300. {
  1301. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1302. FloatVectorOperations::copy(oldBufLeft, outBuffer[i]+timeOffset, static_cast<int>(frames));
  1303. }
  1304. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1305. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1306. for (uint32_t k=0; k < frames; ++k)
  1307. {
  1308. if (isPair)
  1309. {
  1310. // left
  1311. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1312. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1313. }
  1314. else
  1315. {
  1316. // right
  1317. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1318. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1319. }
  1320. }
  1321. }
  1322. // Volume
  1323. if (doVolume)
  1324. {
  1325. for (uint32_t k=0; k < frames; ++k)
  1326. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1327. }
  1328. }
  1329. } // End of Post-processing
  1330. #endif
  1331. // --------------------------------------------------------------------------------------------------------
  1332. pData->singleMutex.unlock();
  1333. return true;
  1334. }
  1335. void bufferSizeChanged(const uint32_t newBufferSize) override
  1336. {
  1337. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1338. carla_debug("VstPlugin::bufferSizeChanged(%i)", newBufferSize);
  1339. if (pData->active)
  1340. deactivate();
  1341. #if ! VST_FORCE_DEPRECATED
  1342. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(newBufferSize), nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1343. #endif
  1344. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(newBufferSize), nullptr, 0.0f);
  1345. if (pData->active)
  1346. activate();
  1347. }
  1348. void sampleRateChanged(const double newSampleRate) override
  1349. {
  1350. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1351. carla_debug("VstPlugin::sampleRateChanged(%g)", newSampleRate);
  1352. if (pData->active)
  1353. deactivate();
  1354. #if ! VST_FORCE_DEPRECATED
  1355. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(newSampleRate));
  1356. #endif
  1357. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(newSampleRate));
  1358. if (pData->active)
  1359. activate();
  1360. }
  1361. // -------------------------------------------------------------------
  1362. // Plugin buffers
  1363. // nothing
  1364. // -------------------------------------------------------------------
  1365. // Post-poned UI Stuff
  1366. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1367. {
  1368. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1369. if (fUI.type != UI::UI_OSC)
  1370. return;
  1371. if (pData->osc.data.target == nullptr)
  1372. return;
  1373. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  1374. }
  1375. void uiProgramChange(const uint32_t index) noexcept override
  1376. {
  1377. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  1378. if (fUI.type != UI::UI_OSC)
  1379. return;
  1380. if (pData->osc.data.target == nullptr)
  1381. return;
  1382. osc_send_program(pData->osc.data, index);
  1383. }
  1384. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1385. {
  1386. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1387. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1388. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1389. if (fUI.type != UI::UI_OSC)
  1390. return;
  1391. if (pData->osc.data.target == nullptr)
  1392. return;
  1393. uint8_t midiData[4];
  1394. midiData[0] = 0;
  1395. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  1396. midiData[2] = note;
  1397. midiData[3] = velo;
  1398. osc_send_midi(pData->osc.data, midiData);
  1399. }
  1400. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1401. {
  1402. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1403. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1404. if (fUI.type != UI::UI_OSC)
  1405. return;
  1406. if (pData->osc.data.target == nullptr)
  1407. return;
  1408. uint8_t midiData[4];
  1409. midiData[0] = 0;
  1410. midiData[1] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  1411. midiData[2] = note;
  1412. midiData[3] = 0;
  1413. osc_send_midi(pData->osc.data, midiData);
  1414. }
  1415. // -------------------------------------------------------------------
  1416. protected:
  1417. void handlePluginUIClosed() override
  1418. {
  1419. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::UI_EMBED || fUI.type == UI::UI_EXTERNAL,);
  1420. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1421. carla_debug("VstPlugin::handlePluginUIClosed()");
  1422. showCustomUI(false);
  1423. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1424. }
  1425. void handlePluginUIResized(const uint width, const uint height) override
  1426. {
  1427. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::UI_EMBED,);
  1428. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1429. carla_debug("VstPlugin::handlePluginUIResized(%u, %u)", width, height);
  1430. return; // unused
  1431. (void)width; (void)height;
  1432. }
  1433. // -------------------------------------------------------------------
  1434. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt) const noexcept
  1435. {
  1436. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  1437. #ifdef DEBUG
  1438. if (opcode != effIdle && opcode != effEditIdle && opcode != effProcessEvents)
  1439. carla_debug("VstPlugin::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1440. #endif
  1441. try {
  1442. return fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt);
  1443. } CARLA_SAFE_EXCEPTION_RETURN("Vst dispatcher", 0);
  1444. }
  1445. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1446. {
  1447. #ifdef DEBUG
  1448. if (opcode != audioMasterGetTime)
  1449. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1450. #endif
  1451. intptr_t ret = 0;
  1452. switch (opcode)
  1453. {
  1454. case audioMasterAutomate: {
  1455. CARLA_SAFE_ASSERT_BREAK(pData->enabled);
  1456. // plugins should never do this:
  1457. CARLA_SAFE_ASSERT_INT(index >= 0 && index < static_cast<int32_t>(pData->param.count), index);
  1458. if (index < 0 || index >= static_cast<int32_t>(pData->param.count))
  1459. break;
  1460. const uint32_t uindex(static_cast<uint32_t>(index));
  1461. const float fixedValue(pData->param.getFixedValue(uindex, opt));
  1462. // Called from plugin process thread, nasty!
  1463. if (pthread_equal(pthread_self(), fProcThread))
  1464. {
  1465. CARLA_SAFE_ASSERT(fIsProcessing);
  1466. pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1467. }
  1468. // Called from UI
  1469. else if (fUI.isVisible)
  1470. {
  1471. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1472. }
  1473. // Unknown
  1474. // TODO - check if plugin or UI is initializing
  1475. else
  1476. {
  1477. carla_stdout("audioMasterAutomate called from unknown source");
  1478. setParameterValue(uindex, fixedValue, true, true, true);
  1479. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1480. }
  1481. break;
  1482. }
  1483. case audioMasterCurrentId:
  1484. // TODO
  1485. // if using old sdk, return effect->uniqueID
  1486. break;
  1487. case audioMasterIdle:
  1488. //pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1489. //pData->engine->idle();
  1490. break;
  1491. #if ! VST_FORCE_DEPRECATED
  1492. case audioMasterPinConnected:
  1493. // Deprecated in VST SDK 2.4
  1494. // TODO
  1495. break;
  1496. case audioMasterWantMidi:
  1497. // Deprecated in VST SDK 2.4
  1498. pData->hints |= PLUGIN_WANTS_MIDI_INPUT;
  1499. break;
  1500. #endif
  1501. case audioMasterGetTime:
  1502. ret = (intptr_t)&fTimeInfo;
  1503. break;
  1504. case audioMasterProcessEvents:
  1505. CARLA_SAFE_ASSERT_RETURN(pData->enabled, 0);
  1506. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, 0);
  1507. CARLA_SAFE_ASSERT_RETURN(pData->event.portOut != nullptr, 0);
  1508. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1509. return 0;
  1510. if (const VstEvents* const vstEvents = (const VstEvents*)ptr)
  1511. {
  1512. for (int32_t i=0; i < vstEvents->numEvents && i < kPluginMaxMidiEvents*2; ++i)
  1513. {
  1514. if (vstEvents->events[i] == nullptr)
  1515. break;
  1516. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)vstEvents->events[i]);
  1517. if (vstMidiEvent->type != kVstMidiType)
  1518. continue;
  1519. // reverse-find first free event, and put it there
  1520. for (uint32_t j=(kPluginMaxMidiEvents*2)-1; j >= fMidiEventCount; --j)
  1521. {
  1522. if (fMidiEvents[j].type == 0)
  1523. {
  1524. std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
  1525. break;
  1526. }
  1527. }
  1528. }
  1529. }
  1530. ret = 1;
  1531. break;
  1532. #if ! VST_FORCE_DEPRECATED
  1533. case audioMasterSetTime:
  1534. // Deprecated in VST SDK 2.4
  1535. break;
  1536. case audioMasterTempoAt:
  1537. // Deprecated in VST SDK 2.4
  1538. ret = static_cast<intptr_t>(fTimeInfo.tempo * 10000);
  1539. break;
  1540. case audioMasterGetNumAutomatableParameters:
  1541. // Deprecated in VST SDK 2.4
  1542. ret = carla_fixValue<intptr_t>(0, static_cast<intptr_t>(pData->engine->getOptions().maxParameters), fEffect->numParams);
  1543. break;
  1544. case audioMasterGetParameterQuantization:
  1545. // Deprecated in VST SDK 2.4
  1546. ret = 1; // full single float precision
  1547. break;
  1548. #endif
  1549. #if 0
  1550. case audioMasterIOChanged:
  1551. CARLA_ASSERT(pData->enabled);
  1552. // TESTING
  1553. if (! pData->enabled)
  1554. {
  1555. ret = 1;
  1556. break;
  1557. }
  1558. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1559. {
  1560. carla_stderr2("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1561. return 0;
  1562. }
  1563. engineProcessLock();
  1564. m_enabled = false;
  1565. engineProcessUnlock();
  1566. if (m_active)
  1567. {
  1568. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1569. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1570. }
  1571. reload();
  1572. if (m_active)
  1573. {
  1574. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1575. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1576. }
  1577. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1578. ret = 1;
  1579. break;
  1580. #endif
  1581. #if ! VST_FORCE_DEPRECATED
  1582. case audioMasterNeedIdle:
  1583. // Deprecated in VST SDK 2.4
  1584. fNeedIdle = true;
  1585. ret = 1;
  1586. break;
  1587. #endif
  1588. case audioMasterSizeWindow:
  1589. CARLA_SAFE_ASSERT_BREAK(fUI.window != nullptr);
  1590. CARLA_SAFE_ASSERT_BREAK(index > 0);
  1591. CARLA_SAFE_ASSERT_BREAK(value > 0);
  1592. fUI.window->setSize(static_cast<uint>(index), static_cast<uint>(value), true);
  1593. ret = 1;
  1594. break;
  1595. case audioMasterGetSampleRate:
  1596. ret = static_cast<intptr_t>(pData->engine->getSampleRate());
  1597. break;
  1598. case audioMasterGetBlockSize:
  1599. ret = static_cast<intptr_t>(pData->engine->getBufferSize());
  1600. break;
  1601. case audioMasterGetInputLatency:
  1602. ret = 0;
  1603. break;
  1604. case audioMasterGetOutputLatency:
  1605. ret = 0;
  1606. break;
  1607. #if ! VST_FORCE_DEPRECATED
  1608. case audioMasterGetPreviousPlug:
  1609. // Deprecated in VST SDK 2.4
  1610. // TODO
  1611. break;
  1612. case audioMasterGetNextPlug:
  1613. // Deprecated in VST SDK 2.4
  1614. // TODO
  1615. break;
  1616. case audioMasterWillReplaceOrAccumulate:
  1617. // Deprecated in VST SDK 2.4
  1618. ret = 1; // replace
  1619. break;
  1620. #endif
  1621. case audioMasterGetCurrentProcessLevel:
  1622. if (pthread_equal(pthread_self(), fProcThread))
  1623. {
  1624. CARLA_SAFE_ASSERT(fIsProcessing);
  1625. if (pData->engine->isOffline())
  1626. ret = kVstProcessLevelOffline;
  1627. else
  1628. ret = kVstProcessLevelRealtime;
  1629. }
  1630. else
  1631. ret = kVstProcessLevelUser;
  1632. break;
  1633. case audioMasterGetAutomationState:
  1634. ret = pData->active ? kVstAutomationReadWrite : kVstAutomationOff;
  1635. break;
  1636. case audioMasterOfflineStart:
  1637. case audioMasterOfflineRead:
  1638. case audioMasterOfflineWrite:
  1639. case audioMasterOfflineGetCurrentPass:
  1640. case audioMasterOfflineGetCurrentMetaPass:
  1641. // TODO
  1642. break;
  1643. #if ! VST_FORCE_DEPRECATED
  1644. case audioMasterSetOutputSampleRate:
  1645. // Deprecated in VST SDK 2.4
  1646. break;
  1647. case audioMasterGetOutputSpeakerArrangement:
  1648. // Deprecated in VST SDK 2.4
  1649. // TODO
  1650. break;
  1651. #endif
  1652. case audioMasterVendorSpecific:
  1653. if (index == 0xedcd && value == 0 && ptr != nullptr && std::strcmp((const char*)ptr, "EditorClosed") == 0)
  1654. {
  1655. CARLA_SAFE_ASSERT_BREAK(fUI.type == UI::UI_EXTERNAL);
  1656. handlePluginUIClosed();
  1657. break;
  1658. }
  1659. // TODO - cockos extensions
  1660. break;
  1661. #if ! VST_FORCE_DEPRECATED
  1662. case audioMasterSetIcon:
  1663. // Deprecated in VST SDK 2.4
  1664. break;
  1665. #endif
  1666. #if ! VST_FORCE_DEPRECATED
  1667. case audioMasterOpenWindow:
  1668. case audioMasterCloseWindow:
  1669. // Deprecated in VST SDK 2.4
  1670. // TODO
  1671. break;
  1672. #endif
  1673. case audioMasterGetDirectory:
  1674. // TODO
  1675. break;
  1676. case audioMasterUpdateDisplay:
  1677. // Idle UI if visible
  1678. if (fUI.isVisible)
  1679. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1680. // Update current program
  1681. if (pData->prog.count > 0)
  1682. {
  1683. const int32_t current = static_cast<int32_t>(dispatcher(effGetProgram, 0, 0, nullptr, 0.0f));
  1684. if (current >= 0 && current < static_cast<int32_t>(pData->prog.count))
  1685. {
  1686. char strBuf[STR_MAX+1] = { '\0' };
  1687. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  1688. if (pData->prog.names[current] != nullptr)
  1689. delete[] pData->prog.names[current];
  1690. pData->prog.names[current] = carla_strdup(strBuf);
  1691. if (pData->prog.current != current)
  1692. {
  1693. pData->prog.current = current;
  1694. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, current, 0, 0.0f, nullptr);
  1695. }
  1696. }
  1697. }
  1698. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  1699. ret = 1;
  1700. break;
  1701. case audioMasterBeginEdit:
  1702. case audioMasterEndEdit:
  1703. // TODO
  1704. break;
  1705. case audioMasterOpenFileSelector:
  1706. case audioMasterCloseFileSelector:
  1707. // TODO
  1708. break;
  1709. #if ! VST_FORCE_DEPRECATED
  1710. case audioMasterEditFile:
  1711. // Deprecated in VST SDK 2.4
  1712. // TODO
  1713. break;
  1714. case audioMasterGetChunkFile:
  1715. // Deprecated in VST SDK 2.4
  1716. // TODO
  1717. break;
  1718. case audioMasterGetInputSpeakerArrangement:
  1719. // Deprecated in VST SDK 2.4
  1720. // TODO
  1721. break;
  1722. #endif
  1723. default:
  1724. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f) UNDEF", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1725. break;
  1726. }
  1727. return ret;
  1728. // unused
  1729. (void)opt;
  1730. }
  1731. bool canDo(const char* const feature) const noexcept
  1732. {
  1733. try {
  1734. return (fEffect->dispatcher(fEffect, effCanDo, 0, 0, const_cast<char*>(feature), 0.0f) == 1);
  1735. } CARLA_SAFE_EXCEPTION_RETURN("vstPluginCanDo", false);
  1736. }
  1737. bool hasMidiInput() const noexcept
  1738. {
  1739. return (canDo("receiveVstEvents") || canDo("receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (pData->hints & PLUGIN_WANTS_MIDI_INPUT));
  1740. }
  1741. bool hasMidiOutput() const noexcept
  1742. {
  1743. return (canDo("sendVstEvents") || canDo("sendVstMidiEvent"));
  1744. }
  1745. // -------------------------------------------------------------------
  1746. const void* getNativeDescriptor() const noexcept override
  1747. {
  1748. return fEffect;
  1749. }
  1750. // -------------------------------------------------------------------
  1751. public:
  1752. bool init(const char* const filename, const char* const name, const int64_t uniqueId)
  1753. {
  1754. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1755. // ---------------------------------------------------------------
  1756. // first checks
  1757. if (pData->client != nullptr)
  1758. {
  1759. pData->engine->setLastError("Plugin client is already registered");
  1760. return false;
  1761. }
  1762. if (filename == nullptr || filename[0] == '\0')
  1763. {
  1764. pData->engine->setLastError("null filename");
  1765. return false;
  1766. }
  1767. // ---------------------------------------------------------------
  1768. // open DLL
  1769. if (! pData->libOpen(filename))
  1770. {
  1771. pData->engine->setLastError(pData->libError(filename));
  1772. return false;
  1773. }
  1774. // ---------------------------------------------------------------
  1775. // get DLL main entry
  1776. VST_Function vstFn = (VST_Function)pData->libSymbol("VSTPluginMain");
  1777. if (vstFn == nullptr)
  1778. {
  1779. vstFn = (VST_Function)pData->libSymbol("main");
  1780. if (vstFn == nullptr)
  1781. {
  1782. pData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1783. return false;
  1784. }
  1785. }
  1786. // ---------------------------------------------------------------
  1787. // initialize plugin (part 1)
  1788. sLastVstPlugin = this;
  1789. fEffect = vstFn(carla_vst_audioMasterCallback);
  1790. sLastVstPlugin = nullptr;
  1791. if (fEffect == nullptr)
  1792. {
  1793. pData->engine->setLastError("Plugin failed to initialize");
  1794. return false;
  1795. }
  1796. if (fEffect->magic != kEffectMagic)
  1797. {
  1798. pData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
  1799. return false;
  1800. }
  1801. #ifdef VESTIGE_HEADER
  1802. fEffect->ptr1 = this;
  1803. #else
  1804. fEffect->resvd1 = (intptr_t)this;
  1805. #endif
  1806. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1807. // ---------------------------------------------------------------
  1808. // get info
  1809. if (name != nullptr && name[0] != '\0')
  1810. {
  1811. pData->name = pData->engine->getUniquePluginName(name);
  1812. }
  1813. else
  1814. {
  1815. char strBuf[STR_MAX+1];
  1816. carla_zeroChar(strBuf, STR_MAX+1);
  1817. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1818. if (strBuf[0] != '\0')
  1819. pData->name = pData->engine->getUniquePluginName(strBuf);
  1820. else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
  1821. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1822. else
  1823. pData->name = pData->engine->getUniquePluginName("unknown");
  1824. }
  1825. pData->filename = carla_strdup(filename);
  1826. // ---------------------------------------------------------------
  1827. // register client
  1828. pData->client = pData->engine->addClient(this);
  1829. if (pData->client == nullptr || ! pData->client->isOk())
  1830. {
  1831. pData->engine->setLastError("Failed to register plugin client");
  1832. return false;
  1833. }
  1834. // ---------------------------------------------------------------
  1835. // initialize plugin (part 2)
  1836. #if ! VST_FORCE_DEPRECATED
  1837. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1838. #endif
  1839. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1840. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
  1841. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1842. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1843. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  1844. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>("hasCockosExtensions"), 0.0f)) == 0xbeef0000)
  1845. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1846. // ---------------------------------------------------------------
  1847. // gui stuff
  1848. if (fEffect->flags & effFlagsHasEditor)
  1849. {
  1850. fUI.type = UI::UI_EMBED;
  1851. if ((fEffect->flags & effFlagsProgramChunks) == 0 && pData->engine->getOptions().preferUiBridges)
  1852. {
  1853. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  1854. #if defined(CARLA_OS_LINUX)
  1855. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-vst-x11";
  1856. #endif
  1857. if (bridgeBinary.isNotEmpty() && File(bridgeBinary.buffer()).existsAsFile())
  1858. {
  1859. pData->osc.thread.setOscData(bridgeBinary, nullptr);
  1860. fUI.type = UI::UI_OSC;
  1861. }
  1862. }
  1863. }
  1864. else if (vstPluginCanDo(fEffect, "ExternalUI"))
  1865. {
  1866. fUI.type = UI::UI_EXTERNAL;
  1867. }
  1868. // ---------------------------------------------------------------
  1869. // set default options
  1870. pData->options = 0x0;
  1871. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1872. if (fEffect->flags & effFlagsProgramChunks)
  1873. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1874. if (hasMidiInput())
  1875. {
  1876. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1877. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1878. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1879. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1880. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1881. }
  1882. return true;
  1883. // unused
  1884. (void)uniqueId;
  1885. }
  1886. private:
  1887. int fUnique1;
  1888. AEffect* fEffect;
  1889. uint32_t fMidiEventCount;
  1890. VstMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1891. VstTimeInfo fTimeInfo;
  1892. bool fNeedIdle;
  1893. void* fLastChunk;
  1894. bool fIsProcessing;
  1895. pthread_t fProcThread;
  1896. struct FixedVstEvents {
  1897. int32_t numEvents;
  1898. intptr_t reserved;
  1899. VstEvent* data[kPluginMaxMidiEvents*2];
  1900. FixedVstEvents()
  1901. : numEvents(0),
  1902. reserved(0)
  1903. {
  1904. carla_fill<VstEvent*>(data, nullptr, kPluginMaxMidiEvents*2);
  1905. }
  1906. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  1907. } fEvents;
  1908. struct UI {
  1909. enum Type {
  1910. UI_NULL = 0,
  1911. UI_EMBED = 1,
  1912. UI_EXTERNAL = 2,
  1913. UI_OSC = 3
  1914. } type;
  1915. bool isVisible; // not used in OSC mode
  1916. CarlaPluginUI* window;
  1917. UI()
  1918. : type(UI_NULL),
  1919. isVisible(false),
  1920. window(nullptr) {}
  1921. ~UI()
  1922. {
  1923. CARLA_ASSERT(! isVisible);
  1924. if (window != nullptr)
  1925. {
  1926. delete window;
  1927. window = nullptr;
  1928. }
  1929. }
  1930. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  1931. } fUI;
  1932. int fUnique2;
  1933. static VstPlugin* sLastVstPlugin;
  1934. // -------------------------------------------------------------------
  1935. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1936. {
  1937. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1938. if (std::strcmp(feature, "supplyIdle") == 0)
  1939. return 1;
  1940. if (std::strcmp(feature, "sendVstEvents") == 0)
  1941. return 1;
  1942. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1943. return 1;
  1944. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1945. return 1;
  1946. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1947. return 1;
  1948. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1949. return 1;
  1950. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1951. return 1;
  1952. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1953. return -1;
  1954. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1955. return -1;
  1956. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1957. return 1;
  1958. if (std::strcmp(feature, "sizeWindow") == 0)
  1959. return 1;
  1960. if (std::strcmp(feature, "offline") == 0)
  1961. return -1;
  1962. if (std::strcmp(feature, "openFileSelector") == 0)
  1963. return -1;
  1964. if (std::strcmp(feature, "closeFileSelector") == 0)
  1965. return -1;
  1966. if (std::strcmp(feature, "startStopProcess") == 0)
  1967. return 1;
  1968. if (std::strcmp(feature, "supportShell") == 0)
  1969. return -1;
  1970. if (std::strcmp(feature, "shellCategory") == 0)
  1971. return -1;
  1972. // unimplemented
  1973. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1974. return 0;
  1975. }
  1976. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1977. {
  1978. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1979. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1980. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1981. #endif
  1982. switch (opcode)
  1983. {
  1984. case audioMasterVersion:
  1985. return kVstVersion;
  1986. case audioMasterGetVendorString:
  1987. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1988. std::strcpy((char*)ptr, "falkTX");
  1989. return 1;
  1990. case audioMasterGetProductString:
  1991. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1992. std::strcpy((char*)ptr, "Carla");
  1993. return 1;
  1994. case audioMasterGetVendorVersion:
  1995. return 0x110; // 1.1.0
  1996. case audioMasterCanDo:
  1997. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1998. return carla_vst_hostCanDo((const char*)ptr);
  1999. case audioMasterGetLanguage:
  2000. return kVstLangEnglish;
  2001. }
  2002. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  2003. VstPlugin* self = nullptr;
  2004. if (effect != nullptr)
  2005. {
  2006. #ifdef VESTIGE_HEADER
  2007. if (effect->ptr1 != nullptr)
  2008. {
  2009. self = (VstPlugin*)effect->ptr1;
  2010. if (self->fUnique1 != self->fUnique2)
  2011. self = nullptr;
  2012. }
  2013. #else
  2014. if (effect->resvd1 != 0)
  2015. {
  2016. self = (VstPlugin*)effect->resvd1;
  2017. if (self->fUnique1 != self->fUnique2)
  2018. self = nullptr;
  2019. }
  2020. #endif
  2021. if (self != nullptr)
  2022. {
  2023. if (self->fEffect == nullptr)
  2024. self->fEffect = effect;
  2025. if (self->fEffect != effect)
  2026. {
  2027. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  2028. self = nullptr;
  2029. }
  2030. }
  2031. else if (sLastVstPlugin != nullptr)
  2032. {
  2033. #ifdef VESTIGE_HEADER
  2034. effect->ptr1 = sLastVstPlugin;
  2035. #else
  2036. effect->resvd1 = (intptr_t)sLastVstPlugin;
  2037. #endif
  2038. self = sLastVstPlugin;
  2039. }
  2040. }
  2041. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  2042. }
  2043. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VstPlugin)
  2044. };
  2045. VstPlugin* VstPlugin::sLastVstPlugin = nullptr;
  2046. CARLA_BACKEND_END_NAMESPACE
  2047. #endif // ! USE_JUCE_FOR_VST
  2048. // -------------------------------------------------------------------------------------------------------------------
  2049. CARLA_BACKEND_START_NAMESPACE
  2050. CarlaPlugin* CarlaPlugin::newVST(const Initializer& init)
  2051. {
  2052. carla_debug("CarlaPlugin::newVST({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.uniqueId);
  2053. #ifdef USE_JUCE_FOR_VST
  2054. return newJuce(init, "VST");
  2055. #else
  2056. VstPlugin* const plugin(new VstPlugin(init.engine, init.id));
  2057. if (! plugin->init(init.filename, init.name, init.uniqueId))
  2058. {
  2059. delete plugin;
  2060. return nullptr;
  2061. }
  2062. plugin->reload();
  2063. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  2064. {
  2065. init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  2066. delete plugin;
  2067. return nullptr;
  2068. }
  2069. return plugin;
  2070. #endif
  2071. }
  2072. // -------------------------------------------------------------------------------------------------------------------
  2073. CARLA_BACKEND_END_NAMESPACE