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.

2621 lines
88KB

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