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.

2056 lines
72KB

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