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.

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