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
71KB

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