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.

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