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.

2370 lines
84KB

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