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.

2643 lines
92KB

  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. 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. #ifndef BUILD_BRIDGE
  316. // close UI
  317. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  318. pData->transientTryCounter = 0;
  319. #endif
  320. pData->singleMutex.lock();
  321. pData->masterMutex.lock();
  322. if (pData->client != nullptr && pData->client->isActive())
  323. pData->client->deactivate();
  324. if (pData->active)
  325. {
  326. deactivate();
  327. pData->active = false;
  328. }
  329. if (fBridgeThread.isThreadRunning())
  330. {
  331. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientQuit);
  332. fShmNonRtClientControl.commitWrite();
  333. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientQuit);
  334. fShmRtClientControl.commitWrite();
  335. if (! fTimedOut)
  336. waitForClient("stopping", 3000);
  337. }
  338. fBridgeThread.stopThread(3000);
  339. fShmNonRtServerControl.clear();
  340. fShmNonRtClientControl.clear();
  341. fShmRtClientControl.clear();
  342. fShmAudioPool.clear();
  343. clearBuffers();
  344. fInfo.chunk.clear();
  345. }
  346. // -------------------------------------------------------------------
  347. // Information (base)
  348. BinaryType getBinaryType() const noexcept override
  349. {
  350. return fBinaryType;
  351. }
  352. PluginType getType() const noexcept override
  353. {
  354. return fPluginType;
  355. }
  356. PluginCategory getCategory() const noexcept override
  357. {
  358. return fInfo.category;
  359. }
  360. int64_t getUniqueId() const noexcept override
  361. {
  362. return fUniqueId;
  363. }
  364. uint32_t getLatencyInFrames() const noexcept override
  365. {
  366. return fLatency;
  367. }
  368. // -------------------------------------------------------------------
  369. // Information (count)
  370. uint32_t getMidiInCount() const noexcept override
  371. {
  372. return fInfo.mIns;
  373. }
  374. uint32_t getMidiOutCount() const noexcept override
  375. {
  376. return fInfo.mOuts;
  377. }
  378. // -------------------------------------------------------------------
  379. // Information (current data)
  380. void updateCustomData() noexcept override
  381. {
  382. waitForSaved();
  383. }
  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. pData->engine->callback(ENGINE_CALLBACK_PLUGIN_UNAVAILABLE, pData->id, 0, 0, 0.0f,
  710. "Plugin bridge has been stopped or crashed");
  711. }
  712. CarlaPlugin::idle();
  713. }
  714. // -------------------------------------------------------------------
  715. // Plugin state
  716. void reload() override
  717. {
  718. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  719. carla_debug("CarlaPluginBridge::reload() - start");
  720. const EngineProcessMode processMode(pData->engine->getProccessMode());
  721. // Safely disable plugin for reload
  722. const ScopedDisabler sd(this);
  723. // cleanup of previous data
  724. pData->audioIn.clear();
  725. pData->audioOut.clear();
  726. pData->cvIn.clear();
  727. pData->cvOut.clear();
  728. pData->event.clear();
  729. bool needsCtrlIn, needsCtrlOut;
  730. needsCtrlIn = needsCtrlOut = false;
  731. if (fInfo.aIns > 0)
  732. {
  733. pData->audioIn.createNew(fInfo.aIns);
  734. }
  735. if (fInfo.aOuts > 0)
  736. {
  737. pData->audioOut.createNew(fInfo.aOuts);
  738. needsCtrlIn = true;
  739. }
  740. if (fInfo.cvIns > 0)
  741. {
  742. pData->cvIn.createNew(fInfo.cvIns);
  743. }
  744. if (fInfo.cvOuts > 0)
  745. {
  746. pData->cvOut.createNew(fInfo.cvOuts);
  747. }
  748. if (fInfo.mIns > 0)
  749. needsCtrlIn = true;
  750. if (fInfo.mOuts > 0)
  751. needsCtrlOut = true;
  752. const uint portNameSize(pData->engine->getMaxPortNameSize());
  753. CarlaString portName;
  754. // Audio Ins
  755. for (uint32_t j=0; j < fInfo.aIns; ++j)
  756. {
  757. portName.clear();
  758. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  759. {
  760. portName = pData->name;
  761. portName += ":";
  762. }
  763. if (fInfo.aInNames != nullptr && fInfo.aInNames[j] != nullptr)
  764. {
  765. portName += fInfo.aInNames[j];
  766. }
  767. else if (fInfo.aIns > 1)
  768. {
  769. portName += "input_";
  770. portName += CarlaString(j+1);
  771. }
  772. else
  773. portName += "input";
  774. portName.truncate(portNameSize);
  775. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  776. pData->audioIn.ports[j].rindex = j;
  777. }
  778. // Audio Outs
  779. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  780. {
  781. portName.clear();
  782. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  783. {
  784. portName = pData->name;
  785. portName += ":";
  786. }
  787. if (fInfo.aOutNames != nullptr && fInfo.aOutNames[j] != nullptr)
  788. {
  789. portName += fInfo.aOutNames[j];
  790. }
  791. else if (fInfo.aOuts > 1)
  792. {
  793. portName += "output_";
  794. portName += CarlaString(j+1);
  795. }
  796. else
  797. portName += "output";
  798. portName.truncate(portNameSize);
  799. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  800. pData->audioOut.ports[j].rindex = j;
  801. }
  802. // TODO - MIDI
  803. // TODO - CV
  804. if (needsCtrlIn)
  805. {
  806. portName.clear();
  807. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  808. {
  809. portName = pData->name;
  810. portName += ":";
  811. }
  812. portName += "event-in";
  813. portName.truncate(portNameSize);
  814. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  815. }
  816. if (needsCtrlOut)
  817. {
  818. portName.clear();
  819. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  820. {
  821. portName = pData->name;
  822. portName += ":";
  823. }
  824. portName += "event-out";
  825. portName.truncate(portNameSize);
  826. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  827. }
  828. // extra plugin hints
  829. pData->extraHints = 0x0;
  830. if (fInfo.mIns > 0)
  831. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  832. if (fInfo.mOuts > 0)
  833. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  834. if (fInfo.aIns <= 2 && fInfo.aOuts <= 2 && (fInfo.aIns == fInfo.aOuts || fInfo.aIns == 0 || fInfo.aOuts == 0))
  835. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  836. bufferSizeChanged(pData->engine->getBufferSize());
  837. reloadPrograms(true);
  838. carla_debug("CarlaPluginBridge::reload() - end");
  839. }
  840. // -------------------------------------------------------------------
  841. // Plugin processing
  842. void activate() noexcept override
  843. {
  844. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  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->midiprog.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. void handleNonRtData()
  1364. {
  1365. for (; fShmNonRtServerControl.isDataAvailableForReading();)
  1366. {
  1367. const PluginBridgeNonRtServerOpcode opcode(fShmNonRtServerControl.readOpcode());
  1368. #ifdef DEBUG
  1369. if (opcode != kPluginBridgeNonRtServerPong) {
  1370. carla_debug("CarlaPluginBridge::handleNonRtData() - got opcode: %s", PluginBridgeNonRtServerOpcode2str(opcode));
  1371. }
  1372. #endif
  1373. if (opcode != kPluginBridgeNonRtServerNull && fLastPongTime > 0)
  1374. fLastPongTime = Time::currentTimeMillis();
  1375. switch (opcode)
  1376. {
  1377. case kPluginBridgeNonRtServerNull:
  1378. case kPluginBridgeNonRtServerPong:
  1379. break;
  1380. case kPluginBridgeNonRtServerPluginInfo1: {
  1381. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  1382. const uint32_t category = fShmNonRtServerControl.readUInt();
  1383. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1384. const uint32_t optionAv = fShmNonRtServerControl.readUInt();
  1385. const uint32_t optionEn = fShmNonRtServerControl.readUInt();
  1386. const int64_t uniqueId = fShmNonRtServerControl.readLong();
  1387. if (fUniqueId != 0) {
  1388. CARLA_SAFE_ASSERT_INT2(fUniqueId == uniqueId, fUniqueId, uniqueId);
  1389. }
  1390. pData->hints = hints | PLUGIN_IS_BRIDGE;
  1391. pData->options = optionEn;
  1392. fInfo.category = static_cast<PluginCategory>(category);
  1393. fInfo.optionsAvailable = optionAv;
  1394. } break;
  1395. case kPluginBridgeNonRtServerPluginInfo2: {
  1396. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  1397. // realName
  1398. const uint32_t realNameSize(fShmNonRtServerControl.readUInt());
  1399. char realName[realNameSize+1];
  1400. carla_zeroChars(realName, realNameSize+1);
  1401. fShmNonRtServerControl.readCustomData(realName, realNameSize);
  1402. // label
  1403. const uint32_t labelSize(fShmNonRtServerControl.readUInt());
  1404. char label[labelSize+1];
  1405. carla_zeroChars(label, labelSize+1);
  1406. fShmNonRtServerControl.readCustomData(label, labelSize);
  1407. // maker
  1408. const uint32_t makerSize(fShmNonRtServerControl.readUInt());
  1409. char maker[makerSize+1];
  1410. carla_zeroChars(maker, makerSize+1);
  1411. fShmNonRtServerControl.readCustomData(maker, makerSize);
  1412. // copyright
  1413. const uint32_t copyrightSize(fShmNonRtServerControl.readUInt());
  1414. char copyright[copyrightSize+1];
  1415. carla_zeroChars(copyright, copyrightSize+1);
  1416. fShmNonRtServerControl.readCustomData(copyright, copyrightSize);
  1417. fInfo.name = realName;
  1418. fInfo.label = label;
  1419. fInfo.maker = maker;
  1420. fInfo.copyright = copyright;
  1421. if (pData->name == nullptr)
  1422. pData->name = pData->engine->getUniquePluginName(realName);
  1423. } break;
  1424. case kPluginBridgeNonRtServerAudioCount: {
  1425. // uint/ins, uint/outs
  1426. fInfo.aIns = fShmNonRtServerControl.readUInt();
  1427. fInfo.aOuts = fShmNonRtServerControl.readUInt();
  1428. CARLA_SAFE_ASSERT(fInfo.aInNames == nullptr);
  1429. CARLA_SAFE_ASSERT(fInfo.aOutNames == nullptr);
  1430. if (fInfo.aIns > 0)
  1431. {
  1432. fInfo.aInNames = new const char*[fInfo.aIns];
  1433. carla_zeroPointers(fInfo.aInNames, fInfo.aIns);
  1434. }
  1435. if (fInfo.aOuts > 0)
  1436. {
  1437. fInfo.aOutNames = new const char*[fInfo.aOuts];
  1438. carla_zeroPointers(fInfo.aOutNames, fInfo.aOuts);
  1439. }
  1440. } break;
  1441. case kPluginBridgeNonRtServerMidiCount: {
  1442. // uint/ins, uint/outs
  1443. fInfo.mIns = fShmNonRtServerControl.readUInt();
  1444. fInfo.mOuts = fShmNonRtServerControl.readUInt();
  1445. } break;
  1446. case kPluginBridgeNonRtServerCvCount: {
  1447. // uint/ins, uint/outs
  1448. fInfo.cvIns = fShmNonRtServerControl.readUInt();
  1449. fInfo.cvOuts = fShmNonRtServerControl.readUInt();
  1450. } break;
  1451. case kPluginBridgeNonRtServerParameterCount: {
  1452. // uint/count
  1453. const uint32_t count = fShmNonRtServerControl.readUInt();
  1454. // delete old data
  1455. pData->param.clear();
  1456. if (fParams != nullptr)
  1457. {
  1458. delete[] fParams;
  1459. fParams = nullptr;
  1460. }
  1461. if (count > 0)
  1462. {
  1463. pData->param.createNew(count, false);
  1464. fParams = new BridgeParamInfo[count];
  1465. // we might not receive all parameter data, so ensure range max is not 0
  1466. for (uint32_t i=0; i<count; ++i)
  1467. {
  1468. pData->param.ranges[i].def = 0.0f;
  1469. pData->param.ranges[i].min = 0.0f;
  1470. pData->param.ranges[i].max = 1.0f;
  1471. pData->param.ranges[i].step = 0.001f;
  1472. pData->param.ranges[i].stepSmall = 0.0001f;
  1473. pData->param.ranges[i].stepLarge = 0.1f;
  1474. }
  1475. }
  1476. } break;
  1477. case kPluginBridgeNonRtServerProgramCount: {
  1478. // uint/count
  1479. pData->prog.clear();
  1480. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1481. pData->prog.createNew(static_cast<uint32_t>(count));
  1482. } break;
  1483. case kPluginBridgeNonRtServerMidiProgramCount: {
  1484. // uint/count
  1485. pData->midiprog.clear();
  1486. if (const uint32_t count = fShmNonRtServerControl.readUInt())
  1487. pData->midiprog.createNew(static_cast<uint32_t>(count));
  1488. } break;
  1489. case kPluginBridgeNonRtServerPortName: {
  1490. // byte/type, uint/index, uint/size, str[] (name)
  1491. const uint8_t portType = fShmNonRtServerControl.readByte();
  1492. const uint32_t index = fShmNonRtServerControl.readUInt();
  1493. // name
  1494. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1495. char* const name = new char[nameSize+1];
  1496. carla_zeroChars(name, nameSize+1);
  1497. fShmNonRtServerControl.readCustomData(name, nameSize);
  1498. CARLA_SAFE_ASSERT_BREAK(portType > kPluginBridgePortNull && portType < kPluginBridgePortTypeCount);
  1499. switch (portType)
  1500. {
  1501. case kPluginBridgePortAudioInput:
  1502. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aIns);
  1503. fInfo.aInNames[index] = name;
  1504. break;
  1505. case kPluginBridgePortAudioOutput:
  1506. CARLA_SAFE_ASSERT_BREAK(index < fInfo.aOuts);
  1507. fInfo.aOutNames[index] = name;
  1508. break;
  1509. }
  1510. } break;
  1511. case kPluginBridgeNonRtServerParameterData1: {
  1512. // uint/index, int/rindex, uint/type, uint/hints, int/cc
  1513. const uint32_t index = fShmNonRtServerControl.readUInt();
  1514. const int32_t rindex = fShmNonRtServerControl.readInt();
  1515. const uint32_t type = fShmNonRtServerControl.readUInt();
  1516. const uint32_t hints = fShmNonRtServerControl.readUInt();
  1517. const int16_t midiCC = fShmNonRtServerControl.readShort();
  1518. CARLA_SAFE_ASSERT_BREAK(midiCC >= -1 && midiCC < MAX_MIDI_CONTROL);
  1519. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1520. if (index < pData->param.count)
  1521. {
  1522. pData->param.data[index].type = static_cast<ParameterType>(type);
  1523. pData->param.data[index].index = static_cast<int32_t>(index);
  1524. pData->param.data[index].rindex = rindex;
  1525. pData->param.data[index].hints = hints;
  1526. pData->param.data[index].midiCC = midiCC;
  1527. }
  1528. } break;
  1529. case kPluginBridgeNonRtServerParameterData2: {
  1530. // uint/index, uint/size, str[] (name), uint/size, str[] (unit)
  1531. const uint32_t index = fShmNonRtServerControl.readUInt();
  1532. // name
  1533. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1534. char name[nameSize+1];
  1535. carla_zeroChars(name, nameSize+1);
  1536. fShmNonRtServerControl.readCustomData(name, nameSize);
  1537. // symbol
  1538. const uint32_t symbolSize(fShmNonRtServerControl.readUInt());
  1539. char symbol[symbolSize+1];
  1540. carla_zeroChars(symbol, symbolSize+1);
  1541. fShmNonRtServerControl.readCustomData(symbol, symbolSize);
  1542. // unit
  1543. const uint32_t unitSize(fShmNonRtServerControl.readUInt());
  1544. char unit[unitSize+1];
  1545. carla_zeroChars(unit, unitSize+1);
  1546. fShmNonRtServerControl.readCustomData(unit, unitSize);
  1547. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1548. if (index < pData->param.count)
  1549. {
  1550. fParams[index].name = name;
  1551. fParams[index].symbol = symbol;
  1552. fParams[index].unit = unit;
  1553. }
  1554. } break;
  1555. case kPluginBridgeNonRtServerParameterRanges: {
  1556. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  1557. const uint32_t index = fShmNonRtServerControl.readUInt();
  1558. const float def = fShmNonRtServerControl.readFloat();
  1559. const float min = fShmNonRtServerControl.readFloat();
  1560. const float max = fShmNonRtServerControl.readFloat();
  1561. const float step = fShmNonRtServerControl.readFloat();
  1562. const float stepSmall = fShmNonRtServerControl.readFloat();
  1563. const float stepLarge = fShmNonRtServerControl.readFloat();
  1564. CARLA_SAFE_ASSERT_BREAK(min < max);
  1565. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1566. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1567. CARLA_SAFE_ASSERT_INT2(index < pData->param.count, index, pData->param.count);
  1568. if (index < pData->param.count)
  1569. {
  1570. pData->param.ranges[index].def = def;
  1571. pData->param.ranges[index].min = min;
  1572. pData->param.ranges[index].max = max;
  1573. pData->param.ranges[index].step = step;
  1574. pData->param.ranges[index].stepSmall = stepSmall;
  1575. pData->param.ranges[index].stepLarge = stepLarge;
  1576. }
  1577. } break;
  1578. case kPluginBridgeNonRtServerParameterValue: {
  1579. // uint/index, float/value
  1580. const uint32_t index = fShmNonRtServerControl.readUInt();
  1581. const float value = fShmNonRtServerControl.readFloat();
  1582. if (index < pData->param.count)
  1583. {
  1584. const float fixedValue(pData->param.getFixedValue(index, value));
  1585. fParams[index].value = fixedValue;
  1586. CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
  1587. }
  1588. } break;
  1589. case kPluginBridgeNonRtServerParameterValue2: {
  1590. // uint/index, float/value
  1591. const uint32_t index = fShmNonRtServerControl.readUInt();
  1592. const float value = fShmNonRtServerControl.readFloat();
  1593. if (index < pData->param.count)
  1594. {
  1595. const float fixedValue(pData->param.getFixedValue(index, value));
  1596. fParams[index].value = fixedValue;
  1597. }
  1598. } break;
  1599. case kPluginBridgeNonRtServerDefaultValue: {
  1600. // uint/index, float/value
  1601. const uint32_t index = fShmNonRtServerControl.readUInt();
  1602. const float value = fShmNonRtServerControl.readFloat();
  1603. if (index < pData->param.count)
  1604. pData->param.ranges[index].def = value;
  1605. } break;
  1606. case kPluginBridgeNonRtServerCurrentProgram: {
  1607. // int/index
  1608. const int32_t index = fShmNonRtServerControl.readInt();
  1609. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1610. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1611. CarlaPlugin::setProgram(index, false, true, true);
  1612. } break;
  1613. case kPluginBridgeNonRtServerCurrentMidiProgram: {
  1614. // int/index
  1615. const int32_t index = fShmNonRtServerControl.readInt();
  1616. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1617. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1618. CarlaPlugin::setMidiProgram(index, false, true, true);
  1619. } break;
  1620. case kPluginBridgeNonRtServerProgramName: {
  1621. // uint/index, uint/size, str[] (name)
  1622. const uint32_t index = fShmNonRtServerControl.readUInt();
  1623. // name
  1624. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1625. char name[nameSize+1];
  1626. carla_zeroChars(name, nameSize+1);
  1627. fShmNonRtServerControl.readCustomData(name, nameSize);
  1628. CARLA_SAFE_ASSERT_INT2(index < pData->prog.count, index, pData->prog.count);
  1629. if (index < pData->prog.count)
  1630. {
  1631. if (pData->prog.names[index] != nullptr)
  1632. delete[] pData->prog.names[index];
  1633. pData->prog.names[index] = carla_strdup(name);
  1634. }
  1635. } break;
  1636. case kPluginBridgeNonRtServerMidiProgramData: {
  1637. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  1638. const uint32_t index = fShmNonRtServerControl.readUInt();
  1639. const uint32_t bank = fShmNonRtServerControl.readUInt();
  1640. const uint32_t program = fShmNonRtServerControl.readUInt();
  1641. // name
  1642. const uint32_t nameSize(fShmNonRtServerControl.readUInt());
  1643. char name[nameSize+1];
  1644. carla_zeroChars(name, nameSize+1);
  1645. fShmNonRtServerControl.readCustomData(name, nameSize);
  1646. CARLA_SAFE_ASSERT_INT2(index < pData->midiprog.count, index, pData->midiprog.count);
  1647. if (index < pData->midiprog.count)
  1648. {
  1649. if (pData->midiprog.data[index].name != nullptr)
  1650. delete[] pData->midiprog.data[index].name;
  1651. pData->midiprog.data[index].bank = bank;
  1652. pData->midiprog.data[index].program = program;
  1653. pData->midiprog.data[index].name = carla_strdup(name);
  1654. }
  1655. } break;
  1656. case kPluginBridgeNonRtServerSetCustomData: {
  1657. // uint/size, str[], uint/size, str[], uint/size, str[]
  1658. // type
  1659. const uint32_t typeSize(fShmNonRtServerControl.readUInt());
  1660. char type[typeSize+1];
  1661. carla_zeroChars(type, typeSize+1);
  1662. fShmNonRtServerControl.readCustomData(type, typeSize);
  1663. // key
  1664. const uint32_t keySize(fShmNonRtServerControl.readUInt());
  1665. char key[keySize+1];
  1666. carla_zeroChars(key, keySize+1);
  1667. fShmNonRtServerControl.readCustomData(key, keySize);
  1668. // value
  1669. const uint32_t valueSize(fShmNonRtServerControl.readUInt());
  1670. char value[valueSize+1];
  1671. carla_zeroChars(value, valueSize+1);
  1672. if (valueSize > 0)
  1673. fShmNonRtServerControl.readCustomData(value, valueSize);
  1674. CarlaPlugin::setCustomData(type, key, value, false);
  1675. } break;
  1676. case kPluginBridgeNonRtServerSetChunkDataFile: {
  1677. // uint/size, str[] (filename)
  1678. // chunkFilePath
  1679. const uint32_t chunkFilePathSize(fShmNonRtServerControl.readUInt());
  1680. char chunkFilePath[chunkFilePathSize+1];
  1681. carla_zeroChars(chunkFilePath, chunkFilePathSize+1);
  1682. fShmNonRtServerControl.readCustomData(chunkFilePath, chunkFilePathSize);
  1683. String realChunkFilePath(chunkFilePath);
  1684. #ifndef CARLA_OS_WIN
  1685. // Using Wine, fix temp dir
  1686. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1687. {
  1688. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1689. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1690. realChunkFilePath = fWinePrefix;
  1691. realChunkFilePath += "/drive_";
  1692. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1693. realChunkFilePath += driveLetterSplit[1];
  1694. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1695. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1696. }
  1697. #endif
  1698. File chunkFile(realChunkFilePath);
  1699. CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
  1700. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1701. chunkFile.deleteFile();
  1702. } break;
  1703. case kPluginBridgeNonRtServerSetLatency:
  1704. if (true)
  1705. {
  1706. // FIXME - latency adjust code on this file is broken
  1707. fShmNonRtServerControl.readUInt();
  1708. break;
  1709. }
  1710. // uint
  1711. fLatency = fShmNonRtServerControl.readUInt();
  1712. #ifndef BUILD_BRIDGE
  1713. if (! fInitiated)
  1714. pData->latency.recreateBuffers(std::max(fInfo.aIns, fInfo.aOuts), fLatency);
  1715. #endif
  1716. break;
  1717. case kPluginBridgeNonRtServerSetParameterText: {
  1718. const int32_t index = fShmNonRtServerControl.readInt();
  1719. const uint32_t textSize(fShmNonRtServerControl.readUInt());
  1720. char text[textSize+1];
  1721. carla_zeroChars(text, textSize+1);
  1722. fShmNonRtServerControl.readCustomData(text, textSize);
  1723. fReceivingParamText.setReceivedData(index, text, textSize);
  1724. } break;
  1725. case kPluginBridgeNonRtServerReady:
  1726. fInitiated = true;
  1727. break;
  1728. case kPluginBridgeNonRtServerSaved:
  1729. fSaved = true;
  1730. break;
  1731. case kPluginBridgeNonRtServerUiClosed:
  1732. #ifndef BUILD_BRIDGE
  1733. pData->transientTryCounter = 0;
  1734. #endif
  1735. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1736. break;
  1737. case kPluginBridgeNonRtServerError: {
  1738. // error
  1739. const uint32_t errorSize(fShmNonRtServerControl.readUInt());
  1740. char error[errorSize+1];
  1741. carla_zeroChars(error, errorSize+1);
  1742. fShmNonRtServerControl.readCustomData(error, errorSize);
  1743. if (fInitiated)
  1744. {
  1745. pData->engine->callback(ENGINE_CALLBACK_ERROR, pData->id, 0, 0, 0.0f, error);
  1746. // just in case
  1747. pData->engine->setLastError(error);
  1748. fInitError = true;
  1749. }
  1750. else
  1751. {
  1752. pData->engine->setLastError(error);
  1753. fInitError = true;
  1754. fInitiated = true;
  1755. }
  1756. } break;
  1757. }
  1758. }
  1759. }
  1760. // -------------------------------------------------------------------
  1761. uintptr_t getUiBridgeProcessId() const noexcept override
  1762. {
  1763. return fBridgeThread.getProcessPID();
  1764. }
  1765. const void* getExtraStuff() const noexcept override
  1766. {
  1767. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  1768. }
  1769. // -------------------------------------------------------------------
  1770. bool init(const char* const filename, const char* const name, const char* const label, const int64_t uniqueId, const char* const bridgeBinary)
  1771. {
  1772. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1773. // ---------------------------------------------------------------
  1774. // first checks
  1775. if (pData->client != nullptr)
  1776. {
  1777. pData->engine->setLastError("Plugin client is already registered");
  1778. return false;
  1779. }
  1780. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  1781. {
  1782. pData->engine->setLastError("null bridge binary");
  1783. return false;
  1784. }
  1785. // ---------------------------------------------------------------
  1786. // set info
  1787. if (name != nullptr && name[0] != '\0')
  1788. pData->name = pData->engine->getUniquePluginName(name);
  1789. if (filename != nullptr && filename[0] != '\0')
  1790. pData->filename = carla_strdup(filename);
  1791. else
  1792. pData->filename = carla_strdup("");
  1793. fUniqueId = uniqueId;
  1794. fBridgeBinary = bridgeBinary;
  1795. std::srand(static_cast<uint>(std::time(nullptr)));
  1796. // ---------------------------------------------------------------
  1797. // init sem/shm
  1798. if (! fShmAudioPool.initializeServer())
  1799. {
  1800. carla_stderr("Failed to initialize shared memory audio pool");
  1801. return false;
  1802. }
  1803. if (! fShmRtClientControl.initializeServer())
  1804. {
  1805. carla_stderr("Failed to initialize RT client control");
  1806. fShmAudioPool.clear();
  1807. return false;
  1808. }
  1809. if (! fShmNonRtClientControl.initializeServer())
  1810. {
  1811. carla_stderr("Failed to initialize Non-RT client control");
  1812. fShmRtClientControl.clear();
  1813. fShmAudioPool.clear();
  1814. return false;
  1815. }
  1816. if (! fShmNonRtServerControl.initializeServer())
  1817. {
  1818. carla_stderr("Failed to initialize Non-RT server control");
  1819. fShmNonRtClientControl.clear();
  1820. fShmRtClientControl.clear();
  1821. fShmAudioPool.clear();
  1822. return false;
  1823. }
  1824. // ---------------------------------------------------------------
  1825. // initial values
  1826. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientVersion);
  1827. fShmNonRtClientControl.writeUInt(CARLA_PLUGIN_BRIDGE_API_VERSION);
  1828. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeRtClientData)));
  1829. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtClientData)));
  1830. fShmNonRtClientControl.writeUInt(static_cast<uint32_t>(sizeof(BridgeNonRtServerData)));
  1831. fShmNonRtClientControl.writeOpcode(kPluginBridgeNonRtClientInitialSetup);
  1832. fShmNonRtClientControl.writeUInt(pData->engine->getBufferSize());
  1833. fShmNonRtClientControl.writeDouble(pData->engine->getSampleRate());
  1834. fShmNonRtClientControl.commitWrite();
  1835. // testing dummy message
  1836. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientNull);
  1837. fShmRtClientControl.commitWrite();
  1838. #ifndef CARLA_OS_WIN
  1839. // ---------------------------------------------------------------
  1840. // set wine prefix
  1841. if (fBridgeBinary.contains(".exe", true))
  1842. {
  1843. const EngineOptions& options(pData->engine->getOptions());
  1844. if (options.wine.autoPrefix)
  1845. fWinePrefix = findWinePrefix(pData->filename);
  1846. if (fWinePrefix.isEmpty())
  1847. {
  1848. const char* const envWinePrefix(std::getenv("WINEPREFIX"));
  1849. if (envWinePrefix != nullptr && envWinePrefix[0] != '\0')
  1850. fWinePrefix = envWinePrefix;
  1851. else if (options.wine.fallbackPrefix != nullptr && options.wine.fallbackPrefix[0] != '\0')
  1852. fWinePrefix = options.wine.fallbackPrefix;
  1853. else
  1854. fWinePrefix = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  1855. }
  1856. }
  1857. #endif
  1858. // ---------------------------------------------------------------
  1859. // init bridge thread
  1860. {
  1861. char shmIdsStr[6*4+1];
  1862. carla_zeroChars(shmIdsStr, 6*4+1);
  1863. std::strncpy(shmIdsStr+6*0, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1864. std::strncpy(shmIdsStr+6*1, &fShmRtClientControl.filename[fShmRtClientControl.filename.length()-6], 6);
  1865. std::strncpy(shmIdsStr+6*2, &fShmNonRtClientControl.filename[fShmNonRtClientControl.filename.length()-6], 6);
  1866. std::strncpy(shmIdsStr+6*3, &fShmNonRtServerControl.filename[fShmNonRtServerControl.filename.length()-6], 6);
  1867. fBridgeThread.setData(
  1868. #ifndef CARLA_OS_WIN
  1869. fWinePrefix.toRawUTF8(),
  1870. #endif
  1871. bridgeBinary, label, shmIdsStr);
  1872. fBridgeThread.startThread();
  1873. }
  1874. // ---------------------------------------------------------------
  1875. // wait for bridge to start
  1876. fInitiated = false;
  1877. fLastPongTime = Time::currentTimeMillis();
  1878. CARLA_SAFE_ASSERT(fLastPongTime > 0);
  1879. static bool sFirstInit = true;
  1880. int64_t timeoutEnd = 5000;
  1881. if (sFirstInit)
  1882. timeoutEnd *= 2;
  1883. #ifndef CARLA_OS_WIN
  1884. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1885. timeoutEnd *= 2;
  1886. #endif
  1887. sFirstInit = false;
  1888. const bool needsEngineIdle = pData->engine->getType() != kEngineTypePlugin;
  1889. for (; Time::currentTimeMillis() < fLastPongTime + timeoutEnd && fBridgeThread.isThreadRunning();)
  1890. {
  1891. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1892. if (needsEngineIdle)
  1893. pData->engine->idle();
  1894. idle();
  1895. if (fInitiated)
  1896. break;
  1897. if (pData->engine->isAboutToClose())
  1898. break;
  1899. carla_msleep(20);
  1900. }
  1901. fLastPongTime = -1;
  1902. if (fInitError || ! fInitiated)
  1903. {
  1904. fBridgeThread.stopThread(6000);
  1905. if (! fInitError)
  1906. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1907. return false;
  1908. }
  1909. // ---------------------------------------------------------------
  1910. // register client
  1911. if (pData->name == nullptr)
  1912. {
  1913. if (label != nullptr && label[0] != '\0')
  1914. pData->name = pData->engine->getUniquePluginName(label);
  1915. else
  1916. pData->name = pData->engine->getUniquePluginName("unknown");
  1917. }
  1918. pData->client = pData->engine->addClient(this);
  1919. if (pData->client == nullptr || ! pData->client->isOk())
  1920. {
  1921. pData->engine->setLastError("Failed to register plugin client");
  1922. return false;
  1923. }
  1924. return true;
  1925. }
  1926. private:
  1927. const BinaryType fBinaryType;
  1928. const PluginType fPluginType;
  1929. bool fInitiated;
  1930. bool fInitError;
  1931. bool fSaved;
  1932. bool fTimedOut;
  1933. bool fTimedError;
  1934. uint fProcWaitTime;
  1935. int64_t fLastPongTime;
  1936. CarlaString fBridgeBinary;
  1937. CarlaPluginBridgeThread fBridgeThread;
  1938. BridgeAudioPool fShmAudioPool;
  1939. BridgeRtClientControl fShmRtClientControl;
  1940. BridgeNonRtClientControl fShmNonRtClientControl;
  1941. BridgeNonRtServerControl fShmNonRtServerControl;
  1942. #ifndef CARLA_OS_WIN
  1943. String fWinePrefix;
  1944. #endif
  1945. class ReceivingParamText {
  1946. public:
  1947. ReceivingParamText() noexcept
  1948. : dataRecv(false),
  1949. dataOk(false),
  1950. index(-1),
  1951. strBuf(nullptr),
  1952. mutex() {}
  1953. bool isCurrentlyWaitingData() const noexcept
  1954. {
  1955. return index >= 0;
  1956. }
  1957. bool wasDataReceived(bool* const success) const noexcept
  1958. {
  1959. *success = dataOk;
  1960. return dataRecv;
  1961. }
  1962. void setTargetData(const int32_t i, char* const b) noexcept
  1963. {
  1964. const CarlaMutexLocker cml(mutex);
  1965. dataOk = false;
  1966. dataRecv = false;
  1967. index = i;
  1968. strBuf = b;
  1969. }
  1970. void setReceivedData(const int32_t i, const char* const b, const uint blen) noexcept
  1971. {
  1972. ScopedValueSetter<bool> svs(dataRecv, false, true);
  1973. const CarlaMutexLocker cml(mutex);
  1974. // make backup and reset data
  1975. const int32_t indexCopy = index;
  1976. char* const strBufCopy = strBuf;
  1977. index = -1;
  1978. strBuf = nullptr;
  1979. CARLA_SAFE_ASSERT_RETURN(indexCopy == i,);
  1980. CARLA_SAFE_ASSERT_RETURN(strBufCopy != nullptr,);
  1981. std::strncpy(strBufCopy, b, std::min(blen, STR_MAX-1U));
  1982. dataOk = true;
  1983. }
  1984. private:
  1985. bool dataRecv;
  1986. bool dataOk;
  1987. int32_t index;
  1988. char* strBuf;
  1989. CarlaMutex mutex;
  1990. CARLA_DECLARE_NON_COPY_CLASS(ReceivingParamText)
  1991. } fReceivingParamText;
  1992. struct Info {
  1993. uint32_t aIns, aOuts;
  1994. uint32_t cvIns, cvOuts;
  1995. uint32_t mIns, mOuts;
  1996. PluginCategory category;
  1997. uint optionsAvailable;
  1998. CarlaString name;
  1999. CarlaString label;
  2000. CarlaString maker;
  2001. CarlaString copyright;
  2002. const char** aInNames;
  2003. const char** aOutNames;
  2004. std::vector<uint8_t> chunk;
  2005. Info()
  2006. : aIns(0),
  2007. aOuts(0),
  2008. cvIns(0),
  2009. cvOuts(0),
  2010. mIns(0),
  2011. mOuts(0),
  2012. category(PLUGIN_CATEGORY_NONE),
  2013. optionsAvailable(0),
  2014. name(),
  2015. label(),
  2016. maker(),
  2017. copyright(),
  2018. aInNames(nullptr),
  2019. aOutNames(nullptr),
  2020. chunk() {}
  2021. CARLA_DECLARE_NON_COPY_STRUCT(Info)
  2022. } fInfo;
  2023. int64_t fUniqueId;
  2024. uint32_t fLatency;
  2025. BridgeParamInfo* fParams;
  2026. void resizeAudioPool(const uint32_t bufferSize)
  2027. {
  2028. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts, fInfo.cvIns+fInfo.cvOuts);
  2029. fShmRtClientControl.writeOpcode(kPluginBridgeRtClientSetAudioPool);
  2030. fShmRtClientControl.writeULong(static_cast<uint64_t>(fShmAudioPool.dataSize));
  2031. fShmRtClientControl.commitWrite();
  2032. waitForClient("resize-pool", 5000);
  2033. }
  2034. void waitForClient(const char* const action, const uint msecs)
  2035. {
  2036. CARLA_SAFE_ASSERT_RETURN(! fTimedOut,);
  2037. CARLA_SAFE_ASSERT_RETURN(! fTimedError,);
  2038. if (fShmRtClientControl.waitForClient(msecs))
  2039. return;
  2040. fTimedOut = true;
  2041. carla_stderr2("waitForClient(%s) timed out", action);
  2042. }
  2043. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginBridge)
  2044. };
  2045. CARLA_BACKEND_END_NAMESPACE
  2046. // ---------------------------------------------------------------------------------------------------------------------
  2047. CARLA_BACKEND_START_NAMESPACE
  2048. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* bridgeBinary)
  2049. {
  2050. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  2051. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  2052. {
  2053. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  2054. return nullptr;
  2055. }
  2056. #ifndef CARLA_OS_WIN
  2057. // FIXME: somewhere, somehow, we end up with double slashes, wine doesn't like that.
  2058. if (std::strncmp(bridgeBinary, "//", 2) == 0)
  2059. ++bridgeBinary;
  2060. #endif
  2061. CarlaPluginBridge* const plugin(new CarlaPluginBridge(init.engine, init.id, btype, ptype));
  2062. if (! plugin->init(init.filename, init.name, init.label, init.uniqueId, bridgeBinary))
  2063. {
  2064. delete plugin;
  2065. return nullptr;
  2066. }
  2067. return plugin;
  2068. }
  2069. CARLA_BACKEND_END_NAMESPACE
  2070. // ---------------------------------------------------------------------------------------------------------------------
  2071. #ifndef BUILD_BRIDGE
  2072. # include "CarlaBridgeUtils.cpp"
  2073. #endif
  2074. // ---------------------------------------------------------------------------------------------------------------------