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.

1349 lines
54KB

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