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.

3183 lines
114KB

  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. void setName(const char* const newName) override
  558. {
  559. CarlaPlugin::setName(newName);
  560. if (pData->uiTitle.isEmpty() && fBridgeVersion >= 8)
  561. _setUiTitleFromName();
  562. }
  563. // -------------------------------------------------------------------
  564. // Set data (plugin-specific stuff)
  565. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  566. {
  567. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  568. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
  569. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  570. fParams[parameterId].value = fixedValue;
  571. {
  572. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  573. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
  574. fShmNonRtClientControl.writeUInt(parameterId);
  575. fShmNonRtClientControl.writeFloat(value);
  576. fShmNonRtClientControl.commitWrite();
  577. fShmNonRtClientControl.waitIfDataIsReachingLimit();
  578. }
  579. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  580. }
  581. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  582. {
  583. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  584. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  585. fParams[parameterId].value = fixedValue;
  586. {
  587. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  588. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterValue);
  589. fShmNonRtClientControl.writeUInt(parameterId);
  590. fShmNonRtClientControl.writeFloat(value);
  591. fShmNonRtClientControl.commitWrite();
  592. fShmNonRtClientControl.waitIfDataIsReachingLimit();
  593. }
  594. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  595. }
  596. void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  597. {
  598. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  599. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  600. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  601. {
  602. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  603. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMidiChannel);
  604. fShmNonRtClientControl.writeUInt(parameterId);
  605. fShmNonRtClientControl.writeByte(channel);
  606. fShmNonRtClientControl.commitWrite();
  607. }
  608. CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
  609. }
  610. void setParameterMappedControlIndex(const uint32_t parameterId, const int16_t index,
  611. const bool sendOsc, const bool sendCallback,
  612. const bool reconfigureNow) noexcept override
  613. {
  614. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  615. CARLA_SAFE_ASSERT_RETURN(index >= CONTROL_INDEX_NONE && index <= CONTROL_INDEX_MAX_ALLOWED,);
  616. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  617. {
  618. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  619. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedControlIndex);
  620. fShmNonRtClientControl.writeUInt(parameterId);
  621. fShmNonRtClientControl.writeShort(index);
  622. fShmNonRtClientControl.commitWrite();
  623. }
  624. CarlaPlugin::setParameterMappedControlIndex(parameterId, index, sendOsc, sendCallback, reconfigureNow);
  625. }
  626. void setParameterMappedRange(const uint32_t parameterId, const float minimum, const float maximum, const bool sendOsc, const bool sendCallback) noexcept override
  627. {
  628. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  629. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,);
  630. // kPluginBridgeNonRtClientSetParameterMappedRange was added in API 7
  631. if (fBridgeVersion >= 7)
  632. {
  633. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  634. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetParameterMappedRange);
  635. fShmNonRtClientControl.writeUInt(parameterId);
  636. fShmNonRtClientControl.writeFloat(minimum);
  637. fShmNonRtClientControl.writeFloat(maximum);
  638. fShmNonRtClientControl.commitWrite();
  639. }
  640. CarlaPlugin::setParameterMappedRange(parameterId, minimum, maximum, sendOsc, sendCallback);
  641. }
  642. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  643. {
  644. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  645. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  646. {
  647. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  648. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
  649. fShmNonRtClientControl.writeInt(index);
  650. fShmNonRtClientControl.commitWrite();
  651. }
  652. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  653. }
  654. void setProgramRT(const uint32_t index, const bool sendCallbackLater) noexcept override
  655. {
  656. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  657. {
  658. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  659. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetProgram);
  660. fShmNonRtClientControl.writeInt(static_cast<int32_t>(index));
  661. fShmNonRtClientControl.commitWrite();
  662. }
  663. CarlaPlugin::setProgramRT(index, sendCallbackLater);
  664. }
  665. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  666. {
  667. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  668. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  669. {
  670. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  671. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
  672. fShmNonRtClientControl.writeInt(index);
  673. fShmNonRtClientControl.commitWrite();
  674. }
  675. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  676. }
  677. void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
  678. {
  679. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  680. {
  681. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  682. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetMidiProgram);
  683. fShmNonRtClientControl.writeInt(static_cast<int32_t>(uindex));
  684. fShmNonRtClientControl.commitWrite();
  685. }
  686. CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
  687. }
  688. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  689. {
  690. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  691. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  692. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  693. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  694. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  695. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "__CarlaPingOnOff__") == 0)
  696. {
  697. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  698. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPingOnOff);
  699. fShmNonRtClientControl.writeBool(std::strcmp(value, "true") == 0);
  700. fShmNonRtClientControl.commitWrite();
  701. return;
  702. }
  703. const uint32_t typeLen(static_cast<uint32_t>(std::strlen(type)));
  704. const uint32_t keyLen(static_cast<uint32_t>(std::strlen(key)));
  705. const uint32_t valueLen(static_cast<uint32_t>(std::strlen(value)));
  706. {
  707. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  708. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetCustomData);
  709. fShmNonRtClientControl.writeUInt(typeLen);
  710. fShmNonRtClientControl.writeCustomData(type, typeLen);
  711. fShmNonRtClientControl.writeUInt(keyLen);
  712. fShmNonRtClientControl.writeCustomData(key, keyLen);
  713. fShmNonRtClientControl.writeUInt(valueLen);
  714. if (valueLen > 0)
  715. fShmNonRtClientControl.writeCustomData(value, valueLen);
  716. fShmNonRtClientControl.commitWrite();
  717. }
  718. CarlaPlugin::setCustomData(type, key, value, sendGui);
  719. }
  720. void setChunkData(const void* const data, const std::size_t dataSize) override
  721. {
  722. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  723. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  724. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  725. CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
  726. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0,);
  727. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  728. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  729. filePath += fShmAudioPool.getFilenameSuffix();
  730. if (File(filePath).replaceWithText(dataBase64.buffer()))
  731. {
  732. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  733. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  734. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
  735. fShmNonRtClientControl.writeUInt(ulength);
  736. fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
  737. fShmNonRtClientControl.commitWrite();
  738. }
  739. // save data internally as well
  740. fInfo.chunk.resize(dataSize);
  741. #ifdef CARLA_PROPER_CPP11_SUPPORT
  742. std::memcpy(fInfo.chunk.data(), data, dataSize);
  743. #else
  744. std::memcpy(&fInfo.chunk.front(), data, dataSize);
  745. #endif
  746. }
  747. // -------------------------------------------------------------------
  748. // Set ui stuff
  749. void setCustomUITitle(const char* const title) noexcept override
  750. {
  751. carla_debug("CarlaPluginBridge::setCustomUITitle(%s)", title);
  752. if (fBridgeVersion >= 8)
  753. {
  754. const uint32_t size = static_cast<uint32_t>(std::strlen(title));
  755. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  756. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetWindowTitle);
  757. fShmNonRtClientControl.writeUInt(size);
  758. fShmNonRtClientControl.writeCustomData(title, size);
  759. fShmNonRtClientControl.commitWrite();
  760. }
  761. CarlaPlugin::setCustomUITitle(title);
  762. }
  763. void showCustomUI(const bool yesNo) override
  764. {
  765. if (yesNo && pData->uiTitle.isEmpty() && fBridgeVersion >= 8)
  766. _setUiTitleFromName();
  767. {
  768. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  769. fShmNonRtClientControl.writeOpcode(yesNo ? kPluginBridgeNonRtClientShowUI : kPluginBridgeNonRtClientHideUI);
  770. fShmNonRtClientControl.commitWrite();
  771. }
  772. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  773. if (yesNo)
  774. {
  775. pData->tryTransient();
  776. }
  777. else
  778. {
  779. pData->transientTryCounter = 0;
  780. }
  781. #endif
  782. }
  783. void idle() override
  784. {
  785. if (fBridgeThread.isThreadRunning())
  786. {
  787. if (fInitiated && fTimedOut && pData->active)
  788. setActive(false, true, true);
  789. {
  790. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  791. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientPing);
  792. fShmNonRtClientControl.commitWrite();
  793. }
  794. try {
  795. handleNonRtData();
  796. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  797. }
  798. else if (fInitiated)
  799. {
  800. fTimedOut = true;
  801. fTimedError = true;
  802. fInitiated = false;
  803. handleProcessStopped();
  804. }
  805. CarlaPlugin::idle();
  806. }
  807. // -------------------------------------------------------------------
  808. // Plugin state
  809. void reload() override
  810. {
  811. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  812. carla_debug("CarlaPluginBridge::reload() - start");
  813. const EngineProcessMode processMode(pData->engine->getProccessMode());
  814. // Safely disable plugin for reload
  815. const ScopedDisabler sd(this);
  816. // cleanup of previous data
  817. pData->audioIn.clear();
  818. pData->audioOut.clear();
  819. pData->cvIn.clear();
  820. pData->cvOut.clear();
  821. pData->event.clear();
  822. bool needsCtrlIn, needsCtrlOut;
  823. needsCtrlIn = needsCtrlOut = false;
  824. if (fInfo.aIns > 0)
  825. {
  826. pData->audioIn.createNew(fInfo.aIns);
  827. }
  828. if (fInfo.aOuts > 0)
  829. {
  830. pData->audioOut.createNew(fInfo.aOuts);
  831. needsCtrlIn = true;
  832. }
  833. if (fInfo.cvIns > 0)
  834. {
  835. pData->cvIn.createNew(fInfo.cvIns);
  836. }
  837. if (fInfo.cvOuts > 0)
  838. {
  839. pData->cvOut.createNew(fInfo.cvOuts);
  840. }
  841. if (fInfo.mIns > 0)
  842. needsCtrlIn = true;
  843. if (fInfo.mOuts > 0)
  844. needsCtrlOut = true;
  845. const uint portNameSize(pData->engine->getMaxPortNameSize());
  846. CarlaString portName;
  847. // Audio Ins
  848. for (uint32_t j=0; j < fInfo.aIns; ++j)
  849. {
  850. portName.clear();
  851. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  852. {
  853. portName = pData->name;
  854. portName += ":";
  855. }
  856. if (fInfo.aInNames != nullptr && fInfo.aInNames[j] != nullptr)
  857. {
  858. portName += fInfo.aInNames[j];
  859. }
  860. else if (fInfo.aIns > 1)
  861. {
  862. portName += "input_";
  863. portName += CarlaString(j+1);
  864. }
  865. else
  866. portName += "input";
  867. portName.truncate(portNameSize);
  868. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  869. pData->audioIn.ports[j].rindex = j;
  870. }
  871. // Audio Outs
  872. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  873. {
  874. portName.clear();
  875. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  876. {
  877. portName = pData->name;
  878. portName += ":";
  879. }
  880. if (fInfo.aOutNames != nullptr && fInfo.aOutNames[j] != nullptr)
  881. {
  882. portName += fInfo.aOutNames[j];
  883. }
  884. else if (fInfo.aOuts > 1)
  885. {
  886. portName += "output_";
  887. portName += CarlaString(j+1);
  888. }
  889. else
  890. portName += "output";
  891. portName.truncate(portNameSize);
  892. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  893. pData->audioOut.ports[j].rindex = j;
  894. }
  895. // TODO - MIDI
  896. // CV Ins
  897. for (uint32_t j=0; j < fInfo.cvIns; ++j)
  898. {
  899. portName.clear();
  900. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  901. {
  902. portName = pData->name;
  903. portName += ":";
  904. }
  905. if (fInfo.cvInNames != nullptr && fInfo.cvInNames[j] != nullptr)
  906. {
  907. portName += fInfo.cvInNames[j];
  908. }
  909. else if (fInfo.cvIns > 1)
  910. {
  911. portName += "cv_input_";
  912. portName += CarlaString(j+1);
  913. }
  914. else
  915. portName += "cv_input";
  916. portName.truncate(portNameSize);
  917. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  918. pData->cvIn.ports[j].rindex = j;
  919. }
  920. // CV Outs
  921. for (uint32_t j=0; j < fInfo.cvOuts; ++j)
  922. {
  923. portName.clear();
  924. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  925. {
  926. portName = pData->name;
  927. portName += ":";
  928. }
  929. if (fInfo.cvOutNames != nullptr && fInfo.cvOutNames[j] != nullptr)
  930. {
  931. portName += fInfo.cvOutNames[j];
  932. }
  933. else if (fInfo.cvOuts > 1)
  934. {
  935. portName += "cv_output_";
  936. portName += CarlaString(j+1);
  937. }
  938. else
  939. portName += "cv_output";
  940. portName.truncate(portNameSize);
  941. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  942. pData->cvOut.ports[j].rindex = j;
  943. }
  944. if (needsCtrlIn)
  945. {
  946. portName.clear();
  947. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  948. {
  949. portName = pData->name;
  950. portName += ":";
  951. }
  952. portName += "event-in";
  953. portName.truncate(portNameSize);
  954. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  955. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  956. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  957. #endif
  958. }
  959. if (needsCtrlOut)
  960. {
  961. portName.clear();
  962. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  963. {
  964. portName = pData->name;
  965. portName += ":";
  966. }
  967. portName += "event-out";
  968. portName.truncate(portNameSize);
  969. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  970. }
  971. // extra plugin hints
  972. pData->extraHints = 0x0;
  973. if (fInfo.mIns > 0)
  974. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  975. if (fInfo.mOuts > 0)
  976. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  977. bufferSizeChanged(pData->engine->getBufferSize());
  978. reloadPrograms(true);
  979. carla_debug("CarlaPluginBridge::reload() - end");
  980. }
  981. // -------------------------------------------------------------------
  982. // Plugin processing
  983. void activate() noexcept override
  984. {
  985. if (! fBridgeThread.isThreadRunning())
  986. {
  987. CARLA_SAFE_ASSERT_RETURN(restartBridgeThread(),);
  988. }
  989. {
  990. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  991. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientActivate);
  992. fShmNonRtClientControl.commitWrite();
  993. }
  994. fTimedOut = false;
  995. try {
  996. waitForClient("activate", 2000);
  997. } CARLA_SAFE_EXCEPTION("activate - waitForClient");
  998. }
  999. void deactivate() noexcept override
  1000. {
  1001. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  1002. {
  1003. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1004. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientDeactivate);
  1005. fShmNonRtClientControl.commitWrite();
  1006. }
  1007. fTimedOut = false;
  1008. try {
  1009. waitForClient("deactivate", 2000);
  1010. } CARLA_SAFE_EXCEPTION("deactivate - waitForClient");
  1011. }
  1012. void process(const float* const* const audioIn,
  1013. float** const audioOut,
  1014. const float* const* const cvIn,
  1015. float** const cvOut,
  1016. const uint32_t frames) override
  1017. {
  1018. // --------------------------------------------------------------------------------------------------------
  1019. // Check if active
  1020. if (fTimedOut || fTimedError || ! pData->active)
  1021. {
  1022. // disable any output sound
  1023. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1024. carla_zeroFloats(audioOut[i], frames);
  1025. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1026. carla_zeroFloats(cvOut[i], frames);
  1027. return;
  1028. }
  1029. // --------------------------------------------------------------------------------------------------------
  1030. // Check if needs reset
  1031. if (pData->needsReset)
  1032. {
  1033. // TODO
  1034. pData->needsReset = false;
  1035. }
  1036. // --------------------------------------------------------------------------------------------------------
  1037. // Event Input
  1038. if (pData->event.portIn != nullptr)
  1039. {
  1040. // ----------------------------------------------------------------------------------------------------
  1041. // MIDI Input (External)
  1042. if (pData->extNotes.mutex.tryLock())
  1043. {
  1044. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  1045. {
  1046. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  1047. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1048. uint8_t data1, data2, data3;
  1049. data1 = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1050. data2 = note.note;
  1051. data3 = note.velo;
  1052. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1053. fShmRtClientControl.writeUInt(0); // time
  1054. fShmRtClientControl.writeByte(0); // port
  1055. fShmRtClientControl.writeByte(3); // size
  1056. fShmRtClientControl.writeByte(data1);
  1057. fShmRtClientControl.writeByte(data2);
  1058. fShmRtClientControl.writeByte(data3);
  1059. fShmRtClientControl.commitWrite();
  1060. }
  1061. pData->extNotes.data.clear();
  1062. pData->extNotes.mutex.unlock();
  1063. } // End of MIDI Input (External)
  1064. // ----------------------------------------------------------------------------------------------------
  1065. // Event Input (System)
  1066. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1067. bool allNotesOffSent = false;
  1068. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1069. {
  1070. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0;
  1071. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  1072. }
  1073. #endif
  1074. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1075. {
  1076. EngineEvent& event(pData->event.portIn->getEvent(i));
  1077. // Control change
  1078. switch (event.type)
  1079. {
  1080. case kEngineEventTypeNull:
  1081. break;
  1082. case kEngineEventTypeControl: {
  1083. EngineControlEvent& ctrlEvent = event.ctrl;
  1084. switch (ctrlEvent.type)
  1085. {
  1086. case kEngineControlEventTypeNull:
  1087. break;
  1088. case kEngineControlEventTypeParameter: {
  1089. float value;
  1090. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1091. // non-midi
  1092. if (event.channel == kEngineEventNonMidiChannel)
  1093. {
  1094. const uint32_t k = ctrlEvent.param;
  1095. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1096. ctrlEvent.handled = true;
  1097. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1098. setParameterValueRT(k, value, true);
  1099. continue;
  1100. }
  1101. // Control backend stuff
  1102. if (event.channel == pData->ctrlChannel)
  1103. {
  1104. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1105. {
  1106. ctrlEvent.handled = true;
  1107. value = ctrlEvent.normalizedValue;
  1108. setDryWetRT(value, true);
  1109. }
  1110. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1111. {
  1112. ctrlEvent.handled = true;
  1113. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  1114. setVolumeRT(value, true);
  1115. }
  1116. else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1117. {
  1118. float left, right;
  1119. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  1120. if (value < 0.0f)
  1121. {
  1122. left = -1.0f;
  1123. right = (value*2.0f)+1.0f;
  1124. }
  1125. else if (value > 0.0f)
  1126. {
  1127. left = (value*2.0f)-1.0f;
  1128. right = 1.0f;
  1129. }
  1130. else
  1131. {
  1132. left = -1.0f;
  1133. right = 1.0f;
  1134. }
  1135. ctrlEvent.handled = true;
  1136. setBalanceLeftRT(left, true);
  1137. setBalanceRightRT(right, true);
  1138. }
  1139. }
  1140. #endif
  1141. // Control plugin parameters
  1142. for (uint32_t k=0; k < pData->param.count; ++k)
  1143. {
  1144. if (pData->param.data[k].midiChannel != event.channel)
  1145. continue;
  1146. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1147. continue;
  1148. if (pData->param.data[k].type != PARAMETER_INPUT)
  1149. continue;
  1150. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1151. continue;
  1152. ctrlEvent.handled = true;
  1153. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1154. setParameterValueRT(k, value, true);
  1155. }
  1156. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1157. if (! ctrlEvent.handled)
  1158. checkForMidiLearn(event);
  1159. #endif
  1160. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1161. {
  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(uint8_t(ctrlEvent.param));
  1168. fShmRtClientControl.writeByte(uint8_t(ctrlEvent.normalizedValue*127.0f));
  1169. }
  1170. break;
  1171. }
  1172. case kEngineControlEventTypeMidiBank:
  1173. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1174. {
  1175. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiBank);
  1176. fShmRtClientControl.writeUInt(event.time);
  1177. fShmRtClientControl.writeByte(event.channel);
  1178. fShmRtClientControl.writeUShort(event.ctrl.param);
  1179. fShmRtClientControl.commitWrite();
  1180. }
  1181. else if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  1182. {
  1183. // 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
  1184. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1185. fShmRtClientControl.writeUInt(event.time);
  1186. fShmRtClientControl.writeByte(0); // port
  1187. fShmRtClientControl.writeByte(3); // size
  1188. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1189. fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT);
  1190. fShmRtClientControl.writeByte(0);
  1191. fShmRtClientControl.commitWrite();
  1192. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1193. fShmRtClientControl.writeUInt(event.time);
  1194. fShmRtClientControl.writeByte(0); // port
  1195. fShmRtClientControl.writeByte(3); // size
  1196. fShmRtClientControl.writeByte(uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT)));
  1197. fShmRtClientControl.writeByte(MIDI_CONTROL_BANK_SELECT__LSB);
  1198. fShmRtClientControl.writeByte(uint8_t(event.ctrl.param));
  1199. fShmRtClientControl.commitWrite();
  1200. }
  1201. break;
  1202. case kEngineControlEventTypeMidiProgram:
  1203. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES || pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1204. {
  1205. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventMidiProgram);
  1206. fShmRtClientControl.writeUInt(event.time);
  1207. fShmRtClientControl.writeByte(event.channel);
  1208. fShmRtClientControl.writeUShort(event.ctrl.param);
  1209. fShmRtClientControl.commitWrite();
  1210. }
  1211. break;
  1212. case kEngineControlEventTypeAllSoundOff:
  1213. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1214. {
  1215. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllSoundOff);
  1216. fShmRtClientControl.writeUInt(event.time);
  1217. fShmRtClientControl.writeByte(event.channel);
  1218. fShmRtClientControl.commitWrite();
  1219. }
  1220. break;
  1221. case kEngineControlEventTypeAllNotesOff:
  1222. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1223. {
  1224. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1225. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1226. {
  1227. allNotesOffSent = true;
  1228. postponeRtAllNotesOff();
  1229. }
  1230. #endif
  1231. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientControlEventAllNotesOff);
  1232. fShmRtClientControl.writeUInt(event.time);
  1233. fShmRtClientControl.writeByte(event.channel);
  1234. fShmRtClientControl.commitWrite();
  1235. }
  1236. break;
  1237. } // switch (ctrlEvent.type)
  1238. break;
  1239. } // case kEngineEventTypeControl
  1240. case kEngineEventTypeMidi: {
  1241. const EngineMidiEvent& midiEvent(event.midi);
  1242. if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
  1243. continue;
  1244. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  1245. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  1246. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1247. continue;
  1248. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1249. continue;
  1250. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1251. continue;
  1252. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1253. continue;
  1254. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1255. continue;
  1256. // Fix bad note-off
  1257. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  1258. status = MIDI_STATUS_NOTE_OFF;
  1259. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientMidiEvent);
  1260. fShmRtClientControl.writeUInt(event.time);
  1261. fShmRtClientControl.writeByte(midiEvent.port);
  1262. fShmRtClientControl.writeByte(midiEvent.size);
  1263. fShmRtClientControl.writeByte(uint8_t(midiData[0] | (event.channel & MIDI_CHANNEL_BIT)));
  1264. for (uint8_t j=1; j < midiEvent.size; ++j)
  1265. fShmRtClientControl.writeByte(midiData[j]);
  1266. fShmRtClientControl.commitWrite();
  1267. if (status == MIDI_STATUS_NOTE_ON)
  1268. {
  1269. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  1270. true,
  1271. event.channel,
  1272. midiData[1],
  1273. midiData[2],
  1274. 0.0f);
  1275. }
  1276. else if (status == MIDI_STATUS_NOTE_OFF)
  1277. {
  1278. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  1279. true,
  1280. event.channel,
  1281. midiData[1],
  1282. 0, 0.0f);
  1283. }
  1284. } break;
  1285. }
  1286. }
  1287. pData->postRtEvents.trySplice();
  1288. } // End of Event Input
  1289. if (! processSingle(audioIn, audioOut, cvIn, cvOut, frames))
  1290. return;
  1291. // --------------------------------------------------------------------------------------------------------
  1292. // Control and MIDI Output
  1293. if (pData->event.portOut != nullptr)
  1294. {
  1295. float value;
  1296. for (uint32_t k=0; k < pData->param.count; ++k)
  1297. {
  1298. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1299. continue;
  1300. if (pData->param.data[k].mappedControlIndex > 0)
  1301. {
  1302. value = pData->param.ranges[k].getNormalizedValue(fParams[k].value);
  1303. pData->event.portOut->writeControlEvent(0,
  1304. pData->param.data[k].midiChannel,
  1305. kEngineControlEventTypeParameter,
  1306. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1307. -1,
  1308. value);
  1309. }
  1310. }
  1311. uint32_t time;
  1312. uint8_t port, size;
  1313. const uint8_t* midiData(fShmRtClientControl.data->midiOut);
  1314. for (std::size_t read=0; read<kBridgeRtClientDataMidiOutSize-kBridgeBaseMidiOutHeaderSize;)
  1315. {
  1316. // get time
  1317. time = *(const uint32_t*)midiData;
  1318. midiData += 4;
  1319. // get port and size
  1320. port = *midiData++;
  1321. size = *midiData++;
  1322. if (size == 0)
  1323. break;
  1324. // store midi data advancing as needed
  1325. uint8_t data[size];
  1326. for (uint8_t j=0; j<size; ++j)
  1327. data[j] = *midiData++;
  1328. pData->event.portOut->writeMidiEvent(time, size, data);
  1329. read += kBridgeBaseMidiOutHeaderSize + size;
  1330. }
  1331. // TODO
  1332. (void)port;
  1333. } // End of Control and MIDI Output
  1334. }
  1335. bool processSingle(const float* const* const audioIn, float** const audioOut,
  1336. const float* const* const cvIn, float** const cvOut, const uint32_t frames)
  1337. {
  1338. CARLA_SAFE_ASSERT_RETURN(! fTimedError, false);
  1339. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1340. CARLA_SAFE_ASSERT_RETURN(frames <= fBufferSize, false);
  1341. if (pData->audioIn.count > 0)
  1342. {
  1343. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1344. }
  1345. if (pData->audioOut.count > 0)
  1346. {
  1347. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1348. }
  1349. if (pData->cvIn.count > 0)
  1350. {
  1351. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1352. }
  1353. if (pData->cvOut.count > 0)
  1354. {
  1355. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1356. }
  1357. // --------------------------------------------------------------------------------------------------------
  1358. // Try lock, silence otherwise
  1359. #ifndef STOAT_TEST_BUILD
  1360. if (pData->engine->isOffline())
  1361. {
  1362. pData->singleMutex.lock();
  1363. }
  1364. else
  1365. #endif
  1366. if (! pData->singleMutex.tryLock())
  1367. {
  1368. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1369. carla_zeroFloats(audioOut[i], frames);
  1370. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1371. carla_zeroFloats(cvOut[i], frames);
  1372. return false;
  1373. }
  1374. // --------------------------------------------------------------------------------------------------------
  1375. // Reset audio buffers
  1376. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1377. carla_copyFloats(fShmAudioPool.data + (i * fBufferSize), audioIn[i], frames);
  1378. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1379. carla_copyFloats(fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + i) * fBufferSize), cvIn[i], frames);
  1380. // --------------------------------------------------------------------------------------------------------
  1381. // TimeInfo
  1382. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1383. BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  1384. bridgeTimeInfo.playing = timeInfo.playing;
  1385. bridgeTimeInfo.frame = timeInfo.frame;
  1386. bridgeTimeInfo.usecs = timeInfo.usecs;
  1387. bridgeTimeInfo.validFlags = timeInfo.bbt.valid ? kPluginBridgeTimeInfoValidBBT : 0x0;
  1388. if (timeInfo.bbt.valid)
  1389. {
  1390. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  1391. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  1392. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  1393. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1394. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  1395. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1396. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1397. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  1398. }
  1399. // --------------------------------------------------------------------------------------------------------
  1400. // Run plugin
  1401. {
  1402. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientProcess);
  1403. fShmRtClientControl.writeUInt(frames);
  1404. fShmRtClientControl.commitWrite();
  1405. }
  1406. waitForClient("process", fProcWaitTime);
  1407. if (fTimedOut)
  1408. {
  1409. pData->singleMutex.unlock();
  1410. return false;
  1411. }
  1412. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1413. carla_copyFloats(audioOut[i], fShmAudioPool.data + ((pData->audioIn.count + i) * fBufferSize), frames);
  1414. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1415. carla_copyFloats(cvOut[i], fShmAudioPool.data + ((pData->audioIn.count + pData->audioOut.count + pData->cvIn.count + i) * fBufferSize), frames);
  1416. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1417. // --------------------------------------------------------------------------------------------------------
  1418. // Post-processing (dry/wet, volume and balance)
  1419. {
  1420. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1421. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1422. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1423. const bool isMono = (pData->audioIn.count == 1);
  1424. bool isPair;
  1425. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1426. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1427. {
  1428. // Dry/Wet
  1429. if (doDryWet)
  1430. {
  1431. const uint32_t c = isMono ? 0 : i;
  1432. for (uint32_t k=0; k < frames; ++k)
  1433. {
  1434. # ifndef BUILD_BRIDGE
  1435. if (k < pData->latency.frames && pData->latency.buffers != nullptr)
  1436. bufValue = pData->latency.buffers[c][k];
  1437. else if (pData->latency.frames < frames)
  1438. bufValue = audioIn[c][k-pData->latency.frames];
  1439. else
  1440. # endif
  1441. bufValue = audioIn[c][k];
  1442. audioOut[i][k] = (audioOut[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1443. }
  1444. }
  1445. // Balance
  1446. if (doBalance)
  1447. {
  1448. isPair = (i % 2 == 0);
  1449. if (isPair)
  1450. {
  1451. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1452. carla_copyFloats(oldBufLeft, audioOut[i], frames);
  1453. }
  1454. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1455. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1456. for (uint32_t k=0; k < frames; ++k)
  1457. {
  1458. if (isPair)
  1459. {
  1460. // left
  1461. audioOut[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1462. audioOut[i][k] += audioOut[i+1][k] * (1.0f - balRangeR);
  1463. }
  1464. else
  1465. {
  1466. // right
  1467. audioOut[i][k] = audioOut[i][k] * balRangeR;
  1468. audioOut[i][k] += oldBufLeft[k] * balRangeL;
  1469. }
  1470. }
  1471. }
  1472. // Volume (and buffer copy)
  1473. if (doVolume)
  1474. {
  1475. for (uint32_t k=0; k < frames; ++k)
  1476. audioOut[i][k] *= pData->postProc.volume;
  1477. }
  1478. }
  1479. } // End of Post-processing
  1480. # ifndef BUILD_BRIDGE
  1481. // --------------------------------------------------------------------------------------------------------
  1482. // Save latency values for next callback
  1483. if (pData->latency.frames != 0 && pData->latency.buffers != nullptr)
  1484. {
  1485. const uint32_t latframes = pData->latency.frames;
  1486. if (latframes <= frames)
  1487. {
  1488. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1489. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  1490. }
  1491. else
  1492. {
  1493. const uint32_t diff = latframes - frames;
  1494. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1495. {
  1496. // push back buffer by 'frames'
  1497. for (k=0; k < diff; ++k)
  1498. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1499. // put current input at the end
  1500. for (uint32_t j=0; k < latframes; ++j, ++k)
  1501. pData->latency.buffers[i][k] = audioIn[i][j];
  1502. }
  1503. }
  1504. }
  1505. # endif
  1506. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1507. // --------------------------------------------------------------------------------------------------------
  1508. pData->singleMutex.unlock();
  1509. return true;
  1510. }
  1511. void bufferSizeChanged(const uint32_t newBufferSize) override
  1512. {
  1513. fBufferSize = newBufferSize;
  1514. resizeAudioPool(newBufferSize);
  1515. {
  1516. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetBufferSize);
  1517. fShmRtClientControl.writeUInt(newBufferSize);
  1518. fShmRtClientControl.commitWrite();
  1519. }
  1520. //fProcWaitTime = newBufferSize*1000/pData->engine->getSampleRate();
  1521. fProcWaitTime = 1000;
  1522. waitForClient("buffersize", 1000);
  1523. }
  1524. void sampleRateChanged(const double newSampleRate) override
  1525. {
  1526. {
  1527. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetSampleRate);
  1528. fShmRtClientControl.writeDouble(newSampleRate);
  1529. fShmRtClientControl.commitWrite();
  1530. }
  1531. //fProcWaitTime = pData->engine->getBufferSize()*1000/newSampleRate;
  1532. fProcWaitTime = 1000;
  1533. waitForClient("samplerate", 1000);
  1534. }
  1535. void offlineModeChanged(const bool isOffline) override
  1536. {
  1537. {
  1538. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetOnline);
  1539. fShmRtClientControl.writeBool(isOffline);
  1540. fShmRtClientControl.commitWrite();
  1541. }
  1542. waitForClient("offline", 1000);
  1543. }
  1544. // -------------------------------------------------------------------
  1545. // Plugin buffers
  1546. void clearBuffers() noexcept override
  1547. {
  1548. if (fParams != nullptr)
  1549. {
  1550. delete[] fParams;
  1551. fParams = nullptr;
  1552. }
  1553. CarlaPlugin::clearBuffers();
  1554. }
  1555. // -------------------------------------------------------------------
  1556. // Post-poned UI Stuff
  1557. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1558. {
  1559. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1560. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1561. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiParameterChange);
  1562. fShmNonRtClientControl.writeUInt(index);
  1563. fShmNonRtClientControl.writeFloat(value);
  1564. fShmNonRtClientControl.commitWrite();
  1565. }
  1566. void uiProgramChange(const uint32_t index) noexcept override
  1567. {
  1568. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  1569. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1570. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiProgramChange);
  1571. fShmNonRtClientControl.writeUInt(index);
  1572. fShmNonRtClientControl.commitWrite();
  1573. }
  1574. void uiMidiProgramChange(const uint32_t index) noexcept override
  1575. {
  1576. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1577. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1578. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiMidiProgramChange);
  1579. fShmNonRtClientControl.writeUInt(index);
  1580. fShmNonRtClientControl.commitWrite();
  1581. }
  1582. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1583. {
  1584. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1585. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1586. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1587. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1588. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOn);
  1589. fShmNonRtClientControl.writeByte(channel);
  1590. fShmNonRtClientControl.writeByte(note);
  1591. fShmNonRtClientControl.writeByte(velo);
  1592. fShmNonRtClientControl.commitWrite();
  1593. }
  1594. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1595. {
  1596. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1597. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1598. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1599. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientUiNoteOff);
  1600. fShmNonRtClientControl.writeByte(channel);
  1601. fShmNonRtClientControl.writeByte(note);
  1602. fShmNonRtClientControl.commitWrite();
  1603. }
  1604. // -------------------------------------------------------------------
  1605. // Internal helper functions
  1606. void restoreLV2State(bool) noexcept override
  1607. {
  1608. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  1609. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientRestoreLV2State);
  1610. fShmNonRtClientControl.commitWrite();
  1611. }
  1612. void waitForBridgeSaveSignal() noexcept override
  1613. {
  1614. // VSTs only save chunks, for which we already have a waitForSaved there
  1615. if (fPluginType != PLUGIN_VST2)
  1616. waitForSaved();
  1617. }
  1618. // -------------------------------------------------------------------
  1619. void handleNonRtData()
  1620. {
  1621. for (; fShmNonRtServerControl.isDataAvailableForReading();)
  1622. {
  1623. const PluginBridgeNonRtServerOpcode opcode(fShmNonRtServerControl.readOpcode());
  1624. #ifdef DEBUG
  1625. if (opcode != kPluginBridgeNonRtServerPong && opcode != kPluginBridgeNonRtServerParameterValue2) {
  1626. carla_debug("CarlaPluginBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtServerOpcode2str(opcode));
  1627. }
  1628. #endif
  1629. switch (opcode)
  1630. {
  1631. case kPluginBridgeNonRtServerNull:
  1632. case kPluginBridgeNonRtServerPong:
  1633. break;
  1634. case kPluginBridgeNonRtServerVersion:
  1635. fBridgeVersion = fShmNonRtServerControl.readUInt();
  1636. break;
  1637. case kPluginBridgeNonRtServerPluginInfo1: {
  1638. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  1639. const uint32_t category = fShmNonRtServerControl.readUInt();
  1640. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1641. const uint32_t optionAv = fShmNonRtServerControl.readUInt();
  1642. const uint32_t optionEn = fShmNonRtServerControl.readUInt();
  1643. const int64_t uniqueId = fShmNonRtServerControl.readLong();
  1644. if (fUniqueId != 0) {
  1645. CARLA_SAFE_ASSERT_INT2(fUniqueId == uniqueId, fUniqueId, uniqueId);
  1646. }
  1647. pData->hints = hints | PLUGIN_IS_BRIDGE;
  1648. pData->options = optionEn;
  1649. fInfo.category = static_cast<PluginCategory>(category);
  1650. fInfo.optionsAvailable = optionAv;
  1651. } break;
  1652. case kPluginBridgeNonRtServerPluginInfo2: {
  1653. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  1654. // realName
  1655. const uint32_t realNameSize(fShmNonRtServerControl.readUInt());
  1656. char realName[realNameSize+1];
  1657. carla_zeroChars(realName, realNameSize+1);
  1658. fShmNonRtServerControl.readCustomData(realName, realNameSize);
  1659. // label
  1660. const uint32_t labelSize(fShmNonRtServerControl.readUInt());
  1661. char label[labelSize+1];
  1662. carla_zeroChars(label, labelSize+1);
  1663. fShmNonRtServerControl.readCustomData(label, labelSize);
  1664. // maker
  1665. const uint32_t makerSize(fShmNonRtServerControl.readUInt());
  1666. char maker[makerSize+1];
  1667. carla_zeroChars(maker, makerSize+1);
  1668. fShmNonRtServerControl.readCustomData(maker, makerSize);
  1669. // copyright
  1670. const uint32_t copyrightSize(fShmNonRtServerControl.readUInt());
  1671. char copyright[copyrightSize+1];
  1672. carla_zeroChars(copyright, copyrightSize+1);
  1673. fShmNonRtServerControl.readCustomData(copyright, copyrightSize);
  1674. fInfo.name = realName;
  1675. fInfo.label = label;
  1676. fInfo.maker = maker;
  1677. fInfo.copyright = copyright;
  1678. if (pData->name == nullptr)
  1679. pData->name = pData->engine->getUniquePluginName(realName);
  1680. } break;
  1681. case kPluginBridgeNonRtServerAudioCount: {
  1682. // uint/ins, uint/outs
  1683. fInfo.clear();
  1684. fInfo.aIns = fShmNonRtServerControl.readUInt();
  1685. fInfo.aOuts = fShmNonRtServerControl.readUInt();
  1686. if (fInfo.aIns > 0)
  1687. {
  1688. fInfo.aInNames = new const char*[fInfo.aIns];
  1689. carla_zeroPointers(fInfo.aInNames, fInfo.aIns);
  1690. }
  1691. if (fInfo.aOuts > 0)
  1692. {
  1693. fInfo.aOutNames = new const char*[fInfo.aOuts];
  1694. carla_zeroPointers(fInfo.aOutNames, fInfo.aOuts);
  1695. }
  1696. } break;
  1697. case kPluginBridgeNonRtServerMidiCount: {
  1698. // uint/ins, uint/outs
  1699. fInfo.mIns = fShmNonRtServerControl.readUInt();
  1700. fInfo.mOuts = fShmNonRtServerControl.readUInt();
  1701. } break;
  1702. case kPluginBridgeNonRtServerCvCount: {
  1703. // uint/ins, uint/outs
  1704. fInfo.cvIns = fShmNonRtServerControl.readUInt();
  1705. fInfo.cvOuts = fShmNonRtServerControl.readUInt();
  1706. } break;
  1707. case kPluginBridgeNonRtServerParameterCount: {
  1708. // uint/count
  1709. const uint32_t count = fShmNonRtServerControl.readUInt();
  1710. // delete old data
  1711. pData->param.clear();
  1712. if (fParams != nullptr)
  1713. {
  1714. delete[] fParams;
  1715. fParams = nullptr;
  1716. }
  1717. if (count > 0)
  1718. {
  1719. pData->param.createNew(count, false);
  1720. fParams = new BridgeParamInfo[count];
  1721. // we might not receive all parameter data, so ensure range max is not 0
  1722. for (uint32_t i=0; i<count; ++i)
  1723. {
  1724. pData->param.ranges[i].def = 0.0f;
  1725. pData->param.ranges[i].min = 0.0f;
  1726. pData->param.ranges[i].max = 1.0f;
  1727. pData->param.ranges[i].step = 0.001f;
  1728. pData->param.ranges[i].stepSmall = 0.0001f;
  1729. pData->param.ranges[i].stepLarge = 0.1f;
  1730. }
  1731. }
  1732. } break;
  1733. case kPluginBridgeNonRtServerProgramCount: {
  1734. // uint/count
  1735. pData->prog.clear();
  1736. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1737. pData->prog.createNew(count);
  1738. } break;
  1739. case kPluginBridgeNonRtServerMidiProgramCount: {
  1740. // uint/count
  1741. pData->midiprog.clear();
  1742. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1743. pData->midiprog.createNew(count);
  1744. } break;
  1745. case kPluginBridgeNonRtServerPortName: {
  1746. // byte/type, uint/index, uint/size, str[] (name)
  1747. const uint8_t portType = fShmNonRtServerControl.readByte();
  1748. const uint32_t index = fShmNonRtServerControl.readUInt();
  1749. // name
  1750. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1751. char* const name = new char[nameSize+1];
  1752. carla_zeroChars(name, nameSize+1);
  1753. fShmNonRtServerControl.readCustomData(name, nameSize);
  1754. CARLA_SAFE_ASSERT_BREAK(portType > kPluginBridgePortNull && portType < kPluginBridgePortTypeCount);
  1755. switch (portType)
  1756. {
  1757. case kPluginBridgePortAudioInput:
  1758. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aIns);
  1759. fInfo.aInNames[index] = name;
  1760. break;
  1761. case kPluginBridgePortAudioOutput:
  1762. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aOuts);
  1763. fInfo.aOutNames[index] = name;
  1764. break;
  1765. }
  1766. } break;
  1767. case kPluginBridgeNonRtServerParameterData1: {
  1768. // uint/index, int/rindex, uint/type, uint/hints, int/cc
  1769. const uint32_t index = fShmNonRtServerControl.readUInt();
  1770. const int32_t rindex = fShmNonRtServerControl.readInt();
  1771. const uint32_t type = fShmNonRtServerControl.readUInt();
  1772. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1773. const int16_t ctrl = fShmNonRtServerControl.readShort();
  1774. CARLA_SAFE_ASSERT_BREAK(ctrl >= CONTROL_INDEX_NONE && ctrl <= CONTROL_INDEX_MAX_ALLOWED);
  1775. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1776. if (index < pData->param.count)
  1777. {
  1778. pData->param.data[index].type = static_cast<ParameterType>(type);
  1779. pData->param.data[index].index = static_cast<int32_t>(index);
  1780. pData->param.data[index].rindex = rindex;
  1781. pData->param.data[index].hints = hints;
  1782. pData->param.data[index].mappedControlIndex = ctrl;
  1783. }
  1784. } break;
  1785. case kPluginBridgeNonRtServerParameterData2: {
  1786. // uint/index, uint/size, str[] (name), uint/size, str[] (unit)
  1787. const uint32_t index = fShmNonRtServerControl.readUInt();
  1788. // name
  1789. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1790. char name[nameSize+1];
  1791. carla_zeroChars(name, nameSize+1);
  1792. fShmNonRtServerControl.readCustomData(name, nameSize);
  1793. // symbol
  1794. const uint32_t symbolSize(fShmNonRtServerControl.readUInt());
  1795. char symbol[symbolSize+1];
  1796. carla_zeroChars(symbol, symbolSize+1);
  1797. fShmNonRtServerControl.readCustomData(symbol, symbolSize);
  1798. // unit
  1799. const uint32_t unitSize(fShmNonRtServerControl.readUInt());
  1800. char unit[unitSize+1];
  1801. carla_zeroChars(unit, unitSize+1);
  1802. fShmNonRtServerControl.readCustomData(unit, unitSize);
  1803. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1804. if (index < pData->param.count)
  1805. {
  1806. fParams[index].name = name;
  1807. fParams[index].symbol = symbol;
  1808. fParams[index].unit = unit;
  1809. }
  1810. } break;
  1811. case kPluginBridgeNonRtServerParameterRanges: {
  1812. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  1813. const uint32_t index = fShmNonRtServerControl.readUInt();
  1814. const float def = fShmNonRtServerControl.readFloat();
  1815. const float min = fShmNonRtServerControl.readFloat();
  1816. const float max = fShmNonRtServerControl.readFloat();
  1817. const float step = fShmNonRtServerControl.readFloat();
  1818. const float stepSmall = fShmNonRtServerControl.readFloat();
  1819. const float stepLarge = fShmNonRtServerControl.readFloat();
  1820. CARLA_SAFE_ASSERT_BREAK(min < max);
  1821. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1822. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1823. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1824. if (index < pData->param.count)
  1825. {
  1826. pData->param.ranges[index].def = def;
  1827. pData->param.ranges[index].min = min;
  1828. pData->param.ranges[index].max = max;
  1829. pData->param.ranges[index].step = step;
  1830. pData->param.ranges[index].stepSmall = stepSmall;
  1831. pData->param.ranges[index].stepLarge = stepLarge;
  1832. }
  1833. } break;
  1834. case kPluginBridgeNonRtServerParameterValue: {
  1835. // uint/index, float/value
  1836. const uint32_t index = fShmNonRtServerControl.readUInt();
  1837. const float value = fShmNonRtServerControl.readFloat();
  1838. if (index < pData->param.count)
  1839. {
  1840. const float fixedValue(pData->param.getFixedValue(index, value));
  1841. if (carla_isNotEqual(fParams[index].value, fixedValue))
  1842. {
  1843. fParams[index].value = fixedValue;
  1844. CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
  1845. }
  1846. }
  1847. } break;
  1848. case kPluginBridgeNonRtServerParameterValue2: {
  1849. // uint/index, float/value
  1850. const uint32_t index = fShmNonRtServerControl.readUInt();
  1851. const float value = fShmNonRtServerControl.readFloat();
  1852. if (index < pData->param.count)
  1853. {
  1854. const float fixedValue(pData->param.getFixedValue(index, value));
  1855. fParams[index].value = fixedValue;
  1856. }
  1857. } break;
  1858. case kPluginBridgeNonRtServerParameterTouch: {
  1859. // uint/index, bool/touch
  1860. const uint32_t index = fShmNonRtServerControl.readUInt();
  1861. const bool touch = fShmNonRtServerControl.readBool();
  1862. pData->engine->touchPluginParameter(pData->id, index, touch);
  1863. } break;
  1864. case kPluginBridgeNonRtServerDefaultValue: {
  1865. // uint/index, float/value
  1866. const uint32_t index = fShmNonRtServerControl.readUInt();
  1867. const float value = fShmNonRtServerControl.readFloat();
  1868. if (index < pData->param.count)
  1869. pData->param.ranges[index].def = value;
  1870. } break;
  1871. case kPluginBridgeNonRtServerCurrentProgram: {
  1872. // int/index
  1873. const int32_t index = fShmNonRtServerControl.readInt();
  1874. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1875. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1876. CarlaPlugin::setProgram(index, false, true, true);
  1877. } break;
  1878. case kPluginBridgeNonRtServerCurrentMidiProgram: {
  1879. // int/index
  1880. const int32_t index = fShmNonRtServerControl.readInt();
  1881. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1882. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1883. CarlaPlugin::setMidiProgram(index, false, true, true);
  1884. } break;
  1885. case kPluginBridgeNonRtServerProgramName: {
  1886. // uint/index, uint/size, str[] (name)
  1887. const uint32_t index = fShmNonRtServerControl.readUInt();
  1888. // name
  1889. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1890. char name[nameSize+1];
  1891. carla_zeroChars(name, nameSize+1);
  1892. fShmNonRtServerControl.readCustomData(name, nameSize);
  1893. CARLA_SAFE_ASSERT_INT2(index < pData->prog.count, index, pData->prog.count);
  1894. if (index < pData->prog.count)
  1895. {
  1896. if (pData->prog.names[index] != nullptr)
  1897. delete[] pData->prog.names[index];
  1898. pData->prog.names[index] = carla_strdup(name);
  1899. }
  1900. } break;
  1901. case kPluginBridgeNonRtServerMidiProgramData: {
  1902. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  1903. const uint32_t index = fShmNonRtServerControl.readUInt();
  1904. const uint32_t bank = fShmNonRtServerControl.readUInt();
  1905. const uint32_t program = fShmNonRtServerControl.readUInt();
  1906. // name
  1907. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1908. char name[nameSize+1];
  1909. carla_zeroChars(name, nameSize+1);
  1910. fShmNonRtServerControl.readCustomData(name, nameSize);
  1911. CARLA_SAFE_ASSERT_INT2(index < pData->midiprog.count, index, pData->midiprog.count);
  1912. if (index < pData->midiprog.count)
  1913. {
  1914. if (pData->midiprog.data[index].name != nullptr)
  1915. delete[] pData->midiprog.data[index].name;
  1916. pData->midiprog.data[index].bank = bank;
  1917. pData->midiprog.data[index].program = program;
  1918. pData->midiprog.data[index].name = carla_strdup(name);
  1919. }
  1920. } break;
  1921. case kPluginBridgeNonRtServerSetCustomData: {
  1922. // uint/size, str[], uint/size, str[], uint/size, str[]
  1923. // type
  1924. const uint32_t typeSize(fShmNonRtServerControl.readUInt());
  1925. char type[typeSize+1];
  1926. carla_zeroChars(type, typeSize+1);
  1927. fShmNonRtServerControl.readCustomData(type, typeSize);
  1928. // key
  1929. const uint32_t keySize(fShmNonRtServerControl.readUInt());
  1930. char key[keySize+1];
  1931. carla_zeroChars(key, keySize+1);
  1932. fShmNonRtServerControl.readCustomData(key, keySize);
  1933. // value
  1934. const uint32_t valueSize(fShmNonRtServerControl.readUInt());
  1935. char value[valueSize+1];
  1936. carla_zeroChars(value, valueSize+1);
  1937. if (valueSize > 0)
  1938. fShmNonRtServerControl.readCustomData(value, valueSize);
  1939. CarlaPlugin::setCustomData(type, key, value, false);
  1940. } break;
  1941. case kPluginBridgeNonRtServerSetChunkDataFile: {
  1942. // uint/size, str[] (filename)
  1943. // chunkFilePath
  1944. const uint32_t chunkFilePathSize(fShmNonRtServerControl.readUInt());
  1945. char chunkFilePath[chunkFilePathSize+1];
  1946. carla_zeroChars(chunkFilePath, chunkFilePathSize+1);
  1947. fShmNonRtServerControl.readCustomData(chunkFilePath, chunkFilePathSize);
  1948. String realChunkFilePath(chunkFilePath);
  1949. #ifndef CARLA_OS_WIN
  1950. // Using Wine, fix temp dir
  1951. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1952. {
  1953. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1954. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1955. realChunkFilePath = fWinePrefix;
  1956. realChunkFilePath += "/drive_";
  1957. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1958. realChunkFilePath += driveLetterSplit[1];
  1959. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1960. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1961. }
  1962. #endif
  1963. File chunkFile(realChunkFilePath);
  1964. CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
  1965. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1966. chunkFile.deleteFile();
  1967. } break;
  1968. case kPluginBridgeNonRtServerSetLatency:
  1969. // uint
  1970. fLatency = fShmNonRtServerControl.readUInt();
  1971. #ifndef BUILD_BRIDGE
  1972. if (! fInitiated)
  1973. pData->latency.recreateBuffers(std::max(fInfo.aIns, fInfo.aOuts), fLatency);
  1974. #endif
  1975. break;
  1976. case kPluginBridgeNonRtServerSetParameterText: {
  1977. const int32_t index = fShmNonRtServerControl.readInt();
  1978. const uint32_t textSize(fShmNonRtServerControl.readUInt());
  1979. char text[textSize+1];
  1980. carla_zeroChars(text, textSize+1);
  1981. fShmNonRtServerControl.readCustomData(text, textSize);
  1982. fReceivingParamText.setReceivedData(index, text, textSize);
  1983. } break;
  1984. case kPluginBridgeNonRtServerReady:
  1985. fInitiated = true;
  1986. break;
  1987. case kPluginBridgeNonRtServerSaved:
  1988. fSaved = true;
  1989. break;
  1990. case kPluginBridgeNonRtServerUiClosed:
  1991. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1992. pData->transientTryCounter = 0;
  1993. #endif
  1994. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  1995. break;
  1996. case kPluginBridgeNonRtServerError: {
  1997. // error
  1998. const uint32_t errorSize(fShmNonRtServerControl.readUInt());
  1999. char error[errorSize+1];
  2000. carla_zeroChars(error, errorSize+1);
  2001. fShmNonRtServerControl.readCustomData(error, errorSize);
  2002. if (fInitiated)
  2003. {
  2004. pData->engine->callback(true, true, ENGINE_CALLBACK_ERROR, pData->id, 0, 0, 0, 0.0f, error);
  2005. // just in case
  2006. pData->engine->setLastError(error);
  2007. fInitError = true;
  2008. }
  2009. else
  2010. {
  2011. pData->engine->setLastError(error);
  2012. fInitError = true;
  2013. fInitiated = true;
  2014. }
  2015. } break;
  2016. }
  2017. }
  2018. }
  2019. // -------------------------------------------------------------------
  2020. uintptr_t getUiBridgeProcessId() const noexcept override
  2021. {
  2022. return fBridgeThread.getProcessPID();
  2023. }
  2024. const void* getExtraStuff() const noexcept override
  2025. {
  2026. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  2027. }
  2028. // -------------------------------------------------------------------
  2029. bool init(CarlaPluginPtr plugin,
  2030. const char* const filename,
  2031. const char* const name,
  2032. const char* const label,
  2033. const int64_t uniqueId,
  2034. const uint options,
  2035. const char* const bridgeBinary)
  2036. {
  2037. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2038. // ---------------------------------------------------------------
  2039. // first checks
  2040. if (pData->client != nullptr)
  2041. {
  2042. pData->engine->setLastError("Plugin client is already registered");
  2043. return false;
  2044. }
  2045. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2046. {
  2047. pData->engine->setLastError("null bridge binary");
  2048. return false;
  2049. }
  2050. // ---------------------------------------------------------------
  2051. // set info
  2052. if (name != nullptr && name[0] != '\0')
  2053. pData->name = pData->engine->getUniquePluginName(name);
  2054. if (filename != nullptr && filename[0] != '\0')
  2055. pData->filename = carla_strdup(filename);
  2056. else
  2057. pData->filename = carla_strdup("");
  2058. fUniqueId = uniqueId;
  2059. fBridgeBinary = bridgeBinary;
  2060. std::srand(static_cast<uint>(std::time(nullptr)));
  2061. // ---------------------------------------------------------------
  2062. // init sem/shm
  2063. if (! fShmAudioPool.initializeServer())
  2064. {
  2065. carla_stderr("Failed to initialize shared memory audio pool");
  2066. return false;
  2067. }
  2068. if (! fShmRtClientControl.initializeServer())
  2069. {
  2070. carla_stderr("Failed to initialize RT client control");
  2071. fShmAudioPool.clear();
  2072. return false;
  2073. }
  2074. if (! fShmNonRtClientControl.initializeServer())
  2075. {
  2076. carla_stderr("Failed to initialize Non-RT client control");
  2077. fShmRtClientControl.clear();
  2078. fShmAudioPool.clear();
  2079. return false;
  2080. }
  2081. if (! fShmNonRtServerControl.initializeServer())
  2082. {
  2083. carla_stderr("Failed to initialize Non-RT server control");
  2084. fShmNonRtClientControl.clear();
  2085. fShmRtClientControl.clear();
  2086. fShmAudioPool.clear();
  2087. return false;
  2088. }
  2089. #ifndef CARLA_OS_WIN
  2090. // ---------------------------------------------------------------
  2091. // set wine prefix
  2092. if (fBridgeBinary.contains(".exe", true))
  2093. {
  2094. const EngineOptions& engineOptions(pData->engine->getOptions());
  2095. if (engineOptions.wine.autoPrefix)
  2096. fWinePrefix = findWinePrefix(pData->filename);
  2097. if (fWinePrefix.isEmpty())
  2098. {
  2099. const char* const envWinePrefix(std::getenv("WINEPREFIX"));
  2100. if (envWinePrefix != nullptr && envWinePrefix[0] != '\0')
  2101. fWinePrefix = envWinePrefix;
  2102. else if (engineOptions.wine.fallbackPrefix != nullptr && engineOptions.wine.fallbackPrefix[0] != '\0')
  2103. fWinePrefix = engineOptions.wine.fallbackPrefix;
  2104. else
  2105. fWinePrefix = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  2106. }
  2107. }
  2108. #endif
  2109. // ---------------------------------------------------------------
  2110. // init bridge thread
  2111. {
  2112. char shmIdsStr[6*4+1];
  2113. carla_zeroChars(shmIdsStr, 6*4+1);
  2114. std::strncpy(shmIdsStr+6*0, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  2115. std::strncpy(shmIdsStr+6*1, &fShmRtClientControl.filename[fShmRtClientControl.filename.length()-6], 6);
  2116. std::strncpy(shmIdsStr+6*2, &fShmNonRtClientControl.filename[fShmNonRtClientControl.filename.length()-6], 6);
  2117. std::strncpy(shmIdsStr+6*3, &fShmNonRtServerControl.filename[fShmNonRtServerControl.filename.length()-6], 6);
  2118. fBridgeThread.setData(
  2119. #ifndef CARLA_OS_WIN
  2120. fWinePrefix.toRawUTF8(),
  2121. #endif
  2122. bridgeBinary, label, shmIdsStr);
  2123. }
  2124. if (! restartBridgeThread())
  2125. return false;
  2126. // ---------------------------------------------------------------
  2127. // register client
  2128. if (pData->name == nullptr)
  2129. {
  2130. if (label != nullptr && label[0] != '\0')
  2131. pData->name = pData->engine->getUniquePluginName(label);
  2132. else
  2133. pData->name = pData->engine->getUniquePluginName("unknown");
  2134. }
  2135. pData->client = pData->engine->addClient(plugin);
  2136. if (pData->client == nullptr || ! pData->client->isOk())
  2137. {
  2138. pData->engine->setLastError("Failed to register plugin client");
  2139. return false;
  2140. }
  2141. // ---------------------------------------------------------------
  2142. // set options
  2143. pData->options = 0x0;
  2144. if ((fInfo.optionsAvailable & PLUGIN_OPTION_FIXED_BUFFERS) == 0x0)
  2145. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2146. else if (isPluginOptionEnabled(options, PLUGIN_OPTION_FIXED_BUFFERS))
  2147. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2148. if (pData->engine->getOptions().forceStereo)
  2149. {
  2150. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2151. }
  2152. else if (fInfo.optionsAvailable & PLUGIN_OPTION_FORCE_STEREO)
  2153. {
  2154. if (options & PLUGIN_OPTION_FORCE_STEREO)
  2155. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2156. }
  2157. if (fInfo.optionsAvailable & PLUGIN_OPTION_USE_CHUNKS)
  2158. if (isPluginOptionEnabled(options, PLUGIN_OPTION_USE_CHUNKS))
  2159. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  2160. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  2161. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2162. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2163. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  2164. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2165. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2166. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  2167. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2168. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2169. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PITCHBEND)
  2170. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2171. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2172. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2173. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2174. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2175. if (fInfo.optionsAvailable & PLUGIN_OPTION_SKIP_SENDING_NOTES)
  2176. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  2177. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  2178. if (fInfo.optionsAvailable & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2179. {
  2180. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  2181. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2182. }
  2183. else if (fInfo.optionsAvailable & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2184. {
  2185. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2186. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2187. }
  2188. // kPluginBridgeNonRtClientSetOptions was added in API 7
  2189. if (fBridgeVersion >= 7)
  2190. {
  2191. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2192. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetOptions);
  2193. fShmNonRtClientControl.writeUInt(pData->options);
  2194. fShmNonRtClientControl.commitWrite();
  2195. }
  2196. return true;
  2197. }
  2198. private:
  2199. const BinaryType fBinaryType;
  2200. const PluginType fPluginType;
  2201. uint fBridgeVersion;
  2202. bool fInitiated;
  2203. bool fInitError;
  2204. bool fSaved;
  2205. bool fTimedOut;
  2206. bool fTimedError;
  2207. uint fBufferSize;
  2208. uint fProcWaitTime;
  2209. CarlaString fBridgeBinary;
  2210. CarlaPluginBridgeThread fBridgeThread;
  2211. BridgeAudioPool fShmAudioPool;
  2212. BridgeRtClientControl fShmRtClientControl;
  2213. BridgeNonRtClientControl fShmNonRtClientControl;
  2214. BridgeNonRtServerControl fShmNonRtServerControl;
  2215. #ifndef CARLA_OS_WIN
  2216. String fWinePrefix;
  2217. #endif
  2218. class ReceivingParamText {
  2219. public:
  2220. ReceivingParamText() noexcept
  2221. : dataRecv(false),
  2222. dataOk(false),
  2223. index(-1),
  2224. strBuf(nullptr),
  2225. mutex() {}
  2226. bool isCurrentlyWaitingData() const noexcept
  2227. {
  2228. return index >= 0;
  2229. }
  2230. bool wasDataReceived(bool* const success) const noexcept
  2231. {
  2232. *success = dataOk;
  2233. return dataRecv;
  2234. }
  2235. void setTargetData(const int32_t i, char* const b) noexcept
  2236. {
  2237. const CarlaMutexLocker cml(mutex);
  2238. dataOk = false;
  2239. dataRecv = false;
  2240. index = i;
  2241. strBuf = b;
  2242. }
  2243. void setReceivedData(const int32_t i, const char* const b, const uint blen) noexcept
  2244. {
  2245. CarlaScopedValueSetter<bool> svs(dataRecv, false, true);
  2246. const CarlaMutexLocker cml(mutex);
  2247. // make backup and reset data
  2248. const int32_t indexCopy = index;
  2249. char* const strBufCopy = strBuf;
  2250. index = -1;
  2251. strBuf = nullptr;
  2252. CARLA_SAFE_ASSERT_RETURN(indexCopy == i,);
  2253. CARLA_SAFE_ASSERT_RETURN(strBufCopy != nullptr,);
  2254. std::strncpy(strBufCopy, b, std::min(blen, STR_MAX-1U));
  2255. dataOk = true;
  2256. }
  2257. private:
  2258. bool dataRecv;
  2259. bool dataOk;
  2260. int32_t index;
  2261. char* strBuf;
  2262. CarlaMutex mutex;
  2263. CARLA_DECLARE_NON_COPY_CLASS(ReceivingParamText)
  2264. } fReceivingParamText;
  2265. struct Info {
  2266. uint32_t aIns, aOuts;
  2267. uint32_t cvIns, cvOuts;
  2268. uint32_t mIns, mOuts;
  2269. PluginCategory category;
  2270. uint optionsAvailable;
  2271. CarlaString name;
  2272. CarlaString label;
  2273. CarlaString maker;
  2274. CarlaString copyright;
  2275. const char** aInNames;
  2276. const char** aOutNames;
  2277. const char** cvInNames;
  2278. const char** cvOutNames;
  2279. std::vector<uint8_t> chunk;
  2280. Info()
  2281. : aIns(0),
  2282. aOuts(0),
  2283. cvIns(0),
  2284. cvOuts(0),
  2285. mIns(0),
  2286. mOuts(0),
  2287. category(PLUGIN_CATEGORY_NONE),
  2288. optionsAvailable(0),
  2289. name(),
  2290. label(),
  2291. maker(),
  2292. copyright(),
  2293. aInNames(nullptr),
  2294. aOutNames(nullptr),
  2295. cvInNames(nullptr),
  2296. cvOutNames(nullptr),
  2297. chunk() {}
  2298. ~Info()
  2299. {
  2300. clear();
  2301. }
  2302. void clear()
  2303. {
  2304. if (aInNames != nullptr)
  2305. {
  2306. CARLA_SAFE_ASSERT_INT(aIns > 0, aIns);
  2307. for (uint32_t i=0; i<aIns; ++i)
  2308. delete[] aInNames[i];
  2309. delete[] aInNames;
  2310. aInNames = nullptr;
  2311. }
  2312. if (aOutNames != nullptr)
  2313. {
  2314. CARLA_SAFE_ASSERT_INT(aOuts > 0, aOuts);
  2315. for (uint32_t i=0; i<aOuts; ++i)
  2316. delete[] aOutNames[i];
  2317. delete[] aOutNames;
  2318. aOutNames = nullptr;
  2319. }
  2320. if (cvInNames != nullptr)
  2321. {
  2322. CARLA_SAFE_ASSERT_INT(cvIns > 0, cvIns);
  2323. for (uint32_t i=0; i<cvIns; ++i)
  2324. delete[] cvInNames[i];
  2325. delete[] cvInNames;
  2326. cvInNames = nullptr;
  2327. }
  2328. if (cvOutNames != nullptr)
  2329. {
  2330. CARLA_SAFE_ASSERT_INT(cvOuts > 0, cvOuts);
  2331. for (uint32_t i=0; i<cvOuts; ++i)
  2332. delete[] cvOutNames[i];
  2333. delete[] cvOutNames;
  2334. cvOutNames = nullptr;
  2335. }
  2336. aIns = aOuts = cvIns = cvOuts = 0;
  2337. }
  2338. CARLA_DECLARE_NON_COPY_STRUCT(Info)
  2339. } fInfo;
  2340. int64_t fUniqueId;
  2341. uint32_t fLatency;
  2342. BridgeParamInfo* fParams;
  2343. void handleProcessStopped() noexcept
  2344. {
  2345. const bool wasActive = pData->active;
  2346. pData->active = false;
  2347. if (wasActive)
  2348. {
  2349. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2350. pData->engine->callback(true, true,
  2351. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  2352. pData->id,
  2353. PARAMETER_ACTIVE,
  2354. 0, 0,
  2355. 0.0f,
  2356. nullptr);
  2357. #endif
  2358. }
  2359. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  2360. {
  2361. pData->engine->callback(true, true,
  2362. ENGINE_CALLBACK_UI_STATE_CHANGED,
  2363. pData->id,
  2364. 0,
  2365. 0, 0, 0.0f, nullptr);
  2366. }
  2367. }
  2368. void resizeAudioPool(const uint32_t bufferSize)
  2369. {
  2370. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts, fInfo.cvIns+fInfo.cvOuts);
  2371. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  2372. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  2373. fShmRtClientControl.commitWrite();
  2374. waitForClient("resize-pool", 5000);
  2375. }
  2376. void waitForClient(const char* const action, const uint msecs)
  2377. {
  2378. CARLA_SAFE_ASSERT_RETURN(! fTimedOut,);
  2379. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  2380. if (fShmRtClientControl.waitForClient(msecs))
  2381. return;
  2382. fTimedOut = true;
  2383. carla_stderr2("waitForClient(%s) timed out", action);
  2384. }
  2385. bool restartBridgeThread()
  2386. {
  2387. fInitiated = false;
  2388. fInitError = false;
  2389. fTimedError = false;
  2390. // reset memory
  2391. fShmRtClientControl.data->procFlags = 0;
  2392. carla_zeroStruct(fShmRtClientControl.data->timeInfo);
  2393. carla_zeroBytes(fShmRtClientControl.data->midiOut, kBridgeRtClientDataMidiOutSize);
  2394. fShmRtClientControl.clearData();
  2395. fShmNonRtClientControl.clearData();
  2396. fShmNonRtServerControl.clearData();
  2397. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientVersion);
  2398. fShmNonRtClientControl.writeUInt(CARLA_PLUGIN_BRIDGE_API_VERSION_CURRENT);
  2399. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeRtClientData)));
  2400. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtClientData)));
  2401. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtServerData)));
  2402. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientInitialSetup);
  2403. fShmNonRtClientControl.writeUInt(pData->engine->getBufferSize());
  2404. fShmNonRtClientControl.writeDouble(pData->engine->getSampleRate());
  2405. fShmNonRtClientControl.commitWrite();
  2406. if (fShmAudioPool.dataSize != 0)
  2407. {
  2408. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  2409. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  2410. fShmRtClientControl.commitWrite();
  2411. }
  2412. else
  2413. {
  2414. // testing dummy message
  2415. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientNull);
  2416. fShmRtClientControl.commitWrite();
  2417. }
  2418. fBridgeThread.startThread();
  2419. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  2420. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2421. const bool needsCancelableAction = ! pData->engine->isLoadingProject();
  2422. if (needsCancelableAction)
  2423. {
  2424. pData->engine->setActionCanceled(false);
  2425. pData->engine->callback(true, true,
  2426. ENGINE_CALLBACK_CANCELABLE_ACTION,
  2427. pData->id,
  2428. 1,
  2429. 0, 0, 0.0f,
  2430. "Loading plugin bridge");
  2431. }
  2432. #endif
  2433. for (;fBridgeThread.isThreadRunning();)
  2434. {
  2435. pData->engine->callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2436. if (needsEngineIdle)
  2437. pData->engine->idle();
  2438. idle();
  2439. if (fInitiated)
  2440. break;
  2441. if (pData->engine->isAboutToClose() || pData->engine->wasActionCanceled())
  2442. break;
  2443. carla_msleep(5);
  2444. }
  2445. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2446. if (needsCancelableAction)
  2447. {
  2448. pData->engine->callback(true, true,
  2449. ENGINE_CALLBACK_CANCELABLE_ACTION,
  2450. pData->id,
  2451. 0,
  2452. 0, 0, 0.0f,
  2453. "Loading JACK application");
  2454. }
  2455. #endif
  2456. if (fInitError || ! fInitiated)
  2457. {
  2458. fBridgeThread.stopThread(6000);
  2459. if (! fInitError)
  2460. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n"
  2461. "(or the plugin crashed on initialization?)");
  2462. return false;
  2463. }
  2464. if (const size_t dataSize = fInfo.chunk.size())
  2465. {
  2466. #ifdef CARLA_PROPER_CPP11_SUPPORT
  2467. void* data = fInfo.chunk.data();
  2468. #else
  2469. void* data = &fInfo.chunk.front();
  2470. #endif
  2471. CarlaString dataBase64(CarlaString::asBase64(data, dataSize));
  2472. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0, true);
  2473. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  2474. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  2475. filePath += fShmAudioPool.getFilenameSuffix();
  2476. if (File(filePath).replaceWithText(dataBase64.buffer()))
  2477. {
  2478. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  2479. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2480. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetChunkDataFile);
  2481. fShmNonRtClientControl.writeUInt(ulength);
  2482. fShmNonRtClientControl.writeCustomData(filePath.toRawUTF8(), ulength);
  2483. fShmNonRtClientControl.commitWrite();
  2484. }
  2485. }
  2486. return true;
  2487. }
  2488. void _setUiTitleFromName()
  2489. {
  2490. CarlaString uiName(pData->name);
  2491. uiName += " (GUI)";
  2492. const uint32_t size = static_cast<uint32_t>(uiName.length());
  2493. const CarlaMutexLocker _cml(fShmNonRtClientControl.mutex);
  2494. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientSetWindowTitle);
  2495. fShmNonRtClientControl.writeUInt(size);
  2496. fShmNonRtClientControl.writeCustomData(uiName.buffer(), size);
  2497. fShmNonRtClientControl.commitWrite();
  2498. }
  2499. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridge)
  2500. };
  2501. CARLA_BACKEND_END_NAMESPACE
  2502. // ---------------------------------------------------------------------------------------------------------------------
  2503. CARLA_BACKEND_START_NAMESPACE
  2504. CarlaPluginPtr CarlaPlugin::newBridge(const Initializer& init,
  2505. BinaryType btype, PluginType ptype, const char* bridgeBinary)
  2506. {
  2507. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")",
  2508. init.engine, init.filename, init.name, init.label,
  2509. BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  2510. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2511. {
  2512. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  2513. return nullptr;
  2514. }
  2515. #ifndef CARLA_OS_WIN
  2516. // FIXME: somewhere, somehow, we end up with double slashes, wine doesn't like that.
  2517. if (std::strncmp(bridgeBinary, "//", 2) == 0)
  2518. ++bridgeBinary;
  2519. #endif
  2520. std::shared_ptr<CarlaPluginBridge> plugin(new CarlaPluginBridge(init.engine, init.id, btype, ptype));
  2521. if (! plugin->init(plugin, init.filename, init.name, init.label, init.uniqueId, init.options, bridgeBinary))
  2522. return nullptr;
  2523. return plugin;
  2524. }
  2525. CARLA_BACKEND_END_NAMESPACE
  2526. // ---------------------------------------------------------------------------------------------------------------------
  2527. #ifndef BUILD_BRIDGE
  2528. # include "CarlaBridgeUtils.cpp"
  2529. #endif
  2530. // ---------------------------------------------------------------------------------------------------------------------