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.

1366 lines
55KB

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