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.

1566 lines
63KB

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