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.

2059 lines
72KB

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