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.

1690 lines
68KB

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