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.

2316 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. #include "CarlaDssiUtils.hpp"
  20. #include "CarlaBase64Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. CARLA_BACKEND_START_NAMESPACE
  23. // -----------------------------------------------------
  24. class DssiPlugin : public CarlaPlugin
  25. {
  26. public:
  27. DssiPlugin(CarlaEngine* const engine, const uint id) noexcept
  28. : CarlaPlugin(engine, id),
  29. fHandle(nullptr),
  30. fHandle2(nullptr),
  31. fDescriptor(nullptr),
  32. fDssiDescriptor(nullptr),
  33. fUsesCustomData(false),
  34. fUiFilename(nullptr),
  35. fAudioInBuffers(nullptr),
  36. fAudioOutBuffers(nullptr),
  37. fParamBuffers(nullptr),
  38. fLatencyChanged(false),
  39. fLatencyIndex(-1)
  40. {
  41. carla_debug("DssiPlugin::DssiPlugin(%p, %i)", engine, id);
  42. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  43. }
  44. ~DssiPlugin() noexcept override
  45. {
  46. carla_debug("DssiPlugin::~DssiPlugin()");
  47. // close UI
  48. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  49. {
  50. showCustomUI(false);
  51. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  52. }
  53. pData->singleMutex.lock();
  54. pData->masterMutex.lock();
  55. if (pData->client != nullptr && pData->client->isActive())
  56. pData->client->deactivate();
  57. if (pData->active)
  58. {
  59. deactivate();
  60. pData->active = false;
  61. }
  62. if (fDescriptor != nullptr)
  63. {
  64. if (pData->name != nullptr && fDssiDescriptor != nullptr && fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  65. removeUniqueMultiSynth(fDescriptor->Label);
  66. if (fDescriptor->cleanup != nullptr)
  67. {
  68. if (fHandle != nullptr)
  69. {
  70. try {
  71. fDescriptor->cleanup(fHandle);
  72. } CARLA_SAFE_EXCEPTION("DSSI cleanup");
  73. }
  74. if (fHandle2 != nullptr)
  75. {
  76. try {
  77. fDescriptor->cleanup(fHandle2);
  78. } CARLA_SAFE_EXCEPTION("DSSI cleanup #2");
  79. }
  80. }
  81. fHandle = nullptr;
  82. fHandle2 = nullptr;
  83. fDescriptor = nullptr;
  84. fDssiDescriptor = nullptr;
  85. }
  86. if (fUiFilename != nullptr)
  87. {
  88. delete[] fUiFilename;
  89. fUiFilename = nullptr;
  90. }
  91. clearBuffers();
  92. }
  93. // -------------------------------------------------------------------
  94. // Information (base)
  95. PluginType getType() const noexcept override
  96. {
  97. return PLUGIN_DSSI;
  98. }
  99. PluginCategory getCategory() const noexcept override
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  102. if (pData->audioIn.count == 0 && pData->audioOut.count > 0 && (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr))
  103. return PLUGIN_CATEGORY_SYNTH;
  104. return CarlaPlugin::getCategory();
  105. }
  106. int64_t getUniqueId() const noexcept override
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  109. return static_cast<int64_t>(fDescriptor->UniqueID);
  110. }
  111. // -------------------------------------------------------------------
  112. // Information (count)
  113. // nothing
  114. // -------------------------------------------------------------------
  115. // Information (current data)
  116. std::size_t getChunkData(void** const dataPtr) noexcept override
  117. {
  118. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData, 0);
  119. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  120. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0);
  121. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->get_custom_data != nullptr, 0);
  122. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  123. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr, 0);
  124. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  125. *dataPtr = nullptr;
  126. int ret = 0;
  127. ulong dataSize = 0;
  128. try {
  129. ret = fDssiDescriptor->get_custom_data(fHandle, dataPtr, &dataSize);
  130. } CARLA_SAFE_EXCEPTION_RETURN("DssiPlugin::getChunkData", 0);
  131. return (ret != 0) ? dataSize : 0;
  132. }
  133. // -------------------------------------------------------------------
  134. // Information (per-plugin data)
  135. uint getOptionsAvailable() const noexcept override
  136. {
  137. #ifdef __USE_GNU
  138. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  139. #else
  140. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  141. #endif
  142. uint options = 0x0;
  143. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  144. if (! isDssiVst)
  145. {
  146. if (fLatencyIndex == -1)
  147. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  148. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  149. {
  150. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  151. options |= PLUGIN_OPTION_FORCE_STEREO;
  152. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  153. options |= PLUGIN_OPTION_FORCE_STEREO;
  154. }
  155. }
  156. if (fUsesCustomData)
  157. options |= PLUGIN_OPTION_USE_CHUNKS;
  158. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  159. {
  160. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  161. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  162. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  163. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  164. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  165. }
  166. return options;
  167. }
  168. float getParameterValue(const uint32_t parameterId) const noexcept override
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  171. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  172. return fParamBuffers[parameterId];
  173. }
  174. void getLabel(char* const strBuf) const noexcept override
  175. {
  176. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  177. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Label != nullptr, nullStrBuf(strBuf));
  178. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  179. }
  180. void getMaker(char* const strBuf) const noexcept override
  181. {
  182. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  183. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Maker != nullptr, nullStrBuf(strBuf));
  184. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  185. }
  186. void getCopyright(char* const strBuf) const noexcept override
  187. {
  188. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  189. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Copyright != nullptr, nullStrBuf(strBuf));
  190. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  191. }
  192. void getRealName(char* const strBuf) const noexcept override
  193. {
  194. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  195. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Name != nullptr, nullStrBuf(strBuf));
  196. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  197. }
  198. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  201. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  202. const int32_t rindex(pData->param.data[parameterId].rindex);
  203. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  204. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames[rindex] != nullptr, nullStrBuf(strBuf));
  205. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, true))
  206. return;
  207. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  208. }
  209. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  210. {
  211. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  212. const int32_t rindex(pData->param.data[parameterId].rindex);
  213. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  214. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, false))
  215. return;
  216. nullStrBuf(strBuf);
  217. }
  218. // -------------------------------------------------------------------
  219. // Set data (state)
  220. // nothing
  221. // -------------------------------------------------------------------
  222. // Set data (internal stuff)
  223. // nothing
  224. // -------------------------------------------------------------------
  225. // Set data (plugin-specific stuff)
  226. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  227. {
  228. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  229. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  230. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  231. fParamBuffers[parameterId] = fixedValue;
  232. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  233. }
  234. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  235. {
  236. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  237. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  238. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  239. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  240. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  241. carla_debug("DssiPlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  242. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  243. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  244. if (fDssiDescriptor->configure != nullptr)
  245. {
  246. try {
  247. fDssiDescriptor->configure(fHandle, key, value);
  248. } catch(...) {}
  249. if (fHandle2 != nullptr)
  250. {
  251. try {
  252. fDssiDescriptor->configure(fHandle2, key, value);
  253. } catch(...) {}
  254. }
  255. }
  256. if (sendGui && pData->osc.data.target != nullptr)
  257. osc_send_configure(pData->osc.data, key, value);
  258. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  259. {
  260. const ScopedSingleProcessLocker spl(this, true);
  261. reloadPrograms(false);
  262. }
  263. CarlaPlugin::setCustomData(type, key, value, sendGui);
  264. }
  265. void setChunkData(const char* const stringData) override
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData,);
  268. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  269. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  270. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->set_custom_data != nullptr,);
  271. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  272. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr,);
  273. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr,);
  274. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stringData));
  275. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0,);
  276. {
  277. const ScopedSingleProcessLocker spl(this, true);
  278. try {
  279. fDssiDescriptor->set_custom_data(fHandle, chunk.data(), chunk.size());
  280. } CARLA_SAFE_EXCEPTION("DssiPlugin::setChunkData");
  281. }
  282. #ifdef BUILD_BRIDGE
  283. const bool sendOsc(false);
  284. #else
  285. const bool sendOsc(pData->engine->isOscControlRegistered());
  286. #endif
  287. pData->updateParameterValues(this, sendOsc, true, false);
  288. }
  289. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  290. {
  291. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  292. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  293. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  294. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  295. if (index >= 0)
  296. {
  297. const uint32_t bank(pData->midiprog.data[index].bank);
  298. const uint32_t program(pData->midiprog.data[index].program);
  299. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  300. try {
  301. fDssiDescriptor->select_program(fHandle, bank, program);
  302. } catch(...) {}
  303. if (fHandle2 != nullptr)
  304. {
  305. try {
  306. fDssiDescriptor->select_program(fHandle2, bank, program);
  307. } catch(...) {}
  308. }
  309. }
  310. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  311. }
  312. // -------------------------------------------------------------------
  313. // Set ui stuff
  314. void showCustomUI(const bool yesNo) override
  315. {
  316. if (yesNo)
  317. {
  318. pData->osc.data.clear();
  319. pData->osc.thread.startThread();
  320. }
  321. else
  322. {
  323. pData->transientTryCounter = 0;
  324. if (pData->osc.data.target != nullptr)
  325. {
  326. osc_send_hide(pData->osc.data);
  327. osc_send_quit(pData->osc.data);
  328. pData->osc.data.clear();
  329. }
  330. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  331. }
  332. }
  333. void idle() override
  334. {
  335. if (fLatencyChanged && fLatencyIndex != -1)
  336. {
  337. fLatencyChanged = false;
  338. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  339. if (latency >= 0)
  340. {
  341. const uint32_t ulatency(static_cast<uint32_t>(latency));
  342. if (pData->latency != ulatency)
  343. {
  344. carla_stdout("latency changed to %i", latency);
  345. const ScopedSingleProcessLocker sspl(this, true);
  346. pData->latency = ulatency;
  347. pData->client->setLatency(ulatency);
  348. #ifndef BUILD_BRIDGE
  349. pData->recreateLatencyBuffers();
  350. #endif
  351. }
  352. }
  353. else
  354. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  355. }
  356. CarlaPlugin::idle();
  357. }
  358. // -------------------------------------------------------------------
  359. // Plugin state
  360. void reload() override
  361. {
  362. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  363. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  364. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  365. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  366. carla_debug("DssiPlugin::reload() - start");
  367. const EngineProcessMode processMode(pData->engine->getProccessMode());
  368. // Safely disable plugin for reload
  369. const ScopedDisabler sd(this);
  370. if (pData->active)
  371. deactivate();
  372. clearBuffers();
  373. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  374. const uint32_t portCount(getSafePortCount());
  375. uint32_t aIns, aOuts, mIns, params;
  376. aIns = aOuts = mIns = params = 0;
  377. bool forcedStereoIn, forcedStereoOut;
  378. forcedStereoIn = forcedStereoOut = false;
  379. bool needsCtrlIn, needsCtrlOut;
  380. needsCtrlIn = needsCtrlOut = false;
  381. for (uint32_t i=0; i < portCount; ++i)
  382. {
  383. const LADSPA_PortDescriptor portType(fDescriptor->PortDescriptors[i]);
  384. if (LADSPA_IS_PORT_AUDIO(portType))
  385. {
  386. if (LADSPA_IS_PORT_INPUT(portType))
  387. aIns += 1;
  388. else if (LADSPA_IS_PORT_OUTPUT(portType))
  389. aOuts += 1;
  390. }
  391. else if (LADSPA_IS_PORT_CONTROL(portType))
  392. params += 1;
  393. }
  394. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  395. {
  396. if (fHandle2 == nullptr)
  397. {
  398. try {
  399. fHandle2 = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(sampleRate));
  400. } CARLA_SAFE_EXCEPTION("DSSI instantiate #2");
  401. }
  402. if (fHandle2 != nullptr)
  403. {
  404. if (aIns == 1)
  405. {
  406. aIns = 2;
  407. forcedStereoIn = true;
  408. }
  409. if (aOuts == 1)
  410. {
  411. aOuts = 2;
  412. forcedStereoOut = true;
  413. }
  414. }
  415. }
  416. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  417. {
  418. mIns = 1;
  419. needsCtrlIn = true;
  420. }
  421. if (aIns > 0)
  422. {
  423. pData->audioIn.createNew(aIns);
  424. fAudioInBuffers = new float*[aIns];
  425. for (uint32_t i=0; i < aIns; ++i)
  426. fAudioInBuffers[i] = nullptr;
  427. }
  428. if (aOuts > 0)
  429. {
  430. pData->audioOut.createNew(aOuts);
  431. fAudioOutBuffers = new float*[aOuts];
  432. needsCtrlIn = true;
  433. for (uint32_t i=0; i < aOuts; ++i)
  434. fAudioOutBuffers[i] = nullptr;
  435. }
  436. if (params > 0)
  437. {
  438. pData->param.createNew(params, true);
  439. fParamBuffers = new float[params];
  440. FloatVectorOperations::clear(fParamBuffers, static_cast<int>(params));
  441. }
  442. const uint portNameSize(pData->engine->getMaxPortNameSize());
  443. CarlaString portName;
  444. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  445. {
  446. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  447. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  448. if (LADSPA_IS_PORT_AUDIO(portType))
  449. {
  450. portName.clear();
  451. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  452. {
  453. portName = pData->name;
  454. portName += ":";
  455. }
  456. if (fDescriptor->PortNames[i] != nullptr && fDescriptor->PortNames[i][0] != '\0')
  457. {
  458. portName += fDescriptor->PortNames[i];
  459. }
  460. else
  461. {
  462. if (LADSPA_IS_PORT_INPUT(portType))
  463. {
  464. if (aIns > 1)
  465. {
  466. portName += "audio-in_";
  467. portName += CarlaString(iAudioIn+1);
  468. }
  469. else
  470. portName += "audio-in";
  471. }
  472. else
  473. {
  474. if (aOuts > 1)
  475. {
  476. portName += "audio-out_";
  477. portName += CarlaString(iAudioOut+1);
  478. }
  479. else
  480. portName += "audio-out";
  481. }
  482. }
  483. portName.truncate(portNameSize);
  484. if (LADSPA_IS_PORT_INPUT(portType))
  485. {
  486. const uint32_t j = iAudioIn++;
  487. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  488. pData->audioIn.ports[j].rindex = i;
  489. if (forcedStereoIn)
  490. {
  491. portName += "_2";
  492. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  493. pData->audioIn.ports[1].rindex = i;
  494. }
  495. }
  496. else if (LADSPA_IS_PORT_OUTPUT(portType))
  497. {
  498. const uint32_t j = iAudioOut++;
  499. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  500. pData->audioOut.ports[j].rindex = i;
  501. if (forcedStereoOut)
  502. {
  503. portName += "_2";
  504. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  505. pData->audioOut.ports[1].rindex = i;
  506. }
  507. }
  508. else
  509. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  510. }
  511. else if (LADSPA_IS_PORT_CONTROL(portType))
  512. {
  513. const uint32_t j = iCtrl++;
  514. pData->param.data[j].index = static_cast<int32_t>(j);
  515. pData->param.data[j].rindex = static_cast<int32_t>(i);
  516. const char* const paramName(fDescriptor->PortNames[i] != nullptr ? fDescriptor->PortNames[i] : "unknown");
  517. float min, max, def, step, stepSmall, stepLarge;
  518. // min value
  519. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  520. min = portRangeHints.LowerBound;
  521. else
  522. min = 0.0f;
  523. // max value
  524. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  525. max = portRangeHints.UpperBound;
  526. else
  527. max = 1.0f;
  528. if (min > max)
  529. {
  530. carla_stderr2("WARNING - Broken plugin parameter '%s': min > max", paramName);
  531. min = max - 0.1f;
  532. }
  533. else if (min == max)
  534. {
  535. carla_stderr2("WARNING - Broken plugin parameter '%s': min == maxf", paramName);
  536. max = min + 0.1f;
  537. }
  538. // default value
  539. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  540. if (def < min)
  541. def = min;
  542. else if (def > max)
  543. def = max;
  544. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  545. {
  546. min *= sampleRate;
  547. max *= sampleRate;
  548. def *= sampleRate;
  549. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  550. }
  551. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  552. {
  553. step = max - min;
  554. stepSmall = step;
  555. stepLarge = step;
  556. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  557. }
  558. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  559. {
  560. step = 1.0f;
  561. stepSmall = 1.0f;
  562. stepLarge = 10.0f;
  563. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  564. }
  565. else
  566. {
  567. const float range = max - min;
  568. step = range/100.0f;
  569. stepSmall = range/1000.0f;
  570. stepLarge = range/10.0f;
  571. }
  572. if (LADSPA_IS_PORT_INPUT(portType))
  573. {
  574. pData->param.data[j].type = PARAMETER_INPUT;
  575. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  576. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  577. needsCtrlIn = true;
  578. // MIDI CC value
  579. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  580. {
  581. int controller = fDssiDescriptor->get_midi_controller_for_port(fHandle, i);
  582. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  583. {
  584. int16_t cc = DSSI_CC_NUMBER(controller);
  585. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  586. pData->param.data[j].midiCC = cc;
  587. }
  588. }
  589. }
  590. else if (LADSPA_IS_PORT_OUTPUT(portType))
  591. {
  592. pData->param.data[j].type = PARAMETER_OUTPUT;
  593. if (std::strcmp(paramName, "latency") == 0 || std::strcmp(paramName, "_latency") == 0)
  594. {
  595. min = 0.0f;
  596. max = sampleRate;
  597. def = 0.0f;
  598. step = 1.0f;
  599. stepSmall = 1.0f;
  600. stepLarge = 1.0f;
  601. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  602. CARLA_SAFE_ASSERT(fLatencyIndex == -1);
  603. fLatencyIndex = static_cast<int32_t>(j);
  604. }
  605. else
  606. {
  607. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  608. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  609. needsCtrlOut = true;
  610. }
  611. }
  612. else
  613. {
  614. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  615. }
  616. // extra parameter hints
  617. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  618. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  619. pData->param.ranges[j].min = min;
  620. pData->param.ranges[j].max = max;
  621. pData->param.ranges[j].def = def;
  622. pData->param.ranges[j].step = step;
  623. pData->param.ranges[j].stepSmall = stepSmall;
  624. pData->param.ranges[j].stepLarge = stepLarge;
  625. // Start parameters in their default values
  626. fParamBuffers[j] = def;
  627. try {
  628. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  629. } CARLA_SAFE_EXCEPTION("DSSI connect_port parameter");
  630. if (fHandle2 != nullptr)
  631. {
  632. try {
  633. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  634. } CARLA_SAFE_EXCEPTION("DSSI connect_port parameter #2");
  635. }
  636. }
  637. else
  638. {
  639. // Not Audio or Control
  640. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  641. try {
  642. fDescriptor->connect_port(fHandle, i, nullptr);
  643. } CARLA_SAFE_EXCEPTION("DSSI connect_port null");
  644. if (fHandle2 != nullptr)
  645. {
  646. try {
  647. fDescriptor->connect_port(fHandle2, i, nullptr);
  648. } CARLA_SAFE_EXCEPTION("DSSI connect_port null #2");
  649. }
  650. }
  651. }
  652. if (needsCtrlIn)
  653. {
  654. portName.clear();
  655. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  656. {
  657. portName = pData->name;
  658. portName += ":";
  659. }
  660. portName += "events-in";
  661. portName.truncate(portNameSize);
  662. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  663. }
  664. if (needsCtrlOut)
  665. {
  666. portName.clear();
  667. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  668. {
  669. portName = pData->name;
  670. portName += ":";
  671. }
  672. portName += "events-out";
  673. portName.truncate(portNameSize);
  674. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  675. }
  676. if (forcedStereoIn || forcedStereoOut)
  677. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  678. else
  679. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  680. // plugin hints
  681. pData->hints = 0x0;
  682. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  683. pData->hints |= PLUGIN_IS_RTSAFE;
  684. if (fUiFilename != nullptr)
  685. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  686. #ifndef BUILD_BRIDGE
  687. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  688. pData->hints |= PLUGIN_CAN_DRYWET;
  689. if (aOuts > 0)
  690. pData->hints |= PLUGIN_CAN_VOLUME;
  691. if (aOuts >= 2 && aOuts % 2 == 0)
  692. pData->hints |= PLUGIN_CAN_BALANCE;
  693. #endif
  694. // extra plugin hints
  695. pData->extraHints = 0x0;
  696. if (mIns > 0)
  697. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  698. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  699. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  700. // check latency
  701. if (fLatencyIndex >= 0)
  702. {
  703. // we need to pre-run the plugin so it can update its latency control-port
  704. float tmpIn [(aIns > 0) ? aIns : 1][2];
  705. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  706. for (uint32_t j=0; j < aIns; ++j)
  707. {
  708. tmpIn[j][0] = 0.0f;
  709. tmpIn[j][1] = 0.0f;
  710. try {
  711. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  712. } CARLA_SAFE_EXCEPTION("DSSI connect_port latency input");
  713. }
  714. for (uint32_t j=0; j < aOuts; ++j)
  715. {
  716. tmpOut[j][0] = 0.0f;
  717. tmpOut[j][1] = 0.0f;
  718. try {
  719. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  720. } CARLA_SAFE_EXCEPTION("DSSI connect_port latency output");
  721. }
  722. if (fDescriptor->activate != nullptr)
  723. {
  724. try {
  725. fDescriptor->activate(fHandle);
  726. } CARLA_SAFE_EXCEPTION("DSSI latency activate");
  727. }
  728. try {
  729. fDescriptor->run(fHandle, 2);
  730. } CARLA_SAFE_EXCEPTION("DSSI latency run");
  731. if (fDescriptor->deactivate != nullptr)
  732. {
  733. try {
  734. fDescriptor->deactivate(fHandle);
  735. } CARLA_SAFE_EXCEPTION("DSSI latency deactivate");
  736. }
  737. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  738. if (latency >= 0)
  739. {
  740. const uint32_t ulatency(static_cast<uint32_t>(latency));
  741. if (pData->latency != ulatency)
  742. {
  743. carla_stdout("latency = %i", latency);
  744. pData->latency = ulatency;
  745. pData->client->setLatency(ulatency);
  746. #ifndef BUILD_BRIDGE
  747. pData->recreateLatencyBuffers();
  748. #endif
  749. }
  750. }
  751. else
  752. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  753. fLatencyChanged = false;
  754. }
  755. bufferSizeChanged(pData->engine->getBufferSize());
  756. reloadPrograms(true);
  757. if (pData->active)
  758. activate();
  759. carla_debug("DssiPlugin::reload() - end");
  760. }
  761. void reloadPrograms(const bool doInit) override
  762. {
  763. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(doInit));
  764. const uint32_t oldCount = pData->midiprog.count;
  765. const int32_t current = pData->midiprog.current;
  766. // Delete old programs
  767. pData->midiprog.clear();
  768. // Query new programs
  769. uint32_t newCount = 0;
  770. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  771. {
  772. for (; fDssiDescriptor->get_program(fHandle, newCount) != nullptr;)
  773. ++newCount;
  774. }
  775. if (newCount > 0)
  776. {
  777. pData->midiprog.createNew(newCount);
  778. // Update data
  779. for (uint32_t i=0; i < newCount; ++i)
  780. {
  781. const DSSI_Program_Descriptor* const pdesc(fDssiDescriptor->get_program(fHandle, i));
  782. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  783. CARLA_SAFE_ASSERT(pdesc->Name != nullptr);
  784. pData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  785. pData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  786. pData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  787. }
  788. }
  789. #ifndef BUILD_BRIDGE
  790. // Update OSC Names
  791. if (pData->engine->isOscControlRegistered())
  792. {
  793. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  794. for (uint32_t i=0; i < newCount; ++i)
  795. 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);
  796. }
  797. #endif
  798. if (doInit)
  799. {
  800. if (newCount > 0)
  801. setMidiProgram(0, false, false, false);
  802. }
  803. else
  804. {
  805. // Check if current program is invalid
  806. bool programChanged = false;
  807. if (newCount == oldCount+1)
  808. {
  809. // one midi program added, probably created by user
  810. pData->midiprog.current = static_cast<int32_t>(oldCount);
  811. programChanged = true;
  812. }
  813. else if (current < 0 && newCount > 0)
  814. {
  815. // programs exist now, but not before
  816. pData->midiprog.current = 0;
  817. programChanged = true;
  818. }
  819. else if (current >= 0 && newCount == 0)
  820. {
  821. // programs existed before, but not anymore
  822. pData->midiprog.current = -1;
  823. programChanged = true;
  824. }
  825. else if (current >= static_cast<int32_t>(newCount))
  826. {
  827. // current midi program > count
  828. pData->midiprog.current = 0;
  829. programChanged = true;
  830. }
  831. else
  832. {
  833. // no change
  834. pData->midiprog.current = current;
  835. }
  836. if (programChanged)
  837. setMidiProgram(pData->midiprog.current, true, true, true);
  838. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  839. }
  840. }
  841. // -------------------------------------------------------------------
  842. // Plugin processing
  843. void activate() noexcept override
  844. {
  845. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  846. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  847. if (fDescriptor->activate != nullptr)
  848. {
  849. try {
  850. fDescriptor->activate(fHandle);
  851. } CARLA_SAFE_EXCEPTION("DSSI activate");
  852. if (fHandle2 != nullptr)
  853. {
  854. try {
  855. fDescriptor->activate(fHandle2);
  856. } CARLA_SAFE_EXCEPTION("DSSI activate #2");
  857. }
  858. }
  859. }
  860. void deactivate() noexcept override
  861. {
  862. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  863. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  864. if (fDescriptor->deactivate != nullptr)
  865. {
  866. try {
  867. fDescriptor->deactivate(fHandle);
  868. } CARLA_SAFE_EXCEPTION("DSSI deactivate");
  869. if (fHandle2 != nullptr)
  870. {
  871. try {
  872. fDescriptor->deactivate(fHandle2);
  873. } CARLA_SAFE_EXCEPTION("DSSI deactivate #2");
  874. }
  875. }
  876. }
  877. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  878. {
  879. // --------------------------------------------------------------------------------------------------------
  880. // Check if active
  881. if (! pData->active)
  882. {
  883. // disable any output sound
  884. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  885. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  886. return;
  887. }
  888. ulong midiEventCount = 0;
  889. // --------------------------------------------------------------------------------------------------------
  890. // Check if needs reset
  891. if (pData->needsReset)
  892. {
  893. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  894. {
  895. midiEventCount = MAX_MIDI_CHANNELS*2;
  896. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  897. for (uchar i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  898. {
  899. fMidiEvents[i].type = SND_SEQ_EVENT_CONTROLLER;
  900. fMidiEvents[i].data.control.channel = i;
  901. fMidiEvents[i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  902. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  903. fMidiEvents[k+i].data.control.channel = i;
  904. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  905. }
  906. }
  907. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  908. {
  909. midiEventCount = MAX_MIDI_NOTE;
  910. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  911. for (uchar i=0; i < MAX_MIDI_NOTE; ++i)
  912. {
  913. fMidiEvents[i].type = SND_SEQ_EVENT_NOTEOFF;
  914. fMidiEvents[i].data.note.channel = static_cast<uchar>(pData->ctrlChannel);
  915. fMidiEvents[i].data.note.note = i;
  916. }
  917. }
  918. #ifndef BUILD_BRIDGE
  919. if (pData->latency > 0)
  920. {
  921. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  922. FloatVectorOperations::clear(pData->latencyBuffers[i], static_cast<int>(pData->latency));
  923. }
  924. #endif
  925. pData->needsReset = false;
  926. }
  927. // --------------------------------------------------------------------------------------------------------
  928. // Event Input and Processing
  929. if (pData->event.portIn != nullptr)
  930. {
  931. // ----------------------------------------------------------------------------------------------------
  932. // MIDI Input (External)
  933. if (pData->extNotes.mutex.tryLock())
  934. {
  935. ExternalMidiNote note = { 0, 0, 0 };
  936. for (; midiEventCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  937. {
  938. note = pData->extNotes.data.getFirst(note, true);
  939. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  940. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  941. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  942. midiEvent.type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  943. midiEvent.data.note.channel = static_cast<uchar>(note.channel);
  944. midiEvent.data.note.note = note.note;
  945. midiEvent.data.note.velocity = note.velo;
  946. }
  947. pData->extNotes.mutex.unlock();
  948. } // End of MIDI Input (External)
  949. // ----------------------------------------------------------------------------------------------------
  950. // Event Input (System)
  951. #ifndef BUILD_BRIDGE
  952. bool allNotesOffSent = false;
  953. #endif
  954. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  955. uint32_t numEvents = pData->event.portIn->getEventCount();
  956. uint32_t startTime = 0;
  957. uint32_t timeOffset = 0;
  958. uint32_t nextBankId;
  959. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  960. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  961. else
  962. nextBankId = 0;
  963. for (uint32_t i=0; i < numEvents; ++i)
  964. {
  965. const EngineEvent& event(pData->event.portIn->getEvent(i));
  966. if (event.time >= frames)
  967. continue;
  968. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  969. if (isSampleAccurate && event.time > timeOffset)
  970. {
  971. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset, midiEventCount))
  972. {
  973. startTime = 0;
  974. timeOffset = event.time;
  975. midiEventCount = 0;
  976. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  977. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  978. else
  979. nextBankId = 0;
  980. }
  981. else
  982. startTime += timeOffset;
  983. }
  984. switch (event.type)
  985. {
  986. case kEngineEventTypeNull:
  987. break;
  988. case kEngineEventTypeControl: {
  989. const EngineControlEvent& ctrlEvent(event.ctrl);
  990. switch (ctrlEvent.type)
  991. {
  992. case kEngineControlEventTypeNull:
  993. break;
  994. case kEngineControlEventTypeParameter: {
  995. #ifndef BUILD_BRIDGE
  996. // Control backend stuff
  997. if (event.channel == pData->ctrlChannel)
  998. {
  999. float value;
  1000. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1001. {
  1002. value = ctrlEvent.value;
  1003. setDryWet(value, false, false);
  1004. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1005. break;
  1006. }
  1007. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1008. {
  1009. value = ctrlEvent.value*127.0f/100.0f;
  1010. setVolume(value, false, false);
  1011. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1012. break;
  1013. }
  1014. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1015. {
  1016. float left, right;
  1017. value = ctrlEvent.value/0.5f - 1.0f;
  1018. if (value < 0.0f)
  1019. {
  1020. left = -1.0f;
  1021. right = (value*2.0f)+1.0f;
  1022. }
  1023. else if (value > 0.0f)
  1024. {
  1025. left = (value*2.0f)-1.0f;
  1026. right = 1.0f;
  1027. }
  1028. else
  1029. {
  1030. left = -1.0f;
  1031. right = 1.0f;
  1032. }
  1033. setBalanceLeft(left, false, false);
  1034. setBalanceRight(right, false, false);
  1035. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1036. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1037. break;
  1038. }
  1039. }
  1040. #endif
  1041. // Control plugin parameters
  1042. uint32_t k;
  1043. for (k=0; k < pData->param.count; ++k)
  1044. {
  1045. if (pData->param.data[k].midiChannel != event.channel)
  1046. continue;
  1047. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1048. continue;
  1049. if (pData->param.data[k].type != PARAMETER_INPUT)
  1050. continue;
  1051. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1052. continue;
  1053. float value;
  1054. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1055. {
  1056. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1057. }
  1058. else
  1059. {
  1060. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1061. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1062. value = std::rint(value);
  1063. }
  1064. setParameterValue(k, value, false, false, false);
  1065. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1066. break;
  1067. }
  1068. // check if event is already handled
  1069. if (k != pData->param.count)
  1070. break;
  1071. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1072. {
  1073. if (midiEventCount >= kPluginMaxMidiEvents)
  1074. continue;
  1075. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1076. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1077. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1078. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1079. midiEvent.data.control.channel = event.channel;
  1080. midiEvent.data.control.param = ctrlEvent.param;
  1081. midiEvent.data.control.value = int8_t(ctrlEvent.value*127.0f);
  1082. }
  1083. break;
  1084. } // case kEngineControlEventTypeParameter
  1085. case kEngineControlEventTypeMidiBank:
  1086. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1087. nextBankId = ctrlEvent.param;
  1088. break;
  1089. case kEngineControlEventTypeMidiProgram:
  1090. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1091. {
  1092. const uint32_t nextProgramId = ctrlEvent.param;
  1093. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1094. {
  1095. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1096. {
  1097. const int32_t index(static_cast<int32_t>(k));
  1098. setMidiProgram(index, false, false, false);
  1099. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. break;
  1105. case kEngineControlEventTypeAllSoundOff:
  1106. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1107. {
  1108. if (midiEventCount >= kPluginMaxMidiEvents)
  1109. continue;
  1110. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1111. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1112. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1113. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1114. midiEvent.data.control.channel = event.channel;
  1115. midiEvent.data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1116. }
  1117. break;
  1118. case kEngineControlEventTypeAllNotesOff:
  1119. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1120. {
  1121. #ifndef BUILD_BRIDGE
  1122. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1123. {
  1124. allNotesOffSent = true;
  1125. sendMidiAllNotesOffToCallback();
  1126. }
  1127. #endif
  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), static_cast<int>(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. // --------------------------------------------------------------------------------------------------------
  1263. // Control Output
  1264. if (pData->event.portOut != nullptr)
  1265. {
  1266. uint8_t channel;
  1267. uint16_t param;
  1268. float value;
  1269. for (uint32_t k=0; k < pData->param.count; ++k)
  1270. {
  1271. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1272. continue;
  1273. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1274. if (pData->param.data[k].midiCC > 0)
  1275. {
  1276. channel = pData->param.data[k].midiChannel;
  1277. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1278. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1279. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1280. }
  1281. }
  1282. } // End of Control Output
  1283. #endif
  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, static_cast<int>(frames));
  1315. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1316. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(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], static_cast<int>(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. void* getNativeHandle() const noexcept override
  1568. {
  1569. return fHandle;
  1570. }
  1571. const void* getNativeDescriptor() const noexcept override
  1572. {
  1573. return fDssiDescriptor;
  1574. }
  1575. const void* getExtraStuff() const noexcept override
  1576. {
  1577. return fUiFilename;
  1578. }
  1579. // -------------------------------------------------------------------
  1580. bool init(const char* const filename, const char* const name, const char* const label)
  1581. {
  1582. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1583. // ---------------------------------------------------------------
  1584. // first checks
  1585. if (pData->client != nullptr)
  1586. {
  1587. pData->engine->setLastError("Plugin client is already registered");
  1588. return false;
  1589. }
  1590. if (filename == nullptr || filename[0] == '\0')
  1591. {
  1592. pData->engine->setLastError("null filename");
  1593. return false;
  1594. }
  1595. if (label == nullptr || label[0] == '\0')
  1596. {
  1597. pData->engine->setLastError("null label");
  1598. return false;
  1599. }
  1600. // ---------------------------------------------------------------
  1601. // open DLL
  1602. if (! pData->libOpen(filename))
  1603. {
  1604. pData->engine->setLastError(pData->libError(filename));
  1605. return false;
  1606. }
  1607. // ---------------------------------------------------------------
  1608. // get DLL main entry
  1609. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)pData->libSymbol("dssi_descriptor");
  1610. if (descFn == nullptr)
  1611. {
  1612. pData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1613. return false;
  1614. }
  1615. // ---------------------------------------------------------------
  1616. // get descriptor that matches label
  1617. ulong i = 0;
  1618. for (;;)
  1619. {
  1620. try {
  1621. fDssiDescriptor = descFn(i++);
  1622. }
  1623. catch(...) {
  1624. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1625. fDescriptor = nullptr;
  1626. fDssiDescriptor = nullptr;
  1627. break;
  1628. }
  1629. if (fDssiDescriptor == nullptr)
  1630. break;
  1631. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1632. if (fDescriptor == nullptr)
  1633. {
  1634. carla_stderr2("WARNING - Missing LADSPA interface, will not use this plugin");
  1635. fDssiDescriptor = nullptr;
  1636. break;
  1637. }
  1638. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1639. {
  1640. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1641. fDescriptor = nullptr;
  1642. fDssiDescriptor = nullptr;
  1643. break;
  1644. }
  1645. if (fDescriptor->run == nullptr)
  1646. {
  1647. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1648. fDescriptor = nullptr;
  1649. fDssiDescriptor = nullptr;
  1650. break;
  1651. }
  1652. if (std::strcmp(fDescriptor->Label, label) == 0)
  1653. break;
  1654. }
  1655. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1656. {
  1657. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1658. return false;
  1659. }
  1660. // ---------------------------------------------------------------
  1661. // check if uses global instance
  1662. if (fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  1663. {
  1664. if (! addUniqueMultiSynth(fDescriptor->Label))
  1665. {
  1666. pData->engine->setLastError("This plugin uses a global instance and can't be used more than once safely");
  1667. return false;
  1668. }
  1669. }
  1670. // ---------------------------------------------------------------
  1671. // get info
  1672. if (name != nullptr && name[0] != '\0')
  1673. pData->name = pData->engine->getUniquePluginName(name);
  1674. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1675. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1676. else
  1677. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1678. pData->filename = carla_strdup(filename);
  1679. // ---------------------------------------------------------------
  1680. // register client
  1681. pData->client = pData->engine->addClient(this);
  1682. if (pData->client == nullptr || ! pData->client->isOk())
  1683. {
  1684. pData->engine->setLastError("Failed to register plugin client");
  1685. return false;
  1686. }
  1687. // ---------------------------------------------------------------
  1688. // initialize plugin
  1689. try {
  1690. fHandle = fDescriptor->instantiate(fDescriptor, (ulong)pData->engine->getSampleRate());
  1691. } CARLA_SAFE_EXCEPTION("DSSI instantiate");
  1692. if (fHandle == nullptr)
  1693. {
  1694. pData->engine->setLastError("Plugin failed to initialize");
  1695. return false;
  1696. }
  1697. // ---------------------------------------------------------------
  1698. // check for custom data extension
  1699. if (fDssiDescriptor->configure != nullptr)
  1700. {
  1701. if (char* const error = fDssiDescriptor->configure(fHandle, DSSI_CUSTOMDATA_EXTENSION_KEY, ""))
  1702. {
  1703. if (std::strcmp(error, "true") == 0 && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  1704. fUsesCustomData = true;
  1705. std::free(error);
  1706. }
  1707. }
  1708. // ---------------------------------------------------------------
  1709. // gui stuff
  1710. if (const char* const guiFilename = find_dssi_ui(filename, fDescriptor->Label))
  1711. {
  1712. pData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1713. fUiFilename = guiFilename;
  1714. }
  1715. // ---------------------------------------------------------------
  1716. // set default options
  1717. #ifdef __USE_GNU
  1718. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  1719. #else
  1720. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  1721. #endif
  1722. pData->options = 0x0;
  1723. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1724. if (fLatencyIndex >= 0 || isDssiVst)
  1725. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1726. if (pData->engine->getOptions().forceStereo)
  1727. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1728. if (fUsesCustomData)
  1729. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1730. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  1731. {
  1732. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1733. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1734. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1735. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1736. if (fDssiDescriptor->run_synth == nullptr)
  1737. carla_stderr("WARNING: Plugin can ONLY use run_multiple_synths!");
  1738. }
  1739. return true;
  1740. }
  1741. // -------------------------------------------------------------------
  1742. private:
  1743. LADSPA_Handle fHandle;
  1744. LADSPA_Handle fHandle2;
  1745. const LADSPA_Descriptor* fDescriptor;
  1746. const DSSI_Descriptor* fDssiDescriptor;
  1747. bool fUsesCustomData;
  1748. const char* fUiFilename;
  1749. float** fAudioInBuffers;
  1750. float** fAudioOutBuffers;
  1751. float* fParamBuffers;
  1752. bool fLatencyChanged;
  1753. int32_t fLatencyIndex; // -1 if invalid
  1754. snd_seq_event_t fMidiEvents[kPluginMaxMidiEvents];
  1755. // -------------------------------------------------------------------
  1756. uint32_t getSafePortCount() const noexcept
  1757. {
  1758. if (fDescriptor->PortCount == 0)
  1759. return 0;
  1760. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1761. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1762. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1763. return static_cast<uint32_t>(fDescriptor->PortCount);
  1764. }
  1765. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1766. {
  1767. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  1768. return true;
  1769. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  1770. return true;
  1771. return false;
  1772. }
  1773. bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf, const bool wantName, const bool useBracket) const noexcept
  1774. {
  1775. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  1776. if (sepBracketStart == nullptr)
  1777. return false;
  1778. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  1779. if (sepBracketEnd == nullptr)
  1780. return false;
  1781. const size_t unitSize(static_cast<size_t>(sepBracketEnd-sepBracketStart-2));
  1782. if (unitSize > 7) // very unlikely to have such big unit
  1783. return false;
  1784. const size_t sepIndex(std::strlen(paramName)-unitSize-3);
  1785. // just in case
  1786. if (sepIndex > STR_MAX)
  1787. return false;
  1788. if (wantName)
  1789. {
  1790. std::strncpy(strBuf, paramName, sepIndex);
  1791. strBuf[sepIndex] = '\0';
  1792. }
  1793. else
  1794. {
  1795. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  1796. strBuf[unitSize] = '\0';
  1797. }
  1798. return true;
  1799. }
  1800. // -------------------------------------------------------------------
  1801. static LinkedList<const char*> sMultiSynthList;
  1802. static bool addUniqueMultiSynth(const char* const label) noexcept
  1803. {
  1804. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', false);
  1805. const char* dlabel = nullptr;
  1806. try {
  1807. dlabel = carla_strdup(label);
  1808. } catch(...) { return false; }
  1809. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1810. {
  1811. const char* const itLabel(it.getValue());
  1812. if (std::strcmp(dlabel, itLabel) == 0)
  1813. {
  1814. delete[] dlabel;
  1815. return false;
  1816. }
  1817. }
  1818. return sMultiSynthList.append(dlabel);
  1819. }
  1820. static void removeUniqueMultiSynth(const char* const label) noexcept
  1821. {
  1822. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0',);
  1823. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1824. {
  1825. const char* const itLabel(it.getValue());
  1826. if (std::strcmp(label, itLabel) == 0)
  1827. {
  1828. sMultiSynthList.remove(it);
  1829. delete[] itLabel;
  1830. break;
  1831. }
  1832. }
  1833. }
  1834. // -------------------------------------------------------------------
  1835. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1836. };
  1837. LinkedList<const char*> DssiPlugin::sMultiSynthList;
  1838. // -------------------------------------------------------------------------------------------------------------------
  1839. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  1840. {
  1841. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1842. DssiPlugin* const plugin(new DssiPlugin(init.engine, init.id));
  1843. if (! plugin->init(init.filename, init.name, init.label))
  1844. {
  1845. delete plugin;
  1846. return nullptr;
  1847. }
  1848. plugin->reload();
  1849. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1850. {
  1851. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1852. delete plugin;
  1853. return nullptr;
  1854. }
  1855. return plugin;
  1856. }
  1857. // -------------------------------------------------------------------------------------------------------------------
  1858. CARLA_BACKEND_END_NAMESPACE