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.

2050 lines
73KB

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