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.

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