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

CarlaPluginVST2.cpp 99KB

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