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.

2311 lines
82KB

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