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.

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