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.

CarlaEngineBridge.cpp 61KB

10 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
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
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef BUILD_BRIDGE
  18. # error This file should not be compiled if not building bridge
  19. #endif
  20. #include "CarlaEngineClient.hpp"
  21. #include "CarlaEngineInit.hpp"
  22. #include "CarlaPlugin.hpp"
  23. #include "CarlaBackendUtils.hpp"
  24. #include "CarlaBase64Utils.hpp"
  25. #include "CarlaBridgeUtils.hpp"
  26. #include "CarlaMIDI.h"
  27. #ifdef __SSE2_MATH__
  28. # include <xmmintrin.h>
  29. #endif
  30. #include "water/files/File.h"
  31. #include "water/misc/Time.h"
  32. // must be last
  33. #include "jackbridge/JackBridge.hpp"
  34. using water::File;
  35. using water::MemoryBlock;
  36. using water::String;
  37. using water::Time;
  38. CARLA_BACKEND_START_NAMESPACE
  39. // -----------------------------------------------------------------------
  40. // just want to access private options...
  41. struct CarlaPlugin::ProtectedData {
  42. CarlaEngine* const engine;
  43. CarlaEngineClient* client;
  44. uint id, hints, options;
  45. // ...etc
  46. };
  47. // -----------------------------------------------------------------------
  48. // Bridge Engine client
  49. struct LatencyChangedCallback {
  50. virtual ~LatencyChangedCallback() noexcept {}
  51. virtual void latencyChanged(const uint32_t samples) noexcept = 0;
  52. };
  53. class CarlaEngineBridgeClient : public CarlaEngineClientForSubclassing
  54. {
  55. public:
  56. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  57. CarlaEngineBridgeClient(const CarlaEngine& engine, EngineInternalGraph& egraph, CarlaPlugin* const plugin, LatencyChangedCallback* const cb)
  58. : CarlaEngineClientForSubclassing(engine, egraph, plugin),
  59. fLatencyCallback(cb) {}
  60. #else
  61. CarlaEngineBridgeClient(const CarlaEngine& engine, LatencyChangedCallback* const cb)
  62. : CarlaEngineClientForSubclassing(engine),
  63. fLatencyCallback(cb) {}
  64. #endif
  65. protected:
  66. void setLatency(const uint32_t samples) noexcept override
  67. {
  68. if (getLatency() == samples)
  69. return;
  70. fLatencyCallback->latencyChanged(samples);
  71. CarlaEngineClient::setLatency(samples);
  72. }
  73. private:
  74. LatencyChangedCallback* const fLatencyCallback;
  75. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineBridgeClient)
  76. };
  77. // -------------------------------------------------------------------
  78. class CarlaEngineBridge : public CarlaEngine,
  79. private CarlaThread,
  80. private LatencyChangedCallback
  81. {
  82. public:
  83. CarlaEngineBridge(const char* const audioPoolBaseName, const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  84. : CarlaEngine(),
  85. CarlaThread("CarlaEngineBridge"),
  86. fShmAudioPool(),
  87. fShmRtClientControl(),
  88. fShmNonRtClientControl(),
  89. fShmNonRtServerControl(),
  90. fBaseNameAudioPool(audioPoolBaseName),
  91. fBaseNameRtClientControl(rtClientBaseName),
  92. fBaseNameNonRtClientControl(nonRtClientBaseName),
  93. fBaseNameNonRtServerControl(nonRtServerBaseName),
  94. fClosingDown(false),
  95. fIsOffline(false),
  96. fFirstIdle(true),
  97. fLastPingTime(-1)
  98. {
  99. carla_debug("CarlaEngineBridge::CarlaEngineBridge(\"%s\", \"%s\", \"%s\", \"%s\")", audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  100. }
  101. ~CarlaEngineBridge() noexcept override
  102. {
  103. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  104. clear();
  105. }
  106. // -------------------------------------
  107. // CarlaEngine virtual calls
  108. bool init(const char* const clientName) override
  109. {
  110. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  111. if (! pData->init(clientName))
  112. {
  113. setLastError("Failed to init internal data");
  114. return false;
  115. }
  116. if (! fShmAudioPool.attachClient(fBaseNameAudioPool))
  117. {
  118. carla_stderr("Failed to attach to audio pool shared memory");
  119. return false;
  120. }
  121. if (! fShmRtClientControl.attachClient(fBaseNameRtClientControl))
  122. {
  123. clear();
  124. carla_stderr("Failed to attach to rt client control shared memory");
  125. return false;
  126. }
  127. if (! fShmRtClientControl.mapData())
  128. {
  129. clear();
  130. carla_stderr("Failed to map rt client control shared memory");
  131. return false;
  132. }
  133. if (! fShmNonRtClientControl.attachClient(fBaseNameNonRtClientControl))
  134. {
  135. clear();
  136. carla_stderr("Failed to attach to non-rt client control shared memory");
  137. return false;
  138. }
  139. if (! fShmNonRtClientControl.mapData())
  140. {
  141. clear();
  142. carla_stderr("Failed to map non-rt control client shared memory");
  143. return false;
  144. }
  145. if (! fShmNonRtServerControl.attachClient(fBaseNameNonRtServerControl))
  146. {
  147. clear();
  148. carla_stderr("Failed to attach to non-rt server control shared memory");
  149. return false;
  150. }
  151. if (! fShmNonRtServerControl.mapData())
  152. {
  153. clear();
  154. carla_stderr("Failed to map non-rt control server shared memory");
  155. return false;
  156. }
  157. PluginBridgeNonRtClientOpcode opcode;
  158. opcode = fShmNonRtClientControl.readOpcode();
  159. CARLA_SAFE_ASSERT_RETURN(opcode == kPluginBridgeNonRtClientVersion, false);
  160. const uint32_t apiVersion = fShmNonRtClientControl.readUInt();
  161. CARLA_SAFE_ASSERT_RETURN(apiVersion >= CARLA_PLUGIN_BRIDGE_API_VERSION_MINIMUM, false);
  162. const uint32_t shmRtClientDataSize = fShmNonRtClientControl.readUInt();
  163. CARLA_SAFE_ASSERT_INT2(shmRtClientDataSize == sizeof(BridgeRtClientData), shmRtClientDataSize, sizeof(BridgeRtClientData));
  164. const uint32_t shmNonRtClientDataSize = fShmNonRtClientControl.readUInt();
  165. CARLA_SAFE_ASSERT_INT2(shmNonRtClientDataSize == sizeof(BridgeNonRtClientData), shmNonRtClientDataSize, sizeof(BridgeNonRtClientData));
  166. const uint32_t shmNonRtServerDataSize = fShmNonRtClientControl.readUInt();
  167. CARLA_SAFE_ASSERT_INT2(shmNonRtServerDataSize == sizeof(BridgeNonRtServerData), shmNonRtServerDataSize, sizeof(BridgeNonRtServerData));
  168. if (shmRtClientDataSize != sizeof(BridgeRtClientData) ||
  169. shmNonRtClientDataSize != sizeof(BridgeNonRtClientData) ||
  170. shmNonRtServerDataSize != sizeof(BridgeNonRtServerData))
  171. {
  172. carla_stderr2("CarlaEngineBridge: data size mismatch");
  173. return false;
  174. }
  175. opcode = fShmNonRtClientControl.readOpcode();
  176. CARLA_SAFE_ASSERT_RETURN(opcode == kPluginBridgeNonRtClientInitialSetup, false);
  177. pData->bufferSize = fShmNonRtClientControl.readUInt();
  178. pData->sampleRate = fShmNonRtClientControl.readDouble();
  179. if (pData->bufferSize == 0 || carla_isZero(pData->sampleRate))
  180. {
  181. carla_stderr2("CarlaEngineBridge: invalid empty state");
  182. return false;
  183. }
  184. pData->initTime(nullptr);
  185. // tell backend we're live
  186. {
  187. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  188. // kPluginBridgeNonRtServerVersion was added in API 7
  189. if (apiVersion >= 7)
  190. {
  191. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerVersion);
  192. fShmNonRtServerControl.writeUInt(CARLA_PLUGIN_BRIDGE_API_VERSION_CURRENT);
  193. }
  194. else
  195. {
  196. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPong);
  197. }
  198. fShmNonRtServerControl.commitWrite();
  199. }
  200. startThread(true);
  201. return true;
  202. }
  203. bool close() override
  204. {
  205. carla_debug("CarlaEngineBridge::close()");
  206. fLastPingTime = -1;
  207. CarlaEngine::close();
  208. stopThread(5000);
  209. clear();
  210. return true;
  211. }
  212. bool isRunning() const noexcept override
  213. {
  214. if (fClosingDown)
  215. return false;
  216. return isThreadRunning() || ! fFirstIdle;
  217. }
  218. bool isOffline() const noexcept override
  219. {
  220. return fIsOffline;
  221. }
  222. EngineType getType() const noexcept override
  223. {
  224. return kEngineTypeBridge;
  225. }
  226. const char* getCurrentDriverName() const noexcept override
  227. {
  228. return "Bridge";
  229. }
  230. void touchPluginParameter(const uint id, const uint32_t parameterId, const bool touch) noexcept override
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(id == 0,);
  233. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  234. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterTouch);
  235. fShmNonRtServerControl.writeUInt(parameterId);
  236. fShmNonRtServerControl.writeBool(touch);
  237. fShmNonRtServerControl.commitWrite();
  238. }
  239. CarlaEngineClient* addClient(CarlaPlugin* const plugin) override
  240. {
  241. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  242. return new CarlaEngineBridgeClient(*this, pData->graph, plugin, this);
  243. #else
  244. return new CarlaEngineBridgeClient(*this, this);
  245. // unused
  246. (void)plugin;
  247. #endif
  248. }
  249. void idle() noexcept override
  250. {
  251. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  252. if (plugin == nullptr)
  253. {
  254. if (const uint32_t length = static_cast<uint32_t>(pData->lastError.length()))
  255. {
  256. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  257. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerError);
  258. fShmNonRtServerControl.writeUInt(length);
  259. fShmNonRtServerControl.writeCustomData(pData->lastError.buffer(), length);
  260. fShmNonRtServerControl.commitWrite();
  261. }
  262. signalThreadShouldExit();
  263. callback(true, true, ENGINE_CALLBACK_QUIT, 0, 0, 0, 0, 0.0f, nullptr);
  264. return;
  265. }
  266. const bool wasFirstIdle(fFirstIdle);
  267. if (wasFirstIdle)
  268. {
  269. fFirstIdle = false;
  270. fLastPingTime = Time::currentTimeMillis();
  271. CARLA_SAFE_ASSERT(fLastPingTime > 0);
  272. char bufStr[STR_MAX+1];
  273. carla_zeroChars(bufStr, STR_MAX+1);
  274. uint32_t bufStrSize;
  275. const CarlaEngineClient* const client(plugin->getEngineClient());
  276. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  277. // kPluginBridgeNonRtServerPluginInfo1
  278. {
  279. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  280. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPluginInfo1);
  281. fShmNonRtServerControl.writeUInt(plugin->getCategory());
  282. fShmNonRtServerControl.writeUInt(plugin->getHints());
  283. fShmNonRtServerControl.writeUInt(plugin->getOptionsAvailable());
  284. fShmNonRtServerControl.writeUInt(plugin->getOptionsEnabled());
  285. fShmNonRtServerControl.writeLong(plugin->getUniqueId());
  286. fShmNonRtServerControl.commitWrite();
  287. }
  288. // kPluginBridgeNonRtServerPluginInfo2
  289. {
  290. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  291. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPluginInfo2);
  292. if (! plugin->getRealName(bufStr))
  293. bufStr[0] = '\0';
  294. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  295. fShmNonRtServerControl.writeUInt(bufStrSize);
  296. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  297. if (! plugin->getLabel(bufStr))
  298. bufStr[0] = '\0';
  299. bufStrSize = carla_fixedValue(1U, 256U, static_cast<uint32_t>(std::strlen(bufStr)));
  300. fShmNonRtServerControl.writeUInt(bufStrSize);
  301. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  302. if (! plugin->getMaker(bufStr))
  303. bufStr[0] = '\0';
  304. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  305. fShmNonRtServerControl.writeUInt(bufStrSize);
  306. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  307. if (! plugin->getCopyright(bufStr))
  308. bufStr[0] = '\0';
  309. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  310. fShmNonRtServerControl.writeUInt(bufStrSize);
  311. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  312. fShmNonRtServerControl.commitWrite();
  313. }
  314. // kPluginBridgeNonRtServerAudioCount
  315. {
  316. const uint32_t aIns = plugin->getAudioInCount();
  317. const uint32_t aOuts = plugin->getAudioOutCount();
  318. // uint/ins, uint/outs
  319. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerAudioCount);
  320. fShmNonRtServerControl.writeUInt(aIns);
  321. fShmNonRtServerControl.writeUInt(aOuts);
  322. fShmNonRtServerControl.commitWrite();
  323. // kPluginBridgeNonRtServerPortName
  324. for (uint32_t i=0; i<aIns; ++i)
  325. {
  326. const char* const portName(client->getAudioPortName(true, i));
  327. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr && portName[0] != '\0');
  328. // byte/type, uint/index, uint/size, str[] (name)
  329. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPortName);
  330. fShmNonRtServerControl.writeByte(kPluginBridgePortAudioInput);
  331. fShmNonRtServerControl.writeUInt(i);
  332. bufStrSize = static_cast<uint32_t>(std::strlen(portName));
  333. fShmNonRtServerControl.writeUInt(bufStrSize);
  334. fShmNonRtServerControl.writeCustomData(portName, bufStrSize);
  335. }
  336. // kPluginBridgeNonRtServerPortName
  337. for (uint32_t i=0; i<aOuts; ++i)
  338. {
  339. const char* const portName(client->getAudioPortName(false, i));
  340. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr && portName[0] != '\0');
  341. // byte/type, uint/index, uint/size, str[] (name)
  342. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPortName);
  343. fShmNonRtServerControl.writeByte(kPluginBridgePortAudioOutput);
  344. fShmNonRtServerControl.writeUInt(i);
  345. bufStrSize = static_cast<uint32_t>(std::strlen(portName));
  346. fShmNonRtServerControl.writeUInt(bufStrSize);
  347. fShmNonRtServerControl.writeCustomData(portName, bufStrSize);
  348. }
  349. }
  350. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  351. // kPluginBridgeNonRtServerMidiCount
  352. {
  353. // uint/ins, uint/outs
  354. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiCount);
  355. fShmNonRtServerControl.writeUInt(plugin->getMidiInCount());
  356. fShmNonRtServerControl.writeUInt(plugin->getMidiOutCount());
  357. fShmNonRtServerControl.commitWrite();
  358. }
  359. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  360. // kPluginBridgeNonRtServerCvCount
  361. {
  362. // uint/ins, uint/outs
  363. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCvCount);
  364. fShmNonRtServerControl.writeUInt(plugin->getCVInCount());
  365. fShmNonRtServerControl.writeUInt(plugin->getCVOutCount());
  366. fShmNonRtServerControl.commitWrite();
  367. }
  368. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  369. // kPluginBridgeNonRtServerParameter*
  370. if (const uint32_t count = std::min(pData->options.maxParameters, plugin->getParameterCount()))
  371. {
  372. // uint/count
  373. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterCount);
  374. fShmNonRtServerControl.writeUInt(count);
  375. fShmNonRtServerControl.commitWrite();
  376. for (uint32_t i=0; i<count; ++i)
  377. {
  378. const ParameterData& paramData(plugin->getParameterData(i));
  379. if (paramData.type != PARAMETER_INPUT && paramData.type != PARAMETER_OUTPUT)
  380. continue;
  381. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  382. continue;
  383. // kPluginBridgeNonRtServerParameterData1
  384. {
  385. // uint/index, int/rindex, uint/type, uint/hints, short/cc
  386. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterData1);
  387. fShmNonRtServerControl.writeUInt(i);
  388. fShmNonRtServerControl.writeInt(paramData.rindex);
  389. fShmNonRtServerControl.writeUInt(paramData.type);
  390. fShmNonRtServerControl.writeUInt(paramData.hints);
  391. fShmNonRtServerControl.writeShort(paramData.mappedControlIndex);
  392. fShmNonRtServerControl.commitWrite();
  393. }
  394. // kPluginBridgeNonRtServerParameterData2
  395. {
  396. // uint/index, uint/size, str[] (name), uint/size, str[] (symbol), uint/size, str[] (unit)
  397. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterData2);
  398. fShmNonRtServerControl.writeUInt(i);
  399. if (! plugin->getParameterName(i, bufStr))
  400. std::snprintf(bufStr, STR_MAX, "Param %u", i+1);
  401. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  402. fShmNonRtServerControl.writeUInt(bufStrSize);
  403. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  404. if (! plugin->getParameterSymbol(i, bufStr))
  405. bufStr[0] = '\0';
  406. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  407. fShmNonRtServerControl.writeUInt(bufStrSize);
  408. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  409. if (! plugin->getParameterUnit(i, bufStr))
  410. bufStr[0] = '\0';
  411. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  412. fShmNonRtServerControl.writeUInt(bufStrSize);
  413. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  414. fShmNonRtServerControl.commitWrite();
  415. }
  416. // kPluginBridgeNonRtServerParameterRanges
  417. {
  418. const ParameterRanges& paramRanges(plugin->getParameterRanges(i));
  419. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  420. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterRanges);
  421. fShmNonRtServerControl.writeUInt(i);
  422. fShmNonRtServerControl.writeFloat(paramRanges.def);
  423. fShmNonRtServerControl.writeFloat(paramRanges.min);
  424. fShmNonRtServerControl.writeFloat(paramRanges.max);
  425. fShmNonRtServerControl.writeFloat(paramRanges.step);
  426. fShmNonRtServerControl.writeFloat(paramRanges.stepSmall);
  427. fShmNonRtServerControl.writeFloat(paramRanges.stepLarge);
  428. fShmNonRtServerControl.commitWrite();
  429. }
  430. // kPluginBridgeNonRtServerParameterValue2
  431. {
  432. // uint/index float/value (used for init/output parameters only, don't resend values)
  433. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue2);
  434. fShmNonRtServerControl.writeUInt(i);
  435. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  436. fShmNonRtServerControl.commitWrite();
  437. }
  438. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  439. }
  440. }
  441. // kPluginBridgeNonRtServerProgram*
  442. if (const uint32_t count = plugin->getProgramCount())
  443. {
  444. // uint/count
  445. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerProgramCount);
  446. fShmNonRtServerControl.writeUInt(count);
  447. fShmNonRtServerControl.commitWrite();
  448. for (uint32_t i=0; i < count; ++i)
  449. {
  450. // uint/index, uint/size, str[] (name)
  451. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerProgramName);
  452. fShmNonRtServerControl.writeUInt(i);
  453. if (! plugin->getProgramName(i, bufStr))
  454. bufStr[0] = '\0';
  455. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  456. fShmNonRtServerControl.writeUInt(bufStrSize);
  457. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  458. fShmNonRtServerControl.commitWrite();
  459. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  460. }
  461. }
  462. // kPluginBridgeNonRtServerMidiProgram*
  463. if (const uint32_t count = plugin->getMidiProgramCount())
  464. {
  465. // uint/count
  466. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiProgramCount);
  467. fShmNonRtServerControl.writeUInt(count);
  468. fShmNonRtServerControl.commitWrite();
  469. for (uint32_t i=0; i < count; ++i)
  470. {
  471. const MidiProgramData& mpData(plugin->getMidiProgramData(i));
  472. CARLA_SAFE_ASSERT_CONTINUE(mpData.name != nullptr);
  473. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  474. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiProgramData);
  475. fShmNonRtServerControl.writeUInt(i);
  476. fShmNonRtServerControl.writeUInt(mpData.bank);
  477. fShmNonRtServerControl.writeUInt(mpData.program);
  478. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(mpData.name)));
  479. fShmNonRtServerControl.writeUInt(bufStrSize);
  480. fShmNonRtServerControl.writeCustomData(mpData.name, bufStrSize);
  481. fShmNonRtServerControl.commitWrite();
  482. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  483. }
  484. }
  485. if (const uint32_t latency = plugin->getLatencyInFrames())
  486. {
  487. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetLatency);
  488. fShmNonRtServerControl.writeUInt(latency);
  489. fShmNonRtServerControl.commitWrite();
  490. }
  491. // ready!
  492. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerReady);
  493. fShmNonRtServerControl.commitWrite();
  494. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  495. carla_stdout("Carla Bridge Ready!");
  496. fLastPingTime = Time::currentTimeMillis();
  497. }
  498. // send parameter outputs
  499. if (const uint32_t count = plugin->getParameterCount())
  500. {
  501. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  502. for (uint32_t i=0; i < count; ++i)
  503. {
  504. if (! plugin->isParameterOutput(i))
  505. continue;
  506. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue2);
  507. fShmNonRtServerControl.writeUInt(i);
  508. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  509. // parameter outputs are not that important, we can skip some
  510. if (! fShmNonRtServerControl.commitWrite())
  511. break;
  512. }
  513. }
  514. CarlaEngine::idle();
  515. try {
  516. handleNonRtData();
  517. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  518. if (fLastPingTime > 0 && Time::currentTimeMillis() > fLastPingTime + 30000 && ! wasFirstIdle)
  519. {
  520. carla_stderr("Did not receive ping message from server for 30 secs, closing...");
  521. signalThreadShouldExit();
  522. callback(true, true, ENGINE_CALLBACK_QUIT, 0, 0, 0, 0, 0.0f, nullptr);
  523. }
  524. }
  525. void callback(const bool sendHost, const bool sendOsc,
  526. const EngineCallbackOpcode action, const uint pluginId,
  527. const int value1, const int value2, const int value3,
  528. const float valuef, const char* const valueStr) noexcept override
  529. {
  530. CarlaEngine::callback(sendHost, sendOsc, action, pluginId, value1, value2, value3, valuef, valueStr);
  531. if (fClosingDown || ! sendHost)
  532. return;
  533. switch (action)
  534. {
  535. // uint/index float/value
  536. case ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED: {
  537. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  538. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  539. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue);
  540. fShmNonRtServerControl.writeUInt(static_cast<uint>(value1));
  541. fShmNonRtServerControl.writeFloat(valuef);
  542. fShmNonRtServerControl.commitWrite();
  543. } break;
  544. // uint/index float/value
  545. case ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED: {
  546. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  547. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  548. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerDefaultValue);
  549. fShmNonRtServerControl.writeUInt(static_cast<uint>(value1));
  550. fShmNonRtServerControl.writeFloat(valuef);
  551. fShmNonRtServerControl.commitWrite();
  552. } break;
  553. // int/index
  554. case ENGINE_CALLBACK_PROGRAM_CHANGED: {
  555. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  556. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  557. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCurrentProgram);
  558. fShmNonRtServerControl.writeInt(value1);
  559. fShmNonRtServerControl.commitWrite();
  560. } break;
  561. // int/index
  562. case ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED: {
  563. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  564. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  565. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCurrentMidiProgram);
  566. fShmNonRtServerControl.writeInt(value1);
  567. fShmNonRtServerControl.commitWrite();
  568. } break;
  569. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  570. if (value1 != 1)
  571. {
  572. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  573. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerUiClosed);
  574. fShmNonRtServerControl.commitWrite();
  575. }
  576. break;
  577. case ENGINE_CALLBACK_RELOAD_PARAMETERS:
  578. if (CarlaPlugin* const plugin = pData->plugins[0].plugin)
  579. {
  580. if (const uint32_t count = std::min(pData->options.maxParameters, plugin->getParameterCount()))
  581. {
  582. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  583. for (uint32_t i=0; i<count; ++i)
  584. {
  585. const ParameterData& paramData(plugin->getParameterData(i));
  586. if (paramData.type != PARAMETER_INPUT && paramData.type != PARAMETER_OUTPUT)
  587. continue;
  588. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  589. continue;
  590. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue);
  591. fShmNonRtServerControl.writeUInt(i);
  592. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  593. fShmNonRtServerControl.commitWrite();
  594. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  595. }
  596. }
  597. }
  598. break;
  599. default:
  600. break;
  601. }
  602. }
  603. // -------------------------------------------------------------------
  604. void clear() noexcept
  605. {
  606. fShmAudioPool.clear();
  607. fShmRtClientControl.clear();
  608. fShmNonRtClientControl.clear();
  609. fShmNonRtServerControl.clear();
  610. }
  611. void handleNonRtData()
  612. {
  613. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  614. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  615. for (; fShmNonRtClientControl.isDataAvailableForReading();)
  616. {
  617. const PluginBridgeNonRtClientOpcode opcode(fShmNonRtClientControl.readOpcode());
  618. #ifdef DEBUG
  619. if (opcode != kPluginBridgeNonRtClientPing) {
  620. carla_debug("CarlaEngineBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtClientOpcode2str(opcode));
  621. }
  622. #endif
  623. if (opcode != kPluginBridgeNonRtClientNull && opcode != kPluginBridgeNonRtClientPingOnOff && fLastPingTime > 0)
  624. fLastPingTime = Time::currentTimeMillis();
  625. switch (opcode)
  626. {
  627. case kPluginBridgeNonRtClientNull:
  628. break;
  629. case kPluginBridgeNonRtClientVersion: {
  630. const uint apiVersion = fShmNonRtServerControl.readUInt();
  631. CARLA_SAFE_ASSERT_UINT2(apiVersion >= CARLA_PLUGIN_BRIDGE_API_VERSION_MINIMUM,
  632. apiVersion, CARLA_PLUGIN_BRIDGE_API_VERSION_MINIMUM);
  633. } break;
  634. case kPluginBridgeNonRtClientPing: {
  635. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  636. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPong);
  637. fShmNonRtServerControl.commitWrite();
  638. } break;
  639. case kPluginBridgeNonRtClientPingOnOff: {
  640. const uint32_t onOff(fShmNonRtClientControl.readBool());
  641. fLastPingTime = onOff ? Time::currentTimeMillis() : -1;
  642. } break;
  643. case kPluginBridgeNonRtClientActivate:
  644. if (plugin != nullptr && plugin->isEnabled())
  645. plugin->setActive(true, false, false);
  646. break;
  647. case kPluginBridgeNonRtClientDeactivate:
  648. if (plugin != nullptr && plugin->isEnabled())
  649. plugin->setActive(false, false, false);
  650. break;
  651. case kPluginBridgeNonRtClientInitialSetup:
  652. // should never happen!!
  653. fShmNonRtServerControl.readUInt();
  654. fShmNonRtServerControl.readDouble();
  655. break;
  656. case kPluginBridgeNonRtClientSetParameterValue: {
  657. const uint32_t index(fShmNonRtClientControl.readUInt());
  658. const float value(fShmNonRtClientControl.readFloat());
  659. if (plugin != nullptr && plugin->isEnabled())
  660. plugin->setParameterValue(index, value, false, false, false);
  661. break;
  662. }
  663. case kPluginBridgeNonRtClientSetParameterMidiChannel: {
  664. const uint32_t index(fShmNonRtClientControl.readUInt());
  665. const uint8_t channel(fShmNonRtClientControl.readByte());
  666. if (plugin != nullptr && plugin->isEnabled())
  667. plugin->setParameterMidiChannel(index, channel, false, false);
  668. break;
  669. }
  670. case kPluginBridgeNonRtClientSetParameterMappedControlIndex: {
  671. const uint32_t index(fShmNonRtClientControl.readUInt());
  672. const int16_t ctrl(fShmNonRtClientControl.readShort());
  673. if (plugin != nullptr && plugin->isEnabled())
  674. plugin->setParameterMappedControlIndex(index, ctrl, false, false);
  675. break;
  676. }
  677. case kPluginBridgeNonRtClientSetParameterMappedRange: {
  678. const uint32_t index = fShmNonRtClientControl.readUInt();
  679. const float minimum = fShmNonRtClientControl.readFloat();
  680. const float maximum = fShmNonRtClientControl.readFloat();
  681. if (plugin != nullptr && plugin->isEnabled())
  682. plugin->setParameterMappedRange(index, minimum, maximum, false, false);
  683. break;
  684. }
  685. case kPluginBridgeNonRtClientSetProgram: {
  686. const int32_t index(fShmNonRtClientControl.readInt());
  687. if (plugin != nullptr && plugin->isEnabled())
  688. plugin->setProgram(index, true, false, false);
  689. break;
  690. }
  691. case kPluginBridgeNonRtClientSetMidiProgram: {
  692. const int32_t index(fShmNonRtClientControl.readInt());
  693. if (plugin != nullptr && plugin->isEnabled())
  694. plugin->setMidiProgram(index, true, false, false);
  695. break;
  696. }
  697. case kPluginBridgeNonRtClientSetCustomData: {
  698. // type
  699. const uint32_t typeSize(fShmNonRtClientControl.readUInt());
  700. char typeStr[typeSize+1];
  701. carla_zeroChars(typeStr, typeSize+1);
  702. fShmNonRtClientControl.readCustomData(typeStr, typeSize);
  703. // key
  704. const uint32_t keySize(fShmNonRtClientControl.readUInt());
  705. char keyStr[keySize+1];
  706. carla_zeroChars(keyStr, keySize+1);
  707. fShmNonRtClientControl.readCustomData(keyStr, keySize);
  708. // value
  709. const uint32_t valueSize(fShmNonRtClientControl.readUInt());
  710. char valueStr[valueSize+1];
  711. carla_zeroChars(valueStr, valueSize+1);
  712. if (valueSize > 0)
  713. fShmNonRtClientControl.readCustomData(valueStr, valueSize);
  714. if (plugin != nullptr && plugin->isEnabled())
  715. plugin->setCustomData(typeStr, keyStr, valueStr, true);
  716. break;
  717. }
  718. case kPluginBridgeNonRtClientSetChunkDataFile: {
  719. const uint32_t size(fShmNonRtClientControl.readUInt());
  720. CARLA_SAFE_ASSERT_BREAK(size > 0);
  721. char chunkFilePathTry[size+1];
  722. carla_zeroChars(chunkFilePathTry, size+1);
  723. fShmNonRtClientControl.readCustomData(chunkFilePathTry, size);
  724. CARLA_SAFE_ASSERT_BREAK(chunkFilePathTry[0] != '\0');
  725. if (plugin == nullptr || ! plugin->isEnabled()) break;
  726. String chunkFilePath(chunkFilePathTry);
  727. #ifdef CARLA_OS_WIN
  728. // check if running under Wine
  729. if (chunkFilePath.startsWith("/"))
  730. chunkFilePath = chunkFilePath.replaceSection(0, 1, "Z:\\").replace("/", "\\");
  731. #endif
  732. File chunkFile(chunkFilePath);
  733. CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
  734. String chunkDataBase64(chunkFile.loadFileAsString());
  735. chunkFile.deleteFile();
  736. CARLA_SAFE_ASSERT_BREAK(chunkDataBase64.isNotEmpty());
  737. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(chunkDataBase64.toRawUTF8()));
  738. #ifdef CARLA_PROPER_CPP11_SUPPORT
  739. plugin->setChunkData(chunk.data(), chunk.size());
  740. #else
  741. plugin->setChunkData(&chunk.front(), chunk.size());
  742. #endif
  743. break;
  744. }
  745. case kPluginBridgeNonRtClientSetCtrlChannel: {
  746. const int16_t channel(fShmNonRtClientControl.readShort());
  747. CARLA_SAFE_ASSERT_BREAK(channel >= -1 && channel < MAX_MIDI_CHANNELS);
  748. if (plugin != nullptr && plugin->isEnabled())
  749. plugin->setCtrlChannel(static_cast<int8_t>(channel), false, false);
  750. break;
  751. }
  752. case kPluginBridgeNonRtClientSetOption: {
  753. const uint32_t option(fShmNonRtClientControl.readUInt());
  754. const bool yesNo(fShmNonRtClientControl.readBool());
  755. if (plugin != nullptr && plugin->isEnabled())
  756. plugin->setOption(option, yesNo, false);
  757. break;
  758. }
  759. case kPluginBridgeNonRtClientSetOptions: {
  760. const uint32_t options(fShmNonRtClientControl.readUInt());
  761. if (plugin != nullptr)
  762. plugin->pData->options = options;
  763. break;
  764. }
  765. case kPluginBridgeNonRtClientGetParameterText: {
  766. const int32_t index(fShmNonRtClientControl.readInt());
  767. if (index >= 0 && plugin != nullptr && plugin->isEnabled())
  768. {
  769. char bufStr[STR_MAX+1];
  770. carla_zeroChars(bufStr, STR_MAX+1);
  771. if (! plugin->getParameterText(static_cast<uint32_t>(index), bufStr))
  772. bufStr[0] = '\0';
  773. const uint32_t bufStrLen = static_cast<uint32_t>(std::strlen(bufStr));
  774. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  775. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetParameterText);
  776. fShmNonRtServerControl.writeInt(index);
  777. fShmNonRtServerControl.writeUInt(bufStrLen);
  778. fShmNonRtServerControl.writeCustomData(bufStr, bufStrLen);
  779. fShmNonRtServerControl.commitWrite();
  780. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  781. }
  782. break;
  783. }
  784. case kPluginBridgeNonRtClientPrepareForSave: {
  785. if (plugin == nullptr || ! plugin->isEnabled())
  786. break;
  787. plugin->prepareForSave();
  788. for (uint32_t i=0, count=plugin->getCustomDataCount(); i<count; ++i)
  789. {
  790. const CustomData& cdata(plugin->getCustomData(i));
  791. if (std::strcmp(cdata.type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  792. continue;
  793. const uint32_t typeLen(static_cast<uint32_t>(std::strlen(cdata.type)));
  794. const uint32_t keyLen(static_cast<uint32_t>(std::strlen(cdata.key)));
  795. const uint32_t valueLen(static_cast<uint32_t>(std::strlen(cdata.value)));
  796. {
  797. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  798. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetCustomData);
  799. fShmNonRtServerControl.writeUInt(typeLen);
  800. fShmNonRtServerControl.writeCustomData(cdata.type, typeLen);
  801. fShmNonRtServerControl.writeUInt(keyLen);
  802. fShmNonRtServerControl.writeCustomData(cdata.key, keyLen);
  803. fShmNonRtServerControl.writeUInt(valueLen);
  804. if (valueLen > 0)
  805. fShmNonRtServerControl.writeCustomData(cdata.value, valueLen);
  806. fShmNonRtServerControl.commitWrite();
  807. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  808. }
  809. }
  810. if (plugin->getOptionsEnabled() & PLUGIN_OPTION_USE_CHUNKS)
  811. {
  812. void* data = nullptr;
  813. if (const std::size_t dataSize = plugin->getChunkData(&data))
  814. {
  815. CARLA_SAFE_ASSERT_BREAK(data != nullptr);
  816. CarlaString dataBase64 = CarlaString::asBase64(data, dataSize);
  817. CARLA_SAFE_ASSERT_BREAK(dataBase64.length() > 0);
  818. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  819. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  820. filePath += fShmAudioPool.getFilenameSuffix();
  821. if (File(filePath).replaceWithText(dataBase64.buffer()))
  822. {
  823. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  824. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  825. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetChunkDataFile);
  826. fShmNonRtServerControl.writeUInt(ulength);
  827. fShmNonRtServerControl.writeCustomData(filePath.toRawUTF8(), ulength);
  828. fShmNonRtServerControl.commitWrite();
  829. }
  830. }
  831. }
  832. {
  833. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  834. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSaved);
  835. fShmNonRtServerControl.commitWrite();
  836. }
  837. break;
  838. }
  839. case kPluginBridgeNonRtClientRestoreLV2State:
  840. if (plugin != nullptr && plugin->isEnabled())
  841. plugin->restoreLV2State();
  842. break;
  843. case kPluginBridgeNonRtClientShowUI:
  844. if (plugin != nullptr && plugin->isEnabled())
  845. plugin->showCustomUI(true);
  846. break;
  847. case kPluginBridgeNonRtClientHideUI:
  848. if (plugin != nullptr && plugin->isEnabled())
  849. plugin->showCustomUI(false);
  850. break;
  851. case kPluginBridgeNonRtClientUiParameterChange: {
  852. const uint32_t index(fShmNonRtClientControl.readUInt());
  853. const float value(fShmNonRtClientControl.readFloat());
  854. if (plugin != nullptr && plugin->isEnabled())
  855. plugin->uiParameterChange(index, value);
  856. break;
  857. }
  858. case kPluginBridgeNonRtClientUiProgramChange: {
  859. const uint32_t index(fShmNonRtClientControl.readUInt());
  860. if (plugin != nullptr && plugin->isEnabled())
  861. plugin->uiProgramChange(index);
  862. break;
  863. }
  864. case kPluginBridgeNonRtClientUiMidiProgramChange: {
  865. const uint32_t index(fShmNonRtClientControl.readUInt());
  866. if (plugin != nullptr && plugin->isEnabled())
  867. plugin->uiMidiProgramChange(index);
  868. break;
  869. }
  870. case kPluginBridgeNonRtClientUiNoteOn: {
  871. const uint8_t chnl(fShmNonRtClientControl.readByte());
  872. const uint8_t note(fShmNonRtClientControl.readByte());
  873. const uint8_t velo(fShmNonRtClientControl.readByte());
  874. if (plugin != nullptr && plugin->isEnabled())
  875. plugin->uiNoteOn(chnl, note, velo);
  876. break;
  877. }
  878. case kPluginBridgeNonRtClientUiNoteOff: {
  879. const uint8_t chnl(fShmNonRtClientControl.readByte());
  880. const uint8_t note(fShmNonRtClientControl.readByte());
  881. if (plugin != nullptr && plugin->isEnabled())
  882. plugin->uiNoteOff(chnl, note);
  883. break;
  884. }
  885. case kPluginBridgeNonRtClientQuit:
  886. fClosingDown = true;
  887. signalThreadShouldExit();
  888. callback(true, true, ENGINE_CALLBACK_QUIT, 0, 0, 0, 0, 0.0f, nullptr);
  889. break;
  890. }
  891. }
  892. }
  893. // -------------------------------------------------------------------
  894. protected:
  895. void run() override
  896. {
  897. #ifdef __SSE2_MATH__
  898. // Set FTZ and DAZ flags
  899. _mm_setcsr(_mm_getcsr() | 0x8040);
  900. #endif
  901. bool quitReceived = false;
  902. for (; ! shouldThreadExit();)
  903. {
  904. const BridgeRtClientControl::WaitHelper helper(fShmRtClientControl);
  905. if (! helper.ok)
  906. continue;
  907. for (; fShmRtClientControl.isDataAvailableForReading();)
  908. {
  909. const PluginBridgeRtClientOpcode opcode(fShmRtClientControl.readOpcode());
  910. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  911. #ifdef DEBUG
  912. if (opcode != kPluginBridgeRtClientProcess && opcode != kPluginBridgeRtClientMidiEvent) {
  913. carla_debug("CarlaEngineBridgeRtThread::run() - got opcode: %s", PluginBridgeRtClientOpcode2str(opcode));
  914. }
  915. #endif
  916. switch (opcode)
  917. {
  918. case kPluginBridgeRtClientNull:
  919. break;
  920. case kPluginBridgeRtClientSetAudioPool: {
  921. if (fShmAudioPool.data != nullptr)
  922. {
  923. jackbridge_shm_unmap(fShmAudioPool.shm, fShmAudioPool.data);
  924. fShmAudioPool.data = nullptr;
  925. }
  926. const uint64_t poolSize(fShmRtClientControl.readULong());
  927. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  928. fShmAudioPool.data = (float*)jackbridge_shm_map(fShmAudioPool.shm, static_cast<size_t>(poolSize));
  929. break;
  930. }
  931. case kPluginBridgeRtClientSetBufferSize: {
  932. const uint32_t bufferSize(fShmRtClientControl.readUInt());
  933. pData->bufferSize = bufferSize;
  934. bufferSizeChanged(bufferSize);
  935. break;
  936. }
  937. case kPluginBridgeRtClientSetSampleRate: {
  938. const double sampleRate(fShmRtClientControl.readDouble());
  939. pData->sampleRate = sampleRate;
  940. sampleRateChanged(sampleRate);
  941. break;
  942. }
  943. case kPluginBridgeRtClientSetOnline:
  944. fIsOffline = fShmRtClientControl.readBool();
  945. offlineModeChanged(fIsOffline);
  946. break;
  947. case kPluginBridgeRtClientControlEventParameter: {
  948. const uint32_t time(fShmRtClientControl.readUInt());
  949. const uint8_t channel(fShmRtClientControl.readByte());
  950. const uint16_t param(fShmRtClientControl.readUShort());
  951. const float value(fShmRtClientControl.readFloat());
  952. if (EngineEvent* const event = getNextFreeInputEvent())
  953. {
  954. event->type = kEngineEventTypeControl;
  955. event->time = time;
  956. event->channel = channel;
  957. event->ctrl.type = kEngineControlEventTypeParameter;
  958. event->ctrl.param = param;
  959. event->ctrl.value = value;
  960. }
  961. break;
  962. }
  963. case kPluginBridgeRtClientControlEventMidiBank: {
  964. const uint32_t time(fShmRtClientControl.readUInt());
  965. const uint8_t channel(fShmRtClientControl.readByte());
  966. const uint16_t index(fShmRtClientControl.readUShort());
  967. if (EngineEvent* const event = getNextFreeInputEvent())
  968. {
  969. event->type = kEngineEventTypeControl;
  970. event->time = time;
  971. event->channel = channel;
  972. event->ctrl.type = kEngineControlEventTypeMidiBank;
  973. event->ctrl.param = index;
  974. event->ctrl.value = 0.0f;
  975. }
  976. break;
  977. }
  978. case kPluginBridgeRtClientControlEventMidiProgram: {
  979. const uint32_t time(fShmRtClientControl.readUInt());
  980. const uint8_t channel(fShmRtClientControl.readByte());
  981. const uint16_t index(fShmRtClientControl.readUShort());
  982. if (EngineEvent* const event = getNextFreeInputEvent())
  983. {
  984. event->type = kEngineEventTypeControl;
  985. event->time = time;
  986. event->channel = channel;
  987. event->ctrl.type = kEngineControlEventTypeMidiProgram;
  988. event->ctrl.param = index;
  989. event->ctrl.value = 0.0f;
  990. }
  991. break;
  992. }
  993. case kPluginBridgeRtClientControlEventAllSoundOff: {
  994. const uint32_t time(fShmRtClientControl.readUInt());
  995. const uint8_t channel(fShmRtClientControl.readByte());
  996. if (EngineEvent* const event = getNextFreeInputEvent())
  997. {
  998. event->type = kEngineEventTypeControl;
  999. event->time = time;
  1000. event->channel = channel;
  1001. event->ctrl.type = kEngineControlEventTypeAllSoundOff;
  1002. event->ctrl.param = 0;
  1003. event->ctrl.value = 0.0f;
  1004. }
  1005. } break;
  1006. case kPluginBridgeRtClientControlEventAllNotesOff: {
  1007. const uint32_t time(fShmRtClientControl.readUInt());
  1008. const uint8_t channel(fShmRtClientControl.readByte());
  1009. if (EngineEvent* const event = getNextFreeInputEvent())
  1010. {
  1011. event->type = kEngineEventTypeControl;
  1012. event->time = time;
  1013. event->channel = channel;
  1014. event->ctrl.type = kEngineControlEventTypeAllNotesOff;
  1015. event->ctrl.param = 0;
  1016. event->ctrl.value = 0.0f;
  1017. }
  1018. } break;
  1019. case kPluginBridgeRtClientMidiEvent: {
  1020. const uint32_t time(fShmRtClientControl.readUInt());
  1021. const uint8_t port(fShmRtClientControl.readByte());
  1022. const uint8_t size(fShmRtClientControl.readByte());
  1023. CARLA_SAFE_ASSERT_BREAK(size > 0);
  1024. // FIXME variable-size stack
  1025. uint8_t data[size];
  1026. for (uint8_t i=0; i<size; ++i)
  1027. data[i] = fShmRtClientControl.readByte();
  1028. if (EngineEvent* const event = getNextFreeInputEvent())
  1029. {
  1030. event->type = kEngineEventTypeMidi;
  1031. event->time = time;
  1032. event->channel = MIDI_GET_CHANNEL_FROM_DATA(data);
  1033. event->midi.port = port;
  1034. event->midi.size = size;
  1035. if (size > EngineMidiEvent::kDataSize)
  1036. {
  1037. event->midi.dataExt = data;
  1038. std::memset(event->midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
  1039. }
  1040. else
  1041. {
  1042. event->midi.data[0] = MIDI_GET_STATUS_FROM_DATA(data);
  1043. uint8_t i=1;
  1044. for (; i < size; ++i)
  1045. event->midi.data[i] = data[i];
  1046. for (; i < EngineMidiEvent::kDataSize; ++i)
  1047. event->midi.data[i] = 0;
  1048. event->midi.dataExt = nullptr;
  1049. }
  1050. }
  1051. break;
  1052. }
  1053. case kPluginBridgeRtClientProcess: {
  1054. const uint32_t frames(fShmRtClientControl.readUInt());
  1055. CARLA_SAFE_ASSERT_BREAK(fShmAudioPool.data != nullptr);
  1056. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(fIsOffline))
  1057. {
  1058. const BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  1059. const uint32_t audioInCount(plugin->getAudioInCount());
  1060. const uint32_t audioOutCount(plugin->getAudioOutCount());
  1061. const uint32_t cvInCount(plugin->getCVInCount());
  1062. const uint32_t cvOutCount(plugin->getCVOutCount());
  1063. const float* audioIn[audioInCount];
  1064. /* */ float* audioOut[audioOutCount];
  1065. const float* cvIn[cvInCount];
  1066. /* */ float* cvOut[cvOutCount];
  1067. float* fdata = fShmAudioPool.data;
  1068. for (uint32_t i=0; i < audioInCount; ++i, fdata += pData->bufferSize)
  1069. audioIn[i] = fdata;
  1070. for (uint32_t i=0; i < audioOutCount; ++i, fdata += pData->bufferSize)
  1071. audioOut[i] = fdata;
  1072. for (uint32_t i=0; i < cvInCount; ++i, fdata += pData->bufferSize)
  1073. cvIn[i] = fdata;
  1074. for (uint32_t i=0; i < cvOutCount; ++i, fdata += pData->bufferSize)
  1075. cvOut[i] = fdata;
  1076. EngineTimeInfo& timeInfo(pData->timeInfo);
  1077. timeInfo.playing = bridgeTimeInfo.playing;
  1078. timeInfo.frame = bridgeTimeInfo.frame;
  1079. timeInfo.usecs = bridgeTimeInfo.usecs;
  1080. timeInfo.bbt.valid = (bridgeTimeInfo.validFlags & kPluginBridgeTimeInfoValidBBT) != 0;
  1081. if (timeInfo.bbt.valid)
  1082. {
  1083. timeInfo.bbt.bar = bridgeTimeInfo.bar;
  1084. timeInfo.bbt.beat = bridgeTimeInfo.beat;
  1085. timeInfo.bbt.tick = bridgeTimeInfo.tick;
  1086. timeInfo.bbt.beatsPerBar = bridgeTimeInfo.beatsPerBar;
  1087. timeInfo.bbt.beatType = bridgeTimeInfo.beatType;
  1088. timeInfo.bbt.ticksPerBeat = bridgeTimeInfo.ticksPerBeat;
  1089. timeInfo.bbt.beatsPerMinute = bridgeTimeInfo.beatsPerMinute;
  1090. timeInfo.bbt.barStartTick = bridgeTimeInfo.barStartTick;
  1091. }
  1092. plugin->initBuffers();
  1093. plugin->process(audioIn, audioOut, cvIn, cvOut, frames);
  1094. plugin->unlock();
  1095. }
  1096. uint8_t* midiData(fShmRtClientControl.data->midiOut);
  1097. carla_zeroBytes(midiData, kBridgeBaseMidiOutHeaderSize);
  1098. std::size_t curMidiDataPos = 0;
  1099. if (pData->events.in[0].type != kEngineEventTypeNull)
  1100. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  1101. if (pData->events.out[0].type != kEngineEventTypeNull)
  1102. {
  1103. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  1104. {
  1105. const EngineEvent& event(pData->events.out[i]);
  1106. if (event.type == kEngineEventTypeNull)
  1107. break;
  1108. if (event.type == kEngineEventTypeControl)
  1109. {
  1110. uint8_t data[3];
  1111. const uint8_t size = event.ctrl.convertToMidiData(event.channel, data);
  1112. CARLA_SAFE_ASSERT_CONTINUE(size > 0 && size <= 3);
  1113. if (curMidiDataPos + kBridgeBaseMidiOutHeaderSize + size >= kBridgeRtClientDataMidiOutSize)
  1114. break;
  1115. // set time
  1116. *(uint32_t*)midiData = event.time;
  1117. midiData = midiData + 4;
  1118. curMidiDataPos += 4;
  1119. // set port
  1120. *midiData++ = 0;
  1121. ++curMidiDataPos;
  1122. // set size
  1123. *midiData++ = size;
  1124. ++curMidiDataPos;
  1125. // set data
  1126. for (uint8_t j=0; j<size; ++j)
  1127. *midiData++ = data[j];
  1128. curMidiDataPos += size;
  1129. }
  1130. else if (event.type == kEngineEventTypeMidi)
  1131. {
  1132. const EngineMidiEvent& _midiEvent(event.midi);
  1133. if (curMidiDataPos + kBridgeBaseMidiOutHeaderSize + _midiEvent.size >= kBridgeRtClientDataMidiOutSize)
  1134. break;
  1135. const uint8_t* const _midiData(_midiEvent.dataExt != nullptr ? _midiEvent.dataExt : _midiEvent.data);
  1136. // set time
  1137. *(uint32_t*)midiData = event.time;
  1138. midiData += 4;
  1139. curMidiDataPos += 4;
  1140. // set port
  1141. *midiData++ = _midiEvent.port;
  1142. ++curMidiDataPos;
  1143. // set size
  1144. *midiData++ = _midiEvent.size;
  1145. ++curMidiDataPos;
  1146. // set data
  1147. *midiData++ = uint8_t(_midiData[0] | (event.channel & MIDI_CHANNEL_BIT));
  1148. for (uint8_t j=1; j<_midiEvent.size; ++j)
  1149. *midiData++ = _midiData[j];
  1150. curMidiDataPos += _midiEvent.size;
  1151. }
  1152. }
  1153. if (curMidiDataPos != 0 &&
  1154. curMidiDataPos + kBridgeBaseMidiOutHeaderSize < kBridgeRtClientDataMidiOutSize)
  1155. carla_zeroBytes(midiData, kBridgeBaseMidiOutHeaderSize);
  1156. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  1157. }
  1158. } break;
  1159. case kPluginBridgeRtClientQuit: {
  1160. quitReceived = true;
  1161. fClosingDown = true;
  1162. signalThreadShouldExit();
  1163. } break;
  1164. }
  1165. }
  1166. }
  1167. callback(true, true, ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0, 0.0f, nullptr);
  1168. if (! quitReceived)
  1169. {
  1170. const char* const message("Plugin bridge error, process thread has stopped");
  1171. const std::size_t messageSize(std::strlen(message));
  1172. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  1173. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerError);
  1174. fShmNonRtServerControl.writeUInt(messageSize);
  1175. fShmNonRtServerControl.writeCustomData(message, messageSize);
  1176. fShmNonRtServerControl.commitWrite();
  1177. }
  1178. }
  1179. // called from process thread above
  1180. EngineEvent* getNextFreeInputEvent() const noexcept
  1181. {
  1182. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  1183. {
  1184. EngineEvent* const event(&pData->events.in[i]);
  1185. if (event->type == kEngineEventTypeNull)
  1186. return event;
  1187. }
  1188. return nullptr;
  1189. }
  1190. void latencyChanged(const uint32_t samples) noexcept override
  1191. {
  1192. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  1193. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetLatency);
  1194. fShmNonRtServerControl.writeUInt(samples);
  1195. fShmNonRtServerControl.commitWrite();
  1196. }
  1197. // -------------------------------------------------------------------
  1198. private:
  1199. BridgeAudioPool fShmAudioPool;
  1200. BridgeRtClientControl fShmRtClientControl;
  1201. BridgeNonRtClientControl fShmNonRtClientControl;
  1202. BridgeNonRtServerControl fShmNonRtServerControl;
  1203. CarlaString fBaseNameAudioPool;
  1204. CarlaString fBaseNameRtClientControl;
  1205. CarlaString fBaseNameNonRtClientControl;
  1206. CarlaString fBaseNameNonRtServerControl;
  1207. bool fClosingDown;
  1208. bool fIsOffline;
  1209. bool fFirstIdle;
  1210. int64_t fLastPingTime;
  1211. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  1212. };
  1213. // -----------------------------------------------------------------------
  1214. namespace EngineInit {
  1215. CarlaEngine* newBridge(const char* const audioPoolBaseName,
  1216. const char* const rtClientBaseName,
  1217. const char* const nonRtClientBaseName,
  1218. const char* const nonRtServerBaseName)
  1219. {
  1220. return new CarlaEngineBridge(audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  1221. }
  1222. }
  1223. // -----------------------------------------------------------------------
  1224. CARLA_BACKEND_END_NAMESPACE
  1225. // -----------------------------------------------------------------------
  1226. #include "CarlaBridgeUtils.cpp"
  1227. // -----------------------------------------------------------------------