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.

CarlaPluginLADSPADSSI.cpp 116KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 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
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
8 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190
  1. /*
  2. * Carla Plugin, LADSPA/DSSI implementation
  3. * Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngineUtils.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaLadspaUtils.hpp"
  21. #include "CarlaDssiUtils.hpp"
  22. #include "CarlaMathUtils.hpp"
  23. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  24. # include "CarlaOscUtils.hpp"
  25. # include "CarlaScopeUtils.hpp"
  26. # include "CarlaThread.hpp"
  27. # include "water/threads/ChildProcess.h"
  28. using water::ChildProcess;
  29. #endif
  30. using water::String;
  31. using water::StringArray;
  32. #define CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  33. /* check argument count */ \
  34. if (argc != argcToCompare) \
  35. { \
  36. carla_stderr("CarlaPluginLADSPADSSI::%s() - argument count mismatch: %i != %i", \
  37. __FUNCTION__, argc, argcToCompare); \
  38. return; \
  39. } \
  40. if (argc > 0) \
  41. { \
  42. /* check for nullness */ \
  43. if (types == nullptr || typesToCompare == nullptr) \
  44. { \
  45. carla_stderr("CarlaPluginLADSPADSSI::%s() - argument types are null", \
  46. __FUNCTION__); \
  47. return; \
  48. } \
  49. /* check argument types */ \
  50. if (std::strcmp(types, typesToCompare) != 0) \
  51. { \
  52. carla_stderr("CarlaPluginLADSPADSSI::%s() - argument types mismatch: '%s' != '%s'", \
  53. __FUNCTION__, types, typesToCompare); \
  54. return; \
  55. } \
  56. }
  57. CARLA_BACKEND_START_NAMESPACE
  58. // -------------------------------------------------------------------
  59. // Fallback data
  60. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  61. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  62. // -------------------------------------------------------------------
  63. class CarlaThreadDSSIUI : public CarlaThread
  64. {
  65. public:
  66. CarlaThreadDSSIUI(CarlaEngine* const engine, CarlaPlugin* const plugin, const CarlaOscData& oscData) noexcept
  67. : CarlaThread("CarlaThreadDSSIUI"),
  68. kEngine(engine),
  69. kPlugin(plugin),
  70. fBinary(),
  71. fLabel(),
  72. fOscData(oscData),
  73. fProcess() {}
  74. void setData(const char* const binary, const char* const label) noexcept
  75. {
  76. CARLA_SAFE_ASSERT_RETURN(binary != nullptr && binary[0] != '\0',);
  77. CARLA_SAFE_ASSERT_RETURN(label != nullptr /*&& label[0] != '\0'*/,);
  78. CARLA_SAFE_ASSERT(! isThreadRunning());
  79. fBinary = binary;
  80. fLabel = label;
  81. if (fLabel.isEmpty())
  82. fLabel = "\"\"";
  83. }
  84. uintptr_t getProcessId() const noexcept
  85. {
  86. CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
  87. return (uintptr_t)fProcess->getPID();
  88. }
  89. void run()
  90. {
  91. carla_stdout("LADSPA/DSSI UI thread started");
  92. if (fProcess == nullptr)
  93. {
  94. fProcess = new ChildProcess();
  95. }
  96. else if (fProcess->isRunning())
  97. {
  98. carla_stderr("CarlaThreadDSSI::run() - already running, giving up...");
  99. fProcess->kill();
  100. fProcess = nullptr;
  101. kEngine->callback(true, true,
  102. ENGINE_CALLBACK_UI_STATE_CHANGED,
  103. kPlugin->getId(),
  104. 0,
  105. 0, 0, 0.0f, nullptr);
  106. return;
  107. }
  108. String name(kPlugin->getName());
  109. String filename(kPlugin->getFilename());
  110. if (name.isEmpty())
  111. name = "(none)";
  112. if (filename.isEmpty())
  113. filename = "\"\"";
  114. StringArray arguments;
  115. // binary
  116. arguments.add(fBinary.buffer());
  117. // osc-url
  118. arguments.add(String(kEngine->getOscServerPathUDP()) + String("/") + String(kPlugin->getId()));
  119. // filename
  120. arguments.add(filename);
  121. // label
  122. arguments.add(fLabel.buffer());
  123. // ui-title
  124. arguments.add(name + String(" (GUI)"));
  125. bool started;
  126. {
  127. #ifdef CARLA_OS_LINUX
  128. /*
  129. * If the frontend uses winId parent, set LD_PRELOAD to auto-map the DSSI UI.
  130. * If not, unset LD_PRELOAD.
  131. */
  132. const uintptr_t winId(kEngine->getOptions().frontendWinId);
  133. // for CARLA_ENGINE_OPTION_FRONTEND_WIN_ID
  134. char winIdStr[STR_MAX+1];
  135. winIdStr[STR_MAX] = '\0';
  136. // for LD_PRELOAD
  137. CarlaString ldPreloadValue;
  138. if (winId != 0)
  139. {
  140. std::snprintf(winIdStr, STR_MAX, P_UINTPTR, winId);
  141. ldPreloadValue = (CarlaString(kEngine->getOptions().binaryDir)
  142. + "/libcarla_interposer-x11.so");
  143. }
  144. else
  145. {
  146. winIdStr[0] = '\0';
  147. }
  148. const ScopedEngineEnvironmentLocker _seel(kEngine);
  149. const CarlaScopedEnvVar _sev1("CARLA_ENGINE_OPTION_FRONTEND_WIN_ID", winIdStr[0] != '\0' ? winIdStr : nullptr);
  150. const CarlaScopedEnvVar _sev2("LD_PRELOAD", ldPreloadValue.isNotEmpty() ? ldPreloadValue.buffer() : nullptr);
  151. #endif // CARLA_OS_LINUX
  152. // start the DSSI UI application
  153. carla_stdout("starting DSSI UI...");
  154. started = fProcess->start(arguments);
  155. }
  156. if (! started)
  157. {
  158. carla_stdout("failed!");
  159. fProcess = nullptr;
  160. return;
  161. }
  162. if (waitForOscGuiShow())
  163. {
  164. for (; fProcess->isRunning() && ! shouldThreadExit();)
  165. carla_sleep(1);
  166. // we only get here if UI was closed or thread asked to exit
  167. if (fProcess->isRunning() && shouldThreadExit())
  168. {
  169. fProcess->waitForProcessToFinish(static_cast<int>(kEngine->getOptions().uiBridgesTimeout));
  170. if (fProcess->isRunning())
  171. {
  172. carla_stdout("CarlaThreadDSSIUI::run() - UI refused to close, force kill now");
  173. fProcess->kill();
  174. }
  175. else
  176. {
  177. carla_stdout("CarlaThreadDSSIUI::run() - UI auto-closed successfully");
  178. }
  179. }
  180. else if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  181. carla_stderr("CarlaThreadDSSIUI::run() - UI crashed while running");
  182. else
  183. carla_stdout("CarlaThreadDSSIUI::run() - UI closed cleanly");
  184. }
  185. else
  186. {
  187. fProcess->kill();
  188. carla_stdout("CarlaThreadDSSIUI::run() - GUI timeout");
  189. }
  190. fProcess = nullptr;
  191. kEngine->callback(true, true,
  192. ENGINE_CALLBACK_UI_STATE_CHANGED,
  193. kPlugin->getId(),
  194. 0,
  195. 0, 0, 0.0f, nullptr);
  196. carla_stdout("LADSPA/DSSI UI thread finished");
  197. }
  198. private:
  199. CarlaEngine* const kEngine;
  200. CarlaPlugin* const kPlugin;
  201. CarlaString fBinary;
  202. CarlaString fLabel;
  203. const CarlaOscData& fOscData;
  204. CarlaScopedPointer<ChildProcess> fProcess;
  205. bool waitForOscGuiShow()
  206. {
  207. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow()");
  208. const uint uiBridgesTimeout = kEngine->getOptions().uiBridgesTimeout;
  209. // wait for UI 'update' call
  210. for (uint i=0; i < uiBridgesTimeout/100; ++i)
  211. {
  212. if (fOscData.target != nullptr)
  213. {
  214. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow() - got response, asking UI to show itself now");
  215. osc_send_show(fOscData);
  216. return true;
  217. }
  218. if (fProcess != nullptr && fProcess->isRunning() && ! shouldThreadExit())
  219. carla_msleep(100);
  220. else
  221. return false;
  222. }
  223. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow() - Timeout while waiting for UI to respond"
  224. "(waited %u msecs)", uiBridgesTimeout);
  225. return false;
  226. }
  227. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThreadDSSIUI)
  228. };
  229. #endif
  230. // -----------------------------------------------------
  231. class CarlaPluginLADSPADSSI : public CarlaPlugin
  232. {
  233. public:
  234. CarlaPluginLADSPADSSI(CarlaEngine* const engine, const uint id) noexcept
  235. : CarlaPlugin(engine, id),
  236. fHandles(),
  237. fDescriptor(nullptr),
  238. fDssiDescriptor(nullptr),
  239. fRdfDescriptor(nullptr),
  240. fAudioInBuffers(nullptr),
  241. fAudioOutBuffers(nullptr),
  242. fExtraStereoBuffer(),
  243. fParamBuffers(nullptr),
  244. fLatencyIndex(-1),
  245. fForcedStereoIn(false),
  246. fForcedStereoOut(false),
  247. fNeedsFixedBuffers(false),
  248. fUsesCustomData(false)
  249. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  250. , fOscData(),
  251. fThreadUI(engine, this, fOscData),
  252. fUiFilename(nullptr)
  253. #endif
  254. {
  255. carla_debug("CarlaPluginLADSPADSSI::CarlaPluginLADSPADSSI(%p, %i)", engine, id);
  256. carla_zeroPointers(fExtraStereoBuffer, 2);
  257. }
  258. ~CarlaPluginLADSPADSSI() noexcept override
  259. {
  260. carla_debug("CarlaPluginLADSPADSSI::~CarlaPluginLADSPADSSI()");
  261. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  262. // close UI
  263. if (fUiFilename != nullptr)
  264. {
  265. showCustomUI(false);
  266. delete[] fUiFilename;
  267. fUiFilename = nullptr;
  268. }
  269. #endif
  270. pData->singleMutex.lock();
  271. pData->masterMutex.lock();
  272. if (pData->client != nullptr && pData->client->isActive())
  273. pData->client->deactivate();
  274. if (pData->active)
  275. {
  276. deactivate();
  277. pData->active = false;
  278. }
  279. if (fDescriptor != nullptr)
  280. {
  281. if (fDescriptor->cleanup != nullptr)
  282. {
  283. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  284. {
  285. LADSPA_Handle const handle(it.getValue(nullptr));
  286. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  287. try {
  288. fDescriptor->cleanup(handle);
  289. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI cleanup");
  290. }
  291. }
  292. fHandles.clear();
  293. fDescriptor = nullptr;
  294. fDssiDescriptor = nullptr;
  295. }
  296. if (fRdfDescriptor != nullptr)
  297. {
  298. delete fRdfDescriptor;
  299. fRdfDescriptor = nullptr;
  300. }
  301. clearBuffers();
  302. }
  303. // -------------------------------------------------------------------
  304. // Information (base)
  305. PluginType getType() const noexcept override
  306. {
  307. return fDssiDescriptor != nullptr ? PLUGIN_DSSI : PLUGIN_LADSPA;
  308. }
  309. PluginCategory getCategory() const noexcept override
  310. {
  311. if (fRdfDescriptor != nullptr)
  312. {
  313. const LADSPA_RDF_PluginType category = fRdfDescriptor->Type;
  314. // Specific Types
  315. if (category & (LADSPA_RDF_PLUGIN_DELAY|LADSPA_RDF_PLUGIN_REVERB))
  316. return PLUGIN_CATEGORY_DELAY;
  317. if (category & (LADSPA_RDF_PLUGIN_PHASER|LADSPA_RDF_PLUGIN_FLANGER|LADSPA_RDF_PLUGIN_CHORUS))
  318. return PLUGIN_CATEGORY_MODULATOR;
  319. if (category & (LADSPA_RDF_PLUGIN_AMPLIFIER))
  320. return PLUGIN_CATEGORY_DYNAMICS;
  321. if (category & (LADSPA_RDF_PLUGIN_UTILITY|LADSPA_RDF_PLUGIN_SPECTRAL|LADSPA_RDF_PLUGIN_FREQUENCY_METER))
  322. return PLUGIN_CATEGORY_UTILITY;
  323. // Pre-set LADSPA Types
  324. if (LADSPA_RDF_IS_PLUGIN_DYNAMICS(category))
  325. return PLUGIN_CATEGORY_DYNAMICS;
  326. if (LADSPA_RDF_IS_PLUGIN_AMPLITUDE(category))
  327. return PLUGIN_CATEGORY_MODULATOR;
  328. if (LADSPA_RDF_IS_PLUGIN_EQ(category))
  329. return PLUGIN_CATEGORY_EQ;
  330. if (LADSPA_RDF_IS_PLUGIN_FILTER(category))
  331. return PLUGIN_CATEGORY_FILTER;
  332. if (LADSPA_RDF_IS_PLUGIN_FREQUENCY(category))
  333. return PLUGIN_CATEGORY_UTILITY;
  334. if (LADSPA_RDF_IS_PLUGIN_SIMULATOR(category))
  335. return PLUGIN_CATEGORY_OTHER;
  336. if (LADSPA_RDF_IS_PLUGIN_TIME(category))
  337. return PLUGIN_CATEGORY_DELAY;
  338. if (LADSPA_RDF_IS_PLUGIN_GENERATOR(category))
  339. return PLUGIN_CATEGORY_SYNTH;
  340. }
  341. if (fDssiDescriptor != nullptr && fDssiDescriptor->run_synth != nullptr)
  342. if (pData->audioIn.count == 0 && pData->audioOut.count > 0)
  343. return PLUGIN_CATEGORY_SYNTH;
  344. return CarlaPlugin::getCategory();
  345. }
  346. int64_t getUniqueId() const noexcept override
  347. {
  348. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  349. return static_cast<int64_t>(fDescriptor->UniqueID);
  350. }
  351. uint32_t getLatencyInFrames() const noexcept override
  352. {
  353. if (fLatencyIndex < 0 || fParamBuffers == nullptr)
  354. return 0;
  355. const float latency(fParamBuffers[fLatencyIndex]);
  356. CARLA_SAFE_ASSERT_RETURN(latency >= 0.0f, 0);
  357. return static_cast<uint32_t>(latency);
  358. }
  359. // -------------------------------------------------------------------
  360. // Information (count)
  361. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  362. {
  363. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  364. if (fRdfDescriptor == nullptr)
  365. return 0;
  366. const int32_t rindex(pData->param.data[parameterId].rindex);
  367. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, 0);
  368. if (rindex >= static_cast<int32_t>(fRdfDescriptor->PortCount))
  369. return 0;
  370. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  371. return static_cast<uint32_t>(port.ScalePointCount);
  372. }
  373. // -------------------------------------------------------------------
  374. // Information (current data)
  375. std::size_t getChunkData(void** const dataPtr) noexcept override
  376. {
  377. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData, 0);
  378. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  379. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0);
  380. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->get_custom_data != nullptr, 0);
  381. CARLA_SAFE_ASSERT_RETURN(fHandles.count() > 0, 0);
  382. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  383. *dataPtr = nullptr;
  384. int ret = 0;
  385. ulong dataSize = 0;
  386. try {
  387. ret = fDssiDescriptor->get_custom_data(fHandles.getFirst(nullptr), dataPtr, &dataSize);
  388. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPluginLADSPADSSI::getChunkData", 0);
  389. return (ret != 0) ? dataSize : 0;
  390. }
  391. // -------------------------------------------------------------------
  392. // Information (per-plugin data)
  393. uint getOptionsAvailable() const noexcept override
  394. {
  395. uint options = 0x0;
  396. // can't disable fixed buffers if using latency
  397. if (fLatencyIndex == -1 && ! fNeedsFixedBuffers)
  398. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  399. // can't disable forced stereo if enabled in the engine
  400. if (pData->engine->getOptions().forceStereo)
  401. pass();
  402. // if inputs or outputs are just 1, then yes we can force stereo
  403. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fForcedStereoIn || fForcedStereoOut)
  404. options |= PLUGIN_OPTION_FORCE_STEREO;
  405. if (fDssiDescriptor != nullptr)
  406. {
  407. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  408. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  409. if (fUsesCustomData)
  410. options |= PLUGIN_OPTION_USE_CHUNKS;
  411. if (fDssiDescriptor->run_synth != nullptr)
  412. {
  413. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  414. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  415. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  416. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  417. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  418. }
  419. }
  420. return options;
  421. }
  422. float getParameterValue(const uint32_t parameterId) const noexcept override
  423. {
  424. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  425. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  426. // bad plugins might have set output values out of bounds
  427. if (pData->param.data[parameterId].type == PARAMETER_OUTPUT)
  428. return pData->param.ranges[parameterId].getFixedValue(fParamBuffers[parameterId]);
  429. // not output, should be fine
  430. return fParamBuffers[parameterId];
  431. }
  432. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  433. {
  434. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  435. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  436. const int32_t rindex(pData->param.data[parameterId].rindex);
  437. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, 0.0f);
  438. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fRdfDescriptor->PortCount), 0.0f);
  439. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  440. CARLA_SAFE_ASSERT_RETURN(scalePointId < port.ScalePointCount, 0.0f);
  441. const LADSPA_RDF_ScalePoint& scalePoint(port.ScalePoints[scalePointId]);
  442. return pData->param.ranges[parameterId].getFixedValue(scalePoint.Value);
  443. }
  444. bool getLabel(char* const strBuf) const noexcept override
  445. {
  446. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  447. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Label != nullptr, false);
  448. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  449. return true;
  450. }
  451. bool getMaker(char* const strBuf) const noexcept override
  452. {
  453. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  454. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Maker != nullptr, false);
  455. if (fRdfDescriptor != nullptr && fRdfDescriptor->Creator != nullptr)
  456. std::strncpy(strBuf, fRdfDescriptor->Creator, STR_MAX);
  457. else
  458. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  459. return true;
  460. }
  461. bool getCopyright(char* const strBuf) const noexcept override
  462. {
  463. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  464. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Copyright != nullptr, false);
  465. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  466. return true;
  467. }
  468. bool getRealName(char* const strBuf) const noexcept override
  469. {
  470. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  471. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Name != nullptr, false);
  472. if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  473. std::strncpy(strBuf, fRdfDescriptor->Title, STR_MAX);
  474. else
  475. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  476. return true;
  477. }
  478. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  479. {
  480. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  481. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  482. const int32_t rindex(pData->param.data[parameterId].rindex);
  483. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  484. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), false);
  485. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames[rindex] != nullptr, false);
  486. if (! getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, true))
  487. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  488. return true;
  489. }
  490. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  491. {
  492. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  493. const int32_t rindex(pData->param.data[parameterId].rindex);
  494. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  495. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  496. {
  497. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  498. if (LADSPA_RDF_PORT_HAS_UNIT(port.Hints))
  499. {
  500. switch (port.Unit)
  501. {
  502. case LADSPA_RDF_UNIT_DB:
  503. std::strncpy(strBuf, "dB", STR_MAX);
  504. return true;
  505. case LADSPA_RDF_UNIT_COEF:
  506. std::strncpy(strBuf, "(coef)", STR_MAX);
  507. return true;
  508. case LADSPA_RDF_UNIT_HZ:
  509. std::strncpy(strBuf, "Hz", STR_MAX);
  510. return true;
  511. case LADSPA_RDF_UNIT_S:
  512. std::strncpy(strBuf, "s", STR_MAX);
  513. return true;
  514. case LADSPA_RDF_UNIT_MS:
  515. std::strncpy(strBuf, "ms", STR_MAX);
  516. return true;
  517. case LADSPA_RDF_UNIT_MIN:
  518. std::strncpy(strBuf, "min", STR_MAX);
  519. return true;
  520. }
  521. }
  522. }
  523. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), false);
  524. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames[rindex] != nullptr, false);
  525. return getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, false);
  526. }
  527. bool getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  528. {
  529. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  530. if (fRdfDescriptor == nullptr)
  531. return false;
  532. const int32_t rindex(pData->param.data[parameterId].rindex);
  533. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  534. if (rindex >= static_cast<int32_t>(fRdfDescriptor->PortCount))
  535. return false;
  536. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  537. if (! LADSPA_RDF_PORT_HAS_LABEL(port.Hints))
  538. return false;
  539. CARLA_SAFE_ASSERT_RETURN(port.Label != nullptr, false);
  540. std::strncpy(strBuf, port.Label, STR_MAX);
  541. return true;
  542. }
  543. bool getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  544. {
  545. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  546. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  547. const int32_t rindex(pData->param.data[parameterId].rindex);
  548. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  549. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fRdfDescriptor->PortCount), false);
  550. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  551. CARLA_SAFE_ASSERT_RETURN(scalePointId < port.ScalePointCount, false);
  552. const LADSPA_RDF_ScalePoint& scalePoint(port.ScalePoints[scalePointId]);
  553. CARLA_SAFE_ASSERT_RETURN(scalePoint.Label != nullptr, false);
  554. std::strncpy(strBuf, scalePoint.Label, STR_MAX);
  555. return true;
  556. }
  557. // -------------------------------------------------------------------
  558. // Set data (state)
  559. // nothing
  560. // -------------------------------------------------------------------
  561. // Set data (internal stuff)
  562. void setId(const uint newId) noexcept override
  563. {
  564. CarlaPlugin::setId(newId);
  565. // UI osc-url uses Id, so we need to close it when it changes
  566. // FIXME - must be RT safe
  567. showCustomUI(false);
  568. }
  569. // -------------------------------------------------------------------
  570. // Set data (plugin-specific stuff)
  571. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  572. {
  573. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  574. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  575. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  576. fParamBuffers[parameterId] = fixedValue;
  577. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  578. }
  579. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  580. {
  581. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  582. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  583. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  584. fParamBuffers[parameterId] = fixedValue;
  585. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  586. }
  587. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  588. {
  589. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  590. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  591. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  592. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  593. carla_debug("CarlaPluginLADSPADSSI::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  594. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  595. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  596. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  597. return carla_stderr2("CarlaPluginLADSPADSSI::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string",
  598. type, key, value, bool2str(sendGui));
  599. if (fDssiDescriptor->configure != nullptr && fHandles.count() > 0)
  600. {
  601. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  602. {
  603. LADSPA_Handle const handle(it.getValue(nullptr));
  604. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  605. try {
  606. fDssiDescriptor->configure(handle, key, value);
  607. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI setCustomData");
  608. }
  609. }
  610. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  611. if (sendGui && fOscData.target != nullptr)
  612. osc_send_configure(fOscData, key, value);
  613. #endif
  614. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  615. {
  616. const ScopedSingleProcessLocker spl(this, true);
  617. reloadPrograms(false);
  618. }
  619. CarlaPlugin::setCustomData(type, key, value, sendGui);
  620. }
  621. void setChunkData(const void* const data, const std::size_t dataSize) override
  622. {
  623. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData,);
  624. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  625. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  626. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->set_custom_data != nullptr,);
  627. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  628. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  629. if (fHandles.count() > 0)
  630. {
  631. const ScopedSingleProcessLocker spl(this, true);
  632. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  633. {
  634. LADSPA_Handle const handle(it.getValue(nullptr));
  635. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  636. try {
  637. fDssiDescriptor->set_custom_data(handle, const_cast<void*>(data), static_cast<ulong>(dataSize));
  638. } CARLA_SAFE_EXCEPTION("CarlaPluginLADSPADSSI::setChunkData");
  639. }
  640. }
  641. pData->updateParameterValues(this, true, true, false);
  642. }
  643. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  644. {
  645. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  646. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  647. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  648. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  649. if (index >= 0 && fHandles.count() > 0)
  650. {
  651. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  652. setMidiProgramInDSSI(static_cast<uint32_t>(index));
  653. }
  654. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  655. }
  656. void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
  657. {
  658. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  659. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  660. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  661. setMidiProgramInDSSI(uindex);
  662. CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
  663. }
  664. void setMidiProgramInDSSI(const uint32_t uindex) noexcept
  665. {
  666. const uint32_t bank(pData->midiprog.data[uindex].bank);
  667. const uint32_t program(pData->midiprog.data[uindex].program);
  668. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  669. {
  670. LADSPA_Handle const handle(it.getValue(nullptr));
  671. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  672. try {
  673. fDssiDescriptor->select_program(handle, bank, program);
  674. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI setMidiProgram")
  675. }
  676. }
  677. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  678. // -------------------------------------------------------------------
  679. // Set ui stuff
  680. void showCustomUI(const bool yesNo) override
  681. {
  682. if (yesNo)
  683. {
  684. fOscData.clear();
  685. fThreadUI.startThread();
  686. }
  687. else
  688. {
  689. #ifndef BUILD_BRIDGE
  690. pData->transientTryCounter = 0;
  691. #endif
  692. if (fOscData.target != nullptr)
  693. {
  694. osc_send_hide(fOscData);
  695. osc_send_quit(fOscData);
  696. fOscData.clear();
  697. }
  698. fThreadUI.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  699. }
  700. }
  701. #endif
  702. // -------------------------------------------------------------------
  703. // Plugin state
  704. void reload() override
  705. {
  706. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  707. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  708. CARLA_SAFE_ASSERT_RETURN(fHandles.count() > 0,);
  709. carla_debug("CarlaPluginLADSPADSSI::reload() - start");
  710. const EngineProcessMode processMode(pData->engine->getProccessMode());
  711. // Safely disable plugin for reload
  712. const ScopedDisabler sd(this);
  713. if (pData->active)
  714. deactivate();
  715. clearBuffers();
  716. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  717. const uint32_t portCount(getSafePortCount());
  718. uint32_t aIns, aOuts, mIns, params;
  719. aIns = aOuts = mIns = params = 0;
  720. bool forcedStereoIn, forcedStereoOut;
  721. forcedStereoIn = forcedStereoOut = false;
  722. bool needsCtrlIn, needsCtrlOut;
  723. needsCtrlIn = needsCtrlOut = false;
  724. for (uint32_t i=0; i < portCount; ++i)
  725. {
  726. const LADSPA_PortDescriptor portType(fDescriptor->PortDescriptors[i]);
  727. if (LADSPA_IS_PORT_AUDIO(portType))
  728. {
  729. if (LADSPA_IS_PORT_INPUT(portType))
  730. aIns += 1;
  731. else if (LADSPA_IS_PORT_OUTPUT(portType))
  732. aOuts += 1;
  733. }
  734. else if (LADSPA_IS_PORT_CONTROL(portType))
  735. params += 1;
  736. }
  737. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  738. {
  739. if ((aIns == 1 || aOuts == 1) && fHandles.count() == 1 && addInstance())
  740. {
  741. if (aIns == 1)
  742. {
  743. aIns = 2;
  744. forcedStereoIn = true;
  745. }
  746. if (aOuts == 1)
  747. {
  748. aOuts = 2;
  749. forcedStereoOut = true;
  750. }
  751. }
  752. }
  753. if (fDssiDescriptor != nullptr && fDssiDescriptor->run_synth != nullptr)
  754. {
  755. mIns = 1;
  756. needsCtrlIn = true;
  757. }
  758. if (aIns > 0)
  759. {
  760. pData->audioIn.createNew(aIns);
  761. fAudioInBuffers = new float*[aIns];
  762. for (uint32_t i=0; i < aIns; ++i)
  763. fAudioInBuffers[i] = nullptr;
  764. }
  765. if (aOuts > 0)
  766. {
  767. pData->audioOut.createNew(aOuts);
  768. fAudioOutBuffers = new float*[aOuts];
  769. needsCtrlIn = true;
  770. for (uint32_t i=0; i < aOuts; ++i)
  771. fAudioOutBuffers[i] = nullptr;
  772. }
  773. if (params > 0)
  774. {
  775. pData->param.createNew(params, true);
  776. fParamBuffers = new float[params];
  777. carla_zeroFloats(fParamBuffers, params);
  778. }
  779. const uint portNameSize(pData->engine->getMaxPortNameSize());
  780. CarlaString portName;
  781. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  782. {
  783. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  784. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  785. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  786. if (LADSPA_IS_PORT_AUDIO(portType))
  787. {
  788. portName.clear();
  789. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  790. {
  791. portName = pData->name;
  792. portName += ":";
  793. }
  794. if (fDescriptor->PortNames[i] != nullptr && fDescriptor->PortNames[i][0] != '\0')
  795. {
  796. portName += fDescriptor->PortNames[i];
  797. }
  798. else
  799. {
  800. if (LADSPA_IS_PORT_INPUT(portType))
  801. {
  802. if (aIns > 1)
  803. {
  804. portName += "audio-in_";
  805. portName += CarlaString(iAudioIn+1);
  806. }
  807. else
  808. portName += "audio-in";
  809. }
  810. else
  811. {
  812. if (aOuts > 1)
  813. {
  814. portName += "audio-out_";
  815. portName += CarlaString(iAudioOut+1);
  816. }
  817. else
  818. portName += "audio-out";
  819. }
  820. }
  821. portName.truncate(portNameSize);
  822. if (LADSPA_IS_PORT_INPUT(portType))
  823. {
  824. const uint32_t j = iAudioIn++;
  825. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  826. pData->audioIn.ports[j].rindex = i;
  827. if (forcedStereoIn)
  828. {
  829. portName += "_2";
  830. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  831. pData->audioIn.ports[1].rindex = i;
  832. }
  833. }
  834. else if (LADSPA_IS_PORT_OUTPUT(portType))
  835. {
  836. const uint32_t j = iAudioOut++;
  837. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  838. pData->audioOut.ports[j].rindex = i;
  839. if (forcedStereoOut)
  840. {
  841. portName += "_2";
  842. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  843. pData->audioOut.ports[1].rindex = i;
  844. }
  845. }
  846. else
  847. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  848. }
  849. else if (LADSPA_IS_PORT_CONTROL(portType))
  850. {
  851. const uint32_t j = iCtrl++;
  852. pData->param.data[j].index = static_cast<int32_t>(j);
  853. pData->param.data[j].rindex = static_cast<int32_t>(i);
  854. const char* const paramName(fDescriptor->PortNames[i] != nullptr ? fDescriptor->PortNames[i] : "unknown");
  855. float min, max, def, step, stepSmall, stepLarge;
  856. // min value
  857. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  858. min = portRangeHints.LowerBound;
  859. else
  860. min = 0.0f;
  861. // max value
  862. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  863. max = portRangeHints.UpperBound;
  864. else
  865. max = 1.0f;
  866. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  867. {
  868. min *= sampleRate;
  869. max *= sampleRate;
  870. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  871. }
  872. if (min >= max)
  873. {
  874. carla_stderr2("WARNING - Broken plugin parameter '%s': min >= max", paramName);
  875. max = min + 0.1f;
  876. }
  877. // default value
  878. if (hasPortRDF && LADSPA_RDF_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  879. def = fRdfDescriptor->Ports[i].Default;
  880. else
  881. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  882. if (def < min)
  883. def = min;
  884. else if (def > max)
  885. def = max;
  886. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  887. {
  888. step = max - min;
  889. stepSmall = step;
  890. stepLarge = step;
  891. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  892. }
  893. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  894. {
  895. step = 1.0f;
  896. stepSmall = 1.0f;
  897. stepLarge = 10.0f;
  898. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  899. }
  900. else
  901. {
  902. const float range = max - min;
  903. step = range/100.0f;
  904. stepSmall = range/1000.0f;
  905. stepLarge = range/10.0f;
  906. }
  907. if (LADSPA_IS_PORT_INPUT(portType))
  908. {
  909. pData->param.data[j].type = PARAMETER_INPUT;
  910. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  911. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  912. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  913. needsCtrlIn = true;
  914. // MIDI CC value
  915. if (fDssiDescriptor != nullptr && fDssiDescriptor->get_midi_controller_for_port != nullptr)
  916. {
  917. const int ctrl = fDssiDescriptor->get_midi_controller_for_port(fHandles.getFirst(nullptr), i);
  918. if (DSSI_CONTROLLER_IS_SET(ctrl) && DSSI_IS_CC(ctrl))
  919. {
  920. const int16_t cc = DSSI_CC_NUMBER(ctrl);
  921. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  922. pData->param.data[j].midiCC = cc;
  923. }
  924. }
  925. }
  926. else if (LADSPA_IS_PORT_OUTPUT(portType))
  927. {
  928. pData->param.data[j].type = PARAMETER_OUTPUT;
  929. if (std::strcmp(paramName, "latency") == 0 || std::strcmp(paramName, "_latency") == 0)
  930. {
  931. min = 0.0f;
  932. max = sampleRate;
  933. def = 0.0f;
  934. step = 1.0f;
  935. stepSmall = 1.0f;
  936. stepLarge = 1.0f;
  937. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  938. CARLA_SAFE_ASSERT_INT2(fLatencyIndex == static_cast<int32_t>(j), fLatencyIndex, j);
  939. }
  940. else
  941. {
  942. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  943. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  944. needsCtrlOut = true;
  945. }
  946. }
  947. else
  948. {
  949. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  950. }
  951. // extra parameter hints
  952. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  953. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  954. // check for scalepoints, require at least 2 to make it useful
  955. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount >= 2)
  956. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  957. pData->param.ranges[j].min = min;
  958. pData->param.ranges[j].max = max;
  959. pData->param.ranges[j].def = def;
  960. pData->param.ranges[j].step = step;
  961. pData->param.ranges[j].stepSmall = stepSmall;
  962. pData->param.ranges[j].stepLarge = stepLarge;
  963. // Start parameters in their default values
  964. fParamBuffers[j] = def;
  965. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  966. {
  967. LADSPA_Handle const handle(it.getValue(nullptr));
  968. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  969. try {
  970. fDescriptor->connect_port(handle, i, &fParamBuffers[j]);
  971. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (parameter)");
  972. }
  973. }
  974. else
  975. {
  976. // Not Audio or Control
  977. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  978. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  979. {
  980. LADSPA_Handle const handle(it.getValue(nullptr));
  981. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  982. try {
  983. fDescriptor->connect_port(handle, i, nullptr);
  984. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (null)");
  985. }
  986. }
  987. }
  988. if (needsCtrlIn)
  989. {
  990. portName.clear();
  991. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  992. {
  993. portName = pData->name;
  994. portName += ":";
  995. }
  996. portName += "events-in";
  997. portName.truncate(portNameSize);
  998. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  999. }
  1000. if (needsCtrlOut)
  1001. {
  1002. portName.clear();
  1003. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1004. {
  1005. portName = pData->name;
  1006. portName += ":";
  1007. }
  1008. portName += "events-out";
  1009. portName.truncate(portNameSize);
  1010. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  1011. }
  1012. if (forcedStereoIn || forcedStereoOut)
  1013. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1014. else
  1015. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1016. // plugin hints
  1017. pData->hints = 0x0;
  1018. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  1019. pData->hints |= PLUGIN_IS_RTSAFE;
  1020. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  1021. if (fUiFilename != nullptr)
  1022. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1023. #endif
  1024. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1025. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1026. pData->hints |= PLUGIN_CAN_DRYWET;
  1027. if (aOuts > 0)
  1028. pData->hints |= PLUGIN_CAN_VOLUME;
  1029. if (aOuts >= 2 && aOuts % 2 == 0)
  1030. pData->hints |= PLUGIN_CAN_BALANCE;
  1031. #endif
  1032. // extra plugin hints
  1033. pData->extraHints = 0x0;
  1034. if (mIns > 0)
  1035. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  1036. // check initial latency
  1037. findInitialLatencyValue(aIns, aOuts);
  1038. fForcedStereoIn = forcedStereoIn;
  1039. fForcedStereoOut = forcedStereoOut;
  1040. bufferSizeChanged(pData->engine->getBufferSize());
  1041. reloadPrograms(true);
  1042. if (pData->active)
  1043. activate();
  1044. carla_debug("CarlaPluginLADSPADSSI::reload() - end");
  1045. }
  1046. void findInitialLatencyValue(const uint32_t aIns, const uint32_t aOuts) const
  1047. {
  1048. if (fLatencyIndex < 0 || fHandles.count() == 0)
  1049. return;
  1050. // we need to pre-run the plugin so it can update its latency control-port
  1051. const LADSPA_Handle handle(fHandles.getFirst(nullptr));
  1052. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1053. float tmpIn [(aIns > 0) ? aIns : 1][2];
  1054. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  1055. for (uint32_t j=0; j < aIns; ++j)
  1056. {
  1057. tmpIn[j][0] = 0.0f;
  1058. tmpIn[j][1] = 0.0f;
  1059. try {
  1060. fDescriptor->connect_port(handle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  1061. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (latency input)");
  1062. }
  1063. for (uint32_t j=0; j < aOuts; ++j)
  1064. {
  1065. tmpOut[j][0] = 0.0f;
  1066. tmpOut[j][1] = 0.0f;
  1067. try {
  1068. fDescriptor->connect_port(handle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  1069. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (latency output)");
  1070. }
  1071. if (fDescriptor->activate != nullptr)
  1072. {
  1073. try {
  1074. fDescriptor->activate(handle);
  1075. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI latency activate");
  1076. }
  1077. try {
  1078. fDescriptor->run(handle, 2);
  1079. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI latency run");
  1080. if (fDescriptor->deactivate != nullptr)
  1081. {
  1082. try {
  1083. fDescriptor->deactivate(handle);
  1084. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI latency deactivate");
  1085. }
  1086. // done, let's get the value
  1087. if (const uint32_t latency = getLatencyInFrames())
  1088. {
  1089. pData->client->setLatency(latency);
  1090. #ifndef BUILD_BRIDGE
  1091. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  1092. #endif
  1093. }
  1094. }
  1095. void reloadPrograms(const bool doInit) override
  1096. {
  1097. carla_debug("CarlaPluginLADSPADSSI::reloadPrograms(%s)", bool2str(doInit));
  1098. const LADSPA_Handle handle(fHandles.getFirst(nullptr));
  1099. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1100. const uint32_t oldCount = pData->midiprog.count;
  1101. const int32_t current = pData->midiprog.current;
  1102. // Delete old programs
  1103. pData->midiprog.clear();
  1104. // nothing to do for simple LADSPA plugins (do we want to bother with lrdf presets?)
  1105. if (fDssiDescriptor == nullptr)
  1106. return;
  1107. // Query new programs
  1108. uint32_t newCount = 0;
  1109. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  1110. {
  1111. for (; fDssiDescriptor->get_program(handle, newCount) != nullptr;)
  1112. ++newCount;
  1113. }
  1114. if (newCount > 0)
  1115. {
  1116. pData->midiprog.createNew(newCount);
  1117. // Update data
  1118. for (uint32_t i=0; i < newCount; ++i)
  1119. {
  1120. const DSSI_Program_Descriptor* const pdesc(fDssiDescriptor->get_program(handle, i));
  1121. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  1122. CARLA_SAFE_ASSERT(pdesc->Name != nullptr);
  1123. pData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  1124. pData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  1125. pData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  1126. }
  1127. }
  1128. if (doInit)
  1129. {
  1130. if (newCount > 0)
  1131. setMidiProgram(0, false, false, false, true);
  1132. }
  1133. else
  1134. {
  1135. // Check if current program is invalid
  1136. bool programChanged = false;
  1137. if (newCount == oldCount+1)
  1138. {
  1139. // one midi program added, probably created by user
  1140. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1141. programChanged = true;
  1142. }
  1143. else if (current < 0 && newCount > 0)
  1144. {
  1145. // programs exist now, but not before
  1146. pData->midiprog.current = 0;
  1147. programChanged = true;
  1148. }
  1149. else if (current >= 0 && newCount == 0)
  1150. {
  1151. // programs existed before, but not anymore
  1152. pData->midiprog.current = -1;
  1153. programChanged = true;
  1154. }
  1155. else if (current >= static_cast<int32_t>(newCount))
  1156. {
  1157. // current midi program > count
  1158. pData->midiprog.current = 0;
  1159. programChanged = true;
  1160. }
  1161. else
  1162. {
  1163. // no change
  1164. pData->midiprog.current = current;
  1165. }
  1166. if (programChanged)
  1167. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1168. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
  1169. }
  1170. }
  1171. // -------------------------------------------------------------------
  1172. // Plugin processing
  1173. void activate() noexcept override
  1174. {
  1175. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1176. if (fDescriptor->activate != nullptr)
  1177. {
  1178. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1179. {
  1180. LADSPA_Handle const handle(it.getValue(nullptr));
  1181. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1182. try {
  1183. fDescriptor->activate(handle);
  1184. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI activate");
  1185. }
  1186. }
  1187. }
  1188. void deactivate() noexcept override
  1189. {
  1190. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1191. if (fDescriptor->deactivate != nullptr)
  1192. {
  1193. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1194. {
  1195. LADSPA_Handle const handle(it.getValue(nullptr));
  1196. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1197. try {
  1198. fDescriptor->deactivate(handle);
  1199. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI deactivate");
  1200. }
  1201. }
  1202. }
  1203. void process(const float** const audioIn, float** const audioOut,
  1204. const float** const cvIn, float** const,
  1205. const uint32_t frames) override
  1206. {
  1207. // --------------------------------------------------------------------------------------------------------
  1208. // Check if active
  1209. if (! pData->active)
  1210. {
  1211. // disable any output sound
  1212. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1213. carla_zeroFloats(audioOut[i], frames);
  1214. return;
  1215. }
  1216. ulong midiEventCount = 0;
  1217. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents);
  1218. // --------------------------------------------------------------------------------------------------------
  1219. // Check if needs reset
  1220. if (pData->needsReset)
  1221. {
  1222. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1223. {
  1224. midiEventCount = MAX_MIDI_CHANNELS*2;
  1225. for (uchar i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  1226. {
  1227. fMidiEvents[i].type = SND_SEQ_EVENT_CONTROLLER;
  1228. fMidiEvents[i].data.control.channel = i;
  1229. fMidiEvents[i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1230. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  1231. fMidiEvents[k+i].data.control.channel = i;
  1232. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1233. }
  1234. }
  1235. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1236. {
  1237. midiEventCount = MAX_MIDI_NOTE;
  1238. for (uchar i=0; i < MAX_MIDI_NOTE; ++i)
  1239. {
  1240. fMidiEvents[i].type = SND_SEQ_EVENT_NOTEOFF;
  1241. fMidiEvents[i].data.note.channel = static_cast<uchar>(pData->ctrlChannel);
  1242. fMidiEvents[i].data.note.note = i;
  1243. }
  1244. }
  1245. pData->needsReset = false;
  1246. }
  1247. // --------------------------------------------------------------------------------------------------------
  1248. // Event Input and Processing
  1249. if (pData->event.portIn != nullptr)
  1250. {
  1251. // ----------------------------------------------------------------------------------------------------
  1252. // MIDI Input (External)
  1253. if (pData->extNotes.mutex.tryLock())
  1254. {
  1255. ExternalMidiNote note = { 0, 0, 0 };
  1256. for (; midiEventCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1257. {
  1258. note = pData->extNotes.data.getFirst(note, true);
  1259. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1260. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1261. seqEvent.type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  1262. seqEvent.data.note.channel = static_cast<uchar>(note.channel);
  1263. seqEvent.data.note.note = note.note;
  1264. seqEvent.data.note.velocity = note.velo;
  1265. }
  1266. pData->extNotes.mutex.unlock();
  1267. } // End of MIDI Input (External)
  1268. // ----------------------------------------------------------------------------------------------------
  1269. // CV Control Input
  1270. for (uint32_t i=0, j=0; i < pData->param.count && i < 32; ++i)
  1271. {
  1272. if (pData->param.data[i].type != PARAMETER_INPUT)
  1273. continue;
  1274. if ((pData->param.data[i].hints & PARAMETER_IS_CV_CONTROLLED) == 0x0)
  1275. continue;
  1276. const uint32_t cvIndex = j++;
  1277. pData->event.portIn->mixWithCvBuffer(cvIn[cvIndex], frames, i);
  1278. }
  1279. // ----------------------------------------------------------------------------------------------------
  1280. // Event Input (System)
  1281. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1282. bool allNotesOffSent = false;
  1283. #endif
  1284. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1285. uint32_t startTime = 0;
  1286. uint32_t timeOffset = 0;
  1287. uint32_t nextBankId;
  1288. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1289. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1290. else
  1291. nextBankId = 0;
  1292. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1293. {
  1294. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1295. uint32_t eventTime = event.time;
  1296. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1297. if (eventTime < timeOffset)
  1298. {
  1299. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1300. eventTime, timeOffset, pData->name);
  1301. eventTime = timeOffset;
  1302. }
  1303. if (isSampleAccurate && eventTime > timeOffset)
  1304. {
  1305. if (processSingle(audioIn, audioOut, eventTime - timeOffset, timeOffset, midiEventCount))
  1306. {
  1307. startTime = 0;
  1308. timeOffset = eventTime;
  1309. midiEventCount = 0;
  1310. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1311. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1312. else
  1313. nextBankId = 0;
  1314. }
  1315. else
  1316. startTime += timeOffset;
  1317. }
  1318. switch (event.type)
  1319. {
  1320. case kEngineEventTypeNull:
  1321. break;
  1322. case kEngineEventTypeControl: {
  1323. const EngineControlEvent& ctrlEvent(event.ctrl);
  1324. switch (ctrlEvent.type)
  1325. {
  1326. case kEngineControlEventTypeNull:
  1327. break;
  1328. case kEngineControlEventTypeParameter: {
  1329. float value;
  1330. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1331. // non-midi
  1332. if (event.channel == kEngineEventNonMidiChannel)
  1333. {
  1334. const uint32_t k = ctrlEvent.param;
  1335. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1336. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1337. {
  1338. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1339. }
  1340. else
  1341. {
  1342. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1343. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1344. else
  1345. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1346. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1347. value = std::rint(value);
  1348. }
  1349. setParameterValueRT(k, value, true);
  1350. continue;
  1351. }
  1352. // Control backend stuff
  1353. if (event.channel == pData->ctrlChannel)
  1354. {
  1355. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1356. {
  1357. value = ctrlEvent.value;
  1358. setDryWetRT(value, true);
  1359. }
  1360. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1361. {
  1362. value = ctrlEvent.value*127.0f/100.0f;
  1363. setVolumeRT(value, true);
  1364. }
  1365. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1366. {
  1367. float left, right;
  1368. value = ctrlEvent.value/0.5f - 1.0f;
  1369. if (value < 0.0f)
  1370. {
  1371. left = -1.0f;
  1372. right = (value*2.0f)+1.0f;
  1373. }
  1374. else if (value > 0.0f)
  1375. {
  1376. left = (value*2.0f)-1.0f;
  1377. right = 1.0f;
  1378. }
  1379. else
  1380. {
  1381. left = -1.0f;
  1382. right = 1.0f;
  1383. }
  1384. setBalanceLeftRT(left, true);
  1385. setBalanceRightRT(right, true);
  1386. }
  1387. }
  1388. #endif
  1389. // Control plugin parameters
  1390. for (uint32_t k=0; k < pData->param.count; ++k)
  1391. {
  1392. if (pData->param.data[k].midiChannel != event.channel)
  1393. continue;
  1394. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1395. continue;
  1396. if (pData->param.data[k].type != PARAMETER_INPUT)
  1397. continue;
  1398. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1399. continue;
  1400. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1401. {
  1402. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1403. }
  1404. else
  1405. {
  1406. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1407. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1408. else
  1409. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1410. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1411. value = std::rint(value);
  1412. }
  1413. setParameterValueRT(k, value, true);
  1414. }
  1415. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1416. {
  1417. if (midiEventCount >= kPluginMaxMidiEvents)
  1418. continue;
  1419. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1420. seqEvent.time.tick = isSampleAccurate ? startTime : eventTime;
  1421. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1422. seqEvent.data.control.channel = event.channel;
  1423. seqEvent.data.control.param = ctrlEvent.param;
  1424. seqEvent.data.control.value = int8_t(ctrlEvent.value*127.0f);
  1425. }
  1426. break;
  1427. } // case kEngineControlEventTypeParameter
  1428. case kEngineControlEventTypeMidiBank:
  1429. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1430. nextBankId = ctrlEvent.param;
  1431. break;
  1432. case kEngineControlEventTypeMidiProgram:
  1433. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1434. {
  1435. const uint32_t nextProgramId = ctrlEvent.param;
  1436. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1437. {
  1438. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1439. {
  1440. setMidiProgramRT(k, true);
  1441. break;
  1442. }
  1443. }
  1444. }
  1445. break;
  1446. case kEngineControlEventTypeAllSoundOff:
  1447. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1448. {
  1449. if (midiEventCount >= kPluginMaxMidiEvents)
  1450. continue;
  1451. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1452. seqEvent.time.tick = isSampleAccurate ? startTime : eventTime;
  1453. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1454. seqEvent.data.control.channel = event.channel;
  1455. seqEvent.data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1456. }
  1457. break;
  1458. case kEngineControlEventTypeAllNotesOff:
  1459. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1460. {
  1461. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1462. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1463. {
  1464. allNotesOffSent = true;
  1465. postponeRtAllNotesOff();
  1466. }
  1467. #endif
  1468. if (midiEventCount >= kPluginMaxMidiEvents)
  1469. continue;
  1470. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1471. seqEvent.time.tick = isSampleAccurate ? startTime : eventTime;
  1472. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1473. seqEvent.data.control.channel = event.channel;
  1474. seqEvent.data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1475. }
  1476. break;
  1477. } // switch (ctrlEvent.type)
  1478. break;
  1479. } // case kEngineEventTypeControl
  1480. case kEngineEventTypeMidi: {
  1481. if (midiEventCount >= kPluginMaxMidiEvents)
  1482. continue;
  1483. const EngineMidiEvent& midiEvent(event.midi);
  1484. if (midiEvent.size > EngineMidiEvent::kDataSize)
  1485. continue;
  1486. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1487. // Fix bad note-off (per DSSI spec)
  1488. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1489. status = MIDI_STATUS_NOTE_OFF;
  1490. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1491. seqEvent.time.tick = isSampleAccurate ? startTime : eventTime;
  1492. switch (status)
  1493. {
  1494. case MIDI_STATUS_NOTE_OFF: {
  1495. const uint8_t note = midiEvent.data[1];
  1496. seqEvent.type = SND_SEQ_EVENT_NOTEOFF;
  1497. seqEvent.data.note.channel = event.channel;
  1498. seqEvent.data.note.note = note;
  1499. pData->postponeRtEvent(kPluginPostRtEventNoteOff, true, event.channel, note, 0, 0.0f);
  1500. break;
  1501. }
  1502. case MIDI_STATUS_NOTE_ON: {
  1503. const uint8_t note = midiEvent.data[1];
  1504. const uint8_t velo = midiEvent.data[2];
  1505. seqEvent.type = SND_SEQ_EVENT_NOTEON;
  1506. seqEvent.data.note.channel = event.channel;
  1507. seqEvent.data.note.note = note;
  1508. seqEvent.data.note.velocity = velo;
  1509. pData->postponeRtEvent(kPluginPostRtEventNoteOn, true, event.channel, note, velo, 0.0f);
  1510. break;
  1511. }
  1512. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1513. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1514. {
  1515. const uint8_t note = midiEvent.data[1];
  1516. const uint8_t pressure = midiEvent.data[2];
  1517. seqEvent.type = SND_SEQ_EVENT_KEYPRESS;
  1518. seqEvent.data.note.channel = event.channel;
  1519. seqEvent.data.note.note = note;
  1520. seqEvent.data.note.velocity = pressure;
  1521. }
  1522. break;
  1523. case MIDI_STATUS_CONTROL_CHANGE:
  1524. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1525. {
  1526. const uint8_t control = midiEvent.data[1];
  1527. const uint8_t value = midiEvent.data[2];
  1528. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1529. seqEvent.data.control.channel = event.channel;
  1530. seqEvent.data.control.param = control;
  1531. seqEvent.data.control.value = value;
  1532. }
  1533. break;
  1534. case MIDI_STATUS_CHANNEL_PRESSURE:
  1535. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1536. {
  1537. const uint8_t pressure = midiEvent.data[1];
  1538. seqEvent.type = SND_SEQ_EVENT_CHANPRESS;
  1539. seqEvent.data.control.channel = event.channel;
  1540. seqEvent.data.control.value = pressure;
  1541. }
  1542. break;
  1543. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1544. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1545. {
  1546. const uint8_t lsb = midiEvent.data[1];
  1547. const uint8_t msb = midiEvent.data[2];
  1548. seqEvent.type = SND_SEQ_EVENT_PITCHBEND;
  1549. seqEvent.data.control.channel = event.channel;
  1550. seqEvent.data.control.value = ((msb << 7) | lsb) - 8192;
  1551. }
  1552. break;
  1553. default:
  1554. --midiEventCount;
  1555. break;
  1556. } // switch (status)
  1557. } break;
  1558. } // switch (event.type)
  1559. }
  1560. pData->postRtEvents.trySplice();
  1561. if (frames > timeOffset)
  1562. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset, midiEventCount);
  1563. } // End of Event Input and Processing
  1564. // --------------------------------------------------------------------------------------------------------
  1565. // Plugin processing (no events)
  1566. else
  1567. {
  1568. processSingle(audioIn, audioOut, frames, 0, midiEventCount);
  1569. } // End of Plugin processing (no events)
  1570. // --------------------------------------------------------------------------------------------------------
  1571. // Control Output
  1572. if (pData->event.portOut != nullptr)
  1573. {
  1574. uint8_t channel;
  1575. uint16_t param;
  1576. float value;
  1577. for (uint32_t k=0; k < pData->param.count; ++k)
  1578. {
  1579. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1580. continue;
  1581. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1582. if (pData->param.data[k].midiCC > 0)
  1583. {
  1584. channel = pData->param.data[k].midiChannel;
  1585. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1586. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1587. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1588. }
  1589. }
  1590. } // End of Control Output
  1591. }
  1592. bool processSingle(const float** const audioIn, float** const audioOut, const uint32_t frames,
  1593. const uint32_t timeOffset, const ulong midiEventCount)
  1594. {
  1595. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1596. if (pData->audioIn.count > 0)
  1597. {
  1598. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1599. }
  1600. if (pData->audioOut.count > 0)
  1601. {
  1602. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1603. }
  1604. // --------------------------------------------------------------------------------------------------------
  1605. // Try lock, silence otherwise
  1606. #ifndef STOAT_TEST_BUILD
  1607. if (pData->engine->isOffline())
  1608. {
  1609. pData->singleMutex.lock();
  1610. }
  1611. else
  1612. #endif
  1613. if (! pData->singleMutex.tryLock())
  1614. {
  1615. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1616. {
  1617. for (uint32_t k=0; k < frames; ++k)
  1618. audioOut[i][k+timeOffset] = 0.0f;
  1619. }
  1620. return false;
  1621. }
  1622. // --------------------------------------------------------------------------------------------------------
  1623. // Set audio buffers
  1624. const bool customMonoOut = pData->audioOut.count == 2 && fForcedStereoOut && ! fForcedStereoIn;
  1625. const bool customStereoOut = pData->audioOut.count == 2 && fForcedStereoIn && ! fForcedStereoOut;
  1626. if (! customMonoOut)
  1627. {
  1628. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1629. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1630. }
  1631. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1632. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1633. // --------------------------------------------------------------------------------------------------------
  1634. // Run plugin
  1635. uint instn = 0;
  1636. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next(), ++instn)
  1637. {
  1638. LADSPA_Handle const handle(it.getValue(nullptr));
  1639. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1640. // ----------------------------------------------------------------------------------------------------
  1641. // Mixdown for forced stereo
  1642. if (customMonoOut)
  1643. carla_zeroFloats(fAudioOutBuffers[instn], frames);
  1644. // ----------------------------------------------------------------------------------------------------
  1645. // Run it
  1646. if (fDssiDescriptor != nullptr && fDssiDescriptor->run_synth != nullptr)
  1647. {
  1648. try {
  1649. fDssiDescriptor->run_synth(handle, frames, fMidiEvents, midiEventCount);
  1650. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI run_synth");
  1651. }
  1652. else
  1653. {
  1654. try {
  1655. fDescriptor->run(handle, frames);
  1656. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI run");
  1657. }
  1658. // ----------------------------------------------------------------------------------------------------
  1659. // Mixdown for forced stereo
  1660. if (customMonoOut)
  1661. carla_multiply(fAudioOutBuffers[instn], 0.5f, frames);
  1662. else if (customStereoOut)
  1663. carla_copyFloats(fExtraStereoBuffer[instn], fAudioOutBuffers[instn], frames);
  1664. }
  1665. if (customStereoOut)
  1666. {
  1667. carla_copyFloats(fAudioOutBuffers[0], fExtraStereoBuffer[0], frames);
  1668. carla_copyFloats(fAudioOutBuffers[1], fExtraStereoBuffer[1], frames);
  1669. }
  1670. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1671. // --------------------------------------------------------------------------------------------------------
  1672. // Post-processing (dry/wet, volume and balance)
  1673. {
  1674. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1675. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1676. const bool isMono = (pData->audioIn.count == 1);
  1677. bool isPair;
  1678. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1679. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1680. {
  1681. // Dry/Wet
  1682. if (doDryWet)
  1683. {
  1684. const uint32_t c = isMono ? 0 : i;
  1685. for (uint32_t k=0; k < frames; ++k)
  1686. {
  1687. # ifndef BUILD_BRIDGE
  1688. if (k < pData->latency.frames && pData->latency.buffers != nullptr)
  1689. bufValue = pData->latency.buffers[c][k];
  1690. else if (pData->latency.frames < frames)
  1691. bufValue = fAudioInBuffers[c][k-pData->latency.frames];
  1692. else
  1693. # endif
  1694. bufValue = fAudioInBuffers[c][k];
  1695. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1696. }
  1697. }
  1698. // Balance
  1699. if (doBalance)
  1700. {
  1701. isPair = (i % 2 == 0);
  1702. if (isPair)
  1703. {
  1704. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1705. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1706. }
  1707. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1708. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1709. for (uint32_t k=0; k < frames; ++k)
  1710. {
  1711. if (isPair)
  1712. {
  1713. // left
  1714. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1715. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1716. }
  1717. else
  1718. {
  1719. // right
  1720. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1721. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1722. }
  1723. }
  1724. }
  1725. // Volume (and buffer copy)
  1726. {
  1727. for (uint32_t k=0; k < frames; ++k)
  1728. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1729. }
  1730. }
  1731. } // End of Post-processing
  1732. # ifndef BUILD_BRIDGE
  1733. // --------------------------------------------------------------------------------------------------------
  1734. // Save latency values for next callback
  1735. if (pData->latency.frames != 0 && pData->latency.buffers != nullptr)
  1736. {
  1737. CARLA_SAFE_ASSERT(timeOffset == 0);
  1738. const uint32_t latframes = pData->latency.frames;
  1739. if (latframes <= frames)
  1740. {
  1741. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1742. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  1743. }
  1744. else
  1745. {
  1746. const uint32_t diff = latframes - frames;
  1747. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1748. {
  1749. // push back buffer by 'frames'
  1750. for (k=0; k < diff; ++k)
  1751. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1752. // put current input at the end
  1753. for (uint32_t j=0; k < latframes; ++j, ++k)
  1754. pData->latency.buffers[i][k] = audioIn[i][j];
  1755. }
  1756. }
  1757. }
  1758. # endif
  1759. #else // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1760. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1761. {
  1762. for (uint32_t k=0; k < frames; ++k)
  1763. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1764. }
  1765. #endif
  1766. // --------------------------------------------------------------------------------------------------------
  1767. pData->singleMutex.unlock();
  1768. return true;
  1769. }
  1770. void bufferSizeChanged(const uint32_t newBufferSize) override
  1771. {
  1772. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1773. carla_debug("CarlaPluginLADSPADSSI::bufferSizeChanged(%i) - start", newBufferSize);
  1774. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1775. {
  1776. if (fAudioInBuffers[i] != nullptr)
  1777. delete[] fAudioInBuffers[i];
  1778. fAudioInBuffers[i] = new float[newBufferSize];
  1779. carla_zeroFloats(fAudioInBuffers[i], newBufferSize);
  1780. }
  1781. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1782. {
  1783. if (fAudioOutBuffers[i] != nullptr)
  1784. delete[] fAudioOutBuffers[i];
  1785. fAudioOutBuffers[i] = new float[newBufferSize];
  1786. carla_zeroFloats(fAudioOutBuffers[i], newBufferSize);
  1787. }
  1788. if (fExtraStereoBuffer[0] != nullptr)
  1789. {
  1790. delete[] fExtraStereoBuffer[0];
  1791. fExtraStereoBuffer[0] = nullptr;
  1792. }
  1793. if (fExtraStereoBuffer[1] != nullptr)
  1794. {
  1795. delete[] fExtraStereoBuffer[1];
  1796. fExtraStereoBuffer[1] = nullptr;
  1797. }
  1798. if (fForcedStereoIn && pData->audioOut.count == 2)
  1799. {
  1800. fExtraStereoBuffer[0] = new float[newBufferSize];
  1801. fExtraStereoBuffer[1] = new float[newBufferSize];
  1802. carla_zeroFloats(fExtraStereoBuffer[0], newBufferSize);
  1803. carla_zeroFloats(fExtraStereoBuffer[1], newBufferSize);
  1804. }
  1805. reconnectAudioPorts();
  1806. carla_debug("CarlaPluginLADSPADSSI::bufferSizeChanged(%i) - end", newBufferSize);
  1807. }
  1808. void sampleRateChanged(const double newSampleRate) override
  1809. {
  1810. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1811. carla_debug("CarlaPluginLADSPADSSI::sampleRateChanged(%g) - start", newSampleRate);
  1812. // TODO - handle UI stuff
  1813. if (pData->active)
  1814. deactivate();
  1815. const std::size_t instanceCount(fHandles.count());
  1816. if (fDescriptor->cleanup != nullptr)
  1817. {
  1818. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1819. {
  1820. LADSPA_Handle const handle(it.getValue(nullptr));
  1821. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1822. try {
  1823. fDescriptor->cleanup(handle);
  1824. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI cleanup");
  1825. }
  1826. }
  1827. fHandles.clear();
  1828. for (std::size_t i=0; i<instanceCount; ++i)
  1829. addInstance();
  1830. reconnectAudioPorts();
  1831. if (pData->active)
  1832. activate();
  1833. carla_debug("CarlaPluginLADSPADSSI::sampleRateChanged(%g) - end", newSampleRate);
  1834. }
  1835. void reconnectAudioPorts() const noexcept
  1836. {
  1837. if (fForcedStereoIn)
  1838. {
  1839. if (LADSPA_Handle const handle = fHandles.getFirst(nullptr))
  1840. {
  1841. try {
  1842. fDescriptor->connect_port(handle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1843. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (forced stereo input, first)");
  1844. }
  1845. if (LADSPA_Handle const handle = fHandles.getLast(nullptr))
  1846. {
  1847. try {
  1848. fDescriptor->connect_port(handle, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1849. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (forced stereo input, last)");
  1850. }
  1851. }
  1852. else
  1853. {
  1854. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1855. {
  1856. LADSPA_Handle const handle(it.getValue(nullptr));
  1857. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1858. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1859. {
  1860. try {
  1861. fDescriptor->connect_port(handle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1862. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (audio input)");
  1863. }
  1864. }
  1865. }
  1866. if (fForcedStereoOut)
  1867. {
  1868. if (LADSPA_Handle const handle = fHandles.getFirst(nullptr))
  1869. {
  1870. try {
  1871. fDescriptor->connect_port(handle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1872. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (forced stereo output, first)");
  1873. }
  1874. if (LADSPA_Handle const handle = fHandles.getLast(nullptr))
  1875. {
  1876. try {
  1877. fDescriptor->connect_port(handle, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1878. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (forced stereo output, last)");
  1879. }
  1880. }
  1881. else
  1882. {
  1883. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1884. {
  1885. LADSPA_Handle const handle(it.getValue(nullptr));
  1886. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1887. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1888. {
  1889. try {
  1890. fDescriptor->connect_port(handle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1891. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port (audio output)");
  1892. }
  1893. }
  1894. }
  1895. }
  1896. // -------------------------------------------------------------------
  1897. // Plugin buffers
  1898. void clearBuffers() noexcept override
  1899. {
  1900. carla_debug("CarlaPluginLADSPADSSI::clearBuffers() - start");
  1901. if (fAudioInBuffers != nullptr)
  1902. {
  1903. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1904. {
  1905. if (fAudioInBuffers[i] != nullptr)
  1906. {
  1907. delete[] fAudioInBuffers[i];
  1908. fAudioInBuffers[i] = nullptr;
  1909. }
  1910. }
  1911. delete[] fAudioInBuffers;
  1912. fAudioInBuffers = nullptr;
  1913. }
  1914. if (fAudioOutBuffers != nullptr)
  1915. {
  1916. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1917. {
  1918. if (fAudioOutBuffers[i] != nullptr)
  1919. {
  1920. delete[] fAudioOutBuffers[i];
  1921. fAudioOutBuffers[i] = nullptr;
  1922. }
  1923. }
  1924. delete[] fAudioOutBuffers;
  1925. fAudioOutBuffers = nullptr;
  1926. }
  1927. if (fExtraStereoBuffer[0] != nullptr)
  1928. {
  1929. delete[] fExtraStereoBuffer[0];
  1930. fExtraStereoBuffer[0] = nullptr;
  1931. }
  1932. if (fExtraStereoBuffer[1] != nullptr)
  1933. {
  1934. delete[] fExtraStereoBuffer[1];
  1935. fExtraStereoBuffer[1] = nullptr;
  1936. }
  1937. if (fParamBuffers != nullptr)
  1938. {
  1939. delete[] fParamBuffers;
  1940. fParamBuffers = nullptr;
  1941. }
  1942. CarlaPlugin::clearBuffers();
  1943. carla_debug("CarlaPluginLADSPADSSI::clearBuffers() - end");
  1944. }
  1945. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  1946. // -------------------------------------------------------------------
  1947. // OSC stuff
  1948. void handleOscMessage(const char* const method, const int argc, const void* const argvx, const char* const types, const lo_message msg) override
  1949. {
  1950. const lo_address source(lo_message_get_source(msg));
  1951. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  1952. // protocol for DSSI UIs *must* be UDP
  1953. CARLA_SAFE_ASSERT_RETURN(lo_address_get_protocol(source) == LO_UDP,);
  1954. if (fOscData.source == nullptr)
  1955. {
  1956. // if no UI is registered yet only "configure" and "update" messages are valid
  1957. CARLA_SAFE_ASSERT_RETURN(std::strcmp(method, "configure") == 0 || std::strcmp(method, "update") == 0,)
  1958. }
  1959. else
  1960. {
  1961. // make sure message source is the DSSI UI
  1962. const char* const msghost = lo_address_get_hostname(source);
  1963. const char* const msgport = lo_address_get_port(source);
  1964. const char* const ourhost = lo_address_get_hostname(fOscData.source);
  1965. const char* const ourport = lo_address_get_port(fOscData.source);
  1966. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msghost, ourhost) == 0,);
  1967. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msgport, ourport) == 0,);
  1968. }
  1969. const lo_arg* const* const argv(static_cast<const lo_arg* const*>(argvx));
  1970. if (std::strcmp(method, "configure") == 0)
  1971. return handleOscMessageConfigure(argc, argv, types);
  1972. if (std::strcmp(method, "control") == 0)
  1973. return handleOscMessageControl(argc, argv, types);
  1974. if (std::strcmp(method, "program") == 0)
  1975. return handleOscMessageProgram(argc, argv, types);
  1976. if (std::strcmp(method, "midi") == 0)
  1977. return handleOscMessageMIDI(argc, argv, types);
  1978. if (std::strcmp(method, "update") == 0)
  1979. return handleOscMessageUpdate(argc, argv, types, lo_message_get_source(msg));
  1980. if (std::strcmp(method, "exiting") == 0)
  1981. return handleOscMessageExiting();
  1982. carla_stdout("CarlaPluginLADSPADSSI::handleOscMessage() - unknown method '%s'", method);
  1983. }
  1984. void handleOscMessageConfigure(const int argc, const lo_arg* const* const argv, const char* const types)
  1985. {
  1986. carla_debug("CarlaPluginLADSPADSSI::handleMsgConfigure()");
  1987. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(2, "ss");
  1988. const char* const key = (const char*)&argv[0]->s;
  1989. const char* const value = (const char*)&argv[1]->s;
  1990. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1991. }
  1992. void handleOscMessageControl(const int argc, const lo_arg* const* const argv, const char* const types)
  1993. {
  1994. carla_debug("CarlaPluginLADSPADSSI::handleMsgControl()");
  1995. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(2, "if");
  1996. const int32_t rindex = argv[0]->i;
  1997. const float value = argv[1]->f;
  1998. setParameterValueByRealIndex(rindex, value, false, true, true);
  1999. }
  2000. void handleOscMessageProgram(const int argc, const lo_arg* const* const argv, const char* const types)
  2001. {
  2002. carla_debug("CarlaPluginLADSPADSSI::handleMsgProgram()");
  2003. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(2, "ii");
  2004. const int32_t bank = argv[0]->i;
  2005. const int32_t program = argv[1]->i;
  2006. CARLA_SAFE_ASSERT_RETURN(bank >= 0,);
  2007. CARLA_SAFE_ASSERT_RETURN(program >= 0,);
  2008. setMidiProgramById(static_cast<uint32_t>(bank), static_cast<uint32_t>(program), false, true, true);
  2009. }
  2010. void handleOscMessageMIDI(const int argc, const lo_arg* const* const argv, const char* const types)
  2011. {
  2012. carla_debug("CarlaPluginLADSPADSSI::handleMsgMidi()");
  2013. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(1, "m");
  2014. if (getMidiInCount() == 0)
  2015. {
  2016. carla_stderr("CarlaPluginLADSPADSSI::handleMsgMidi() - received midi when plugin has no midi inputs");
  2017. return;
  2018. }
  2019. const uint8_t* const data = argv[0]->m;
  2020. uint8_t status = data[1];
  2021. uint8_t channel = status & 0x0F;
  2022. // Fix bad note-off
  2023. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  2024. status = MIDI_STATUS_NOTE_OFF;
  2025. if (MIDI_IS_STATUS_NOTE_OFF(status))
  2026. {
  2027. const uint8_t note = data[2];
  2028. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2029. sendMidiSingleNote(channel, note, 0, false, true, true);
  2030. }
  2031. else if (MIDI_IS_STATUS_NOTE_ON(status))
  2032. {
  2033. const uint8_t note = data[2];
  2034. const uint8_t velo = data[3];
  2035. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2036. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  2037. sendMidiSingleNote(channel, note, velo, false, true, true);
  2038. }
  2039. }
  2040. void handleOscMessageUpdate(const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  2041. {
  2042. carla_debug("CarlaPluginLADSPADSSI::handleMsgUpdate()");
  2043. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(1, "s");
  2044. const char* const url = (const char*)&argv[0]->s;
  2045. // FIXME - remove debug prints later
  2046. carla_stdout("CarlaPluginLADSPADSSI::updateOscData(%p, \"%s\")", source, url);
  2047. fOscData.clear();
  2048. const int proto = lo_address_get_protocol(source);
  2049. {
  2050. const char* host = lo_address_get_hostname(source);
  2051. const char* port = lo_address_get_port(source);
  2052. fOscData.source = lo_address_new_with_proto(proto, host, port);
  2053. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  2054. }
  2055. {
  2056. char* host = lo_url_get_hostname(url);
  2057. char* port = lo_url_get_port(url);
  2058. fOscData.path = carla_strdup_free(lo_url_get_path(url));
  2059. fOscData.target = lo_address_new_with_proto(proto, host, port);
  2060. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, fOscData.path);
  2061. std::free(host);
  2062. std::free(port);
  2063. }
  2064. osc_send_sample_rate(fOscData, static_cast<float>(pData->engine->getSampleRate()));
  2065. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  2066. {
  2067. const CustomData& customData(it.getValue(kCustomDataFallback));
  2068. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  2069. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_STRING) == 0)
  2070. osc_send_configure(fOscData, customData.key, customData.value);
  2071. }
  2072. if (pData->prog.current >= 0)
  2073. osc_send_program(fOscData, static_cast<uint32_t>(pData->prog.current));
  2074. if (pData->midiprog.current >= 0)
  2075. {
  2076. const MidiProgramData& curMidiProg(pData->midiprog.getCurrent());
  2077. osc_send_program(fOscData, curMidiProg.bank, curMidiProg.program);
  2078. }
  2079. for (uint32_t i=0; i < pData->param.count; ++i)
  2080. osc_send_control(fOscData, pData->param.data[i].rindex, getParameterValue(i));
  2081. #ifndef BUILD_BRIDGE
  2082. if (pData->engine->getOptions().frontendWinId != 0)
  2083. pData->transientTryCounter = 1;
  2084. #endif
  2085. carla_stdout("CarlaPluginLADSPADSSI::updateOscData() - done");
  2086. }
  2087. void handleOscMessageExiting()
  2088. {
  2089. carla_debug("CarlaPluginLADSPADSSI::handleMsgExiting()");
  2090. // hide UI
  2091. showCustomUI(false);
  2092. // tell frontend
  2093. pData->engine->callback(true, true,
  2094. ENGINE_CALLBACK_UI_STATE_CHANGED,
  2095. pData->id,
  2096. 0,
  2097. 0, 0, 0.0f, nullptr);
  2098. }
  2099. // -------------------------------------------------------------------
  2100. // Post-poned UI Stuff
  2101. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2102. {
  2103. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2104. if (fOscData.target == nullptr)
  2105. return;
  2106. osc_send_control(fOscData, pData->param.data[index].rindex, value);
  2107. }
  2108. void uiMidiProgramChange(const uint32_t index) noexcept override
  2109. {
  2110. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2111. if (fOscData.target == nullptr)
  2112. return;
  2113. osc_send_program(fOscData, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2114. }
  2115. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2116. {
  2117. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2118. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2119. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2120. if (fOscData.target == nullptr)
  2121. return;
  2122. #if 0
  2123. uint8_t midiData[4];
  2124. midiData[0] = 0;
  2125. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  2126. midiData[2] = note;
  2127. midiData[3] = velo;
  2128. osc_send_midi(fOscData, midiData);
  2129. #endif
  2130. }
  2131. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2132. {
  2133. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2134. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2135. if (fOscData.target == nullptr)
  2136. return;
  2137. #if 0
  2138. uint8_t midiData[4];
  2139. midiData[0] = 0;
  2140. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  2141. midiData[2] = note;
  2142. midiData[3] = 0;
  2143. osc_send_midi(fOscData, midiData);
  2144. #endif
  2145. }
  2146. #endif // HAVE_LIBLO && !BUILD_BRIDGE
  2147. // -------------------------------------------------------------------
  2148. const void* getNativeDescriptor() const noexcept override
  2149. {
  2150. return fDssiDescriptor != nullptr
  2151. ? (const void*)fDssiDescriptor
  2152. : (const void*)fDescriptor;
  2153. }
  2154. const void* getExtraStuff() const noexcept override
  2155. {
  2156. if (fDssiDescriptor != nullptr)
  2157. {
  2158. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2159. return fUiFilename;
  2160. #else
  2161. return nullptr;
  2162. #endif
  2163. }
  2164. return fRdfDescriptor;
  2165. }
  2166. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2167. uintptr_t getUiBridgeProcessId() const noexcept override
  2168. {
  2169. return fThreadUI.getProcessId();
  2170. }
  2171. #endif
  2172. // -------------------------------------------------------------------
  2173. bool initLADSPA(const char* const filename, const char* name, const char* const label, const uint options,
  2174. const LADSPA_RDF_Descriptor* const rdfDescriptor)
  2175. {
  2176. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2177. // ---------------------------------------------------------------
  2178. // first checks
  2179. if (pData->client != nullptr)
  2180. {
  2181. pData->engine->setLastError("Plugin client is already registered");
  2182. return false;
  2183. }
  2184. if (filename == nullptr || filename[0] == '\0')
  2185. {
  2186. pData->engine->setLastError("null filename");
  2187. return false;
  2188. }
  2189. // ---------------------------------------------------------------
  2190. // open DLL
  2191. if (! pData->libOpen(filename))
  2192. {
  2193. pData->engine->setLastError(pData->libError(filename));
  2194. return false;
  2195. }
  2196. // ---------------------------------------------------------------
  2197. // get DLL main entry
  2198. const LADSPA_Descriptor_Function descFn = pData->libSymbol<LADSPA_Descriptor_Function>("ladspa_descriptor");
  2199. if (descFn == nullptr)
  2200. {
  2201. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  2202. return false;
  2203. }
  2204. // ---------------------------------------------------------------
  2205. // get descriptor that matches label
  2206. // if label is null, get first valid plugin
  2207. const bool nullLabel = (label == nullptr || label[0] == '\0');
  2208. for (ulong d=0;; ++d)
  2209. {
  2210. try {
  2211. fDescriptor = descFn(d);
  2212. }
  2213. catch(...) {
  2214. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  2215. fDescriptor = nullptr;
  2216. break;
  2217. }
  2218. if (fDescriptor == nullptr)
  2219. break;
  2220. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  2221. {
  2222. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  2223. fDescriptor = nullptr;
  2224. break;
  2225. }
  2226. if (fDescriptor->run == nullptr)
  2227. {
  2228. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  2229. fDescriptor = nullptr;
  2230. break;
  2231. }
  2232. if (nullLabel || std::strcmp(fDescriptor->Label, label) == 0)
  2233. break;
  2234. }
  2235. if (fDescriptor == nullptr)
  2236. {
  2237. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  2238. return false;
  2239. }
  2240. return init2(filename, name, options, rdfDescriptor);
  2241. }
  2242. bool initDSSI(const char* const filename, const char* name, const char* const label, const uint options)
  2243. {
  2244. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2245. // ---------------------------------------------------------------
  2246. // first checks
  2247. if (pData->client != nullptr)
  2248. {
  2249. pData->engine->setLastError("Plugin client is already registered");
  2250. return false;
  2251. }
  2252. if (filename == nullptr || filename[0] == '\0')
  2253. {
  2254. pData->engine->setLastError("null filename");
  2255. return false;
  2256. }
  2257. // ---------------------------------------------------------------
  2258. // open DLL
  2259. if (! pData->libOpen(filename))
  2260. {
  2261. pData->engine->setLastError(pData->libError(filename));
  2262. return false;
  2263. }
  2264. // ---------------------------------------------------------------
  2265. // get DLL main entry
  2266. const DSSI_Descriptor_Function descFn = pData->libSymbol<DSSI_Descriptor_Function>("dssi_descriptor");
  2267. if (descFn == nullptr)
  2268. {
  2269. pData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  2270. return false;
  2271. }
  2272. // ---------------------------------------------------------------
  2273. // get descriptor that matches label
  2274. // if label is null, get first valid plugin
  2275. const bool nullLabel = (label == nullptr || label[0] == '\0');
  2276. for (ulong d=0;; ++d)
  2277. {
  2278. try {
  2279. fDssiDescriptor = descFn(d);
  2280. }
  2281. catch(...) {
  2282. carla_stderr2("Caught exception when trying to get DSSI descriptor");
  2283. fDescriptor = nullptr;
  2284. fDssiDescriptor = nullptr;
  2285. break;
  2286. }
  2287. if (fDssiDescriptor == nullptr)
  2288. break;
  2289. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  2290. if (fDescriptor == nullptr)
  2291. {
  2292. carla_stderr2("WARNING - Missing LADSPA interface, will not use this plugin");
  2293. fDssiDescriptor = nullptr;
  2294. break;
  2295. }
  2296. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  2297. {
  2298. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  2299. fDescriptor = nullptr;
  2300. fDssiDescriptor = nullptr;
  2301. break;
  2302. }
  2303. if (fDescriptor->run == nullptr)
  2304. {
  2305. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  2306. fDescriptor = nullptr;
  2307. fDssiDescriptor = nullptr;
  2308. break;
  2309. }
  2310. if (nullLabel || std::strcmp(fDescriptor->Label, label) == 0)
  2311. break;
  2312. }
  2313. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  2314. {
  2315. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  2316. return false;
  2317. }
  2318. // ---------------------------------------------------------------
  2319. // check if uses global instance
  2320. if (fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  2321. {
  2322. pData->engine->setLastError("This plugin requires run_multiple_synths which is not supported");
  2323. return false;
  2324. }
  2325. return init2(filename, name, options, nullptr);
  2326. }
  2327. bool init2(const char* const filename, const char* name, const uint options,
  2328. const LADSPA_RDF_Descriptor* const rdfDescriptor)
  2329. {
  2330. // ---------------------------------------------------------------
  2331. // check for fixed buffer size requirement
  2332. fNeedsFixedBuffers = CarlaString(filename).contains("dssi-vst", true);
  2333. if (fNeedsFixedBuffers && ! pData->engine->usesConstantBufferSize())
  2334. {
  2335. pData->engine->setLastError("Cannot use this plugin under the current engine.\n"
  2336. "The plugin requires a fixed block size which is not possible right now.");
  2337. return false;
  2338. }
  2339. // ---------------------------------------------------------------
  2340. // get info
  2341. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  2342. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  2343. if (name == nullptr || name[0] == '\0')
  2344. {
  2345. /**/ if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  2346. name = fRdfDescriptor->Title;
  2347. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  2348. name = fDescriptor->Name;
  2349. else
  2350. name = fDescriptor->Label;
  2351. }
  2352. pData->name = pData->engine->getUniquePluginName(name);
  2353. pData->filename = carla_strdup(filename);
  2354. // ---------------------------------------------------------------
  2355. // register client
  2356. pData->client = pData->engine->addClient(this);
  2357. if (pData->client == nullptr || ! pData->client->isOk())
  2358. {
  2359. pData->engine->setLastError("Failed to register plugin client");
  2360. return false;
  2361. }
  2362. // ---------------------------------------------------------------
  2363. // initialize plugin
  2364. if (! addInstance())
  2365. return false;
  2366. // ---------------------------------------------------------------
  2367. // find latency port index
  2368. for (uint32_t i=0, iCtrl=0, count=getSafePortCount(); i<count; ++i)
  2369. {
  2370. const int portType(fDescriptor->PortDescriptors[i]);
  2371. if (! LADSPA_IS_PORT_CONTROL(portType))
  2372. continue;
  2373. const uint32_t index(iCtrl++);
  2374. if (! LADSPA_IS_PORT_OUTPUT(portType))
  2375. continue;
  2376. const char* const portName(fDescriptor->PortNames[i]);
  2377. CARLA_SAFE_ASSERT_BREAK(portName != nullptr);
  2378. if (std::strcmp(portName, "latency") == 0 ||
  2379. std::strcmp(portName, "_latency") == 0)
  2380. {
  2381. fLatencyIndex = static_cast<int32_t>(index);
  2382. break;
  2383. }
  2384. }
  2385. // ---------------------------------------------------------------
  2386. // check for custom data extension
  2387. if (fDssiDescriptor != nullptr && fDssiDescriptor->configure != nullptr)
  2388. {
  2389. if (char* const error = fDssiDescriptor->configure(fHandles.getFirst(nullptr), DSSI_CUSTOMDATA_EXTENSION_KEY, ""))
  2390. {
  2391. if (std::strcmp(error, "true") == 0 && fDssiDescriptor->get_custom_data != nullptr
  2392. && fDssiDescriptor->set_custom_data != nullptr)
  2393. fUsesCustomData = true;
  2394. std::free(error);
  2395. }
  2396. }
  2397. // ---------------------------------------------------------------
  2398. // get engine options
  2399. const EngineOptions& opts(pData->engine->getOptions());
  2400. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2401. // ---------------------------------------------------------------
  2402. // check for gui
  2403. if (opts.oscEnabled && opts.oscPortUDP >= 0)
  2404. {
  2405. if (const char* const guiFilename = find_dssi_ui(filename, fDescriptor->Label))
  2406. {
  2407. fUiFilename = guiFilename;
  2408. fThreadUI.setData(guiFilename, fDescriptor->Label);
  2409. }
  2410. }
  2411. #endif
  2412. // ---------------------------------------------------------------
  2413. // set options
  2414. pData->options = 0x0;
  2415. /**/ if (fLatencyIndex >= 0 || fNeedsFixedBuffers)
  2416. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2417. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2418. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2419. /**/ if (opts.forceStereo)
  2420. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2421. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2422. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2423. if (fDssiDescriptor != nullptr)
  2424. {
  2425. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  2426. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2427. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2428. if (fUsesCustomData)
  2429. if (isPluginOptionEnabled(options, PLUGIN_OPTION_USE_CHUNKS))
  2430. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  2431. if (fDssiDescriptor->run_synth != nullptr)
  2432. {
  2433. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2434. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2435. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2436. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2437. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2438. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2439. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2440. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2441. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2442. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2443. }
  2444. }
  2445. return true;
  2446. }
  2447. // -------------------------------------------------------------------
  2448. private:
  2449. LinkedList<LADSPA_Handle> fHandles;
  2450. const LADSPA_Descriptor* fDescriptor;
  2451. const DSSI_Descriptor* fDssiDescriptor;
  2452. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  2453. float** fAudioInBuffers;
  2454. float** fAudioOutBuffers;
  2455. float* fExtraStereoBuffer[2]; // used only if forcedStereoIn and audioOut == 2
  2456. float* fParamBuffers;
  2457. snd_seq_event_t fMidiEvents[kPluginMaxMidiEvents];
  2458. int32_t fLatencyIndex; // -1 if invalid
  2459. bool fForcedStereoIn;
  2460. bool fForcedStereoOut;
  2461. bool fNeedsFixedBuffers;
  2462. bool fUsesCustomData;
  2463. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2464. CarlaOscData fOscData;
  2465. CarlaThreadDSSIUI fThreadUI;
  2466. const char* fUiFilename;
  2467. #endif
  2468. // -------------------------------------------------------------------
  2469. bool addInstance()
  2470. {
  2471. LADSPA_Handle handle;
  2472. try {
  2473. handle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  2474. } CARLA_SAFE_EXCEPTION_RETURN_ERR("LADSPA/DSSI instantiate", "Plugin failed to initialize");
  2475. for (uint32_t i=0, count=pData->param.count; i<count; ++i)
  2476. {
  2477. const int32_t rindex(pData->param.data[i].rindex);
  2478. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0);
  2479. try {
  2480. fDescriptor->connect_port(handle, static_cast<ulong>(rindex), &fParamBuffers[i]);
  2481. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI connect_port");
  2482. }
  2483. if (fHandles.append(handle))
  2484. return true;
  2485. try {
  2486. fDescriptor->cleanup(handle);
  2487. } CARLA_SAFE_EXCEPTION("LADSPA/DSSI cleanup");
  2488. pData->engine->setLastError("Out of memory");
  2489. return false;
  2490. }
  2491. uint32_t getSafePortCount() const noexcept
  2492. {
  2493. if (fDescriptor->PortCount == 0)
  2494. return 0;
  2495. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  2496. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  2497. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  2498. return static_cast<uint32_t>(fDescriptor->PortCount);
  2499. }
  2500. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  2501. {
  2502. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  2503. return true;
  2504. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  2505. return true;
  2506. return false;
  2507. }
  2508. static bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf,
  2509. const bool wantName, const bool useBracket) noexcept
  2510. {
  2511. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  2512. if (sepBracketStart == nullptr)
  2513. return false;
  2514. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  2515. if (sepBracketEnd == nullptr)
  2516. return false;
  2517. const std::size_t unitSize(static_cast<std::size_t>(sepBracketEnd-sepBracketStart-2));
  2518. if (unitSize > 7) // very unlikely to have such big unit
  2519. return false;
  2520. const std::size_t sepIndex(std::strlen(paramName)-unitSize-3);
  2521. // just in case
  2522. if (sepIndex+2 >= STR_MAX)
  2523. return false;
  2524. if (wantName)
  2525. {
  2526. std::strncpy(strBuf, paramName, sepIndex);
  2527. strBuf[sepIndex] = '\0';
  2528. }
  2529. else
  2530. {
  2531. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  2532. strBuf[unitSize] = '\0';
  2533. }
  2534. return true;
  2535. }
  2536. // -------------------------------------------------------------------
  2537. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginLADSPADSSI)
  2538. };
  2539. // -------------------------------------------------------------------------------------------------------------------
  2540. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  2541. {
  2542. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\", " P_INT64 ", %x}, %p)",
  2543. init.engine, init.filename, init.name, init.label, init.uniqueId, init.options, rdfDescriptor);
  2544. CarlaPluginLADSPADSSI* const plugin(new CarlaPluginLADSPADSSI(init.engine, init.id));
  2545. if (! plugin->initLADSPA(init.filename, init.name, init.label, init.options, rdfDescriptor))
  2546. {
  2547. delete plugin;
  2548. return nullptr;
  2549. }
  2550. return plugin;
  2551. }
  2552. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  2553. {
  2554. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\", " P_INT64 ", %x})",
  2555. init.engine, init.filename, init.name, init.label, init.uniqueId, init.options);
  2556. CarlaPluginLADSPADSSI* const plugin(new CarlaPluginLADSPADSSI(init.engine, init.id));
  2557. if (! plugin->initDSSI(init.filename, init.name, init.label, init.options))
  2558. {
  2559. delete plugin;
  2560. return nullptr;
  2561. }
  2562. return plugin;
  2563. }
  2564. // -------------------------------------------------------------------------------------------------------------------
  2565. CARLA_BACKEND_END_NAMESPACE