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

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