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.

2089 lines
74KB

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