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.

2101 lines
74KB

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