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.

2707 lines
91KB

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