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.

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