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.

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