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.

2632 lines
92KB

  1. /*
  2. * Carla DSSI Plugin
  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. // TODO: set fUsesCustomData and latency index before init finishes
  18. // TODO: common laterncy code
  19. #include "CarlaPluginInternal.hpp"
  20. #include "CarlaEngine.hpp"
  21. #include "CarlaDssiUtils.hpp"
  22. #include "CarlaMathUtils.hpp"
  23. #include "CarlaThread.hpp"
  24. using juce::ChildProcess;
  25. using juce::ScopedPointer;
  26. using juce::String;
  27. using juce::StringArray;
  28. CARLA_BACKEND_START_NAMESPACE
  29. class CarlaPluginDSSI;
  30. // -------------------------------------------------------------------
  31. // Fallback data
  32. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  33. // -------------------------------------------------------------------
  34. class CarlaThreadDSSIUI : public CarlaThread
  35. {
  36. public:
  37. CarlaThreadDSSIUI(CarlaEngine* const engine, CarlaPlugin* const plugin, const CarlaOscData& oscData) noexcept
  38. : CarlaThread("CarlaThreadDSSIUI"),
  39. kEngine(engine),
  40. kPlugin(plugin),
  41. fBinary(),
  42. fLabel(),
  43. fOscData(oscData),
  44. fProcess(),
  45. leakDetector_CarlaThreadDSSIUI() {}
  46. void setData(const char* const binary, const char* const label) noexcept
  47. {
  48. CARLA_SAFE_ASSERT_RETURN(binary != nullptr && binary[0] != '\0',);
  49. CARLA_SAFE_ASSERT_RETURN(label != nullptr /*&& label[0] != '\0'*/,);
  50. CARLA_SAFE_ASSERT(! isThreadRunning());
  51. fBinary = binary;
  52. fLabel = label;
  53. if (fLabel.isEmpty())
  54. fLabel = "\"\"";
  55. }
  56. uintptr_t getProcessPID() const noexcept
  57. {
  58. CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
  59. return (uintptr_t)fProcess->getPID();
  60. }
  61. void run()
  62. {
  63. if (fProcess == nullptr)
  64. {
  65. fProcess = new ChildProcess();
  66. }
  67. else if (fProcess->isRunning())
  68. {
  69. carla_stderr("CarlaThreadDSSI::run() - already running, giving up...");
  70. kEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, kPlugin->getId(), 0, 0, 0.0f, nullptr);
  71. fProcess->kill();
  72. fProcess = nullptr;
  73. return;
  74. }
  75. #if 0 //def CARLA_OS_LINUX
  76. const char* const oldPreload(std::getenv("LD_PRELOAD"));
  77. ::unsetenv("LD_PRELOAD");
  78. if (oldPreload != nullptr)
  79. ::setenv("LD_PRELOAD", oldPreload, 1);
  80. #endif
  81. String name(kPlugin->getName());
  82. String filename(kPlugin->getFilename());
  83. if (name.isEmpty())
  84. name = "(none)";
  85. if (filename.isEmpty())
  86. filename = "\"\"";
  87. StringArray arguments;
  88. #ifndef CARLA_OS_WIN
  89. // start with "wine" if needed
  90. if (fBinary.endsWith(".exe"))
  91. arguments.add("wine");
  92. #endif
  93. // binary
  94. arguments.add(fBinary.buffer());
  95. // osc-url
  96. arguments.add(String(kEngine->getOscServerPathUDP()) + String("/") + String(kPlugin->getId()));
  97. // filename
  98. arguments.add(filename);
  99. // label
  100. arguments.add(fLabel.buffer());
  101. // ui-title
  102. arguments.add(name + String(" (GUI)"));
  103. carla_stdout("starting DSSI UI...");
  104. if (! fProcess->start(arguments))
  105. {
  106. carla_stdout("failed!");
  107. fProcess = nullptr;
  108. return;
  109. }
  110. if (waitForOscGuiShow())
  111. {
  112. for (; fProcess->isRunning() && ! shouldThreadExit();)
  113. carla_sleep(1);
  114. // we only get here if UI was closed or thread asked to exit
  115. if (fProcess->isRunning() && shouldThreadExit())
  116. {
  117. fProcess->waitForProcessToFinish(static_cast<int>(kEngine->getOptions().uiBridgesTimeout));
  118. if (fProcess->isRunning())
  119. {
  120. carla_stdout("CarlaThreadDSSIUI::run() - UI refused to close, force kill now");
  121. fProcess->kill();
  122. }
  123. else
  124. {
  125. carla_stdout("CarlaThreadDSSIUI::run() - UI auto-closed successfully");
  126. }
  127. }
  128. else if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  129. carla_stderr("CarlaThreadDSSIUI::run() - UI crashed while running");
  130. else
  131. carla_stdout("CarlaThreadDSSIUI::run() - UI closed cleanly");
  132. kEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, kPlugin->getId(), 0, 0, 0.0f, nullptr);
  133. }
  134. else
  135. {
  136. fProcess->kill();
  137. carla_stdout("CarlaThreadDSSIUI::run() - GUI timeout");
  138. kEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, kPlugin->getId(), 0, 0, 0.0f, nullptr);
  139. }
  140. carla_stdout("DSSI UI finished");
  141. fProcess = nullptr;
  142. }
  143. private:
  144. CarlaEngine* const kEngine;
  145. CarlaPlugin* const kPlugin;
  146. CarlaString fBinary;
  147. CarlaString fLabel;
  148. const CarlaOscData& fOscData;
  149. ScopedPointer<ChildProcess> fProcess;
  150. bool waitForOscGuiShow()
  151. {
  152. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow()");
  153. uint i=0, oscUiTimeout = kEngine->getOptions().uiBridgesTimeout;
  154. // wait for UI 'update' call
  155. for (; i < oscUiTimeout/100; ++i)
  156. {
  157. if (fOscData.target != nullptr)
  158. {
  159. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow() - got response, asking UI to show itself now");
  160. osc_send_show(fOscData);
  161. return true;
  162. }
  163. if (fProcess != nullptr && fProcess->isRunning())
  164. carla_msleep(100);
  165. else
  166. return false;
  167. }
  168. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  169. return false;
  170. }
  171. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThreadDSSIUI)
  172. };
  173. // -----------------------------------------------------
  174. class CarlaPluginDSSI : public CarlaPlugin
  175. {
  176. public:
  177. CarlaPluginDSSI(CarlaEngine* const engine, const uint id) noexcept
  178. : CarlaPlugin(engine, id),
  179. fHandle(nullptr),
  180. fHandle2(nullptr),
  181. fDescriptor(nullptr),
  182. fDssiDescriptor(nullptr),
  183. fUsesCustomData(false),
  184. fUiFilename(nullptr),
  185. fAudioInBuffers(nullptr),
  186. fAudioOutBuffers(nullptr),
  187. fParamBuffers(nullptr),
  188. fLatencyChanged(false),
  189. fLatencyIndex(-1),
  190. fOscData(),
  191. fThreadUI(engine, this, fOscData),
  192. leakDetector_CarlaPluginDSSI()
  193. {
  194. carla_debug("CarlaPluginDSSI::CarlaPluginDSSI(%p, %i)", engine, id);
  195. }
  196. ~CarlaPluginDSSI() noexcept override
  197. {
  198. carla_debug("CarlaPluginDSSI::~CarlaPluginDSSI()");
  199. // close UI
  200. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  201. {
  202. showCustomUI(false);
  203. fThreadUI.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  204. }
  205. pData->singleMutex.lock();
  206. pData->masterMutex.lock();
  207. if (pData->client != nullptr && pData->client->isActive())
  208. pData->client->deactivate();
  209. if (pData->active)
  210. {
  211. deactivate();
  212. pData->active = false;
  213. }
  214. if (fDescriptor != nullptr)
  215. {
  216. if (pData->name != nullptr && fDssiDescriptor != nullptr && fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  217. removeUniqueMultiSynth(fDescriptor->Label);
  218. if (fDescriptor->cleanup != nullptr)
  219. {
  220. if (fHandle != nullptr)
  221. {
  222. try {
  223. fDescriptor->cleanup(fHandle);
  224. } CARLA_SAFE_EXCEPTION("DSSI cleanup");
  225. }
  226. if (fHandle2 != nullptr)
  227. {
  228. try {
  229. fDescriptor->cleanup(fHandle2);
  230. } CARLA_SAFE_EXCEPTION("DSSI cleanup #2");
  231. }
  232. }
  233. fHandle = nullptr;
  234. fHandle2 = nullptr;
  235. fDescriptor = nullptr;
  236. fDssiDescriptor = nullptr;
  237. }
  238. if (fUiFilename != nullptr)
  239. {
  240. delete[] fUiFilename;
  241. fUiFilename = nullptr;
  242. }
  243. clearBuffers();
  244. }
  245. // -------------------------------------------------------------------
  246. // Information (base)
  247. PluginType getType() const noexcept override
  248. {
  249. return PLUGIN_DSSI;
  250. }
  251. PluginCategory getCategory() const noexcept override
  252. {
  253. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  254. if (pData->audioIn.count == 0 && pData->audioOut.count > 0 && (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr))
  255. return PLUGIN_CATEGORY_SYNTH;
  256. return CarlaPlugin::getCategory();
  257. }
  258. int64_t getUniqueId() const noexcept override
  259. {
  260. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  261. return static_cast<int64_t>(fDescriptor->UniqueID);
  262. }
  263. // -------------------------------------------------------------------
  264. // Information (count)
  265. // nothing
  266. // -------------------------------------------------------------------
  267. // Information (current data)
  268. std::size_t getChunkData(void** const dataPtr) noexcept override
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData, 0);
  271. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  272. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0);
  273. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->get_custom_data != nullptr, 0);
  274. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  275. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr, 0);
  276. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  277. *dataPtr = nullptr;
  278. int ret = 0;
  279. ulong dataSize = 0;
  280. try {
  281. ret = fDssiDescriptor->get_custom_data(fHandle, dataPtr, &dataSize);
  282. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPluginDSSI::getChunkData", 0);
  283. return (ret != 0) ? dataSize : 0;
  284. }
  285. // -------------------------------------------------------------------
  286. // Information (per-plugin data)
  287. uint getOptionsAvailable() const noexcept override
  288. {
  289. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0x0);
  290. #ifdef __USE_GNU
  291. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  292. #else
  293. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  294. #endif
  295. uint options = 0x0;
  296. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  297. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  298. if (! isDssiVst)
  299. {
  300. if (fLatencyIndex == -1)
  301. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  302. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  303. {
  304. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  305. options |= PLUGIN_OPTION_FORCE_STEREO;
  306. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  307. options |= PLUGIN_OPTION_FORCE_STEREO;
  308. }
  309. }
  310. if (fUsesCustomData)
  311. options |= PLUGIN_OPTION_USE_CHUNKS;
  312. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  313. {
  314. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  315. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  316. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  317. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  318. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  319. }
  320. return options;
  321. }
  322. float getParameterValue(const uint32_t parameterId) const noexcept override
  323. {
  324. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  325. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  326. return fParamBuffers[parameterId];
  327. }
  328. void getLabel(char* const strBuf) const noexcept override
  329. {
  330. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  331. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Label != nullptr, nullStrBuf(strBuf));
  332. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  333. }
  334. void getMaker(char* const strBuf) const noexcept override
  335. {
  336. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  337. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Maker != nullptr, nullStrBuf(strBuf));
  338. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  339. }
  340. void getCopyright(char* const strBuf) const noexcept override
  341. {
  342. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  343. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Copyright != nullptr, nullStrBuf(strBuf));
  344. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  345. }
  346. void getRealName(char* const strBuf) const noexcept override
  347. {
  348. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  349. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Name != nullptr, nullStrBuf(strBuf));
  350. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  351. }
  352. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  353. {
  354. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  355. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  356. const int32_t rindex(pData->param.data[parameterId].rindex);
  357. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  358. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames[rindex] != nullptr, nullStrBuf(strBuf));
  359. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, true))
  360. return;
  361. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  362. }
  363. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  364. {
  365. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  366. const int32_t rindex(pData->param.data[parameterId].rindex);
  367. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  368. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, false))
  369. return;
  370. nullStrBuf(strBuf);
  371. }
  372. // -------------------------------------------------------------------
  373. // Set data (state)
  374. // nothing
  375. // -------------------------------------------------------------------
  376. // Set data (internal stuff)
  377. void setId(const uint newId) noexcept
  378. {
  379. CarlaPlugin::setId(newId);
  380. // UI osc-url uses Id, so we need to close it when it changes
  381. showCustomUI(false);
  382. }
  383. // -------------------------------------------------------------------
  384. // Set data (plugin-specific stuff)
  385. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  389. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  390. fParamBuffers[parameterId] = fixedValue;
  391. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  392. }
  393. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  394. {
  395. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  396. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  397. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  398. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  399. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  400. carla_debug("CarlaPluginDSSI::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  401. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  402. return carla_stderr2("CarlaPluginDSSI::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  403. if (fDssiDescriptor->configure != nullptr)
  404. {
  405. try {
  406. fDssiDescriptor->configure(fHandle, key, value);
  407. } catch(...) {}
  408. if (fHandle2 != nullptr)
  409. {
  410. try {
  411. fDssiDescriptor->configure(fHandle2, key, value);
  412. } catch(...) {}
  413. }
  414. }
  415. if (sendGui && fOscData.target != nullptr)
  416. osc_send_configure(fOscData, key, value);
  417. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  418. {
  419. const ScopedSingleProcessLocker spl(this, true);
  420. reloadPrograms(false);
  421. }
  422. CarlaPlugin::setCustomData(type, key, value, sendGui);
  423. }
  424. void setChunkData(const void* const data, const std::size_t dataSize) override
  425. {
  426. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData,);
  427. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  428. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  429. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->set_custom_data != nullptr,);
  430. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  431. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr,);
  432. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  433. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  434. {
  435. const ScopedSingleProcessLocker spl(this, true);
  436. try {
  437. fDssiDescriptor->set_custom_data(fHandle, const_cast<void*>(data), static_cast<ulong>(dataSize));
  438. } CARLA_SAFE_EXCEPTION("CarlaPluginDSSI::setChunkData");
  439. }
  440. #ifdef BUILD_BRIDGE
  441. const bool sendOsc(false);
  442. #else
  443. const bool sendOsc(pData->engine->isOscControlRegistered());
  444. #endif
  445. pData->updateParameterValues(this, sendOsc, true, false);
  446. }
  447. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  448. {
  449. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  450. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  451. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  452. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  453. if (index >= 0)
  454. {
  455. const uint32_t bank(pData->midiprog.data[index].bank);
  456. const uint32_t program(pData->midiprog.data[index].program);
  457. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  458. try {
  459. fDssiDescriptor->select_program(fHandle, bank, program);
  460. } catch(...) {}
  461. if (fHandle2 != nullptr)
  462. {
  463. try {
  464. fDssiDescriptor->select_program(fHandle2, bank, program);
  465. } catch(...) {}
  466. }
  467. }
  468. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  469. }
  470. // -------------------------------------------------------------------
  471. // Set ui stuff
  472. void showCustomUI(const bool yesNo) override
  473. {
  474. if (yesNo)
  475. {
  476. fOscData.clear();
  477. fThreadUI.startThread();
  478. }
  479. else
  480. {
  481. pData->transientTryCounter = 0;
  482. if (fOscData.target != nullptr)
  483. {
  484. osc_send_hide(fOscData);
  485. osc_send_quit(fOscData);
  486. fOscData.clear();
  487. }
  488. fThreadUI.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  489. }
  490. }
  491. void idle() override
  492. {
  493. if (fLatencyChanged && fLatencyIndex != -1)
  494. {
  495. fLatencyChanged = false;
  496. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  497. if (latency >= 0)
  498. {
  499. const uint32_t ulatency(static_cast<uint32_t>(latency));
  500. if (pData->latency != ulatency)
  501. {
  502. carla_stdout("latency changed to %i", latency);
  503. const ScopedSingleProcessLocker sspl(this, true);
  504. pData->latency = ulatency;
  505. pData->client->setLatency(ulatency);
  506. #ifndef BUILD_BRIDGE
  507. pData->recreateLatencyBuffers();
  508. #endif
  509. }
  510. }
  511. else
  512. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  513. }
  514. CarlaPlugin::idle();
  515. }
  516. // -------------------------------------------------------------------
  517. // Plugin state
  518. void reload() override
  519. {
  520. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  521. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  522. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  523. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  524. carla_debug("CarlaPluginDSSI::reload() - start");
  525. const EngineProcessMode processMode(pData->engine->getProccessMode());
  526. // Safely disable plugin for reload
  527. const ScopedDisabler sd(this);
  528. if (pData->active)
  529. deactivate();
  530. clearBuffers();
  531. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  532. const uint32_t portCount(getSafePortCount());
  533. uint32_t aIns, aOuts, mIns, params;
  534. aIns = aOuts = mIns = params = 0;
  535. bool forcedStereoIn, forcedStereoOut;
  536. forcedStereoIn = forcedStereoOut = false;
  537. bool needsCtrlIn, needsCtrlOut;
  538. needsCtrlIn = needsCtrlOut = false;
  539. for (uint32_t i=0; i < portCount; ++i)
  540. {
  541. const LADSPA_PortDescriptor portType(fDescriptor->PortDescriptors[i]);
  542. if (LADSPA_IS_PORT_AUDIO(portType))
  543. {
  544. if (LADSPA_IS_PORT_INPUT(portType))
  545. aIns += 1;
  546. else if (LADSPA_IS_PORT_OUTPUT(portType))
  547. aOuts += 1;
  548. }
  549. else if (LADSPA_IS_PORT_CONTROL(portType))
  550. params += 1;
  551. }
  552. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  553. {
  554. if (fHandle2 == nullptr)
  555. {
  556. try {
  557. fHandle2 = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(sampleRate));
  558. } CARLA_SAFE_EXCEPTION("DSSI instantiate #2");
  559. }
  560. if (fHandle2 != nullptr)
  561. {
  562. if (aIns == 1)
  563. {
  564. aIns = 2;
  565. forcedStereoIn = true;
  566. }
  567. if (aOuts == 1)
  568. {
  569. aOuts = 2;
  570. forcedStereoOut = true;
  571. }
  572. }
  573. }
  574. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  575. {
  576. mIns = 1;
  577. needsCtrlIn = true;
  578. }
  579. if (aIns > 0)
  580. {
  581. pData->audioIn.createNew(aIns);
  582. fAudioInBuffers = new float*[aIns];
  583. for (uint32_t i=0; i < aIns; ++i)
  584. fAudioInBuffers[i] = nullptr;
  585. }
  586. if (aOuts > 0)
  587. {
  588. pData->audioOut.createNew(aOuts);
  589. fAudioOutBuffers = new float*[aOuts];
  590. needsCtrlIn = true;
  591. for (uint32_t i=0; i < aOuts; ++i)
  592. fAudioOutBuffers[i] = nullptr;
  593. }
  594. if (params > 0)
  595. {
  596. pData->param.createNew(params, true);
  597. fParamBuffers = new float[params];
  598. FloatVectorOperations::clear(fParamBuffers, static_cast<int>(params));
  599. }
  600. const uint portNameSize(pData->engine->getMaxPortNameSize());
  601. CarlaString portName;
  602. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  603. {
  604. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  605. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  606. if (LADSPA_IS_PORT_AUDIO(portType))
  607. {
  608. portName.clear();
  609. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  610. {
  611. portName = pData->name;
  612. portName += ":";
  613. }
  614. if (fDescriptor->PortNames[i] != nullptr && fDescriptor->PortNames[i][0] != '\0')
  615. {
  616. portName += fDescriptor->PortNames[i];
  617. }
  618. else
  619. {
  620. if (LADSPA_IS_PORT_INPUT(portType))
  621. {
  622. if (aIns > 1)
  623. {
  624. portName += "audio-in_";
  625. portName += CarlaString(iAudioIn+1);
  626. }
  627. else
  628. portName += "audio-in";
  629. }
  630. else
  631. {
  632. if (aOuts > 1)
  633. {
  634. portName += "audio-out_";
  635. portName += CarlaString(iAudioOut+1);
  636. }
  637. else
  638. portName += "audio-out";
  639. }
  640. }
  641. portName.truncate(portNameSize);
  642. if (LADSPA_IS_PORT_INPUT(portType))
  643. {
  644. const uint32_t j = iAudioIn++;
  645. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  646. pData->audioIn.ports[j].rindex = i;
  647. if (forcedStereoIn)
  648. {
  649. portName += "_2";
  650. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  651. pData->audioIn.ports[1].rindex = i;
  652. }
  653. }
  654. else if (LADSPA_IS_PORT_OUTPUT(portType))
  655. {
  656. const uint32_t j = iAudioOut++;
  657. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  658. pData->audioOut.ports[j].rindex = i;
  659. if (forcedStereoOut)
  660. {
  661. portName += "_2";
  662. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  663. pData->audioOut.ports[1].rindex = i;
  664. }
  665. }
  666. else
  667. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  668. }
  669. else if (LADSPA_IS_PORT_CONTROL(portType))
  670. {
  671. const uint32_t j = iCtrl++;
  672. pData->param.data[j].index = static_cast<int32_t>(j);
  673. pData->param.data[j].rindex = static_cast<int32_t>(i);
  674. const char* const paramName(fDescriptor->PortNames[i] != nullptr ? fDescriptor->PortNames[i] : "unknown");
  675. float min, max, def, step, stepSmall, stepLarge;
  676. // min value
  677. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  678. min = portRangeHints.LowerBound;
  679. else
  680. min = 0.0f;
  681. // max value
  682. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  683. max = portRangeHints.UpperBound;
  684. else
  685. max = 1.0f;
  686. if (min > max)
  687. {
  688. carla_stderr2("WARNING - Broken plugin parameter '%s': min > max", paramName);
  689. min = max - 0.1f;
  690. }
  691. else if (carla_compareFloats(min, max))
  692. {
  693. carla_stderr2("WARNING - Broken plugin parameter '%s': min == max", paramName);
  694. max = min + 0.1f;
  695. }
  696. // default value
  697. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  698. if (def < min)
  699. def = min;
  700. else if (def > max)
  701. def = max;
  702. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  703. {
  704. min *= sampleRate;
  705. max *= sampleRate;
  706. def *= sampleRate;
  707. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  708. }
  709. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  710. {
  711. step = max - min;
  712. stepSmall = step;
  713. stepLarge = step;
  714. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  715. }
  716. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  717. {
  718. step = 1.0f;
  719. stepSmall = 1.0f;
  720. stepLarge = 10.0f;
  721. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  722. }
  723. else
  724. {
  725. const float range = max - min;
  726. step = range/100.0f;
  727. stepSmall = range/1000.0f;
  728. stepLarge = range/10.0f;
  729. }
  730. if (LADSPA_IS_PORT_INPUT(portType))
  731. {
  732. pData->param.data[j].type = PARAMETER_INPUT;
  733. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  734. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  735. needsCtrlIn = true;
  736. // MIDI CC value
  737. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  738. {
  739. int controller = fDssiDescriptor->get_midi_controller_for_port(fHandle, i);
  740. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  741. {
  742. int16_t cc = DSSI_CC_NUMBER(controller);
  743. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  744. pData->param.data[j].midiCC = cc;
  745. }
  746. }
  747. }
  748. else if (LADSPA_IS_PORT_OUTPUT(portType))
  749. {
  750. pData->param.data[j].type = PARAMETER_OUTPUT;
  751. if (std::strcmp(paramName, "latency") == 0 || std::strcmp(paramName, "_latency") == 0)
  752. {
  753. min = 0.0f;
  754. max = sampleRate;
  755. def = 0.0f;
  756. step = 1.0f;
  757. stepSmall = 1.0f;
  758. stepLarge = 1.0f;
  759. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  760. CARLA_SAFE_ASSERT(fLatencyIndex == -1);
  761. fLatencyIndex = static_cast<int32_t>(j);
  762. }
  763. else
  764. {
  765. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  766. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  767. needsCtrlOut = true;
  768. }
  769. }
  770. else
  771. {
  772. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  773. }
  774. // extra parameter hints
  775. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  776. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  777. pData->param.ranges[j].min = min;
  778. pData->param.ranges[j].max = max;
  779. pData->param.ranges[j].def = def;
  780. pData->param.ranges[j].step = step;
  781. pData->param.ranges[j].stepSmall = stepSmall;
  782. pData->param.ranges[j].stepLarge = stepLarge;
  783. // Start parameters in their default values
  784. fParamBuffers[j] = def;
  785. try {
  786. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  787. } CARLA_SAFE_EXCEPTION("DSSI connect_port parameter");
  788. if (fHandle2 != nullptr)
  789. {
  790. try {
  791. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  792. } CARLA_SAFE_EXCEPTION("DSSI connect_port parameter #2");
  793. }
  794. }
  795. else
  796. {
  797. // Not Audio or Control
  798. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  799. try {
  800. fDescriptor->connect_port(fHandle, i, nullptr);
  801. } CARLA_SAFE_EXCEPTION("DSSI connect_port null");
  802. if (fHandle2 != nullptr)
  803. {
  804. try {
  805. fDescriptor->connect_port(fHandle2, i, nullptr);
  806. } CARLA_SAFE_EXCEPTION("DSSI connect_port null #2");
  807. }
  808. }
  809. }
  810. if (needsCtrlIn)
  811. {
  812. portName.clear();
  813. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  814. {
  815. portName = pData->name;
  816. portName += ":";
  817. }
  818. portName += "events-in";
  819. portName.truncate(portNameSize);
  820. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  821. }
  822. if (needsCtrlOut)
  823. {
  824. portName.clear();
  825. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  826. {
  827. portName = pData->name;
  828. portName += ":";
  829. }
  830. portName += "events-out";
  831. portName.truncate(portNameSize);
  832. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  833. }
  834. if (forcedStereoIn || forcedStereoOut)
  835. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  836. else
  837. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  838. // plugin hints
  839. pData->hints = 0x0;
  840. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  841. pData->hints |= PLUGIN_IS_RTSAFE;
  842. if (fUiFilename != nullptr)
  843. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  844. #ifndef BUILD_BRIDGE
  845. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  846. pData->hints |= PLUGIN_CAN_DRYWET;
  847. if (aOuts > 0)
  848. pData->hints |= PLUGIN_CAN_VOLUME;
  849. if (aOuts >= 2 && aOuts % 2 == 0)
  850. pData->hints |= PLUGIN_CAN_BALANCE;
  851. #endif
  852. // extra plugin hints
  853. pData->extraHints = 0x0;
  854. if (mIns > 0)
  855. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  856. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  857. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  858. // check latency
  859. if (fLatencyIndex >= 0)
  860. {
  861. // we need to pre-run the plugin so it can update its latency control-port
  862. float tmpIn [(aIns > 0) ? aIns : 1][2];
  863. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  864. for (uint32_t j=0; j < aIns; ++j)
  865. {
  866. tmpIn[j][0] = 0.0f;
  867. tmpIn[j][1] = 0.0f;
  868. try {
  869. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  870. } CARLA_SAFE_EXCEPTION("DSSI connect_port latency input");
  871. }
  872. for (uint32_t j=0; j < aOuts; ++j)
  873. {
  874. tmpOut[j][0] = 0.0f;
  875. tmpOut[j][1] = 0.0f;
  876. try {
  877. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  878. } CARLA_SAFE_EXCEPTION("DSSI connect_port latency output");
  879. }
  880. if (fDescriptor->activate != nullptr)
  881. {
  882. try {
  883. fDescriptor->activate(fHandle);
  884. } CARLA_SAFE_EXCEPTION("DSSI latency activate");
  885. }
  886. try {
  887. fDescriptor->run(fHandle, 2);
  888. } CARLA_SAFE_EXCEPTION("DSSI latency run");
  889. if (fDescriptor->deactivate != nullptr)
  890. {
  891. try {
  892. fDescriptor->deactivate(fHandle);
  893. } CARLA_SAFE_EXCEPTION("DSSI latency deactivate");
  894. }
  895. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  896. if (latency >= 0)
  897. {
  898. const uint32_t ulatency(static_cast<uint32_t>(latency));
  899. if (pData->latency != ulatency)
  900. {
  901. carla_stdout("latency = %i", latency);
  902. pData->latency = ulatency;
  903. pData->client->setLatency(ulatency);
  904. #ifndef BUILD_BRIDGE
  905. pData->recreateLatencyBuffers();
  906. #endif
  907. }
  908. }
  909. else
  910. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  911. fLatencyChanged = false;
  912. }
  913. bufferSizeChanged(pData->engine->getBufferSize());
  914. reloadPrograms(true);
  915. if (pData->active)
  916. activate();
  917. carla_debug("CarlaPluginDSSI::reload() - end");
  918. }
  919. void reloadPrograms(const bool doInit) override
  920. {
  921. carla_debug("CarlaPluginDSSI::reloadPrograms(%s)", bool2str(doInit));
  922. const uint32_t oldCount = pData->midiprog.count;
  923. const int32_t current = pData->midiprog.current;
  924. // Delete old programs
  925. pData->midiprog.clear();
  926. // Query new programs
  927. uint32_t newCount = 0;
  928. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  929. {
  930. for (; fDssiDescriptor->get_program(fHandle, newCount) != nullptr;)
  931. ++newCount;
  932. }
  933. if (newCount > 0)
  934. {
  935. pData->midiprog.createNew(newCount);
  936. // Update data
  937. for (uint32_t i=0; i < newCount; ++i)
  938. {
  939. const DSSI_Program_Descriptor* const pdesc(fDssiDescriptor->get_program(fHandle, i));
  940. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  941. CARLA_SAFE_ASSERT(pdesc->Name != nullptr);
  942. pData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  943. pData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  944. pData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  945. }
  946. }
  947. #ifndef BUILD_BRIDGE
  948. // Update OSC Names
  949. if (pData->engine->isOscControlRegistered())
  950. {
  951. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  952. for (uint32_t i=0; i < newCount; ++i)
  953. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  954. }
  955. #endif
  956. if (doInit)
  957. {
  958. if (newCount > 0)
  959. setMidiProgram(0, false, false, false);
  960. }
  961. else
  962. {
  963. // Check if current program is invalid
  964. bool programChanged = false;
  965. if (newCount == oldCount+1)
  966. {
  967. // one midi program added, probably created by user
  968. pData->midiprog.current = static_cast<int32_t>(oldCount);
  969. programChanged = true;
  970. }
  971. else if (current < 0 && newCount > 0)
  972. {
  973. // programs exist now, but not before
  974. pData->midiprog.current = 0;
  975. programChanged = true;
  976. }
  977. else if (current >= 0 && newCount == 0)
  978. {
  979. // programs existed before, but not anymore
  980. pData->midiprog.current = -1;
  981. programChanged = true;
  982. }
  983. else if (current >= static_cast<int32_t>(newCount))
  984. {
  985. // current midi program > count
  986. pData->midiprog.current = 0;
  987. programChanged = true;
  988. }
  989. else
  990. {
  991. // no change
  992. pData->midiprog.current = current;
  993. }
  994. if (programChanged)
  995. setMidiProgram(pData->midiprog.current, true, true, true);
  996. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  997. }
  998. }
  999. // -------------------------------------------------------------------
  1000. // Plugin processing
  1001. void activate() noexcept override
  1002. {
  1003. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1004. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1005. if (fDescriptor->activate != nullptr)
  1006. {
  1007. try {
  1008. fDescriptor->activate(fHandle);
  1009. } CARLA_SAFE_EXCEPTION("DSSI activate");
  1010. if (fHandle2 != nullptr)
  1011. {
  1012. try {
  1013. fDescriptor->activate(fHandle2);
  1014. } CARLA_SAFE_EXCEPTION("DSSI activate #2");
  1015. }
  1016. }
  1017. }
  1018. void deactivate() noexcept override
  1019. {
  1020. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1021. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1022. if (fDescriptor->deactivate != nullptr)
  1023. {
  1024. try {
  1025. fDescriptor->deactivate(fHandle);
  1026. } CARLA_SAFE_EXCEPTION("DSSI deactivate");
  1027. if (fHandle2 != nullptr)
  1028. {
  1029. try {
  1030. fDescriptor->deactivate(fHandle2);
  1031. } CARLA_SAFE_EXCEPTION("DSSI deactivate #2");
  1032. }
  1033. }
  1034. }
  1035. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  1036. {
  1037. // --------------------------------------------------------------------------------------------------------
  1038. // Check if active
  1039. if (! pData->active)
  1040. {
  1041. // disable any output sound
  1042. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1043. FloatVectorOperations::clear(audioOut[i], static_cast<int>(frames));
  1044. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1045. FloatVectorOperations::clear(cvOut[i], static_cast<int>(frames));
  1046. return;
  1047. }
  1048. ulong midiEventCount = 0;
  1049. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, kPluginMaxMidiEvents);
  1050. // --------------------------------------------------------------------------------------------------------
  1051. // Check if needs reset
  1052. if (pData->needsReset)
  1053. {
  1054. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1055. {
  1056. midiEventCount = MAX_MIDI_CHANNELS*2;
  1057. for (uchar i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  1058. {
  1059. fMidiEvents[i].type = SND_SEQ_EVENT_CONTROLLER;
  1060. fMidiEvents[i].data.control.channel = i;
  1061. fMidiEvents[i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1062. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  1063. fMidiEvents[k+i].data.control.channel = i;
  1064. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1065. }
  1066. }
  1067. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1068. {
  1069. midiEventCount = MAX_MIDI_NOTE;
  1070. for (uchar i=0; i < MAX_MIDI_NOTE; ++i)
  1071. {
  1072. fMidiEvents[i].type = SND_SEQ_EVENT_NOTEOFF;
  1073. fMidiEvents[i].data.note.channel = static_cast<uchar>(pData->ctrlChannel);
  1074. fMidiEvents[i].data.note.note = i;
  1075. }
  1076. }
  1077. #ifndef BUILD_BRIDGE
  1078. if (pData->latency > 0)
  1079. {
  1080. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1081. FloatVectorOperations::clear(pData->latencyBuffers[i], static_cast<int>(pData->latency));
  1082. }
  1083. #endif
  1084. pData->needsReset = false;
  1085. }
  1086. // --------------------------------------------------------------------------------------------------------
  1087. // Event Input and Processing
  1088. if (pData->event.portIn != nullptr)
  1089. {
  1090. // ----------------------------------------------------------------------------------------------------
  1091. // MIDI Input (External)
  1092. if (pData->extNotes.mutex.tryLock())
  1093. {
  1094. ExternalMidiNote note = { 0, 0, 0 };
  1095. for (; midiEventCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1096. {
  1097. note = pData->extNotes.data.getFirst(note, true);
  1098. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1099. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1100. seqEvent.type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  1101. seqEvent.data.note.channel = static_cast<uchar>(note.channel);
  1102. seqEvent.data.note.note = note.note;
  1103. seqEvent.data.note.velocity = note.velo;
  1104. }
  1105. pData->extNotes.mutex.unlock();
  1106. } // End of MIDI Input (External)
  1107. // ----------------------------------------------------------------------------------------------------
  1108. // Event Input (System)
  1109. #ifndef BUILD_BRIDGE
  1110. bool allNotesOffSent = false;
  1111. #endif
  1112. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1113. uint32_t startTime = 0;
  1114. uint32_t timeOffset = 0;
  1115. uint32_t nextBankId;
  1116. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1117. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1118. else
  1119. nextBankId = 0;
  1120. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1121. {
  1122. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1123. if (event.time >= frames)
  1124. continue;
  1125. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  1126. if (isSampleAccurate && event.time > timeOffset)
  1127. {
  1128. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset, midiEventCount))
  1129. {
  1130. startTime = 0;
  1131. timeOffset = event.time;
  1132. midiEventCount = 0;
  1133. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1134. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1135. else
  1136. nextBankId = 0;
  1137. }
  1138. else
  1139. startTime += timeOffset;
  1140. }
  1141. switch (event.type)
  1142. {
  1143. case kEngineEventTypeNull:
  1144. break;
  1145. case kEngineEventTypeControl: {
  1146. const EngineControlEvent& ctrlEvent(event.ctrl);
  1147. switch (ctrlEvent.type)
  1148. {
  1149. case kEngineControlEventTypeNull:
  1150. break;
  1151. case kEngineControlEventTypeParameter: {
  1152. #ifndef BUILD_BRIDGE
  1153. // Control backend stuff
  1154. if (event.channel == pData->ctrlChannel)
  1155. {
  1156. float value;
  1157. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1158. {
  1159. value = ctrlEvent.value;
  1160. setDryWet(value, false, false);
  1161. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1162. break;
  1163. }
  1164. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1165. {
  1166. value = ctrlEvent.value*127.0f/100.0f;
  1167. setVolume(value, false, false);
  1168. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1169. break;
  1170. }
  1171. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1172. {
  1173. float left, right;
  1174. value = ctrlEvent.value/0.5f - 1.0f;
  1175. if (value < 0.0f)
  1176. {
  1177. left = -1.0f;
  1178. right = (value*2.0f)+1.0f;
  1179. }
  1180. else if (value > 0.0f)
  1181. {
  1182. left = (value*2.0f)-1.0f;
  1183. right = 1.0f;
  1184. }
  1185. else
  1186. {
  1187. left = -1.0f;
  1188. right = 1.0f;
  1189. }
  1190. setBalanceLeft(left, false, false);
  1191. setBalanceRight(right, false, false);
  1192. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1193. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1194. break;
  1195. }
  1196. }
  1197. #endif
  1198. // Control plugin parameters
  1199. uint32_t k;
  1200. for (k=0; k < pData->param.count; ++k)
  1201. {
  1202. if (pData->param.data[k].midiChannel != event.channel)
  1203. continue;
  1204. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1205. continue;
  1206. if (pData->param.data[k].type != PARAMETER_INPUT)
  1207. continue;
  1208. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1209. continue;
  1210. float value;
  1211. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1212. {
  1213. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1214. }
  1215. else
  1216. {
  1217. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1218. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1219. value = std::rint(value);
  1220. }
  1221. setParameterValue(k, value, false, false, false);
  1222. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1223. break;
  1224. }
  1225. // check if event is already handled
  1226. if (k != pData->param.count)
  1227. break;
  1228. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1229. {
  1230. if (midiEventCount >= kPluginMaxMidiEvents)
  1231. continue;
  1232. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1233. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1234. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1235. seqEvent.data.control.channel = event.channel;
  1236. seqEvent.data.control.param = ctrlEvent.param;
  1237. seqEvent.data.control.value = int8_t(ctrlEvent.value*127.0f);
  1238. }
  1239. break;
  1240. } // case kEngineControlEventTypeParameter
  1241. case kEngineControlEventTypeMidiBank:
  1242. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1243. nextBankId = ctrlEvent.param;
  1244. break;
  1245. case kEngineControlEventTypeMidiProgram:
  1246. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1247. {
  1248. const uint32_t nextProgramId = ctrlEvent.param;
  1249. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1250. {
  1251. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1252. {
  1253. const int32_t index(static_cast<int32_t>(k));
  1254. setMidiProgram(index, false, false, false);
  1255. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  1256. break;
  1257. }
  1258. }
  1259. }
  1260. break;
  1261. case kEngineControlEventTypeAllSoundOff:
  1262. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1263. {
  1264. if (midiEventCount >= kPluginMaxMidiEvents)
  1265. continue;
  1266. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1267. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1268. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1269. seqEvent.data.control.channel = event.channel;
  1270. seqEvent.data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1271. }
  1272. break;
  1273. case kEngineControlEventTypeAllNotesOff:
  1274. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1275. {
  1276. #ifndef BUILD_BRIDGE
  1277. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1278. {
  1279. allNotesOffSent = true;
  1280. sendMidiAllNotesOffToCallback();
  1281. }
  1282. #endif
  1283. if (midiEventCount >= kPluginMaxMidiEvents)
  1284. continue;
  1285. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1286. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1287. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1288. seqEvent.data.control.channel = event.channel;
  1289. seqEvent.data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1290. }
  1291. break;
  1292. } // switch (ctrlEvent.type)
  1293. break;
  1294. } // case kEngineEventTypeControl
  1295. case kEngineEventTypeMidi: {
  1296. if (midiEventCount >= kPluginMaxMidiEvents)
  1297. continue;
  1298. const EngineMidiEvent& midiEvent(event.midi);
  1299. if (midiEvent.size > EngineMidiEvent::kDataSize)
  1300. continue;
  1301. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1302. // Fix bad note-off (per DSSI spec)
  1303. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1304. status = MIDI_STATUS_NOTE_OFF;
  1305. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1306. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1307. switch (status)
  1308. {
  1309. case MIDI_STATUS_NOTE_OFF: {
  1310. const uint8_t note = midiEvent.data[1];
  1311. seqEvent.type = SND_SEQ_EVENT_NOTEOFF;
  1312. seqEvent.data.note.channel = event.channel;
  1313. seqEvent.data.note.note = note;
  1314. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, note, 0.0f);
  1315. break;
  1316. }
  1317. case MIDI_STATUS_NOTE_ON: {
  1318. const uint8_t note = midiEvent.data[1];
  1319. const uint8_t velo = midiEvent.data[2];
  1320. seqEvent.type = SND_SEQ_EVENT_NOTEON;
  1321. seqEvent.data.note.channel = event.channel;
  1322. seqEvent.data.note.note = note;
  1323. seqEvent.data.note.velocity = velo;
  1324. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, note, velo);
  1325. break;
  1326. }
  1327. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1328. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1329. {
  1330. const uint8_t note = midiEvent.data[1];
  1331. const uint8_t pressure = midiEvent.data[2];
  1332. seqEvent.type = SND_SEQ_EVENT_KEYPRESS;
  1333. seqEvent.data.note.channel = event.channel;
  1334. seqEvent.data.note.note = note;
  1335. seqEvent.data.note.velocity = pressure;
  1336. }
  1337. break;
  1338. case MIDI_STATUS_CONTROL_CHANGE:
  1339. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1340. {
  1341. const uint8_t control = midiEvent.data[1];
  1342. const uint8_t value = midiEvent.data[2];
  1343. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1344. seqEvent.data.control.channel = event.channel;
  1345. seqEvent.data.control.param = control;
  1346. seqEvent.data.control.value = value;
  1347. }
  1348. break;
  1349. case MIDI_STATUS_CHANNEL_PRESSURE:
  1350. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1351. {
  1352. const uint8_t pressure = midiEvent.data[1];
  1353. seqEvent.type = SND_SEQ_EVENT_CHANPRESS;
  1354. seqEvent.data.control.channel = event.channel;
  1355. seqEvent.data.control.value = pressure;
  1356. }
  1357. break;
  1358. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1359. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1360. {
  1361. const uint8_t lsb = midiEvent.data[1];
  1362. const uint8_t msb = midiEvent.data[2];
  1363. seqEvent.type = SND_SEQ_EVENT_PITCHBEND;
  1364. seqEvent.data.control.channel = event.channel;
  1365. seqEvent.data.control.value = ((msb << 7) | lsb) - 8192;
  1366. }
  1367. break;
  1368. default:
  1369. --midiEventCount;
  1370. break;
  1371. } // switch (status)
  1372. } break;
  1373. } // switch (event.type)
  1374. }
  1375. pData->postRtEvents.trySplice();
  1376. if (frames > timeOffset)
  1377. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset, midiEventCount);
  1378. } // End of Event Input and Processing
  1379. // --------------------------------------------------------------------------------------------------------
  1380. // Plugin processing (no events)
  1381. else
  1382. {
  1383. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0, midiEventCount);
  1384. } // End of Plugin processing (no events)
  1385. #ifndef BUILD_BRIDGE
  1386. // --------------------------------------------------------------------------------------------------------
  1387. // Latency, save values for next callback
  1388. if (fLatencyIndex != -1)
  1389. {
  1390. if (pData->latency != static_cast<uint32_t>(fParamBuffers[fLatencyIndex]))
  1391. {
  1392. fLatencyChanged = true;
  1393. }
  1394. else if (pData->latency > 0)
  1395. {
  1396. if (pData->latency <= frames)
  1397. {
  1398. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1399. FloatVectorOperations::copy(pData->latencyBuffers[i], audioIn[i]+(frames-pData->latency), static_cast<int>(pData->latency));
  1400. }
  1401. else
  1402. {
  1403. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  1404. {
  1405. for (k=0; k < pData->latency-frames; ++k)
  1406. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  1407. for (j=0; k < pData->latency; ++j, ++k)
  1408. pData->latencyBuffers[i][k] = audioIn[i][j];
  1409. }
  1410. }
  1411. }
  1412. }
  1413. // --------------------------------------------------------------------------------------------------------
  1414. // Control Output
  1415. if (pData->event.portOut != nullptr)
  1416. {
  1417. uint8_t channel;
  1418. uint16_t param;
  1419. float value;
  1420. for (uint32_t k=0; k < pData->param.count; ++k)
  1421. {
  1422. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1423. continue;
  1424. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1425. if (pData->param.data[k].midiCC > 0)
  1426. {
  1427. channel = pData->param.data[k].midiChannel;
  1428. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1429. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1430. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1431. }
  1432. }
  1433. } // End of Control Output
  1434. #endif
  1435. }
  1436. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset, const ulong midiEventCount)
  1437. {
  1438. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1439. if (pData->audioIn.count > 0)
  1440. {
  1441. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1442. }
  1443. if (pData->audioOut.count > 0)
  1444. {
  1445. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1446. }
  1447. if (pData->cvIn.count > 0)
  1448. {
  1449. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1450. }
  1451. if (pData->cvOut.count > 0)
  1452. {
  1453. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1454. }
  1455. // --------------------------------------------------------------------------------------------------------
  1456. // Try lock, silence otherwise
  1457. if (pData->engine->isOffline())
  1458. {
  1459. pData->singleMutex.lock();
  1460. }
  1461. else if (! pData->singleMutex.tryLock())
  1462. {
  1463. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1464. {
  1465. for (uint32_t k=0; k < frames; ++k)
  1466. audioOut[i][k+timeOffset] = 0.0f;
  1467. }
  1468. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1469. {
  1470. for (uint32_t k=0; k < frames; ++k)
  1471. cvOut[i][k+timeOffset] = 0.0f;
  1472. }
  1473. return false;
  1474. }
  1475. // --------------------------------------------------------------------------------------------------------
  1476. // Set audio buffers
  1477. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1478. FloatVectorOperations::copy(fAudioInBuffers[i], audioIn[i]+timeOffset, static_cast<int>(frames));
  1479. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1480. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  1481. #if 0
  1482. // --------------------------------------------------------------------------------------------------------
  1483. // Set CV buffers
  1484. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1485. FloatVectorOperations::copy(fCvInBuffers[i], cvIn[i]+timeOffset, static_cast<int>(frames));
  1486. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1487. FloatVectorOperations::clear(fCvOutBuffers[i], static_cast<int>(frames));
  1488. #endif
  1489. // --------------------------------------------------------------------------------------------------------
  1490. // Run plugin
  1491. // TODO - try catch
  1492. if (fDssiDescriptor->run_synth != nullptr)
  1493. {
  1494. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1495. if (fHandle2 != nullptr)
  1496. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1497. }
  1498. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1499. {
  1500. ulong instances = (fHandle2 != nullptr) ? 2 : 1;
  1501. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1502. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1503. ulong midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1504. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1505. }
  1506. else
  1507. {
  1508. fDescriptor->run(fHandle, frames);
  1509. if (fHandle2 != nullptr)
  1510. fDescriptor->run(fHandle2, frames);
  1511. }
  1512. #ifndef BUILD_BRIDGE
  1513. // --------------------------------------------------------------------------------------------------------
  1514. // Post-processing (dry/wet, volume and balance)
  1515. {
  1516. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && ! carla_compareFloats(pData->postProc.dryWet, 1.0f);
  1517. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_compareFloats(pData->postProc.balanceLeft, -1.0f) && carla_compareFloats(pData->postProc.balanceRight, 1.0f));
  1518. const bool isMono = (pData->audioIn.count == 1);
  1519. bool isPair;
  1520. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1521. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1522. {
  1523. // Dry/Wet
  1524. if (doDryWet)
  1525. {
  1526. for (uint32_t k=0; k < frames; ++k)
  1527. {
  1528. if (k < pData->latency)
  1529. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  1530. else if (pData->latency < frames)
  1531. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  1532. else
  1533. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  1534. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1535. }
  1536. }
  1537. // Balance
  1538. if (doBalance)
  1539. {
  1540. isPair = (i % 2 == 0);
  1541. if (isPair)
  1542. {
  1543. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1544. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  1545. }
  1546. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1547. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1548. for (uint32_t k=0; k < frames; ++k)
  1549. {
  1550. if (isPair)
  1551. {
  1552. // left
  1553. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1554. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1555. }
  1556. else
  1557. {
  1558. // right
  1559. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1560. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1561. }
  1562. }
  1563. }
  1564. // Volume (and buffer copy)
  1565. {
  1566. for (uint32_t k=0; k < frames; ++k)
  1567. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1568. }
  1569. }
  1570. } // End of Post-processing
  1571. #else // BUILD_BRIDGE
  1572. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1573. {
  1574. for (uint32_t k=0; k < frames; ++k)
  1575. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1576. }
  1577. #endif
  1578. #if 0
  1579. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1580. {
  1581. for (uint32_t k=0; k < frames; ++k)
  1582. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  1583. }
  1584. #endif
  1585. // --------------------------------------------------------------------------------------------------------
  1586. pData->singleMutex.unlock();
  1587. return true;
  1588. }
  1589. void bufferSizeChanged(const uint32_t newBufferSize) override
  1590. {
  1591. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1592. carla_debug("CarlaPluginDSSI::bufferSizeChanged(%i) - start", newBufferSize);
  1593. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1594. {
  1595. if (fAudioInBuffers[i] != nullptr)
  1596. delete[] fAudioInBuffers[i];
  1597. fAudioInBuffers[i] = new float[newBufferSize];
  1598. }
  1599. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1600. {
  1601. if (fAudioOutBuffers[i] != nullptr)
  1602. delete[] fAudioOutBuffers[i];
  1603. fAudioOutBuffers[i] = new float[newBufferSize];
  1604. }
  1605. if (fHandle2 == nullptr)
  1606. {
  1607. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1608. {
  1609. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1610. try {
  1611. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1612. } CARLA_SAFE_EXCEPTION("DSSI connect_port audio input");
  1613. }
  1614. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1615. {
  1616. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1617. try {
  1618. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1619. } CARLA_SAFE_EXCEPTION("DSSI connect_port audio output");
  1620. }
  1621. }
  1622. else
  1623. {
  1624. if (pData->audioIn.count > 0)
  1625. {
  1626. CARLA_ASSERT(pData->audioIn.count == 2);
  1627. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1628. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1629. try {
  1630. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1631. } CARLA_SAFE_EXCEPTION("DSSI connect_port audio input #1");
  1632. try {
  1633. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1634. } CARLA_SAFE_EXCEPTION("DSSI connect_port audio input #2");
  1635. }
  1636. if (pData->audioOut.count > 0)
  1637. {
  1638. CARLA_ASSERT(pData->audioOut.count == 2);
  1639. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1640. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1641. try {
  1642. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1643. } CARLA_SAFE_EXCEPTION("DSSI connect_port audio output #1");
  1644. try {
  1645. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1646. } CARLA_SAFE_EXCEPTION("DSSI connect_port audio output #2");
  1647. }
  1648. }
  1649. carla_debug("CarlaPluginDSSI::bufferSizeChanged(%i) - end", newBufferSize);
  1650. }
  1651. void sampleRateChanged(const double newSampleRate) override
  1652. {
  1653. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1654. carla_debug("CarlaPluginDSSI::sampleRateChanged(%g) - start", newSampleRate);
  1655. // TODO
  1656. (void)newSampleRate;
  1657. carla_debug("CarlaPluginDSSI::sampleRateChanged(%g) - end", newSampleRate);
  1658. }
  1659. // -------------------------------------------------------------------
  1660. // Plugin buffers
  1661. void clearBuffers() noexcept override
  1662. {
  1663. carla_debug("CarlaPluginDSSI::clearBuffers() - start");
  1664. if (fAudioInBuffers != nullptr)
  1665. {
  1666. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1667. {
  1668. if (fAudioInBuffers[i] != nullptr)
  1669. {
  1670. delete[] fAudioInBuffers[i];
  1671. fAudioInBuffers[i] = nullptr;
  1672. }
  1673. }
  1674. delete[] fAudioInBuffers;
  1675. fAudioInBuffers = nullptr;
  1676. }
  1677. if (fAudioOutBuffers != nullptr)
  1678. {
  1679. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1680. {
  1681. if (fAudioOutBuffers[i] != nullptr)
  1682. {
  1683. delete[] fAudioOutBuffers[i];
  1684. fAudioOutBuffers[i] = nullptr;
  1685. }
  1686. }
  1687. delete[] fAudioOutBuffers;
  1688. fAudioOutBuffers = nullptr;
  1689. }
  1690. if (fParamBuffers != nullptr)
  1691. {
  1692. delete[] fParamBuffers;
  1693. fParamBuffers = nullptr;
  1694. }
  1695. CarlaPlugin::clearBuffers();
  1696. carla_debug("CarlaPluginDSSI::clearBuffers() - end");
  1697. }
  1698. // -------------------------------------------------------------------
  1699. // Post-poned UI Stuff
  1700. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1701. {
  1702. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1703. if (fOscData.target == nullptr)
  1704. return;
  1705. osc_send_control(fOscData, pData->param.data[index].rindex, value);
  1706. }
  1707. void uiMidiProgramChange(const uint32_t index) noexcept override
  1708. {
  1709. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1710. if (fOscData.target == nullptr)
  1711. return;
  1712. osc_send_program(fOscData, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1713. }
  1714. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1715. {
  1716. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1717. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1718. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1719. if (fOscData.target == nullptr)
  1720. return;
  1721. #if 0
  1722. uint8_t midiData[4];
  1723. midiData[0] = 0;
  1724. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  1725. midiData[2] = note;
  1726. midiData[3] = velo;
  1727. osc_send_midi(fOscData, midiData);
  1728. #endif
  1729. }
  1730. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1731. {
  1732. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1733. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1734. if (fOscData.target == nullptr)
  1735. return;
  1736. #if 0
  1737. uint8_t midiData[4];
  1738. midiData[0] = 0;
  1739. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  1740. midiData[2] = note;
  1741. midiData[3] = 0;
  1742. osc_send_midi(fOscData, midiData);
  1743. #endif
  1744. }
  1745. // -------------------------------------------------------------------
  1746. void* getNativeHandle() const noexcept override
  1747. {
  1748. return fHandle;
  1749. }
  1750. const void* getNativeDescriptor() const noexcept override
  1751. {
  1752. return fDssiDescriptor;
  1753. }
  1754. uintptr_t getUiBridgeProcessId() const noexcept override
  1755. {
  1756. return fThreadUI.getProcessPID();
  1757. }
  1758. const void* getExtraStuff() const noexcept override
  1759. {
  1760. return fUiFilename;
  1761. }
  1762. // -------------------------------------------------------------------
  1763. void updateOscData(const lo_address& source, const char* const url) override
  1764. {
  1765. // FIXME - remove debug prints later
  1766. carla_stdout("CarlaPluginDSSI::updateOscData(%p, \"%s\")", source, url);
  1767. fOscData.clear();
  1768. const int proto = lo_address_get_protocol(source);
  1769. {
  1770. const char* host = lo_address_get_hostname(source);
  1771. const char* port = lo_address_get_port(source);
  1772. fOscData.source = lo_address_new_with_proto(proto, host, port);
  1773. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1774. }
  1775. {
  1776. char* host = lo_url_get_hostname(url);
  1777. char* port = lo_url_get_port(url);
  1778. fOscData.path = carla_strdup_free(lo_url_get_path(url));
  1779. fOscData.target = lo_address_new_with_proto(proto, host, port);
  1780. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, fOscData.path);
  1781. std::free(host);
  1782. std::free(port);
  1783. }
  1784. osc_send_sample_rate(fOscData, static_cast<float>(pData->engine->getSampleRate()));
  1785. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  1786. {
  1787. const CustomData& customData(it.getValue(kCustomDataFallback));
  1788. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1789. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_STRING) == 0)
  1790. osc_send_configure(fOscData, customData.key, customData.value);
  1791. }
  1792. if (pData->prog.current >= 0)
  1793. osc_send_program(fOscData, static_cast<uint32_t>(pData->prog.current));
  1794. if (pData->midiprog.current >= 0)
  1795. {
  1796. const MidiProgramData& curMidiProg(pData->midiprog.getCurrent());
  1797. if (getType() == PLUGIN_DSSI)
  1798. osc_send_program(fOscData, curMidiProg.bank, curMidiProg.program);
  1799. else
  1800. osc_send_midi_program(fOscData, curMidiProg.bank, curMidiProg.program);
  1801. }
  1802. for (uint32_t i=0; i < pData->param.count; ++i)
  1803. osc_send_control(fOscData, pData->param.data[i].rindex, getParameterValue(i));
  1804. if ((pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0 && pData->engine->getOptions().frontendWinId != 0)
  1805. pData->transientTryCounter = 1;
  1806. carla_stdout("CarlaPluginDSSI::updateOscData() - done");
  1807. }
  1808. // -------------------------------------------------------------------
  1809. bool init(const char* const filename, const char* const name, const char* const label)
  1810. {
  1811. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1812. // ---------------------------------------------------------------
  1813. // first checks
  1814. if (pData->client != nullptr)
  1815. {
  1816. pData->engine->setLastError("Plugin client is already registered");
  1817. return false;
  1818. }
  1819. if (filename == nullptr || filename[0] == '\0')
  1820. {
  1821. pData->engine->setLastError("null filename");
  1822. return false;
  1823. }
  1824. if (label == nullptr || label[0] == '\0')
  1825. {
  1826. pData->engine->setLastError("null label");
  1827. return false;
  1828. }
  1829. // ---------------------------------------------------------------
  1830. // open DLL
  1831. if (! pData->libOpen(filename))
  1832. {
  1833. pData->engine->setLastError(pData->libError(filename));
  1834. return false;
  1835. }
  1836. // ---------------------------------------------------------------
  1837. // get DLL main entry
  1838. const DSSI_Descriptor_Function descFn = pData->libSymbol<DSSI_Descriptor_Function>("dssi_descriptor");
  1839. if (descFn == nullptr)
  1840. {
  1841. pData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1842. return false;
  1843. }
  1844. // ---------------------------------------------------------------
  1845. // get descriptor that matches label
  1846. ulong i = 0;
  1847. for (;;)
  1848. {
  1849. try {
  1850. fDssiDescriptor = descFn(i++);
  1851. }
  1852. catch(...) {
  1853. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1854. fDescriptor = nullptr;
  1855. fDssiDescriptor = nullptr;
  1856. break;
  1857. }
  1858. if (fDssiDescriptor == nullptr)
  1859. break;
  1860. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1861. if (fDescriptor == nullptr)
  1862. {
  1863. carla_stderr2("WARNING - Missing LADSPA interface, will not use this plugin");
  1864. fDssiDescriptor = nullptr;
  1865. break;
  1866. }
  1867. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1868. {
  1869. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1870. fDescriptor = nullptr;
  1871. fDssiDescriptor = nullptr;
  1872. break;
  1873. }
  1874. if (fDescriptor->run == nullptr)
  1875. {
  1876. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1877. fDescriptor = nullptr;
  1878. fDssiDescriptor = nullptr;
  1879. break;
  1880. }
  1881. if (std::strcmp(fDescriptor->Label, label) == 0)
  1882. break;
  1883. }
  1884. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1885. {
  1886. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1887. return false;
  1888. }
  1889. // ---------------------------------------------------------------
  1890. // check if uses global instance
  1891. if (fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  1892. {
  1893. if (! addUniqueMultiSynth(fDescriptor->Label))
  1894. {
  1895. pData->engine->setLastError("This plugin uses a global instance and can't be used more than once safely");
  1896. return false;
  1897. }
  1898. }
  1899. // ---------------------------------------------------------------
  1900. // get info
  1901. if (name != nullptr && name[0] != '\0')
  1902. pData->name = pData->engine->getUniquePluginName(name);
  1903. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1904. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1905. else
  1906. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1907. pData->filename = carla_strdup(filename);
  1908. // ---------------------------------------------------------------
  1909. // register client
  1910. pData->client = pData->engine->addClient(this);
  1911. if (pData->client == nullptr || ! pData->client->isOk())
  1912. {
  1913. pData->engine->setLastError("Failed to register plugin client");
  1914. return false;
  1915. }
  1916. // ---------------------------------------------------------------
  1917. // initialize plugin
  1918. try {
  1919. fHandle = fDescriptor->instantiate(fDescriptor, (ulong)pData->engine->getSampleRate());
  1920. } CARLA_SAFE_EXCEPTION("DSSI instantiate");
  1921. if (fHandle == nullptr)
  1922. {
  1923. pData->engine->setLastError("Plugin failed to initialize");
  1924. return false;
  1925. }
  1926. // ---------------------------------------------------------------
  1927. // check for custom data extension
  1928. if (fDssiDescriptor->configure != nullptr)
  1929. {
  1930. if (char* const error = fDssiDescriptor->configure(fHandle, DSSI_CUSTOMDATA_EXTENSION_KEY, ""))
  1931. {
  1932. if (std::strcmp(error, "true") == 0 && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  1933. fUsesCustomData = true;
  1934. std::free(error);
  1935. }
  1936. }
  1937. // ---------------------------------------------------------------
  1938. // gui stuff
  1939. if (const char* const guiFilename = find_dssi_ui(filename, fDescriptor->Label))
  1940. {
  1941. fThreadUI.setData(guiFilename, fDescriptor->Label);
  1942. fUiFilename = guiFilename;
  1943. }
  1944. // ---------------------------------------------------------------
  1945. // set default options
  1946. #ifdef __USE_GNU
  1947. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  1948. #else
  1949. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  1950. #endif
  1951. pData->options = 0x0;
  1952. if (fLatencyIndex >= 0 || isDssiVst)
  1953. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1954. if (pData->engine->getOptions().forceStereo)
  1955. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1956. if (fUsesCustomData)
  1957. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1958. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  1959. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1960. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  1961. {
  1962. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1963. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1964. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1965. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1966. if (fDssiDescriptor->run_synth == nullptr)
  1967. carla_stderr("WARNING: Plugin can ONLY use run_multiple_synths!");
  1968. }
  1969. return true;
  1970. }
  1971. // -------------------------------------------------------------------
  1972. private:
  1973. LADSPA_Handle fHandle;
  1974. LADSPA_Handle fHandle2;
  1975. const LADSPA_Descriptor* fDescriptor;
  1976. const DSSI_Descriptor* fDssiDescriptor;
  1977. bool fUsesCustomData;
  1978. const char* fUiFilename;
  1979. float** fAudioInBuffers;
  1980. float** fAudioOutBuffers;
  1981. float* fParamBuffers;
  1982. bool fLatencyChanged;
  1983. int32_t fLatencyIndex; // -1 if invalid
  1984. snd_seq_event_t fMidiEvents[kPluginMaxMidiEvents];
  1985. CarlaOscData fOscData;
  1986. CarlaThreadDSSIUI fThreadUI;
  1987. // -------------------------------------------------------------------
  1988. uint32_t getSafePortCount() const noexcept
  1989. {
  1990. if (fDescriptor->PortCount == 0)
  1991. return 0;
  1992. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1993. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1994. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1995. return static_cast<uint32_t>(fDescriptor->PortCount);
  1996. }
  1997. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1998. {
  1999. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  2000. return true;
  2001. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  2002. return true;
  2003. return false;
  2004. }
  2005. bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf, const bool wantName, const bool useBracket) const noexcept
  2006. {
  2007. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  2008. if (sepBracketStart == nullptr)
  2009. return false;
  2010. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  2011. if (sepBracketEnd == nullptr)
  2012. return false;
  2013. const size_t unitSize(static_cast<size_t>(sepBracketEnd-sepBracketStart-2));
  2014. if (unitSize > 7) // very unlikely to have such big unit
  2015. return false;
  2016. const size_t sepIndex(std::strlen(paramName)-unitSize-3);
  2017. // just in case
  2018. if (sepIndex > STR_MAX)
  2019. return false;
  2020. if (wantName)
  2021. {
  2022. std::strncpy(strBuf, paramName, sepIndex);
  2023. strBuf[sepIndex] = '\0';
  2024. }
  2025. else
  2026. {
  2027. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  2028. strBuf[unitSize] = '\0';
  2029. }
  2030. return true;
  2031. }
  2032. // -------------------------------------------------------------------
  2033. static LinkedList<const char*> sMultiSynthList;
  2034. static bool addUniqueMultiSynth(const char* const label) noexcept
  2035. {
  2036. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', false);
  2037. const char* dlabel = nullptr;
  2038. try {
  2039. dlabel = carla_strdup(label);
  2040. } catch(...) { return false; }
  2041. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  2042. {
  2043. const char* const itLabel(it.getValue());
  2044. if (std::strcmp(dlabel, itLabel) == 0)
  2045. {
  2046. delete[] dlabel;
  2047. return false;
  2048. }
  2049. }
  2050. return sMultiSynthList.append(dlabel);
  2051. }
  2052. static void removeUniqueMultiSynth(const char* const label) noexcept
  2053. {
  2054. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0',);
  2055. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  2056. {
  2057. const char* const itLabel(it.getValue());
  2058. if (std::strcmp(label, itLabel) == 0)
  2059. {
  2060. sMultiSynthList.remove(it);
  2061. delete[] itLabel;
  2062. break;
  2063. }
  2064. }
  2065. }
  2066. // -------------------------------------------------------------------
  2067. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginDSSI)
  2068. };
  2069. LinkedList<const char*> CarlaPluginDSSI::sMultiSynthList;
  2070. // -------------------------------------------------------------------------------------------------------------------
  2071. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  2072. {
  2073. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2074. CarlaPluginDSSI* const plugin(new CarlaPluginDSSI(init.engine, init.id));
  2075. if (! plugin->init(init.filename, init.name, init.label))
  2076. {
  2077. delete plugin;
  2078. return nullptr;
  2079. }
  2080. plugin->reload();
  2081. bool canRun = true;
  2082. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2083. {
  2084. if (! plugin->canRunInRack())
  2085. {
  2086. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  2087. canRun = false;
  2088. }
  2089. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  2090. {
  2091. init.engine->setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  2092. canRun = false;
  2093. }
  2094. }
  2095. else if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_PATCHBAY && (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0))
  2096. {
  2097. init.engine->setLastError("CV ports in patchbay mode is still TODO");
  2098. canRun = false;
  2099. }
  2100. if (! canRun)
  2101. {
  2102. delete plugin;
  2103. return nullptr;
  2104. }
  2105. return plugin;
  2106. }
  2107. // -------------------------------------------------------------------------------------------------------------------
  2108. CARLA_BACKEND_END_NAMESPACE