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.

1530 lines
61KB

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