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.

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