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.

2333 lines
82KB

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