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.

2618 lines
92KB

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