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.

2739 lines
95KB

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