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.

2342 lines
83KB

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