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.

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