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

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