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.

2614 lines
92KB

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