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.

1444 lines
58KB

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