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.

2041 lines
72KB

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