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 82KB

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