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

VstPlugin.cpp 87KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588
  1. /*
  2. * Carla VST Plugin
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  20. # define USE_JUCE_FOR_VST
  21. #endif
  22. #ifndef USE_JUCE_FOR_VST
  23. #include "CarlaVstUtils.hpp"
  24. #include "CarlaMathUtils.hpp"
  25. #include "CarlaPluginUI.hpp"
  26. #include "juce_core.h"
  27. #include <pthread.h>
  28. #undef VST_FORCE_DEPRECATED
  29. #define VST_FORCE_DEPRECATED 0
  30. using juce::File;
  31. CARLA_BACKEND_START_NAMESPACE
  32. // -----------------------------------------------------
  33. const uint PLUGIN_CAN_PROCESS_REPLACING = 0x1000;
  34. const uint PLUGIN_HAS_COCKOS_EXTENSIONS = 0x2000;
  35. const uint PLUGIN_USES_OLD_VSTSDK = 0x4000;
  36. const uint PLUGIN_WANTS_MIDI_INPUT = 0x8000;
  37. static const int32_t kVstMidiEventSize = static_cast<int32_t>(sizeof(VstMidiEvent));
  38. // -----------------------------------------------------
  39. class VstPlugin : public CarlaPlugin,
  40. CarlaPluginUI::CloseCallback
  41. {
  42. public:
  43. VstPlugin(CarlaEngine* const engine, const uint id)
  44. : CarlaPlugin(engine, id),
  45. fUnique1(1),
  46. fEffect(nullptr),
  47. fMidiEventCount(0),
  48. fTimeInfo(),
  49. fNeedIdle(false),
  50. fLastChunk(nullptr),
  51. fIsProcessing(false),
  52. #ifdef PTW32_DLLPORT
  53. fProcThread({nullptr, 0}),
  54. #else
  55. fProcThread(0),
  56. #endif
  57. fEvents(),
  58. fUI(),
  59. fUnique2(2),
  60. leakDetector_VstPlugin()
  61. {
  62. carla_debug("VstPlugin::VstPlugin(%p, %i)", engine, id);
  63. carla_zeroStruct<VstMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  64. carla_zeroStruct<VstTimeInfo>(fTimeInfo);
  65. for (ushort i=0; i < kPluginMaxMidiEvents*2; ++i)
  66. fEvents.data[i] = (VstEvent*)&fMidiEvents[i];
  67. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_VST_GUI);
  68. #ifdef CARLA_OS_WIN
  69. fProcThread.p = nullptr;
  70. fProcThread.x = 0;
  71. #else
  72. fProcThread = 0;
  73. #endif
  74. // make plugin valid
  75. srand(id);
  76. fUnique1 = fUnique2 = rand();
  77. }
  78. ~VstPlugin() override
  79. {
  80. carla_debug("VstPlugin::~VstPlugin()");
  81. // close UI
  82. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  83. {
  84. showCustomUI(false);
  85. if (fUI.type == UI::UI_OSC)
  86. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  87. }
  88. pData->singleMutex.lock();
  89. pData->masterMutex.lock();
  90. if (pData->client != nullptr && pData->client->isActive())
  91. pData->client->deactivate();
  92. CARLA_ASSERT(! fIsProcessing);
  93. if (pData->active)
  94. {
  95. deactivate();
  96. pData->active = false;
  97. }
  98. if (fEffect != nullptr)
  99. {
  100. dispatcher(effClose, 0, 0, nullptr, 0.0f);
  101. fEffect = nullptr;
  102. }
  103. // make plugin invalid
  104. fUnique2 += 1;
  105. if (fLastChunk != nullptr)
  106. {
  107. std::free(fLastChunk);
  108. fLastChunk = nullptr;
  109. }
  110. clearBuffers();
  111. }
  112. // -------------------------------------------------------------------
  113. // Information (base)
  114. PluginType getType() const noexcept override
  115. {
  116. return PLUGIN_VST;
  117. }
  118. PluginCategory getCategory() const noexcept override
  119. {
  120. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, CarlaPlugin::getCategory());
  121. const intptr_t category(dispatcher(effGetPlugCategory, 0, 0, nullptr, 0.0f));
  122. switch (category)
  123. {
  124. case kPlugCategSynth:
  125. return PLUGIN_CATEGORY_SYNTH;
  126. case kPlugCategAnalysis:
  127. return PLUGIN_CATEGORY_UTILITY;
  128. case kPlugCategMastering:
  129. return PLUGIN_CATEGORY_DYNAMICS;
  130. case kPlugCategRoomFx:
  131. return PLUGIN_CATEGORY_DELAY;
  132. case kPlugCategRestoration:
  133. return PLUGIN_CATEGORY_UTILITY;
  134. case kPlugCategGenerator:
  135. return PLUGIN_CATEGORY_SYNTH;
  136. }
  137. if (fEffect->flags & effFlagsIsSynth)
  138. return PLUGIN_CATEGORY_SYNTH;
  139. return CarlaPlugin::getCategory();
  140. }
  141. int64_t getUniqueId() const noexcept override
  142. {
  143. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  144. return static_cast<int64_t>(fEffect->uniqueID);
  145. }
  146. // -------------------------------------------------------------------
  147. // Information (count)
  148. // nothing
  149. // -------------------------------------------------------------------
  150. // Information (current data)
  151. std::size_t getChunkData(void** const dataPtr) noexcept override
  152. {
  153. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  154. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  155. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  156. *dataPtr = nullptr;
  157. try {
  158. const intptr_t ret = dispatcher(effGetChunk, 0 /* bank */, 0, dataPtr, 0.0f);
  159. CARLA_SAFE_ASSERT_RETURN(ret >= 0, 0);
  160. return static_cast<std::size_t>(ret);
  161. } CARLA_SAFE_EXCEPTION_RETURN("VstPlugin::getChunkData", 0);
  162. }
  163. // -------------------------------------------------------------------
  164. // Information (per-plugin data)
  165. uint getOptionsAvailable() const noexcept override
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  168. uint options = 0x0;
  169. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  170. if (fEffect->flags & effFlagsProgramChunks)
  171. options |= PLUGIN_OPTION_USE_CHUNKS;
  172. if (getMidiInCount() == 0)
  173. {
  174. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  175. }
  176. else
  177. {
  178. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  179. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  180. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  181. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  182. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  183. }
  184. return options;
  185. }
  186. float getParameterValue(const uint32_t parameterId) const noexcept override
  187. {
  188. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0.0f);
  189. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  190. return fEffect->getParameter(fEffect, static_cast<int32_t>(parameterId));
  191. }
  192. void getLabel(char* const strBuf) const noexcept override
  193. {
  194. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  195. strBuf[0] = '\0';
  196. dispatcher(effGetProductString, 0, 0, strBuf, 0.0f);
  197. }
  198. void getMaker(char* const strBuf) const noexcept override
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  201. strBuf[0] = '\0';
  202. dispatcher(effGetVendorString, 0, 0, strBuf, 0.0f);
  203. }
  204. void getCopyright(char* const strBuf) const noexcept override
  205. {
  206. getMaker(strBuf);
  207. }
  208. void getRealName(char* const strBuf) const noexcept override
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  211. strBuf[0] = '\0';
  212. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  213. }
  214. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  217. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  218. strBuf[0] = '\0';
  219. dispatcher(effGetParamName, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  220. }
  221. void getParameterText(const uint32_t parameterId, char* const strBuf) const noexcept override
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  224. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  225. strBuf[0] = '\0';
  226. dispatcher(effGetParamDisplay, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  227. if (strBuf[0] == '\0')
  228. std::snprintf(strBuf, STR_MAX, "%f", getParameterValue(parameterId));
  229. }
  230. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  233. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  234. strBuf[0] = '\0';
  235. dispatcher(effGetParamLabel, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  236. }
  237. // -------------------------------------------------------------------
  238. // Set data (state)
  239. // nothing
  240. // -------------------------------------------------------------------
  241. // Set data (internal stuff)
  242. void setName(const char* const newName) override
  243. {
  244. CarlaPlugin::setName(newName);
  245. if (fUI.window != nullptr)
  246. {
  247. CarlaString guiTitle(pData->name);
  248. guiTitle += " (GUI)";
  249. fUI.window->setTitle(guiTitle.buffer());
  250. }
  251. }
  252. // -------------------------------------------------------------------
  253. // Set data (plugin-specific stuff)
  254. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  257. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  258. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  259. fEffect->setParameter(fEffect, static_cast<int32_t>(parameterId), fixedValue);
  260. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  261. }
  262. void setChunkData(const void* const data, const std::size_t dataSize) override
  263. {
  264. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  265. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  266. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  267. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  268. if (fLastChunk != nullptr)
  269. std::free(fLastChunk);
  270. fLastChunk = std::malloc(dataSize);
  271. CARLA_SAFE_ASSERT_RETURN(fLastChunk != nullptr,);
  272. std::memcpy(fLastChunk, data, dataSize);
  273. {
  274. const ScopedSingleProcessLocker spl(this, true);
  275. dispatcher(effSetChunk, 0 /* bank */, static_cast<intptr_t>(dataSize), fLastChunk, 0.0f);
  276. }
  277. // simulate an updateDisplay callback
  278. handleAudioMasterCallback(audioMasterUpdateDisplay, 0, 0, nullptr, 0.0f);
  279. #ifdef BUILD_BRIDGE
  280. const bool sendOsc(false);
  281. #else
  282. const bool sendOsc(pData->engine->isOscControlRegistered());
  283. #endif
  284. pData->updateParameterValues(this, sendOsc, true, false);
  285. }
  286. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  287. {
  288. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  289. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  290. if (index >= 0)
  291. {
  292. try {
  293. dispatcher(effBeginSetProgram, 0, 0, nullptr, 0.0f);
  294. } CARLA_SAFE_EXCEPTION_RETURN("effBeginSetProgram",);
  295. {
  296. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  297. try {
  298. dispatcher(effSetProgram, 0, index, nullptr, 0.0f);
  299. } CARLA_SAFE_EXCEPTION("effSetProgram");
  300. }
  301. try {
  302. dispatcher(effEndSetProgram, 0, 0, nullptr, 0.0f);
  303. } CARLA_SAFE_EXCEPTION("effEndSetProgram");
  304. }
  305. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  306. }
  307. // -------------------------------------------------------------------
  308. // Set ui stuff
  309. void showCustomUI(const bool yesNo) override
  310. {
  311. if (fUI.type == UI::UI_OSC)
  312. {
  313. if (yesNo)
  314. {
  315. pData->osc.data.clear();
  316. pData->osc.thread.startThread();
  317. }
  318. else
  319. {
  320. pData->transientTryCounter = 0;
  321. if (pData->osc.data.target != nullptr)
  322. {
  323. osc_send_hide(pData->osc.data);
  324. osc_send_quit(pData->osc.data);
  325. pData->osc.data.clear();
  326. }
  327. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  328. }
  329. return;
  330. }
  331. if (fUI.isVisible == yesNo)
  332. return;
  333. if (yesNo)
  334. {
  335. CarlaString uiTitle(pData->name);
  336. uiTitle += " (GUI)";
  337. intptr_t value = 0;
  338. void* vstPtr = nullptr;
  339. ERect* vstRect = nullptr;
  340. if (fUI.window == nullptr && fUI.type == UI::UI_EMBED)
  341. {
  342. const char* msg = nullptr;
  343. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  344. #if defined(CARLA_OS_LINUX)
  345. # ifdef HAVE_X11
  346. fUI.window = CarlaPluginUI::newX11(this, frontendWinId, false);
  347. # else
  348. msg = "UI is only for systems with X11";
  349. (void)frontendWinId; // unused
  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(const float** const audioIn, float** const audioOut, const float** const, float** const, 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(audioOut[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 = kVstMidiEventSize;
  873. fMidiEvents[k].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  874. fMidiEvents[k].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  875. fMidiEvents[k+i].type = kVstMidiType;
  876. fMidiEvents[k+i].byteSize = kVstMidiEventSize;
  877. fMidiEvents[k+i].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  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 = kVstMidiEventSize;
  888. fMidiEvents[i].midiData[0] = char(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  889. fMidiEvents[i].midiData[1] = 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. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  960. vstMidiEvent.type = kVstMidiType;
  961. vstMidiEvent.byteSize = kVstMidiEventSize;
  962. vstMidiEvent.midiData[0] = char((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  963. vstMidiEvent.midiData[1] = char(note.note);
  964. vstMidiEvent.midiData[2] = char(note.velo);
  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 startTime = 0;
  975. uint32_t timeOffset = 0;
  976. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  977. {
  978. const EngineEvent& event(pData->event.portIn->getEvent(i));
  979. if (event.time >= frames)
  980. continue;
  981. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  982. if (isSampleAccurate && event.time > timeOffset)
  983. {
  984. if (processSingle(audioIn, audioOut, event.time - timeOffset, timeOffset))
  985. {
  986. startTime = 0;
  987. timeOffset = event.time;
  988. if (fMidiEventCount > 0)
  989. {
  990. carla_zeroStruct<VstMidiEvent>(fMidiEvents, fMidiEventCount);
  991. fMidiEventCount = 0;
  992. }
  993. }
  994. else
  995. startTime += timeOffset;
  996. }
  997. switch (event.type)
  998. {
  999. case kEngineEventTypeNull:
  1000. break;
  1001. case kEngineEventTypeControl: {
  1002. const EngineControlEvent& ctrlEvent(event.ctrl);
  1003. switch (ctrlEvent.type)
  1004. {
  1005. case kEngineControlEventTypeNull:
  1006. break;
  1007. case kEngineControlEventTypeParameter: {
  1008. #ifndef BUILD_BRIDGE
  1009. // Control backend stuff
  1010. if (event.channel == pData->ctrlChannel)
  1011. {
  1012. float value;
  1013. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1014. {
  1015. value = ctrlEvent.value;
  1016. setDryWet(value, false, false);
  1017. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1018. break;
  1019. }
  1020. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1021. {
  1022. value = ctrlEvent.value*127.0f/100.0f;
  1023. setVolume(value, false, false);
  1024. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1025. break;
  1026. }
  1027. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1028. {
  1029. float left, right;
  1030. value = ctrlEvent.value/0.5f - 1.0f;
  1031. if (value < 0.0f)
  1032. {
  1033. left = -1.0f;
  1034. right = (value*2.0f)+1.0f;
  1035. }
  1036. else if (value > 0.0f)
  1037. {
  1038. left = (value*2.0f)-1.0f;
  1039. right = 1.0f;
  1040. }
  1041. else
  1042. {
  1043. left = -1.0f;
  1044. right = 1.0f;
  1045. }
  1046. setBalanceLeft(left, false, false);
  1047. setBalanceRight(right, false, false);
  1048. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1049. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1050. break;
  1051. }
  1052. }
  1053. #endif
  1054. // Control plugin parameters
  1055. uint32_t k;
  1056. for (k=0; k < pData->param.count; ++k)
  1057. {
  1058. if (pData->param.data[k].midiChannel != event.channel)
  1059. continue;
  1060. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1061. continue;
  1062. if (pData->param.data[k].type != PARAMETER_INPUT)
  1063. continue;
  1064. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1065. continue;
  1066. float value;
  1067. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1068. {
  1069. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1070. }
  1071. else
  1072. {
  1073. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1074. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1075. value = std::rint(value);
  1076. }
  1077. setParameterValue(k, value, false, false, false);
  1078. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1079. break;
  1080. }
  1081. // check if event is already handled
  1082. if (k != pData->param.count)
  1083. break;
  1084. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1085. {
  1086. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1087. continue;
  1088. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1089. carla_zeroStruct(vstMidiEvent);
  1090. vstMidiEvent.type = kVstMidiType;
  1091. vstMidiEvent.byteSize = kVstMidiEventSize;
  1092. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1093. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1094. vstMidiEvent.midiData[1] = char(ctrlEvent.param);
  1095. vstMidiEvent.midiData[2] = char(ctrlEvent.value*127.0f);
  1096. }
  1097. break;
  1098. } // case kEngineControlEventTypeParameter
  1099. case kEngineControlEventTypeMidiBank:
  1100. break;
  1101. case kEngineControlEventTypeMidiProgram:
  1102. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1103. {
  1104. if (ctrlEvent.param < pData->prog.count)
  1105. {
  1106. setProgram(ctrlEvent.param, false, false, false);
  1107. pData->postponeRtEvent(kPluginPostRtEventProgramChange, ctrlEvent.param, 0, 0.0f);
  1108. break;
  1109. }
  1110. }
  1111. break;
  1112. case kEngineControlEventTypeAllSoundOff:
  1113. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1114. {
  1115. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1116. continue;
  1117. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1118. carla_zeroStruct(vstMidiEvent);
  1119. vstMidiEvent.type = kVstMidiType;
  1120. vstMidiEvent.byteSize = kVstMidiEventSize;
  1121. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1122. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1123. vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1124. }
  1125. break;
  1126. case kEngineControlEventTypeAllNotesOff:
  1127. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1128. {
  1129. #ifndef BUILD_BRIDGE
  1130. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1131. {
  1132. allNotesOffSent = true;
  1133. sendMidiAllNotesOffToCallback();
  1134. }
  1135. #endif
  1136. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1137. continue;
  1138. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1139. carla_zeroStruct(vstMidiEvent);
  1140. vstMidiEvent.type = kVstMidiType;
  1141. vstMidiEvent.byteSize = kVstMidiEventSize;
  1142. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1143. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1144. vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1145. }
  1146. break;
  1147. } // switch (ctrlEvent.type)
  1148. break;
  1149. } // case kEngineEventTypeControl
  1150. case kEngineEventTypeMidi: {
  1151. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1152. continue;
  1153. const EngineMidiEvent& midiEvent(event.midi);
  1154. if (midiEvent.size > 3)
  1155. continue;
  1156. static_assert(3 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1157. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1158. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1159. continue;
  1160. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1161. continue;
  1162. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1163. continue;
  1164. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1165. continue;
  1166. // Fix bad note-off
  1167. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1168. status = MIDI_STATUS_NOTE_OFF;
  1169. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1170. carla_zeroStruct(vstMidiEvent);
  1171. vstMidiEvent.type = kVstMidiType;
  1172. vstMidiEvent.byteSize = kVstMidiEventSize;
  1173. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1174. vstMidiEvent.midiData[0] = char(status | (event.channel & MIDI_CHANNEL_BIT));
  1175. vstMidiEvent.midiData[1] = char(midiEvent.size >= 2 ? midiEvent.data[1] : 0);
  1176. vstMidiEvent.midiData[2] = char(midiEvent.size >= 3 ? midiEvent.data[2] : 0);
  1177. if (status == MIDI_STATUS_NOTE_ON)
  1178. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1179. else if (status == MIDI_STATUS_NOTE_OFF)
  1180. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1181. } break;
  1182. } // switch (event.type)
  1183. }
  1184. pData->postRtEvents.trySplice();
  1185. if (frames > timeOffset)
  1186. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1187. } // End of Event Input and Processing
  1188. // --------------------------------------------------------------------------------------------------------
  1189. // Plugin processing (no events)
  1190. else
  1191. {
  1192. processSingle(audioIn, audioOut, 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. const VstMidiEvent& vstMidiEvent(fMidiEvents[k]);
  1204. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.deltaFrames >= 0);
  1205. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.midiData[0] != 0);
  1206. uint8_t midiData[3];
  1207. midiData[0] = static_cast<uint8_t>(vstMidiEvent.midiData[0]);
  1208. midiData[1] = static_cast<uint8_t>(vstMidiEvent.midiData[1]);
  1209. midiData[2] = static_cast<uint8_t>(vstMidiEvent.midiData[2]);
  1210. pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(vstMidiEvent.deltaFrames), MIDI_GET_CHANNEL_FROM_DATA(midiData), 0, 3, midiData);
  1211. }
  1212. } // End of MIDI Output
  1213. }
  1214. bool processSingle(const float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1215. {
  1216. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1217. if (pData->audioIn.count > 0)
  1218. {
  1219. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1220. }
  1221. if (pData->audioOut.count > 0)
  1222. {
  1223. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1224. }
  1225. // --------------------------------------------------------------------------------------------------------
  1226. // Try lock, silence otherwise
  1227. if (pData->engine->isOffline())
  1228. {
  1229. pData->singleMutex.lock();
  1230. }
  1231. else if (! pData->singleMutex.tryLock())
  1232. {
  1233. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1234. {
  1235. for (uint32_t k=0; k < frames; ++k)
  1236. outBuffer[i][k+timeOffset] = 0.0f;
  1237. }
  1238. return false;
  1239. }
  1240. // --------------------------------------------------------------------------------------------------------
  1241. // Set audio buffers
  1242. float* vstInBuffer[pData->audioIn.count];
  1243. float* vstOutBuffer[pData->audioOut.count];
  1244. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1245. vstInBuffer[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1246. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1247. vstOutBuffer[i] = outBuffer[i]+timeOffset;
  1248. // --------------------------------------------------------------------------------------------------------
  1249. // Set MIDI events
  1250. if (fMidiEventCount > 0)
  1251. {
  1252. fEvents.numEvents = static_cast<int32_t>(fMidiEventCount);
  1253. fEvents.reserved = 0;
  1254. dispatcher(effProcessEvents, 0, 0, &fEvents, 0.0f);
  1255. }
  1256. // --------------------------------------------------------------------------------------------------------
  1257. // Run plugin
  1258. fIsProcessing = true;
  1259. if (pData->hints & PLUGIN_CAN_PROCESS_REPLACING)
  1260. {
  1261. fEffect->processReplacing(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1262. }
  1263. else
  1264. {
  1265. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1266. FloatVectorOperations::clear(vstOutBuffer[i], static_cast<int>(frames));
  1267. #if ! VST_FORCE_DEPRECATED
  1268. fEffect->process(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1269. #endif
  1270. }
  1271. fIsProcessing = false;
  1272. fTimeInfo.samplePos += frames;
  1273. #ifndef BUILD_BRIDGE
  1274. // --------------------------------------------------------------------------------------------------------
  1275. // Post-processing (dry/wet, volume and balance)
  1276. {
  1277. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && ! carla_compareFloats(pData->postProc.volume, 1.0f);
  1278. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && ! carla_compareFloats(pData->postProc.dryWet, 1.0f);
  1279. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_compareFloats(pData->postProc.balanceLeft, -1.0f) && carla_compareFloats(pData->postProc.balanceRight, 1.0f));
  1280. bool isPair;
  1281. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1282. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1283. {
  1284. // Dry/Wet
  1285. if (doDryWet)
  1286. {
  1287. for (uint32_t k=0; k < frames; ++k)
  1288. {
  1289. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k+timeOffset];
  1290. outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1291. }
  1292. }
  1293. // Balance
  1294. if (doBalance)
  1295. {
  1296. isPair = (i % 2 == 0);
  1297. if (isPair)
  1298. {
  1299. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1300. FloatVectorOperations::copy(oldBufLeft, outBuffer[i]+timeOffset, static_cast<int>(frames));
  1301. }
  1302. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1303. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1304. for (uint32_t k=0; k < frames; ++k)
  1305. {
  1306. if (isPair)
  1307. {
  1308. // left
  1309. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1310. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1311. }
  1312. else
  1313. {
  1314. // right
  1315. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1316. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1317. }
  1318. }
  1319. }
  1320. // Volume
  1321. if (doVolume)
  1322. {
  1323. for (uint32_t k=0; k < frames; ++k)
  1324. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1325. }
  1326. }
  1327. } // End of Post-processing
  1328. #endif
  1329. // --------------------------------------------------------------------------------------------------------
  1330. pData->singleMutex.unlock();
  1331. return true;
  1332. }
  1333. void bufferSizeChanged(const uint32_t newBufferSize) override
  1334. {
  1335. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1336. carla_debug("VstPlugin::bufferSizeChanged(%i)", newBufferSize);
  1337. if (pData->active)
  1338. deactivate();
  1339. #if ! VST_FORCE_DEPRECATED
  1340. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(newBufferSize), nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1341. #endif
  1342. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(newBufferSize), nullptr, 0.0f);
  1343. if (pData->active)
  1344. activate();
  1345. }
  1346. void sampleRateChanged(const double newSampleRate) override
  1347. {
  1348. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1349. carla_debug("VstPlugin::sampleRateChanged(%g)", newSampleRate);
  1350. if (pData->active)
  1351. deactivate();
  1352. #if ! VST_FORCE_DEPRECATED
  1353. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(newSampleRate));
  1354. #endif
  1355. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(newSampleRate));
  1356. if (pData->active)
  1357. activate();
  1358. }
  1359. // -------------------------------------------------------------------
  1360. // Plugin buffers
  1361. // nothing
  1362. // -------------------------------------------------------------------
  1363. // Post-poned UI Stuff
  1364. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1365. {
  1366. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1367. if (fUI.type != UI::UI_OSC)
  1368. return;
  1369. if (pData->osc.data.target == nullptr)
  1370. return;
  1371. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  1372. }
  1373. void uiProgramChange(const uint32_t index) noexcept override
  1374. {
  1375. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  1376. if (fUI.type != UI::UI_OSC)
  1377. return;
  1378. if (pData->osc.data.target == nullptr)
  1379. return;
  1380. osc_send_program(pData->osc.data, index);
  1381. }
  1382. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1383. {
  1384. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1385. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1386. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1387. if (fUI.type != UI::UI_OSC)
  1388. return;
  1389. if (pData->osc.data.target == nullptr)
  1390. return;
  1391. uint8_t midiData[4];
  1392. midiData[0] = 0;
  1393. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  1394. midiData[2] = note;
  1395. midiData[3] = velo;
  1396. osc_send_midi(pData->osc.data, midiData);
  1397. }
  1398. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1399. {
  1400. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1401. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1402. if (fUI.type != UI::UI_OSC)
  1403. return;
  1404. if (pData->osc.data.target == nullptr)
  1405. return;
  1406. uint8_t midiData[4];
  1407. midiData[0] = 0;
  1408. midiData[1] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  1409. midiData[2] = note;
  1410. midiData[3] = 0;
  1411. osc_send_midi(pData->osc.data, midiData);
  1412. }
  1413. // -------------------------------------------------------------------
  1414. protected:
  1415. void handlePluginUIClosed() override
  1416. {
  1417. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::UI_EMBED || fUI.type == UI::UI_EXTERNAL,);
  1418. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1419. carla_debug("VstPlugin::handlePluginUIClosed()");
  1420. showCustomUI(false);
  1421. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1422. }
  1423. void handlePluginUIResized(const uint width, const uint height) override
  1424. {
  1425. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::UI_EMBED,);
  1426. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1427. carla_debug("VstPlugin::handlePluginUIResized(%u, %u)", width, height);
  1428. return; // unused
  1429. (void)width; (void)height;
  1430. }
  1431. // -------------------------------------------------------------------
  1432. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt) const noexcept
  1433. {
  1434. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  1435. #ifdef DEBUG
  1436. if (opcode != effIdle && opcode != effEditIdle && opcode != effProcessEvents)
  1437. carla_debug("VstPlugin::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1438. #endif
  1439. try {
  1440. return fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt);
  1441. } CARLA_SAFE_EXCEPTION_RETURN("Vst dispatcher", 0);
  1442. }
  1443. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1444. {
  1445. #ifdef DEBUG
  1446. if (opcode != audioMasterGetTime)
  1447. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1448. #endif
  1449. intptr_t ret = 0;
  1450. switch (opcode)
  1451. {
  1452. case audioMasterAutomate: {
  1453. CARLA_SAFE_ASSERT_BREAK(pData->enabled);
  1454. // plugins should never do this:
  1455. CARLA_SAFE_ASSERT_INT(index >= 0 && index < static_cast<int32_t>(pData->param.count), index);
  1456. if (index < 0 || index >= static_cast<int32_t>(pData->param.count))
  1457. break;
  1458. const uint32_t uindex(static_cast<uint32_t>(index));
  1459. const float fixedValue(pData->param.getFixedValue(uindex, opt));
  1460. // Called from plugin process thread, nasty!
  1461. if (pthread_equal(pthread_self(), fProcThread))
  1462. {
  1463. CARLA_SAFE_ASSERT(fIsProcessing);
  1464. pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1465. }
  1466. // Called from UI
  1467. else if (fUI.isVisible)
  1468. {
  1469. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1470. }
  1471. // Unknown
  1472. // TODO - check if plugin or UI is initializing
  1473. else
  1474. {
  1475. carla_stdout("audioMasterAutomate called from unknown source");
  1476. setParameterValue(uindex, fixedValue, true, true, true);
  1477. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1478. }
  1479. break;
  1480. }
  1481. case audioMasterCurrentId:
  1482. // TODO
  1483. // if using old sdk, return effect->uniqueID
  1484. break;
  1485. case audioMasterIdle:
  1486. //pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1487. //pData->engine->idle();
  1488. break;
  1489. #if ! VST_FORCE_DEPRECATED
  1490. case audioMasterPinConnected:
  1491. // Deprecated in VST SDK 2.4
  1492. // TODO
  1493. break;
  1494. case audioMasterWantMidi:
  1495. // Deprecated in VST SDK 2.4
  1496. pData->hints |= PLUGIN_WANTS_MIDI_INPUT;
  1497. break;
  1498. #endif
  1499. case audioMasterGetTime:
  1500. ret = (intptr_t)&fTimeInfo;
  1501. break;
  1502. case audioMasterProcessEvents:
  1503. CARLA_SAFE_ASSERT_RETURN(pData->enabled, 0);
  1504. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, 0);
  1505. CARLA_SAFE_ASSERT_RETURN(pData->event.portOut != nullptr, 0);
  1506. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1507. return 0;
  1508. if (const VstEvents* const vstEvents = (const VstEvents*)ptr)
  1509. {
  1510. for (int32_t i=0; i < vstEvents->numEvents && i < kPluginMaxMidiEvents*2; ++i)
  1511. {
  1512. if (vstEvents->events[i] == nullptr)
  1513. break;
  1514. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)vstEvents->events[i]);
  1515. if (vstMidiEvent->type != kVstMidiType)
  1516. continue;
  1517. // reverse-find first free event, and put it there
  1518. for (uint32_t j=(kPluginMaxMidiEvents*2)-1; j >= fMidiEventCount; --j)
  1519. {
  1520. if (fMidiEvents[j].type == 0)
  1521. {
  1522. std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
  1523. break;
  1524. }
  1525. }
  1526. }
  1527. }
  1528. ret = 1;
  1529. break;
  1530. #if ! VST_FORCE_DEPRECATED
  1531. case audioMasterSetTime:
  1532. // Deprecated in VST SDK 2.4
  1533. break;
  1534. case audioMasterTempoAt:
  1535. // Deprecated in VST SDK 2.4
  1536. ret = static_cast<intptr_t>(fTimeInfo.tempo * 10000);
  1537. break;
  1538. case audioMasterGetNumAutomatableParameters:
  1539. // Deprecated in VST SDK 2.4
  1540. ret = carla_fixValue<intptr_t>(0, static_cast<intptr_t>(pData->engine->getOptions().maxParameters), fEffect->numParams);
  1541. break;
  1542. case audioMasterGetParameterQuantization:
  1543. // Deprecated in VST SDK 2.4
  1544. ret = 1; // full single float precision
  1545. break;
  1546. #endif
  1547. #if 0
  1548. case audioMasterIOChanged:
  1549. CARLA_ASSERT(pData->enabled);
  1550. // TESTING
  1551. if (! pData->enabled)
  1552. {
  1553. ret = 1;
  1554. break;
  1555. }
  1556. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1557. {
  1558. carla_stderr2("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1559. return 0;
  1560. }
  1561. engineProcessLock();
  1562. m_enabled = false;
  1563. engineProcessUnlock();
  1564. if (m_active)
  1565. {
  1566. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1567. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1568. }
  1569. reload();
  1570. if (m_active)
  1571. {
  1572. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1573. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1574. }
  1575. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1576. ret = 1;
  1577. break;
  1578. #endif
  1579. #if ! VST_FORCE_DEPRECATED
  1580. case audioMasterNeedIdle:
  1581. // Deprecated in VST SDK 2.4
  1582. fNeedIdle = true;
  1583. ret = 1;
  1584. break;
  1585. #endif
  1586. case audioMasterSizeWindow:
  1587. CARLA_SAFE_ASSERT_BREAK(fUI.window != nullptr);
  1588. CARLA_SAFE_ASSERT_BREAK(index > 0);
  1589. CARLA_SAFE_ASSERT_BREAK(value > 0);
  1590. fUI.window->setSize(static_cast<uint>(index), static_cast<uint>(value), true);
  1591. ret = 1;
  1592. break;
  1593. case audioMasterGetSampleRate:
  1594. ret = static_cast<intptr_t>(pData->engine->getSampleRate());
  1595. break;
  1596. case audioMasterGetBlockSize:
  1597. ret = static_cast<intptr_t>(pData->engine->getBufferSize());
  1598. break;
  1599. case audioMasterGetInputLatency:
  1600. ret = 0;
  1601. break;
  1602. case audioMasterGetOutputLatency:
  1603. ret = 0;
  1604. break;
  1605. #if ! VST_FORCE_DEPRECATED
  1606. case audioMasterGetPreviousPlug:
  1607. // Deprecated in VST SDK 2.4
  1608. // TODO
  1609. break;
  1610. case audioMasterGetNextPlug:
  1611. // Deprecated in VST SDK 2.4
  1612. // TODO
  1613. break;
  1614. case audioMasterWillReplaceOrAccumulate:
  1615. // Deprecated in VST SDK 2.4
  1616. ret = 1; // replace
  1617. break;
  1618. #endif
  1619. case audioMasterGetCurrentProcessLevel:
  1620. if (pthread_equal(pthread_self(), fProcThread))
  1621. {
  1622. CARLA_SAFE_ASSERT(fIsProcessing);
  1623. if (pData->engine->isOffline())
  1624. ret = kVstProcessLevelOffline;
  1625. else
  1626. ret = kVstProcessLevelRealtime;
  1627. }
  1628. else
  1629. ret = kVstProcessLevelUser;
  1630. break;
  1631. case audioMasterGetAutomationState:
  1632. ret = pData->active ? kVstAutomationReadWrite : kVstAutomationOff;
  1633. break;
  1634. case audioMasterOfflineStart:
  1635. case audioMasterOfflineRead:
  1636. case audioMasterOfflineWrite:
  1637. case audioMasterOfflineGetCurrentPass:
  1638. case audioMasterOfflineGetCurrentMetaPass:
  1639. // TODO
  1640. break;
  1641. #if ! VST_FORCE_DEPRECATED
  1642. case audioMasterSetOutputSampleRate:
  1643. // Deprecated in VST SDK 2.4
  1644. break;
  1645. case audioMasterGetOutputSpeakerArrangement:
  1646. // Deprecated in VST SDK 2.4
  1647. // TODO
  1648. break;
  1649. #endif
  1650. case audioMasterVendorSpecific:
  1651. if (index == 0xedcd && value == 0 && ptr != nullptr && std::strcmp((const char*)ptr, "EditorClosed") == 0)
  1652. {
  1653. CARLA_SAFE_ASSERT_BREAK(fUI.type == UI::UI_EXTERNAL);
  1654. handlePluginUIClosed();
  1655. break;
  1656. }
  1657. // TODO - cockos extensions
  1658. break;
  1659. #if ! VST_FORCE_DEPRECATED
  1660. case audioMasterSetIcon:
  1661. // Deprecated in VST SDK 2.4
  1662. break;
  1663. #endif
  1664. #if ! VST_FORCE_DEPRECATED
  1665. case audioMasterOpenWindow:
  1666. case audioMasterCloseWindow:
  1667. // Deprecated in VST SDK 2.4
  1668. // TODO
  1669. break;
  1670. #endif
  1671. case audioMasterGetDirectory:
  1672. // TODO
  1673. break;
  1674. case audioMasterUpdateDisplay:
  1675. // Idle UI if visible
  1676. if (fUI.isVisible)
  1677. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1678. // Update current program
  1679. if (pData->prog.count > 0)
  1680. {
  1681. const int32_t current = static_cast<int32_t>(dispatcher(effGetProgram, 0, 0, nullptr, 0.0f));
  1682. if (current >= 0 && current < static_cast<int32_t>(pData->prog.count))
  1683. {
  1684. char strBuf[STR_MAX+1] = { '\0' };
  1685. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  1686. if (pData->prog.names[current] != nullptr)
  1687. delete[] pData->prog.names[current];
  1688. pData->prog.names[current] = carla_strdup(strBuf);
  1689. if (pData->prog.current != current)
  1690. {
  1691. pData->prog.current = current;
  1692. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, current, 0, 0.0f, nullptr);
  1693. }
  1694. }
  1695. }
  1696. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  1697. ret = 1;
  1698. break;
  1699. case audioMasterBeginEdit:
  1700. case audioMasterEndEdit:
  1701. // TODO
  1702. break;
  1703. case audioMasterOpenFileSelector:
  1704. case audioMasterCloseFileSelector:
  1705. // TODO
  1706. break;
  1707. #if ! VST_FORCE_DEPRECATED
  1708. case audioMasterEditFile:
  1709. // Deprecated in VST SDK 2.4
  1710. // TODO
  1711. break;
  1712. case audioMasterGetChunkFile:
  1713. // Deprecated in VST SDK 2.4
  1714. // TODO
  1715. break;
  1716. case audioMasterGetInputSpeakerArrangement:
  1717. // Deprecated in VST SDK 2.4
  1718. // TODO
  1719. break;
  1720. #endif
  1721. default:
  1722. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f) UNDEF", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1723. break;
  1724. }
  1725. return ret;
  1726. // unused
  1727. (void)opt;
  1728. }
  1729. bool canDo(const char* const feature) const noexcept
  1730. {
  1731. try {
  1732. return (fEffect->dispatcher(fEffect, effCanDo, 0, 0, const_cast<char*>(feature), 0.0f) == 1);
  1733. } CARLA_SAFE_EXCEPTION_RETURN("vstPluginCanDo", false);
  1734. }
  1735. bool hasMidiInput() const noexcept
  1736. {
  1737. return (canDo("receiveVstEvents") || canDo("receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (pData->hints & PLUGIN_WANTS_MIDI_INPUT));
  1738. }
  1739. bool hasMidiOutput() const noexcept
  1740. {
  1741. return (canDo("sendVstEvents") || canDo("sendVstMidiEvent"));
  1742. }
  1743. // -------------------------------------------------------------------
  1744. const void* getNativeDescriptor() const noexcept override
  1745. {
  1746. return fEffect;
  1747. }
  1748. // -------------------------------------------------------------------
  1749. public:
  1750. bool init(const char* const filename, const char* const name, const int64_t uniqueId)
  1751. {
  1752. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1753. // ---------------------------------------------------------------
  1754. // first checks
  1755. if (pData->client != nullptr)
  1756. {
  1757. pData->engine->setLastError("Plugin client is already registered");
  1758. return false;
  1759. }
  1760. if (filename == nullptr || filename[0] == '\0')
  1761. {
  1762. pData->engine->setLastError("null filename");
  1763. return false;
  1764. }
  1765. // ---------------------------------------------------------------
  1766. // open DLL
  1767. if (! pData->libOpen(filename))
  1768. {
  1769. pData->engine->setLastError(pData->libError(filename));
  1770. return false;
  1771. }
  1772. // ---------------------------------------------------------------
  1773. // get DLL main entry
  1774. VST_Function vstFn = (VST_Function)pData->libSymbol("VSTPluginMain");
  1775. if (vstFn == nullptr)
  1776. {
  1777. vstFn = (VST_Function)pData->libSymbol("main");
  1778. if (vstFn == nullptr)
  1779. {
  1780. pData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1781. return false;
  1782. }
  1783. }
  1784. // ---------------------------------------------------------------
  1785. // initialize plugin (part 1)
  1786. sLastVstPlugin = this;
  1787. fEffect = vstFn(carla_vst_audioMasterCallback);
  1788. sLastVstPlugin = nullptr;
  1789. if (fEffect == nullptr)
  1790. {
  1791. pData->engine->setLastError("Plugin failed to initialize");
  1792. return false;
  1793. }
  1794. if (fEffect->magic != kEffectMagic)
  1795. {
  1796. pData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
  1797. return false;
  1798. }
  1799. #ifdef VESTIGE_HEADER
  1800. fEffect->ptr1 = this;
  1801. #else
  1802. fEffect->resvd1 = (intptr_t)this;
  1803. #endif
  1804. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1805. // ---------------------------------------------------------------
  1806. // get info
  1807. if (name != nullptr && name[0] != '\0')
  1808. {
  1809. pData->name = pData->engine->getUniquePluginName(name);
  1810. }
  1811. else
  1812. {
  1813. char strBuf[STR_MAX+1];
  1814. carla_zeroChar(strBuf, STR_MAX+1);
  1815. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1816. if (strBuf[0] != '\0')
  1817. pData->name = pData->engine->getUniquePluginName(strBuf);
  1818. else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
  1819. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1820. else
  1821. pData->name = pData->engine->getUniquePluginName("unknown");
  1822. }
  1823. pData->filename = carla_strdup(filename);
  1824. // ---------------------------------------------------------------
  1825. // register client
  1826. pData->client = pData->engine->addClient(this);
  1827. if (pData->client == nullptr || ! pData->client->isOk())
  1828. {
  1829. pData->engine->setLastError("Failed to register plugin client");
  1830. return false;
  1831. }
  1832. // ---------------------------------------------------------------
  1833. // initialize plugin (part 2)
  1834. #if ! VST_FORCE_DEPRECATED
  1835. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1836. #endif
  1837. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1838. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
  1839. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1840. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1841. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  1842. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>("hasCockosExtensions"), 0.0f)) == 0xbeef0000)
  1843. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1844. // ---------------------------------------------------------------
  1845. // gui stuff
  1846. if (fEffect->flags & effFlagsHasEditor)
  1847. {
  1848. fUI.type = UI::UI_EMBED;
  1849. if ((fEffect->flags & effFlagsProgramChunks) == 0 && pData->engine->getOptions().preferUiBridges)
  1850. {
  1851. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  1852. #if defined(CARLA_OS_LINUX)
  1853. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-vst-x11";
  1854. #endif
  1855. if (bridgeBinary.isNotEmpty() && File(bridgeBinary.buffer()).existsAsFile())
  1856. {
  1857. pData->osc.thread.setOscData(bridgeBinary, nullptr);
  1858. fUI.type = UI::UI_OSC;
  1859. }
  1860. }
  1861. }
  1862. else if (vstPluginCanDo(fEffect, "ExternalUI"))
  1863. {
  1864. fUI.type = UI::UI_EXTERNAL;
  1865. }
  1866. // ---------------------------------------------------------------
  1867. // set default options
  1868. pData->options = 0x0;
  1869. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1870. if (fEffect->flags & effFlagsProgramChunks)
  1871. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1872. if (hasMidiInput())
  1873. {
  1874. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1875. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1876. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1877. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1878. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1879. }
  1880. return true;
  1881. // unused
  1882. (void)uniqueId;
  1883. }
  1884. private:
  1885. int fUnique1;
  1886. AEffect* fEffect;
  1887. uint32_t fMidiEventCount;
  1888. VstMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1889. VstTimeInfo fTimeInfo;
  1890. bool fNeedIdle;
  1891. void* fLastChunk;
  1892. bool fIsProcessing;
  1893. pthread_t fProcThread;
  1894. struct FixedVstEvents {
  1895. int32_t numEvents;
  1896. intptr_t reserved;
  1897. VstEvent* data[kPluginMaxMidiEvents*2];
  1898. FixedVstEvents()
  1899. : numEvents(0),
  1900. reserved(0)
  1901. {
  1902. carla_fill<VstEvent*>(data, nullptr, kPluginMaxMidiEvents*2);
  1903. }
  1904. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  1905. } fEvents;
  1906. struct UI {
  1907. enum Type {
  1908. UI_NULL = 0,
  1909. UI_EMBED = 1,
  1910. UI_EXTERNAL = 2,
  1911. UI_OSC = 3
  1912. } type;
  1913. bool isVisible; // not used in OSC mode
  1914. CarlaPluginUI* window;
  1915. UI()
  1916. : type(UI_NULL),
  1917. isVisible(false),
  1918. window(nullptr) {}
  1919. ~UI()
  1920. {
  1921. CARLA_ASSERT(! isVisible);
  1922. if (window != nullptr)
  1923. {
  1924. delete window;
  1925. window = nullptr;
  1926. }
  1927. }
  1928. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  1929. } fUI;
  1930. int fUnique2;
  1931. static VstPlugin* sLastVstPlugin;
  1932. // -------------------------------------------------------------------
  1933. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1934. {
  1935. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1936. if (std::strcmp(feature, "supplyIdle") == 0)
  1937. return 1;
  1938. if (std::strcmp(feature, "sendVstEvents") == 0)
  1939. return 1;
  1940. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1941. return 1;
  1942. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1943. return 1;
  1944. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1945. return 1;
  1946. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1947. return 1;
  1948. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1949. return 1;
  1950. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1951. return -1;
  1952. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1953. return -1;
  1954. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1955. return 1;
  1956. if (std::strcmp(feature, "sizeWindow") == 0)
  1957. return 1;
  1958. if (std::strcmp(feature, "offline") == 0)
  1959. return -1;
  1960. if (std::strcmp(feature, "openFileSelector") == 0)
  1961. return -1;
  1962. if (std::strcmp(feature, "closeFileSelector") == 0)
  1963. return -1;
  1964. if (std::strcmp(feature, "startStopProcess") == 0)
  1965. return 1;
  1966. if (std::strcmp(feature, "supportShell") == 0)
  1967. return -1;
  1968. if (std::strcmp(feature, "shellCategory") == 0)
  1969. return -1;
  1970. // unimplemented
  1971. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1972. return 0;
  1973. }
  1974. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1975. {
  1976. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1977. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1978. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1979. #endif
  1980. switch (opcode)
  1981. {
  1982. case audioMasterVersion:
  1983. return kVstVersion;
  1984. case audioMasterGetVendorString:
  1985. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1986. std::strcpy((char*)ptr, "falkTX");
  1987. return 1;
  1988. case audioMasterGetProductString:
  1989. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1990. std::strcpy((char*)ptr, "Carla");
  1991. return 1;
  1992. case audioMasterGetVendorVersion:
  1993. return 0x110; // 1.1.0
  1994. case audioMasterCanDo:
  1995. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1996. return carla_vst_hostCanDo((const char*)ptr);
  1997. case audioMasterGetLanguage:
  1998. return kVstLangEnglish;
  1999. }
  2000. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  2001. VstPlugin* self = nullptr;
  2002. if (effect != nullptr)
  2003. {
  2004. #ifdef VESTIGE_HEADER
  2005. if (effect->ptr1 != nullptr)
  2006. {
  2007. self = (VstPlugin*)effect->ptr1;
  2008. if (self->fUnique1 != self->fUnique2)
  2009. self = nullptr;
  2010. }
  2011. #else
  2012. if (effect->resvd1 != 0)
  2013. {
  2014. self = (VstPlugin*)effect->resvd1;
  2015. if (self->fUnique1 != self->fUnique2)
  2016. self = nullptr;
  2017. }
  2018. #endif
  2019. if (self != nullptr)
  2020. {
  2021. if (self->fEffect == nullptr)
  2022. self->fEffect = effect;
  2023. if (self->fEffect != effect)
  2024. {
  2025. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  2026. self = nullptr;
  2027. }
  2028. }
  2029. else if (sLastVstPlugin != nullptr)
  2030. {
  2031. #ifdef VESTIGE_HEADER
  2032. effect->ptr1 = sLastVstPlugin;
  2033. #else
  2034. effect->resvd1 = (intptr_t)sLastVstPlugin;
  2035. #endif
  2036. self = sLastVstPlugin;
  2037. }
  2038. }
  2039. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  2040. }
  2041. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VstPlugin)
  2042. };
  2043. VstPlugin* VstPlugin::sLastVstPlugin = nullptr;
  2044. CARLA_BACKEND_END_NAMESPACE
  2045. #endif // ! USE_JUCE_FOR_VST
  2046. // -------------------------------------------------------------------------------------------------------------------
  2047. CARLA_BACKEND_START_NAMESPACE
  2048. CarlaPlugin* CarlaPlugin::newVST(const Initializer& init)
  2049. {
  2050. carla_debug("CarlaPlugin::newVST({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.uniqueId);
  2051. #ifdef USE_JUCE_FOR_VST
  2052. return newJuce(init, "VST");
  2053. #else
  2054. VstPlugin* const plugin(new VstPlugin(init.engine, init.id));
  2055. if (! plugin->init(init.filename, init.name, init.uniqueId))
  2056. {
  2057. delete plugin;
  2058. return nullptr;
  2059. }
  2060. plugin->reload();
  2061. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  2062. {
  2063. init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  2064. delete plugin;
  2065. return nullptr;
  2066. }
  2067. return plugin;
  2068. #endif
  2069. }
  2070. // -------------------------------------------------------------------------------------------------------------------
  2071. CARLA_BACKEND_END_NAMESPACE