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.

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