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.

2053 lines
73KB

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