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

VstPlugin.cpp 87KB

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