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

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