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.

CarlaPluginBridge.cpp 115KB

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
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
11 years ago
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
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 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
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
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
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 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
10 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
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202
  1. /*
  2. * Carla Plugin Bridge
  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 "CarlaBackendUtils.hpp"
  19. #include "CarlaBase64Utils.hpp"
  20. #include "CarlaBridgeUtils.hpp"
  21. #include "CarlaEngineUtils.hpp"
  22. #include "CarlaMathUtils.hpp"
  23. #include "CarlaPipeUtils.hpp"
  24. #include "CarlaScopeUtils.hpp"
  25. #include "CarlaShmUtils.hpp"
  26. #include "CarlaThread.hpp"
  27. #include "jackbridge/JackBridge.hpp"
  28. #include <ctime>
  29. #include "water/files/File.h"
  30. #include "water/misc/Time.h"
  31. #include "water/threads/ChildProcess.h"
  32. // ---------------------------------------------------------------------------------------------------------------------
  33. using water::ChildProcess;
  34. using water::File;
  35. using water::String;
  36. using water::StringArray;
  37. using water::Time;
  38. CARLA_BACKEND_START_NAMESPACE
  39. // ---------------------------------------------------------------------------------------------------------------------
  40. // Fallback data
  41. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  42. // ---------------------------------------------------------------------------------------------------------------------
  43. static String findWinePrefix(const String filename, const int recursionLimit = 10)
  44. {
  45. if (recursionLimit == 0 || filename.length() < 5 || ! filename.contains("/"))
  46. return "";
  47. const String path(filename.upToLastOccurrenceOf("/", false, false));
  48. if (File(path + "/dosdevices").isDirectory())
  49. return path;
  50. return findWinePrefix(path, recursionLimit-1);
  51. }
  52. // ---------------------------------------------------------------------------------------------------------------------
  53. struct BridgeParamInfo {
  54. float value;
  55. CarlaString name;
  56. CarlaString symbol;
  57. CarlaString unit;
  58. BridgeParamInfo() noexcept
  59. : value(0.0f),
  60. name(),
  61. symbol(),
  62. unit() {}
  63. CARLA_DECLARE_NON_COPY_STRUCT(BridgeParamInfo)
  64. };
  65. // ---------------------------------------------------------------------------------------------------------------------
  66. class CarlaPluginBridgeThread : public CarlaThread
  67. {
  68. public:
  69. CarlaPluginBridgeThread(CarlaEngine* const engine, CarlaPlugin* const plugin) noexcept
  70. : CarlaThread("CarlaPluginBridgeThread"),
  71. kEngine(engine),
  72. kPlugin(plugin),
  73. fBinaryArchName(),
  74. fBridgeBinary(),
  75. fLabel(),
  76. fShmIds(),
  77. #ifndef CARLA_OS_WIN
  78. fWinePrefix(),
  79. #endif
  80. fProcess() {}
  81. void setData(
  82. #ifndef CARLA_OS_WIN
  83. const char* const winePrefix,
  84. #endif
  85. const char* const binaryArchName,
  86. const char* const bridgeBinary,
  87. const char* const label,
  88. const char* const shmIds) noexcept
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(bridgeBinary != nullptr && bridgeBinary[0] != '\0',);
  91. CARLA_SAFE_ASSERT_RETURN(shmIds != nullptr && shmIds[0] != '\0',);
  92. CARLA_SAFE_ASSERT(! isThreadRunning());
  93. #ifndef CARLA_OS_WIN
  94. fWinePrefix = winePrefix;
  95. #endif
  96. fBinaryArchName = binaryArchName;
  97. fBridgeBinary = bridgeBinary;
  98. fShmIds = shmIds;
  99. if (label != nullptr)
  100. fLabel = label;
  101. if (fLabel.isEmpty())
  102. fLabel = "(none)";
  103. }
  104. uintptr_t getProcessPID() const noexcept
  105. {
  106. CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
  107. return (uintptr_t)fProcess->getPID();
  108. }
  109. protected:
  110. void run()
  111. {
  112. if (fProcess == nullptr)
  113. {
  114. fProcess = new ChildProcess();
  115. }
  116. else if (fProcess->isRunning())
  117. {
  118. carla_stderr("CarlaPluginBridgeThread::run() - already running");
  119. }
  120. char strBuf[STR_MAX+1];
  121. strBuf[STR_MAX] = '\0';
  122. const EngineOptions& options(kEngine->getOptions());
  123. String filename(kPlugin->getFilename());
  124. if (filename.isEmpty())
  125. filename = "(none)";
  126. StringArray arguments;
  127. #ifndef CARLA_OS_WIN
  128. // start with "wine" if needed
  129. if (fBridgeBinary.endsWithIgnoreCase(".exe"))
  130. {
  131. String wineCMD;
  132. if (options.wine.executable != nullptr && options.wine.executable[0] != '\0')
  133. {
  134. wineCMD = options.wine.executable;
  135. if (fBridgeBinary.endsWithIgnoreCase("64.exe")
  136. && options.wine.executable[0] == CARLA_OS_SEP
  137. && File(wineCMD + "64").existsAsFile())
  138. wineCMD += "64";
  139. }
  140. else
  141. {
  142. wineCMD = "wine";
  143. }
  144. arguments.add(wineCMD);
  145. }
  146. #endif
  147. // setup binary arch
  148. ChildProcess::Type childType;
  149. #ifdef CARLA_OS_MAC
  150. if (fBinaryArchName == "arm64")
  151. childType = ChildProcess::TypeARM;
  152. else if (fBinaryArchName == "x86_64")
  153. childType = ChildProcess::TypeIntel;
  154. else
  155. #endif
  156. childType = ChildProcess::TypeAny;
  157. // bridge binary
  158. arguments.add(fBridgeBinary);
  159. // plugin type
  160. arguments.add(getPluginTypeAsString(kPlugin->getType()));
  161. // filename
  162. arguments.add(filename);
  163. // label
  164. arguments.add(fLabel);
  165. // uniqueId
  166. arguments.add(String(static_cast<water::int64>(kPlugin->getUniqueId())));
  167. bool started;
  168. {
  169. const ScopedEngineEnvironmentLocker _seel(kEngine);
  170. #ifdef CARLA_OS_LINUX
  171. const CarlaScopedEnvVar sev1("LD_LIBRARY_PATH", nullptr);
  172. const CarlaScopedEnvVar sev2("LD_PRELOAD", nullptr);
  173. #endif
  174. carla_setenv("ENGINE_OPTION_FORCE_STEREO", bool2str(options.forceStereo));
  175. carla_setenv("ENGINE_OPTION_PREFER_PLUGIN_BRIDGES", bool2str(options.preferPluginBridges));
  176. carla_setenv("ENGINE_OPTION_PREFER_UI_BRIDGES", bool2str(options.preferUiBridges));
  177. carla_setenv("ENGINE_OPTION_UIS_ALWAYS_ON_TOP", bool2str(options.uisAlwaysOnTop));
  178. std::snprintf(strBuf, STR_MAX, "%u", options.maxParameters);
  179. carla_setenv("ENGINE_OPTION_MAX_PARAMETERS", strBuf);
  180. std::snprintf(strBuf, STR_MAX, "%u", options.uiBridgesTimeout);
  181. carla_setenv("ENGINE_OPTION_UI_BRIDGES_TIMEOUT",strBuf);
  182. if (options.pathLADSPA != nullptr)
  183. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", options.pathLADSPA);
  184. else
  185. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", "");
  186. if (options.pathDSSI != nullptr)
  187. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", options.pathDSSI);
  188. else
  189. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", "");
  190. if (options.pathLV2 != nullptr)
  191. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", options.pathLV2);
  192. else
  193. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", "");
  194. if (options.pathVST2 != nullptr)
  195. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", options.pathVST2);
  196. else
  197. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", "");
  198. if (options.pathVST3 != nullptr)
  199. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", options.pathVST3);
  200. else
  201. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", "");
  202. if (options.pathSF2 != nullptr)
  203. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", options.pathSF2);
  204. else
  205. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", "");
  206. if (options.pathSFZ != nullptr)
  207. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", options.pathSFZ);
  208. else
  209. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", "");
  210. if (options.binaryDir != nullptr)
  211. carla_setenv("ENGINE_OPTION_PATH_BINARIES", options.binaryDir);
  212. else
  213. carla_setenv("ENGINE_OPTION_PATH_BINARIES", "");
  214. if (options.resourceDir != nullptr)
  215. carla_setenv("ENGINE_OPTION_PATH_RESOURCES", options.resourceDir);
  216. else
  217. carla_setenv("ENGINE_OPTION_PATH_RESOURCES", "");
  218. carla_setenv("ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR", bool2str(options.preventBadBehaviour));
  219. std::snprintf(strBuf, STR_MAX, P_UINTPTR, options.frontendWinId);
  220. carla_setenv("ENGINE_OPTION_FRONTEND_WIN_ID", strBuf);
  221. carla_setenv("ENGINE_BRIDGE_SHM_IDS", fShmIds.toRawUTF8());
  222. #ifndef CARLA_OS_WIN
  223. if (fWinePrefix.isNotEmpty())
  224. {
  225. carla_setenv("WINEDEBUG", "-all");
  226. carla_setenv("WINEPREFIX", fWinePrefix.toRawUTF8());
  227. if (options.wine.rtPrio)
  228. {
  229. carla_setenv("STAGING_SHARED_MEMORY", "1");
  230. carla_setenv("WINE_RT_POLICY", "FF");
  231. std::snprintf(strBuf, STR_MAX, "%i", options.wine.baseRtPrio);
  232. carla_setenv("STAGING_RT_PRIORITY_BASE", strBuf);
  233. carla_setenv("WINE_RT", strBuf);
  234. carla_setenv("WINE_RT_PRIO", strBuf);
  235. std::snprintf(strBuf, STR_MAX, "%i", options.wine.serverRtPrio);
  236. carla_setenv("STAGING_RT_PRIORITY_SERVER", strBuf);
  237. carla_setenv("WINE_SVR_RT", strBuf);
  238. carla_stdout("Using WINEPREFIX '%s', with base RT prio %i and server RT prio %i",
  239. fWinePrefix.toRawUTF8(), options.wine.baseRtPrio, options.wine.serverRtPrio);
  240. }
  241. else
  242. {
  243. carla_unsetenv("STAGING_SHARED_MEMORY");
  244. carla_unsetenv("WINE_RT_POLICY");
  245. carla_unsetenv("STAGING_RT_PRIORITY_BASE");
  246. carla_unsetenv("STAGING_RT_PRIORITY_SERVER");
  247. carla_unsetenv("WINE_RT");
  248. carla_unsetenv("WINE_RT_PRIO");
  249. carla_unsetenv("WINE_SVR_RT");
  250. carla_stdout("Using WINEPREFIX '%s', without RT priorities", fWinePrefix.toRawUTF8());
  251. }
  252. }
  253. #endif
  254. carla_stdout("Starting plugin bridge, command is:\n%s \"%s\" \"%s\" \"%s\" " P_INT64,
  255. fBridgeBinary.toRawUTF8(), getPluginTypeAsString(kPlugin->getType()), filename.toRawUTF8(), fLabel.toRawUTF8(), kPlugin->getUniqueId());
  256. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  257. const File projFolder(kEngine->getCurrentProjectFolder());
  258. if (projFolder.isNotNull())
  259. {
  260. const File oldFolder(File::getCurrentWorkingDirectory());
  261. projFolder.setAsCurrentWorkingDirectory();
  262. started = fProcess->start(arguments, childType);
  263. oldFolder.setAsCurrentWorkingDirectory();
  264. }
  265. else
  266. #endif
  267. {
  268. started = fProcess->start(arguments, childType);
  269. }
  270. }
  271. if (! started)
  272. {
  273. carla_stdout("failed!");
  274. fProcess = nullptr;
  275. return;
  276. }
  277. for (; fProcess->isRunning() && ! shouldThreadExit();)
  278. carla_sleep(1);
  279. // we only get here if bridge crashed or thread asked to exit
  280. if (fProcess->isRunning() && shouldThreadExit())
  281. {
  282. fProcess->waitForProcessToFinish(2000);
  283. if (fProcess->isRunning())
  284. {
  285. carla_stdout("CarlaPluginBridgeThread::run() - bridge refused to close, force kill now");
  286. fProcess->kill();
  287. }
  288. else
  289. {
  290. carla_stdout("CarlaPluginBridgeThread::run() - bridge auto-closed successfully");
  291. }
  292. }
  293. else
  294. {
  295. // forced quit, may have crashed
  296. if (fProcess->getExitCodeAndClearPID() != 0)
  297. {
  298. carla_stderr("CarlaPluginBridgeThread::run() - bridge crashed");
  299. CarlaString errorString("Plugin '" + CarlaString(kPlugin->getName()) + "' has crashed!\n"
  300. "Saving now will lose its current settings.\n"
  301. "Please remove this plugin, and not rely on it from this point.");
  302. kEngine->callback(true, true,
  303. CarlaBackend::ENGINE_CALLBACK_ERROR, kPlugin->getId(), 0, 0, 0, 0.0f, errorString);
  304. }
  305. }
  306. fProcess = nullptr;
  307. }
  308. private:
  309. CarlaEngine* const kEngine;
  310. CarlaPlugin* const kPlugin;
  311. String fBinaryArchName;
  312. String fBridgeBinary;
  313. String fLabel;
  314. String fShmIds;
  315. #ifndef CARLA_OS_WIN
  316. String fWinePrefix;
  317. #endif
  318. CarlaScopedPointer<ChildProcess> fProcess;
  319. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridgeThread)
  320. };
  321. // ---------------------------------------------------------------------------------------------------------------------
  322. class CarlaPluginBridge : public CarlaPlugin
  323. {
  324. public:
  325. CarlaPluginBridge(CarlaEngine* const engine, const uint id, const BinaryType btype, const PluginType ptype)
  326. : CarlaPlugin(engine, id),
  327. fBinaryType(btype),
  328. fPluginType(ptype),
  329. fBridgeVersion(6), // before kPluginBridgeNonRtServerVersion was a thing, API was at 6
  330. fInitiated(false),
  331. fInitError(false),
  332. fSaved(true),
  333. fTimedOut(false),
  334. fTimedError(false),
  335. fBufferSize(engine->getBufferSize()),
  336. fProcWaitTime(0),
  337. fBridgeBinary(),
  338. fBridgeThread(engine, this),
  339. fShmAudioPool(),
  340. fShmRtClientControl(),
  341. fShmNonRtClientControl(),
  342. fShmNonRtServerControl(),
  343. #ifndef CARLA_OS_WIN
  344. fWinePrefix(),
  345. #endif
  346. fReceivingParamText(),
  347. fInfo(),
  348. fUniqueId(0),
  349. fLatency(0),
  350. fParams(nullptr)
  351. {
  352. carla_debug("CarlaPluginBridge::CarlaPluginBridge(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
  353. pData->hints |= PLUGIN_IS_BRIDGE;
  354. }
  355. ~CarlaPluginBridge() override
  356. {
  357. carla_debug("CarlaPluginBridge::~CarlaPluginBridge()");
  358. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  359. // close UI
  360. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  361. pData->transientTryCounter = 0;
  362. #endif
  363. pData->singleMutex.lock();
  364. pData->masterMutex.lock();
  365. if (pData->client != nullptr && pData->client->isActive())
  366. pData->client->deactivate(true);
  367. if (pData->active)
  368. {
  369. deactivate();
  370. pData->active = false;
  371. }
  372. if (fBridgeThread.isThreadRunning())
  373. {
  374. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientQuit);
  375. fShmNonRtClientControl.commitWrite();
  376. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientQuit);
  377. fShmRtClientControl.commitWrite();
  378. if (! fTimedOut)
  379. waitForClient("stopping", 3000);
  380. }
  381. fBridgeThread.stopThread(3000);
  382. fShmNonRtServerControl.clear();
  383. fShmNonRtClientControl.clear();
  384. fShmRtClientControl.clear();
  385. fShmAudioPool.clear();
  386. clearBuffers();
  387. fInfo.chunk.clear();
  388. }
  389. // -------------------------------------------------------------------
  390. // Information (base)
  391. BinaryType getBinaryType() const noexcept override
  392. {
  393. return fBinaryType;
  394. }
  395. PluginType getType() const noexcept override
  396. {
  397. return fPluginType;
  398. }
  399. PluginCategory getCategory() const noexcept override
  400. {
  401. return fInfo.category;
  402. }
  403. int64_t getUniqueId() const noexcept override
  404. {
  405. return fUniqueId;
  406. }
  407. uint32_t getLatencyInFrames() const noexcept override
  408. {
  409. return fLatency;
  410. }
  411. // -------------------------------------------------------------------
  412. // Information (count)
  413. uint32_t getMidiInCount() const noexcept override
  414. {
  415. return fInfo.mIns;
  416. }
  417. uint32_t getMidiOutCount() const noexcept override
  418. {
  419. return fInfo.mOuts;
  420. }
  421. // -------------------------------------------------------------------
  422. // Information (current data)
  423. std::size_t getChunkData(void** const dataPtr) noexcept override
  424. {
  425. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  426. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  427. waitForSaved();
  428. CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.size() > 0, 0);
  429. #ifdef CARLA_PROPER_CPP11_SUPPORT
  430. *dataPtr = fInfo.chunk.data();
  431. #else
  432. *dataPtr = &fInfo.chunk.front();
  433. #endif
  434. return fInfo.chunk.size();
  435. }
  436. // -------------------------------------------------------------------
  437. // Information (per-plugin data)
  438. uint getOptionsAvailable() const noexcept override
  439. {
  440. return fInfo.optionsAvailable;
  441. }
  442. float getParameterValue(const uint32_t parameterId) const noexcept override
  443. {
  444. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  445. return fParams[parameterId].value;
  446. }
  447. bool getLabel(char* const strBuf) const noexcept override
  448. {
  449. std::strncpy(strBuf, fInfo.label, STR_MAX);
  450. return true;
  451. }
  452. bool getMaker(char* const strBuf) const noexcept override
  453. {
  454. std::strncpy(strBuf, fInfo.maker, STR_MAX);
  455. return true;
  456. }
  457. bool getCopyright(char* const strBuf) const noexcept override
  458. {
  459. std::strncpy(strBuf, fInfo.copyright, STR_MAX);
  460. return true;
  461. }
  462. bool getRealName(char* const strBuf) const noexcept override
  463. {
  464. std::strncpy(strBuf, fInfo.name, STR_MAX);
  465. return true;
  466. }
  467. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  468. {
  469. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  470. std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
  471. return true;
  472. }
  473. bool getParameterText(const uint32_t parameterId, char* const strBuf) noexcept override
  474. {
  475. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  476. CARLA_SAFE_ASSERT_RETURN(! fReceivingParamText.isCurrentlyWaitingData(), false);
  477. const int32_t parameterIdi = static_cast<int32_t>(parameterId);
  478. fReceivingParamText.setTargetData(parameterIdi, strBuf);
  479. {
  480. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  481. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientGetParameterText);
  482. fShmNonRtClientControl.writeInt(parameterIdi);
  483. fShmNonRtClientControl.commitWrite();
  484. }
  485. if (waitForParameterText())
  486. return true;
  487. std::snprintf(strBuf, STR_MAX, "%.12g", static_cast<double>(fParams[parameterId].value));
  488. return false;
  489. }
  490. bool getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  491. {
  492. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  493. std::strncpy(strBuf, fParams[parameterId].symbol.buffer(), STR_MAX);
  494. return true;
  495. }
  496. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  497. {
  498. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  499. std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
  500. return true;
  501. }
  502. // -------------------------------------------------------------------
  503. // Set data (state)
  504. void prepareForSave(bool) noexcept override
  505. {
  506. fSaved = false;
  507. {
  508. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  509. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPrepareForSave);
  510. fShmNonRtClientControl.commitWrite();
  511. }
  512. }
  513. bool waitForParameterText()
  514. {
  515. bool success;
  516. if (fReceivingParamText.wasDataReceived(&success))
  517. return success;
  518. const uint32_t timeoutEnd = Time::getMillisecondCounter() + 500; // 500 ms
  519. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  520. for (; Time::getMillisecondCounter() < timeoutEnd && fBridgeThread.isThreadRunning();)
  521. {
  522. if (fReceivingParamText.wasDataReceived(&success))
  523. return success;
  524. if (needsEngineIdle)
  525. pData->engine->idle();
  526. carla_msleep(5);
  527. }
  528. if (! fBridgeThread.isThreadRunning())
  529. {
  530. carla_stderr("CarlaPluginBridge::waitForParameterText() - Bridge is not running");
  531. return false;
  532. }
  533. carla_stderr("CarlaPluginBridge::waitForParameterText() - Timeout while requesting text");
  534. return false;
  535. }
  536. void waitForSaved()
  537. {
  538. if (fSaved)
  539. return;
  540. // TODO: only wait 1 minute for NI plugins
  541. const uint32_t timeoutEnd = Time::getMillisecondCounter() + 60*1000; // 60 secs, 1 minute
  542. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  543. for (; Time::getMillisecondCounter() < timeoutEnd && fBridgeThread.isThreadRunning();)
  544. {
  545. pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  546. if (needsEngineIdle)
  547. pData->engine->idle();
  548. if (fSaved)
  549. break;
  550. carla_msleep(20);
  551. }
  552. if (! fBridgeThread.isThreadRunning())
  553. return carla_stderr("CarlaPluginBridge::waitForSaved() - Bridge is not running");
  554. if (! fSaved)
  555. return carla_stderr("CarlaPluginBridge::waitForSaved() - Timeout while requesting save state");
  556. }
  557. // -------------------------------------------------------------------
  558. // Set data (internal stuff)
  559. void setOption(const uint option, const bool yesNo, const bool sendCallback) override
  560. {
  561. {
  562. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  563. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOption);
  564. fShmNonRtClientControl.writeUInt(option);
  565. fShmNonRtClientControl.writeBool(yesNo);
  566. fShmNonRtClientControl.commitWrite();
  567. }
  568. CarlaPlugin::setOption(option, yesNo, sendCallback);
  569. }
  570. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  571. {
  572. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  573. {
  574. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  575. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCtrlChannel);
  576. fShmNonRtClientControl.writeShort(channel);
  577. fShmNonRtClientControl.commitWrite();
  578. }
  579. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  580. }
  581. void setName(const char* const newName) override
  582. {
  583. CarlaPlugin::setName(newName);
  584. if (pData->uiTitle.isEmpty() && fBridgeVersion >= 8)
  585. _setUiTitleFromName();
  586. }
  587. // -------------------------------------------------------------------
  588. // Set data (plugin-specific stuff)
  589. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  590. {
  591. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  592. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
  593. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  594. fParams[parameterId].value = fixedValue;
  595. {
  596. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  597. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
  598. fShmNonRtClientControl.writeUInt(parameterId);
  599. fShmNonRtClientControl.writeFloat(value);
  600. fShmNonRtClientControl.commitWrite();
  601. fShmNonRtClientControl.waitIfDataIsReachingLimit();
  602. }
  603. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  604. }
  605. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  606. {
  607. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  608. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  609. fParams[parameterId].value = fixedValue;
  610. {
  611. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  612. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
  613. fShmNonRtClientControl.writeUInt(parameterId);
  614. fShmNonRtClientControl.writeFloat(value);
  615. fShmNonRtClientControl.commitWrite();
  616. fShmNonRtClientControl.waitIfDataIsReachingLimit();
  617. }
  618. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  619. }
  620. void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  621. {
  622. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  623. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  624. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  625. {
  626. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  627. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMidiChannel);
  628. fShmNonRtClientControl.writeUInt(parameterId);
  629. fShmNonRtClientControl.writeByte(channel);
  630. fShmNonRtClientControl.commitWrite();
  631. }
  632. CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
  633. }
  634. void setParameterMappedControlIndex(const uint32_t parameterId, const int16_t index,
  635. const bool sendOsc, const bool sendCallback,
  636. const bool reconfigureNow) noexcept override
  637. {
  638. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  639. CARLA_SAFE_ASSERT_RETURN(index >= CONTROL_INDEX_NONE && index <= CONTROL_INDEX_MAX_ALLOWED,);
  640. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  641. {
  642. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  643. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedControlIndex);
  644. fShmNonRtClientControl.writeUInt(parameterId);
  645. fShmNonRtClientControl.writeShort(index);
  646. fShmNonRtClientControl.commitWrite();
  647. }
  648. CarlaPlugin::setParameterMappedControlIndex(parameterId, index, sendOsc, sendCallback, reconfigureNow);
  649. }
  650. void setParameterMappedRange(const uint32_t parameterId, const float minimum, const float maximum, const bool sendOsc, const bool sendCallback) noexcept override
  651. {
  652. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  653. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  654. // kPluginBridgeNonRtClientSetParameterMappedRange was added in API 7
  655. if (fBridgeVersion >= 7)
  656. {
  657. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  658. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedRange);
  659. fShmNonRtClientControl.writeUInt(parameterId);
  660. fShmNonRtClientControl.writeFloat(minimum);
  661. fShmNonRtClientControl.writeFloat(maximum);
  662. fShmNonRtClientControl.commitWrite();
  663. }
  664. CarlaPlugin::setParameterMappedRange(parameterId, minimum, maximum, sendOsc, sendCallback);
  665. }
  666. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  667. {
  668. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  669. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  670. {
  671. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  672. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
  673. fShmNonRtClientControl.writeInt(index);
  674. fShmNonRtClientControl.commitWrite();
  675. }
  676. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  677. }
  678. void setProgramRT(const uint32_t index, const bool sendCallbackLater) noexcept override
  679. {
  680. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  681. {
  682. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  683. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
  684. fShmNonRtClientControl.writeInt(static_cast<int32_t>(index));
  685. fShmNonRtClientControl.commitWrite();
  686. }
  687. CarlaPlugin::setProgramRT(index, sendCallbackLater);
  688. }
  689. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  690. {
  691. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  692. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  693. {
  694. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  695. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
  696. fShmNonRtClientControl.writeInt(index);
  697. fShmNonRtClientControl.commitWrite();
  698. }
  699. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  700. }
  701. void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
  702. {
  703. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  704. {
  705. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  706. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
  707. fShmNonRtClientControl.writeInt(static_cast<int32_t>(uindex));
  708. fShmNonRtClientControl.commitWrite();
  709. }
  710. CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
  711. }
  712. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  713. {
  714. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  715. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  716. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  717. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  718. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  719. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "__CarlaPingOnOff__") == 0)
  720. {
  721. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  722. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPingOnOff);
  723. fShmNonRtClientControl.writeBool(std::strcmp(value, "true") == 0);
  724. fShmNonRtClientControl.commitWrite();
  725. return;
  726. }
  727. const uint32_t typeLen(static_cast<uint32_t>(std::strlen(type)));
  728. const uint32_t keyLen(static_cast<uint32_t>(std::strlen(key)));
  729. const uint32_t valueLen(static_cast<uint32_t>(std::strlen(value)));
  730. {
  731. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  732. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCustomData);
  733. fShmNonRtClientControl.writeUInt(typeLen);
  734. fShmNonRtClientControl.writeCustomData(type, typeLen);
  735. fShmNonRtClientControl.writeUInt(keyLen);
  736. fShmNonRtClientControl.writeCustomData(key, keyLen);
  737. fShmNonRtClientControl.writeUInt(valueLen);
  738. if (valueLen > 0)
  739. fShmNonRtClientControl.writeCustomData(value, valueLen);
  740. fShmNonRtClientControl.commitWrite();
  741. }
  742. CarlaPlugin::setCustomData(type, key, value, sendGui);
  743. }
  744. void setChunkData(const void* const data, const std::size_t dataSize) override
  745. {
  746. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  747. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  748. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  749. CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
  750. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0,);
  751. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  752. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  753. filePath += fShmAudioPool.getFilenameSuffix();
  754. if (File(filePath).replaceWithText(dataBase64.buffer()))
  755. {
  756. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  757. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  758. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
  759. fShmNonRtClientControl.writeUInt(ulength);
  760. fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
  761. fShmNonRtClientControl.commitWrite();
  762. }
  763. // save data internally as well
  764. fInfo.chunk.resize(dataSize);
  765. #ifdef CARLA_PROPER_CPP11_SUPPORT
  766. std::memcpy(fInfo.chunk.data(), data, dataSize);
  767. #else
  768. std::memcpy(&fInfo.chunk.front(), data, dataSize);
  769. #endif
  770. }
  771. // -------------------------------------------------------------------
  772. // Set ui stuff
  773. void setCustomUITitle(const char* const title) noexcept override
  774. {
  775. carla_debug("CarlaPluginBridge::setCustomUITitle(%s)", title);
  776. if (fBridgeVersion >= 8)
  777. {
  778. const uint32_t size = static_cast<uint32_t>(std::strlen(title));
  779. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  780. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetWindowTitle);
  781. fShmNonRtClientControl.writeUInt(size);
  782. fShmNonRtClientControl.writeCustomData(title, size);
  783. fShmNonRtClientControl.commitWrite();
  784. }
  785. CarlaPlugin::setCustomUITitle(title);
  786. }
  787. void showCustomUI(const bool yesNo) override
  788. {
  789. if (yesNo && pData->uiTitle.isEmpty() && fBridgeVersion >= 8)
  790. _setUiTitleFromName();
  791. {
  792. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  793. fShmNonRtClientControl.writeOpcode(yesNo ? kPluginBridgeNonRtClientShowUI : kPluginBridgeNonRtClientHideUI);
  794. fShmNonRtClientControl.commitWrite();
  795. }
  796. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  797. if (yesNo)
  798. {
  799. pData->tryTransient();
  800. }
  801. else
  802. {
  803. pData->transientTryCounter = 0;
  804. }
  805. #endif
  806. }
  807. void idle() override
  808. {
  809. if (fBridgeThread.isThreadRunning())
  810. {
  811. if (fInitiated && fTimedOut && pData->active)
  812. setActive(false, true, true);
  813. {
  814. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  815. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPing);
  816. fShmNonRtClientControl.commitWrite();
  817. }
  818. try {
  819. handleNonRtData();
  820. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  821. }
  822. else if (fInitiated)
  823. {
  824. fTimedOut = true;
  825. fTimedError = true;
  826. fInitiated = false;
  827. handleProcessStopped();
  828. }
  829. CarlaPlugin::idle();
  830. }
  831. // -------------------------------------------------------------------
  832. // Plugin state
  833. void reload() override
  834. {
  835. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  836. carla_debug("CarlaPluginBridge::reload() - start");
  837. const EngineProcessMode processMode(pData->engine->getProccessMode());
  838. // Safely disable plugin for reload
  839. const ScopedDisabler sd(this);
  840. // cleanup of previous data
  841. pData->audioIn.clear();
  842. pData->audioOut.clear();
  843. pData->cvIn.clear();
  844. pData->cvOut.clear();
  845. pData->event.clear();
  846. bool needsCtrlIn, needsCtrlOut;
  847. needsCtrlIn = needsCtrlOut = false;
  848. if (fInfo.aIns > 0)
  849. {
  850. pData->audioIn.createNew(fInfo.aIns);
  851. }
  852. if (fInfo.aOuts > 0)
  853. {
  854. pData->audioOut.createNew(fInfo.aOuts);
  855. needsCtrlIn = true;
  856. }
  857. if (fInfo.cvIns > 0)
  858. {
  859. pData->cvIn.createNew(fInfo.cvIns);
  860. }
  861. if (fInfo.cvOuts > 0)
  862. {
  863. pData->cvOut.createNew(fInfo.cvOuts);
  864. }
  865. if (fInfo.mIns > 0)
  866. needsCtrlIn = true;
  867. if (fInfo.mOuts > 0)
  868. needsCtrlOut = true;
  869. const uint portNameSize(pData->engine->getMaxPortNameSize());
  870. CarlaString portName;
  871. // Audio Ins
  872. for (uint32_t j=0; j < fInfo.aIns; ++j)
  873. {
  874. portName.clear();
  875. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  876. {
  877. portName = pData->name;
  878. portName += ":";
  879. }
  880. if (fInfo.aInNames != nullptr && fInfo.aInNames[j] != nullptr)
  881. {
  882. portName += fInfo.aInNames[j];
  883. }
  884. else if (fInfo.aIns > 1)
  885. {
  886. portName += "input_";
  887. portName += CarlaString(j+1);
  888. }
  889. else
  890. portName += "input";
  891. portName.truncate(portNameSize);
  892. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  893. pData->audioIn.ports[j].rindex = j;
  894. }
  895. // Audio Outs
  896. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  897. {
  898. portName.clear();
  899. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  900. {
  901. portName = pData->name;
  902. portName += ":";
  903. }
  904. if (fInfo.aOutNames != nullptr && fInfo.aOutNames[j] != nullptr)
  905. {
  906. portName += fInfo.aOutNames[j];
  907. }
  908. else if (fInfo.aOuts > 1)
  909. {
  910. portName += "output_";
  911. portName += CarlaString(j+1);
  912. }
  913. else
  914. portName += "output";
  915. portName.truncate(portNameSize);
  916. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  917. pData->audioOut.ports[j].rindex = j;
  918. }
  919. // TODO - MIDI
  920. // CV Ins
  921. for (uint32_t j=0; j < fInfo.cvIns; ++j)
  922. {
  923. portName.clear();
  924. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  925. {
  926. portName = pData->name;
  927. portName += ":";
  928. }
  929. if (fInfo.cvInNames != nullptr && fInfo.cvInNames[j] != nullptr)
  930. {
  931. portName += fInfo.cvInNames[j];
  932. }
  933. else if (fInfo.cvIns > 1)
  934. {
  935. portName += "cv_input_";
  936. portName += CarlaString(j+1);
  937. }
  938. else
  939. portName += "cv_input";
  940. portName.truncate(portNameSize);
  941. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  942. pData->cvIn.ports[j].rindex = j;
  943. }
  944. // CV Outs
  945. for (uint32_t j=0; j < fInfo.cvOuts; ++j)
  946. {
  947. portName.clear();
  948. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  949. {
  950. portName = pData->name;
  951. portName += ":";
  952. }
  953. if (fInfo.cvOutNames != nullptr && fInfo.cvOutNames[j] != nullptr)
  954. {
  955. portName += fInfo.cvOutNames[j];
  956. }
  957. else if (fInfo.cvOuts > 1)
  958. {
  959. portName += "cv_output_";
  960. portName += CarlaString(j+1);
  961. }
  962. else
  963. portName += "cv_output";
  964. portName.truncate(portNameSize);
  965. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  966. pData->cvOut.ports[j].rindex = j;
  967. }
  968. if (needsCtrlIn)
  969. {
  970. portName.clear();
  971. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  972. {
  973. portName = pData->name;
  974. portName += ":";
  975. }
  976. portName += "event-in";
  977. portName.truncate(portNameSize);
  978. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  979. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  980. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  981. #endif
  982. }
  983. if (needsCtrlOut)
  984. {
  985. portName.clear();
  986. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  987. {
  988. portName = pData->name;
  989. portName += ":";
  990. }
  991. portName += "event-out";
  992. portName.truncate(portNameSize);
  993. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  994. }
  995. // extra plugin hints
  996. pData->extraHints = 0x0;
  997. if (fInfo.mIns > 0)
  998. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  999. if (fInfo.mOuts > 0)
  1000. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  1001. bufferSizeChanged(pData->engine->getBufferSize());
  1002. reloadPrograms(true);
  1003. carla_debug("CarlaPluginBridge::reload() - end");
  1004. }
  1005. // -------------------------------------------------------------------
  1006. // Plugin processing
  1007. void activate() noexcept override
  1008. {
  1009. if (! fBridgeThread.isThreadRunning())
  1010. {
  1011. CARLA_SAFE_ASSERT_RETURN(restartBridgeThread(),);
  1012. }
  1013. {
  1014. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1015. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientActivate);
  1016. fShmNonRtClientControl.commitWrite();
  1017. }
  1018. fTimedOut = false;
  1019. try {
  1020. waitForClient("activate", 2000);
  1021. } CARLA_SAFE_EXCEPTION("activate - waitForClient");
  1022. }
  1023. void deactivate() noexcept override
  1024. {
  1025. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  1026. {
  1027. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1028. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientDeactivate);
  1029. fShmNonRtClientControl.commitWrite();
  1030. }
  1031. fTimedOut = false;
  1032. try {
  1033. waitForClient("deactivate", 2000);
  1034. } CARLA_SAFE_EXCEPTION("deactivate - waitForClient");
  1035. }
  1036. void process(const float* const* const audioIn,
  1037. float** const audioOut,
  1038. const float* const* const cvIn,
  1039. float** const cvOut,
  1040. const uint32_t frames) override
  1041. {
  1042. // --------------------------------------------------------------------------------------------------------
  1043. // Check if active
  1044. if (fTimedOut || fTimedError || ! pData->active)
  1045. {
  1046. // disable any output sound
  1047. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1048. carla_zeroFloats(audioOut[i], frames);
  1049. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1050. carla_zeroFloats(cvOut[i], frames);
  1051. return;
  1052. }
  1053. // --------------------------------------------------------------------------------------------------------
  1054. // Check if needs reset
  1055. if (pData->needsReset)
  1056. {
  1057. // TODO
  1058. pData->needsReset = false;
  1059. }
  1060. // --------------------------------------------------------------------------------------------------------
  1061. // Event Input
  1062. if (pData->event.portIn != nullptr)
  1063. {
  1064. // ----------------------------------------------------------------------------------------------------
  1065. // MIDI Input (External)
  1066. if (pData->extNotes.mutex.tryLock())
  1067. {
  1068. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  1069. {
  1070. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  1071. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1072. uint8_t data1, data2, data3;
  1073. data1 = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1074. data2 = note.note;
  1075. data3 = note.velo;
  1076. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1077. fShmRtClientControl.writeUInt(0); // time
  1078. fShmRtClientControl.writeByte(0); // port
  1079. fShmRtClientControl.writeByte(3); // size
  1080. fShmRtClientControl.writeByte(data1);
  1081. fShmRtClientControl.writeByte(data2);
  1082. fShmRtClientControl.writeByte(data3);
  1083. fShmRtClientControl.commitWrite();
  1084. }
  1085. pData->extNotes.data.clear();
  1086. pData->extNotes.mutex.unlock();
  1087. } // End of MIDI Input (External)
  1088. // ----------------------------------------------------------------------------------------------------
  1089. // Event Input (System)
  1090. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1091. bool allNotesOffSent = false;
  1092. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1093. {
  1094. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0;
  1095. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  1096. }
  1097. #endif
  1098. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1099. {
  1100. EngineEvent& event(pData->event.portIn->getEvent(i));
  1101. // Control change
  1102. switch (event.type)
  1103. {
  1104. case kEngineEventTypeNull:
  1105. break;
  1106. case kEngineEventTypeControl: {
  1107. EngineControlEvent& ctrlEvent = event.ctrl;
  1108. switch (ctrlEvent.type)
  1109. {
  1110. case kEngineControlEventTypeNull:
  1111. break;
  1112. case kEngineControlEventTypeParameter: {
  1113. float value;
  1114. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1115. // non-midi
  1116. if (event.channel == kEngineEventNonMidiChannel)
  1117. {
  1118. const uint32_t k = ctrlEvent.param;
  1119. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1120. ctrlEvent.handled = true;
  1121. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1122. setParameterValueRT(k, value, true);
  1123. continue;
  1124. }
  1125. // Control backend stuff
  1126. if (event.channel == pData->ctrlChannel)
  1127. {
  1128. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1129. {
  1130. ctrlEvent.handled = true;
  1131. value = ctrlEvent.normalizedValue;
  1132. setDryWetRT(value, true);
  1133. }
  1134. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1135. {
  1136. ctrlEvent.handled = true;
  1137. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  1138. setVolumeRT(value, true);
  1139. }
  1140. else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1141. {
  1142. float left, right;
  1143. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  1144. if (value < 0.0f)
  1145. {
  1146. left = -1.0f;
  1147. right = (value*2.0f)+1.0f;
  1148. }
  1149. else if (value > 0.0f)
  1150. {
  1151. left = (value*2.0f)-1.0f;
  1152. right = 1.0f;
  1153. }
  1154. else
  1155. {
  1156. left = -1.0f;
  1157. right = 1.0f;
  1158. }
  1159. ctrlEvent.handled = true;
  1160. setBalanceLeftRT(left, true);
  1161. setBalanceRightRT(right, true);
  1162. }
  1163. }
  1164. #endif
  1165. // Control plugin parameters
  1166. for (uint32_t k=0; k < pData->param.count; ++k)
  1167. {
  1168. if (pData->param.data[k].midiChannel != event.channel)
  1169. continue;
  1170. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1171. continue;
  1172. if (pData->param.data[k].type != PARAMETER_INPUT)
  1173. continue;
  1174. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMATABLE) == 0)
  1175. continue;
  1176. ctrlEvent.handled = true;
  1177. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1178. setParameterValueRT(k, value, true);
  1179. }
  1180. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1181. if (! ctrlEvent.handled)
  1182. checkForMidiLearn(event);
  1183. #endif
  1184. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1185. {
  1186. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1187. fShmRtClientControl.writeUInt(event.time);
  1188. fShmRtClientControl.writeByte(0); // port
  1189. fShmRtClientControl.writeByte(3); // size
  1190. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1191. fShmRtClientControl.writeByte(uint8_t(ctrlEvent.param));
  1192. fShmRtClientControl.writeByte(uint8_t(ctrlEvent.normalizedValue*127.0f));
  1193. }
  1194. break;
  1195. }
  1196. case kEngineControlEventTypeMidiBank:
  1197. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1198. {
  1199. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiBank);
  1200. fShmRtClientControl.writeUInt(event.time);
  1201. fShmRtClientControl.writeByte(event.channel);
  1202. fShmRtClientControl.writeUShort(event.ctrl.param);
  1203. fShmRtClientControl.commitWrite();
  1204. }
  1205. else if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  1206. {
  1207. // VST2's that use banks usually require both a MSB bank message and a LSB bank message. The MSB bank message can just be 0
  1208. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1209. fShmRtClientControl.writeUInt(event.time);
  1210. fShmRtClientControl.writeByte(0); // port
  1211. fShmRtClientControl.writeByte(3); // size
  1212. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1213. fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT);
  1214. fShmRtClientControl.writeByte(0);
  1215. fShmRtClientControl.commitWrite();
  1216. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1217. fShmRtClientControl.writeUInt(event.time);
  1218. fShmRtClientControl.writeByte(0); // port
  1219. fShmRtClientControl.writeByte(3); // size
  1220. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1221. fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT__LSB);
  1222. fShmRtClientControl.writeByte(uint8_t(event.ctrl.param));
  1223. fShmRtClientControl.commitWrite();
  1224. }
  1225. break;
  1226. case kEngineControlEventTypeMidiProgram:
  1227. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES || pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1228. {
  1229. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiProgram);
  1230. fShmRtClientControl.writeUInt(event.time);
  1231. fShmRtClientControl.writeByte(event.channel);
  1232. fShmRtClientControl.writeUShort(event.ctrl.param);
  1233. fShmRtClientControl.commitWrite();
  1234. }
  1235. break;
  1236. case kEngineControlEventTypeAllSoundOff:
  1237. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1238. {
  1239. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllSoundOff);
  1240. fShmRtClientControl.writeUInt(event.time);
  1241. fShmRtClientControl.writeByte(event.channel);
  1242. fShmRtClientControl.commitWrite();
  1243. }
  1244. break;
  1245. case kEngineControlEventTypeAllNotesOff:
  1246. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1247. {
  1248. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1249. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1250. {
  1251. allNotesOffSent = true;
  1252. postponeRtAllNotesOff();
  1253. }
  1254. #endif
  1255. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllNotesOff);
  1256. fShmRtClientControl.writeUInt(event.time);
  1257. fShmRtClientControl.writeByte(event.channel);
  1258. fShmRtClientControl.commitWrite();
  1259. }
  1260. break;
  1261. } // switch (ctrlEvent.type)
  1262. break;
  1263. } // case kEngineEventTypeControl
  1264. case kEngineEventTypeMidi: {
  1265. const EngineMidiEvent& midiEvent(event.midi);
  1266. if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
  1267. continue;
  1268. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  1269. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  1270. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1271. continue;
  1272. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1273. continue;
  1274. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1275. continue;
  1276. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1277. continue;
  1278. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1279. continue;
  1280. // Fix bad note-off
  1281. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  1282. status = MIDI_STATUS_NOTE_OFF;
  1283. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1284. fShmRtClientControl.writeUInt(event.time);
  1285. fShmRtClientControl.writeByte(midiEvent.port);
  1286. fShmRtClientControl.writeByte(midiEvent.size);
  1287. fShmRtClientControl.writeByte(uint8_t(midiData[0] | (event.channel & MIDI_CHANNEL_BIT)));
  1288. for (uint8_t j=1; j < midiEvent.size; ++j)
  1289. fShmRtClientControl.writeByte(midiData[j]);
  1290. fShmRtClientControl.commitWrite();
  1291. if (status == MIDI_STATUS_NOTE_ON)
  1292. {
  1293. pData->postponeNoteOnRtEvent(true, event.channel, midiData[1], midiData[2]);
  1294. }
  1295. else if (status == MIDI_STATUS_NOTE_OFF)
  1296. {
  1297. pData->postponeNoteOffRtEvent(true, event.channel, midiData[1]);
  1298. }
  1299. } break;
  1300. }
  1301. }
  1302. pData->postRtEvents.trySplice();
  1303. } // End of Event Input
  1304. if (! processSingle(audioIn, audioOut, cvIn, cvOut, frames))
  1305. return;
  1306. // --------------------------------------------------------------------------------------------------------
  1307. // Control and MIDI Output
  1308. if (pData->event.portOut != nullptr)
  1309. {
  1310. float value;
  1311. for (uint32_t k=0; k < pData->param.count; ++k)
  1312. {
  1313. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1314. continue;
  1315. if (pData->param.data[k].mappedControlIndex > 0)
  1316. {
  1317. value = pData->param.ranges[k].getNormalizedValue(fParams[k].value);
  1318. pData->event.portOut->writeControlEvent(0,
  1319. pData->param.data[k].midiChannel,
  1320. kEngineControlEventTypeParameter,
  1321. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1322. -1,
  1323. value);
  1324. }
  1325. }
  1326. uint32_t time;
  1327. uint8_t port, size;
  1328. const uint8_t* midiData(fShmRtClientControl.data->midiOut);
  1329. for (std::size_t read=0; read<kBridgeRtClientDataMidiOutSize-kBridgeBaseMidiOutHeaderSize;)
  1330. {
  1331. // get time
  1332. time = *(const uint32_t*)midiData;
  1333. midiData += 4;
  1334. // get port and size
  1335. port = *midiData++;
  1336. size = *midiData++;
  1337. if (size == 0)
  1338. break;
  1339. // store midi data advancing as needed
  1340. uint8_t data[size];
  1341. for (uint8_t j=0; j<size; ++j)
  1342. data[j] = *midiData++;
  1343. pData->event.portOut->writeMidiEvent(time, size, data);
  1344. read += kBridgeBaseMidiOutHeaderSize + size;
  1345. }
  1346. // TODO
  1347. (void)port;
  1348. } // End of Control and MIDI Output
  1349. }
  1350. bool processSingle(const float* const* const audioIn, float** const audioOut,
  1351. const float* const* const cvIn, float** const cvOut, const uint32_t frames)
  1352. {
  1353. CARLA_SAFE_ASSERT_RETURN(! fTimedError, false);
  1354. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1355. CARLA_SAFE_ASSERT_RETURN(frames <= fBufferSize, false);
  1356. if (pData->audioIn.count > 0)
  1357. {
  1358. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1359. }
  1360. if (pData->audioOut.count > 0)
  1361. {
  1362. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1363. }
  1364. if (pData->cvIn.count > 0)
  1365. {
  1366. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1367. }
  1368. if (pData->cvOut.count > 0)
  1369. {
  1370. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1371. }
  1372. // --------------------------------------------------------------------------------------------------------
  1373. // Try lock, silence otherwise
  1374. #ifndef STOAT_TEST_BUILD
  1375. if (pData->engine->isOffline())
  1376. {
  1377. pData->singleMutex.lock();
  1378. }
  1379. else
  1380. #endif
  1381. if (! pData->singleMutex.tryLock())
  1382. {
  1383. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1384. carla_zeroFloats(audioOut[i], frames);
  1385. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1386. carla_zeroFloats(cvOut[i], frames);
  1387. return false;
  1388. }
  1389. // --------------------------------------------------------------------------------------------------------
  1390. // Reset audio buffers
  1391. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1392. carla_copyFloats(fShmAudioPool.data + (i * fBufferSize), audioIn[i], frames);
  1393. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1394. carla_copyFloats(fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + i) * fBufferSize), cvIn[i], frames);
  1395. // --------------------------------------------------------------------------------------------------------
  1396. // TimeInfo
  1397. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1398. BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  1399. bridgeTimeInfo.playing = timeInfo.playing;
  1400. bridgeTimeInfo.frame = timeInfo.frame;
  1401. bridgeTimeInfo.usecs = timeInfo.usecs;
  1402. bridgeTimeInfo.validFlags = timeInfo.bbt.valid ? kPluginBridgeTimeInfoValidBBT : 0x0;
  1403. if (timeInfo.bbt.valid)
  1404. {
  1405. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  1406. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  1407. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  1408. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1409. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  1410. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1411. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1412. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  1413. }
  1414. // --------------------------------------------------------------------------------------------------------
  1415. // Run plugin
  1416. {
  1417. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientProcess);
  1418. fShmRtClientControl.writeUInt(frames);
  1419. fShmRtClientControl.commitWrite();
  1420. }
  1421. waitForClient("process", fProcWaitTime);
  1422. if (fTimedOut)
  1423. {
  1424. pData->singleMutex.unlock();
  1425. return false;
  1426. }
  1427. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1428. carla_copyFloats(audioOut[i], fShmAudioPool.data + ((pData->audioIn.count + i) * fBufferSize), frames);
  1429. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1430. carla_copyFloats(cvOut[i], fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + pData->cvIn.count + i) * fBufferSize), frames);
  1431. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1432. // --------------------------------------------------------------------------------------------------------
  1433. // Post-processing (dry/wet, volume and balance)
  1434. {
  1435. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1436. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1437. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1438. const bool isMono = (pData->audioIn.count == 1);
  1439. bool isPair;
  1440. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1441. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1442. {
  1443. // Dry/Wet
  1444. if (doDryWet)
  1445. {
  1446. const uint32_t c = isMono ? 0 : i;
  1447. for (uint32_t k=0; k < frames; ++k)
  1448. {
  1449. # ifndef BUILD_BRIDGE
  1450. if (k < pData->latency.frames && pData->latency.buffers != nullptr)
  1451. bufValue = pData->latency.buffers[c][k];
  1452. else if (pData->latency.frames < frames)
  1453. bufValue = audioIn[c][k-pData->latency.frames];
  1454. else
  1455. # endif
  1456. bufValue = audioIn[c][k];
  1457. audioOut[i][k] = (audioOut[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1458. }
  1459. }
  1460. // Balance
  1461. if (doBalance)
  1462. {
  1463. isPair = (i % 2 == 0);
  1464. if (isPair)
  1465. {
  1466. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1467. carla_copyFloats(oldBufLeft, audioOut[i], frames);
  1468. }
  1469. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1470. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1471. for (uint32_t k=0; k < frames; ++k)
  1472. {
  1473. if (isPair)
  1474. {
  1475. // left
  1476. audioOut[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1477. audioOut[i][k] += audioOut[i+1][k] * (1.0f - balRangeR);
  1478. }
  1479. else
  1480. {
  1481. // right
  1482. audioOut[i][k] = audioOut[i][k] * balRangeR;
  1483. audioOut[i][k] += oldBufLeft[k] * balRangeL;
  1484. }
  1485. }
  1486. }
  1487. // Volume (and buffer copy)
  1488. if (doVolume)
  1489. {
  1490. for (uint32_t k=0; k < frames; ++k)
  1491. audioOut[i][k] *= pData->postProc.volume;
  1492. }
  1493. }
  1494. } // End of Post-processing
  1495. # ifndef BUILD_BRIDGE
  1496. // --------------------------------------------------------------------------------------------------------
  1497. // Save latency values for next callback
  1498. if (pData->latency.frames != 0 && pData->latency.buffers != nullptr)
  1499. {
  1500. const uint32_t latframes = pData->latency.frames;
  1501. if (latframes <= frames)
  1502. {
  1503. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1504. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  1505. }
  1506. else
  1507. {
  1508. const uint32_t diff = latframes - frames;
  1509. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1510. {
  1511. // push back buffer by 'frames'
  1512. for (k=0; k < diff; ++k)
  1513. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1514. // put current input at the end
  1515. for (uint32_t j=0; k < latframes; ++j, ++k)
  1516. pData->latency.buffers[i][k] = audioIn[i][j];
  1517. }
  1518. }
  1519. }
  1520. # endif
  1521. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1522. // --------------------------------------------------------------------------------------------------------
  1523. pData->singleMutex.unlock();
  1524. return true;
  1525. }
  1526. void bufferSizeChanged(const uint32_t newBufferSize) override
  1527. {
  1528. fBufferSize = newBufferSize;
  1529. resizeAudioPool(newBufferSize);
  1530. {
  1531. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetBufferSize);
  1532. fShmRtClientControl.writeUInt(newBufferSize);
  1533. fShmRtClientControl.commitWrite();
  1534. }
  1535. //fProcWaitTime = newBufferSize*1000/pData->engine->getSampleRate();
  1536. fProcWaitTime = 1000;
  1537. waitForClient("buffersize", 1000);
  1538. }
  1539. void sampleRateChanged(const double newSampleRate) override
  1540. {
  1541. {
  1542. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetSampleRate);
  1543. fShmRtClientControl.writeDouble(newSampleRate);
  1544. fShmRtClientControl.commitWrite();
  1545. }
  1546. //fProcWaitTime = pData->engine->getBufferSize()*1000/newSampleRate;
  1547. fProcWaitTime = 1000;
  1548. waitForClient("samplerate", 1000);
  1549. }
  1550. void offlineModeChanged(const bool isOffline) override
  1551. {
  1552. {
  1553. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetOnline);
  1554. fShmRtClientControl.writeBool(isOffline);
  1555. fShmRtClientControl.commitWrite();
  1556. }
  1557. waitForClient("offline", 1000);
  1558. }
  1559. // -------------------------------------------------------------------
  1560. // Plugin buffers
  1561. void clearBuffers() noexcept override
  1562. {
  1563. if (fParams != nullptr)
  1564. {
  1565. delete[] fParams;
  1566. fParams = nullptr;
  1567. }
  1568. CarlaPlugin::clearBuffers();
  1569. }
  1570. // -------------------------------------------------------------------
  1571. // Post-poned UI Stuff
  1572. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1573. {
  1574. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1575. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1576. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiParameterChange);
  1577. fShmNonRtClientControl.writeUInt(index);
  1578. fShmNonRtClientControl.writeFloat(value);
  1579. fShmNonRtClientControl.commitWrite();
  1580. }
  1581. void uiProgramChange(const uint32_t index) noexcept override
  1582. {
  1583. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  1584. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1585. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiProgramChange);
  1586. fShmNonRtClientControl.writeUInt(index);
  1587. fShmNonRtClientControl.commitWrite();
  1588. }
  1589. void uiMidiProgramChange(const uint32_t index) noexcept override
  1590. {
  1591. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1592. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1593. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiMidiProgramChange);
  1594. fShmNonRtClientControl.writeUInt(index);
  1595. fShmNonRtClientControl.commitWrite();
  1596. }
  1597. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1598. {
  1599. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1600. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1601. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1602. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1603. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOn);
  1604. fShmNonRtClientControl.writeByte(channel);
  1605. fShmNonRtClientControl.writeByte(note);
  1606. fShmNonRtClientControl.writeByte(velo);
  1607. fShmNonRtClientControl.commitWrite();
  1608. }
  1609. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1610. {
  1611. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1612. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1613. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1614. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOff);
  1615. fShmNonRtClientControl.writeByte(channel);
  1616. fShmNonRtClientControl.writeByte(note);
  1617. fShmNonRtClientControl.commitWrite();
  1618. }
  1619. // -------------------------------------------------------------------
  1620. // Internal helper functions
  1621. void restoreLV2State(bool) noexcept override
  1622. {
  1623. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1624. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientRestoreLV2State);
  1625. fShmNonRtClientControl.commitWrite();
  1626. }
  1627. void waitForBridgeSaveSignal() noexcept override
  1628. {
  1629. // VSTs only save chunks, for which we already have a waitForSaved there
  1630. if (fPluginType != PLUGIN_VST2)
  1631. waitForSaved();
  1632. }
  1633. // -------------------------------------------------------------------
  1634. void handleNonRtData()
  1635. {
  1636. for (; fShmNonRtServerControl.isDataAvailableForReading();)
  1637. {
  1638. const PluginBridgeNonRtServerOpcode opcode(fShmNonRtServerControl.readOpcode());
  1639. #ifdef DEBUG
  1640. if (opcode != kPluginBridgeNonRtServerPong && opcode != kPluginBridgeNonRtServerParameterValue2) {
  1641. carla_debug("CarlaPluginBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtServerOpcode2str(opcode));
  1642. }
  1643. #endif
  1644. switch (opcode)
  1645. {
  1646. case kPluginBridgeNonRtServerNull:
  1647. case kPluginBridgeNonRtServerPong:
  1648. break;
  1649. case kPluginBridgeNonRtServerVersion:
  1650. fBridgeVersion = fShmNonRtServerControl.readUInt();
  1651. break;
  1652. case kPluginBridgeNonRtServerPluginInfo1: {
  1653. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  1654. const uint32_t category = fShmNonRtServerControl.readUInt();
  1655. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1656. const uint32_t optionAv = fShmNonRtServerControl.readUInt();
  1657. const uint32_t optionEn = fShmNonRtServerControl.readUInt();
  1658. const int64_t uniqueId = fShmNonRtServerControl.readLong();
  1659. if (fUniqueId != 0) {
  1660. CARLA_SAFE_ASSERT_INT2(fUniqueId == uniqueId, fUniqueId, uniqueId);
  1661. }
  1662. pData->hints = hints | PLUGIN_IS_BRIDGE;
  1663. pData->options = optionEn;
  1664. fInfo.category = static_cast<PluginCategory>(category);
  1665. fInfo.optionsAvailable = optionAv;
  1666. } break;
  1667. case kPluginBridgeNonRtServerPluginInfo2: {
  1668. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  1669. // realName
  1670. const uint32_t realNameSize(fShmNonRtServerControl.readUInt());
  1671. char realName[realNameSize+1];
  1672. carla_zeroChars(realName, realNameSize+1);
  1673. fShmNonRtServerControl.readCustomData(realName, realNameSize);
  1674. // label
  1675. const uint32_t labelSize(fShmNonRtServerControl.readUInt());
  1676. char label[labelSize+1];
  1677. carla_zeroChars(label, labelSize+1);
  1678. fShmNonRtServerControl.readCustomData(label, labelSize);
  1679. // maker
  1680. const uint32_t makerSize(fShmNonRtServerControl.readUInt());
  1681. char maker[makerSize+1];
  1682. carla_zeroChars(maker, makerSize+1);
  1683. fShmNonRtServerControl.readCustomData(maker, makerSize);
  1684. // copyright
  1685. const uint32_t copyrightSize(fShmNonRtServerControl.readUInt());
  1686. char copyright[copyrightSize+1];
  1687. carla_zeroChars(copyright, copyrightSize+1);
  1688. fShmNonRtServerControl.readCustomData(copyright, copyrightSize);
  1689. fInfo.name = realName;
  1690. fInfo.label = label;
  1691. fInfo.maker = maker;
  1692. fInfo.copyright = copyright;
  1693. if (pData->name == nullptr)
  1694. pData->name = pData->engine->getUniquePluginName(realName);
  1695. } break;
  1696. case kPluginBridgeNonRtServerAudioCount: {
  1697. // uint/ins, uint/outs
  1698. fInfo.clear();
  1699. fInfo.aIns = fShmNonRtServerControl.readUInt();
  1700. fInfo.aOuts = fShmNonRtServerControl.readUInt();
  1701. if (fInfo.aIns > 0)
  1702. {
  1703. fInfo.aInNames = new const char*[fInfo.aIns];
  1704. carla_zeroPointers(fInfo.aInNames, fInfo.aIns);
  1705. }
  1706. if (fInfo.aOuts > 0)
  1707. {
  1708. fInfo.aOutNames = new const char*[fInfo.aOuts];
  1709. carla_zeroPointers(fInfo.aOutNames, fInfo.aOuts);
  1710. }
  1711. } break;
  1712. case kPluginBridgeNonRtServerMidiCount: {
  1713. // uint/ins, uint/outs
  1714. fInfo.mIns = fShmNonRtServerControl.readUInt();
  1715. fInfo.mOuts = fShmNonRtServerControl.readUInt();
  1716. } break;
  1717. case kPluginBridgeNonRtServerCvCount: {
  1718. // uint/ins, uint/outs
  1719. fInfo.cvIns = fShmNonRtServerControl.readUInt();
  1720. fInfo.cvOuts = fShmNonRtServerControl.readUInt();
  1721. } break;
  1722. case kPluginBridgeNonRtServerParameterCount: {
  1723. // uint/count
  1724. const uint32_t count = fShmNonRtServerControl.readUInt();
  1725. // delete old data
  1726. pData->param.clear();
  1727. if (fParams != nullptr)
  1728. {
  1729. delete[] fParams;
  1730. fParams = nullptr;
  1731. }
  1732. if (count > 0)
  1733. {
  1734. pData->param.createNew(count, false);
  1735. fParams = new BridgeParamInfo[count];
  1736. // we might not receive all parameter data, so ensure range max is not 0
  1737. for (uint32_t i=0; i<count; ++i)
  1738. {
  1739. pData->param.ranges[i].def = 0.0f;
  1740. pData->param.ranges[i].min = 0.0f;
  1741. pData->param.ranges[i].max = 1.0f;
  1742. pData->param.ranges[i].step = 0.001f;
  1743. pData->param.ranges[i].stepSmall = 0.0001f;
  1744. pData->param.ranges[i].stepLarge = 0.1f;
  1745. }
  1746. }
  1747. } break;
  1748. case kPluginBridgeNonRtServerProgramCount: {
  1749. // uint/count
  1750. pData->prog.clear();
  1751. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1752. pData->prog.createNew(count);
  1753. } break;
  1754. case kPluginBridgeNonRtServerMidiProgramCount: {
  1755. // uint/count
  1756. pData->midiprog.clear();
  1757. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1758. pData->midiprog.createNew(count);
  1759. } break;
  1760. case kPluginBridgeNonRtServerPortName: {
  1761. // byte/type, uint/index, uint/size, str[] (name)
  1762. const uint8_t portType = fShmNonRtServerControl.readByte();
  1763. const uint32_t index = fShmNonRtServerControl.readUInt();
  1764. // name
  1765. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1766. char* const name = new char[nameSize+1];
  1767. carla_zeroChars(name, nameSize+1);
  1768. fShmNonRtServerControl.readCustomData(name, nameSize);
  1769. CARLA_SAFE_ASSERT_BREAK(portType > kPluginBridgePortNull && portType < kPluginBridgePortTypeCount);
  1770. switch (portType)
  1771. {
  1772. case kPluginBridgePortAudioInput:
  1773. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aIns);
  1774. fInfo.aInNames[index] = name;
  1775. break;
  1776. case kPluginBridgePortAudioOutput:
  1777. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aOuts);
  1778. fInfo.aOutNames[index] = name;
  1779. break;
  1780. }
  1781. } break;
  1782. case kPluginBridgeNonRtServerParameterData1: {
  1783. // uint/index, int/rindex, uint/type, uint/hints, int/cc
  1784. const uint32_t index = fShmNonRtServerControl.readUInt();
  1785. const int32_t rindex = fShmNonRtServerControl.readInt();
  1786. const uint32_t type = fShmNonRtServerControl.readUInt();
  1787. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1788. const int16_t ctrl = fShmNonRtServerControl.readShort();
  1789. CARLA_SAFE_ASSERT_BREAK(ctrl >= CONTROL_INDEX_NONE && ctrl <= CONTROL_INDEX_MAX_ALLOWED);
  1790. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1791. if (index < pData->param.count)
  1792. {
  1793. pData->param.data[index].type = static_cast<ParameterType>(type);
  1794. pData->param.data[index].index = static_cast<int32_t>(index);
  1795. pData->param.data[index].rindex = rindex;
  1796. pData->param.data[index].hints = hints;
  1797. pData->param.data[index].mappedControlIndex = ctrl;
  1798. }
  1799. } break;
  1800. case kPluginBridgeNonRtServerParameterData2: {
  1801. // uint/index, uint/size, str[] (name), uint/size, str[] (unit)
  1802. const uint32_t index = fShmNonRtServerControl.readUInt();
  1803. // name
  1804. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1805. char name[nameSize+1];
  1806. carla_zeroChars(name, nameSize+1);
  1807. fShmNonRtServerControl.readCustomData(name, nameSize);
  1808. // symbol
  1809. const uint32_t symbolSize(fShmNonRtServerControl.readUInt());
  1810. char symbol[symbolSize+1];
  1811. carla_zeroChars(symbol, symbolSize+1);
  1812. fShmNonRtServerControl.readCustomData(symbol, symbolSize);
  1813. // unit
  1814. const uint32_t unitSize(fShmNonRtServerControl.readUInt());
  1815. char unit[unitSize+1];
  1816. carla_zeroChars(unit, unitSize+1);
  1817. fShmNonRtServerControl.readCustomData(unit, unitSize);
  1818. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1819. if (index < pData->param.count)
  1820. {
  1821. fParams[index].name = name;
  1822. fParams[index].symbol = symbol;
  1823. fParams[index].unit = unit;
  1824. }
  1825. } break;
  1826. case kPluginBridgeNonRtServerParameterRanges: {
  1827. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  1828. const uint32_t index = fShmNonRtServerControl.readUInt();
  1829. const float def = fShmNonRtServerControl.readFloat();
  1830. const float min = fShmNonRtServerControl.readFloat();
  1831. const float max = fShmNonRtServerControl.readFloat();
  1832. const float step = fShmNonRtServerControl.readFloat();
  1833. const float stepSmall = fShmNonRtServerControl.readFloat();
  1834. const float stepLarge = fShmNonRtServerControl.readFloat();
  1835. CARLA_SAFE_ASSERT_BREAK(min < max);
  1836. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1837. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1838. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1839. if (index < pData->param.count)
  1840. {
  1841. pData->param.ranges[index].def = def;
  1842. pData->param.ranges[index].min = min;
  1843. pData->param.ranges[index].max = max;
  1844. pData->param.ranges[index].step = step;
  1845. pData->param.ranges[index].stepSmall = stepSmall;
  1846. pData->param.ranges[index].stepLarge = stepLarge;
  1847. }
  1848. } break;
  1849. case kPluginBridgeNonRtServerParameterValue: {
  1850. // uint/index, float/value
  1851. const uint32_t index = fShmNonRtServerControl.readUInt();
  1852. const float value = fShmNonRtServerControl.readFloat();
  1853. if (index < pData->param.count)
  1854. {
  1855. const float fixedValue(pData->param.getFixedValue(index, value));
  1856. if (carla_isNotEqual(fParams[index].value, fixedValue))
  1857. {
  1858. fParams[index].value = fixedValue;
  1859. CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
  1860. }
  1861. }
  1862. } break;
  1863. case kPluginBridgeNonRtServerParameterValue2: {
  1864. // uint/index, float/value
  1865. const uint32_t index = fShmNonRtServerControl.readUInt();
  1866. const float value = fShmNonRtServerControl.readFloat();
  1867. if (index < pData->param.count)
  1868. {
  1869. const float fixedValue(pData->param.getFixedValue(index, value));
  1870. fParams[index].value = fixedValue;
  1871. }
  1872. } break;
  1873. case kPluginBridgeNonRtServerParameterTouch: {
  1874. // uint/index, bool/touch
  1875. const uint32_t index = fShmNonRtServerControl.readUInt();
  1876. const bool touch = fShmNonRtServerControl.readBool();
  1877. pData->engine->touchPluginParameter(pData->id, index, touch);
  1878. } break;
  1879. case kPluginBridgeNonRtServerDefaultValue: {
  1880. // uint/index, float/value
  1881. const uint32_t index = fShmNonRtServerControl.readUInt();
  1882. const float value = fShmNonRtServerControl.readFloat();
  1883. if (index < pData->param.count)
  1884. pData->param.ranges[index].def = value;
  1885. } break;
  1886. case kPluginBridgeNonRtServerCurrentProgram: {
  1887. // int/index
  1888. const int32_t index = fShmNonRtServerControl.readInt();
  1889. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1890. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1891. CarlaPlugin::setProgram(index, false, true, true);
  1892. } break;
  1893. case kPluginBridgeNonRtServerCurrentMidiProgram: {
  1894. // int/index
  1895. const int32_t index = fShmNonRtServerControl.readInt();
  1896. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1897. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1898. CarlaPlugin::setMidiProgram(index, false, true, true);
  1899. } break;
  1900. case kPluginBridgeNonRtServerProgramName: {
  1901. // uint/index, uint/size, str[] (name)
  1902. const uint32_t index = fShmNonRtServerControl.readUInt();
  1903. // name
  1904. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1905. char name[nameSize+1];
  1906. carla_zeroChars(name, nameSize+1);
  1907. fShmNonRtServerControl.readCustomData(name, nameSize);
  1908. CARLA_SAFE_ASSERT_INT2(index < pData->prog.count, index, pData->prog.count);
  1909. if (index < pData->prog.count)
  1910. {
  1911. if (pData->prog.names[index] != nullptr)
  1912. delete[] pData->prog.names[index];
  1913. pData->prog.names[index] = carla_strdup(name);
  1914. }
  1915. } break;
  1916. case kPluginBridgeNonRtServerMidiProgramData: {
  1917. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  1918. const uint32_t index = fShmNonRtServerControl.readUInt();
  1919. const uint32_t bank = fShmNonRtServerControl.readUInt();
  1920. const uint32_t program = fShmNonRtServerControl.readUInt();
  1921. // name
  1922. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1923. char name[nameSize+1];
  1924. carla_zeroChars(name, nameSize+1);
  1925. fShmNonRtServerControl.readCustomData(name, nameSize);
  1926. CARLA_SAFE_ASSERT_INT2(index < pData->midiprog.count, index, pData->midiprog.count);
  1927. if (index < pData->midiprog.count)
  1928. {
  1929. if (pData->midiprog.data[index].name != nullptr)
  1930. delete[] pData->midiprog.data[index].name;
  1931. pData->midiprog.data[index].bank = bank;
  1932. pData->midiprog.data[index].program = program;
  1933. pData->midiprog.data[index].name = carla_strdup(name);
  1934. }
  1935. } break;
  1936. case kPluginBridgeNonRtServerSetCustomData: {
  1937. // uint/size, str[], uint/size, str[], uint/size, str[]
  1938. // type
  1939. const uint32_t typeSize(fShmNonRtServerControl.readUInt());
  1940. char type[typeSize+1];
  1941. carla_zeroChars(type, typeSize+1);
  1942. fShmNonRtServerControl.readCustomData(type, typeSize);
  1943. // key
  1944. const uint32_t keySize(fShmNonRtServerControl.readUInt());
  1945. char key[keySize+1];
  1946. carla_zeroChars(key, keySize+1);
  1947. fShmNonRtServerControl.readCustomData(key, keySize);
  1948. // value
  1949. const uint32_t valueSize(fShmNonRtServerControl.readUInt());
  1950. char value[valueSize+1];
  1951. carla_zeroChars(value, valueSize+1);
  1952. if (valueSize > 0)
  1953. fShmNonRtServerControl.readCustomData(value, valueSize);
  1954. CarlaPlugin::setCustomData(type, key, value, false);
  1955. } break;
  1956. case kPluginBridgeNonRtServerSetChunkDataFile: {
  1957. // uint/size, str[] (filename)
  1958. // chunkFilePath
  1959. const uint32_t chunkFilePathSize(fShmNonRtServerControl.readUInt());
  1960. char chunkFilePath[chunkFilePathSize+1];
  1961. carla_zeroChars(chunkFilePath, chunkFilePathSize+1);
  1962. fShmNonRtServerControl.readCustomData(chunkFilePath, chunkFilePathSize);
  1963. String realChunkFilePath(chunkFilePath);
  1964. #ifndef CARLA_OS_WIN
  1965. // Using Wine, fix temp dir
  1966. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1967. {
  1968. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1969. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1970. realChunkFilePath = fWinePrefix;
  1971. realChunkFilePath += "/drive_";
  1972. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1973. realChunkFilePath += driveLetterSplit[1];
  1974. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1975. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1976. }
  1977. #endif
  1978. File chunkFile(realChunkFilePath);
  1979. CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
  1980. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1981. chunkFile.deleteFile();
  1982. } break;
  1983. case kPluginBridgeNonRtServerSetLatency:
  1984. // uint
  1985. fLatency = fShmNonRtServerControl.readUInt();
  1986. #ifndef BUILD_BRIDGE
  1987. if (! fInitiated)
  1988. pData->latency.recreateBuffers(std::max(fInfo.aIns, fInfo.aOuts), fLatency);
  1989. #endif
  1990. break;
  1991. case kPluginBridgeNonRtServerSetParameterText: {
  1992. const int32_t index = fShmNonRtServerControl.readInt();
  1993. const uint32_t textSize(fShmNonRtServerControl.readUInt());
  1994. char text[textSize+1];
  1995. carla_zeroChars(text, textSize+1);
  1996. fShmNonRtServerControl.readCustomData(text, textSize);
  1997. fReceivingParamText.setReceivedData(index, text, textSize);
  1998. } break;
  1999. case kPluginBridgeNonRtServerReady:
  2000. fInitiated = true;
  2001. break;
  2002. case kPluginBridgeNonRtServerSaved:
  2003. fSaved = true;
  2004. break;
  2005. case kPluginBridgeNonRtServerUiClosed:
  2006. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2007. pData->transientTryCounter = 0;
  2008. #endif
  2009. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  2010. break;
  2011. case kPluginBridgeNonRtServerError: {
  2012. // error
  2013. const uint32_t errorSize(fShmNonRtServerControl.readUInt());
  2014. char error[errorSize+1];
  2015. carla_zeroChars(error, errorSize+1);
  2016. fShmNonRtServerControl.readCustomData(error, errorSize);
  2017. if (fInitiated)
  2018. {
  2019. pData->engine->callback(true, true, ENGINE_CALLBACK_ERROR, pData->id, 0, 0, 0, 0.0f, error);
  2020. // just in case
  2021. pData->engine->setLastError(error);
  2022. fInitError = true;
  2023. }
  2024. else
  2025. {
  2026. pData->engine->setLastError(error);
  2027. fInitError = true;
  2028. fInitiated = true;
  2029. }
  2030. } break;
  2031. }
  2032. }
  2033. }
  2034. // -------------------------------------------------------------------
  2035. uintptr_t getUiBridgeProcessId() const noexcept override
  2036. {
  2037. return fBridgeThread.getProcessPID();
  2038. }
  2039. const void* getExtraStuff() const noexcept override
  2040. {
  2041. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  2042. }
  2043. // -------------------------------------------------------------------
  2044. bool init(CarlaPluginPtr plugin,
  2045. const char* const filename,
  2046. const char* const name,
  2047. const char* const label,
  2048. const int64_t uniqueId,
  2049. const uint options,
  2050. const char* const binaryArchName,
  2051. const char* const bridgeBinary)
  2052. {
  2053. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2054. // ---------------------------------------------------------------
  2055. // first checks
  2056. if (pData->client != nullptr)
  2057. {
  2058. pData->engine->setLastError("Plugin client is already registered");
  2059. return false;
  2060. }
  2061. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2062. {
  2063. pData->engine->setLastError("null bridge binary");
  2064. return false;
  2065. }
  2066. // ---------------------------------------------------------------
  2067. // set info
  2068. if (name != nullptr && name[0] != '\0')
  2069. pData->name = pData->engine->getUniquePluginName(name);
  2070. if (filename != nullptr && filename[0] != '\0')
  2071. pData->filename = carla_strdup(filename);
  2072. else
  2073. pData->filename = carla_strdup("");
  2074. fUniqueId = uniqueId;
  2075. fBridgeBinary = bridgeBinary;
  2076. std::srand(static_cast<uint>(std::time(nullptr)));
  2077. // ---------------------------------------------------------------
  2078. // init sem/shm
  2079. if (! fShmAudioPool.initializeServer())
  2080. {
  2081. carla_stderr("Failed to initialize shared memory audio pool");
  2082. return false;
  2083. }
  2084. if (! fShmRtClientControl.initializeServer())
  2085. {
  2086. carla_stderr("Failed to initialize RT client control");
  2087. fShmAudioPool.clear();
  2088. return false;
  2089. }
  2090. if (! fShmNonRtClientControl.initializeServer())
  2091. {
  2092. carla_stderr("Failed to initialize Non-RT client control");
  2093. fShmRtClientControl.clear();
  2094. fShmAudioPool.clear();
  2095. return false;
  2096. }
  2097. if (! fShmNonRtServerControl.initializeServer())
  2098. {
  2099. carla_stderr("Failed to initialize Non-RT server control");
  2100. fShmNonRtClientControl.clear();
  2101. fShmRtClientControl.clear();
  2102. fShmAudioPool.clear();
  2103. return false;
  2104. }
  2105. #ifndef CARLA_OS_WIN
  2106. // ---------------------------------------------------------------
  2107. // set wine prefix
  2108. if (fBridgeBinary.contains(".exe", true))
  2109. {
  2110. const EngineOptions& engineOptions(pData->engine->getOptions());
  2111. if (engineOptions.wine.autoPrefix)
  2112. fWinePrefix = findWinePrefix(pData->filename);
  2113. if (fWinePrefix.isEmpty())
  2114. {
  2115. const char* const envWinePrefix(std::getenv("WINEPREFIX"));
  2116. if (envWinePrefix != nullptr && envWinePrefix[0] != '\0')
  2117. fWinePrefix = envWinePrefix;
  2118. else if (engineOptions.wine.fallbackPrefix != nullptr && engineOptions.wine.fallbackPrefix[0] != '\0')
  2119. fWinePrefix = engineOptions.wine.fallbackPrefix;
  2120. else
  2121. fWinePrefix = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  2122. }
  2123. }
  2124. #endif
  2125. // ---------------------------------------------------------------
  2126. // init bridge thread
  2127. {
  2128. char shmIdsStr[6*4+1];
  2129. carla_zeroChars(shmIdsStr, 6*4+1);
  2130. std::strncpy(shmIdsStr+6*0, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  2131. std::strncpy(shmIdsStr+6*1, &fShmRtClientControl.filename[fShmRtClientControl.filename.length()-6], 6);
  2132. std::strncpy(shmIdsStr+6*2, &fShmNonRtClientControl.filename[fShmNonRtClientControl.filename.length()-6], 6);
  2133. std::strncpy(shmIdsStr+6*3, &fShmNonRtServerControl.filename[fShmNonRtServerControl.filename.length()-6], 6);
  2134. fBridgeThread.setData(
  2135. #ifndef CARLA_OS_WIN
  2136. fWinePrefix.toRawUTF8(),
  2137. #endif
  2138. binaryArchName, bridgeBinary, label, shmIdsStr);
  2139. }
  2140. if (! restartBridgeThread())
  2141. return false;
  2142. // ---------------------------------------------------------------
  2143. // register client
  2144. if (pData->name == nullptr)
  2145. {
  2146. if (label != nullptr && label[0] != '\0')
  2147. pData->name = pData->engine->getUniquePluginName(label);
  2148. else
  2149. pData->name = pData->engine->getUniquePluginName("unknown");
  2150. }
  2151. pData->client = pData->engine->addClient(plugin);
  2152. if (pData->client == nullptr || ! pData->client->isOk())
  2153. {
  2154. pData->engine->setLastError("Failed to register plugin client");
  2155. return false;
  2156. }
  2157. // ---------------------------------------------------------------
  2158. // set options
  2159. pData->options = 0x0;
  2160. if ((fInfo.optionsAvailable & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0)
  2161. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2162. else if (isPluginOptionEnabled(options, PLUGIN_OPTION_FIXED_BUFFERS))
  2163. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2164. if (pData->engine->getOptions().forceStereo)
  2165. {
  2166. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2167. }
  2168. else if (fInfo.optionsAvailable & PLUGIN_OPTION_FORCE_STEREO)
  2169. {
  2170. if (options & PLUGIN_OPTION_FORCE_STEREO)
  2171. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2172. }
  2173. if (fInfo.optionsAvailable & PLUGIN_OPTION_USE_CHUNKS)
  2174. if (isPluginOptionEnabled(options, PLUGIN_OPTION_USE_CHUNKS))
  2175. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  2176. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  2177. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2178. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2179. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  2180. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2181. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2182. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  2183. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2184. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2185. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PITCHBEND)
  2186. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2187. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2188. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2189. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2190. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2191. if (fInfo.optionsAvailable & PLUGIN_OPTION_SKIP_SENDING_NOTES)
  2192. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  2193. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  2194. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2195. {
  2196. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  2197. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2198. }
  2199. else if (fInfo.optionsAvailable & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2200. {
  2201. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2202. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2203. }
  2204. // kPluginBridgeNonRtClientSetOptions was added in API 7
  2205. if (fBridgeVersion >= 7)
  2206. {
  2207. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2208. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOptions);
  2209. fShmNonRtClientControl.writeUInt(pData->options);
  2210. fShmNonRtClientControl.commitWrite();
  2211. }
  2212. return true;
  2213. }
  2214. private:
  2215. const BinaryType fBinaryType;
  2216. const PluginType fPluginType;
  2217. uint fBridgeVersion;
  2218. bool fInitiated;
  2219. bool fInitError;
  2220. bool fSaved;
  2221. bool fTimedOut;
  2222. bool fTimedError;
  2223. uint fBufferSize;
  2224. uint fProcWaitTime;
  2225. CarlaString fBridgeBinary;
  2226. CarlaPluginBridgeThread fBridgeThread;
  2227. BridgeAudioPool fShmAudioPool;
  2228. BridgeRtClientControl fShmRtClientControl;
  2229. BridgeNonRtClientControl fShmNonRtClientControl;
  2230. BridgeNonRtServerControl fShmNonRtServerControl;
  2231. #ifndef CARLA_OS_WIN
  2232. String fWinePrefix;
  2233. #endif
  2234. class ReceivingParamText {
  2235. public:
  2236. ReceivingParamText() noexcept
  2237. : dataRecv(false),
  2238. dataOk(false),
  2239. index(-1),
  2240. strBuf(nullptr),
  2241. mutex() {}
  2242. bool isCurrentlyWaitingData() const noexcept
  2243. {
  2244. return index >= 0;
  2245. }
  2246. bool wasDataReceived(bool* const success) const noexcept
  2247. {
  2248. *success = dataOk;
  2249. return dataRecv;
  2250. }
  2251. void setTargetData(const int32_t i, char* const b) noexcept
  2252. {
  2253. const CarlaMutexLocker cml(mutex);
  2254. dataOk = false;
  2255. dataRecv = false;
  2256. index = i;
  2257. strBuf = b;
  2258. }
  2259. void setReceivedData(const int32_t i, const char* const b, const uint blen) noexcept
  2260. {
  2261. CarlaScopedValueSetter<bool> svs(dataRecv, false, true);
  2262. const CarlaMutexLocker cml(mutex);
  2263. // make backup and reset data
  2264. const int32_t indexCopy = index;
  2265. char* const strBufCopy = strBuf;
  2266. index = -1;
  2267. strBuf = nullptr;
  2268. CARLA_SAFE_ASSERT_RETURN(indexCopy == i,);
  2269. CARLA_SAFE_ASSERT_RETURN(strBufCopy != nullptr,);
  2270. std::strncpy(strBufCopy, b, std::min(blen, STR_MAX-1U));
  2271. dataOk = true;
  2272. }
  2273. private:
  2274. bool dataRecv;
  2275. bool dataOk;
  2276. int32_t index;
  2277. char* strBuf;
  2278. CarlaMutex mutex;
  2279. CARLA_DECLARE_NON_COPY_CLASS(ReceivingParamText)
  2280. } fReceivingParamText;
  2281. struct Info {
  2282. uint32_t aIns, aOuts;
  2283. uint32_t cvIns, cvOuts;
  2284. uint32_t mIns, mOuts;
  2285. PluginCategory category;
  2286. uint optionsAvailable;
  2287. CarlaString name;
  2288. CarlaString label;
  2289. CarlaString maker;
  2290. CarlaString copyright;
  2291. const char** aInNames;
  2292. const char** aOutNames;
  2293. const char** cvInNames;
  2294. const char** cvOutNames;
  2295. std::vector<uint8_t> chunk;
  2296. Info()
  2297. : aIns(0),
  2298. aOuts(0),
  2299. cvIns(0),
  2300. cvOuts(0),
  2301. mIns(0),
  2302. mOuts(0),
  2303. category(PLUGIN_CATEGORY_NONE),
  2304. optionsAvailable(0),
  2305. name(),
  2306. label(),
  2307. maker(),
  2308. copyright(),
  2309. aInNames(nullptr),
  2310. aOutNames(nullptr),
  2311. cvInNames(nullptr),
  2312. cvOutNames(nullptr),
  2313. chunk() {}
  2314. ~Info()
  2315. {
  2316. clear();
  2317. }
  2318. void clear()
  2319. {
  2320. if (aInNames != nullptr)
  2321. {
  2322. CARLA_SAFE_ASSERT_INT(aIns > 0, aIns);
  2323. for (uint32_t i=0; i<aIns; ++i)
  2324. delete[] aInNames[i];
  2325. delete[] aInNames;
  2326. aInNames = nullptr;
  2327. }
  2328. if (aOutNames != nullptr)
  2329. {
  2330. CARLA_SAFE_ASSERT_INT(aOuts > 0, aOuts);
  2331. for (uint32_t i=0; i<aOuts; ++i)
  2332. delete[] aOutNames[i];
  2333. delete[] aOutNames;
  2334. aOutNames = nullptr;
  2335. }
  2336. if (cvInNames != nullptr)
  2337. {
  2338. CARLA_SAFE_ASSERT_INT(cvIns > 0, cvIns);
  2339. for (uint32_t i=0; i<cvIns; ++i)
  2340. delete[] cvInNames[i];
  2341. delete[] cvInNames;
  2342. cvInNames = nullptr;
  2343. }
  2344. if (cvOutNames != nullptr)
  2345. {
  2346. CARLA_SAFE_ASSERT_INT(cvOuts > 0, cvOuts);
  2347. for (uint32_t i=0; i<cvOuts; ++i)
  2348. delete[] cvOutNames[i];
  2349. delete[] cvOutNames;
  2350. cvOutNames = nullptr;
  2351. }
  2352. aIns = aOuts = cvIns = cvOuts = 0;
  2353. }
  2354. CARLA_DECLARE_NON_COPY_STRUCT(Info)
  2355. } fInfo;
  2356. int64_t fUniqueId;
  2357. uint32_t fLatency;
  2358. BridgeParamInfo* fParams;
  2359. void handleProcessStopped() noexcept
  2360. {
  2361. const bool wasActive = pData->active;
  2362. pData->active = false;
  2363. if (wasActive)
  2364. {
  2365. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2366. pData->engine->callback(true, true,
  2367. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  2368. pData->id,
  2369. PARAMETER_ACTIVE,
  2370. 0, 0,
  2371. 0.0f,
  2372. nullptr);
  2373. #endif
  2374. }
  2375. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  2376. {
  2377. pData->engine->callback(true, true,
  2378. ENGINE_CALLBACK_UI_STATE_CHANGED,
  2379. pData->id,
  2380. 0,
  2381. 0, 0, 0.0f, nullptr);
  2382. }
  2383. }
  2384. void resizeAudioPool(const uint32_t bufferSize)
  2385. {
  2386. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts, fInfo.cvIns+fInfo.cvOuts);
  2387. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  2388. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  2389. fShmRtClientControl.commitWrite();
  2390. waitForClient("resize-pool", 5000);
  2391. }
  2392. void waitForClient(const char* const action, const uint msecs)
  2393. {
  2394. CARLA_SAFE_ASSERT_RETURN(! fTimedOut,);
  2395. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  2396. if (fShmRtClientControl.waitForClient(msecs))
  2397. return;
  2398. fTimedOut = true;
  2399. carla_stderr2("waitForClient(%s) timed out", action);
  2400. }
  2401. bool restartBridgeThread()
  2402. {
  2403. fInitiated = false;
  2404. fInitError = false;
  2405. fTimedError = false;
  2406. // reset memory
  2407. fShmRtClientControl.data->procFlags = 0;
  2408. carla_zeroStruct(fShmRtClientControl.data->timeInfo);
  2409. carla_zeroBytes(fShmRtClientControl.data->midiOut, kBridgeRtClientDataMidiOutSize);
  2410. fShmRtClientControl.clearData();
  2411. fShmNonRtClientControl.clearData();
  2412. fShmNonRtServerControl.clearData();
  2413. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientVersion);
  2414. fShmNonRtClientControl.writeUInt(CARLA_PLUGIN_BRIDGE_API_VERSION_CURRENT);
  2415. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeRtClientData)));
  2416. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtClientData)));
  2417. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtServerData)));
  2418. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientInitialSetup);
  2419. fShmNonRtClientControl.writeUInt(pData->engine->getBufferSize());
  2420. fShmNonRtClientControl.writeDouble(pData->engine->getSampleRate());
  2421. fShmNonRtClientControl.commitWrite();
  2422. if (fShmAudioPool.dataSize != 0)
  2423. {
  2424. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  2425. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  2426. fShmRtClientControl.commitWrite();
  2427. }
  2428. else
  2429. {
  2430. // testing dummy message
  2431. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientNull);
  2432. fShmRtClientControl.commitWrite();
  2433. }
  2434. fBridgeThread.startThread();
  2435. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  2436. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2437. const bool needsCancelableAction = ! pData->engine->isLoadingProject();
  2438. if (needsCancelableAction)
  2439. {
  2440. pData->engine->setActionCanceled(false);
  2441. pData->engine->callback(true, true,
  2442. ENGINE_CALLBACK_CANCELABLE_ACTION,
  2443. pData->id,
  2444. 1,
  2445. 0, 0, 0.0f,
  2446. "Loading plugin bridge");
  2447. }
  2448. #endif
  2449. for (;fBridgeThread.isThreadRunning();)
  2450. {
  2451. pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2452. if (needsEngineIdle)
  2453. pData->engine->idle();
  2454. idle();
  2455. if (fInitiated)
  2456. break;
  2457. if (pData->engine->isAboutToClose() || pData->engine->wasActionCanceled())
  2458. break;
  2459. carla_msleep(5);
  2460. }
  2461. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2462. if (needsCancelableAction)
  2463. {
  2464. pData->engine->callback(true, true,
  2465. ENGINE_CALLBACK_CANCELABLE_ACTION,
  2466. pData->id,
  2467. 0,
  2468. 0, 0, 0.0f,
  2469. "Loading JACK application");
  2470. }
  2471. #endif
  2472. if (fInitError || ! fInitiated)
  2473. {
  2474. fBridgeThread.stopThread(6000);
  2475. if (! fInitError)
  2476. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n"
  2477. "(or the plugin crashed on initialization?)");
  2478. return false;
  2479. }
  2480. if (const size_t dataSize = fInfo.chunk.size())
  2481. {
  2482. #ifdef CARLA_PROPER_CPP11_SUPPORT
  2483. void* data = fInfo.chunk.data();
  2484. #else
  2485. void* data = &fInfo.chunk.front();
  2486. #endif
  2487. CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
  2488. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0, true);
  2489. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  2490. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  2491. filePath += fShmAudioPool.getFilenameSuffix();
  2492. if (File(filePath).replaceWithText(dataBase64.buffer()))
  2493. {
  2494. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  2495. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2496. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
  2497. fShmNonRtClientControl.writeUInt(ulength);
  2498. fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
  2499. fShmNonRtClientControl.commitWrite();
  2500. }
  2501. }
  2502. return true;
  2503. }
  2504. void _setUiTitleFromName()
  2505. {
  2506. CarlaString uiName(pData->name);
  2507. uiName += " (GUI)";
  2508. const uint32_t size = static_cast<uint32_t>(uiName.length());
  2509. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2510. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetWindowTitle);
  2511. fShmNonRtClientControl.writeUInt(size);
  2512. fShmNonRtClientControl.writeCustomData(uiName.buffer(), size);
  2513. fShmNonRtClientControl.commitWrite();
  2514. }
  2515. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridge)
  2516. };
  2517. CARLA_BACKEND_END_NAMESPACE
  2518. // ---------------------------------------------------------------------------------------------------------------------
  2519. CARLA_BACKEND_START_NAMESPACE
  2520. CarlaPluginPtr CarlaPlugin::newBridge(const Initializer& init,
  2521. const BinaryType btype,
  2522. const PluginType ptype,
  2523. const char* const binaryArchName,
  2524. const char* bridgeBinary)
  2525. {
  2526. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\", \"%s\")",
  2527. init.engine, init.filename, init.name, init.label,
  2528. BinaryType2Str(btype), PluginType2Str(ptype), binaryArchName, bridgeBinary);
  2529. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2530. {
  2531. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  2532. return nullptr;
  2533. }
  2534. #ifndef CARLA_OS_WIN
  2535. // FIXME: somewhere, somehow, we end up with double slashes, wine doesn't like that.
  2536. if (std::strncmp(bridgeBinary, "//", 2) == 0)
  2537. ++bridgeBinary;
  2538. #endif
  2539. std::shared_ptr<CarlaPluginBridge> plugin(new CarlaPluginBridge(init.engine, init.id, btype, ptype));
  2540. if (! plugin->init(plugin, init.filename, init.name, init.label, init.uniqueId, init.options, binaryArchName, bridgeBinary))
  2541. return nullptr;
  2542. return plugin;
  2543. }
  2544. CARLA_BACKEND_END_NAMESPACE
  2545. // ---------------------------------------------------------------------------------------------------------------------
  2546. #ifndef BUILD_BRIDGE
  2547. # include "CarlaBridgeUtils.cpp"
  2548. #endif
  2549. // ---------------------------------------------------------------------------------------------------------------------