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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  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, false);
  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. void handlePluginUIResized(const uint width, const uint height) override
  1425. {
  1426. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::UI_EMBED,);
  1427. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1428. carla_debug("VstPlugin::handlePluginUIResized(%u, %u)", width, height);
  1429. return; // unused
  1430. (void)width; (void)height;
  1431. }
  1432. // -------------------------------------------------------------------
  1433. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt) const noexcept
  1434. {
  1435. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  1436. #ifdef DEBUG
  1437. if (opcode != effIdle && opcode != effEditIdle && opcode != effProcessEvents)
  1438. carla_debug("VstPlugin::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1439. #endif
  1440. try {
  1441. return fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt);
  1442. } CARLA_SAFE_EXCEPTION_RETURN("Vst dispatcher", 0);
  1443. }
  1444. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1445. {
  1446. #ifdef DEBUG
  1447. if (opcode != audioMasterGetTime)
  1448. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1449. #endif
  1450. intptr_t ret = 0;
  1451. switch (opcode)
  1452. {
  1453. case audioMasterAutomate: {
  1454. CARLA_SAFE_ASSERT_BREAK(pData->enabled);
  1455. // plugins should never do this:
  1456. CARLA_SAFE_ASSERT_INT(index >= 0 && index < static_cast<int32_t>(pData->param.count), index);
  1457. if (index < 0 || index >= static_cast<int32_t>(pData->param.count))
  1458. break;
  1459. const uint32_t uindex(static_cast<uint32_t>(index));
  1460. const float fixedValue(pData->param.getFixedValue(uindex, opt));
  1461. // Called from plugin process thread, nasty!
  1462. if (pthread_equal(pthread_self(), fProcThread))
  1463. {
  1464. CARLA_SAFE_ASSERT(fIsProcessing);
  1465. pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1466. }
  1467. // Called from UI
  1468. else if (fUI.isVisible)
  1469. {
  1470. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1471. }
  1472. // Unknown
  1473. // TODO - check if plugin or UI is initializing
  1474. else
  1475. {
  1476. carla_stdout("audioMasterAutomate called from unknown source");
  1477. setParameterValue(uindex, fixedValue, true, true, true);
  1478. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1479. }
  1480. break;
  1481. }
  1482. case audioMasterCurrentId:
  1483. // TODO
  1484. // if using old sdk, return effect->uniqueID
  1485. break;
  1486. case audioMasterIdle:
  1487. //pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1488. //pData->engine->idle();
  1489. break;
  1490. #if ! VST_FORCE_DEPRECATED
  1491. case audioMasterPinConnected:
  1492. // Deprecated in VST SDK 2.4
  1493. // TODO
  1494. break;
  1495. case audioMasterWantMidi:
  1496. // Deprecated in VST SDK 2.4
  1497. pData->hints |= PLUGIN_WANTS_MIDI_INPUT;
  1498. break;
  1499. #endif
  1500. case audioMasterGetTime:
  1501. ret = (intptr_t)&fTimeInfo;
  1502. break;
  1503. case audioMasterProcessEvents:
  1504. CARLA_SAFE_ASSERT_RETURN(pData->enabled, 0);
  1505. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, 0);
  1506. CARLA_SAFE_ASSERT_RETURN(pData->event.portOut != nullptr, 0);
  1507. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1508. return 0;
  1509. if (const VstEvents* const vstEvents = (const VstEvents*)ptr)
  1510. {
  1511. for (int32_t i=0; i < vstEvents->numEvents && i < kPluginMaxMidiEvents*2; ++i)
  1512. {
  1513. if (vstEvents->events[i] == nullptr)
  1514. break;
  1515. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)vstEvents->events[i]);
  1516. if (vstMidiEvent->type != kVstMidiType)
  1517. continue;
  1518. // reverse-find first free event, and put it there
  1519. for (uint32_t j=(kPluginMaxMidiEvents*2)-1; j >= fMidiEventCount; --j)
  1520. {
  1521. if (fMidiEvents[j].type == 0)
  1522. {
  1523. std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. }
  1529. ret = 1;
  1530. break;
  1531. #if ! VST_FORCE_DEPRECATED
  1532. case audioMasterSetTime:
  1533. // Deprecated in VST SDK 2.4
  1534. break;
  1535. case audioMasterTempoAt:
  1536. // Deprecated in VST SDK 2.4
  1537. ret = static_cast<intptr_t>(fTimeInfo.tempo * 10000);
  1538. break;
  1539. case audioMasterGetNumAutomatableParameters:
  1540. // Deprecated in VST SDK 2.4
  1541. ret = carla_fixValue<intptr_t>(0, static_cast<intptr_t>(pData->engine->getOptions().maxParameters), fEffect->numParams);
  1542. break;
  1543. case audioMasterGetParameterQuantization:
  1544. // Deprecated in VST SDK 2.4
  1545. ret = 1; // full single float precision
  1546. break;
  1547. #endif
  1548. #if 0
  1549. case audioMasterIOChanged:
  1550. CARLA_ASSERT(pData->enabled);
  1551. // TESTING
  1552. if (! pData->enabled)
  1553. {
  1554. ret = 1;
  1555. break;
  1556. }
  1557. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1558. {
  1559. carla_stderr2("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1560. return 0;
  1561. }
  1562. engineProcessLock();
  1563. m_enabled = false;
  1564. engineProcessUnlock();
  1565. if (m_active)
  1566. {
  1567. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1568. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1569. }
  1570. reload();
  1571. if (m_active)
  1572. {
  1573. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1574. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1575. }
  1576. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1577. ret = 1;
  1578. break;
  1579. #endif
  1580. #if ! VST_FORCE_DEPRECATED
  1581. case audioMasterNeedIdle:
  1582. // Deprecated in VST SDK 2.4
  1583. fNeedIdle = true;
  1584. ret = 1;
  1585. break;
  1586. #endif
  1587. case audioMasterSizeWindow:
  1588. CARLA_SAFE_ASSERT_BREAK(fUI.window != nullptr);
  1589. CARLA_SAFE_ASSERT_BREAK(index > 0);
  1590. CARLA_SAFE_ASSERT_BREAK(value > 0);
  1591. fUI.window->setSize(static_cast<uint>(index), static_cast<uint>(value), true);
  1592. ret = 1;
  1593. break;
  1594. case audioMasterGetSampleRate:
  1595. ret = static_cast<intptr_t>(pData->engine->getSampleRate());
  1596. break;
  1597. case audioMasterGetBlockSize:
  1598. ret = static_cast<intptr_t>(pData->engine->getBufferSize());
  1599. break;
  1600. case audioMasterGetInputLatency:
  1601. ret = 0;
  1602. break;
  1603. case audioMasterGetOutputLatency:
  1604. ret = 0;
  1605. break;
  1606. #if ! VST_FORCE_DEPRECATED
  1607. case audioMasterGetPreviousPlug:
  1608. // Deprecated in VST SDK 2.4
  1609. // TODO
  1610. break;
  1611. case audioMasterGetNextPlug:
  1612. // Deprecated in VST SDK 2.4
  1613. // TODO
  1614. break;
  1615. case audioMasterWillReplaceOrAccumulate:
  1616. // Deprecated in VST SDK 2.4
  1617. ret = 1; // replace
  1618. break;
  1619. #endif
  1620. case audioMasterGetCurrentProcessLevel:
  1621. if (pthread_equal(pthread_self(), fProcThread))
  1622. {
  1623. CARLA_SAFE_ASSERT(fIsProcessing);
  1624. if (pData->engine->isOffline())
  1625. ret = kVstProcessLevelOffline;
  1626. else
  1627. ret = kVstProcessLevelRealtime;
  1628. }
  1629. else
  1630. ret = kVstProcessLevelUser;
  1631. break;
  1632. case audioMasterGetAutomationState:
  1633. ret = pData->active ? kVstAutomationReadWrite : kVstAutomationOff;
  1634. break;
  1635. case audioMasterOfflineStart:
  1636. case audioMasterOfflineRead:
  1637. case audioMasterOfflineWrite:
  1638. case audioMasterOfflineGetCurrentPass:
  1639. case audioMasterOfflineGetCurrentMetaPass:
  1640. // TODO
  1641. break;
  1642. #if ! VST_FORCE_DEPRECATED
  1643. case audioMasterSetOutputSampleRate:
  1644. // Deprecated in VST SDK 2.4
  1645. break;
  1646. case audioMasterGetOutputSpeakerArrangement:
  1647. // Deprecated in VST SDK 2.4
  1648. // TODO
  1649. break;
  1650. #endif
  1651. case audioMasterVendorSpecific:
  1652. if (index == 0xedcd && value == 0 && ptr != nullptr && std::strcmp((const char*)ptr, "EditorClosed") == 0)
  1653. {
  1654. CARLA_SAFE_ASSERT_BREAK(fUI.type == UI::UI_EXTERNAL);
  1655. handlePluginUIClosed();
  1656. break;
  1657. }
  1658. // TODO - cockos extensions
  1659. break;
  1660. #if ! VST_FORCE_DEPRECATED
  1661. case audioMasterSetIcon:
  1662. // Deprecated in VST SDK 2.4
  1663. break;
  1664. #endif
  1665. #if ! VST_FORCE_DEPRECATED
  1666. case audioMasterOpenWindow:
  1667. case audioMasterCloseWindow:
  1668. // Deprecated in VST SDK 2.4
  1669. // TODO
  1670. break;
  1671. #endif
  1672. case audioMasterGetDirectory:
  1673. // TODO
  1674. break;
  1675. case audioMasterUpdateDisplay:
  1676. // Idle UI if visible
  1677. if (fUI.isVisible)
  1678. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1679. // Update current program
  1680. if (pData->prog.count > 0)
  1681. {
  1682. const int32_t current = static_cast<int32_t>(dispatcher(effGetProgram, 0, 0, nullptr, 0.0f));
  1683. if (current >= 0 && current < static_cast<int32_t>(pData->prog.count))
  1684. {
  1685. char strBuf[STR_MAX+1] = { '\0' };
  1686. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  1687. if (pData->prog.names[current] != nullptr)
  1688. delete[] pData->prog.names[current];
  1689. pData->prog.names[current] = carla_strdup(strBuf);
  1690. if (pData->prog.current != current)
  1691. {
  1692. pData->prog.current = current;
  1693. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, current, 0, 0.0f, nullptr);
  1694. }
  1695. }
  1696. }
  1697. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  1698. ret = 1;
  1699. break;
  1700. case audioMasterBeginEdit:
  1701. case audioMasterEndEdit:
  1702. // TODO
  1703. break;
  1704. case audioMasterOpenFileSelector:
  1705. case audioMasterCloseFileSelector:
  1706. // TODO
  1707. break;
  1708. #if ! VST_FORCE_DEPRECATED
  1709. case audioMasterEditFile:
  1710. // Deprecated in VST SDK 2.4
  1711. // TODO
  1712. break;
  1713. case audioMasterGetChunkFile:
  1714. // Deprecated in VST SDK 2.4
  1715. // TODO
  1716. break;
  1717. case audioMasterGetInputSpeakerArrangement:
  1718. // Deprecated in VST SDK 2.4
  1719. // TODO
  1720. break;
  1721. #endif
  1722. default:
  1723. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f) UNDEF", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1724. break;
  1725. }
  1726. return ret;
  1727. // unused
  1728. (void)opt;
  1729. }
  1730. bool canDo(const char* const feature) const noexcept
  1731. {
  1732. try {
  1733. return (fEffect->dispatcher(fEffect, effCanDo, 0, 0, const_cast<char*>(feature), 0.0f) == 1);
  1734. } CARLA_SAFE_EXCEPTION_RETURN("vstPluginCanDo", false);
  1735. }
  1736. bool hasMidiInput() const noexcept
  1737. {
  1738. return (canDo("receiveVstEvents") || canDo("receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (pData->hints & PLUGIN_WANTS_MIDI_INPUT));
  1739. }
  1740. bool hasMidiOutput() const noexcept
  1741. {
  1742. return (canDo("sendVstEvents") || canDo("sendVstMidiEvent"));
  1743. }
  1744. // -------------------------------------------------------------------
  1745. const void* getNativeDescriptor() const noexcept override
  1746. {
  1747. return fEffect;
  1748. }
  1749. // -------------------------------------------------------------------
  1750. public:
  1751. bool init(const char* const filename, const char* const name, const int64_t uniqueId)
  1752. {
  1753. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1754. // ---------------------------------------------------------------
  1755. // first checks
  1756. if (pData->client != nullptr)
  1757. {
  1758. pData->engine->setLastError("Plugin client is already registered");
  1759. return false;
  1760. }
  1761. if (filename == nullptr || filename[0] == '\0')
  1762. {
  1763. pData->engine->setLastError("null filename");
  1764. return false;
  1765. }
  1766. // ---------------------------------------------------------------
  1767. // open DLL
  1768. if (! pData->libOpen(filename))
  1769. {
  1770. pData->engine->setLastError(pData->libError(filename));
  1771. return false;
  1772. }
  1773. // ---------------------------------------------------------------
  1774. // get DLL main entry
  1775. VST_Function vstFn = (VST_Function)pData->libSymbol("VSTPluginMain");
  1776. if (vstFn == nullptr)
  1777. {
  1778. vstFn = (VST_Function)pData->libSymbol("main");
  1779. if (vstFn == nullptr)
  1780. {
  1781. pData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1782. return false;
  1783. }
  1784. }
  1785. // ---------------------------------------------------------------
  1786. // initialize plugin (part 1)
  1787. sLastVstPlugin = this;
  1788. fEffect = vstFn(carla_vst_audioMasterCallback);
  1789. sLastVstPlugin = nullptr;
  1790. if (fEffect == nullptr)
  1791. {
  1792. pData->engine->setLastError("Plugin failed to initialize");
  1793. return false;
  1794. }
  1795. if (fEffect->magic != kEffectMagic)
  1796. {
  1797. pData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
  1798. return false;
  1799. }
  1800. #ifdef VESTIGE_HEADER
  1801. fEffect->ptr1 = this;
  1802. #else
  1803. fEffect->resvd1 = (intptr_t)this;
  1804. #endif
  1805. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1806. // ---------------------------------------------------------------
  1807. // get info
  1808. if (name != nullptr && name[0] != '\0')
  1809. {
  1810. pData->name = pData->engine->getUniquePluginName(name);
  1811. }
  1812. else
  1813. {
  1814. char strBuf[STR_MAX+1];
  1815. carla_zeroChar(strBuf, STR_MAX+1);
  1816. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1817. if (strBuf[0] != '\0')
  1818. pData->name = pData->engine->getUniquePluginName(strBuf);
  1819. else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
  1820. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1821. else
  1822. pData->name = pData->engine->getUniquePluginName("unknown");
  1823. }
  1824. pData->filename = carla_strdup(filename);
  1825. // ---------------------------------------------------------------
  1826. // register client
  1827. pData->client = pData->engine->addClient(this);
  1828. if (pData->client == nullptr || ! pData->client->isOk())
  1829. {
  1830. pData->engine->setLastError("Failed to register plugin client");
  1831. return false;
  1832. }
  1833. // ---------------------------------------------------------------
  1834. // initialize plugin (part 2)
  1835. #if ! VST_FORCE_DEPRECATED
  1836. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1837. #endif
  1838. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1839. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
  1840. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1841. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1842. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  1843. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>("hasCockosExtensions"), 0.0f)) == 0xbeef0000)
  1844. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1845. // ---------------------------------------------------------------
  1846. // gui stuff
  1847. if (fEffect->flags & effFlagsHasEditor)
  1848. {
  1849. fUI.type = UI::UI_EMBED;
  1850. if ((fEffect->flags & effFlagsProgramChunks) == 0 && pData->engine->getOptions().preferUiBridges)
  1851. {
  1852. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  1853. #if defined(CARLA_OS_LINUX)
  1854. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-vst-x11";
  1855. #endif
  1856. if (bridgeBinary.isNotEmpty() && File(bridgeBinary.buffer()).existsAsFile())
  1857. {
  1858. pData->osc.thread.setOscData(bridgeBinary, nullptr);
  1859. fUI.type = UI::UI_OSC;
  1860. }
  1861. }
  1862. }
  1863. else if (vstPluginCanDo(fEffect, "ExternalUI"))
  1864. {
  1865. fUI.type = UI::UI_EXTERNAL;
  1866. }
  1867. // ---------------------------------------------------------------
  1868. // set default options
  1869. pData->options = 0x0;
  1870. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1871. if (fEffect->flags & effFlagsProgramChunks)
  1872. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1873. if (hasMidiInput())
  1874. {
  1875. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1876. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1877. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1878. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1879. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1880. }
  1881. return true;
  1882. // unused
  1883. (void)uniqueId;
  1884. }
  1885. private:
  1886. int fUnique1;
  1887. AEffect* fEffect;
  1888. uint32_t fMidiEventCount;
  1889. VstMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1890. VstTimeInfo fTimeInfo;
  1891. bool fNeedIdle;
  1892. void* fLastChunk;
  1893. bool fIsProcessing;
  1894. pthread_t fProcThread;
  1895. struct FixedVstEvents {
  1896. int32_t numEvents;
  1897. intptr_t reserved;
  1898. VstEvent* data[kPluginMaxMidiEvents*2];
  1899. FixedVstEvents()
  1900. : numEvents(0),
  1901. reserved(0)
  1902. {
  1903. carla_fill<VstEvent*>(data, nullptr, kPluginMaxMidiEvents*2);
  1904. }
  1905. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  1906. } fEvents;
  1907. struct UI {
  1908. enum Type {
  1909. UI_NULL = 0,
  1910. UI_EMBED = 1,
  1911. UI_EXTERNAL = 2,
  1912. UI_OSC = 3
  1913. } type;
  1914. bool isVisible; // not used in OSC mode
  1915. CarlaPluginUI* window;
  1916. UI()
  1917. : type(UI_NULL),
  1918. isVisible(false),
  1919. window(nullptr) {}
  1920. ~UI()
  1921. {
  1922. CARLA_ASSERT(! isVisible);
  1923. if (window != nullptr)
  1924. {
  1925. delete window;
  1926. window = nullptr;
  1927. }
  1928. }
  1929. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  1930. } fUI;
  1931. int fUnique2;
  1932. static VstPlugin* sLastVstPlugin;
  1933. // -------------------------------------------------------------------
  1934. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1935. {
  1936. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1937. if (std::strcmp(feature, "supplyIdle") == 0)
  1938. return 1;
  1939. if (std::strcmp(feature, "sendVstEvents") == 0)
  1940. return 1;
  1941. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1942. return 1;
  1943. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1944. return 1;
  1945. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1946. return 1;
  1947. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1948. return 1;
  1949. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1950. return 1;
  1951. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1952. return -1;
  1953. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1954. return -1;
  1955. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1956. return 1;
  1957. if (std::strcmp(feature, "sizeWindow") == 0)
  1958. return 1;
  1959. if (std::strcmp(feature, "offline") == 0)
  1960. return -1;
  1961. if (std::strcmp(feature, "openFileSelector") == 0)
  1962. return -1;
  1963. if (std::strcmp(feature, "closeFileSelector") == 0)
  1964. return -1;
  1965. if (std::strcmp(feature, "startStopProcess") == 0)
  1966. return 1;
  1967. if (std::strcmp(feature, "supportShell") == 0)
  1968. return -1;
  1969. if (std::strcmp(feature, "shellCategory") == 0)
  1970. return -1;
  1971. // unimplemented
  1972. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1973. return 0;
  1974. }
  1975. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1976. {
  1977. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1978. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1979. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1980. #endif
  1981. switch (opcode)
  1982. {
  1983. case audioMasterVersion:
  1984. return kVstVersion;
  1985. case audioMasterGetVendorString:
  1986. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1987. std::strcpy((char*)ptr, "falkTX");
  1988. return 1;
  1989. case audioMasterGetProductString:
  1990. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1991. std::strcpy((char*)ptr, "Carla");
  1992. return 1;
  1993. case audioMasterGetVendorVersion:
  1994. return 0x110; // 1.1.0
  1995. case audioMasterCanDo:
  1996. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1997. return carla_vst_hostCanDo((const char*)ptr);
  1998. case audioMasterGetLanguage:
  1999. return kVstLangEnglish;
  2000. }
  2001. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  2002. VstPlugin* self = nullptr;
  2003. if (effect != nullptr)
  2004. {
  2005. #ifdef VESTIGE_HEADER
  2006. if (effect->ptr1 != nullptr)
  2007. {
  2008. self = (VstPlugin*)effect->ptr1;
  2009. if (self->fUnique1 != self->fUnique2)
  2010. self = nullptr;
  2011. }
  2012. #else
  2013. if (effect->resvd1 != 0)
  2014. {
  2015. self = (VstPlugin*)effect->resvd1;
  2016. if (self->fUnique1 != self->fUnique2)
  2017. self = nullptr;
  2018. }
  2019. #endif
  2020. if (self != nullptr)
  2021. {
  2022. if (self->fEffect == nullptr)
  2023. self->fEffect = effect;
  2024. if (self->fEffect != effect)
  2025. {
  2026. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  2027. self = nullptr;
  2028. }
  2029. }
  2030. else if (sLastVstPlugin != nullptr)
  2031. {
  2032. #ifdef VESTIGE_HEADER
  2033. effect->ptr1 = sLastVstPlugin;
  2034. #else
  2035. effect->resvd1 = (intptr_t)sLastVstPlugin;
  2036. #endif
  2037. self = sLastVstPlugin;
  2038. }
  2039. }
  2040. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  2041. }
  2042. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VstPlugin)
  2043. };
  2044. VstPlugin* VstPlugin::sLastVstPlugin = nullptr;
  2045. CARLA_BACKEND_END_NAMESPACE
  2046. #endif // ! USE_JUCE_FOR_VST
  2047. // -------------------------------------------------------------------------------------------------------------------
  2048. CARLA_BACKEND_START_NAMESPACE
  2049. CarlaPlugin* CarlaPlugin::newVST(const Initializer& init)
  2050. {
  2051. carla_debug("CarlaPlugin::newVST({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.uniqueId);
  2052. #ifdef USE_JUCE_FOR_VST
  2053. return newJuce(init, "VST");
  2054. #else
  2055. VstPlugin* const plugin(new VstPlugin(init.engine, init.id));
  2056. if (! plugin->init(init.filename, init.name, init.uniqueId))
  2057. {
  2058. delete plugin;
  2059. return nullptr;
  2060. }
  2061. plugin->reload();
  2062. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  2063. {
  2064. init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  2065. delete plugin;
  2066. return nullptr;
  2067. }
  2068. return plugin;
  2069. #endif
  2070. }
  2071. // -------------------------------------------------------------------------------------------------------------------
  2072. CARLA_BACKEND_END_NAMESPACE