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.

2606 lines
90KB

  1. /*
  2. * Carla Plugin Bridge
  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. #ifdef BUILD_BRIDGE
  18. # error This file should be used under bridge mode
  19. #endif
  20. #include "CarlaPluginInternal.hpp"
  21. #include "CarlaBackendUtils.hpp"
  22. #include "CarlaBase64Utils.hpp"
  23. #include "CarlaBridgeUtils.hpp"
  24. #include "CarlaEngineUtils.hpp"
  25. #include "CarlaMathUtils.hpp"
  26. #include "CarlaShmUtils.hpp"
  27. #include "CarlaThread.hpp"
  28. #include "jackbridge/JackBridge.hpp"
  29. #include <ctime>
  30. // -------------------------------------------------------------------------------------------------------------------
  31. using juce::ChildProcess;
  32. using juce::File;
  33. using juce::ScopedPointer;
  34. using juce::String;
  35. using juce::StringArray;
  36. using juce::Time;
  37. CARLA_BACKEND_START_NAMESPACE
  38. // -------------------------------------------------------------------------------------------------------------------
  39. // Fallback data
  40. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  41. // -------------------------------------------------------------------------------------------------------------------
  42. struct BridgeNonRtClientControl : public CarlaRingBufferControl<BigStackBuffer> {
  43. BridgeNonRtClientData* data;
  44. CarlaString filename;
  45. CarlaMutex mutex;
  46. carla_shm_t shm;
  47. BridgeNonRtClientControl() noexcept
  48. : data(nullptr),
  49. filename(),
  50. mutex()
  51. #ifdef CARLA_PROPER_CPP11_SUPPORT
  52. , shm(carla_shm_t_INIT) {}
  53. #else
  54. {
  55. carla_shm_init(shm);
  56. }
  57. #endif
  58. ~BridgeNonRtClientControl() noexcept override
  59. {
  60. // should be cleared by now
  61. CARLA_SAFE_ASSERT(data == nullptr);
  62. clear();
  63. }
  64. bool initialize() noexcept
  65. {
  66. char tmpFileBase[64];
  67. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "XXXXXX");
  68. shm = carla_shm_create_temp(tmpFileBase);
  69. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm), false);
  70. if (! mapData())
  71. {
  72. carla_shm_close(shm);
  73. carla_shm_init(shm);
  74. return false;
  75. }
  76. CARLA_SAFE_ASSERT(data != nullptr);
  77. filename = tmpFileBase;
  78. return true;
  79. }
  80. void clear() noexcept
  81. {
  82. filename.clear();
  83. if (data != nullptr)
  84. unmapData();
  85. if (! carla_is_shm_valid(shm))
  86. return;
  87. carla_shm_close(shm);
  88. carla_shm_init(shm);
  89. }
  90. bool mapData() noexcept
  91. {
  92. CARLA_SAFE_ASSERT(data == nullptr);
  93. if (carla_shm_map<BridgeNonRtClientData>(shm, data))
  94. {
  95. setRingBuffer(&data->ringBuffer, true);
  96. return true;
  97. }
  98. return false;
  99. }
  100. void unmapData() noexcept
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  103. carla_shm_unmap(shm, data);
  104. data = nullptr;
  105. setRingBuffer(nullptr, false);
  106. }
  107. void writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept
  108. {
  109. writeUInt(static_cast<uint32_t>(opcode));
  110. }
  111. void waitIfDataIsReachingLimit() noexcept
  112. {
  113. if (getAvailableDataSize() < BigStackBuffer::size/4)
  114. return;
  115. for (int i=50; --i >= 0;)
  116. {
  117. if (getAvailableDataSize() >= BigStackBuffer::size*3/4)
  118. {
  119. writeOpcode(kPluginBridgeNonRtClientPing);
  120. commitWrite();
  121. return;
  122. }
  123. carla_msleep(20);
  124. }
  125. carla_stderr("Server waitIfDataIsReachingLimit() reached and failed");
  126. }
  127. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtClientControl)
  128. };
  129. // -------------------------------------------------------------------------------------------------------------------
  130. struct BridgeNonRtServerControl : public CarlaRingBufferControl<HugeStackBuffer> {
  131. BridgeNonRtServerData* data;
  132. CarlaString filename;
  133. carla_shm_t shm;
  134. BridgeNonRtServerControl() noexcept
  135. : data(nullptr),
  136. filename()
  137. #ifdef CARLA_PROPER_CPP11_SUPPORT
  138. , shm(carla_shm_t_INIT) {}
  139. #else
  140. {
  141. carla_shm_init(shm);
  142. }
  143. #endif
  144. ~BridgeNonRtServerControl() noexcept override
  145. {
  146. // should be cleared by now
  147. CARLA_SAFE_ASSERT(data == nullptr);
  148. clear();
  149. }
  150. bool initialize() noexcept
  151. {
  152. char tmpFileBase[64];
  153. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "XXXXXX");
  154. shm = carla_shm_create_temp(tmpFileBase);
  155. if (! carla_is_shm_valid(shm))
  156. return false;
  157. if (! mapData())
  158. {
  159. carla_shm_close(shm);
  160. carla_shm_init(shm);
  161. return false;
  162. }
  163. CARLA_SAFE_ASSERT(data != nullptr);
  164. filename = tmpFileBase;
  165. return true;
  166. }
  167. void clear() noexcept
  168. {
  169. filename.clear();
  170. if (data != nullptr)
  171. unmapData();
  172. if (! carla_is_shm_valid(shm))
  173. return;
  174. carla_shm_close(shm);
  175. carla_shm_init(shm);
  176. }
  177. bool mapData() noexcept
  178. {
  179. CARLA_SAFE_ASSERT(data == nullptr);
  180. if (carla_shm_map<BridgeNonRtServerData>(shm, data))
  181. {
  182. setRingBuffer(&data->ringBuffer, true);
  183. return true;
  184. }
  185. return false;
  186. }
  187. void unmapData() noexcept
  188. {
  189. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  190. carla_shm_unmap(shm, data);
  191. data = nullptr;
  192. setRingBuffer(nullptr, false);
  193. }
  194. PluginBridgeNonRtServerOpcode readOpcode() noexcept
  195. {
  196. return static_cast<PluginBridgeNonRtServerOpcode>(readUInt());
  197. }
  198. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtServerControl)
  199. };
  200. // -------------------------------------------------------------------------------------------------------------------
  201. struct BridgeParamInfo {
  202. float value;
  203. CarlaString name;
  204. CarlaString symbol;
  205. CarlaString unit;
  206. BridgeParamInfo() noexcept
  207. : value(0.0f),
  208. name(),
  209. symbol(),
  210. unit() {}
  211. CARLA_DECLARE_NON_COPY_STRUCT(BridgeParamInfo)
  212. };
  213. // -------------------------------------------------------------------------------------------------------------------
  214. class CarlaPluginBridgeThread : public CarlaThread
  215. {
  216. public:
  217. CarlaPluginBridgeThread(CarlaEngine* const engine, CarlaPlugin* const plugin) noexcept
  218. : CarlaThread("CarlaPluginBridgeThread"),
  219. kEngine(engine),
  220. kPlugin(plugin),
  221. fBinary(),
  222. fLabel(),
  223. fShmIds(),
  224. fProcess() {}
  225. void setData(const char* const binary, const char* const label, const char* const shmIds) noexcept
  226. {
  227. CARLA_SAFE_ASSERT_RETURN(binary != nullptr && binary[0] != '\0',);
  228. CARLA_SAFE_ASSERT_RETURN(shmIds != nullptr && shmIds[0] != '\0',);
  229. CARLA_SAFE_ASSERT(! isThreadRunning());
  230. fBinary = binary;
  231. fShmIds = shmIds;
  232. if (label != nullptr)
  233. fLabel = label;
  234. if (fLabel.isEmpty())
  235. fLabel = "\"\"";
  236. }
  237. uintptr_t getProcessPID() const noexcept
  238. {
  239. CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
  240. return (uintptr_t)fProcess->getPID();
  241. }
  242. protected:
  243. void run()
  244. {
  245. if (fProcess == nullptr)
  246. {
  247. fProcess = new ChildProcess();
  248. }
  249. else if (fProcess->isRunning())
  250. {
  251. carla_stderr("CarlaPluginBridgeThread::run() - already running");
  252. }
  253. String name(kPlugin->getName());
  254. String filename(kPlugin->getFilename());
  255. if (name.isEmpty())
  256. name = "(none)";
  257. if (filename.isEmpty())
  258. filename = "\"\"";
  259. StringArray arguments;
  260. #ifndef CARLA_OS_WIN
  261. // start with "wine" if needed
  262. if (fBinary.endsWithIgnoreCase(".exe"))
  263. arguments.add("wine");
  264. #endif
  265. // binary
  266. arguments.add(fBinary);
  267. // plugin type
  268. arguments.add(getPluginTypeAsString(kPlugin->getType()));
  269. // filename
  270. arguments.add(filename);
  271. // label
  272. arguments.add(fLabel);
  273. // uniqueId
  274. arguments.add(String(static_cast<juce::int64>(kPlugin->getUniqueId())));
  275. bool started;
  276. {
  277. char strBuf[STR_MAX+1];
  278. strBuf[STR_MAX] = '\0';
  279. const EngineOptions& options(kEngine->getOptions());
  280. const ScopedEngineEnvironmentLocker _seel(kEngine);
  281. #ifdef CARLA_OS_LINUX
  282. const char* const oldPreload(std::getenv("LD_PRELOAD"));
  283. if (oldPreload != nullptr)
  284. ::unsetenv("LD_PRELOAD");
  285. #endif
  286. carla_setenv("ENGINE_OPTION_FORCE_STEREO", bool2str(options.forceStereo));
  287. carla_setenv("ENGINE_OPTION_PREFER_PLUGIN_BRIDGES", bool2str(options.preferPluginBridges));
  288. carla_setenv("ENGINE_OPTION_PREFER_UI_BRIDGES", bool2str(options.preferUiBridges));
  289. carla_setenv("ENGINE_OPTION_UIS_ALWAYS_ON_TOP", bool2str(options.uisAlwaysOnTop));
  290. std::snprintf(strBuf, STR_MAX, "%u", options.maxParameters);
  291. carla_setenv("ENGINE_OPTION_MAX_PARAMETERS", strBuf);
  292. std::snprintf(strBuf, STR_MAX, "%u", options.uiBridgesTimeout);
  293. carla_setenv("ENGINE_OPTION_UI_BRIDGES_TIMEOUT",strBuf);
  294. if (options.pathLADSPA != nullptr)
  295. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", options.pathLADSPA);
  296. else
  297. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", "");
  298. if (options.pathDSSI != nullptr)
  299. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", options.pathDSSI);
  300. else
  301. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", "");
  302. if (options.pathLV2 != nullptr)
  303. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", options.pathLV2);
  304. else
  305. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", "");
  306. if (options.pathVST2 != nullptr)
  307. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", options.pathVST2);
  308. else
  309. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", "");
  310. if (options.pathVST3 != nullptr)
  311. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", options.pathVST3);
  312. else
  313. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", "");
  314. if (options.pathGIG != nullptr)
  315. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_GIG", options.pathGIG);
  316. else
  317. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_GIG", "");
  318. if (options.pathSF2 != nullptr)
  319. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", options.pathSF2);
  320. else
  321. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", "");
  322. if (options.pathSFZ != nullptr)
  323. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", options.pathSFZ);
  324. else
  325. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", "");
  326. if (options.binaryDir != nullptr)
  327. carla_setenv("ENGINE_OPTION_PATH_BINARIES", options.binaryDir);
  328. else
  329. carla_setenv("ENGINE_OPTION_PATH_BINARIES", "");
  330. if (options.resourceDir != nullptr)
  331. carla_setenv("ENGINE_OPTION_PATH_RESOURCES", options.resourceDir);
  332. else
  333. carla_setenv("ENGINE_OPTION_PATH_RESOURCES", "");
  334. carla_setenv("ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR", bool2str(options.preventBadBehaviour));
  335. std::snprintf(strBuf, STR_MAX, P_UINTPTR, options.frontendWinId);
  336. carla_setenv("ENGINE_OPTION_FRONTEND_WIN_ID", strBuf);
  337. carla_setenv("ENGINE_BRIDGE_SHM_IDS", fShmIds.toRawUTF8());
  338. carla_setenv("WINEDEBUG", "-all");
  339. carla_stdout("starting plugin bridge, command is:\n%s \"%s\" \"%s\" \"%s\" " P_INT64,
  340. fBinary.toRawUTF8(), getPluginTypeAsString(kPlugin->getType()), filename.toRawUTF8(), fLabel.toRawUTF8(), kPlugin->getUniqueId());
  341. started = fProcess->start(arguments);
  342. #ifdef CARLA_OS_LINUX
  343. if (oldPreload != nullptr)
  344. ::setenv("LD_PRELOAD", oldPreload, 1);
  345. #endif
  346. }
  347. if (! started)
  348. {
  349. carla_stdout("failed!");
  350. fProcess = nullptr;
  351. return;
  352. }
  353. for (; fProcess->isRunning() && ! shouldThreadExit();)
  354. carla_sleep(1);
  355. // we only get here if bridge crashed or thread asked to exit
  356. if (fProcess->isRunning() && shouldThreadExit())
  357. {
  358. fProcess->waitForProcessToFinish(2000);
  359. if (fProcess->isRunning())
  360. {
  361. carla_stdout("CarlaPluginBridgeThread::run() - bridge refused to close, force kill now");
  362. fProcess->kill();
  363. }
  364. else
  365. {
  366. carla_stdout("CarlaPluginBridgeThread::run() - bridge auto-closed successfully");
  367. }
  368. }
  369. else
  370. {
  371. // forced quit, may have crashed
  372. if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  373. {
  374. carla_stderr("CarlaPluginBridgeThread::run() - bridge crashed");
  375. CarlaString errorString("Plugin '" + CarlaString(kPlugin->getName()) + "' has crashed!\n"
  376. "Saving now will lose its current settings.\n"
  377. "Please remove this plugin, and not rely on it from this point.");
  378. kEngine->callback(CarlaBackend::ENGINE_CALLBACK_ERROR, kPlugin->getId(), 0, 0, 0.0f, errorString);
  379. }
  380. }
  381. fProcess = nullptr;
  382. }
  383. private:
  384. CarlaEngine* const kEngine;
  385. CarlaPlugin* const kPlugin;
  386. String fBinary;
  387. String fLabel;
  388. String fShmIds;
  389. ScopedPointer<ChildProcess> fProcess;
  390. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridgeThread)
  391. };
  392. // -------------------------------------------------------------------------------------------------------------------
  393. class CarlaPluginBridge : public CarlaPlugin
  394. {
  395. public:
  396. CarlaPluginBridge(CarlaEngine* const engine, const uint id, const BinaryType btype, const PluginType ptype)
  397. : CarlaPlugin(engine, id),
  398. fBinaryType(btype),
  399. fPluginType(ptype),
  400. fInitiated(false),
  401. fInitError(false),
  402. fSaved(true),
  403. fTimedOut(false),
  404. fTimedError(false),
  405. fProcWaitTime(0),
  406. fLastPongTime(-1),
  407. fBridgeBinary(),
  408. fBridgeThread(engine, this),
  409. fShmAudioPool(),
  410. fShmRtClientControl(),
  411. fShmNonRtClientControl(),
  412. fShmNonRtServerControl(),
  413. fInfo(),
  414. fUniqueId(0),
  415. fLatency(0),
  416. fParams(nullptr)
  417. {
  418. carla_debug("CarlaPluginBridge::CarlaPluginBridge(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
  419. pData->hints |= PLUGIN_IS_BRIDGE;
  420. }
  421. ~CarlaPluginBridge() override
  422. {
  423. carla_debug("CarlaPluginBridge::~CarlaPluginBridge()");
  424. // close UI
  425. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  426. pData->transientTryCounter = 0;
  427. pData->singleMutex.lock();
  428. pData->masterMutex.lock();
  429. if (pData->client != nullptr && pData->client->isActive())
  430. pData->client->deactivate();
  431. if (pData->active)
  432. {
  433. deactivate();
  434. pData->active = false;
  435. }
  436. if (fBridgeThread.isThreadRunning())
  437. {
  438. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientQuit);
  439. fShmNonRtClientControl.commitWrite();
  440. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientQuit);
  441. fShmRtClientControl.commitWrite();
  442. if (! fTimedOut)
  443. waitForClient("stopping", 3000);
  444. }
  445. fBridgeThread.stopThread(3000);
  446. fShmNonRtServerControl.clear();
  447. fShmNonRtClientControl.clear();
  448. fShmRtClientControl.clear();
  449. fShmAudioPool.clear();
  450. clearBuffers();
  451. fInfo.chunk.clear();
  452. }
  453. // -------------------------------------------------------------------
  454. // Information (base)
  455. BinaryType getBinaryType() const noexcept override
  456. {
  457. return fBinaryType;
  458. }
  459. PluginType getType() const noexcept override
  460. {
  461. return fPluginType;
  462. }
  463. PluginCategory getCategory() const noexcept override
  464. {
  465. return fInfo.category;
  466. }
  467. int64_t getUniqueId() const noexcept override
  468. {
  469. return fUniqueId;
  470. }
  471. uint32_t getLatencyInFrames() const noexcept override
  472. {
  473. return fLatency;
  474. }
  475. // -------------------------------------------------------------------
  476. // Information (count)
  477. uint32_t getMidiInCount() const noexcept override
  478. {
  479. return fInfo.mIns;
  480. }
  481. uint32_t getMidiOutCount() const noexcept override
  482. {
  483. return fInfo.mOuts;
  484. }
  485. // -------------------------------------------------------------------
  486. // Information (current data)
  487. // TODO - missing getCustomData
  488. std::size_t getChunkData(void** const dataPtr) noexcept override
  489. {
  490. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  491. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  492. waitForSaved();
  493. CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.size() > 0, 0);
  494. *dataPtr = fInfo.chunk.data();
  495. return fInfo.chunk.size();
  496. }
  497. // -------------------------------------------------------------------
  498. // Information (per-plugin data)
  499. uint getOptionsAvailable() const noexcept override
  500. {
  501. return fInfo.optionsAvailable;
  502. }
  503. float getParameterValue(const uint32_t parameterId) const noexcept override
  504. {
  505. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  506. return fParams[parameterId].value;
  507. }
  508. void getLabel(char* const strBuf) const noexcept override
  509. {
  510. std::strncpy(strBuf, fInfo.label, STR_MAX);
  511. }
  512. void getMaker(char* const strBuf) const noexcept override
  513. {
  514. std::strncpy(strBuf, fInfo.maker, STR_MAX);
  515. }
  516. void getCopyright(char* const strBuf) const noexcept override
  517. {
  518. std::strncpy(strBuf, fInfo.copyright, STR_MAX);
  519. }
  520. void getRealName(char* const strBuf) const noexcept override
  521. {
  522. std::strncpy(strBuf, fInfo.name, STR_MAX);
  523. }
  524. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  525. {
  526. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  527. std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
  528. }
  529. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  530. {
  531. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  532. std::strncpy(strBuf, fParams[parameterId].symbol.buffer(), STR_MAX);
  533. }
  534. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  535. {
  536. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  537. std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
  538. }
  539. // -------------------------------------------------------------------
  540. // Set data (state)
  541. void prepareForSave() noexcept override
  542. {
  543. fSaved = false;
  544. {
  545. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  546. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPrepareForSave);
  547. fShmNonRtClientControl.commitWrite();
  548. }
  549. }
  550. void waitForSaved()
  551. {
  552. if (fSaved)
  553. return;
  554. // TODO: only wait 1 minute for NI plugins
  555. const uint32_t timeoutEnd(Time::getMillisecondCounter() + 60*1000); // 60 secs, 1 minute
  556. const bool needsEngineIdle(pData->engine->getType() != kEngineTypePlugin);
  557. for (; Time::getMillisecondCounter() < timeoutEnd && fBridgeThread.isThreadRunning();)
  558. {
  559. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  560. if (needsEngineIdle)
  561. pData->engine->idle();
  562. if (fSaved)
  563. break;
  564. carla_msleep(20);
  565. }
  566. if (! fSaved)
  567. carla_stderr("CarlaPluginBridge::waitForSaved() - Timeout while requesting save state");
  568. }
  569. // -------------------------------------------------------------------
  570. // Set data (internal stuff)
  571. void setOption(const uint option, const bool yesNo, const bool sendCallback) override
  572. {
  573. {
  574. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  575. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOption);
  576. fShmNonRtClientControl.writeUInt(option);
  577. fShmNonRtClientControl.writeBool(yesNo);
  578. fShmNonRtClientControl.commitWrite();
  579. }
  580. CarlaPlugin::setOption(option, yesNo, sendCallback);
  581. }
  582. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  583. {
  584. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  585. {
  586. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  587. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCtrlChannel);
  588. fShmNonRtClientControl.writeShort(channel);
  589. fShmNonRtClientControl.commitWrite();
  590. }
  591. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  592. }
  593. // -------------------------------------------------------------------
  594. // Set data (plugin-specific stuff)
  595. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  596. {
  597. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  598. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  599. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  600. fParams[parameterId].value = fixedValue;
  601. {
  602. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  603. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
  604. fShmNonRtClientControl.writeUInt(parameterId);
  605. fShmNonRtClientControl.writeFloat(value);
  606. fShmNonRtClientControl.commitWrite();
  607. fShmNonRtClientControl.waitIfDataIsReachingLimit();
  608. }
  609. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  610. }
  611. void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  612. {
  613. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  614. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  615. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  616. {
  617. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  618. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMidiChannel);
  619. fShmNonRtClientControl.writeUInt(parameterId);
  620. fShmNonRtClientControl.writeByte(channel);
  621. fShmNonRtClientControl.commitWrite();
  622. }
  623. CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
  624. }
  625. void setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept override
  626. {
  627. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  628. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  629. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  630. {
  631. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  632. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMidiCC);
  633. fShmNonRtClientControl.writeUInt(parameterId);
  634. fShmNonRtClientControl.writeShort(cc);
  635. fShmNonRtClientControl.commitWrite();
  636. }
  637. CarlaPlugin::setParameterMidiCC(parameterId, cc, sendOsc, sendCallback);
  638. }
  639. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  640. {
  641. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  642. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  643. {
  644. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  645. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
  646. fShmNonRtClientControl.writeInt(index);
  647. fShmNonRtClientControl.commitWrite();
  648. }
  649. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  650. }
  651. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  652. {
  653. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  654. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  655. {
  656. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  657. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
  658. fShmNonRtClientControl.writeInt(index);
  659. fShmNonRtClientControl.commitWrite();
  660. }
  661. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  662. }
  663. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  664. {
  665. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  666. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  667. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  668. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  669. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  670. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "__CarlaPingOnOff__") == 0)
  671. {
  672. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  673. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPingOnOff);
  674. fShmNonRtClientControl.writeBool(std::strcmp(value, "true") == 0);
  675. fShmNonRtClientControl.commitWrite();
  676. return;
  677. }
  678. const uint32_t typeLen(static_cast<uint32_t>(std::strlen(type)));
  679. const uint32_t keyLen(static_cast<uint32_t>(std::strlen(key)));
  680. const uint32_t valueLen(static_cast<uint32_t>(std::strlen(value)));
  681. {
  682. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  683. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCustomData);
  684. fShmNonRtClientControl.writeUInt(typeLen);
  685. fShmNonRtClientControl.writeCustomData(type, typeLen);
  686. fShmNonRtClientControl.writeUInt(keyLen);
  687. fShmNonRtClientControl.writeCustomData(key, keyLen);
  688. fShmNonRtClientControl.writeUInt(valueLen);
  689. fShmNonRtClientControl.writeCustomData(value, valueLen);
  690. fShmNonRtClientControl.commitWrite();
  691. }
  692. CarlaPlugin::setCustomData(type, key, value, sendGui);
  693. }
  694. void setChunkData(const void* const data, const std::size_t dataSize) override
  695. {
  696. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  697. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  698. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  699. CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
  700. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0,);
  701. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  702. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  703. filePath += fShmAudioPool.filename.buffer() + 18;
  704. if (File(filePath).replaceWithText(dataBase64.buffer()))
  705. {
  706. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  707. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  708. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
  709. fShmNonRtClientControl.writeUInt(ulength);
  710. fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
  711. fShmNonRtClientControl.commitWrite();
  712. }
  713. // save data internally as well
  714. fInfo.chunk.resize(dataSize);
  715. std::memcpy(fInfo.chunk.data(), data, dataSize);
  716. }
  717. // -------------------------------------------------------------------
  718. // Set ui stuff
  719. void showCustomUI(const bool yesNo) override
  720. {
  721. {
  722. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  723. fShmNonRtClientControl.writeOpcode(yesNo ? kPluginBridgeNonRtClientShowUI : kPluginBridgeNonRtClientHideUI);
  724. fShmNonRtClientControl.commitWrite();
  725. }
  726. #ifndef BUILD_BRIDGE
  727. if (yesNo)
  728. {
  729. pData->tryTransient();
  730. }
  731. else
  732. {
  733. pData->transientTryCounter = 0;
  734. }
  735. #endif
  736. }
  737. void idle() override
  738. {
  739. if (fBridgeThread.isThreadRunning())
  740. {
  741. if (fInitiated && fTimedOut && pData->active)
  742. setActive(false, true, true);
  743. {
  744. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  745. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPing);
  746. fShmNonRtClientControl.commitWrite();
  747. }
  748. try {
  749. handleNonRtData();
  750. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  751. }
  752. else if (fInitiated)
  753. {
  754. fTimedOut = true;
  755. fTimedError = true;
  756. fInitiated = false;
  757. pData->engine->callback(ENGINE_CALLBACK_PLUGIN_UNAVAILABLE, pData->id, 0, 0, 0.0f,
  758. "Plugin bridge has been stopped or crashed");
  759. }
  760. CarlaPlugin::idle();
  761. }
  762. // -------------------------------------------------------------------
  763. // Plugin state
  764. void reload() override
  765. {
  766. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  767. carla_debug("CarlaPluginBridge::reload() - start");
  768. const EngineProcessMode processMode(pData->engine->getProccessMode());
  769. // Safely disable plugin for reload
  770. const ScopedDisabler sd(this);
  771. // cleanup of previous data
  772. pData->audioIn.clear();
  773. pData->audioOut.clear();
  774. pData->cvIn.clear();
  775. pData->cvOut.clear();
  776. pData->event.clear();
  777. bool needsCtrlIn, needsCtrlOut;
  778. needsCtrlIn = needsCtrlOut = false;
  779. if (fInfo.aIns > 0)
  780. {
  781. pData->audioIn.createNew(fInfo.aIns);
  782. }
  783. if (fInfo.aOuts > 0)
  784. {
  785. pData->audioOut.createNew(fInfo.aOuts);
  786. needsCtrlIn = true;
  787. }
  788. if (fInfo.cvIns > 0)
  789. {
  790. pData->cvIn.createNew(fInfo.cvIns);
  791. }
  792. if (fInfo.cvOuts > 0)
  793. {
  794. pData->cvOut.createNew(fInfo.cvOuts);
  795. }
  796. if (fInfo.mIns > 0)
  797. needsCtrlIn = true;
  798. if (fInfo.mOuts > 0)
  799. needsCtrlOut = true;
  800. const uint portNameSize(pData->engine->getMaxPortNameSize());
  801. CarlaString portName;
  802. // Audio Ins
  803. for (uint32_t j=0; j < fInfo.aIns; ++j)
  804. {
  805. portName.clear();
  806. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  807. {
  808. portName = pData->name;
  809. portName += ":";
  810. }
  811. if (fInfo.aInNames != nullptr && fInfo.aInNames[j] != nullptr)
  812. {
  813. portName += fInfo.aInNames[j];
  814. }
  815. else if (fInfo.aIns > 1)
  816. {
  817. portName += "input_";
  818. portName += CarlaString(j+1);
  819. }
  820. else
  821. portName += "input";
  822. portName.truncate(portNameSize);
  823. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  824. pData->audioIn.ports[j].rindex = j;
  825. }
  826. // Audio Outs
  827. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  828. {
  829. portName.clear();
  830. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  831. {
  832. portName = pData->name;
  833. portName += ":";
  834. }
  835. if (fInfo.aOutNames != nullptr && fInfo.aOutNames[j] != nullptr)
  836. {
  837. portName += fInfo.aOutNames[j];
  838. }
  839. else if (fInfo.aOuts > 1)
  840. {
  841. portName += "output_";
  842. portName += CarlaString(j+1);
  843. }
  844. else
  845. portName += "output";
  846. portName.truncate(portNameSize);
  847. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  848. pData->audioOut.ports[j].rindex = j;
  849. }
  850. // TODO - MIDI
  851. // TODO - CV
  852. if (needsCtrlIn)
  853. {
  854. portName.clear();
  855. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  856. {
  857. portName = pData->name;
  858. portName += ":";
  859. }
  860. portName += "event-in";
  861. portName.truncate(portNameSize);
  862. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  863. }
  864. if (needsCtrlOut)
  865. {
  866. portName.clear();
  867. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  868. {
  869. portName = pData->name;
  870. portName += ":";
  871. }
  872. portName += "event-out";
  873. portName.truncate(portNameSize);
  874. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  875. }
  876. // extra plugin hints
  877. pData->extraHints = 0x0;
  878. if (fInfo.mIns > 0)
  879. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  880. if (fInfo.mOuts > 0)
  881. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  882. if (fInfo.aIns <= 2 && fInfo.aOuts <= 2 && (fInfo.aIns == fInfo.aOuts || fInfo.aIns == 0 || fInfo.aOuts == 0))
  883. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  884. bufferSizeChanged(pData->engine->getBufferSize());
  885. reloadPrograms(true);
  886. carla_debug("CarlaPluginBridge::reload() - end");
  887. }
  888. // -------------------------------------------------------------------
  889. // Plugin processing
  890. void activate() noexcept override
  891. {
  892. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  893. {
  894. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  895. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientActivate);
  896. fShmNonRtClientControl.commitWrite();
  897. }
  898. fTimedOut = false;
  899. try {
  900. waitForClient("activate", 2000);
  901. } CARLA_SAFE_EXCEPTION("activate - waitForClient");
  902. }
  903. void deactivate() noexcept override
  904. {
  905. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  906. {
  907. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  908. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientDeactivate);
  909. fShmNonRtClientControl.commitWrite();
  910. }
  911. fTimedOut = false;
  912. try {
  913. waitForClient("deactivate", 2000);
  914. } CARLA_SAFE_EXCEPTION("deactivate - waitForClient");
  915. }
  916. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  917. {
  918. // --------------------------------------------------------------------------------------------------------
  919. // Check if active
  920. if (fTimedOut || fTimedError || ! pData->active)
  921. {
  922. // disable any output sound
  923. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  924. FloatVectorOperations::clear(audioOut[i], static_cast<int>(frames));
  925. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  926. FloatVectorOperations::clear(cvOut[i], static_cast<int>(frames));
  927. return;
  928. }
  929. // --------------------------------------------------------------------------------------------------------
  930. // Check if needs reset
  931. if (pData->needsReset)
  932. {
  933. // TODO
  934. pData->needsReset = false;
  935. }
  936. // --------------------------------------------------------------------------------------------------------
  937. // Event Input
  938. if (pData->event.portIn != nullptr)
  939. {
  940. // ----------------------------------------------------------------------------------------------------
  941. // MIDI Input (External)
  942. if (pData->extNotes.mutex.tryLock())
  943. {
  944. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  945. {
  946. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  947. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  948. uint8_t data1, data2, data3;
  949. data1 = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  950. data2 = note.note;
  951. data3 = note.velo;
  952. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  953. fShmRtClientControl.writeUInt(0); // time
  954. fShmRtClientControl.writeByte(0); // port
  955. fShmRtClientControl.writeByte(3); // size
  956. fShmRtClientControl.writeByte(data1);
  957. fShmRtClientControl.writeByte(data2);
  958. fShmRtClientControl.writeByte(data3);
  959. fShmRtClientControl.commitWrite();
  960. }
  961. pData->extNotes.data.clear();
  962. pData->extNotes.mutex.unlock();
  963. } // End of MIDI Input (External)
  964. // ----------------------------------------------------------------------------------------------------
  965. // Event Input (System)
  966. #ifndef BUILD_BRIDGE
  967. bool allNotesOffSent = false;
  968. #endif
  969. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  970. {
  971. const EngineEvent& event(pData->event.portIn->getEvent(i));
  972. // Control change
  973. switch (event.type)
  974. {
  975. case kEngineEventTypeNull:
  976. break;
  977. case kEngineEventTypeControl: {
  978. const EngineControlEvent& ctrlEvent = event.ctrl;
  979. switch (ctrlEvent.type)
  980. {
  981. case kEngineControlEventTypeNull:
  982. break;
  983. case kEngineControlEventTypeParameter:
  984. #ifndef BUILD_BRIDGE
  985. // Control backend stuff
  986. if (event.channel == pData->ctrlChannel)
  987. {
  988. float value;
  989. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  990. {
  991. value = ctrlEvent.value;
  992. setDryWet(value, false, false);
  993. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  994. }
  995. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  996. {
  997. value = ctrlEvent.value*127.0f/100.0f;
  998. setVolume(value, false, false);
  999. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1000. }
  1001. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1002. {
  1003. float left, right;
  1004. value = ctrlEvent.value/0.5f - 1.0f;
  1005. if (value < 0.0f)
  1006. {
  1007. left = -1.0f;
  1008. right = (value*2.0f)+1.0f;
  1009. }
  1010. else if (value > 0.0f)
  1011. {
  1012. left = (value*2.0f)-1.0f;
  1013. right = 1.0f;
  1014. }
  1015. else
  1016. {
  1017. left = -1.0f;
  1018. right = 1.0f;
  1019. }
  1020. setBalanceLeft(left, false, false);
  1021. setBalanceRight(right, false, false);
  1022. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1023. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1024. }
  1025. }
  1026. #endif
  1027. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventParameter);
  1028. fShmRtClientControl.writeUInt(event.time);
  1029. fShmRtClientControl.writeByte(event.channel);
  1030. fShmRtClientControl.writeUShort(event.ctrl.param);
  1031. fShmRtClientControl.writeFloat(event.ctrl.value);
  1032. fShmRtClientControl.commitWrite();
  1033. break;
  1034. case kEngineControlEventTypeMidiBank:
  1035. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1036. {
  1037. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiBank);
  1038. fShmRtClientControl.writeUInt(event.time);
  1039. fShmRtClientControl.writeByte(event.channel);
  1040. fShmRtClientControl.writeUShort(event.ctrl.param);
  1041. fShmRtClientControl.commitWrite();
  1042. }
  1043. break;
  1044. case kEngineControlEventTypeMidiProgram:
  1045. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1046. {
  1047. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiProgram);
  1048. fShmRtClientControl.writeUInt(event.time);
  1049. fShmRtClientControl.writeByte(event.channel);
  1050. fShmRtClientControl.writeUShort(event.ctrl.param);
  1051. fShmRtClientControl.commitWrite();
  1052. }
  1053. break;
  1054. case kEngineControlEventTypeAllSoundOff:
  1055. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1056. {
  1057. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllSoundOff);
  1058. fShmRtClientControl.writeUInt(event.time);
  1059. fShmRtClientControl.writeByte(event.channel);
  1060. fShmRtClientControl.commitWrite();
  1061. }
  1062. break;
  1063. case kEngineControlEventTypeAllNotesOff:
  1064. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1065. {
  1066. #ifndef BUILD_BRIDGE
  1067. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1068. {
  1069. allNotesOffSent = true;
  1070. sendMidiAllNotesOffToCallback();
  1071. }
  1072. #endif
  1073. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllNotesOff);
  1074. fShmRtClientControl.writeUInt(event.time);
  1075. fShmRtClientControl.writeByte(event.channel);
  1076. fShmRtClientControl.commitWrite();
  1077. }
  1078. break;
  1079. } // switch (ctrlEvent.type)
  1080. break;
  1081. } // case kEngineEventTypeControl
  1082. case kEngineEventTypeMidi: {
  1083. const EngineMidiEvent& midiEvent(event.midi);
  1084. if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
  1085. continue;
  1086. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  1087. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  1088. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1089. continue;
  1090. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1091. continue;
  1092. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1093. continue;
  1094. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1095. continue;
  1096. // Fix bad note-off
  1097. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  1098. status = MIDI_STATUS_NOTE_OFF;
  1099. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1100. fShmRtClientControl.writeUInt(event.time);
  1101. fShmRtClientControl.writeByte(midiEvent.port);
  1102. fShmRtClientControl.writeByte(midiEvent.size);
  1103. fShmRtClientControl.writeByte(uint8_t(midiData[0] | (event.channel & MIDI_CHANNEL_BIT)));
  1104. for (uint8_t j=1; j < midiEvent.size; ++j)
  1105. fShmRtClientControl.writeByte(midiData[j]);
  1106. fShmRtClientControl.commitWrite();
  1107. if (status == MIDI_STATUS_NOTE_ON)
  1108. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiData[1], midiData[2]);
  1109. else if (status == MIDI_STATUS_NOTE_OFF)
  1110. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiData[1], 0.0f);
  1111. } break;
  1112. }
  1113. }
  1114. pData->postRtEvents.trySplice();
  1115. } // End of Event Input
  1116. if (! processSingle(audioIn, audioOut, cvIn, cvOut, frames))
  1117. return;
  1118. // --------------------------------------------------------------------------------------------------------
  1119. // Control and MIDI Output
  1120. if (pData->event.portOut != nullptr)
  1121. {
  1122. float value;
  1123. for (uint32_t k=0; k < pData->param.count; ++k)
  1124. {
  1125. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1126. continue;
  1127. if (pData->param.data[k].midiCC > 0)
  1128. {
  1129. value = pData->param.ranges[k].getNormalizedValue(fParams[k].value);
  1130. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1131. }
  1132. }
  1133. uint8_t size;
  1134. uint32_t time;
  1135. const uint8_t* midiData(fShmRtClientControl.data->midiOut);
  1136. for (std::size_t read=0; read<kBridgeRtClientDataMidiOutSize;)
  1137. {
  1138. size = *midiData;
  1139. if (size == 0)
  1140. break;
  1141. // advance 8 bits (1 byte)
  1142. midiData = midiData + 1;
  1143. // get time as 32bit
  1144. time = *(const uint32_t*)midiData;
  1145. // advance 32 bits (4 bytes)
  1146. midiData = midiData + 4;
  1147. // store midi data advancing as needed
  1148. uint8_t data[size];
  1149. for (uint8_t j=0; j<size; ++j)
  1150. data[j] = *midiData++;
  1151. pData->event.portOut->writeMidiEvent(time, size, data);
  1152. read += 1U /* size*/ + 4U /* time */ + size;
  1153. }
  1154. } // End of Control and MIDI Output
  1155. }
  1156. bool processSingle(const float** const audioIn, float** const audioOut,
  1157. const float** const cvIn, float** const cvOut, const uint32_t frames)
  1158. {
  1159. CARLA_SAFE_ASSERT_RETURN(! fTimedError, false);
  1160. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1161. if (pData->audioIn.count > 0)
  1162. {
  1163. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1164. }
  1165. if (pData->audioOut.count > 0)
  1166. {
  1167. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1168. }
  1169. if (pData->cvIn.count > 0)
  1170. {
  1171. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1172. }
  1173. if (pData->cvOut.count > 0)
  1174. {
  1175. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1176. }
  1177. const int iframes(static_cast<int>(frames));
  1178. // --------------------------------------------------------------------------------------------------------
  1179. // Try lock, silence otherwise
  1180. if (pData->engine->isOffline())
  1181. {
  1182. pData->singleMutex.lock();
  1183. }
  1184. else if (! pData->singleMutex.tryLock())
  1185. {
  1186. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1187. FloatVectorOperations::clear(audioOut[i], iframes);
  1188. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1189. FloatVectorOperations::clear(cvOut[i], iframes);
  1190. return false;
  1191. }
  1192. // --------------------------------------------------------------------------------------------------------
  1193. // Reset audio buffers
  1194. for (uint32_t i=0; i < fInfo.aIns; ++i)
  1195. FloatVectorOperations::copy(fShmAudioPool.data + (i * frames), audioIn[i], iframes);
  1196. // --------------------------------------------------------------------------------------------------------
  1197. // TimeInfo
  1198. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1199. BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  1200. bridgeTimeInfo.playing = timeInfo.playing;
  1201. bridgeTimeInfo.frame = timeInfo.frame;
  1202. bridgeTimeInfo.usecs = timeInfo.usecs;
  1203. bridgeTimeInfo.valid = timeInfo.valid;
  1204. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1205. {
  1206. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  1207. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  1208. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  1209. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1210. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  1211. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1212. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1213. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  1214. }
  1215. // --------------------------------------------------------------------------------------------------------
  1216. // Run plugin
  1217. {
  1218. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientProcess);
  1219. fShmRtClientControl.commitWrite();
  1220. }
  1221. waitForClient("process", fProcWaitTime);
  1222. if (fTimedOut)
  1223. {
  1224. pData->singleMutex.unlock();
  1225. return false;
  1226. }
  1227. for (uint32_t i=0; i < fInfo.aOuts; ++i)
  1228. FloatVectorOperations::copy(audioOut[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), iframes);
  1229. #ifndef BUILD_BRIDGE
  1230. // --------------------------------------------------------------------------------------------------------
  1231. // Post-processing (dry/wet, volume and balance)
  1232. {
  1233. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1234. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1235. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1236. const bool isMono = (pData->audioIn.count == 1);
  1237. bool isPair;
  1238. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1239. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1240. {
  1241. // Dry/Wet
  1242. if (doDryWet)
  1243. {
  1244. const uint32_t c = isMono ? 0 : i;
  1245. for (uint32_t k=0; k < frames; ++k)
  1246. {
  1247. if (k < pData->latency.frames)
  1248. bufValue = pData->latency.buffers[c][k];
  1249. else if (pData->latency.frames < frames)
  1250. bufValue = audioIn[c][k-pData->latency.frames];
  1251. else
  1252. bufValue = audioIn[c][k];
  1253. audioOut[i][k] = (audioOut[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1254. }
  1255. }
  1256. // Balance
  1257. if (doBalance)
  1258. {
  1259. isPair = (i % 2 == 0);
  1260. if (isPair)
  1261. {
  1262. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1263. FloatVectorOperations::copy(oldBufLeft, audioOut[i], iframes);
  1264. }
  1265. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1266. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1267. for (uint32_t k=0; k < frames; ++k)
  1268. {
  1269. if (isPair)
  1270. {
  1271. // left
  1272. audioOut[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1273. audioOut[i][k] += audioOut[i+1][k] * (1.0f - balRangeR);
  1274. }
  1275. else
  1276. {
  1277. // right
  1278. audioOut[i][k] = audioOut[i][k] * balRangeR;
  1279. audioOut[i][k] += oldBufLeft[k] * balRangeL;
  1280. }
  1281. }
  1282. }
  1283. // Volume (and buffer copy)
  1284. if (doVolume)
  1285. {
  1286. for (uint32_t k=0; k < frames; ++k)
  1287. audioOut[i][k] *= pData->postProc.volume;
  1288. }
  1289. }
  1290. } // End of Post-processing
  1291. // --------------------------------------------------------------------------------------------------------
  1292. // Save latency values for next callback
  1293. if (const uint32_t latframes = pData->latency.frames)
  1294. {
  1295. if (latframes <= frames)
  1296. {
  1297. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1298. FloatVectorOperations::copy(pData->latency.buffers[i], audioIn[i]+(frames-latframes), static_cast<int>(latframes));
  1299. }
  1300. else
  1301. {
  1302. const uint32_t diff = pData->latency.frames-frames;
  1303. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1304. {
  1305. // push back buffer by 'frames'
  1306. for (k=0; k < diff; ++k)
  1307. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1308. // put current input at the end
  1309. for (uint32_t j=0; k < latframes; ++j, ++k)
  1310. pData->latency.buffers[i][k] = audioIn[i][j];
  1311. }
  1312. }
  1313. }
  1314. #endif // BUILD_BRIDGE
  1315. // --------------------------------------------------------------------------------------------------------
  1316. pData->singleMutex.unlock();
  1317. return true;
  1318. }
  1319. void bufferSizeChanged(const uint32_t newBufferSize) override
  1320. {
  1321. resizeAudioPool(newBufferSize);
  1322. {
  1323. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1324. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetBufferSize);
  1325. fShmNonRtClientControl.writeUInt(newBufferSize);
  1326. fShmNonRtClientControl.commitWrite();
  1327. }
  1328. //fProcWaitTime = newBufferSize*1000/pData->engine->getSampleRate();
  1329. fProcWaitTime = 1000;
  1330. waitForClient("buffersize", 1000);
  1331. }
  1332. void sampleRateChanged(const double newSampleRate) override
  1333. {
  1334. {
  1335. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1336. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetSampleRate);
  1337. fShmNonRtClientControl.writeDouble(newSampleRate);
  1338. fShmNonRtClientControl.commitWrite();
  1339. }
  1340. //fProcWaitTime = pData->engine->getBufferSize()*1000/newSampleRate;
  1341. fProcWaitTime = 1000;
  1342. waitForClient("samplerate", 1000);
  1343. }
  1344. void offlineModeChanged(const bool isOffline) override
  1345. {
  1346. {
  1347. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1348. fShmNonRtClientControl.writeOpcode(isOffline ? kPluginBridgeNonRtClientSetOffline : kPluginBridgeNonRtClientSetOnline);
  1349. fShmNonRtClientControl.commitWrite();
  1350. }
  1351. waitForClient("offline", 1000);
  1352. }
  1353. // -------------------------------------------------------------------
  1354. // Plugin buffers
  1355. void clearBuffers() noexcept override
  1356. {
  1357. if (fParams != nullptr)
  1358. {
  1359. delete[] fParams;
  1360. fParams = nullptr;
  1361. }
  1362. CarlaPlugin::clearBuffers();
  1363. }
  1364. // -------------------------------------------------------------------
  1365. // Post-poned UI Stuff
  1366. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1367. {
  1368. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1369. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1370. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiParameterChange);
  1371. fShmNonRtClientControl.writeUInt(index);
  1372. fShmNonRtClientControl.writeFloat(value);
  1373. fShmNonRtClientControl.commitWrite();
  1374. }
  1375. void uiProgramChange(const uint32_t index) noexcept override
  1376. {
  1377. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1378. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1379. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiProgramChange);
  1380. fShmNonRtClientControl.writeUInt(index);
  1381. fShmNonRtClientControl.commitWrite();
  1382. }
  1383. void uiMidiProgramChange(const uint32_t index) noexcept override
  1384. {
  1385. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1386. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1387. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiMidiProgramChange);
  1388. fShmNonRtClientControl.writeUInt(index);
  1389. fShmNonRtClientControl.commitWrite();
  1390. }
  1391. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1392. {
  1393. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1394. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1395. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1396. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1397. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOn);
  1398. fShmNonRtClientControl.writeByte(channel);
  1399. fShmNonRtClientControl.writeByte(note);
  1400. fShmNonRtClientControl.writeByte(velo);
  1401. fShmNonRtClientControl.commitWrite();
  1402. }
  1403. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1404. {
  1405. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1406. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1407. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1408. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOff);
  1409. fShmNonRtClientControl.writeByte(channel);
  1410. fShmNonRtClientControl.writeByte(note);
  1411. fShmNonRtClientControl.commitWrite();
  1412. }
  1413. // -------------------------------------------------------------------
  1414. void handleNonRtData()
  1415. {
  1416. for (; fShmNonRtServerControl.isDataAvailableForReading();)
  1417. {
  1418. const PluginBridgeNonRtServerOpcode opcode(fShmNonRtServerControl.readOpcode());
  1419. #ifdef DEBUG
  1420. if (opcode != kPluginBridgeNonRtServerPong) {
  1421. carla_debug("CarlaPluginBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtServerOpcode2str(opcode));
  1422. }
  1423. #endif
  1424. if (opcode != kPluginBridgeNonRtServerNull && fLastPongTime > 0)
  1425. fLastPongTime = Time::currentTimeMillis();
  1426. switch (opcode)
  1427. {
  1428. case kPluginBridgeNonRtServerNull:
  1429. case kPluginBridgeNonRtServerPong:
  1430. break;
  1431. case kPluginBridgeNonRtServerPluginInfo1: {
  1432. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  1433. const uint32_t category = fShmNonRtServerControl.readUInt();
  1434. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1435. const uint32_t optionAv = fShmNonRtServerControl.readUInt();
  1436. const uint32_t optionEn = fShmNonRtServerControl.readUInt();
  1437. const int64_t uniqueId = fShmNonRtServerControl.readLong();
  1438. CARLA_SAFE_ASSERT_INT2(fUniqueId == uniqueId, fUniqueId, uniqueId);
  1439. pData->hints = hints | PLUGIN_IS_BRIDGE;
  1440. pData->options = optionEn;
  1441. fInfo.category = static_cast<PluginCategory>(category);
  1442. fInfo.optionsAvailable = optionAv;
  1443. } break;
  1444. case kPluginBridgeNonRtServerPluginInfo2: {
  1445. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  1446. // realName
  1447. const uint32_t realNameSize(fShmNonRtServerControl.readUInt());
  1448. char realName[realNameSize+1];
  1449. carla_zeroChars(realName, realNameSize+1);
  1450. fShmNonRtServerControl.readCustomData(realName, realNameSize);
  1451. // label
  1452. const uint32_t labelSize(fShmNonRtServerControl.readUInt());
  1453. char label[labelSize+1];
  1454. carla_zeroChars(label, labelSize+1);
  1455. fShmNonRtServerControl.readCustomData(label, labelSize);
  1456. // maker
  1457. const uint32_t makerSize(fShmNonRtServerControl.readUInt());
  1458. char maker[makerSize+1];
  1459. carla_zeroChars(maker, makerSize+1);
  1460. fShmNonRtServerControl.readCustomData(maker, makerSize);
  1461. // copyright
  1462. const uint32_t copyrightSize(fShmNonRtServerControl.readUInt());
  1463. char copyright[copyrightSize+1];
  1464. carla_zeroChars(copyright, copyrightSize+1);
  1465. fShmNonRtServerControl.readCustomData(copyright, copyrightSize);
  1466. fInfo.name = realName;
  1467. fInfo.label = label;
  1468. fInfo.maker = maker;
  1469. fInfo.copyright = copyright;
  1470. if (pData->name == nullptr)
  1471. pData->name = pData->engine->getUniquePluginName(realName);
  1472. } break;
  1473. case kPluginBridgeNonRtServerAudioCount: {
  1474. // uint/ins, uint/outs
  1475. fInfo.aIns = fShmNonRtServerControl.readUInt();
  1476. fInfo.aOuts = fShmNonRtServerControl.readUInt();
  1477. CARLA_SAFE_ASSERT(fInfo.aInNames == nullptr);
  1478. CARLA_SAFE_ASSERT(fInfo.aOutNames == nullptr);
  1479. if (fInfo.aIns > 0)
  1480. {
  1481. fInfo.aInNames = new const char*[fInfo.aIns];
  1482. carla_zeroPointers(fInfo.aInNames, fInfo.aIns);
  1483. }
  1484. if (fInfo.aOuts > 0)
  1485. {
  1486. fInfo.aOutNames = new const char*[fInfo.aOuts];
  1487. carla_zeroPointers(fInfo.aOutNames, fInfo.aOuts);
  1488. }
  1489. } break;
  1490. case kPluginBridgeNonRtServerMidiCount: {
  1491. // uint/ins, uint/outs
  1492. fInfo.mIns = fShmNonRtServerControl.readUInt();
  1493. fInfo.mOuts = fShmNonRtServerControl.readUInt();
  1494. } break;
  1495. case kPluginBridgeNonRtServerCvCount: {
  1496. // uint/ins, uint/outs
  1497. fInfo.cvIns = fShmNonRtServerControl.readUInt();
  1498. fInfo.cvOuts = fShmNonRtServerControl.readUInt();
  1499. } break;
  1500. case kPluginBridgeNonRtServerParameterCount: {
  1501. // uint/count
  1502. const uint32_t count = fShmNonRtServerControl.readUInt();
  1503. // delete old data
  1504. pData->param.clear();
  1505. if (fParams != nullptr)
  1506. {
  1507. delete[] fParams;
  1508. fParams = nullptr;
  1509. }
  1510. if (count > 0)
  1511. {
  1512. pData->param.createNew(count, false);
  1513. fParams = new BridgeParamInfo[count];
  1514. // we might not receive all parameter data, so ensure range max is not 0
  1515. for (uint32_t i=0; i<count; ++i)
  1516. {
  1517. pData->param.ranges[i].def = 0.0f;
  1518. pData->param.ranges[i].min = 0.0f;
  1519. pData->param.ranges[i].max = 1.0f;
  1520. pData->param.ranges[i].step = 0.001f;
  1521. pData->param.ranges[i].stepSmall = 0.0001f;
  1522. pData->param.ranges[i].stepLarge = 0.1f;
  1523. }
  1524. }
  1525. } break;
  1526. case kPluginBridgeNonRtServerProgramCount: {
  1527. // uint/count
  1528. pData->prog.clear();
  1529. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1530. pData->prog.createNew(static_cast<uint32_t>(count));
  1531. } break;
  1532. case kPluginBridgeNonRtServerMidiProgramCount: {
  1533. // uint/count
  1534. pData->midiprog.clear();
  1535. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1536. pData->midiprog.createNew(static_cast<uint32_t>(count));
  1537. } break;
  1538. case kPluginBridgeNonRtServerPortName: {
  1539. // byte/type, uint/index, uint/size, str[] (name)
  1540. const uint8_t portType = fShmNonRtServerControl.readByte();
  1541. const uint32_t index = fShmNonRtServerControl.readUInt();
  1542. // name
  1543. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1544. char* const name = new char[nameSize+1];
  1545. carla_zeroChars(name, nameSize+1);
  1546. fShmNonRtServerControl.readCustomData(name, nameSize);
  1547. CARLA_SAFE_ASSERT_BREAK(portType > kPluginBridgePortNull && portType < kPluginBridgePortTypeCount);
  1548. switch (portType)
  1549. {
  1550. case kPluginBridgePortAudioInput:
  1551. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aIns);
  1552. fInfo.aInNames[index] = name;
  1553. break;
  1554. case kPluginBridgePortAudioOutput:
  1555. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aOuts);
  1556. fInfo.aOutNames[index] = name;
  1557. break;
  1558. }
  1559. } break;
  1560. case kPluginBridgeNonRtServerParameterData1: {
  1561. // uint/index, int/rindex, uint/type, uint/hints, int/cc
  1562. const uint32_t index = fShmNonRtServerControl.readUInt();
  1563. const int32_t rindex = fShmNonRtServerControl.readInt();
  1564. const uint32_t type = fShmNonRtServerControl.readUInt();
  1565. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1566. const int16_t midiCC = fShmNonRtServerControl.readShort();
  1567. CARLA_SAFE_ASSERT_BREAK(midiCC >= -1 && midiCC < MAX_MIDI_CONTROL);
  1568. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1569. if (index < pData->param.count)
  1570. {
  1571. pData->param.data[index].type = static_cast<ParameterType>(type);
  1572. pData->param.data[index].index = static_cast<int32_t>(index);
  1573. pData->param.data[index].rindex = rindex;
  1574. pData->param.data[index].hints = hints;
  1575. pData->param.data[index].midiCC = midiCC;
  1576. }
  1577. } break;
  1578. case kPluginBridgeNonRtServerParameterData2: {
  1579. // uint/index, uint/size, str[] (name), uint/size, str[] (unit)
  1580. const uint32_t index = fShmNonRtServerControl.readUInt();
  1581. // name
  1582. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1583. char name[nameSize+1];
  1584. carla_zeroChars(name, nameSize+1);
  1585. fShmNonRtServerControl.readCustomData(name, nameSize);
  1586. // symbol
  1587. const uint32_t symbolSize(fShmNonRtServerControl.readUInt());
  1588. char symbol[symbolSize+1];
  1589. carla_zeroChars(symbol, symbolSize+1);
  1590. fShmNonRtServerControl.readCustomData(symbol, symbolSize);
  1591. // unit
  1592. const uint32_t unitSize(fShmNonRtServerControl.readUInt());
  1593. char unit[unitSize+1];
  1594. carla_zeroChars(unit, unitSize+1);
  1595. fShmNonRtServerControl.readCustomData(unit, unitSize);
  1596. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1597. if (index < pData->param.count)
  1598. {
  1599. fParams[index].name = name;
  1600. fParams[index].symbol = symbol;
  1601. fParams[index].unit = unit;
  1602. }
  1603. } break;
  1604. case kPluginBridgeNonRtServerParameterRanges: {
  1605. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  1606. const uint32_t index = fShmNonRtServerControl.readUInt();
  1607. const float def = fShmNonRtServerControl.readFloat();
  1608. const float min = fShmNonRtServerControl.readFloat();
  1609. const float max = fShmNonRtServerControl.readFloat();
  1610. const float step = fShmNonRtServerControl.readFloat();
  1611. const float stepSmall = fShmNonRtServerControl.readFloat();
  1612. const float stepLarge = fShmNonRtServerControl.readFloat();
  1613. CARLA_SAFE_ASSERT_BREAK(min < max);
  1614. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1615. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1616. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1617. if (index < pData->param.count)
  1618. {
  1619. pData->param.ranges[index].def = def;
  1620. pData->param.ranges[index].min = min;
  1621. pData->param.ranges[index].max = max;
  1622. pData->param.ranges[index].step = step;
  1623. pData->param.ranges[index].stepSmall = stepSmall;
  1624. pData->param.ranges[index].stepLarge = stepLarge;
  1625. }
  1626. } break;
  1627. case kPluginBridgeNonRtServerParameterValue: {
  1628. // uint/index, float/value
  1629. const uint32_t index = fShmNonRtServerControl.readUInt();
  1630. const float value = fShmNonRtServerControl.readFloat();
  1631. if (index < pData->param.count)
  1632. {
  1633. const float fixedValue(pData->param.getFixedValue(index, value));
  1634. fParams[index].value = fixedValue;
  1635. CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
  1636. }
  1637. } break;
  1638. case kPluginBridgeNonRtServerParameterValue2: {
  1639. // uint/index, float/value
  1640. const uint32_t index = fShmNonRtServerControl.readUInt();
  1641. const float value = fShmNonRtServerControl.readFloat();
  1642. if (index < pData->param.count)
  1643. {
  1644. const float fixedValue(pData->param.getFixedValue(index, value));
  1645. fParams[index].value = fixedValue;
  1646. }
  1647. } break;
  1648. case kPluginBridgeNonRtServerDefaultValue: {
  1649. // uint/index, float/value
  1650. const uint32_t index = fShmNonRtServerControl.readUInt();
  1651. const float value = fShmNonRtServerControl.readFloat();
  1652. if (index < pData->param.count)
  1653. pData->param.ranges[index].def = value;
  1654. } break;
  1655. case kPluginBridgeNonRtServerCurrentProgram: {
  1656. // int/index
  1657. const int32_t index = fShmNonRtServerControl.readInt();
  1658. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1659. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1660. CarlaPlugin::setProgram(index, false, true, true);
  1661. } break;
  1662. case kPluginBridgeNonRtServerCurrentMidiProgram: {
  1663. // int/index
  1664. const int32_t index = fShmNonRtServerControl.readInt();
  1665. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1666. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1667. CarlaPlugin::setMidiProgram(index, false, true, true);
  1668. } break;
  1669. case kPluginBridgeNonRtServerProgramName: {
  1670. // uint/index, uint/size, str[] (name)
  1671. const uint32_t index = fShmNonRtServerControl.readUInt();
  1672. // name
  1673. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1674. char name[nameSize+1];
  1675. carla_zeroChars(name, nameSize+1);
  1676. fShmNonRtServerControl.readCustomData(name, nameSize);
  1677. CARLA_SAFE_ASSERT_INT2(index < pData->prog.count, index, pData->prog.count);
  1678. if (index < pData->prog.count)
  1679. {
  1680. if (pData->prog.names[index] != nullptr)
  1681. delete[] pData->prog.names[index];
  1682. pData->prog.names[index] = carla_strdup(name);
  1683. }
  1684. } break;
  1685. case kPluginBridgeNonRtServerMidiProgramData: {
  1686. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  1687. const uint32_t index = fShmNonRtServerControl.readUInt();
  1688. const uint32_t bank = fShmNonRtServerControl.readUInt();
  1689. const uint32_t program = fShmNonRtServerControl.readUInt();
  1690. // name
  1691. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1692. char name[nameSize+1];
  1693. carla_zeroChars(name, nameSize+1);
  1694. fShmNonRtServerControl.readCustomData(name, nameSize);
  1695. CARLA_SAFE_ASSERT_INT2(index < pData->midiprog.count, index, pData->midiprog.count);
  1696. if (index < pData->midiprog.count)
  1697. {
  1698. if (pData->midiprog.data[index].name != nullptr)
  1699. delete[] pData->midiprog.data[index].name;
  1700. pData->midiprog.data[index].bank = bank;
  1701. pData->midiprog.data[index].program = program;
  1702. pData->midiprog.data[index].name = carla_strdup(name);
  1703. }
  1704. } break;
  1705. case kPluginBridgeNonRtServerSetCustomData: {
  1706. // uint/size, str[], uint/size, str[], uint/size, str[]
  1707. // type
  1708. const uint32_t typeSize(fShmNonRtServerControl.readUInt());
  1709. char type[typeSize+1];
  1710. carla_zeroChars(type, typeSize+1);
  1711. fShmNonRtServerControl.readCustomData(type, typeSize);
  1712. // key
  1713. const uint32_t keySize(fShmNonRtServerControl.readUInt());
  1714. char key[keySize+1];
  1715. carla_zeroChars(key, keySize+1);
  1716. fShmNonRtServerControl.readCustomData(key, keySize);
  1717. // value
  1718. const uint32_t valueSize(fShmNonRtServerControl.readUInt());
  1719. char value[valueSize+1];
  1720. carla_zeroChars(value, valueSize+1);
  1721. fShmNonRtServerControl.readCustomData(value, valueSize);
  1722. CarlaPlugin::setCustomData(type, key, value, false);
  1723. } break;
  1724. case kPluginBridgeNonRtServerSetChunkDataFile: {
  1725. // uint/size, str[] (filename)
  1726. // chunkFilePath
  1727. const uint32_t chunkFilePathSize(fShmNonRtServerControl.readUInt());
  1728. char chunkFilePath[chunkFilePathSize+1];
  1729. carla_zeroChars(chunkFilePath, chunkFilePathSize+1);
  1730. fShmNonRtServerControl.readCustomData(chunkFilePath, chunkFilePathSize);
  1731. String realChunkFilePath(chunkFilePath);
  1732. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1733. #ifndef CARLA_OS_WIN
  1734. // Using Wine, fix temp dir
  1735. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1736. {
  1737. // Get WINEPREFIX
  1738. String wineDir;
  1739. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  1740. wineDir = String(WINEPREFIX);
  1741. else
  1742. wineDir = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  1743. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1744. realChunkFilePath = wineDir;
  1745. realChunkFilePath += "/drive_";
  1746. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1747. realChunkFilePath += "/";
  1748. realChunkFilePath += driveLetterSplit[1];
  1749. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1750. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1751. }
  1752. #endif
  1753. File chunkFile(realChunkFilePath);
  1754. if (chunkFile.existsAsFile())
  1755. {
  1756. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1757. chunkFile.deleteFile();
  1758. }
  1759. } break;
  1760. case kPluginBridgeNonRtServerSetLatency:
  1761. // uint
  1762. fLatency = fShmNonRtServerControl.readUInt();
  1763. #ifndef BUILD_BRIDGE
  1764. if (! fInitiated)
  1765. pData->latency.recreateBuffers(std::max(fInfo.aIns, fInfo.aOuts), fLatency);
  1766. #endif
  1767. break;
  1768. case kPluginBridgeNonRtServerReady:
  1769. fInitiated = true;
  1770. break;
  1771. case kPluginBridgeNonRtServerSaved:
  1772. fSaved = true;
  1773. break;
  1774. case kPluginBridgeNonRtServerUiClosed:
  1775. pData->transientTryCounter = 0;
  1776. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1777. break;
  1778. case kPluginBridgeNonRtServerError: {
  1779. // error
  1780. const uint32_t errorSize(fShmNonRtServerControl.readUInt());
  1781. char error[errorSize+1];
  1782. carla_zeroChars(error, errorSize+1);
  1783. fShmNonRtServerControl.readCustomData(error, errorSize);
  1784. if (fInitiated)
  1785. {
  1786. pData->engine->callback(ENGINE_CALLBACK_ERROR, pData->id, 0, 0, 0.0f, error);
  1787. // just in case
  1788. pData->engine->setLastError(error);
  1789. fInitError = true;
  1790. }
  1791. else
  1792. {
  1793. pData->engine->setLastError(error);
  1794. fInitError = true;
  1795. fInitiated = true;
  1796. }
  1797. } break;
  1798. }
  1799. }
  1800. }
  1801. // -------------------------------------------------------------------
  1802. uintptr_t getUiBridgeProcessId() const noexcept override
  1803. {
  1804. return fBridgeThread.getProcessPID();
  1805. }
  1806. const void* getExtraStuff() const noexcept override
  1807. {
  1808. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  1809. }
  1810. // -------------------------------------------------------------------
  1811. bool init(const char* const filename, const char* const name, const char* const label, const int64_t uniqueId, const char* const bridgeBinary)
  1812. {
  1813. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1814. // ---------------------------------------------------------------
  1815. // first checks
  1816. if (pData->client != nullptr)
  1817. {
  1818. pData->engine->setLastError("Plugin client is already registered");
  1819. return false;
  1820. }
  1821. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  1822. {
  1823. pData->engine->setLastError("null bridge binary");
  1824. return false;
  1825. }
  1826. // ---------------------------------------------------------------
  1827. // set info
  1828. if (name != nullptr && name[0] != '\0')
  1829. pData->name = pData->engine->getUniquePluginName(name);
  1830. if (filename != nullptr && filename[0] != '\0')
  1831. pData->filename = carla_strdup(filename);
  1832. else
  1833. pData->filename = carla_strdup("");
  1834. fUniqueId = uniqueId;
  1835. fBridgeBinary = bridgeBinary;
  1836. std::srand(static_cast<uint>(std::time(nullptr)));
  1837. // ---------------------------------------------------------------
  1838. // init sem/shm
  1839. if (! fShmAudioPool.initializeServer())
  1840. {
  1841. carla_stderr("Failed to initialize shared memory audio pool");
  1842. return false;
  1843. }
  1844. if (! fShmRtClientControl.initializeServer())
  1845. {
  1846. carla_stderr("Failed to initialize RT client control");
  1847. fShmAudioPool.clear();
  1848. return false;
  1849. }
  1850. if (! fShmNonRtClientControl.initialize())
  1851. {
  1852. carla_stderr("Failed to initialize Non-RT client control");
  1853. fShmRtClientControl.clear();
  1854. fShmAudioPool.clear();
  1855. return false;
  1856. }
  1857. if (! fShmNonRtServerControl.initialize())
  1858. {
  1859. carla_stderr("Failed to initialize Non-RT server control");
  1860. fShmNonRtClientControl.clear();
  1861. fShmRtClientControl.clear();
  1862. fShmAudioPool.clear();
  1863. return false;
  1864. }
  1865. // ---------------------------------------------------------------
  1866. // initial values
  1867. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientNull);
  1868. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeRtClientData)));
  1869. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtClientData)));
  1870. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtServerData)));
  1871. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetBufferSize);
  1872. fShmNonRtClientControl.writeUInt(pData->engine->getBufferSize());
  1873. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetSampleRate);
  1874. fShmNonRtClientControl.writeDouble(pData->engine->getSampleRate());
  1875. fShmNonRtClientControl.commitWrite();
  1876. // testing dummy message
  1877. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientNull);
  1878. fShmRtClientControl.commitWrite();
  1879. // init bridge thread
  1880. {
  1881. char shmIdsStr[6*4+1];
  1882. carla_zeroChars(shmIdsStr, 6*4+1);
  1883. std::strncpy(shmIdsStr+6*0, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1884. std::strncpy(shmIdsStr+6*1, &fShmRtClientControl.filename[fShmRtClientControl.filename.length()-6], 6);
  1885. std::strncpy(shmIdsStr+6*2, &fShmNonRtClientControl.filename[fShmNonRtClientControl.filename.length()-6], 6);
  1886. std::strncpy(shmIdsStr+6*3, &fShmNonRtServerControl.filename[fShmNonRtServerControl.filename.length()-6], 6);
  1887. fBridgeThread.setData(bridgeBinary, label, shmIdsStr);
  1888. fBridgeThread.startThread();
  1889. }
  1890. fInitiated = false;
  1891. fLastPongTime = Time::currentTimeMillis();
  1892. CARLA_SAFE_ASSERT(fLastPongTime > 0);
  1893. static bool sFirstInit = true;
  1894. int64_t timeoutEnd = 5000;
  1895. if (sFirstInit)
  1896. timeoutEnd *= 2;
  1897. #ifndef CARLA_OS_WIN
  1898. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1899. timeoutEnd *= 2;
  1900. #endif
  1901. sFirstInit = false;
  1902. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  1903. for (; Time::currentTimeMillis() < fLastPongTime + timeoutEnd && fBridgeThread.isThreadRunning();)
  1904. {
  1905. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1906. if (needsEngineIdle)
  1907. pData->engine->idle();
  1908. idle();
  1909. if (fInitiated)
  1910. break;
  1911. if (pData->engine->isAboutToClose())
  1912. break;
  1913. carla_msleep(20);
  1914. }
  1915. fLastPongTime = -1;
  1916. if (fInitError || ! fInitiated)
  1917. {
  1918. fBridgeThread.stopThread(6000);
  1919. if (! fInitError)
  1920. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1921. return false;
  1922. }
  1923. // ---------------------------------------------------------------
  1924. // register client
  1925. if (pData->name == nullptr)
  1926. {
  1927. if (label != nullptr && label[0] != '\0')
  1928. pData->name = pData->engine->getUniquePluginName(label);
  1929. else
  1930. pData->name = pData->engine->getUniquePluginName("unknown");
  1931. }
  1932. pData->client = pData->engine->addClient(this);
  1933. if (pData->client == nullptr || ! pData->client->isOk())
  1934. {
  1935. pData->engine->setLastError("Failed to register plugin client");
  1936. return false;
  1937. }
  1938. return true;
  1939. }
  1940. private:
  1941. const BinaryType fBinaryType;
  1942. const PluginType fPluginType;
  1943. bool fInitiated;
  1944. bool fInitError;
  1945. bool fSaved;
  1946. bool fTimedOut;
  1947. bool fTimedError;
  1948. uint fProcWaitTime;
  1949. int64_t fLastPongTime;
  1950. CarlaString fBridgeBinary;
  1951. CarlaPluginBridgeThread fBridgeThread;
  1952. BridgeAudioPool fShmAudioPool;
  1953. BridgeRtClientControl fShmRtClientControl;
  1954. BridgeNonRtClientControl fShmNonRtClientControl;
  1955. BridgeNonRtServerControl fShmNonRtServerControl;
  1956. struct Info {
  1957. uint32_t aIns, aOuts;
  1958. uint32_t cvIns, cvOuts;
  1959. uint32_t mIns, mOuts;
  1960. PluginCategory category;
  1961. uint optionsAvailable;
  1962. CarlaString name;
  1963. CarlaString label;
  1964. CarlaString maker;
  1965. CarlaString copyright;
  1966. const char** aInNames;
  1967. const char** aOutNames;
  1968. std::vector<uint8_t> chunk;
  1969. Info()
  1970. : aIns(0),
  1971. aOuts(0),
  1972. cvIns(0),
  1973. cvOuts(0),
  1974. mIns(0),
  1975. mOuts(0),
  1976. category(PLUGIN_CATEGORY_NONE),
  1977. optionsAvailable(0),
  1978. name(),
  1979. label(),
  1980. maker(),
  1981. copyright(),
  1982. aInNames(nullptr),
  1983. aOutNames(nullptr),
  1984. chunk() {}
  1985. CARLA_DECLARE_NON_COPY_STRUCT(Info)
  1986. } fInfo;
  1987. int64_t fUniqueId;
  1988. uint32_t fLatency;
  1989. BridgeParamInfo* fParams;
  1990. void resizeAudioPool(const uint32_t bufferSize)
  1991. {
  1992. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts, fInfo.cvIns+fInfo.cvOuts);
  1993. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  1994. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  1995. fShmRtClientControl.commitWrite();
  1996. waitForClient("resize-pool", 5000);
  1997. }
  1998. void waitForClient(const char* const action, const uint msecs)
  1999. {
  2000. CARLA_SAFE_ASSERT_RETURN(! fTimedOut,);
  2001. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  2002. if (fShmRtClientControl.waitForClient(msecs))
  2003. return;
  2004. fTimedOut = true;
  2005. carla_stderr("waitForClient(%s) timed out", action);
  2006. }
  2007. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridge)
  2008. };
  2009. CARLA_BACKEND_END_NAMESPACE
  2010. // -------------------------------------------------------------------------------------------------------------------
  2011. CARLA_BACKEND_START_NAMESPACE
  2012. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* bridgeBinary)
  2013. {
  2014. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  2015. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2016. {
  2017. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  2018. return nullptr;
  2019. }
  2020. #ifndef CARLA_OS_WIN
  2021. // FIXME: somewhere, somehow, we end up with double slashes, wine doesn't like that.
  2022. if (std::strncmp(bridgeBinary, "//", 2) == 0)
  2023. ++bridgeBinary;
  2024. #endif
  2025. CarlaPluginBridge* const plugin(new CarlaPluginBridge(init.engine, init.id, btype, ptype));
  2026. if (! plugin->init(init.filename, init.name, init.label, init.uniqueId, bridgeBinary))
  2027. {
  2028. delete plugin;
  2029. return nullptr;
  2030. }
  2031. return plugin;
  2032. }
  2033. CARLA_BACKEND_END_NAMESPACE
  2034. // -------------------------------------------------------------------------------------------------------------------
  2035. #include "CarlaBridgeUtils.cpp"