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

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