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.

2110 lines
74KB

  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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifdef WANT_DSSI
  20. #include "CarlaDssiUtils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include <QtCore/QByteArray>
  23. // -----------------------------------------------------
  24. CARLA_BACKEND_START_NAMESPACE
  25. #if 0
  26. }
  27. #endif
  28. // -----------------------------------------------------
  29. class DssiPlugin : public CarlaPlugin
  30. {
  31. public:
  32. DssiPlugin(CarlaEngine* const engine, const uint id) noexcept
  33. : CarlaPlugin(engine, id),
  34. fHandle(nullptr),
  35. fHandle2(nullptr),
  36. fDescriptor(nullptr),
  37. fDssiDescriptor(nullptr),
  38. fUsesCustomData(false),
  39. fUiFilename(nullptr),
  40. fAudioInBuffers(nullptr),
  41. fAudioOutBuffers(nullptr),
  42. fParamBuffers(nullptr)
  43. {
  44. carla_debug("DssiPlugin::DssiPlugin(%p, %i)", engine, id);
  45. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  46. }
  47. ~DssiPlugin() noexcept override
  48. {
  49. carla_debug("DssiPlugin::~DssiPlugin()");
  50. // close UI
  51. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  52. {
  53. showCustomUI(false);
  54. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  55. }
  56. pData->singleMutex.lock();
  57. pData->masterMutex.lock();
  58. if (pData->client != nullptr && pData->client->isActive())
  59. pData->client->deactivate();
  60. if (pData->active)
  61. {
  62. deactivate();
  63. pData->active = false;
  64. }
  65. if (fDescriptor != nullptr)
  66. {
  67. if (pData->name != nullptr && fDssiDescriptor != nullptr && fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  68. removeUniqueMultiSynth(fDescriptor->Label);
  69. if (fDescriptor->cleanup != nullptr)
  70. {
  71. if (fHandle != nullptr)
  72. {
  73. try {
  74. fDescriptor->cleanup(fHandle);
  75. } catch(...) {}
  76. }
  77. if (fHandle2 != nullptr)
  78. {
  79. try {
  80. fDescriptor->cleanup(fHandle2);
  81. } catch(...) {}
  82. }
  83. }
  84. fHandle = nullptr;
  85. fHandle2 = nullptr;
  86. fDescriptor = nullptr;
  87. fDssiDescriptor = nullptr;
  88. }
  89. if (fUiFilename != nullptr)
  90. {
  91. delete[] fUiFilename;
  92. fUiFilename = nullptr;
  93. }
  94. clearBuffers();
  95. }
  96. // -------------------------------------------------------------------
  97. // Information (base)
  98. PluginType getType() const noexcept override
  99. {
  100. return PLUGIN_DSSI;
  101. }
  102. PluginCategory getCategory() const noexcept override
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  105. if (pData->audioIn.count == 0 && pData->audioOut.count > 0 && (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr))
  106. return PLUGIN_CATEGORY_SYNTH;
  107. return CarlaPlugin::getCategory();
  108. }
  109. int64_t getUniqueId() const noexcept override
  110. {
  111. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  112. return static_cast<int64_t>(fDescriptor->UniqueID);
  113. }
  114. // -------------------------------------------------------------------
  115. // Information (count)
  116. // nothing
  117. // -------------------------------------------------------------------
  118. // Information (current data)
  119. int32_t getChunkData(void** const dataPtr) const noexcept override
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData, 0);
  122. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  123. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0);
  124. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->get_custom_data != nullptr, 0);
  125. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  126. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr, 0);
  127. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  128. int ret = 0;
  129. ulong dataSize = 0;
  130. try {
  131. ret = fDssiDescriptor->get_custom_data(fHandle, dataPtr, &dataSize);
  132. } catch(...) {}
  133. return (ret != 0) ? static_cast<int32_t>(dataSize) : 0;
  134. }
  135. // -------------------------------------------------------------------
  136. // Information (per-plugin data)
  137. uint getOptionsAvailable() const noexcept override
  138. {
  139. #ifdef __USE_GNU
  140. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  141. #else
  142. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  143. #endif
  144. uint options = 0x0;
  145. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  146. if (! isDssiVst)
  147. {
  148. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  149. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  150. {
  151. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  152. options |= PLUGIN_OPTION_FORCE_STEREO;
  153. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  154. options |= PLUGIN_OPTION_FORCE_STEREO;
  155. }
  156. }
  157. if (fUsesCustomData)
  158. options |= PLUGIN_OPTION_USE_CHUNKS;
  159. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  160. {
  161. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  162. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  163. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  164. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  165. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  166. }
  167. return options;
  168. }
  169. float getParameterValue(const uint32_t parameterId) const noexcept override
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  172. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  173. return fParamBuffers[parameterId];
  174. }
  175. void getLabel(char* const strBuf) const noexcept override
  176. {
  177. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  178. if (fDescriptor->Label != nullptr)
  179. {
  180. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  181. return;
  182. }
  183. CarlaPlugin::getLabel(strBuf);
  184. }
  185. void getMaker(char* const strBuf) const noexcept override
  186. {
  187. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  188. if (fDescriptor->Maker != nullptr)
  189. {
  190. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  191. return;
  192. }
  193. CarlaPlugin::getMaker(strBuf);
  194. }
  195. void getCopyright(char* const strBuf) const noexcept override
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  198. if (fDescriptor->Copyright != nullptr)
  199. {
  200. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  201. return;
  202. }
  203. CarlaPlugin::getCopyright(strBuf);
  204. }
  205. void getRealName(char* const strBuf) const noexcept override
  206. {
  207. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  208. if (fDescriptor->Name != nullptr)
  209. {
  210. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  211. return;
  212. }
  213. CarlaPlugin::getRealName(strBuf);
  214. }
  215. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  216. {
  217. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  218. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  219. const int32_t rindex(pData->param.data[parameterId].rindex);
  220. if (rindex < static_cast<int32_t>(fDescriptor->PortCount) && fDescriptor->PortNames[rindex] != nullptr)
  221. {
  222. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  223. return;
  224. }
  225. CarlaPlugin::getParameterName(parameterId, strBuf);
  226. }
  227. // -------------------------------------------------------------------
  228. // Set data (state)
  229. // nothing
  230. // -------------------------------------------------------------------
  231. // Set data (internal stuff)
  232. // nothing
  233. // -------------------------------------------------------------------
  234. // Set data (plugin-specific stuff)
  235. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  236. {
  237. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  238. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  239. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  240. fParamBuffers[parameterId] = fixedValue;
  241. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  242. }
  243. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  244. {
  245. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  246. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  247. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  248. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  249. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  250. carla_debug("DssiPlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  251. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  252. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  253. if (fDssiDescriptor->configure != nullptr)
  254. {
  255. try {
  256. fDssiDescriptor->configure(fHandle, key, value);
  257. } catch(...) {}
  258. if (fHandle2 != nullptr)
  259. {
  260. try {
  261. fDssiDescriptor->configure(fHandle2, key, value);
  262. } catch(...) {}
  263. }
  264. }
  265. if (sendGui && pData->osc.data.target != nullptr)
  266. osc_send_configure(pData->osc.data, key, value);
  267. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  268. {
  269. const ScopedSingleProcessLocker spl(this, true);
  270. reloadPrograms(false);
  271. }
  272. CarlaPlugin::setCustomData(type, key, value, sendGui);
  273. }
  274. void setChunkData(const char* const stringData) override
  275. {
  276. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData,);
  277. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  278. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  279. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->set_custom_data != nullptr,);
  280. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  281. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr,);
  282. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr,);
  283. QByteArray chunk(QByteArray::fromBase64(stringData));
  284. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0,);
  285. const ScopedSingleProcessLocker spl(this, true);
  286. try {
  287. fDssiDescriptor->set_custom_data(fHandle, chunk.data(), static_cast<ulong>(chunk.size()));
  288. } catch(...) {}
  289. }
  290. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  291. {
  292. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  293. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  294. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  295. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  296. if (index >= 0)
  297. {
  298. const uint32_t bank(pData->midiprog.data[index].bank);
  299. const uint32_t program(pData->midiprog.data[index].program);
  300. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  301. try {
  302. fDssiDescriptor->select_program(fHandle, bank, program);
  303. } catch(...) {}
  304. if (fHandle2 != nullptr)
  305. {
  306. try {
  307. fDssiDescriptor->select_program(fHandle2, bank, program);
  308. } catch(...) {}
  309. }
  310. }
  311. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  312. }
  313. // -------------------------------------------------------------------
  314. // Set ui stuff
  315. void showCustomUI(const bool yesNo) override
  316. {
  317. if (yesNo)
  318. {
  319. pData->osc.data.free();
  320. pData->osc.thread.startThread();
  321. }
  322. else
  323. {
  324. pData->transientTryCounter = 0;
  325. if (pData->osc.data.target != nullptr)
  326. {
  327. osc_send_hide(pData->osc.data);
  328. osc_send_quit(pData->osc.data);
  329. pData->osc.data.free();
  330. }
  331. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  332. }
  333. }
  334. // -------------------------------------------------------------------
  335. // Plugin state
  336. void reload() override
  337. {
  338. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  339. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  340. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  341. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  342. carla_debug("DssiPlugin::reload() - start");
  343. const EngineProcessMode processMode(pData->engine->getProccessMode());
  344. // Safely disable plugin for reload
  345. const ScopedDisabler sd(this);
  346. if (pData->active)
  347. deactivate();
  348. clearBuffers();
  349. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  350. const uint32_t portCount(static_cast<uint32_t>(fDescriptor->PortCount));
  351. uint32_t aIns, aOuts, mIns, params;
  352. aIns = aOuts = mIns = params = 0;
  353. bool forcedStereoIn, forcedStereoOut;
  354. forcedStereoIn = forcedStereoOut = false;
  355. bool needsCtrlIn, needsCtrlOut;
  356. needsCtrlIn = needsCtrlOut = false;
  357. if (portCount > 0)
  358. {
  359. CARLA_ASSERT(fDescriptor->PortDescriptors != nullptr);
  360. CARLA_ASSERT(fDescriptor->PortRangeHints != nullptr);
  361. CARLA_ASSERT(fDescriptor->PortNames != nullptr);
  362. for (uint32_t i=0; i < portCount; ++i)
  363. {
  364. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  365. if (LADSPA_IS_PORT_AUDIO(portType))
  366. {
  367. if (LADSPA_IS_PORT_INPUT(portType))
  368. aIns += 1;
  369. else if (LADSPA_IS_PORT_OUTPUT(portType))
  370. aOuts += 1;
  371. }
  372. else if (LADSPA_IS_PORT_CONTROL(portType))
  373. params += 1;
  374. }
  375. }
  376. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  377. {
  378. if (fHandle2 == nullptr)
  379. {
  380. try {
  381. fHandle2 = fDescriptor->instantiate(fDescriptor, (ulong)sampleRate);
  382. } catch(...) {}
  383. }
  384. if (fHandle2 != nullptr)
  385. {
  386. if (aIns == 1)
  387. {
  388. aIns = 2;
  389. forcedStereoIn = true;
  390. }
  391. if (aOuts == 1)
  392. {
  393. aOuts = 2;
  394. forcedStereoOut = true;
  395. }
  396. }
  397. }
  398. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  399. {
  400. mIns = 1;
  401. needsCtrlIn = true;
  402. }
  403. if (aIns > 0)
  404. {
  405. pData->audioIn.createNew(aIns);
  406. fAudioInBuffers = new float*[aIns];
  407. for (uint32_t i=0; i < aIns; ++i)
  408. fAudioInBuffers[i] = nullptr;
  409. }
  410. if (aOuts > 0)
  411. {
  412. pData->audioOut.createNew(aOuts);
  413. fAudioOutBuffers = new float*[aOuts];
  414. needsCtrlIn = true;
  415. for (uint32_t i=0; i < aOuts; ++i)
  416. fAudioOutBuffers[i] = nullptr;
  417. }
  418. if (params > 0)
  419. {
  420. pData->param.createNew(params, true);
  421. fParamBuffers = new float[params];
  422. FLOAT_CLEAR(fParamBuffers, params);
  423. }
  424. const uint portNameSize(pData->engine->getMaxPortNameSize());
  425. CarlaString portName;
  426. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  427. {
  428. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  429. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  430. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  431. if (LADSPA_IS_PORT_AUDIO(portType))
  432. {
  433. portName.clear();
  434. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  435. {
  436. portName = pData->name;
  437. portName += ":";
  438. }
  439. portName += fDescriptor->PortNames[i];
  440. portName.truncate(portNameSize);
  441. if (LADSPA_IS_PORT_INPUT(portType))
  442. {
  443. const uint32_t j = iAudioIn++;
  444. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  445. pData->audioIn.ports[j].rindex = i;
  446. if (forcedStereoIn)
  447. {
  448. portName += "_2";
  449. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  450. pData->audioIn.ports[1].rindex = i;
  451. }
  452. }
  453. else if (LADSPA_IS_PORT_OUTPUT(portType))
  454. {
  455. const uint32_t j = iAudioOut++;
  456. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  457. pData->audioOut.ports[j].rindex = i;
  458. if (forcedStereoOut)
  459. {
  460. portName += "_2";
  461. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  462. pData->audioOut.ports[1].rindex = i;
  463. }
  464. }
  465. else
  466. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  467. }
  468. else if (LADSPA_IS_PORT_CONTROL(portType))
  469. {
  470. const uint32_t j = iCtrl++;
  471. pData->param.data[j].index = static_cast<int32_t>(j);
  472. pData->param.data[j].rindex = static_cast<int32_t>(i);
  473. float min, max, def, step, stepSmall, stepLarge;
  474. // min value
  475. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  476. min = portRangeHints.LowerBound;
  477. else
  478. min = 0.0f;
  479. // max value
  480. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  481. max = portRangeHints.UpperBound;
  482. else
  483. max = 1.0f;
  484. if (min > max)
  485. max = min;
  486. if (max - min == 0.0f)
  487. {
  488. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  489. max = min + 0.1f;
  490. }
  491. // default value
  492. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  493. if (def < min)
  494. def = min;
  495. else if (def > max)
  496. def = max;
  497. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  498. {
  499. min *= sampleRate;
  500. max *= sampleRate;
  501. def *= sampleRate;
  502. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  503. }
  504. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  505. {
  506. step = max - min;
  507. stepSmall = step;
  508. stepLarge = step;
  509. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  510. }
  511. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  512. {
  513. step = 1.0f;
  514. stepSmall = 1.0f;
  515. stepLarge = 10.0f;
  516. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  517. }
  518. else
  519. {
  520. float range = max - min;
  521. step = range/100.0f;
  522. stepSmall = range/1000.0f;
  523. stepLarge = range/10.0f;
  524. }
  525. if (LADSPA_IS_PORT_INPUT(portType))
  526. {
  527. pData->param.data[j].type = PARAMETER_INPUT;
  528. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  529. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  530. needsCtrlIn = true;
  531. // MIDI CC value
  532. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  533. {
  534. int controller = fDssiDescriptor->get_midi_controller_for_port(fHandle, i);
  535. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  536. {
  537. int16_t cc = DSSI_CC_NUMBER(controller);
  538. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  539. pData->param.data[j].midiCC = cc;
  540. }
  541. }
  542. }
  543. else if (LADSPA_IS_PORT_OUTPUT(portType))
  544. {
  545. pData->param.data[j].type = PARAMETER_OUTPUT;
  546. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  547. {
  548. min = 0.0f;
  549. max = sampleRate;
  550. def = 0.0f;
  551. step = 1.0f;
  552. stepSmall = 1.0f;
  553. stepLarge = 1.0f;
  554. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  555. }
  556. else
  557. {
  558. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  559. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  560. needsCtrlOut = true;
  561. }
  562. }
  563. else
  564. {
  565. pData->param.data[j].type = PARAMETER_UNKNOWN;
  566. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  567. }
  568. // extra parameter hints
  569. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  570. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  571. pData->param.ranges[j].min = min;
  572. pData->param.ranges[j].max = max;
  573. pData->param.ranges[j].def = def;
  574. pData->param.ranges[j].step = step;
  575. pData->param.ranges[j].stepSmall = stepSmall;
  576. pData->param.ranges[j].stepLarge = stepLarge;
  577. // Start parameters in their default values
  578. fParamBuffers[j] = def;
  579. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  580. if (fHandle2 != nullptr)
  581. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  582. }
  583. else
  584. {
  585. // Not Audio or Control
  586. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  587. fDescriptor->connect_port(fHandle, i, nullptr);
  588. if (fHandle2 != nullptr)
  589. fDescriptor->connect_port(fHandle2, i, nullptr);
  590. }
  591. }
  592. if (needsCtrlIn)
  593. {
  594. portName.clear();
  595. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  596. {
  597. portName = pData->name;
  598. portName += ":";
  599. }
  600. portName += "events-in";
  601. portName.truncate(portNameSize);
  602. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  603. }
  604. if (needsCtrlOut)
  605. {
  606. portName.clear();
  607. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  608. {
  609. portName = pData->name;
  610. portName += ":";
  611. }
  612. portName += "events-out";
  613. portName.truncate(portNameSize);
  614. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  615. }
  616. if (forcedStereoIn || forcedStereoOut)
  617. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  618. else
  619. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  620. // plugin hints
  621. pData->hints = 0x0;
  622. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  623. pData->hints |= PLUGIN_IS_RTSAFE;
  624. if (fUiFilename != nullptr)
  625. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  626. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  627. pData->hints |= PLUGIN_CAN_DRYWET;
  628. if (aOuts > 0)
  629. pData->hints |= PLUGIN_CAN_VOLUME;
  630. if (aOuts >= 2 && aOuts % 2 == 0)
  631. pData->hints |= PLUGIN_CAN_BALANCE;
  632. // extra plugin hints
  633. pData->extraHints = 0x0;
  634. if (mIns > 0)
  635. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  636. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  637. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  638. // check latency
  639. if (pData->hints & PLUGIN_CAN_DRYWET)
  640. {
  641. for (uint32_t i=0; i < pData->param.count; ++i)
  642. {
  643. if (pData->param.special[i] != PARAMETER_SPECIAL_LATENCY)
  644. continue;
  645. // we need to pre-run the plugin so it can update its latency control-port
  646. float tmpIn[aIns][2];
  647. float tmpOut[aOuts][2];
  648. for (uint32_t j=0; j < aIns; ++j)
  649. {
  650. tmpIn[j][0] = 0.0f;
  651. tmpIn[j][1] = 0.0f;
  652. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  653. }
  654. for (uint32_t j=0; j < aOuts; ++j)
  655. {
  656. tmpOut[j][0] = 0.0f;
  657. tmpOut[j][1] = 0.0f;
  658. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  659. }
  660. if (fDescriptor->activate != nullptr)
  661. fDescriptor->activate(fHandle);
  662. fDescriptor->run(fHandle, 2);
  663. if (fDescriptor->deactivate != nullptr)
  664. fDescriptor->deactivate(fHandle);
  665. const uint32_t latency = (uint32_t)fParamBuffers[i];
  666. if (pData->latency != latency)
  667. {
  668. pData->latency = latency;
  669. pData->client->setLatency(latency);
  670. #ifndef BUILD_BRIDGE
  671. pData->recreateLatencyBuffers();
  672. #endif
  673. }
  674. break;
  675. }
  676. }
  677. bufferSizeChanged(pData->engine->getBufferSize());
  678. reloadPrograms(true);
  679. if (pData->active)
  680. activate();
  681. carla_debug("DssiPlugin::reload() - end");
  682. }
  683. void reloadPrograms(const bool doInit) override
  684. {
  685. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(doInit));
  686. const uint32_t oldCount = pData->midiprog.count;
  687. const int32_t current = pData->midiprog.current;
  688. // Delete old programs
  689. pData->midiprog.clear();
  690. // Query new programs
  691. uint32_t newCount = 0;
  692. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  693. {
  694. for (; fDssiDescriptor->get_program(fHandle, newCount) != nullptr;)
  695. ++newCount;
  696. }
  697. if (newCount > 0)
  698. {
  699. pData->midiprog.createNew(newCount);
  700. // Update data
  701. for (uint32_t i=0; i < newCount; ++i)
  702. {
  703. const DSSI_Program_Descriptor* const pdesc(fDssiDescriptor->get_program(fHandle, i));
  704. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  705. CARLA_SAFE_ASSERT(pdesc->Name != nullptr);
  706. pData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  707. pData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  708. pData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  709. }
  710. }
  711. #ifndef BUILD_BRIDGE
  712. // Update OSC Names
  713. if (pData->engine->isOscControlRegistered())
  714. {
  715. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  716. for (uint32_t i=0; i < newCount; ++i)
  717. 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);
  718. }
  719. #endif
  720. if (doInit)
  721. {
  722. if (newCount > 0)
  723. setMidiProgram(0, false, false, false);
  724. }
  725. else
  726. {
  727. // Check if current program is invalid
  728. bool programChanged = false;
  729. if (newCount == oldCount+1)
  730. {
  731. // one midi program added, probably created by user
  732. pData->midiprog.current = static_cast<int32_t>(oldCount);
  733. programChanged = true;
  734. }
  735. else if (current < 0 && newCount > 0)
  736. {
  737. // programs exist now, but not before
  738. pData->midiprog.current = 0;
  739. programChanged = true;
  740. }
  741. else if (current >= 0 && newCount == 0)
  742. {
  743. // programs existed before, but not anymore
  744. pData->midiprog.current = -1;
  745. programChanged = true;
  746. }
  747. else if (current >= static_cast<int32_t>(newCount))
  748. {
  749. // current midi program > count
  750. pData->midiprog.current = 0;
  751. programChanged = true;
  752. }
  753. else
  754. {
  755. // no change
  756. pData->midiprog.current = current;
  757. }
  758. if (programChanged)
  759. setMidiProgram(pData->midiprog.current, true, true, true);
  760. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  761. }
  762. }
  763. // -------------------------------------------------------------------
  764. // Plugin processing
  765. void activate() noexcept override
  766. {
  767. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  768. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  769. if (fDescriptor->activate != nullptr)
  770. {
  771. try {
  772. fDescriptor->activate(fHandle);
  773. } catch(...) {}
  774. if (fHandle2 != nullptr)
  775. {
  776. try {
  777. fDescriptor->activate(fHandle2);
  778. } catch(...) {}
  779. }
  780. }
  781. }
  782. void deactivate() noexcept override
  783. {
  784. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  785. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  786. if (fDescriptor->deactivate != nullptr)
  787. {
  788. try {
  789. fDescriptor->deactivate(fHandle);
  790. } catch(...) {}
  791. if (fHandle2 != nullptr)
  792. {
  793. try {
  794. fDescriptor->deactivate(fHandle2);
  795. } catch(...) {}
  796. }
  797. }
  798. }
  799. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  800. {
  801. // --------------------------------------------------------------------------------------------------------
  802. // Check if active
  803. if (! pData->active)
  804. {
  805. // disable any output sound
  806. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  807. FLOAT_CLEAR(outBuffer[i], frames);
  808. return;
  809. }
  810. ulong midiEventCount = 0;
  811. // --------------------------------------------------------------------------------------------------------
  812. // Check if needs reset
  813. if (pData->needsReset)
  814. {
  815. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  816. {
  817. midiEventCount = MAX_MIDI_CHANNELS*2;
  818. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  819. for (uchar i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  820. {
  821. fMidiEvents[i].type = SND_SEQ_EVENT_CONTROLLER;
  822. fMidiEvents[i].data.control.channel = i;
  823. fMidiEvents[i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  824. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  825. fMidiEvents[k+i].data.control.channel = i;
  826. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  827. }
  828. }
  829. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  830. {
  831. midiEventCount = MAX_MIDI_NOTE;
  832. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  833. for (uchar i=0; i < MAX_MIDI_NOTE; ++i)
  834. {
  835. fMidiEvents[i].type = SND_SEQ_EVENT_NOTEOFF;
  836. fMidiEvents[i].data.note.channel = static_cast<uchar>(pData->ctrlChannel);
  837. fMidiEvents[i].data.note.note = i;
  838. }
  839. }
  840. #ifndef BUILD_BRIDGE
  841. if (pData->latency > 0)
  842. {
  843. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  844. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  845. }
  846. #endif
  847. pData->needsReset = false;
  848. }
  849. // --------------------------------------------------------------------------------------------------------
  850. // Event Input and Processing
  851. if (pData->event.portIn != nullptr)
  852. {
  853. // ----------------------------------------------------------------------------------------------------
  854. // MIDI Input (External)
  855. if (pData->extNotes.mutex.tryLock())
  856. {
  857. ExternalMidiNote note = { 0, 0, 0 };
  858. for (; midiEventCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  859. {
  860. note = pData->extNotes.data.getFirst(note, true);
  861. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  862. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  863. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  864. midiEvent.type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  865. midiEvent.data.note.channel = static_cast<uchar>(note.channel);
  866. midiEvent.data.note.note = note.note;
  867. midiEvent.data.note.velocity = note.velo;
  868. }
  869. pData->extNotes.mutex.unlock();
  870. } // End of MIDI Input (External)
  871. // ----------------------------------------------------------------------------------------------------
  872. // Event Input (System)
  873. bool allNotesOffSent = false;
  874. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  875. uint32_t numEvents = pData->event.portIn->getEventCount();
  876. uint32_t startTime = 0;
  877. uint32_t timeOffset = 0;
  878. uint32_t nextBankId;
  879. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  880. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  881. else
  882. nextBankId = 0;
  883. for (uint32_t i=0; i < numEvents; ++i)
  884. {
  885. const EngineEvent& event(pData->event.portIn->getEvent(i));
  886. if (event.time >= frames)
  887. continue;
  888. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  889. if (isSampleAccurate && event.time > timeOffset)
  890. {
  891. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset, midiEventCount))
  892. {
  893. startTime = 0;
  894. timeOffset = event.time;
  895. midiEventCount = 0;
  896. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  897. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  898. else
  899. nextBankId = 0;
  900. }
  901. else
  902. startTime += timeOffset;
  903. }
  904. switch (event.type)
  905. {
  906. case kEngineEventTypeNull:
  907. break;
  908. case kEngineEventTypeControl: {
  909. const EngineControlEvent& ctrlEvent(event.ctrl);
  910. switch (ctrlEvent.type)
  911. {
  912. case kEngineControlEventTypeNull:
  913. break;
  914. case kEngineControlEventTypeParameter: {
  915. #ifndef BUILD_BRIDGE
  916. // Control backend stuff
  917. if (event.channel == pData->ctrlChannel)
  918. {
  919. float value;
  920. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  921. {
  922. value = ctrlEvent.value;
  923. setDryWet(value, false, false);
  924. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  925. break;
  926. }
  927. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  928. {
  929. value = ctrlEvent.value*127.0f/100.0f;
  930. setVolume(value, false, false);
  931. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  932. break;
  933. }
  934. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  935. {
  936. float left, right;
  937. value = ctrlEvent.value/0.5f - 1.0f;
  938. if (value < 0.0f)
  939. {
  940. left = -1.0f;
  941. right = (value*2.0f)+1.0f;
  942. }
  943. else if (value > 0.0f)
  944. {
  945. left = (value*2.0f)-1.0f;
  946. right = 1.0f;
  947. }
  948. else
  949. {
  950. left = -1.0f;
  951. right = 1.0f;
  952. }
  953. setBalanceLeft(left, false, false);
  954. setBalanceRight(right, false, false);
  955. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  956. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  957. break;
  958. }
  959. }
  960. #endif
  961. // Control plugin parameters
  962. uint32_t k;
  963. for (k=0; k < pData->param.count; ++k)
  964. {
  965. if (pData->param.data[k].midiChannel != event.channel)
  966. continue;
  967. if (pData->param.data[k].midiCC != ctrlEvent.param)
  968. continue;
  969. if (pData->param.data[k].type != PARAMETER_INPUT)
  970. continue;
  971. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  972. continue;
  973. float value;
  974. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  975. {
  976. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  977. }
  978. else
  979. {
  980. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  981. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  982. value = std::rint(value);
  983. }
  984. setParameterValue(k, value, false, false, false);
  985. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  986. break;
  987. }
  988. // check if event is already handled
  989. if (k != pData->param.count)
  990. break;
  991. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  992. {
  993. if (midiEventCount >= kPluginMaxMidiEvents)
  994. continue;
  995. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  996. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  997. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  998. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  999. midiEvent.data.control.channel = event.channel;
  1000. midiEvent.data.control.param = ctrlEvent.param;
  1001. midiEvent.data.control.value = int8_t(ctrlEvent.value*127.0f);
  1002. }
  1003. break;
  1004. } // case kEngineControlEventTypeParameter
  1005. case kEngineControlEventTypeMidiBank:
  1006. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1007. nextBankId = ctrlEvent.param;
  1008. break;
  1009. case kEngineControlEventTypeMidiProgram:
  1010. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1011. {
  1012. const uint32_t nextProgramId = ctrlEvent.param;
  1013. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1014. {
  1015. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1016. {
  1017. const int32_t index(static_cast<int32_t>(k));
  1018. setMidiProgram(index, false, false, false);
  1019. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  1020. break;
  1021. }
  1022. }
  1023. }
  1024. break;
  1025. case kEngineControlEventTypeAllSoundOff:
  1026. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1027. {
  1028. if (midiEventCount >= kPluginMaxMidiEvents)
  1029. continue;
  1030. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1031. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1032. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1033. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1034. midiEvent.data.control.channel = event.channel;
  1035. midiEvent.data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1036. }
  1037. break;
  1038. case kEngineControlEventTypeAllNotesOff:
  1039. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1040. {
  1041. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1042. {
  1043. allNotesOffSent = true;
  1044. sendMidiAllNotesOffToCallback();
  1045. }
  1046. if (midiEventCount >= kPluginMaxMidiEvents)
  1047. continue;
  1048. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1049. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1050. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1051. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1052. midiEvent.data.control.channel = event.channel;
  1053. midiEvent.data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1054. midiEventCount += 1;
  1055. }
  1056. break;
  1057. } // switch (ctrlEvent.type)
  1058. break;
  1059. } // case kEngineEventTypeControl
  1060. case kEngineEventTypeMidi: {
  1061. if (midiEventCount >= kPluginMaxMidiEvents)
  1062. continue;
  1063. const EngineMidiEvent& engineEvent(event.midi);
  1064. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(engineEvent.data));
  1065. uint8_t channel = event.channel;
  1066. // Fix bad note-off (per DSSI spec)
  1067. if (MIDI_IS_STATUS_NOTE_ON(status) && engineEvent.data[2] == 0)
  1068. status = MIDI_STATUS_NOTE_OFF;
  1069. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount]);
  1070. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1071. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1072. switch (status)
  1073. {
  1074. case MIDI_STATUS_NOTE_OFF: {
  1075. const uint8_t note = engineEvent.data[1];
  1076. midiEvent.type = SND_SEQ_EVENT_NOTEOFF;
  1077. midiEvent.data.note.channel = channel;
  1078. midiEvent.data.note.note = note;
  1079. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1080. break;
  1081. }
  1082. case MIDI_STATUS_NOTE_ON: {
  1083. const uint8_t note = engineEvent.data[1];
  1084. const uint8_t velo = engineEvent.data[2];
  1085. midiEvent.type = SND_SEQ_EVENT_NOTEON;
  1086. midiEvent.data.note.channel = channel;
  1087. midiEvent.data.note.note = note;
  1088. midiEvent.data.note.velocity = velo;
  1089. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1090. break;
  1091. }
  1092. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1093. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1094. {
  1095. const uint8_t note = engineEvent.data[1];
  1096. const uint8_t pressure = engineEvent.data[2];
  1097. midiEvent.type = SND_SEQ_EVENT_KEYPRESS;
  1098. midiEvent.data.note.channel = channel;
  1099. midiEvent.data.note.note = note;
  1100. midiEvent.data.note.velocity = pressure;
  1101. }
  1102. break;
  1103. case MIDI_STATUS_CONTROL_CHANGE:
  1104. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1105. {
  1106. const uint8_t control = engineEvent.data[1];
  1107. const uint8_t value = engineEvent.data[2];
  1108. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1109. midiEvent.data.control.channel = channel;
  1110. midiEvent.data.control.param = control;
  1111. midiEvent.data.control.value = value;
  1112. }
  1113. break;
  1114. case MIDI_STATUS_CHANNEL_PRESSURE:
  1115. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1116. {
  1117. const uint8_t pressure = engineEvent.data[1];
  1118. midiEvent.type = SND_SEQ_EVENT_CHANPRESS;
  1119. midiEvent.data.control.channel = channel;
  1120. midiEvent.data.control.value = pressure;
  1121. }
  1122. break;
  1123. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1124. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1125. {
  1126. const uint8_t lsb = engineEvent.data[1];
  1127. const uint8_t msb = engineEvent.data[2];
  1128. midiEvent.type = SND_SEQ_EVENT_PITCHBEND;
  1129. midiEvent.data.control.channel = channel;
  1130. midiEvent.data.control.value = ((msb << 7) | lsb) - 8192;
  1131. }
  1132. break;
  1133. default:
  1134. continue;
  1135. break;
  1136. } // switch (status)
  1137. midiEventCount += 1;
  1138. break;
  1139. } // case kEngineEventTypeMidi
  1140. } // switch (event.type)
  1141. }
  1142. pData->postRtEvents.trySplice();
  1143. if (frames > timeOffset)
  1144. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1145. } // End of Event Input and Processing
  1146. // --------------------------------------------------------------------------------------------------------
  1147. // Plugin processing (no events)
  1148. else
  1149. {
  1150. processSingle(inBuffer, outBuffer, frames, 0, midiEventCount);
  1151. } // End of Plugin processing (no events)
  1152. CARLA_PROCESS_CONTINUE_CHECK;
  1153. // --------------------------------------------------------------------------------------------------------
  1154. // Control Output
  1155. if (pData->event.portOut != nullptr)
  1156. {
  1157. uint8_t channel;
  1158. uint16_t param;
  1159. float value;
  1160. for (uint32_t k=0; k < pData->param.count; ++k)
  1161. {
  1162. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1163. continue;
  1164. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1165. if (pData->param.data[k].midiCC > 0)
  1166. {
  1167. channel = pData->param.data[k].midiChannel;
  1168. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1169. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1170. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1171. }
  1172. }
  1173. } // End of Control Output
  1174. }
  1175. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const ulong midiEventCount)
  1176. {
  1177. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1178. if (pData->audioIn.count > 0)
  1179. {
  1180. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1181. }
  1182. if (pData->audioOut.count > 0)
  1183. {
  1184. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1185. }
  1186. // --------------------------------------------------------------------------------------------------------
  1187. // Try lock, silence otherwise
  1188. if (pData->engine->isOffline())
  1189. {
  1190. pData->singleMutex.lock();
  1191. }
  1192. else if (! pData->singleMutex.tryLock())
  1193. {
  1194. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1195. {
  1196. for (uint32_t k=0; k < frames; ++k)
  1197. outBuffer[i][k+timeOffset] = 0.0f;
  1198. }
  1199. return false;
  1200. }
  1201. // --------------------------------------------------------------------------------------------------------
  1202. // Reset audio buffers
  1203. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1204. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1205. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1206. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  1207. // --------------------------------------------------------------------------------------------------------
  1208. // Run plugin
  1209. if (fDssiDescriptor->run_synth != nullptr)
  1210. {
  1211. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1212. if (fHandle2 != nullptr)
  1213. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1214. }
  1215. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1216. {
  1217. ulong instances = (fHandle2 != nullptr) ? 2 : 1;
  1218. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1219. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1220. ulong midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1221. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1222. }
  1223. else
  1224. {
  1225. fDescriptor->run(fHandle, frames);
  1226. if (fHandle2 != nullptr)
  1227. fDescriptor->run(fHandle2, frames);
  1228. }
  1229. #ifndef BUILD_BRIDGE
  1230. // --------------------------------------------------------------------------------------------------------
  1231. // Post-processing (dry/wet, volume and balance)
  1232. {
  1233. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1234. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1235. bool isPair;
  1236. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1237. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1238. {
  1239. // Dry/Wet
  1240. if (doDryWet)
  1241. {
  1242. for (uint32_t k=0; k < frames; ++k)
  1243. {
  1244. // TODO
  1245. //if (k < pData->latency && pData->latency < frames)
  1246. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  1247. //else
  1248. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1249. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1250. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1251. }
  1252. }
  1253. // Balance
  1254. if (doBalance)
  1255. {
  1256. isPair = (i % 2 == 0);
  1257. if (isPair)
  1258. {
  1259. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1260. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1261. }
  1262. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1263. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1264. for (uint32_t k=0; k < frames; ++k)
  1265. {
  1266. if (isPair)
  1267. {
  1268. // left
  1269. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1270. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1271. }
  1272. else
  1273. {
  1274. // right
  1275. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1276. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1277. }
  1278. }
  1279. }
  1280. // Volume (and buffer copy)
  1281. {
  1282. for (uint32_t k=0; k < frames; ++k)
  1283. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1284. }
  1285. }
  1286. #if 0
  1287. // Latency, save values for next callback, TODO
  1288. if (pData->latency > 0 && pData->latency < frames)
  1289. {
  1290. for (i=0; i < pData->audioIn.count; ++i)
  1291. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i] + (frames - pData->latency), pData->latency);
  1292. }
  1293. #endif
  1294. } // End of Post-processing
  1295. #else // BUILD_BRIDGE
  1296. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1297. {
  1298. for (uint32_t k=0; k < frames; ++k)
  1299. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1300. }
  1301. #endif
  1302. // --------------------------------------------------------------------------------------------------------
  1303. pData->singleMutex.unlock();
  1304. return true;
  1305. }
  1306. void bufferSizeChanged(const uint32_t newBufferSize) override
  1307. {
  1308. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1309. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1310. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1311. {
  1312. if (fAudioInBuffers[i] != nullptr)
  1313. delete[] fAudioInBuffers[i];
  1314. fAudioInBuffers[i] = new float[newBufferSize];
  1315. }
  1316. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1317. {
  1318. if (fAudioOutBuffers[i] != nullptr)
  1319. delete[] fAudioOutBuffers[i];
  1320. fAudioOutBuffers[i] = new float[newBufferSize];
  1321. }
  1322. if (fHandle2 == nullptr)
  1323. {
  1324. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1325. {
  1326. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1327. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1328. }
  1329. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1330. {
  1331. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1332. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1333. }
  1334. }
  1335. else
  1336. {
  1337. if (pData->audioIn.count > 0)
  1338. {
  1339. CARLA_ASSERT(pData->audioIn.count == 2);
  1340. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1341. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1342. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1343. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1344. }
  1345. if (pData->audioOut.count > 0)
  1346. {
  1347. CARLA_ASSERT(pData->audioOut.count == 2);
  1348. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1349. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1350. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1351. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1352. }
  1353. }
  1354. carla_debug("DssiPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1355. }
  1356. void sampleRateChanged(const double newSampleRate) override
  1357. {
  1358. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1359. carla_debug("DssiPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1360. // TODO
  1361. (void)newSampleRate;
  1362. carla_debug("DssiPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1363. }
  1364. // -------------------------------------------------------------------
  1365. // Plugin buffers
  1366. void clearBuffers() noexcept override
  1367. {
  1368. carla_debug("DssiPlugin::clearBuffers() - start");
  1369. if (fAudioInBuffers != nullptr)
  1370. {
  1371. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1372. {
  1373. if (fAudioInBuffers[i] != nullptr)
  1374. {
  1375. delete[] fAudioInBuffers[i];
  1376. fAudioInBuffers[i] = nullptr;
  1377. }
  1378. }
  1379. delete[] fAudioInBuffers;
  1380. fAudioInBuffers = nullptr;
  1381. }
  1382. if (fAudioOutBuffers != nullptr)
  1383. {
  1384. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1385. {
  1386. if (fAudioOutBuffers[i] != nullptr)
  1387. {
  1388. delete[] fAudioOutBuffers[i];
  1389. fAudioOutBuffers[i] = nullptr;
  1390. }
  1391. }
  1392. delete[] fAudioOutBuffers;
  1393. fAudioOutBuffers = nullptr;
  1394. }
  1395. if (fParamBuffers != nullptr)
  1396. {
  1397. delete[] fParamBuffers;
  1398. fParamBuffers = nullptr;
  1399. }
  1400. CarlaPlugin::clearBuffers();
  1401. carla_debug("DssiPlugin::clearBuffers() - end");
  1402. }
  1403. // -------------------------------------------------------------------
  1404. // Post-poned UI Stuff
  1405. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1406. {
  1407. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1408. if (pData->osc.data.target == nullptr)
  1409. return;
  1410. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  1411. }
  1412. void uiMidiProgramChange(const uint32_t index) noexcept override
  1413. {
  1414. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1415. if (pData->osc.data.target == nullptr)
  1416. return;
  1417. osc_send_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1418. }
  1419. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1420. {
  1421. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1422. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1423. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1424. if (pData->osc.data.target == nullptr)
  1425. return;
  1426. #if 0
  1427. uint8_t midiData[4];
  1428. midiData[0] = 0;
  1429. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  1430. midiData[2] = note;
  1431. midiData[3] = velo;
  1432. osc_send_midi(pData->osc.data, midiData);
  1433. #endif
  1434. }
  1435. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1436. {
  1437. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1438. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1439. if (pData->osc.data.target == nullptr)
  1440. return;
  1441. #if 0
  1442. uint8_t midiData[4];
  1443. midiData[0] = 0;
  1444. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  1445. midiData[2] = note;
  1446. midiData[3] = 0;
  1447. osc_send_midi(pData->osc.data, midiData);
  1448. #endif
  1449. }
  1450. // -------------------------------------------------------------------
  1451. const void* getExtraStuff() const noexcept override
  1452. {
  1453. return fUiFilename;
  1454. }
  1455. bool init(const char* const filename, const char* const name, const char* const label)
  1456. {
  1457. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1458. // ---------------------------------------------------------------
  1459. // first checks
  1460. if (pData->client != nullptr)
  1461. {
  1462. pData->engine->setLastError("Plugin client is already registered");
  1463. return false;
  1464. }
  1465. if (filename == nullptr || filename[0] == '\0')
  1466. {
  1467. pData->engine->setLastError("null filename");
  1468. return false;
  1469. }
  1470. if (label == nullptr || label[0] == '\0')
  1471. {
  1472. pData->engine->setLastError("null label");
  1473. return false;
  1474. }
  1475. // ---------------------------------------------------------------
  1476. // open DLL
  1477. if (! pData->libOpen(filename))
  1478. {
  1479. pData->engine->setLastError(pData->libError(filename));
  1480. return false;
  1481. }
  1482. // ---------------------------------------------------------------
  1483. // get DLL main entry
  1484. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)pData->libSymbol("dssi_descriptor");
  1485. if (descFn == nullptr)
  1486. {
  1487. pData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1488. return false;
  1489. }
  1490. // ---------------------------------------------------------------
  1491. // get descriptor that matches label
  1492. ulong i = 0;
  1493. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1494. {
  1495. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1496. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1497. break;
  1498. }
  1499. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1500. {
  1501. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1502. return false;
  1503. }
  1504. // ---------------------------------------------------------------
  1505. // check if uses global instance
  1506. if (fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  1507. {
  1508. if (! addUniqueMultiSynth(fDescriptor->Label))
  1509. {
  1510. pData->engine->setLastError("This plugin uses a global instance and can't be used more than once safely");
  1511. return false;
  1512. }
  1513. }
  1514. // ---------------------------------------------------------------
  1515. // get info
  1516. if (name != nullptr && name[0] != '\0')
  1517. pData->name = pData->engine->getUniquePluginName(name);
  1518. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1519. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1520. else
  1521. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1522. pData->filename = carla_strdup(filename);
  1523. // ---------------------------------------------------------------
  1524. // register client
  1525. pData->client = pData->engine->addClient(this);
  1526. if (pData->client == nullptr || ! pData->client->isOk())
  1527. {
  1528. pData->engine->setLastError("Failed to register plugin client");
  1529. return false;
  1530. }
  1531. // ---------------------------------------------------------------
  1532. // initialize plugin
  1533. try {
  1534. fHandle = fDescriptor->instantiate(fDescriptor, (ulong)pData->engine->getSampleRate());
  1535. } catch(...) {}
  1536. if (fHandle == nullptr)
  1537. {
  1538. pData->engine->setLastError("Plugin failed to initialize");
  1539. return false;
  1540. }
  1541. // ---------------------------------------------------------------
  1542. // check for custom data extension
  1543. if (fDssiDescriptor->configure != nullptr)
  1544. {
  1545. if (char* const error = fDssiDescriptor->configure(fHandle, DSSI_CUSTOMDATA_EXTENSION_KEY, ""))
  1546. {
  1547. if (std::strcmp(error, "true") == 0 && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  1548. fUsesCustomData = true;
  1549. std::free(error);
  1550. }
  1551. }
  1552. // ---------------------------------------------------------------
  1553. // gui stuff
  1554. if (const char* const guiFilename = find_dssi_ui(filename, fDescriptor->Label))
  1555. {
  1556. pData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1557. fUiFilename = guiFilename;
  1558. }
  1559. // ---------------------------------------------------------------
  1560. // load plugin settings
  1561. {
  1562. #ifdef __USE_GNU
  1563. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  1564. #else
  1565. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  1566. #endif
  1567. // set default options
  1568. pData->options = 0x0;
  1569. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1570. if (isDssiVst)
  1571. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1572. if (pData->engine->getOptions().forceStereo)
  1573. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1574. if (fUsesCustomData)
  1575. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1576. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  1577. {
  1578. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1579. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1580. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1581. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1582. if (fDssiDescriptor->run_synth == nullptr)
  1583. carla_stderr("WARNING: Plugin can ONLY use run_multiple_synths!");
  1584. }
  1585. #ifndef BUILD_BRIDGE
  1586. // set identifier string
  1587. CarlaString identifier("DSSI/");
  1588. if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1589. {
  1590. identifier += shortname+1;
  1591. identifier += ",";
  1592. }
  1593. identifier += label;
  1594. pData->identifier = identifier.dup();
  1595. // load settings
  1596. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1597. // ignore settings, we need this anyway
  1598. if (isDssiVst)
  1599. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1600. #endif
  1601. }
  1602. return true;
  1603. }
  1604. // -------------------------------------------------------------------
  1605. private:
  1606. LADSPA_Handle fHandle;
  1607. LADSPA_Handle fHandle2;
  1608. const LADSPA_Descriptor* fDescriptor;
  1609. const DSSI_Descriptor* fDssiDescriptor;
  1610. bool fUsesCustomData;
  1611. const char* fUiFilename;
  1612. float** fAudioInBuffers;
  1613. float** fAudioOutBuffers;
  1614. float* fParamBuffers;
  1615. snd_seq_event_t fMidiEvents[kPluginMaxMidiEvents];
  1616. // -------------------------------------------------------------------
  1617. static LinkedList<const char*> sMultiSynthList;
  1618. static bool addUniqueMultiSynth(const char* const label) noexcept
  1619. {
  1620. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', false);
  1621. const char* dlabel = nullptr;
  1622. try {
  1623. dlabel = carla_strdup(label);
  1624. } catch(...) { return false; }
  1625. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1626. {
  1627. const char* const itLabel(it.getValue());
  1628. if (std::strcmp(dlabel, itLabel) == 0)
  1629. {
  1630. delete[] dlabel;
  1631. return false;
  1632. }
  1633. }
  1634. return sMultiSynthList.append(dlabel);
  1635. }
  1636. static void removeUniqueMultiSynth(const char* const label) noexcept
  1637. {
  1638. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0',);
  1639. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1640. {
  1641. const char* const itLabel(it.getValue());
  1642. if (std::strcmp(label, itLabel) == 0)
  1643. {
  1644. sMultiSynthList.remove(it);
  1645. delete[] itLabel;
  1646. break;
  1647. }
  1648. }
  1649. }
  1650. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1651. };
  1652. LinkedList<const char*> DssiPlugin::sMultiSynthList;
  1653. CARLA_BACKEND_END_NAMESPACE
  1654. #endif // WANT_DSSI
  1655. // -------------------------------------------------------------------------------------------------------------------
  1656. CARLA_BACKEND_START_NAMESPACE
  1657. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  1658. {
  1659. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1660. #ifdef WANT_DSSI
  1661. DssiPlugin* const plugin(new DssiPlugin(init.engine, init.id));
  1662. if (! plugin->init(init.filename, init.name, init.label))
  1663. {
  1664. delete plugin;
  1665. return nullptr;
  1666. }
  1667. plugin->reload();
  1668. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1669. {
  1670. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1671. delete plugin;
  1672. return nullptr;
  1673. }
  1674. return plugin;
  1675. #else
  1676. init.engine->setLastError("DSSI support not available");
  1677. return nullptr;
  1678. #endif
  1679. }
  1680. CARLA_BACKEND_END_NAMESPACE
  1681. // -------------------------------------------------------------------------------------------------------------------