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.

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