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.

2100 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(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 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(), 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(pData->engine->getOptions().uiBridgesTimeout);
  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, j;
  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. 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. 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. j = iCtrl++;
  468. pData->param.data[j].type = PARAMETER_UNKNOWN;
  469. pData->param.data[j].hints = 0x0;
  470. pData->param.data[j].index = j;
  471. pData->param.data[j].rindex = 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 (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 (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. uint32_t i, 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 count = 0;
  703. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  704. {
  705. while (fDssiDescriptor->get_program(fHandle, count))
  706. count++;
  707. }
  708. if (count > 0)
  709. {
  710. pData->midiprog.createNew(count);
  711. // Update data
  712. for (i=0; i < count; ++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, count);
  727. for (i=0; i < count; ++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 (count > 0)
  734. setMidiProgram(0, false, false, false);
  735. }
  736. else
  737. {
  738. // Check if current program is invalid
  739. bool programChanged = false;
  740. if (count == oldCount+1)
  741. {
  742. // one midi program added, probably created by user
  743. pData->midiprog.current = oldCount;
  744. programChanged = true;
  745. }
  746. else if (current < 0 && count > 0)
  747. {
  748. // programs exist now, but not before
  749. pData->midiprog.current = 0;
  750. programChanged = true;
  751. }
  752. else if (current >= 0 && count == 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>(count))
  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 = 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 = 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. setMidiProgram(k, false, false, false);
  1021. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1022. break;
  1023. }
  1024. }
  1025. }
  1026. break;
  1027. case kEngineControlEventTypeAllSoundOff:
  1028. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1029. {
  1030. if (midiEventCount >= kPluginMaxMidiEvents)
  1031. continue;
  1032. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1033. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1034. midiEvent.time.tick = isSampleAccurate ? startTime : time;
  1035. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1036. midiEvent.data.control.channel = event.channel;
  1037. midiEvent.data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1038. }
  1039. break;
  1040. case kEngineControlEventTypeAllNotesOff:
  1041. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1042. {
  1043. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1044. {
  1045. allNotesOffSent = true;
  1046. sendMidiAllNotesOffToCallback();
  1047. }
  1048. if (midiEventCount >= kPluginMaxMidiEvents)
  1049. continue;
  1050. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1051. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1052. midiEvent.time.tick = isSampleAccurate ? startTime : time;
  1053. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1054. midiEvent.data.control.channel = event.channel;
  1055. midiEvent.data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1056. midiEventCount += 1;
  1057. }
  1058. break;
  1059. }
  1060. break;
  1061. }
  1062. case kEngineEventTypeMidi:
  1063. {
  1064. if (midiEventCount >= kPluginMaxMidiEvents)
  1065. continue;
  1066. const EngineMidiEvent& engineEvent(event.midi);
  1067. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(engineEvent.data));
  1068. uint8_t channel = event.channel;
  1069. // Fix bad note-off (per DSSI spec)
  1070. if (MIDI_IS_STATUS_NOTE_ON(status) && engineEvent.data[2] == 0)
  1071. status = MIDI_STATUS_NOTE_OFF;
  1072. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount]);
  1073. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1074. midiEvent.time.tick = isSampleAccurate ? startTime : time;
  1075. switch (status)
  1076. {
  1077. case MIDI_STATUS_NOTE_OFF:
  1078. {
  1079. const uint8_t note = engineEvent.data[1];
  1080. midiEvent.type = SND_SEQ_EVENT_NOTEOFF;
  1081. midiEvent.data.note.channel = channel;
  1082. midiEvent.data.note.note = note;
  1083. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1084. break;
  1085. }
  1086. case MIDI_STATUS_NOTE_ON:
  1087. {
  1088. const uint8_t note = engineEvent.data[1];
  1089. const uint8_t velo = engineEvent.data[2];
  1090. midiEvent.type = SND_SEQ_EVENT_NOTEON;
  1091. midiEvent.data.note.channel = channel;
  1092. midiEvent.data.note.note = note;
  1093. midiEvent.data.note.velocity = velo;
  1094. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1095. break;
  1096. }
  1097. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1098. {
  1099. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1100. {
  1101. const uint8_t note = engineEvent.data[1];
  1102. const uint8_t pressure = engineEvent.data[2];
  1103. midiEvent.type = SND_SEQ_EVENT_KEYPRESS;
  1104. midiEvent.data.note.channel = channel;
  1105. midiEvent.data.note.note = note;
  1106. midiEvent.data.note.velocity = pressure;
  1107. }
  1108. break;
  1109. }
  1110. case MIDI_STATUS_CONTROL_CHANGE:
  1111. {
  1112. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1113. {
  1114. const uint8_t control = engineEvent.data[1];
  1115. const uint8_t value = engineEvent.data[2];
  1116. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1117. midiEvent.data.control.channel = channel;
  1118. midiEvent.data.control.param = control;
  1119. midiEvent.data.control.value = value;
  1120. }
  1121. break;
  1122. }
  1123. case MIDI_STATUS_CHANNEL_PRESSURE:
  1124. {
  1125. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1126. {
  1127. const uint8_t pressure = engineEvent.data[1];
  1128. midiEvent.type = SND_SEQ_EVENT_CHANPRESS;
  1129. midiEvent.data.control.channel = channel;
  1130. midiEvent.data.control.value = pressure;
  1131. }
  1132. break;
  1133. }
  1134. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1135. {
  1136. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1137. {
  1138. const uint8_t lsb = engineEvent.data[1];
  1139. const uint8_t msb = engineEvent.data[2];
  1140. midiEvent.type = SND_SEQ_EVENT_PITCHBEND;
  1141. midiEvent.data.control.channel = channel;
  1142. midiEvent.data.control.value = ((msb << 7) | lsb) - 8192;
  1143. }
  1144. break;
  1145. }
  1146. default:
  1147. continue;
  1148. break;
  1149. }
  1150. midiEventCount += 1;
  1151. break;
  1152. }
  1153. }
  1154. }
  1155. pData->postRtEvents.trySplice();
  1156. if (frames > timeOffset)
  1157. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1158. } // End of Event Input and Processing
  1159. // --------------------------------------------------------------------------------------------------------
  1160. // Plugin processing (no events)
  1161. else
  1162. {
  1163. processSingle(inBuffer, outBuffer, frames, 0, midiEventCount);
  1164. } // End of Plugin processing (no events)
  1165. CARLA_PROCESS_CONTINUE_CHECK;
  1166. // --------------------------------------------------------------------------------------------------------
  1167. // Control Output
  1168. if (pData->event.portOut != nullptr)
  1169. {
  1170. uint8_t channel;
  1171. uint16_t param;
  1172. float value;
  1173. for (uint32_t k=0; k < pData->param.count; ++k)
  1174. {
  1175. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1176. continue;
  1177. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1178. if (pData->param.data[k].midiCC > 0)
  1179. {
  1180. channel = pData->param.data[k].midiChannel;
  1181. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1182. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1183. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1184. }
  1185. }
  1186. } // End of Control Output
  1187. }
  1188. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const unsigned long midiEventCount)
  1189. {
  1190. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1191. if (pData->audioIn.count > 0)
  1192. {
  1193. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1194. }
  1195. if (pData->audioOut.count > 0)
  1196. {
  1197. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1198. }
  1199. // --------------------------------------------------------------------------------------------------------
  1200. // Try lock, silence otherwise
  1201. if (pData->engine->isOffline())
  1202. {
  1203. pData->singleMutex.lock();
  1204. }
  1205. else if (! pData->singleMutex.tryLock())
  1206. {
  1207. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1208. {
  1209. for (uint32_t k=0; k < frames; ++k)
  1210. outBuffer[i][k+timeOffset] = 0.0f;
  1211. }
  1212. return false;
  1213. }
  1214. // --------------------------------------------------------------------------------------------------------
  1215. // Reset audio buffers
  1216. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1217. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1218. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1219. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  1220. // --------------------------------------------------------------------------------------------------------
  1221. // Run plugin
  1222. if (fDssiDescriptor->run_synth != nullptr)
  1223. {
  1224. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1225. if (fHandle2 != nullptr)
  1226. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1227. }
  1228. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1229. {
  1230. unsigned long instances = (fHandle2 != nullptr) ? 2 : 1;
  1231. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1232. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1233. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1234. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1235. }
  1236. else
  1237. {
  1238. fDescriptor->run(fHandle, frames);
  1239. if (fHandle2 != nullptr)
  1240. fDescriptor->run(fHandle2, frames);
  1241. }
  1242. #ifndef BUILD_BRIDGE
  1243. // --------------------------------------------------------------------------------------------------------
  1244. // Post-processing (dry/wet, volume and balance)
  1245. {
  1246. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1247. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1248. bool isPair;
  1249. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1250. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1251. {
  1252. // Dry/Wet
  1253. if (doDryWet)
  1254. {
  1255. for (uint32_t k=0; k < frames; ++k)
  1256. {
  1257. // TODO
  1258. //if (k < pData->latency && pData->latency < frames)
  1259. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  1260. //else
  1261. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1262. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1263. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1264. }
  1265. }
  1266. // Balance
  1267. if (doBalance)
  1268. {
  1269. isPair = (i % 2 == 0);
  1270. if (isPair)
  1271. {
  1272. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1273. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1274. }
  1275. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1276. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1277. for (uint32_t k=0; k < frames; ++k)
  1278. {
  1279. if (isPair)
  1280. {
  1281. // left
  1282. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1283. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1284. }
  1285. else
  1286. {
  1287. // right
  1288. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1289. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1290. }
  1291. }
  1292. }
  1293. // Volume (and buffer copy)
  1294. {
  1295. for (uint32_t k=0; k < frames; ++k)
  1296. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1297. }
  1298. }
  1299. #if 0
  1300. // Latency, save values for next callback, TODO
  1301. if (pData->latency > 0 && pData->latency < frames)
  1302. {
  1303. for (i=0; i < pData->audioIn.count; ++i)
  1304. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i] + (frames - pData->latency), pData->latency);
  1305. }
  1306. #endif
  1307. } // End of Post-processing
  1308. #else // BUILD_BRIDGE
  1309. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1310. {
  1311. for (uint32_t k=0; k < frames; ++k)
  1312. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1313. }
  1314. #endif
  1315. // --------------------------------------------------------------------------------------------------------
  1316. pData->singleMutex.unlock();
  1317. return true;
  1318. }
  1319. void bufferSizeChanged(const uint32_t newBufferSize) override
  1320. {
  1321. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1322. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1323. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1324. {
  1325. if (fAudioInBuffers[i] != nullptr)
  1326. delete[] fAudioInBuffers[i];
  1327. fAudioInBuffers[i] = new float[newBufferSize];
  1328. }
  1329. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1330. {
  1331. if (fAudioOutBuffers[i] != nullptr)
  1332. delete[] fAudioOutBuffers[i];
  1333. fAudioOutBuffers[i] = new float[newBufferSize];
  1334. }
  1335. if (fHandle2 == nullptr)
  1336. {
  1337. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1338. {
  1339. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1340. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1341. }
  1342. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1343. {
  1344. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1345. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1346. }
  1347. }
  1348. else
  1349. {
  1350. if (pData->audioIn.count > 0)
  1351. {
  1352. CARLA_ASSERT(pData->audioIn.count == 2);
  1353. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1354. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1355. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1356. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1357. }
  1358. if (pData->audioOut.count > 0)
  1359. {
  1360. CARLA_ASSERT(pData->audioOut.count == 2);
  1361. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1362. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1363. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1364. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1365. }
  1366. }
  1367. carla_debug("DssiPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1368. }
  1369. void sampleRateChanged(const double newSampleRate) override
  1370. {
  1371. CARLA_ASSERT_INT(newSampleRate > 0.0, (int)newSampleRate);
  1372. carla_debug("DssiPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1373. // TODO
  1374. (void)newSampleRate;
  1375. carla_debug("DssiPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1376. }
  1377. // -------------------------------------------------------------------
  1378. // Plugin buffers
  1379. void clearBuffers() override
  1380. {
  1381. carla_debug("DssiPlugin::clearBuffers() - start");
  1382. if (fAudioInBuffers != nullptr)
  1383. {
  1384. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1385. {
  1386. if (fAudioInBuffers[i] != nullptr)
  1387. {
  1388. delete[] fAudioInBuffers[i];
  1389. fAudioInBuffers[i] = nullptr;
  1390. }
  1391. }
  1392. delete[] fAudioInBuffers;
  1393. fAudioInBuffers = nullptr;
  1394. }
  1395. if (fAudioOutBuffers != nullptr)
  1396. {
  1397. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1398. {
  1399. if (fAudioOutBuffers[i] != nullptr)
  1400. {
  1401. delete[] fAudioOutBuffers[i];
  1402. fAudioOutBuffers[i] = nullptr;
  1403. }
  1404. }
  1405. delete[] fAudioOutBuffers;
  1406. fAudioOutBuffers = nullptr;
  1407. }
  1408. if (fParamBuffers != nullptr)
  1409. {
  1410. delete[] fParamBuffers;
  1411. fParamBuffers = nullptr;
  1412. }
  1413. CarlaPlugin::clearBuffers();
  1414. carla_debug("DssiPlugin::clearBuffers() - end");
  1415. }
  1416. // -------------------------------------------------------------------
  1417. // Post-poned UI Stuff
  1418. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1419. {
  1420. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1421. if (pData->osc.data.target == nullptr)
  1422. return;
  1423. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  1424. }
  1425. void uiMidiProgramChange(const uint32_t index) noexcept override
  1426. {
  1427. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1428. if (pData->osc.data.target == nullptr)
  1429. return;
  1430. osc_send_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1431. }
  1432. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1433. {
  1434. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1435. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1436. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1437. if (pData->osc.data.target == nullptr)
  1438. return;
  1439. #if 0
  1440. uint8_t midiData[4] = { 0 };
  1441. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1442. midiData[2] = note;
  1443. midiData[3] = velo;
  1444. osc_send_midi(pData->osc.data, midiData);
  1445. #endif
  1446. }
  1447. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1448. {
  1449. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1450. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1451. if (pData->osc.data.target == nullptr)
  1452. return;
  1453. #if 0
  1454. uint8_t midiData[4] = { 0 };
  1455. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1456. midiData[2] = note;
  1457. osc_send_midi(pData->osc.data, midiData);
  1458. #endif
  1459. }
  1460. // -------------------------------------------------------------------
  1461. const void* getExtraStuff() const noexcept override
  1462. {
  1463. return fGuiFilename;
  1464. }
  1465. bool init(const char* const filename, const char* const name, const char* const label)
  1466. {
  1467. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1468. // ---------------------------------------------------------------
  1469. // first checks
  1470. if (pData->client != nullptr)
  1471. {
  1472. pData->engine->setLastError("Plugin client is already registered");
  1473. return false;
  1474. }
  1475. if (filename == nullptr || filename[0] == '\0')
  1476. {
  1477. pData->engine->setLastError("null filename");
  1478. return false;
  1479. }
  1480. if (label == nullptr || label[0] == '\0')
  1481. {
  1482. pData->engine->setLastError("null label");
  1483. return false;
  1484. }
  1485. // ---------------------------------------------------------------
  1486. // open DLL
  1487. if (! pData->libOpen(filename))
  1488. {
  1489. pData->engine->setLastError(pData->libError(filename));
  1490. return false;
  1491. }
  1492. // ---------------------------------------------------------------
  1493. // get DLL main entry
  1494. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)pData->libSymbol("dssi_descriptor");
  1495. if (descFn == nullptr)
  1496. {
  1497. pData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1498. return false;
  1499. }
  1500. // ---------------------------------------------------------------
  1501. // get descriptor that matches label
  1502. unsigned long i = 0;
  1503. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1504. {
  1505. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1506. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1507. break;
  1508. }
  1509. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1510. {
  1511. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1512. return false;
  1513. }
  1514. // ---------------------------------------------------------------
  1515. // check if uses global instance
  1516. if (fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  1517. {
  1518. if (! addUniqueMultiSynth(fDescriptor->Label))
  1519. {
  1520. pData->engine->setLastError("This plugin uses a global instance and can't be used more than once safely");
  1521. return false;
  1522. }
  1523. }
  1524. // ---------------------------------------------------------------
  1525. // get info
  1526. if (name != nullptr && name[0] != '\0')
  1527. pData->name = pData->engine->getUniquePluginName(name);
  1528. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1529. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1530. else
  1531. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1532. pData->filename = carla_strdup(filename);
  1533. // ---------------------------------------------------------------
  1534. // register client
  1535. pData->client = pData->engine->addClient(this);
  1536. if (pData->client == nullptr || ! pData->client->isOk())
  1537. {
  1538. pData->engine->setLastError("Failed to register plugin client");
  1539. return false;
  1540. }
  1541. // ---------------------------------------------------------------
  1542. // initialize plugin
  1543. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)pData->engine->getSampleRate());
  1544. if (fHandle == nullptr)
  1545. {
  1546. pData->engine->setLastError("Plugin failed to initialize");
  1547. return false;
  1548. }
  1549. // ---------------------------------------------------------------
  1550. // check for custom data extension
  1551. if (fDssiDescriptor->configure != nullptr)
  1552. {
  1553. if (char* const error = fDssiDescriptor->configure(fHandle, DSSI_CUSTOMDATA_EXTENSION_KEY, ""))
  1554. {
  1555. if (std::strcmp(error, "true") == 0 && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  1556. fUsesCustomData = true;
  1557. std::free(error);
  1558. }
  1559. }
  1560. // ---------------------------------------------------------------
  1561. // gui stuff
  1562. if (const char* const guiFilename = find_dssi_ui(filename, fDescriptor->Label))
  1563. {
  1564. pData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1565. fGuiFilename = guiFilename;
  1566. }
  1567. // ---------------------------------------------------------------
  1568. // load plugin settings
  1569. {
  1570. #ifdef __USE_GNU
  1571. const bool isAmSynth(strcasestr(pData->filename, "amsynth"));
  1572. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst"));
  1573. #else
  1574. const bool isAmSynth(std::strstr(pData->filename, "amsynth"));
  1575. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst"));
  1576. #endif
  1577. // set default options
  1578. pData->options = 0x0;
  1579. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1580. if (isAmSynth || isDssiVst)
  1581. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1582. if (pData->engine->getOptions().forceStereo)
  1583. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1584. if (fUsesCustomData)
  1585. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1586. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  1587. {
  1588. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1589. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1590. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1591. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1592. if (fDssiDescriptor->run_synth == nullptr)
  1593. carla_stderr2("WARNING: Plugin can ONLY use run_multiple_synths!");
  1594. }
  1595. // set identifier string
  1596. CarlaString identifier("DSSI/");
  1597. if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1598. {
  1599. identifier += shortname+1;
  1600. identifier += ",";
  1601. }
  1602. identifier += label;
  1603. pData->identifier = identifier.dup();
  1604. // load settings
  1605. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1606. // ignore settings, we need this anyway
  1607. if (isAmSynth || isDssiVst)
  1608. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1609. }
  1610. return true;
  1611. }
  1612. private:
  1613. LADSPA_Handle fHandle;
  1614. LADSPA_Handle fHandle2;
  1615. const LADSPA_Descriptor* fDescriptor;
  1616. const DSSI_Descriptor* fDssiDescriptor;
  1617. bool fUsesCustomData;
  1618. const char* fGuiFilename;
  1619. float** fAudioInBuffers;
  1620. float** fAudioOutBuffers;
  1621. float* fParamBuffers;
  1622. snd_seq_event_t fMidiEvents[kPluginMaxMidiEvents];
  1623. static LinkedList<const char*> sMultiSynthList;
  1624. static bool addUniqueMultiSynth(const char* const label)
  1625. {
  1626. CARLA_SAFE_ASSERT_RETURN(label != nullptr, true);
  1627. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1628. {
  1629. const char*& itLabel(it.getValue());
  1630. if (std::strcmp(label, itLabel) == 0)
  1631. return false;
  1632. }
  1633. sMultiSynthList.append(carla_strdup(label));
  1634. return true;
  1635. }
  1636. static void removeUniqueMultiSynth(const char* const label)
  1637. {
  1638. CARLA_SAFE_ASSERT_RETURN(label != nullptr,);
  1639. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1640. {
  1641. const char*& itLabel(it.getValue());
  1642. if (std::strcmp(label, itLabel) == 0)
  1643. {
  1644. sMultiSynthList.remove(it);
  1645. delete[] itLabel;
  1646. return;
  1647. }
  1648. }
  1649. }
  1650. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1651. };
  1652. LinkedList<const char*> DssiPlugin::sMultiSynthList;
  1653. CARLA_BACKEND_END_NAMESPACE
  1654. #endif // WANT_DSSI
  1655. CARLA_BACKEND_START_NAMESPACE
  1656. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  1657. {
  1658. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1659. #ifdef WANT_DSSI
  1660. DssiPlugin* const plugin(new DssiPlugin(init.engine, init.id));
  1661. if (! plugin->init(init.filename, init.name, init.label))
  1662. {
  1663. delete plugin;
  1664. return nullptr;
  1665. }
  1666. plugin->reload();
  1667. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1668. {
  1669. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1670. delete plugin;
  1671. return nullptr;
  1672. }
  1673. return plugin;
  1674. #else
  1675. init.engine->setLastError("DSSI support not available");
  1676. return nullptr;
  1677. #endif
  1678. }
  1679. CARLA_BACKEND_END_NAMESPACE