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.

1384 lines
56KB

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