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

VstPlugin.cpp 87KB

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