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.

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