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.

3138 lines
113KB

  1. /*
  2. * Carla Plugin Bridge
  3. * Copyright (C) 2011-2020 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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaBackendUtils.hpp"
  19. #include "CarlaBase64Utils.hpp"
  20. #include "CarlaBridgeUtils.hpp"
  21. #include "CarlaEngineUtils.hpp"
  22. #include "CarlaMathUtils.hpp"
  23. #include "CarlaPipeUtils.hpp"
  24. #include "CarlaScopeUtils.hpp"
  25. #include "CarlaShmUtils.hpp"
  26. #include "CarlaThread.hpp"
  27. #include "jackbridge/JackBridge.hpp"
  28. #include <ctime>
  29. #include "water/files/File.h"
  30. #include "water/misc/Time.h"
  31. #include "water/threads/ChildProcess.h"
  32. // ---------------------------------------------------------------------------------------------------------------------
  33. using water::ChildProcess;
  34. using water::File;
  35. using water::String;
  36. using water::StringArray;
  37. using water::Time;
  38. CARLA_BACKEND_START_NAMESPACE
  39. // ---------------------------------------------------------------------------------------------------------------------
  40. // Fallback data
  41. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  42. // ---------------------------------------------------------------------------------------------------------------------
  43. static String findWinePrefix(const String filename, const int recursionLimit = 10)
  44. {
  45. if (recursionLimit == 0 || filename.length() < 5 || ! filename.contains("/"))
  46. return "";
  47. const String path(filename.upToLastOccurrenceOf("/", false, false));
  48. if (File(path + "/dosdevices").isDirectory())
  49. return path;
  50. return findWinePrefix(path, recursionLimit-1);
  51. }
  52. // ---------------------------------------------------------------------------------------------------------------------
  53. struct BridgeParamInfo {
  54. float value;
  55. CarlaString name;
  56. CarlaString symbol;
  57. CarlaString unit;
  58. BridgeParamInfo() noexcept
  59. : value(0.0f),
  60. name(),
  61. symbol(),
  62. unit() {}
  63. CARLA_DECLARE_NON_COPY_STRUCT(BridgeParamInfo)
  64. };
  65. // ---------------------------------------------------------------------------------------------------------------------
  66. class CarlaPluginBridgeThread : public CarlaThread
  67. {
  68. public:
  69. CarlaPluginBridgeThread(CarlaEngine* const engine, CarlaPlugin* const plugin) noexcept
  70. : CarlaThread("CarlaPluginBridgeThread"),
  71. kEngine(engine),
  72. kPlugin(plugin),
  73. fBinary(),
  74. fLabel(),
  75. fShmIds(),
  76. #ifndef CARLA_OS_WIN
  77. fWinePrefix(),
  78. #endif
  79. fProcess() {}
  80. void setData(
  81. #ifndef CARLA_OS_WIN
  82. const char* const winePrefix,
  83. #endif
  84. const char* const binary,
  85. const char* const label,
  86. const char* const shmIds) noexcept
  87. {
  88. CARLA_SAFE_ASSERT_RETURN(binary != nullptr && binary[0] != '\0',);
  89. CARLA_SAFE_ASSERT_RETURN(shmIds != nullptr && shmIds[0] != '\0',);
  90. CARLA_SAFE_ASSERT(! isThreadRunning());
  91. #ifndef CARLA_OS_WIN
  92. fWinePrefix = winePrefix;
  93. #endif
  94. fBinary = binary;
  95. fShmIds = shmIds;
  96. if (label != nullptr)
  97. fLabel = label;
  98. if (fLabel.isEmpty())
  99. fLabel = "\"\"";
  100. }
  101. uintptr_t getProcessPID() const noexcept
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
  104. return (uintptr_t)fProcess->getPID();
  105. }
  106. protected:
  107. void run()
  108. {
  109. if (fProcess == nullptr)
  110. {
  111. fProcess = new ChildProcess();
  112. }
  113. else if (fProcess->isRunning())
  114. {
  115. carla_stderr("CarlaPluginBridgeThread::run() - already running");
  116. }
  117. char strBuf[STR_MAX+1];
  118. strBuf[STR_MAX] = '\0';
  119. const EngineOptions& options(kEngine->getOptions());
  120. String name(kPlugin->getName());
  121. String filename(kPlugin->getFilename());
  122. if (name.isEmpty())
  123. name = "(none)";
  124. if (filename.isEmpty())
  125. filename = "\"\"";
  126. StringArray arguments;
  127. #ifndef CARLA_OS_WIN
  128. // start with "wine" if needed
  129. if (fBinary.endsWithIgnoreCase(".exe"))
  130. {
  131. String wineCMD;
  132. if (options.wine.executable != nullptr && options.wine.executable[0] != '\0')
  133. {
  134. wineCMD = options.wine.executable;
  135. if (fBinary.endsWithIgnoreCase("64.exe")
  136. && options.wine.executable[0] == CARLA_OS_SEP
  137. && File(wineCMD + "64").existsAsFile())
  138. wineCMD += "64";
  139. }
  140. else
  141. {
  142. wineCMD = "wine";
  143. }
  144. arguments.add(wineCMD);
  145. }
  146. #endif
  147. // binary
  148. arguments.add(fBinary);
  149. // plugin type
  150. arguments.add(getPluginTypeAsString(kPlugin->getType()));
  151. // filename
  152. arguments.add(filename);
  153. // label
  154. arguments.add(fLabel);
  155. // uniqueId
  156. arguments.add(String(static_cast<water::int64>(kPlugin->getUniqueId())));
  157. bool started;
  158. {
  159. const ScopedEngineEnvironmentLocker _seel(kEngine);
  160. #ifdef CARLA_OS_LINUX
  161. const CarlaScopedEnvVar sev1("LD_LIBRARY_PATH", nullptr);
  162. const CarlaScopedEnvVar sev2("LD_PRELOAD", nullptr);
  163. #endif
  164. carla_setenv("ENGINE_OPTION_FORCE_STEREO", bool2str(options.forceStereo));
  165. carla_setenv("ENGINE_OPTION_PREFER_PLUGIN_BRIDGES", bool2str(options.preferPluginBridges));
  166. carla_setenv("ENGINE_OPTION_PREFER_UI_BRIDGES", bool2str(options.preferUiBridges));
  167. carla_setenv("ENGINE_OPTION_UIS_ALWAYS_ON_TOP", bool2str(options.uisAlwaysOnTop));
  168. std::snprintf(strBuf, STR_MAX, "%u", options.maxParameters);
  169. carla_setenv("ENGINE_OPTION_MAX_PARAMETERS", strBuf);
  170. std::snprintf(strBuf, STR_MAX, "%u", options.uiBridgesTimeout);
  171. carla_setenv("ENGINE_OPTION_UI_BRIDGES_TIMEOUT",strBuf);
  172. if (options.pathLADSPA != nullptr)
  173. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", options.pathLADSPA);
  174. else
  175. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA", "");
  176. if (options.pathDSSI != nullptr)
  177. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", options.pathDSSI);
  178. else
  179. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_DSSI", "");
  180. if (options.pathLV2 != nullptr)
  181. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", options.pathLV2);
  182. else
  183. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_LV2", "");
  184. if (options.pathVST2 != nullptr)
  185. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", options.pathVST2);
  186. else
  187. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST2", "");
  188. if (options.pathVST3 != nullptr)
  189. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", options.pathVST3);
  190. else
  191. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_VST3", "");
  192. if (options.pathSF2 != nullptr)
  193. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", options.pathSF2);
  194. else
  195. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SF2", "");
  196. if (options.pathSFZ != nullptr)
  197. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", options.pathSFZ);
  198. else
  199. carla_setenv("ENGINE_OPTION_PLUGIN_PATH_SFZ", "");
  200. if (options.binaryDir != nullptr)
  201. carla_setenv("ENGINE_OPTION_PATH_BINARIES", options.binaryDir);
  202. else
  203. carla_setenv("ENGINE_OPTION_PATH_BINARIES", "");
  204. if (options.resourceDir != nullptr)
  205. carla_setenv("ENGINE_OPTION_PATH_RESOURCES", options.resourceDir);
  206. else
  207. carla_setenv("ENGINE_OPTION_PATH_RESOURCES", "");
  208. carla_setenv("ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR", bool2str(options.preventBadBehaviour));
  209. std::snprintf(strBuf, STR_MAX, P_UINTPTR, options.frontendWinId);
  210. carla_setenv("ENGINE_OPTION_FRONTEND_WIN_ID", strBuf);
  211. carla_setenv("ENGINE_BRIDGE_SHM_IDS", fShmIds.toRawUTF8());
  212. #ifndef CARLA_OS_WIN
  213. if (fWinePrefix.isNotEmpty())
  214. {
  215. carla_setenv("WINEDEBUG", "-all");
  216. carla_setenv("WINEPREFIX", fWinePrefix.toRawUTF8());
  217. if (options.wine.rtPrio)
  218. {
  219. carla_setenv("STAGING_SHARED_MEMORY", "1");
  220. carla_setenv("WINE_RT_POLICY", "FF");
  221. std::snprintf(strBuf, STR_MAX, "%i", options.wine.baseRtPrio);
  222. carla_setenv("STAGING_RT_PRIORITY_BASE", strBuf);
  223. carla_setenv("WINE_RT", strBuf);
  224. carla_setenv("WINE_RT_PRIO", strBuf);
  225. std::snprintf(strBuf, STR_MAX, "%i", options.wine.serverRtPrio);
  226. carla_setenv("STAGING_RT_PRIORITY_SERVER", strBuf);
  227. carla_setenv("WINE_SVR_RT", strBuf);
  228. carla_stdout("Using WINEPREFIX '%s', with base RT prio %i and server RT prio %i",
  229. fWinePrefix.toRawUTF8(), options.wine.baseRtPrio, options.wine.serverRtPrio);
  230. }
  231. else
  232. {
  233. carla_unsetenv("STAGING_SHARED_MEMORY");
  234. carla_unsetenv("WINE_RT_POLICY");
  235. carla_unsetenv("STAGING_RT_PRIORITY_BASE");
  236. carla_unsetenv("STAGING_RT_PRIORITY_SERVER");
  237. carla_unsetenv("WINE_RT");
  238. carla_unsetenv("WINE_RT_PRIO");
  239. carla_unsetenv("WINE_SVR_RT");
  240. carla_stdout("Using WINEPREFIX '%s', without RT priorities", fWinePrefix.toRawUTF8());
  241. }
  242. }
  243. #endif
  244. carla_stdout("Starting plugin bridge, command is:\n%s \"%s\" \"%s\" \"%s\" " P_INT64,
  245. fBinary.toRawUTF8(), getPluginTypeAsString(kPlugin->getType()), filename.toRawUTF8(), fLabel.toRawUTF8(), kPlugin->getUniqueId());
  246. started = fProcess->start(arguments);
  247. }
  248. if (! started)
  249. {
  250. carla_stdout("failed!");
  251. fProcess = nullptr;
  252. return;
  253. }
  254. for (; fProcess->isRunning() && ! shouldThreadExit();)
  255. carla_sleep(1);
  256. // we only get here if bridge crashed or thread asked to exit
  257. if (fProcess->isRunning() && shouldThreadExit())
  258. {
  259. fProcess->waitForProcessToFinish(2000);
  260. if (fProcess->isRunning())
  261. {
  262. carla_stdout("CarlaPluginBridgeThread::run() - bridge refused to close, force kill now");
  263. fProcess->kill();
  264. }
  265. else
  266. {
  267. carla_stdout("CarlaPluginBridgeThread::run() - bridge auto-closed successfully");
  268. }
  269. }
  270. else
  271. {
  272. // forced quit, may have crashed
  273. if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  274. {
  275. carla_stderr("CarlaPluginBridgeThread::run() - bridge crashed");
  276. CarlaString errorString("Plugin '" + CarlaString(kPlugin->getName()) + "' has crashed!\n"
  277. "Saving now will lose its current settings.\n"
  278. "Please remove this plugin, and not rely on it from this point.");
  279. kEngine->callback(true, true,
  280. CarlaBackend::ENGINE_CALLBACK_ERROR, kPlugin->getId(), 0, 0, 0, 0.0f, errorString);
  281. }
  282. }
  283. fProcess = nullptr;
  284. }
  285. private:
  286. CarlaEngine* const kEngine;
  287. CarlaPlugin* const kPlugin;
  288. String fBinary;
  289. String fLabel;
  290. String fShmIds;
  291. #ifndef CARLA_OS_WIN
  292. String fWinePrefix;
  293. #endif
  294. CarlaScopedPointer<ChildProcess> fProcess;
  295. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridgeThread)
  296. };
  297. // ---------------------------------------------------------------------------------------------------------------------
  298. class CarlaPluginBridge : public CarlaPlugin
  299. {
  300. public:
  301. CarlaPluginBridge(CarlaEngine* const engine, const uint id, const BinaryType btype, const PluginType ptype)
  302. : CarlaPlugin(engine, id),
  303. fBinaryType(btype),
  304. fPluginType(ptype),
  305. fBridgeVersion(6), // before kPluginBridgeNonRtServerVersion was a thing, API was at 6
  306. fInitiated(false),
  307. fInitError(false),
  308. fSaved(true),
  309. fTimedOut(false),
  310. fTimedError(false),
  311. fBufferSize(engine->getBufferSize()),
  312. fProcWaitTime(0),
  313. fBridgeBinary(),
  314. fBridgeThread(engine, this),
  315. fShmAudioPool(),
  316. fShmRtClientControl(),
  317. fShmNonRtClientControl(),
  318. fShmNonRtServerControl(),
  319. #ifndef CARLA_OS_WIN
  320. fWinePrefix(),
  321. #endif
  322. fReceivingParamText(),
  323. fInfo(),
  324. fUniqueId(0),
  325. fLatency(0),
  326. fParams(nullptr)
  327. {
  328. carla_debug("CarlaPluginBridge::CarlaPluginBridge(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
  329. pData->hints |= PLUGIN_IS_BRIDGE;
  330. }
  331. ~CarlaPluginBridge() override
  332. {
  333. carla_debug("CarlaPluginBridge::~CarlaPluginBridge()");
  334. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  335. // close UI
  336. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  337. pData->transientTryCounter = 0;
  338. #endif
  339. pData->singleMutex.lock();
  340. pData->masterMutex.lock();
  341. if (pData->client != nullptr && pData->client->isActive())
  342. pData->client->deactivate(true);
  343. if (pData->active)
  344. {
  345. deactivate();
  346. pData->active = false;
  347. }
  348. if (fBridgeThread.isThreadRunning())
  349. {
  350. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientQuit);
  351. fShmNonRtClientControl.commitWrite();
  352. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientQuit);
  353. fShmRtClientControl.commitWrite();
  354. if (! fTimedOut)
  355. waitForClient("stopping", 3000);
  356. }
  357. fBridgeThread.stopThread(3000);
  358. fShmNonRtServerControl.clear();
  359. fShmNonRtClientControl.clear();
  360. fShmRtClientControl.clear();
  361. fShmAudioPool.clear();
  362. clearBuffers();
  363. fInfo.chunk.clear();
  364. }
  365. // -------------------------------------------------------------------
  366. // Information (base)
  367. BinaryType getBinaryType() const noexcept override
  368. {
  369. return fBinaryType;
  370. }
  371. PluginType getType() const noexcept override
  372. {
  373. return fPluginType;
  374. }
  375. PluginCategory getCategory() const noexcept override
  376. {
  377. return fInfo.category;
  378. }
  379. int64_t getUniqueId() const noexcept override
  380. {
  381. return fUniqueId;
  382. }
  383. uint32_t getLatencyInFrames() const noexcept override
  384. {
  385. return fLatency;
  386. }
  387. // -------------------------------------------------------------------
  388. // Information (count)
  389. uint32_t getMidiInCount() const noexcept override
  390. {
  391. return fInfo.mIns;
  392. }
  393. uint32_t getMidiOutCount() const noexcept override
  394. {
  395. return fInfo.mOuts;
  396. }
  397. // -------------------------------------------------------------------
  398. // Information (current data)
  399. std::size_t getChunkData(void** const dataPtr) noexcept override
  400. {
  401. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  402. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  403. waitForSaved();
  404. CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.size() > 0, 0);
  405. #ifdef CARLA_PROPER_CPP11_SUPPORT
  406. *dataPtr = fInfo.chunk.data();
  407. #else
  408. *dataPtr = &fInfo.chunk.front();
  409. #endif
  410. return fInfo.chunk.size();
  411. }
  412. // -------------------------------------------------------------------
  413. // Information (per-plugin data)
  414. uint getOptionsAvailable() const noexcept override
  415. {
  416. return fInfo.optionsAvailable;
  417. }
  418. float getParameterValue(const uint32_t parameterId) const noexcept override
  419. {
  420. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  421. return fParams[parameterId].value;
  422. }
  423. bool getLabel(char* const strBuf) const noexcept override
  424. {
  425. std::strncpy(strBuf, fInfo.label, STR_MAX);
  426. return true;
  427. }
  428. bool getMaker(char* const strBuf) const noexcept override
  429. {
  430. std::strncpy(strBuf, fInfo.maker, STR_MAX);
  431. return true;
  432. }
  433. bool getCopyright(char* const strBuf) const noexcept override
  434. {
  435. std::strncpy(strBuf, fInfo.copyright, STR_MAX);
  436. return true;
  437. }
  438. bool getRealName(char* const strBuf) const noexcept override
  439. {
  440. std::strncpy(strBuf, fInfo.name, STR_MAX);
  441. return true;
  442. }
  443. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  444. {
  445. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  446. std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
  447. return true;
  448. }
  449. bool getParameterText(const uint32_t parameterId, char* const strBuf) noexcept override
  450. {
  451. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  452. CARLA_SAFE_ASSERT_RETURN(! fReceivingParamText.isCurrentlyWaitingData(), false);
  453. const int32_t parameterIdi = static_cast<int32_t>(parameterId);
  454. fReceivingParamText.setTargetData(parameterIdi, strBuf);
  455. {
  456. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  457. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientGetParameterText);
  458. fShmNonRtClientControl.writeInt(parameterIdi);
  459. fShmNonRtClientControl.commitWrite();
  460. }
  461. if (waitForParameterText())
  462. return true;
  463. std::snprintf(strBuf, STR_MAX, "%.12g", static_cast<double>(fParams[parameterId].value));
  464. return false;
  465. }
  466. bool getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  467. {
  468. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  469. std::strncpy(strBuf, fParams[parameterId].symbol.buffer(), STR_MAX);
  470. return true;
  471. }
  472. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  473. {
  474. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  475. std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
  476. return true;
  477. }
  478. // -------------------------------------------------------------------
  479. // Set data (state)
  480. void prepareForSave(bool) noexcept override
  481. {
  482. fSaved = false;
  483. {
  484. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  485. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPrepareForSave);
  486. fShmNonRtClientControl.commitWrite();
  487. }
  488. }
  489. bool waitForParameterText()
  490. {
  491. bool success;
  492. if (fReceivingParamText.wasDataReceived(&success))
  493. return success;
  494. const uint32_t timeoutEnd = Time::getMillisecondCounter() + 500; // 500 ms
  495. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  496. for (; Time::getMillisecondCounter() < timeoutEnd && fBridgeThread.isThreadRunning();)
  497. {
  498. if (fReceivingParamText.wasDataReceived(&success))
  499. return success;
  500. if (needsEngineIdle)
  501. pData->engine->idle();
  502. carla_msleep(5);
  503. }
  504. if (! fBridgeThread.isThreadRunning())
  505. {
  506. carla_stderr("CarlaPluginBridge::waitForParameterText() - Bridge is not running");
  507. return false;
  508. }
  509. carla_stderr("CarlaPluginBridge::waitForParameterText() - Timeout while requesting text");
  510. return false;
  511. }
  512. void waitForSaved()
  513. {
  514. if (fSaved)
  515. return;
  516. // TODO: only wait 1 minute for NI plugins
  517. const uint32_t timeoutEnd = Time::getMillisecondCounter() + 60*1000; // 60 secs, 1 minute
  518. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  519. for (; Time::getMillisecondCounter() < timeoutEnd && fBridgeThread.isThreadRunning();)
  520. {
  521. pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  522. if (needsEngineIdle)
  523. pData->engine->idle();
  524. if (fSaved)
  525. break;
  526. carla_msleep(20);
  527. }
  528. if (! fBridgeThread.isThreadRunning())
  529. return carla_stderr("CarlaPluginBridge::waitForSaved() - Bridge is not running");
  530. if (! fSaved)
  531. return carla_stderr("CarlaPluginBridge::waitForSaved() - Timeout while requesting save state");
  532. }
  533. // -------------------------------------------------------------------
  534. // Set data (internal stuff)
  535. void setOption(const uint option, const bool yesNo, const bool sendCallback) override
  536. {
  537. {
  538. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  539. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOption);
  540. fShmNonRtClientControl.writeUInt(option);
  541. fShmNonRtClientControl.writeBool(yesNo);
  542. fShmNonRtClientControl.commitWrite();
  543. }
  544. CarlaPlugin::setOption(option, yesNo, sendCallback);
  545. }
  546. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  547. {
  548. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  549. {
  550. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  551. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCtrlChannel);
  552. fShmNonRtClientControl.writeShort(channel);
  553. fShmNonRtClientControl.commitWrite();
  554. }
  555. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  556. }
  557. // -------------------------------------------------------------------
  558. // Set data (plugin-specific stuff)
  559. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  560. {
  561. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  562. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
  563. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  564. fParams[parameterId].value = fixedValue;
  565. {
  566. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  567. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
  568. fShmNonRtClientControl.writeUInt(parameterId);
  569. fShmNonRtClientControl.writeFloat(value);
  570. fShmNonRtClientControl.commitWrite();
  571. fShmNonRtClientControl.waitIfDataIsReachingLimit();
  572. }
  573. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  574. }
  575. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  576. {
  577. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  578. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  579. fParams[parameterId].value = fixedValue;
  580. {
  581. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  582. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
  583. fShmNonRtClientControl.writeUInt(parameterId);
  584. fShmNonRtClientControl.writeFloat(value);
  585. fShmNonRtClientControl.commitWrite();
  586. fShmNonRtClientControl.waitIfDataIsReachingLimit();
  587. }
  588. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  589. }
  590. void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  591. {
  592. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  593. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  594. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  595. {
  596. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  597. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMidiChannel);
  598. fShmNonRtClientControl.writeUInt(parameterId);
  599. fShmNonRtClientControl.writeByte(channel);
  600. fShmNonRtClientControl.commitWrite();
  601. }
  602. CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
  603. }
  604. void setParameterMappedControlIndex(const uint32_t parameterId, const int16_t index,
  605. const bool sendOsc, const bool sendCallback,
  606. const bool reconfigureNow) noexcept override
  607. {
  608. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  609. CARLA_SAFE_ASSERT_RETURN(index >= CONTROL_INDEX_NONE && index <= CONTROL_INDEX_MAX_ALLOWED,);
  610. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  611. {
  612. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  613. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedControlIndex);
  614. fShmNonRtClientControl.writeUInt(parameterId);
  615. fShmNonRtClientControl.writeShort(index);
  616. fShmNonRtClientControl.commitWrite();
  617. }
  618. CarlaPlugin::setParameterMappedControlIndex(parameterId, index, sendOsc, sendCallback, reconfigureNow);
  619. }
  620. void setParameterMappedRange(const uint32_t parameterId, const float minimum, const float maximum, const bool sendOsc, const bool sendCallback) noexcept override
  621. {
  622. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  623. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  624. // kPluginBridgeNonRtClientSetParameterMappedRange was added in API 7
  625. if (fBridgeVersion >= 7)
  626. {
  627. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  628. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedRange);
  629. fShmNonRtClientControl.writeUInt(parameterId);
  630. fShmNonRtClientControl.writeFloat(minimum);
  631. fShmNonRtClientControl.writeFloat(maximum);
  632. fShmNonRtClientControl.commitWrite();
  633. }
  634. CarlaPlugin::setParameterMappedRange(parameterId, minimum, maximum, sendOsc, sendCallback);
  635. }
  636. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  637. {
  638. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  639. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  640. {
  641. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  642. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
  643. fShmNonRtClientControl.writeInt(index);
  644. fShmNonRtClientControl.commitWrite();
  645. }
  646. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  647. }
  648. void setProgramRT(const uint32_t index, const bool sendCallbackLater) noexcept override
  649. {
  650. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  651. {
  652. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  653. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
  654. fShmNonRtClientControl.writeInt(static_cast<int32_t>(index));
  655. fShmNonRtClientControl.commitWrite();
  656. }
  657. CarlaPlugin::setProgramRT(index, sendCallbackLater);
  658. }
  659. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  660. {
  661. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  662. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  663. {
  664. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  665. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
  666. fShmNonRtClientControl.writeInt(index);
  667. fShmNonRtClientControl.commitWrite();
  668. }
  669. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  670. }
  671. void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
  672. {
  673. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  674. {
  675. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  676. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
  677. fShmNonRtClientControl.writeInt(static_cast<int32_t>(uindex));
  678. fShmNonRtClientControl.commitWrite();
  679. }
  680. CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
  681. }
  682. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  683. {
  684. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  685. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  686. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  687. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  688. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  689. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "__CarlaPingOnOff__") == 0)
  690. {
  691. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  692. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPingOnOff);
  693. fShmNonRtClientControl.writeBool(std::strcmp(value, "true") == 0);
  694. fShmNonRtClientControl.commitWrite();
  695. return;
  696. }
  697. const uint32_t typeLen(static_cast<uint32_t>(std::strlen(type)));
  698. const uint32_t keyLen(static_cast<uint32_t>(std::strlen(key)));
  699. const uint32_t valueLen(static_cast<uint32_t>(std::strlen(value)));
  700. {
  701. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  702. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCustomData);
  703. fShmNonRtClientControl.writeUInt(typeLen);
  704. fShmNonRtClientControl.writeCustomData(type, typeLen);
  705. fShmNonRtClientControl.writeUInt(keyLen);
  706. fShmNonRtClientControl.writeCustomData(key, keyLen);
  707. fShmNonRtClientControl.writeUInt(valueLen);
  708. if (valueLen > 0)
  709. fShmNonRtClientControl.writeCustomData(value, valueLen);
  710. fShmNonRtClientControl.commitWrite();
  711. }
  712. CarlaPlugin::setCustomData(type, key, value, sendGui);
  713. }
  714. void setChunkData(const void* const data, const std::size_t dataSize) override
  715. {
  716. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  717. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  718. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  719. CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
  720. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0,);
  721. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  722. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  723. filePath += fShmAudioPool.getFilenameSuffix();
  724. if (File(filePath).replaceWithText(dataBase64.buffer()))
  725. {
  726. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  727. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  728. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
  729. fShmNonRtClientControl.writeUInt(ulength);
  730. fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
  731. fShmNonRtClientControl.commitWrite();
  732. }
  733. // save data internally as well
  734. fInfo.chunk.resize(dataSize);
  735. #ifdef CARLA_PROPER_CPP11_SUPPORT
  736. std::memcpy(fInfo.chunk.data(), data, dataSize);
  737. #else
  738. std::memcpy(&fInfo.chunk.front(), data, dataSize);
  739. #endif
  740. }
  741. // -------------------------------------------------------------------
  742. // Set ui stuff
  743. void showCustomUI(const bool yesNo) override
  744. {
  745. {
  746. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  747. fShmNonRtClientControl.writeOpcode(yesNo ? kPluginBridgeNonRtClientShowUI : kPluginBridgeNonRtClientHideUI);
  748. fShmNonRtClientControl.commitWrite();
  749. }
  750. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  751. if (yesNo)
  752. {
  753. pData->tryTransient();
  754. }
  755. else
  756. {
  757. pData->transientTryCounter = 0;
  758. }
  759. #endif
  760. }
  761. void idle() override
  762. {
  763. if (fBridgeThread.isThreadRunning())
  764. {
  765. if (fInitiated && fTimedOut && pData->active)
  766. setActive(false, true, true);
  767. {
  768. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  769. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPing);
  770. fShmNonRtClientControl.commitWrite();
  771. }
  772. try {
  773. handleNonRtData();
  774. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  775. }
  776. else if (fInitiated)
  777. {
  778. fTimedOut = true;
  779. fTimedError = true;
  780. fInitiated = false;
  781. handleProcessStopped();
  782. }
  783. CarlaPlugin::idle();
  784. }
  785. // -------------------------------------------------------------------
  786. // Plugin state
  787. void reload() override
  788. {
  789. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  790. carla_debug("CarlaPluginBridge::reload() - start");
  791. const EngineProcessMode processMode(pData->engine->getProccessMode());
  792. // Safely disable plugin for reload
  793. const ScopedDisabler sd(this);
  794. // cleanup of previous data
  795. pData->audioIn.clear();
  796. pData->audioOut.clear();
  797. pData->cvIn.clear();
  798. pData->cvOut.clear();
  799. pData->event.clear();
  800. bool needsCtrlIn, needsCtrlOut;
  801. needsCtrlIn = needsCtrlOut = false;
  802. if (fInfo.aIns > 0)
  803. {
  804. pData->audioIn.createNew(fInfo.aIns);
  805. }
  806. if (fInfo.aOuts > 0)
  807. {
  808. pData->audioOut.createNew(fInfo.aOuts);
  809. needsCtrlIn = true;
  810. }
  811. if (fInfo.cvIns > 0)
  812. {
  813. pData->cvIn.createNew(fInfo.cvIns);
  814. }
  815. if (fInfo.cvOuts > 0)
  816. {
  817. pData->cvOut.createNew(fInfo.cvOuts);
  818. }
  819. if (fInfo.mIns > 0)
  820. needsCtrlIn = true;
  821. if (fInfo.mOuts > 0)
  822. needsCtrlOut = true;
  823. const uint portNameSize(pData->engine->getMaxPortNameSize());
  824. CarlaString portName;
  825. // Audio Ins
  826. for (uint32_t j=0; j < fInfo.aIns; ++j)
  827. {
  828. portName.clear();
  829. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  830. {
  831. portName = pData->name;
  832. portName += ":";
  833. }
  834. if (fInfo.aInNames != nullptr && fInfo.aInNames[j] != nullptr)
  835. {
  836. portName += fInfo.aInNames[j];
  837. }
  838. else if (fInfo.aIns > 1)
  839. {
  840. portName += "input_";
  841. portName += CarlaString(j+1);
  842. }
  843. else
  844. portName += "input";
  845. portName.truncate(portNameSize);
  846. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  847. pData->audioIn.ports[j].rindex = j;
  848. }
  849. // Audio Outs
  850. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  851. {
  852. portName.clear();
  853. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  854. {
  855. portName = pData->name;
  856. portName += ":";
  857. }
  858. if (fInfo.aOutNames != nullptr && fInfo.aOutNames[j] != nullptr)
  859. {
  860. portName += fInfo.aOutNames[j];
  861. }
  862. else if (fInfo.aOuts > 1)
  863. {
  864. portName += "output_";
  865. portName += CarlaString(j+1);
  866. }
  867. else
  868. portName += "output";
  869. portName.truncate(portNameSize);
  870. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  871. pData->audioOut.ports[j].rindex = j;
  872. }
  873. // TODO - MIDI
  874. // CV Ins
  875. for (uint32_t j=0; j < fInfo.cvIns; ++j)
  876. {
  877. portName.clear();
  878. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  879. {
  880. portName = pData->name;
  881. portName += ":";
  882. }
  883. if (fInfo.cvInNames != nullptr && fInfo.cvInNames[j] != nullptr)
  884. {
  885. portName += fInfo.cvInNames[j];
  886. }
  887. else if (fInfo.cvIns > 1)
  888. {
  889. portName += "cv_input_";
  890. portName += CarlaString(j+1);
  891. }
  892. else
  893. portName += "cv_input";
  894. portName.truncate(portNameSize);
  895. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  896. pData->cvIn.ports[j].rindex = j;
  897. }
  898. // CV Outs
  899. for (uint32_t j=0; j < fInfo.cvOuts; ++j)
  900. {
  901. portName.clear();
  902. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  903. {
  904. portName = pData->name;
  905. portName += ":";
  906. }
  907. if (fInfo.cvOutNames != nullptr && fInfo.cvOutNames[j] != nullptr)
  908. {
  909. portName += fInfo.cvOutNames[j];
  910. }
  911. else if (fInfo.cvOuts > 1)
  912. {
  913. portName += "cv_output_";
  914. portName += CarlaString(j+1);
  915. }
  916. else
  917. portName += "cv_output";
  918. portName.truncate(portNameSize);
  919. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  920. pData->cvOut.ports[j].rindex = j;
  921. }
  922. if (needsCtrlIn)
  923. {
  924. portName.clear();
  925. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  926. {
  927. portName = pData->name;
  928. portName += ":";
  929. }
  930. portName += "event-in";
  931. portName.truncate(portNameSize);
  932. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  933. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  934. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  935. #endif
  936. }
  937. if (needsCtrlOut)
  938. {
  939. portName.clear();
  940. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  941. {
  942. portName = pData->name;
  943. portName += ":";
  944. }
  945. portName += "event-out";
  946. portName.truncate(portNameSize);
  947. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  948. }
  949. // extra plugin hints
  950. pData->extraHints = 0x0;
  951. if (fInfo.mIns > 0)
  952. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  953. if (fInfo.mOuts > 0)
  954. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  955. bufferSizeChanged(pData->engine->getBufferSize());
  956. reloadPrograms(true);
  957. carla_debug("CarlaPluginBridge::reload() - end");
  958. }
  959. // -------------------------------------------------------------------
  960. // Plugin processing
  961. void activate() noexcept override
  962. {
  963. if (! fBridgeThread.isThreadRunning())
  964. {
  965. CARLA_SAFE_ASSERT_RETURN(restartBridgeThread(),);
  966. }
  967. {
  968. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  969. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientActivate);
  970. fShmNonRtClientControl.commitWrite();
  971. }
  972. fTimedOut = false;
  973. try {
  974. waitForClient("activate", 2000);
  975. } CARLA_SAFE_EXCEPTION("activate - waitForClient");
  976. }
  977. void deactivate() noexcept override
  978. {
  979. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  980. {
  981. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  982. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientDeactivate);
  983. fShmNonRtClientControl.commitWrite();
  984. }
  985. fTimedOut = false;
  986. try {
  987. waitForClient("deactivate", 2000);
  988. } CARLA_SAFE_EXCEPTION("deactivate - waitForClient");
  989. }
  990. void process(const float* const* const audioIn,
  991. float** const audioOut,
  992. const float* const* const cvIn,
  993. float** const cvOut,
  994. const uint32_t frames) override
  995. {
  996. // --------------------------------------------------------------------------------------------------------
  997. // Check if active
  998. if (fTimedOut || fTimedError || ! pData->active)
  999. {
  1000. // disable any output sound
  1001. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1002. carla_zeroFloats(audioOut[i], frames);
  1003. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1004. carla_zeroFloats(cvOut[i], frames);
  1005. return;
  1006. }
  1007. // --------------------------------------------------------------------------------------------------------
  1008. // Check if needs reset
  1009. if (pData->needsReset)
  1010. {
  1011. // TODO
  1012. pData->needsReset = false;
  1013. }
  1014. // --------------------------------------------------------------------------------------------------------
  1015. // Event Input
  1016. if (pData->event.portIn != nullptr)
  1017. {
  1018. // ----------------------------------------------------------------------------------------------------
  1019. // MIDI Input (External)
  1020. if (pData->extNotes.mutex.tryLock())
  1021. {
  1022. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  1023. {
  1024. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  1025. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1026. uint8_t data1, data2, data3;
  1027. data1 = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1028. data2 = note.note;
  1029. data3 = note.velo;
  1030. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1031. fShmRtClientControl.writeUInt(0); // time
  1032. fShmRtClientControl.writeByte(0); // port
  1033. fShmRtClientControl.writeByte(3); // size
  1034. fShmRtClientControl.writeByte(data1);
  1035. fShmRtClientControl.writeByte(data2);
  1036. fShmRtClientControl.writeByte(data3);
  1037. fShmRtClientControl.commitWrite();
  1038. }
  1039. pData->extNotes.data.clear();
  1040. pData->extNotes.mutex.unlock();
  1041. } // End of MIDI Input (External)
  1042. // ----------------------------------------------------------------------------------------------------
  1043. // Event Input (System)
  1044. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1045. bool allNotesOffSent = false;
  1046. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1047. {
  1048. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0;
  1049. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  1050. }
  1051. #endif
  1052. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1053. {
  1054. EngineEvent& event(pData->event.portIn->getEvent(i));
  1055. // Control change
  1056. switch (event.type)
  1057. {
  1058. case kEngineEventTypeNull:
  1059. break;
  1060. case kEngineEventTypeControl: {
  1061. EngineControlEvent& ctrlEvent = event.ctrl;
  1062. switch (ctrlEvent.type)
  1063. {
  1064. case kEngineControlEventTypeNull:
  1065. break;
  1066. case kEngineControlEventTypeParameter: {
  1067. float value;
  1068. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1069. // non-midi
  1070. if (event.channel == kEngineEventNonMidiChannel)
  1071. {
  1072. const uint32_t k = ctrlEvent.param;
  1073. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1074. ctrlEvent.handled = true;
  1075. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1076. setParameterValueRT(k, value, true);
  1077. continue;
  1078. }
  1079. // Control backend stuff
  1080. if (event.channel == pData->ctrlChannel)
  1081. {
  1082. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1083. {
  1084. ctrlEvent.handled = true;
  1085. value = ctrlEvent.normalizedValue;
  1086. setDryWetRT(value, true);
  1087. }
  1088. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1089. {
  1090. ctrlEvent.handled = true;
  1091. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  1092. setVolumeRT(value, true);
  1093. }
  1094. else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1095. {
  1096. float left, right;
  1097. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  1098. if (value < 0.0f)
  1099. {
  1100. left = -1.0f;
  1101. right = (value*2.0f)+1.0f;
  1102. }
  1103. else if (value > 0.0f)
  1104. {
  1105. left = (value*2.0f)-1.0f;
  1106. right = 1.0f;
  1107. }
  1108. else
  1109. {
  1110. left = -1.0f;
  1111. right = 1.0f;
  1112. }
  1113. ctrlEvent.handled = true;
  1114. setBalanceLeftRT(left, true);
  1115. setBalanceRightRT(right, true);
  1116. }
  1117. }
  1118. #endif
  1119. // Control plugin parameters
  1120. for (uint32_t k=0; k < pData->param.count; ++k)
  1121. {
  1122. if (pData->param.data[k].midiChannel != event.channel)
  1123. continue;
  1124. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1125. continue;
  1126. if (pData->param.data[k].type != PARAMETER_INPUT)
  1127. continue;
  1128. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1129. continue;
  1130. ctrlEvent.handled = true;
  1131. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1132. setParameterValueRT(k, value, true);
  1133. }
  1134. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1135. if (! ctrlEvent.handled)
  1136. checkForMidiLearn(event);
  1137. #endif
  1138. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1139. {
  1140. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1141. fShmRtClientControl.writeUInt(event.time);
  1142. fShmRtClientControl.writeByte(0); // port
  1143. fShmRtClientControl.writeByte(3); // size
  1144. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1145. fShmRtClientControl.writeByte(uint8_t(ctrlEvent.param));
  1146. fShmRtClientControl.writeByte(uint8_t(ctrlEvent.normalizedValue*127.0f));
  1147. }
  1148. break;
  1149. }
  1150. case kEngineControlEventTypeMidiBank:
  1151. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1152. {
  1153. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiBank);
  1154. fShmRtClientControl.writeUInt(event.time);
  1155. fShmRtClientControl.writeByte(event.channel);
  1156. fShmRtClientControl.writeUShort(event.ctrl.param);
  1157. fShmRtClientControl.commitWrite();
  1158. }
  1159. else if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  1160. {
  1161. // VST2's that use banks usually require both a MSB bank message and a LSB bank message. The MSB bank message can just be 0
  1162. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1163. fShmRtClientControl.writeUInt(event.time);
  1164. fShmRtClientControl.writeByte(0); // port
  1165. fShmRtClientControl.writeByte(3); // size
  1166. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1167. fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT);
  1168. fShmRtClientControl.writeByte(0);
  1169. fShmRtClientControl.commitWrite();
  1170. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1171. fShmRtClientControl.writeUInt(event.time);
  1172. fShmRtClientControl.writeByte(0); // port
  1173. fShmRtClientControl.writeByte(3); // size
  1174. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1175. fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT__LSB);
  1176. fShmRtClientControl.writeByte(uint8_t(event.ctrl.param));
  1177. fShmRtClientControl.commitWrite();
  1178. }
  1179. break;
  1180. case kEngineControlEventTypeMidiProgram:
  1181. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES || pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1182. {
  1183. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiProgram);
  1184. fShmRtClientControl.writeUInt(event.time);
  1185. fShmRtClientControl.writeByte(event.channel);
  1186. fShmRtClientControl.writeUShort(event.ctrl.param);
  1187. fShmRtClientControl.commitWrite();
  1188. }
  1189. break;
  1190. case kEngineControlEventTypeAllSoundOff:
  1191. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1192. {
  1193. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllSoundOff);
  1194. fShmRtClientControl.writeUInt(event.time);
  1195. fShmRtClientControl.writeByte(event.channel);
  1196. fShmRtClientControl.commitWrite();
  1197. }
  1198. break;
  1199. case kEngineControlEventTypeAllNotesOff:
  1200. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1201. {
  1202. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1203. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1204. {
  1205. allNotesOffSent = true;
  1206. postponeRtAllNotesOff();
  1207. }
  1208. #endif
  1209. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllNotesOff);
  1210. fShmRtClientControl.writeUInt(event.time);
  1211. fShmRtClientControl.writeByte(event.channel);
  1212. fShmRtClientControl.commitWrite();
  1213. }
  1214. break;
  1215. } // switch (ctrlEvent.type)
  1216. break;
  1217. } // case kEngineEventTypeControl
  1218. case kEngineEventTypeMidi: {
  1219. const EngineMidiEvent& midiEvent(event.midi);
  1220. if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
  1221. continue;
  1222. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  1223. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  1224. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1225. continue;
  1226. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1227. continue;
  1228. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1229. continue;
  1230. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1231. continue;
  1232. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1233. continue;
  1234. // Fix bad note-off
  1235. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  1236. status = MIDI_STATUS_NOTE_OFF;
  1237. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1238. fShmRtClientControl.writeUInt(event.time);
  1239. fShmRtClientControl.writeByte(midiEvent.port);
  1240. fShmRtClientControl.writeByte(midiEvent.size);
  1241. fShmRtClientControl.writeByte(uint8_t(midiData[0] | (event.channel & MIDI_CHANNEL_BIT)));
  1242. for (uint8_t j=1; j < midiEvent.size; ++j)
  1243. fShmRtClientControl.writeByte(midiData[j]);
  1244. fShmRtClientControl.commitWrite();
  1245. if (status == MIDI_STATUS_NOTE_ON)
  1246. {
  1247. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  1248. true,
  1249. event.channel,
  1250. midiData[1],
  1251. midiData[2],
  1252. 0.0f);
  1253. }
  1254. else if (status == MIDI_STATUS_NOTE_OFF)
  1255. {
  1256. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  1257. true,
  1258. event.channel,
  1259. midiData[1],
  1260. 0, 0.0f);
  1261. }
  1262. } break;
  1263. }
  1264. }
  1265. pData->postRtEvents.trySplice();
  1266. } // End of Event Input
  1267. if (! processSingle(audioIn, audioOut, cvIn, cvOut, frames))
  1268. return;
  1269. // --------------------------------------------------------------------------------------------------------
  1270. // Control and MIDI Output
  1271. if (pData->event.portOut != nullptr)
  1272. {
  1273. float value;
  1274. for (uint32_t k=0; k < pData->param.count; ++k)
  1275. {
  1276. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1277. continue;
  1278. if (pData->param.data[k].mappedControlIndex > 0)
  1279. {
  1280. value = pData->param.ranges[k].getNormalizedValue(fParams[k].value);
  1281. pData->event.portOut->writeControlEvent(0,
  1282. pData->param.data[k].midiChannel,
  1283. kEngineControlEventTypeParameter,
  1284. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1285. -1,
  1286. value);
  1287. }
  1288. }
  1289. uint32_t time;
  1290. uint8_t port, size;
  1291. const uint8_t* midiData(fShmRtClientControl.data->midiOut);
  1292. for (std::size_t read=0; read<kBridgeRtClientDataMidiOutSize-kBridgeBaseMidiOutHeaderSize;)
  1293. {
  1294. // get time
  1295. time = *(const uint32_t*)midiData;
  1296. midiData += 4;
  1297. // get port and size
  1298. port = *midiData++;
  1299. size = *midiData++;
  1300. if (size == 0)
  1301. break;
  1302. // store midi data advancing as needed
  1303. uint8_t data[size];
  1304. for (uint8_t j=0; j<size; ++j)
  1305. data[j] = *midiData++;
  1306. pData->event.portOut->writeMidiEvent(time, size, data);
  1307. read += kBridgeBaseMidiOutHeaderSize + size;
  1308. }
  1309. // TODO
  1310. (void)port;
  1311. } // End of Control and MIDI Output
  1312. }
  1313. bool processSingle(const float* const* const audioIn, float** const audioOut,
  1314. const float* const* const cvIn, float** const cvOut, const uint32_t frames)
  1315. {
  1316. CARLA_SAFE_ASSERT_RETURN(! fTimedError, false);
  1317. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1318. CARLA_SAFE_ASSERT_RETURN(frames <= fBufferSize, false);
  1319. if (pData->audioIn.count > 0)
  1320. {
  1321. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1322. }
  1323. if (pData->audioOut.count > 0)
  1324. {
  1325. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1326. }
  1327. if (pData->cvIn.count > 0)
  1328. {
  1329. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1330. }
  1331. if (pData->cvOut.count > 0)
  1332. {
  1333. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1334. }
  1335. // --------------------------------------------------------------------------------------------------------
  1336. // Try lock, silence otherwise
  1337. #ifndef STOAT_TEST_BUILD
  1338. if (pData->engine->isOffline())
  1339. {
  1340. pData->singleMutex.lock();
  1341. }
  1342. else
  1343. #endif
  1344. if (! pData->singleMutex.tryLock())
  1345. {
  1346. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1347. carla_zeroFloats(audioOut[i], frames);
  1348. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1349. carla_zeroFloats(cvOut[i], frames);
  1350. return false;
  1351. }
  1352. // --------------------------------------------------------------------------------------------------------
  1353. // Reset audio buffers
  1354. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1355. carla_copyFloats(fShmAudioPool.data + (i * fBufferSize), audioIn[i], frames);
  1356. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1357. carla_copyFloats(fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + i) * fBufferSize), cvIn[i], frames);
  1358. // --------------------------------------------------------------------------------------------------------
  1359. // TimeInfo
  1360. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1361. BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  1362. bridgeTimeInfo.playing = timeInfo.playing;
  1363. bridgeTimeInfo.frame = timeInfo.frame;
  1364. bridgeTimeInfo.usecs = timeInfo.usecs;
  1365. bridgeTimeInfo.validFlags = timeInfo.bbt.valid ? kPluginBridgeTimeInfoValidBBT : 0x0;
  1366. if (timeInfo.bbt.valid)
  1367. {
  1368. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  1369. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  1370. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  1371. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1372. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  1373. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1374. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1375. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  1376. }
  1377. // --------------------------------------------------------------------------------------------------------
  1378. // Run plugin
  1379. {
  1380. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientProcess);
  1381. fShmRtClientControl.writeUInt(frames);
  1382. fShmRtClientControl.commitWrite();
  1383. }
  1384. waitForClient("process", fProcWaitTime);
  1385. if (fTimedOut)
  1386. {
  1387. pData->singleMutex.unlock();
  1388. return false;
  1389. }
  1390. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1391. carla_copyFloats(audioOut[i], fShmAudioPool.data + ((pData->audioIn.count + i) * fBufferSize), frames);
  1392. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1393. carla_copyFloats(cvOut[i], fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + pData->cvIn.count + i) * fBufferSize), frames);
  1394. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1395. // --------------------------------------------------------------------------------------------------------
  1396. // Post-processing (dry/wet, volume and balance)
  1397. {
  1398. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1399. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1400. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1401. const bool isMono = (pData->audioIn.count == 1);
  1402. bool isPair;
  1403. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1404. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1405. {
  1406. // Dry/Wet
  1407. if (doDryWet)
  1408. {
  1409. const uint32_t c = isMono ? 0 : i;
  1410. for (uint32_t k=0; k < frames; ++k)
  1411. {
  1412. # ifndef BUILD_BRIDGE
  1413. if (k < pData->latency.frames && pData->latency.buffers != nullptr)
  1414. bufValue = pData->latency.buffers[c][k];
  1415. else if (pData->latency.frames < frames)
  1416. bufValue = audioIn[c][k-pData->latency.frames];
  1417. else
  1418. # endif
  1419. bufValue = audioIn[c][k];
  1420. audioOut[i][k] = (audioOut[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1421. }
  1422. }
  1423. // Balance
  1424. if (doBalance)
  1425. {
  1426. isPair = (i % 2 == 0);
  1427. if (isPair)
  1428. {
  1429. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1430. carla_copyFloats(oldBufLeft, audioOut[i], frames);
  1431. }
  1432. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1433. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1434. for (uint32_t k=0; k < frames; ++k)
  1435. {
  1436. if (isPair)
  1437. {
  1438. // left
  1439. audioOut[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1440. audioOut[i][k] += audioOut[i+1][k] * (1.0f - balRangeR);
  1441. }
  1442. else
  1443. {
  1444. // right
  1445. audioOut[i][k] = audioOut[i][k] * balRangeR;
  1446. audioOut[i][k] += oldBufLeft[k] * balRangeL;
  1447. }
  1448. }
  1449. }
  1450. // Volume (and buffer copy)
  1451. if (doVolume)
  1452. {
  1453. for (uint32_t k=0; k < frames; ++k)
  1454. audioOut[i][k] *= pData->postProc.volume;
  1455. }
  1456. }
  1457. } // End of Post-processing
  1458. # ifndef BUILD_BRIDGE
  1459. // --------------------------------------------------------------------------------------------------------
  1460. // Save latency values for next callback
  1461. if (pData->latency.frames != 0 && pData->latency.buffers != nullptr)
  1462. {
  1463. const uint32_t latframes = pData->latency.frames;
  1464. if (latframes <= frames)
  1465. {
  1466. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1467. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  1468. }
  1469. else
  1470. {
  1471. const uint32_t diff = latframes - frames;
  1472. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1473. {
  1474. // push back buffer by 'frames'
  1475. for (k=0; k < diff; ++k)
  1476. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1477. // put current input at the end
  1478. for (uint32_t j=0; k < latframes; ++j, ++k)
  1479. pData->latency.buffers[i][k] = audioIn[i][j];
  1480. }
  1481. }
  1482. }
  1483. # endif
  1484. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1485. // --------------------------------------------------------------------------------------------------------
  1486. pData->singleMutex.unlock();
  1487. return true;
  1488. }
  1489. void bufferSizeChanged(const uint32_t newBufferSize) override
  1490. {
  1491. fBufferSize = newBufferSize;
  1492. resizeAudioPool(newBufferSize);
  1493. {
  1494. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetBufferSize);
  1495. fShmRtClientControl.writeUInt(newBufferSize);
  1496. fShmRtClientControl.commitWrite();
  1497. }
  1498. //fProcWaitTime = newBufferSize*1000/pData->engine->getSampleRate();
  1499. fProcWaitTime = 1000;
  1500. waitForClient("buffersize", 1000);
  1501. }
  1502. void sampleRateChanged(const double newSampleRate) override
  1503. {
  1504. {
  1505. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetSampleRate);
  1506. fShmRtClientControl.writeDouble(newSampleRate);
  1507. fShmRtClientControl.commitWrite();
  1508. }
  1509. //fProcWaitTime = pData->engine->getBufferSize()*1000/newSampleRate;
  1510. fProcWaitTime = 1000;
  1511. waitForClient("samplerate", 1000);
  1512. }
  1513. void offlineModeChanged(const bool isOffline) override
  1514. {
  1515. {
  1516. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetOnline);
  1517. fShmRtClientControl.writeBool(isOffline);
  1518. fShmRtClientControl.commitWrite();
  1519. }
  1520. waitForClient("offline", 1000);
  1521. }
  1522. // -------------------------------------------------------------------
  1523. // Plugin buffers
  1524. void clearBuffers() noexcept override
  1525. {
  1526. if (fParams != nullptr)
  1527. {
  1528. delete[] fParams;
  1529. fParams = nullptr;
  1530. }
  1531. CarlaPlugin::clearBuffers();
  1532. }
  1533. // -------------------------------------------------------------------
  1534. // Post-poned UI Stuff
  1535. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1536. {
  1537. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1538. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1539. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiParameterChange);
  1540. fShmNonRtClientControl.writeUInt(index);
  1541. fShmNonRtClientControl.writeFloat(value);
  1542. fShmNonRtClientControl.commitWrite();
  1543. }
  1544. void uiProgramChange(const uint32_t index) noexcept override
  1545. {
  1546. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  1547. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1548. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiProgramChange);
  1549. fShmNonRtClientControl.writeUInt(index);
  1550. fShmNonRtClientControl.commitWrite();
  1551. }
  1552. void uiMidiProgramChange(const uint32_t index) noexcept override
  1553. {
  1554. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1555. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1556. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiMidiProgramChange);
  1557. fShmNonRtClientControl.writeUInt(index);
  1558. fShmNonRtClientControl.commitWrite();
  1559. }
  1560. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1561. {
  1562. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1563. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1564. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1565. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1566. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOn);
  1567. fShmNonRtClientControl.writeByte(channel);
  1568. fShmNonRtClientControl.writeByte(note);
  1569. fShmNonRtClientControl.writeByte(velo);
  1570. fShmNonRtClientControl.commitWrite();
  1571. }
  1572. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1573. {
  1574. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1575. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1576. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1577. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOff);
  1578. fShmNonRtClientControl.writeByte(channel);
  1579. fShmNonRtClientControl.writeByte(note);
  1580. fShmNonRtClientControl.commitWrite();
  1581. }
  1582. // -------------------------------------------------------------------
  1583. // Internal helper functions
  1584. void restoreLV2State(bool) noexcept override
  1585. {
  1586. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1587. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientRestoreLV2State);
  1588. fShmNonRtClientControl.commitWrite();
  1589. }
  1590. void waitForBridgeSaveSignal() noexcept override
  1591. {
  1592. // VSTs only save chunks, for which we already have a waitForSaved there
  1593. if (fPluginType != PLUGIN_VST2)
  1594. waitForSaved();
  1595. }
  1596. // -------------------------------------------------------------------
  1597. void handleNonRtData()
  1598. {
  1599. for (; fShmNonRtServerControl.isDataAvailableForReading();)
  1600. {
  1601. const PluginBridgeNonRtServerOpcode opcode(fShmNonRtServerControl.readOpcode());
  1602. #ifdef DEBUG
  1603. if (opcode != kPluginBridgeNonRtServerPong && opcode != kPluginBridgeNonRtServerParameterValue2) {
  1604. carla_debug("CarlaPluginBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtServerOpcode2str(opcode));
  1605. }
  1606. #endif
  1607. switch (opcode)
  1608. {
  1609. case kPluginBridgeNonRtServerNull:
  1610. case kPluginBridgeNonRtServerPong:
  1611. break;
  1612. case kPluginBridgeNonRtServerVersion:
  1613. fBridgeVersion = fShmNonRtServerControl.readUInt();
  1614. break;
  1615. case kPluginBridgeNonRtServerPluginInfo1: {
  1616. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  1617. const uint32_t category = fShmNonRtServerControl.readUInt();
  1618. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1619. const uint32_t optionAv = fShmNonRtServerControl.readUInt();
  1620. const uint32_t optionEn = fShmNonRtServerControl.readUInt();
  1621. const int64_t uniqueId = fShmNonRtServerControl.readLong();
  1622. if (fUniqueId != 0) {
  1623. CARLA_SAFE_ASSERT_INT2(fUniqueId == uniqueId, fUniqueId, uniqueId);
  1624. }
  1625. pData->hints = hints | PLUGIN_IS_BRIDGE;
  1626. pData->options = optionEn;
  1627. fInfo.category = static_cast<PluginCategory>(category);
  1628. fInfo.optionsAvailable = optionAv;
  1629. } break;
  1630. case kPluginBridgeNonRtServerPluginInfo2: {
  1631. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  1632. // realName
  1633. const uint32_t realNameSize(fShmNonRtServerControl.readUInt());
  1634. char realName[realNameSize+1];
  1635. carla_zeroChars(realName, realNameSize+1);
  1636. fShmNonRtServerControl.readCustomData(realName, realNameSize);
  1637. // label
  1638. const uint32_t labelSize(fShmNonRtServerControl.readUInt());
  1639. char label[labelSize+1];
  1640. carla_zeroChars(label, labelSize+1);
  1641. fShmNonRtServerControl.readCustomData(label, labelSize);
  1642. // maker
  1643. const uint32_t makerSize(fShmNonRtServerControl.readUInt());
  1644. char maker[makerSize+1];
  1645. carla_zeroChars(maker, makerSize+1);
  1646. fShmNonRtServerControl.readCustomData(maker, makerSize);
  1647. // copyright
  1648. const uint32_t copyrightSize(fShmNonRtServerControl.readUInt());
  1649. char copyright[copyrightSize+1];
  1650. carla_zeroChars(copyright, copyrightSize+1);
  1651. fShmNonRtServerControl.readCustomData(copyright, copyrightSize);
  1652. fInfo.name = realName;
  1653. fInfo.label = label;
  1654. fInfo.maker = maker;
  1655. fInfo.copyright = copyright;
  1656. if (pData->name == nullptr)
  1657. pData->name = pData->engine->getUniquePluginName(realName);
  1658. } break;
  1659. case kPluginBridgeNonRtServerAudioCount: {
  1660. // uint/ins, uint/outs
  1661. fInfo.clear();
  1662. fInfo.aIns = fShmNonRtServerControl.readUInt();
  1663. fInfo.aOuts = fShmNonRtServerControl.readUInt();
  1664. if (fInfo.aIns > 0)
  1665. {
  1666. fInfo.aInNames = new const char*[fInfo.aIns];
  1667. carla_zeroPointers(fInfo.aInNames, fInfo.aIns);
  1668. }
  1669. if (fInfo.aOuts > 0)
  1670. {
  1671. fInfo.aOutNames = new const char*[fInfo.aOuts];
  1672. carla_zeroPointers(fInfo.aOutNames, fInfo.aOuts);
  1673. }
  1674. } break;
  1675. case kPluginBridgeNonRtServerMidiCount: {
  1676. // uint/ins, uint/outs
  1677. fInfo.mIns = fShmNonRtServerControl.readUInt();
  1678. fInfo.mOuts = fShmNonRtServerControl.readUInt();
  1679. } break;
  1680. case kPluginBridgeNonRtServerCvCount: {
  1681. // uint/ins, uint/outs
  1682. fInfo.cvIns = fShmNonRtServerControl.readUInt();
  1683. fInfo.cvOuts = fShmNonRtServerControl.readUInt();
  1684. } break;
  1685. case kPluginBridgeNonRtServerParameterCount: {
  1686. // uint/count
  1687. const uint32_t count = fShmNonRtServerControl.readUInt();
  1688. // delete old data
  1689. pData->param.clear();
  1690. if (fParams != nullptr)
  1691. {
  1692. delete[] fParams;
  1693. fParams = nullptr;
  1694. }
  1695. if (count > 0)
  1696. {
  1697. pData->param.createNew(count, false);
  1698. fParams = new BridgeParamInfo[count];
  1699. // we might not receive all parameter data, so ensure range max is not 0
  1700. for (uint32_t i=0; i<count; ++i)
  1701. {
  1702. pData->param.ranges[i].def = 0.0f;
  1703. pData->param.ranges[i].min = 0.0f;
  1704. pData->param.ranges[i].max = 1.0f;
  1705. pData->param.ranges[i].step = 0.001f;
  1706. pData->param.ranges[i].stepSmall = 0.0001f;
  1707. pData->param.ranges[i].stepLarge = 0.1f;
  1708. }
  1709. }
  1710. } break;
  1711. case kPluginBridgeNonRtServerProgramCount: {
  1712. // uint/count
  1713. pData->prog.clear();
  1714. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1715. pData->prog.createNew(count);
  1716. } break;
  1717. case kPluginBridgeNonRtServerMidiProgramCount: {
  1718. // uint/count
  1719. pData->midiprog.clear();
  1720. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1721. pData->midiprog.createNew(count);
  1722. } break;
  1723. case kPluginBridgeNonRtServerPortName: {
  1724. // byte/type, uint/index, uint/size, str[] (name)
  1725. const uint8_t portType = fShmNonRtServerControl.readByte();
  1726. const uint32_t index = fShmNonRtServerControl.readUInt();
  1727. // name
  1728. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1729. char* const name = new char[nameSize+1];
  1730. carla_zeroChars(name, nameSize+1);
  1731. fShmNonRtServerControl.readCustomData(name, nameSize);
  1732. CARLA_SAFE_ASSERT_BREAK(portType > kPluginBridgePortNull && portType < kPluginBridgePortTypeCount);
  1733. switch (portType)
  1734. {
  1735. case kPluginBridgePortAudioInput:
  1736. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aIns);
  1737. fInfo.aInNames[index] = name;
  1738. break;
  1739. case kPluginBridgePortAudioOutput:
  1740. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aOuts);
  1741. fInfo.aOutNames[index] = name;
  1742. break;
  1743. }
  1744. } break;
  1745. case kPluginBridgeNonRtServerParameterData1: {
  1746. // uint/index, int/rindex, uint/type, uint/hints, int/cc
  1747. const uint32_t index = fShmNonRtServerControl.readUInt();
  1748. const int32_t rindex = fShmNonRtServerControl.readInt();
  1749. const uint32_t type = fShmNonRtServerControl.readUInt();
  1750. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1751. const int16_t ctrl = fShmNonRtServerControl.readShort();
  1752. CARLA_SAFE_ASSERT_BREAK(ctrl >= CONTROL_INDEX_NONE && ctrl <= CONTROL_INDEX_MAX_ALLOWED);
  1753. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1754. if (index < pData->param.count)
  1755. {
  1756. pData->param.data[index].type = static_cast<ParameterType>(type);
  1757. pData->param.data[index].index = static_cast<int32_t>(index);
  1758. pData->param.data[index].rindex = rindex;
  1759. pData->param.data[index].hints = hints;
  1760. pData->param.data[index].mappedControlIndex = ctrl;
  1761. }
  1762. } break;
  1763. case kPluginBridgeNonRtServerParameterData2: {
  1764. // uint/index, uint/size, str[] (name), uint/size, str[] (unit)
  1765. const uint32_t index = fShmNonRtServerControl.readUInt();
  1766. // name
  1767. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1768. char name[nameSize+1];
  1769. carla_zeroChars(name, nameSize+1);
  1770. fShmNonRtServerControl.readCustomData(name, nameSize);
  1771. // symbol
  1772. const uint32_t symbolSize(fShmNonRtServerControl.readUInt());
  1773. char symbol[symbolSize+1];
  1774. carla_zeroChars(symbol, symbolSize+1);
  1775. fShmNonRtServerControl.readCustomData(symbol, symbolSize);
  1776. // unit
  1777. const uint32_t unitSize(fShmNonRtServerControl.readUInt());
  1778. char unit[unitSize+1];
  1779. carla_zeroChars(unit, unitSize+1);
  1780. fShmNonRtServerControl.readCustomData(unit, unitSize);
  1781. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1782. if (index < pData->param.count)
  1783. {
  1784. fParams[index].name = name;
  1785. fParams[index].symbol = symbol;
  1786. fParams[index].unit = unit;
  1787. }
  1788. } break;
  1789. case kPluginBridgeNonRtServerParameterRanges: {
  1790. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  1791. const uint32_t index = fShmNonRtServerControl.readUInt();
  1792. const float def = fShmNonRtServerControl.readFloat();
  1793. const float min = fShmNonRtServerControl.readFloat();
  1794. const float max = fShmNonRtServerControl.readFloat();
  1795. const float step = fShmNonRtServerControl.readFloat();
  1796. const float stepSmall = fShmNonRtServerControl.readFloat();
  1797. const float stepLarge = fShmNonRtServerControl.readFloat();
  1798. CARLA_SAFE_ASSERT_BREAK(min < max);
  1799. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1800. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1801. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1802. if (index < pData->param.count)
  1803. {
  1804. pData->param.ranges[index].def = def;
  1805. pData->param.ranges[index].min = min;
  1806. pData->param.ranges[index].max = max;
  1807. pData->param.ranges[index].step = step;
  1808. pData->param.ranges[index].stepSmall = stepSmall;
  1809. pData->param.ranges[index].stepLarge = stepLarge;
  1810. }
  1811. } break;
  1812. case kPluginBridgeNonRtServerParameterValue: {
  1813. // uint/index, float/value
  1814. const uint32_t index = fShmNonRtServerControl.readUInt();
  1815. const float value = fShmNonRtServerControl.readFloat();
  1816. if (index < pData->param.count)
  1817. {
  1818. const float fixedValue(pData->param.getFixedValue(index, value));
  1819. if (carla_isNotEqual(fParams[index].value, fixedValue))
  1820. {
  1821. fParams[index].value = fixedValue;
  1822. CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
  1823. }
  1824. }
  1825. } break;
  1826. case kPluginBridgeNonRtServerParameterValue2: {
  1827. // uint/index, float/value
  1828. const uint32_t index = fShmNonRtServerControl.readUInt();
  1829. const float value = fShmNonRtServerControl.readFloat();
  1830. if (index < pData->param.count)
  1831. {
  1832. const float fixedValue(pData->param.getFixedValue(index, value));
  1833. fParams[index].value = fixedValue;
  1834. }
  1835. } break;
  1836. case kPluginBridgeNonRtServerParameterTouch: {
  1837. // uint/index, bool/touch
  1838. const uint32_t index = fShmNonRtServerControl.readUInt();
  1839. const bool touch = fShmNonRtServerControl.readBool();
  1840. pData->engine->touchPluginParameter(pData->id, index, touch);
  1841. } break;
  1842. case kPluginBridgeNonRtServerDefaultValue: {
  1843. // uint/index, float/value
  1844. const uint32_t index = fShmNonRtServerControl.readUInt();
  1845. const float value = fShmNonRtServerControl.readFloat();
  1846. if (index < pData->param.count)
  1847. pData->param.ranges[index].def = value;
  1848. } break;
  1849. case kPluginBridgeNonRtServerCurrentProgram: {
  1850. // int/index
  1851. const int32_t index = fShmNonRtServerControl.readInt();
  1852. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1853. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1854. CarlaPlugin::setProgram(index, false, true, true);
  1855. } break;
  1856. case kPluginBridgeNonRtServerCurrentMidiProgram: {
  1857. // int/index
  1858. const int32_t index = fShmNonRtServerControl.readInt();
  1859. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1860. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1861. CarlaPlugin::setMidiProgram(index, false, true, true);
  1862. } break;
  1863. case kPluginBridgeNonRtServerProgramName: {
  1864. // uint/index, uint/size, str[] (name)
  1865. const uint32_t index = fShmNonRtServerControl.readUInt();
  1866. // name
  1867. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1868. char name[nameSize+1];
  1869. carla_zeroChars(name, nameSize+1);
  1870. fShmNonRtServerControl.readCustomData(name, nameSize);
  1871. CARLA_SAFE_ASSERT_INT2(index < pData->prog.count, index, pData->prog.count);
  1872. if (index < pData->prog.count)
  1873. {
  1874. if (pData->prog.names[index] != nullptr)
  1875. delete[] pData->prog.names[index];
  1876. pData->prog.names[index] = carla_strdup(name);
  1877. }
  1878. } break;
  1879. case kPluginBridgeNonRtServerMidiProgramData: {
  1880. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  1881. const uint32_t index = fShmNonRtServerControl.readUInt();
  1882. const uint32_t bank = fShmNonRtServerControl.readUInt();
  1883. const uint32_t program = fShmNonRtServerControl.readUInt();
  1884. // name
  1885. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1886. char name[nameSize+1];
  1887. carla_zeroChars(name, nameSize+1);
  1888. fShmNonRtServerControl.readCustomData(name, nameSize);
  1889. CARLA_SAFE_ASSERT_INT2(index < pData->midiprog.count, index, pData->midiprog.count);
  1890. if (index < pData->midiprog.count)
  1891. {
  1892. if (pData->midiprog.data[index].name != nullptr)
  1893. delete[] pData->midiprog.data[index].name;
  1894. pData->midiprog.data[index].bank = bank;
  1895. pData->midiprog.data[index].program = program;
  1896. pData->midiprog.data[index].name = carla_strdup(name);
  1897. }
  1898. } break;
  1899. case kPluginBridgeNonRtServerSetCustomData: {
  1900. // uint/size, str[], uint/size, str[], uint/size, str[]
  1901. // type
  1902. const uint32_t typeSize(fShmNonRtServerControl.readUInt());
  1903. char type[typeSize+1];
  1904. carla_zeroChars(type, typeSize+1);
  1905. fShmNonRtServerControl.readCustomData(type, typeSize);
  1906. // key
  1907. const uint32_t keySize(fShmNonRtServerControl.readUInt());
  1908. char key[keySize+1];
  1909. carla_zeroChars(key, keySize+1);
  1910. fShmNonRtServerControl.readCustomData(key, keySize);
  1911. // value
  1912. const uint32_t valueSize(fShmNonRtServerControl.readUInt());
  1913. char value[valueSize+1];
  1914. carla_zeroChars(value, valueSize+1);
  1915. if (valueSize > 0)
  1916. fShmNonRtServerControl.readCustomData(value, valueSize);
  1917. CarlaPlugin::setCustomData(type, key, value, false);
  1918. } break;
  1919. case kPluginBridgeNonRtServerSetChunkDataFile: {
  1920. // uint/size, str[] (filename)
  1921. // chunkFilePath
  1922. const uint32_t chunkFilePathSize(fShmNonRtServerControl.readUInt());
  1923. char chunkFilePath[chunkFilePathSize+1];
  1924. carla_zeroChars(chunkFilePath, chunkFilePathSize+1);
  1925. fShmNonRtServerControl.readCustomData(chunkFilePath, chunkFilePathSize);
  1926. String realChunkFilePath(chunkFilePath);
  1927. #ifndef CARLA_OS_WIN
  1928. // Using Wine, fix temp dir
  1929. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1930. {
  1931. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1932. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1933. realChunkFilePath = fWinePrefix;
  1934. realChunkFilePath += "/drive_";
  1935. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1936. realChunkFilePath += driveLetterSplit[1];
  1937. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1938. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1939. }
  1940. #endif
  1941. File chunkFile(realChunkFilePath);
  1942. CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
  1943. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1944. chunkFile.deleteFile();
  1945. } break;
  1946. case kPluginBridgeNonRtServerSetLatency:
  1947. // uint
  1948. fLatency = fShmNonRtServerControl.readUInt();
  1949. #ifndef BUILD_BRIDGE
  1950. if (! fInitiated)
  1951. pData->latency.recreateBuffers(std::max(fInfo.aIns, fInfo.aOuts), fLatency);
  1952. #endif
  1953. break;
  1954. case kPluginBridgeNonRtServerSetParameterText: {
  1955. const int32_t index = fShmNonRtServerControl.readInt();
  1956. const uint32_t textSize(fShmNonRtServerControl.readUInt());
  1957. char text[textSize+1];
  1958. carla_zeroChars(text, textSize+1);
  1959. fShmNonRtServerControl.readCustomData(text, textSize);
  1960. fReceivingParamText.setReceivedData(index, text, textSize);
  1961. } break;
  1962. case kPluginBridgeNonRtServerReady:
  1963. fInitiated = true;
  1964. break;
  1965. case kPluginBridgeNonRtServerSaved:
  1966. fSaved = true;
  1967. break;
  1968. case kPluginBridgeNonRtServerUiClosed:
  1969. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1970. pData->transientTryCounter = 0;
  1971. #endif
  1972. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  1973. break;
  1974. case kPluginBridgeNonRtServerError: {
  1975. // error
  1976. const uint32_t errorSize(fShmNonRtServerControl.readUInt());
  1977. char error[errorSize+1];
  1978. carla_zeroChars(error, errorSize+1);
  1979. fShmNonRtServerControl.readCustomData(error, errorSize);
  1980. if (fInitiated)
  1981. {
  1982. pData->engine->callback(true, true, ENGINE_CALLBACK_ERROR, pData->id, 0, 0, 0, 0.0f, error);
  1983. // just in case
  1984. pData->engine->setLastError(error);
  1985. fInitError = true;
  1986. }
  1987. else
  1988. {
  1989. pData->engine->setLastError(error);
  1990. fInitError = true;
  1991. fInitiated = true;
  1992. }
  1993. } break;
  1994. }
  1995. }
  1996. }
  1997. // -------------------------------------------------------------------
  1998. uintptr_t getUiBridgeProcessId() const noexcept override
  1999. {
  2000. return fBridgeThread.getProcessPID();
  2001. }
  2002. const void* getExtraStuff() const noexcept override
  2003. {
  2004. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  2005. }
  2006. // -------------------------------------------------------------------
  2007. bool init(CarlaPluginPtr plugin,
  2008. const char* const filename,
  2009. const char* const name,
  2010. const char* const label,
  2011. const int64_t uniqueId,
  2012. const uint options,
  2013. const char* const bridgeBinary)
  2014. {
  2015. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2016. // ---------------------------------------------------------------
  2017. // first checks
  2018. if (pData->client != nullptr)
  2019. {
  2020. pData->engine->setLastError("Plugin client is already registered");
  2021. return false;
  2022. }
  2023. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2024. {
  2025. pData->engine->setLastError("null bridge binary");
  2026. return false;
  2027. }
  2028. // ---------------------------------------------------------------
  2029. // set info
  2030. if (name != nullptr && name[0] != '\0')
  2031. pData->name = pData->engine->getUniquePluginName(name);
  2032. if (filename != nullptr && filename[0] != '\0')
  2033. pData->filename = carla_strdup(filename);
  2034. else
  2035. pData->filename = carla_strdup("");
  2036. fUniqueId = uniqueId;
  2037. fBridgeBinary = bridgeBinary;
  2038. std::srand(static_cast<uint>(std::time(nullptr)));
  2039. // ---------------------------------------------------------------
  2040. // init sem/shm
  2041. if (! fShmAudioPool.initializeServer())
  2042. {
  2043. carla_stderr("Failed to initialize shared memory audio pool");
  2044. return false;
  2045. }
  2046. if (! fShmRtClientControl.initializeServer())
  2047. {
  2048. carla_stderr("Failed to initialize RT client control");
  2049. fShmAudioPool.clear();
  2050. return false;
  2051. }
  2052. if (! fShmNonRtClientControl.initializeServer())
  2053. {
  2054. carla_stderr("Failed to initialize Non-RT client control");
  2055. fShmRtClientControl.clear();
  2056. fShmAudioPool.clear();
  2057. return false;
  2058. }
  2059. if (! fShmNonRtServerControl.initializeServer())
  2060. {
  2061. carla_stderr("Failed to initialize Non-RT server control");
  2062. fShmNonRtClientControl.clear();
  2063. fShmRtClientControl.clear();
  2064. fShmAudioPool.clear();
  2065. return false;
  2066. }
  2067. #ifndef CARLA_OS_WIN
  2068. // ---------------------------------------------------------------
  2069. // set wine prefix
  2070. if (fBridgeBinary.contains(".exe", true))
  2071. {
  2072. const EngineOptions& engineOptions(pData->engine->getOptions());
  2073. if (engineOptions.wine.autoPrefix)
  2074. fWinePrefix = findWinePrefix(pData->filename);
  2075. if (fWinePrefix.isEmpty())
  2076. {
  2077. const char* const envWinePrefix(std::getenv("WINEPREFIX"));
  2078. if (envWinePrefix != nullptr && envWinePrefix[0] != '\0')
  2079. fWinePrefix = envWinePrefix;
  2080. else if (engineOptions.wine.fallbackPrefix != nullptr && engineOptions.wine.fallbackPrefix[0] != '\0')
  2081. fWinePrefix = engineOptions.wine.fallbackPrefix;
  2082. else
  2083. fWinePrefix = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  2084. }
  2085. }
  2086. #endif
  2087. // ---------------------------------------------------------------
  2088. // init bridge thread
  2089. {
  2090. char shmIdsStr[6*4+1];
  2091. carla_zeroChars(shmIdsStr, 6*4+1);
  2092. std::strncpy(shmIdsStr+6*0, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  2093. std::strncpy(shmIdsStr+6*1, &fShmRtClientControl.filename[fShmRtClientControl.filename.length()-6], 6);
  2094. std::strncpy(shmIdsStr+6*2, &fShmNonRtClientControl.filename[fShmNonRtClientControl.filename.length()-6], 6);
  2095. std::strncpy(shmIdsStr+6*3, &fShmNonRtServerControl.filename[fShmNonRtServerControl.filename.length()-6], 6);
  2096. fBridgeThread.setData(
  2097. #ifndef CARLA_OS_WIN
  2098. fWinePrefix.toRawUTF8(),
  2099. #endif
  2100. bridgeBinary, label, shmIdsStr);
  2101. }
  2102. if (! restartBridgeThread())
  2103. return false;
  2104. // ---------------------------------------------------------------
  2105. // register client
  2106. if (pData->name == nullptr)
  2107. {
  2108. if (label != nullptr && label[0] != '\0')
  2109. pData->name = pData->engine->getUniquePluginName(label);
  2110. else
  2111. pData->name = pData->engine->getUniquePluginName("unknown");
  2112. }
  2113. pData->client = pData->engine->addClient(plugin);
  2114. if (pData->client == nullptr || ! pData->client->isOk())
  2115. {
  2116. pData->engine->setLastError("Failed to register plugin client");
  2117. return false;
  2118. }
  2119. // ---------------------------------------------------------------
  2120. // set options
  2121. pData->options = 0x0;
  2122. if ((fInfo.optionsAvailable & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0)
  2123. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2124. else if (isPluginOptionEnabled(options, PLUGIN_OPTION_FIXED_BUFFERS))
  2125. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2126. if (pData->engine->getOptions().forceStereo)
  2127. {
  2128. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2129. }
  2130. else if (fInfo.optionsAvailable & PLUGIN_OPTION_FORCE_STEREO)
  2131. {
  2132. if (options & PLUGIN_OPTION_FORCE_STEREO)
  2133. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2134. }
  2135. if (fInfo.optionsAvailable & PLUGIN_OPTION_USE_CHUNKS)
  2136. if (isPluginOptionEnabled(options, PLUGIN_OPTION_USE_CHUNKS))
  2137. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  2138. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  2139. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2140. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2141. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  2142. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2143. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2144. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  2145. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2146. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2147. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PITCHBEND)
  2148. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2149. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2150. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2151. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2152. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2153. if (fInfo.optionsAvailable & PLUGIN_OPTION_SKIP_SENDING_NOTES)
  2154. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  2155. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  2156. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2157. {
  2158. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  2159. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2160. }
  2161. else if (fInfo.optionsAvailable & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2162. {
  2163. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2164. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2165. }
  2166. // kPluginBridgeNonRtClientSetOptions was added in API 7
  2167. if (fBridgeVersion >= 7)
  2168. {
  2169. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2170. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOptions);
  2171. fShmNonRtClientControl.writeUInt(pData->options);
  2172. fShmNonRtClientControl.commitWrite();
  2173. }
  2174. return true;
  2175. }
  2176. private:
  2177. const BinaryType fBinaryType;
  2178. const PluginType fPluginType;
  2179. uint fBridgeVersion;
  2180. bool fInitiated;
  2181. bool fInitError;
  2182. bool fSaved;
  2183. bool fTimedOut;
  2184. bool fTimedError;
  2185. uint fBufferSize;
  2186. uint fProcWaitTime;
  2187. CarlaString fBridgeBinary;
  2188. CarlaPluginBridgeThread fBridgeThread;
  2189. BridgeAudioPool fShmAudioPool;
  2190. BridgeRtClientControl fShmRtClientControl;
  2191. BridgeNonRtClientControl fShmNonRtClientControl;
  2192. BridgeNonRtServerControl fShmNonRtServerControl;
  2193. #ifndef CARLA_OS_WIN
  2194. String fWinePrefix;
  2195. #endif
  2196. class ReceivingParamText {
  2197. public:
  2198. ReceivingParamText() noexcept
  2199. : dataRecv(false),
  2200. dataOk(false),
  2201. index(-1),
  2202. strBuf(nullptr),
  2203. mutex() {}
  2204. bool isCurrentlyWaitingData() const noexcept
  2205. {
  2206. return index >= 0;
  2207. }
  2208. bool wasDataReceived(bool* const success) const noexcept
  2209. {
  2210. *success = dataOk;
  2211. return dataRecv;
  2212. }
  2213. void setTargetData(const int32_t i, char* const b) noexcept
  2214. {
  2215. const CarlaMutexLocker cml(mutex);
  2216. dataOk = false;
  2217. dataRecv = false;
  2218. index = i;
  2219. strBuf = b;
  2220. }
  2221. void setReceivedData(const int32_t i, const char* const b, const uint blen) noexcept
  2222. {
  2223. CarlaScopedValueSetter<bool> svs(dataRecv, false, true);
  2224. const CarlaMutexLocker cml(mutex);
  2225. // make backup and reset data
  2226. const int32_t indexCopy = index;
  2227. char* const strBufCopy = strBuf;
  2228. index = -1;
  2229. strBuf = nullptr;
  2230. CARLA_SAFE_ASSERT_RETURN(indexCopy == i,);
  2231. CARLA_SAFE_ASSERT_RETURN(strBufCopy != nullptr,);
  2232. std::strncpy(strBufCopy, b, std::min(blen, STR_MAX-1U));
  2233. dataOk = true;
  2234. }
  2235. private:
  2236. bool dataRecv;
  2237. bool dataOk;
  2238. int32_t index;
  2239. char* strBuf;
  2240. CarlaMutex mutex;
  2241. CARLA_DECLARE_NON_COPY_CLASS(ReceivingParamText)
  2242. } fReceivingParamText;
  2243. struct Info {
  2244. uint32_t aIns, aOuts;
  2245. uint32_t cvIns, cvOuts;
  2246. uint32_t mIns, mOuts;
  2247. PluginCategory category;
  2248. uint optionsAvailable;
  2249. CarlaString name;
  2250. CarlaString label;
  2251. CarlaString maker;
  2252. CarlaString copyright;
  2253. const char** aInNames;
  2254. const char** aOutNames;
  2255. const char** cvInNames;
  2256. const char** cvOutNames;
  2257. std::vector<uint8_t> chunk;
  2258. Info()
  2259. : aIns(0),
  2260. aOuts(0),
  2261. cvIns(0),
  2262. cvOuts(0),
  2263. mIns(0),
  2264. mOuts(0),
  2265. category(PLUGIN_CATEGORY_NONE),
  2266. optionsAvailable(0),
  2267. name(),
  2268. label(),
  2269. maker(),
  2270. copyright(),
  2271. aInNames(nullptr),
  2272. aOutNames(nullptr),
  2273. cvInNames(nullptr),
  2274. cvOutNames(nullptr),
  2275. chunk() {}
  2276. ~Info()
  2277. {
  2278. clear();
  2279. }
  2280. void clear()
  2281. {
  2282. if (aInNames != nullptr)
  2283. {
  2284. CARLA_SAFE_ASSERT_INT(aIns > 0, aIns);
  2285. for (uint32_t i=0; i<aIns; ++i)
  2286. delete[] aInNames[i];
  2287. delete[] aInNames;
  2288. aInNames = nullptr;
  2289. }
  2290. if (aOutNames != nullptr)
  2291. {
  2292. CARLA_SAFE_ASSERT_INT(aOuts > 0, aOuts);
  2293. for (uint32_t i=0; i<aOuts; ++i)
  2294. delete[] aOutNames[i];
  2295. delete[] aOutNames;
  2296. aOutNames = nullptr;
  2297. }
  2298. if (cvInNames != nullptr)
  2299. {
  2300. CARLA_SAFE_ASSERT_INT(cvIns > 0, cvIns);
  2301. for (uint32_t i=0; i<cvIns; ++i)
  2302. delete[] cvInNames[i];
  2303. delete[] cvInNames;
  2304. cvInNames = nullptr;
  2305. }
  2306. if (cvOutNames != nullptr)
  2307. {
  2308. CARLA_SAFE_ASSERT_INT(cvOuts > 0, cvOuts);
  2309. for (uint32_t i=0; i<cvOuts; ++i)
  2310. delete[] cvOutNames[i];
  2311. delete[] cvOutNames;
  2312. cvOutNames = nullptr;
  2313. }
  2314. aIns = aOuts = cvIns = cvOuts = 0;
  2315. }
  2316. CARLA_DECLARE_NON_COPY_STRUCT(Info)
  2317. } fInfo;
  2318. int64_t fUniqueId;
  2319. uint32_t fLatency;
  2320. BridgeParamInfo* fParams;
  2321. void handleProcessStopped() noexcept
  2322. {
  2323. const bool wasActive = pData->active;
  2324. pData->active = false;
  2325. if (wasActive)
  2326. {
  2327. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2328. pData->engine->callback(true, true,
  2329. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  2330. pData->id,
  2331. PARAMETER_ACTIVE,
  2332. 0, 0,
  2333. 0.0f,
  2334. nullptr);
  2335. #endif
  2336. }
  2337. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  2338. {
  2339. pData->engine->callback(true, true,
  2340. ENGINE_CALLBACK_UI_STATE_CHANGED,
  2341. pData->id,
  2342. 0,
  2343. 0, 0, 0.0f, nullptr);
  2344. }
  2345. }
  2346. void resizeAudioPool(const uint32_t bufferSize)
  2347. {
  2348. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts, fInfo.cvIns+fInfo.cvOuts);
  2349. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  2350. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  2351. fShmRtClientControl.commitWrite();
  2352. waitForClient("resize-pool", 5000);
  2353. }
  2354. void waitForClient(const char* const action, const uint msecs)
  2355. {
  2356. CARLA_SAFE_ASSERT_RETURN(! fTimedOut,);
  2357. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  2358. if (fShmRtClientControl.waitForClient(msecs))
  2359. return;
  2360. fTimedOut = true;
  2361. carla_stderr2("waitForClient(%s) timed out", action);
  2362. }
  2363. bool restartBridgeThread()
  2364. {
  2365. fInitiated = false;
  2366. fInitError = false;
  2367. fTimedError = false;
  2368. // reset memory
  2369. fShmRtClientControl.data->procFlags = 0;
  2370. carla_zeroStruct(fShmRtClientControl.data->timeInfo);
  2371. carla_zeroBytes(fShmRtClientControl.data->midiOut, kBridgeRtClientDataMidiOutSize);
  2372. fShmRtClientControl.clearData();
  2373. fShmNonRtClientControl.clearData();
  2374. fShmNonRtServerControl.clearData();
  2375. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientVersion);
  2376. fShmNonRtClientControl.writeUInt(CARLA_PLUGIN_BRIDGE_API_VERSION_CURRENT);
  2377. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeRtClientData)));
  2378. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtClientData)));
  2379. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtServerData)));
  2380. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientInitialSetup);
  2381. fShmNonRtClientControl.writeUInt(pData->engine->getBufferSize());
  2382. fShmNonRtClientControl.writeDouble(pData->engine->getSampleRate());
  2383. fShmNonRtClientControl.commitWrite();
  2384. if (fShmAudioPool.dataSize != 0)
  2385. {
  2386. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  2387. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  2388. fShmRtClientControl.commitWrite();
  2389. }
  2390. else
  2391. {
  2392. // testing dummy message
  2393. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientNull);
  2394. fShmRtClientControl.commitWrite();
  2395. }
  2396. fBridgeThread.startThread();
  2397. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  2398. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2399. const bool needsCancelableAction = ! pData->engine->isLoadingProject();
  2400. if (needsCancelableAction)
  2401. {
  2402. pData->engine->setActionCanceled(false);
  2403. pData->engine->callback(true, true,
  2404. ENGINE_CALLBACK_CANCELABLE_ACTION,
  2405. pData->id,
  2406. 1,
  2407. 0, 0, 0.0f,
  2408. "Loading plugin bridge");
  2409. }
  2410. #endif
  2411. for (;fBridgeThread.isThreadRunning();)
  2412. {
  2413. pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2414. if (needsEngineIdle)
  2415. pData->engine->idle();
  2416. idle();
  2417. if (fInitiated)
  2418. break;
  2419. if (pData->engine->isAboutToClose() || pData->engine->wasActionCanceled())
  2420. break;
  2421. carla_msleep(5);
  2422. }
  2423. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2424. if (needsCancelableAction)
  2425. {
  2426. pData->engine->callback(true, true,
  2427. ENGINE_CALLBACK_CANCELABLE_ACTION,
  2428. pData->id,
  2429. 0,
  2430. 0, 0, 0.0f,
  2431. "Loading JACK application");
  2432. }
  2433. #endif
  2434. if (fInitError || ! fInitiated)
  2435. {
  2436. fBridgeThread.stopThread(6000);
  2437. if (! fInitError)
  2438. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n"
  2439. "(or the plugin crashed on initialization?)");
  2440. return false;
  2441. }
  2442. if (const size_t dataSize = fInfo.chunk.size())
  2443. {
  2444. #ifdef CARLA_PROPER_CPP11_SUPPORT
  2445. void* data = fInfo.chunk.data();
  2446. #else
  2447. void* data = &fInfo.chunk.front();
  2448. #endif
  2449. CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
  2450. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0, true);
  2451. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  2452. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  2453. filePath += fShmAudioPool.getFilenameSuffix();
  2454. if (File(filePath).replaceWithText(dataBase64.buffer()))
  2455. {
  2456. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  2457. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2458. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
  2459. fShmNonRtClientControl.writeUInt(ulength);
  2460. fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
  2461. fShmNonRtClientControl.commitWrite();
  2462. }
  2463. }
  2464. return true;
  2465. }
  2466. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridge)
  2467. };
  2468. CARLA_BACKEND_END_NAMESPACE
  2469. // ---------------------------------------------------------------------------------------------------------------------
  2470. CARLA_BACKEND_START_NAMESPACE
  2471. CarlaPluginPtr CarlaPlugin::newBridge(const Initializer& init,
  2472. BinaryType btype, PluginType ptype, const char* bridgeBinary)
  2473. {
  2474. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")",
  2475. init.engine, init.filename, init.name, init.label,
  2476. BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  2477. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2478. {
  2479. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  2480. return nullptr;
  2481. }
  2482. #ifndef CARLA_OS_WIN
  2483. // FIXME: somewhere, somehow, we end up with double slashes, wine doesn't like that.
  2484. if (std::strncmp(bridgeBinary, "//", 2) == 0)
  2485. ++bridgeBinary;
  2486. #endif
  2487. std::shared_ptr<CarlaPluginBridge> plugin(new CarlaPluginBridge(init.engine, init.id, btype, ptype));
  2488. if (! plugin->init(plugin, init.filename, init.name, init.label, init.uniqueId, init.options, bridgeBinary))
  2489. return nullptr;
  2490. return plugin;
  2491. }
  2492. CARLA_BACKEND_END_NAMESPACE
  2493. // ---------------------------------------------------------------------------------------------------------------------
  2494. #ifndef BUILD_BRIDGE
  2495. # include "CarlaBridgeUtils.cpp"
  2496. #endif
  2497. // ---------------------------------------------------------------------------------------------------------------------