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
73KB

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