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