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.

1478 lines
59KB

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