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.

2052 lines
73KB

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