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.

1423 lines
57KB

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