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.

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