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.

2088 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)
  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() 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. fDescriptor->cleanup(fHandle);
  73. if (fHandle2 != nullptr)
  74. fDescriptor->cleanup(fHandle2);
  75. }
  76. fHandle = nullptr;
  77. fHandle2 = nullptr;
  78. fDescriptor = nullptr;
  79. fDssiDescriptor = nullptr;
  80. }
  81. if (fUiFilename != nullptr)
  82. {
  83. delete[] fUiFilename;
  84. fUiFilename = nullptr;
  85. }
  86. clearBuffers();
  87. }
  88. // -------------------------------------------------------------------
  89. // Information (base)
  90. PluginType getType() const noexcept override
  91. {
  92. return PLUGIN_DSSI;
  93. }
  94. PluginCategory getCategory() const noexcept override
  95. {
  96. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  97. if (pData->audioIn.count == 0 && pData->audioOut.count > 0 && (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr))
  98. return PLUGIN_CATEGORY_SYNTH;
  99. return CarlaPlugin::getCategory();
  100. }
  101. int64_t getUniqueId() const noexcept override
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  104. return static_cast<int64_t>(fDescriptor->UniqueID);
  105. }
  106. // -------------------------------------------------------------------
  107. // Information (count)
  108. // nothing
  109. // -------------------------------------------------------------------
  110. // Information (current data)
  111. int32_t getChunkData(void** const dataPtr) const noexcept override
  112. {
  113. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData, 0);
  114. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  115. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0);
  116. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->get_custom_data != nullptr, 0);
  117. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  118. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr, 0);
  119. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  120. int ret = 0;
  121. ulong dataSize = 0;
  122. try {
  123. ret = fDssiDescriptor->get_custom_data(fHandle, dataPtr, &dataSize);
  124. } catch(...) {}
  125. return (ret != 0) ? static_cast<int32_t>(dataSize) : 0;
  126. }
  127. // -------------------------------------------------------------------
  128. // Information (per-plugin data)
  129. uint getOptionsAvailable() const noexcept override
  130. {
  131. #ifdef __USE_GNU
  132. const bool isAmSynth(strcasestr(pData->filename, "amsynth"));
  133. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst"));
  134. #else
  135. const bool isAmSynth(std::strstr(pData->filename, "amsynth"));
  136. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst"));
  137. #endif
  138. uint options = 0x0;
  139. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  140. if (! (isAmSynth || isDssiVst))
  141. {
  142. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  143. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  144. {
  145. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  146. options |= PLUGIN_OPTION_FORCE_STEREO;
  147. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  148. options |= PLUGIN_OPTION_FORCE_STEREO;
  149. }
  150. }
  151. if (fUsesCustomData)
  152. options |= PLUGIN_OPTION_USE_CHUNKS;
  153. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  154. {
  155. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  156. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  157. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  158. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  159. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  160. }
  161. return options;
  162. }
  163. float getParameterValue(const uint32_t parameterId) const noexcept override
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  166. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  167. return fParamBuffers[parameterId];
  168. }
  169. void getLabel(char* const strBuf) const noexcept override
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  172. if (fDescriptor->Label != nullptr)
  173. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  174. else
  175. CarlaPlugin::getLabel(strBuf);
  176. }
  177. void getMaker(char* const strBuf) const noexcept override
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  180. if (fDescriptor->Maker != nullptr)
  181. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  182. else
  183. CarlaPlugin::getMaker(strBuf);
  184. }
  185. void getCopyright(char* const strBuf) const noexcept override
  186. {
  187. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  188. if (fDescriptor->Copyright != nullptr)
  189. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  190. else
  191. CarlaPlugin::getCopyright(strBuf);
  192. }
  193. void getRealName(char* const strBuf) const noexcept override
  194. {
  195. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  196. if (fDescriptor->Name != nullptr)
  197. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  198. else
  199. CarlaPlugin::getRealName(strBuf);
  200. }
  201. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  202. {
  203. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  204. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  205. const int32_t rindex(pData->param.data[parameterId].rindex);
  206. if (rindex < static_cast<int32_t>(fDescriptor->PortCount))
  207. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  208. else
  209. CarlaPlugin::getParameterName(parameterId, strBuf);
  210. }
  211. // -------------------------------------------------------------------
  212. // Set data (state)
  213. // nothing
  214. // -------------------------------------------------------------------
  215. // Set data (internal stuff)
  216. // nothing
  217. // -------------------------------------------------------------------
  218. // Set data (plugin-specific stuff)
  219. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  220. {
  221. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  222. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  223. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  224. fParamBuffers[parameterId] = fixedValue;
  225. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  226. }
  227. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  228. {
  229. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  230. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  231. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  232. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  233. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  234. carla_debug("DssiPlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  235. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  236. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  237. if (fDssiDescriptor->configure != nullptr)
  238. {
  239. try {
  240. fDssiDescriptor->configure(fHandle, key, value);
  241. } catch(...) {}
  242. if (fHandle2 != nullptr)
  243. {
  244. try {
  245. fDssiDescriptor->configure(fHandle2, key, value);
  246. } catch(...) {}
  247. }
  248. }
  249. if (sendGui && pData->osc.data.target != nullptr)
  250. osc_send_configure(pData->osc.data, key, value);
  251. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  252. {
  253. const ScopedSingleProcessLocker spl(this, true);
  254. reloadPrograms(false);
  255. }
  256. CarlaPlugin::setCustomData(type, key, value, sendGui);
  257. }
  258. void setChunkData(const char* const stringData) override
  259. {
  260. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData,);
  261. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  262. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  263. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->set_custom_data != nullptr,);
  264. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  265. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr,);
  266. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr,);
  267. QByteArray chunk(QByteArray::fromBase64(stringData));
  268. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0,);
  269. const ScopedSingleProcessLocker spl(this, true);
  270. try {
  271. fDssiDescriptor->set_custom_data(fHandle, chunk.data(), static_cast<ulong>(chunk.size()));
  272. } catch(...) {}
  273. }
  274. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  275. {
  276. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  277. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  278. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  279. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  280. if (index >= 0)
  281. {
  282. const uint32_t bank(pData->midiprog.data[index].bank);
  283. const uint32_t program(pData->midiprog.data[index].program);
  284. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  285. try {
  286. fDssiDescriptor->select_program(fHandle, bank, program);
  287. } catch(...) {}
  288. if (fHandle2 != nullptr)
  289. {
  290. try {
  291. fDssiDescriptor->select_program(fHandle2, bank, program);
  292. } catch(...) {}
  293. }
  294. }
  295. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  296. }
  297. // -------------------------------------------------------------------
  298. // Set ui stuff
  299. void showCustomUI(const bool yesNo) override
  300. {
  301. if (yesNo)
  302. {
  303. pData->osc.data.free();
  304. pData->osc.thread.startThread();
  305. }
  306. else
  307. {
  308. pData->transientTryCounter = 0;
  309. if (pData->osc.data.target != nullptr)
  310. {
  311. osc_send_hide(pData->osc.data);
  312. osc_send_quit(pData->osc.data);
  313. pData->osc.data.free();
  314. }
  315. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  316. }
  317. }
  318. // -------------------------------------------------------------------
  319. // Plugin state
  320. void reload() override
  321. {
  322. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  323. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  324. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  325. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  326. carla_debug("DssiPlugin::reload() - start");
  327. const EngineProcessMode processMode(pData->engine->getProccessMode());
  328. // Safely disable plugin for reload
  329. const ScopedDisabler sd(this);
  330. if (pData->active)
  331. deactivate();
  332. clearBuffers();
  333. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  334. const uint32_t portCount(static_cast<uint32_t>(fDescriptor->PortCount));
  335. uint32_t aIns, aOuts, mIns, params;
  336. aIns = aOuts = mIns = params = 0;
  337. bool forcedStereoIn, forcedStereoOut;
  338. forcedStereoIn = forcedStereoOut = false;
  339. bool needsCtrlIn, needsCtrlOut;
  340. needsCtrlIn = needsCtrlOut = false;
  341. if (portCount > 0)
  342. {
  343. CARLA_ASSERT(fDescriptor->PortDescriptors != nullptr);
  344. CARLA_ASSERT(fDescriptor->PortRangeHints != nullptr);
  345. CARLA_ASSERT(fDescriptor->PortNames != nullptr);
  346. for (uint32_t i=0; i < portCount; ++i)
  347. {
  348. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  349. if (LADSPA_IS_PORT_AUDIO(portType))
  350. {
  351. if (LADSPA_IS_PORT_INPUT(portType))
  352. aIns += 1;
  353. else if (LADSPA_IS_PORT_OUTPUT(portType))
  354. aOuts += 1;
  355. }
  356. else if (LADSPA_IS_PORT_CONTROL(portType))
  357. params += 1;
  358. }
  359. }
  360. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  361. {
  362. if (fHandle2 == nullptr)
  363. {
  364. try {
  365. fHandle2 = fDescriptor->instantiate(fDescriptor, (ulong)sampleRate);
  366. } catch(...) {}
  367. }
  368. if (fHandle2 != nullptr)
  369. {
  370. if (aIns == 1)
  371. {
  372. aIns = 2;
  373. forcedStereoIn = true;
  374. }
  375. if (aOuts == 1)
  376. {
  377. aOuts = 2;
  378. forcedStereoOut = true;
  379. }
  380. }
  381. }
  382. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  383. {
  384. mIns = 1;
  385. needsCtrlIn = true;
  386. }
  387. if (aIns > 0)
  388. {
  389. pData->audioIn.createNew(aIns);
  390. fAudioInBuffers = new float*[aIns];
  391. for (uint32_t i=0; i < aIns; ++i)
  392. fAudioInBuffers[i] = nullptr;
  393. }
  394. if (aOuts > 0)
  395. {
  396. pData->audioOut.createNew(aOuts);
  397. fAudioOutBuffers = new float*[aOuts];
  398. needsCtrlIn = true;
  399. for (uint32_t i=0; i < aOuts; ++i)
  400. fAudioOutBuffers[i] = nullptr;
  401. }
  402. if (params > 0)
  403. {
  404. pData->param.createNew(params, true);
  405. fParamBuffers = new float[params];
  406. FLOAT_CLEAR(fParamBuffers, params);
  407. }
  408. const uint portNameSize(pData->engine->getMaxPortNameSize());
  409. CarlaString portName;
  410. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  411. {
  412. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  413. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  414. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  415. if (LADSPA_IS_PORT_AUDIO(portType))
  416. {
  417. portName.clear();
  418. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  419. {
  420. portName = pData->name;
  421. portName += ":";
  422. }
  423. portName += fDescriptor->PortNames[i];
  424. portName.truncate(portNameSize);
  425. if (LADSPA_IS_PORT_INPUT(portType))
  426. {
  427. uint32_t j = iAudioIn++;
  428. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  429. pData->audioIn.ports[j].rindex = i;
  430. if (forcedStereoIn)
  431. {
  432. portName += "_2";
  433. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  434. pData->audioIn.ports[1].rindex = i;
  435. }
  436. }
  437. else if (LADSPA_IS_PORT_OUTPUT(portType))
  438. {
  439. uint32_t j = iAudioOut++;
  440. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  441. pData->audioOut.ports[j].rindex = i;
  442. if (forcedStereoOut)
  443. {
  444. portName += "_2";
  445. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  446. pData->audioOut.ports[1].rindex = i;
  447. }
  448. }
  449. else
  450. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  451. }
  452. else if (LADSPA_IS_PORT_CONTROL(portType))
  453. {
  454. uint32_t j = iCtrl++;
  455. pData->param.data[j].hints = 0x0;
  456. pData->param.data[j].index = static_cast<int32_t>(j);
  457. pData->param.data[j].rindex = static_cast<int32_t>(i);
  458. pData->param.data[j].midiCC = -1;
  459. pData->param.data[j].midiChannel = 0;
  460. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  461. float min, max, def, step, stepSmall, stepLarge;
  462. // min value
  463. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  464. min = portRangeHints.LowerBound;
  465. else
  466. min = 0.0f;
  467. // max value
  468. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  469. max = portRangeHints.UpperBound;
  470. else
  471. max = 1.0f;
  472. if (min > max)
  473. max = min;
  474. if (max - min == 0.0f)
  475. {
  476. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  477. max = min + 0.1f;
  478. }
  479. // default value
  480. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  481. if (def < min)
  482. def = min;
  483. else if (def > max)
  484. def = max;
  485. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  486. {
  487. min *= sampleRate;
  488. max *= sampleRate;
  489. def *= sampleRate;
  490. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  491. }
  492. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  493. {
  494. step = max - min;
  495. stepSmall = step;
  496. stepLarge = step;
  497. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  498. }
  499. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  500. {
  501. step = 1.0f;
  502. stepSmall = 1.0f;
  503. stepLarge = 10.0f;
  504. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  505. }
  506. else
  507. {
  508. float range = max - min;
  509. step = range/100.0f;
  510. stepSmall = range/1000.0f;
  511. stepLarge = range/10.0f;
  512. }
  513. if (LADSPA_IS_PORT_INPUT(portType))
  514. {
  515. pData->param.data[j].type = PARAMETER_INPUT;
  516. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  517. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  518. needsCtrlIn = true;
  519. // MIDI CC value
  520. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  521. {
  522. int controller = fDssiDescriptor->get_midi_controller_for_port(fHandle, i);
  523. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  524. {
  525. int16_t cc = DSSI_CC_NUMBER(controller);
  526. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  527. pData->param.data[j].midiCC = cc;
  528. }
  529. }
  530. }
  531. else if (LADSPA_IS_PORT_OUTPUT(portType))
  532. {
  533. pData->param.data[j].type = PARAMETER_OUTPUT;
  534. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  535. {
  536. min = 0.0f;
  537. max = sampleRate;
  538. def = 0.0f;
  539. step = 1.0f;
  540. stepSmall = 1.0f;
  541. stepLarge = 1.0f;
  542. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  543. }
  544. else
  545. {
  546. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  547. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  548. needsCtrlOut = true;
  549. }
  550. }
  551. else
  552. {
  553. pData->param.data[j].type = PARAMETER_UNKNOWN;
  554. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  555. }
  556. // extra parameter hints
  557. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  558. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  559. pData->param.ranges[j].min = min;
  560. pData->param.ranges[j].max = max;
  561. pData->param.ranges[j].def = def;
  562. pData->param.ranges[j].step = step;
  563. pData->param.ranges[j].stepSmall = stepSmall;
  564. pData->param.ranges[j].stepLarge = stepLarge;
  565. // Start parameters in their default values
  566. fParamBuffers[j] = def;
  567. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  568. if (fHandle2 != nullptr)
  569. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  570. }
  571. else
  572. {
  573. // Not Audio or Control
  574. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  575. fDescriptor->connect_port(fHandle, i, nullptr);
  576. if (fHandle2 != nullptr)
  577. fDescriptor->connect_port(fHandle2, i, nullptr);
  578. }
  579. }
  580. if (needsCtrlIn)
  581. {
  582. portName.clear();
  583. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  584. {
  585. portName = pData->name;
  586. portName += ":";
  587. }
  588. portName += "events-in";
  589. portName.truncate(portNameSize);
  590. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  591. }
  592. if (needsCtrlOut)
  593. {
  594. portName.clear();
  595. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  596. {
  597. portName = pData->name;
  598. portName += ":";
  599. }
  600. portName += "events-out";
  601. portName.truncate(portNameSize);
  602. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  603. }
  604. if (forcedStereoIn || forcedStereoOut)
  605. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  606. else
  607. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  608. // plugin hints
  609. pData->hints = 0x0;
  610. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  611. pData->hints |= PLUGIN_IS_RTSAFE;
  612. if (fUiFilename != nullptr)
  613. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  614. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  615. pData->hints |= PLUGIN_CAN_DRYWET;
  616. if (aOuts > 0)
  617. pData->hints |= PLUGIN_CAN_VOLUME;
  618. if (aOuts >= 2 && aOuts % 2 == 0)
  619. pData->hints |= PLUGIN_CAN_BALANCE;
  620. // extra plugin hints
  621. pData->extraHints = 0x0;
  622. if (mIns > 0)
  623. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  624. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  625. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  626. // check latency
  627. if (pData->hints & PLUGIN_CAN_DRYWET)
  628. {
  629. for (uint32_t i=0; i < pData->param.count; ++i)
  630. {
  631. if (pData->param.special[i] != PARAMETER_SPECIAL_LATENCY)
  632. continue;
  633. // we need to pre-run the plugin so it can update its latency control-port
  634. float tmpIn[aIns][2];
  635. float tmpOut[aOuts][2];
  636. for (uint32_t j=0; j < aIns; ++j)
  637. {
  638. tmpIn[j][0] = 0.0f;
  639. tmpIn[j][1] = 0.0f;
  640. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  641. }
  642. for (uint32_t j=0; j < aOuts; ++j)
  643. {
  644. tmpOut[j][0] = 0.0f;
  645. tmpOut[j][1] = 0.0f;
  646. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  647. }
  648. if (fDescriptor->activate != nullptr)
  649. fDescriptor->activate(fHandle);
  650. fDescriptor->run(fHandle, 2);
  651. if (fDescriptor->deactivate != nullptr)
  652. fDescriptor->deactivate(fHandle);
  653. const uint32_t latency = (uint32_t)fParamBuffers[i];
  654. if (pData->latency != latency)
  655. {
  656. pData->latency = latency;
  657. pData->client->setLatency(latency);
  658. pData->recreateLatencyBuffers();
  659. }
  660. break;
  661. }
  662. }
  663. bufferSizeChanged(pData->engine->getBufferSize());
  664. reloadPrograms(true);
  665. if (pData->active)
  666. activate();
  667. carla_debug("DssiPlugin::reload() - end");
  668. }
  669. void reloadPrograms(const bool doInit) override
  670. {
  671. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(doInit));
  672. const uint32_t oldCount = pData->midiprog.count;
  673. const int32_t current = pData->midiprog.current;
  674. // Delete old programs
  675. pData->midiprog.clear();
  676. // Query new programs
  677. uint32_t newCount = 0;
  678. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  679. {
  680. for (; fDssiDescriptor->get_program(fHandle, newCount) != nullptr;)
  681. ++newCount;
  682. }
  683. if (newCount > 0)
  684. {
  685. pData->midiprog.createNew(newCount);
  686. // Update data
  687. for (uint32_t i=0; i < newCount; ++i)
  688. {
  689. const DSSI_Program_Descriptor* const pdesc(fDssiDescriptor->get_program(fHandle, i));
  690. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  691. CARLA_SAFE_ASSERT(pdesc->Name != nullptr);
  692. pData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  693. pData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  694. pData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  695. }
  696. }
  697. #ifndef BUILD_BRIDGE
  698. // Update OSC Names
  699. if (pData->engine->isOscControlRegistered())
  700. {
  701. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  702. for (uint32_t i=0; i < newCount; ++i)
  703. 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);
  704. }
  705. #endif
  706. if (doInit)
  707. {
  708. if (newCount > 0)
  709. setMidiProgram(0, false, false, false);
  710. }
  711. else
  712. {
  713. // Check if current program is invalid
  714. bool programChanged = false;
  715. if (newCount == oldCount+1)
  716. {
  717. // one midi program added, probably created by user
  718. pData->midiprog.current = static_cast<int32_t>(oldCount);
  719. programChanged = true;
  720. }
  721. else if (current < 0 && newCount > 0)
  722. {
  723. // programs exist now, but not before
  724. pData->midiprog.current = 0;
  725. programChanged = true;
  726. }
  727. else if (current >= 0 && newCount == 0)
  728. {
  729. // programs existed before, but not anymore
  730. pData->midiprog.current = -1;
  731. programChanged = true;
  732. }
  733. else if (current >= static_cast<int32_t>(newCount))
  734. {
  735. // current midi program > count
  736. pData->midiprog.current = 0;
  737. programChanged = true;
  738. }
  739. else
  740. {
  741. // no change
  742. pData->midiprog.current = current;
  743. }
  744. if (programChanged)
  745. setMidiProgram(pData->midiprog.current, true, true, true);
  746. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  747. }
  748. }
  749. // -------------------------------------------------------------------
  750. // Plugin processing
  751. void activate() noexcept override
  752. {
  753. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  754. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  755. if (fDescriptor->activate != nullptr)
  756. {
  757. try {
  758. fDescriptor->activate(fHandle);
  759. } catch(...) {}
  760. if (fHandle2 != nullptr)
  761. {
  762. try {
  763. fDescriptor->activate(fHandle2);
  764. } catch(...) {}
  765. }
  766. }
  767. }
  768. void deactivate() noexcept override
  769. {
  770. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  771. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  772. if (fDescriptor->deactivate != nullptr)
  773. {
  774. try {
  775. fDescriptor->deactivate(fHandle);
  776. } catch(...) {}
  777. if (fHandle2 != nullptr)
  778. {
  779. try {
  780. fDescriptor->deactivate(fHandle2);
  781. } catch(...) {}
  782. }
  783. }
  784. }
  785. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  786. {
  787. // --------------------------------------------------------------------------------------------------------
  788. // Check if active
  789. if (! pData->active)
  790. {
  791. // disable any output sound
  792. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  793. FLOAT_CLEAR(outBuffer[i], frames);
  794. return;
  795. }
  796. ulong midiEventCount = 0;
  797. // --------------------------------------------------------------------------------------------------------
  798. // Check if needs reset
  799. if (pData->needsReset)
  800. {
  801. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  802. {
  803. midiEventCount = MAX_MIDI_CHANNELS*2;
  804. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  805. for (uchar i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  806. {
  807. fMidiEvents[i].type = SND_SEQ_EVENT_CONTROLLER;
  808. fMidiEvents[i].data.control.channel = i;
  809. fMidiEvents[i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  810. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  811. fMidiEvents[k+i].data.control.channel = i;
  812. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  813. }
  814. }
  815. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  816. {
  817. midiEventCount = MAX_MIDI_NOTE;
  818. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  819. for (uchar i=0; i < MAX_MIDI_NOTE; ++i)
  820. {
  821. fMidiEvents[i].type = SND_SEQ_EVENT_NOTEOFF;
  822. fMidiEvents[i].data.note.channel = static_cast<uchar>(pData->ctrlChannel);
  823. fMidiEvents[i].data.note.note = i;
  824. }
  825. }
  826. if (pData->latency > 0)
  827. {
  828. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  829. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  830. }
  831. pData->needsReset = false;
  832. }
  833. // --------------------------------------------------------------------------------------------------------
  834. // Event Input and Processing
  835. if (pData->event.portIn != nullptr)
  836. {
  837. // ----------------------------------------------------------------------------------------------------
  838. // MIDI Input (External)
  839. if (pData->extNotes.mutex.tryLock())
  840. {
  841. ExternalMidiNote note = { 0, 0, 0 };
  842. for (; midiEventCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  843. {
  844. note = pData->extNotes.data.getFirst(note, 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 ulong 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. ulong instances = (fHandle2 != nullptr) ? 2 : 1;
  1202. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1203. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1204. ulong 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() noexcept 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. ulong 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, (ulong)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_stderr("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) noexcept
  1603. {
  1604. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', false);
  1605. const char* dlabel = nullptr;
  1606. try {
  1607. dlabel = carla_strdup(label);
  1608. } catch(...) { return false; }
  1609. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1610. {
  1611. const char* const itLabel(it.getValue());
  1612. if (std::strcmp(dlabel, itLabel) == 0)
  1613. {
  1614. delete[] dlabel;
  1615. return false;
  1616. }
  1617. }
  1618. return sMultiSynthList.append(dlabel);
  1619. }
  1620. static void removeUniqueMultiSynth(const char* const label) noexcept
  1621. {
  1622. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0',);
  1623. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1624. {
  1625. const char* const itLabel(it.getValue());
  1626. if (std::strcmp(label, itLabel) == 0)
  1627. {
  1628. sMultiSynthList.remove(it);
  1629. delete[] itLabel;
  1630. break;
  1631. }
  1632. }
  1633. }
  1634. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1635. };
  1636. LinkedList<const char*> DssiPlugin::sMultiSynthList;
  1637. CARLA_BACKEND_END_NAMESPACE
  1638. #endif // WANT_DSSI
  1639. // -------------------------------------------------------------------------------------------------------------------
  1640. CARLA_BACKEND_START_NAMESPACE
  1641. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  1642. {
  1643. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1644. #ifdef WANT_DSSI
  1645. DssiPlugin* const plugin(new DssiPlugin(init.engine, init.id));
  1646. if (! plugin->init(init.filename, init.name, init.label))
  1647. {
  1648. delete plugin;
  1649. return nullptr;
  1650. }
  1651. plugin->reload();
  1652. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1653. {
  1654. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1655. delete plugin;
  1656. return nullptr;
  1657. }
  1658. return plugin;
  1659. #else
  1660. init.engine->setLastError("DSSI support not available");
  1661. return nullptr;
  1662. #endif
  1663. }
  1664. CARLA_BACKEND_END_NAMESPACE
  1665. // -------------------------------------------------------------------------------------------------------------------