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.

2093 lines
75KB

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