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.

CarlaPluginVST2.cpp 88KB

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