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

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