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.

2032 lines
71KB

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