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.

1499 lines
60KB

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