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.

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