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.

1706 lines
59KB

  1. /*
  2. * Carla LADSPA Plugin
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifdef WANT_LADSPA
  20. #include "CarlaLadspaUtils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. // -----------------------------------------------------
  23. CARLA_BACKEND_START_NAMESPACE
  24. #if 0
  25. }
  26. #endif
  27. // -----------------------------------------------------
  28. class LadspaPlugin : public CarlaPlugin
  29. {
  30. public:
  31. LadspaPlugin(CarlaEngine* const engine, const uint id) noexcept
  32. : CarlaPlugin(engine, id),
  33. fHandle(nullptr),
  34. fHandle2(nullptr),
  35. fDescriptor(nullptr),
  36. fRdfDescriptor(nullptr),
  37. fAudioInBuffers(nullptr),
  38. fAudioOutBuffers(nullptr),
  39. fParamBuffers(nullptr),
  40. fLatencyIndex(-1)
  41. {
  42. carla_debug("LadspaPlugin::LadspaPlugin(%p, %i)", engine, id);
  43. }
  44. ~LadspaPlugin() noexcept override
  45. {
  46. carla_debug("LadspaPlugin::~LadspaPlugin()");
  47. pData->singleMutex.lock();
  48. pData->masterMutex.lock();
  49. if (pData->client != nullptr && pData->client->isActive())
  50. pData->client->deactivate();
  51. if (pData->active)
  52. {
  53. deactivate();
  54. pData->active = false;
  55. }
  56. if (fDescriptor != nullptr)
  57. {
  58. if (fDescriptor->cleanup != nullptr)
  59. {
  60. if (fHandle != nullptr)
  61. {
  62. try {
  63. fDescriptor->cleanup(fHandle);
  64. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  65. }
  66. if (fHandle2 != nullptr)
  67. {
  68. try {
  69. fDescriptor->cleanup(fHandle2);
  70. } CARLA_SAFE_EXCEPTION("LADSPA cleanup #2");
  71. }
  72. }
  73. fHandle = nullptr;
  74. fHandle2 = nullptr;
  75. fDescriptor = nullptr;
  76. }
  77. if (fRdfDescriptor != nullptr)
  78. {
  79. delete fRdfDescriptor;
  80. fRdfDescriptor = nullptr;
  81. }
  82. clearBuffers();
  83. }
  84. // -------------------------------------------------------------------
  85. // Information (base)
  86. PluginType getType() const noexcept override
  87. {
  88. return PLUGIN_LADSPA;
  89. }
  90. PluginCategory getCategory() const noexcept override
  91. {
  92. if (fRdfDescriptor != nullptr)
  93. {
  94. const LADSPA_PluginType category(fRdfDescriptor->Type);
  95. // Specific Types
  96. if (category & (LADSPA_PLUGIN_DELAY|LADSPA_PLUGIN_REVERB))
  97. return PLUGIN_CATEGORY_DELAY;
  98. if (category & (LADSPA_PLUGIN_PHASER|LADSPA_PLUGIN_FLANGER|LADSPA_PLUGIN_CHORUS))
  99. return PLUGIN_CATEGORY_MODULATOR;
  100. if (category & (LADSPA_PLUGIN_AMPLIFIER))
  101. return PLUGIN_CATEGORY_DYNAMICS;
  102. if (category & (LADSPA_PLUGIN_UTILITY|LADSPA_PLUGIN_SPECTRAL|LADSPA_PLUGIN_FREQUENCY_METER))
  103. return PLUGIN_CATEGORY_UTILITY;
  104. // Pre-set LADSPA Types
  105. if (LADSPA_IS_PLUGIN_DYNAMICS(category))
  106. return PLUGIN_CATEGORY_DYNAMICS;
  107. if (LADSPA_IS_PLUGIN_AMPLITUDE(category))
  108. return PLUGIN_CATEGORY_MODULATOR;
  109. if (LADSPA_IS_PLUGIN_EQ(category))
  110. return PLUGIN_CATEGORY_EQ;
  111. if (LADSPA_IS_PLUGIN_FILTER(category))
  112. return PLUGIN_CATEGORY_FILTER;
  113. if (LADSPA_IS_PLUGIN_FREQUENCY(category))
  114. return PLUGIN_CATEGORY_UTILITY;
  115. if (LADSPA_IS_PLUGIN_SIMULATOR(category))
  116. return PLUGIN_CATEGORY_OTHER;
  117. if (LADSPA_IS_PLUGIN_TIME(category))
  118. return PLUGIN_CATEGORY_DELAY;
  119. if (LADSPA_IS_PLUGIN_GENERATOR(category))
  120. return PLUGIN_CATEGORY_SYNTH;
  121. }
  122. return CarlaPlugin::getCategory();
  123. }
  124. int64_t getUniqueId() const noexcept override
  125. {
  126. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  127. return static_cast<int64_t>(fDescriptor->UniqueID);
  128. }
  129. // -------------------------------------------------------------------
  130. // Information (count)
  131. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  134. const int32_t rindex(pData->param.data[parameterId].rindex);
  135. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  136. {
  137. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  138. return static_cast<uint32_t>(port->ScalePointCount);
  139. }
  140. return 0;
  141. }
  142. // -------------------------------------------------------------------
  143. // Information (current data)
  144. // nothing
  145. // -------------------------------------------------------------------
  146. // Information (per-plugin data)
  147. uint getOptionsAvailable() const noexcept override
  148. {
  149. #ifdef __USE_GNU
  150. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  151. #else
  152. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  153. #endif
  154. uint options = 0x0;
  155. if (! isDssiVst)
  156. {
  157. if (fLatencyIndex == -1)
  158. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  159. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  160. {
  161. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  162. options |= PLUGIN_OPTION_FORCE_STEREO;
  163. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  164. options |= PLUGIN_OPTION_FORCE_STEREO;
  165. }
  166. }
  167. return options;
  168. }
  169. float getParameterValue(const uint32_t parameterId) const noexcept override
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  172. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  173. return fParamBuffers[parameterId];
  174. }
  175. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  176. {
  177. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  178. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  179. const int32_t rindex(pData->param.data[parameterId].rindex);
  180. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  181. {
  182. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  183. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, 0.0f);
  184. const LADSPA_RDF_ScalePoint* const scalePoint(&port->ScalePoints[scalePointId]);
  185. return scalePoint->Value;
  186. }
  187. return 0.0f;
  188. }
  189. void getLabel(char* const strBuf) const noexcept override
  190. {
  191. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  192. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Label != nullptr, nullStrBuf(strBuf));
  193. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  194. }
  195. void getMaker(char* const strBuf) const noexcept override
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  198. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Maker != nullptr, nullStrBuf(strBuf));
  199. if (fRdfDescriptor != nullptr && fRdfDescriptor->Creator != nullptr)
  200. {
  201. std::strncpy(strBuf, fRdfDescriptor->Creator, STR_MAX);
  202. return;
  203. }
  204. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  205. }
  206. void getCopyright(char* const strBuf) const noexcept override
  207. {
  208. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  209. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Copyright != nullptr, nullStrBuf(strBuf));
  210. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  211. }
  212. void getRealName(char* const strBuf) const noexcept override
  213. {
  214. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  215. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Name != nullptr, nullStrBuf(strBuf));
  216. if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  217. {
  218. std::strncpy(strBuf, fRdfDescriptor->Title, STR_MAX);
  219. return;
  220. }
  221. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  222. }
  223. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  224. {
  225. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  226. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  227. const int32_t rindex(pData->param.data[parameterId].rindex);
  228. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  229. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames[rindex] != nullptr, nullStrBuf(strBuf));
  230. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, true))
  231. return;
  232. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  233. }
  234. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  235. {
  236. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  237. const int32_t rindex(pData->param.data[parameterId].rindex);
  238. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  239. {
  240. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  241. if (LADSPA_PORT_HAS_LABEL(port->Hints))
  242. {
  243. CARLA_SAFE_ASSERT_RETURN(port->Label != nullptr, nullStrBuf(strBuf));
  244. std::strncpy(strBuf, port->Label, STR_MAX);
  245. return;
  246. }
  247. }
  248. nullStrBuf(strBuf);
  249. }
  250. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  251. {
  252. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  253. const int32_t rindex(pData->param.data[parameterId].rindex);
  254. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  255. {
  256. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  257. if (LADSPA_PORT_HAS_UNIT(port->Hints))
  258. {
  259. switch (port->Unit)
  260. {
  261. case LADSPA_UNIT_DB:
  262. std::strncpy(strBuf, "dB", STR_MAX);
  263. return;
  264. case LADSPA_UNIT_COEF:
  265. std::strncpy(strBuf, "(coef)", STR_MAX);
  266. return;
  267. case LADSPA_UNIT_HZ:
  268. std::strncpy(strBuf, "Hz", STR_MAX);
  269. return;
  270. case LADSPA_UNIT_S:
  271. std::strncpy(strBuf, "s", STR_MAX);
  272. return;
  273. case LADSPA_UNIT_MS:
  274. std::strncpy(strBuf, "ms", STR_MAX);
  275. return;
  276. case LADSPA_UNIT_MIN:
  277. std::strncpy(strBuf, "min", STR_MAX);
  278. return;
  279. }
  280. }
  281. }
  282. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, false))
  283. return;
  284. nullStrBuf(strBuf);
  285. }
  286. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  287. {
  288. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, nullStrBuf(strBuf));
  289. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  290. const int32_t rindex(pData->param.data[parameterId].rindex);
  291. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  292. {
  293. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  294. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, nullStrBuf(strBuf));
  295. const LADSPA_RDF_ScalePoint* const scalePoint(&port->ScalePoints[scalePointId]);
  296. CARLA_SAFE_ASSERT_RETURN(scalePoint->Label != nullptr, nullStrBuf(strBuf));
  297. std::strncpy(strBuf, scalePoint->Label, STR_MAX);
  298. return;
  299. }
  300. nullStrBuf(strBuf);
  301. }
  302. // -------------------------------------------------------------------
  303. // Set data (state)
  304. // nothing
  305. // -------------------------------------------------------------------
  306. // Set data (internal stuff)
  307. // nothing
  308. // -------------------------------------------------------------------
  309. // Set data (plugin-specific stuff)
  310. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  311. {
  312. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  313. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  314. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  315. fParamBuffers[parameterId] = fixedValue;
  316. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  317. }
  318. // -------------------------------------------------------------------
  319. // Set ui stuff
  320. // nothing
  321. // -------------------------------------------------------------------
  322. // Plugin state
  323. void reload() override
  324. {
  325. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  326. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  327. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  328. carla_debug("LadspaPlugin::reload() - start");
  329. const EngineProcessMode processMode(pData->engine->getProccessMode());
  330. // Safely disable plugin for reload
  331. const ScopedDisabler sd(this);
  332. if (pData->active)
  333. deactivate();
  334. clearBuffers();
  335. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  336. const uint32_t portCount(getSafePortCount());
  337. uint32_t aIns, aOuts, params;
  338. aIns = aOuts = params = 0;
  339. bool forcedStereoIn, forcedStereoOut;
  340. forcedStereoIn = forcedStereoOut = false;
  341. bool needsCtrlIn, needsCtrlOut;
  342. needsCtrlIn = needsCtrlOut = false;
  343. for (uint32_t i=0; i < portCount; ++i)
  344. {
  345. const LADSPA_PortDescriptor portType(fDescriptor->PortDescriptors[i]);
  346. if (LADSPA_IS_PORT_AUDIO(portType))
  347. {
  348. if (LADSPA_IS_PORT_INPUT(portType))
  349. aIns += 1;
  350. else if (LADSPA_IS_PORT_OUTPUT(portType))
  351. aOuts += 1;
  352. }
  353. else if (LADSPA_IS_PORT_CONTROL(portType))
  354. params += 1;
  355. }
  356. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  357. {
  358. if (fHandle2 == nullptr)
  359. {
  360. try {
  361. fHandle2 = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(sampleRate));
  362. } CARLA_SAFE_EXCEPTION("LADSPA instantiate #2");
  363. }
  364. if (fHandle2 != nullptr)
  365. {
  366. if (aIns == 1)
  367. {
  368. aIns = 2;
  369. forcedStereoIn = true;
  370. }
  371. if (aOuts == 1)
  372. {
  373. aOuts = 2;
  374. forcedStereoOut = true;
  375. }
  376. }
  377. }
  378. if (aIns > 0)
  379. {
  380. pData->audioIn.createNew(aIns);
  381. fAudioInBuffers = new float*[aIns];
  382. for (uint32_t i=0; i < aIns; ++i)
  383. fAudioInBuffers[i] = nullptr;
  384. }
  385. if (aOuts > 0)
  386. {
  387. pData->audioOut.createNew(aOuts);
  388. fAudioOutBuffers = new float*[aOuts];
  389. needsCtrlIn = true;
  390. for (uint32_t i=0; i < aOuts; ++i)
  391. fAudioOutBuffers[i] = nullptr;
  392. }
  393. if (params > 0)
  394. {
  395. pData->param.createNew(params, true);
  396. fParamBuffers = new float[params];
  397. FLOAT_CLEAR(fParamBuffers, params);
  398. }
  399. const uint portNameSize(pData->engine->getMaxPortNameSize());
  400. CarlaString portName;
  401. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  402. {
  403. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  404. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  405. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  406. if (LADSPA_IS_PORT_AUDIO(portType))
  407. {
  408. portName.clear();
  409. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  410. {
  411. portName = pData->name;
  412. portName += ":";
  413. }
  414. if (fDescriptor->PortNames[i] != nullptr && fDescriptor->PortNames[i][0] != '\0')
  415. {
  416. portName += fDescriptor->PortNames[i];
  417. }
  418. else
  419. {
  420. if (LADSPA_IS_PORT_INPUT(portType))
  421. {
  422. if (aIns > 1)
  423. {
  424. portName += "audio-in_";
  425. portName += CarlaString(iAudioIn+1);
  426. }
  427. else
  428. portName += "audio-in";
  429. }
  430. else
  431. {
  432. if (aOuts > 1)
  433. {
  434. portName += "audio-out_";
  435. portName += CarlaString(iAudioOut+1);
  436. }
  437. else
  438. portName += "audio-out";
  439. }
  440. }
  441. portName.truncate(portNameSize);
  442. if (LADSPA_IS_PORT_INPUT(portType))
  443. {
  444. const uint32_t j = iAudioIn++;
  445. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  446. pData->audioIn.ports[j].rindex = i;
  447. if (forcedStereoIn)
  448. {
  449. portName += "_2";
  450. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  451. pData->audioIn.ports[1].rindex = i;
  452. }
  453. }
  454. else if (LADSPA_IS_PORT_OUTPUT(portType))
  455. {
  456. const uint32_t j = iAudioOut++;
  457. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  458. pData->audioOut.ports[j].rindex = i;
  459. if (forcedStereoOut)
  460. {
  461. portName += "_2";
  462. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  463. pData->audioOut.ports[1].rindex = i;
  464. }
  465. }
  466. else
  467. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  468. }
  469. else if (LADSPA_IS_PORT_CONTROL(portType))
  470. {
  471. const uint32_t j = iCtrl++;
  472. pData->param.data[j].index = static_cast<int32_t>(j);
  473. pData->param.data[j].rindex = static_cast<int32_t>(i);
  474. const char* const paramName(fDescriptor->PortNames[i] != nullptr ? fDescriptor->PortNames[i] : "unknown");
  475. float min, max, def, step, stepSmall, stepLarge;
  476. // min value
  477. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  478. min = portRangeHints.LowerBound;
  479. else
  480. min = 0.0f;
  481. // max value
  482. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  483. max = portRangeHints.UpperBound;
  484. else
  485. max = 1.0f;
  486. if (min > max)
  487. {
  488. carla_stderr2("WARNING - Broken plugin parameter '%s': min > max", paramName);
  489. min = max - 0.1f;
  490. }
  491. else if (min == max)
  492. {
  493. carla_stderr2("WARNING - Broken plugin parameter '%s': min == maxf", paramName);
  494. max = min + 0.1f;
  495. }
  496. // default value
  497. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  498. def = fRdfDescriptor->Ports[i].Default;
  499. else
  500. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  501. if (def < min)
  502. def = min;
  503. else if (def > max)
  504. def = max;
  505. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  506. {
  507. min *= sampleRate;
  508. max *= sampleRate;
  509. def *= sampleRate;
  510. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  511. }
  512. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  513. {
  514. step = max - min;
  515. stepSmall = step;
  516. stepLarge = step;
  517. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  518. }
  519. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  520. {
  521. step = 1.0f;
  522. stepSmall = 1.0f;
  523. stepLarge = 10.0f;
  524. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  525. }
  526. else
  527. {
  528. const float range = max - min;
  529. step = range/100.0f;
  530. stepSmall = range/1000.0f;
  531. stepLarge = range/10.0f;
  532. }
  533. if (LADSPA_IS_PORT_INPUT(portType))
  534. {
  535. pData->param.data[j].type = PARAMETER_INPUT;
  536. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  537. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  538. needsCtrlIn = true;
  539. }
  540. else if (LADSPA_IS_PORT_OUTPUT(portType))
  541. {
  542. pData->param.data[j].type = PARAMETER_OUTPUT;
  543. if (std::strcmp(paramName, "latency") == 0 || std::strcmp(paramName, "_latency") == 0)
  544. {
  545. min = 0.0f;
  546. max = sampleRate;
  547. def = 0.0f;
  548. step = 1.0f;
  549. stepSmall = 1.0f;
  550. stepLarge = 1.0f;
  551. fLatencyIndex = static_cast<int32_t>(j);
  552. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  553. }
  554. else
  555. {
  556. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  557. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  558. needsCtrlOut = true;
  559. }
  560. }
  561. else
  562. {
  563. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  564. }
  565. // extra parameter hints
  566. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  567. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  568. // check for scalepoints, require at least 2 to make it useful
  569. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount >= 2)
  570. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  571. pData->param.ranges[j].min = min;
  572. pData->param.ranges[j].max = max;
  573. pData->param.ranges[j].def = def;
  574. pData->param.ranges[j].step = step;
  575. pData->param.ranges[j].stepSmall = stepSmall;
  576. pData->param.ranges[j].stepLarge = stepLarge;
  577. // Start parameters in their default values
  578. fParamBuffers[j] = def;
  579. try {
  580. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  581. } CARLA_SAFE_EXCEPTION("LADSPA connect_port parameter");
  582. if (fHandle2 != nullptr)
  583. {
  584. try {
  585. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  586. } CARLA_SAFE_EXCEPTION("LADSPA connect_port parameter #2");
  587. }
  588. }
  589. else
  590. {
  591. // Not Audio or Control
  592. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  593. try {
  594. fDescriptor->connect_port(fHandle, i, nullptr);
  595. } CARLA_SAFE_EXCEPTION("LADSPA connect_port null");
  596. if (fHandle2 != nullptr)
  597. {
  598. try {
  599. fDescriptor->connect_port(fHandle2, i, nullptr);
  600. } CARLA_SAFE_EXCEPTION("LADSPA connect_port null #2");
  601. }
  602. }
  603. }
  604. if (needsCtrlIn)
  605. {
  606. portName.clear();
  607. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  608. {
  609. portName = pData->name;
  610. portName += ":";
  611. }
  612. portName += "events-in";
  613. portName.truncate(portNameSize);
  614. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  615. }
  616. if (needsCtrlOut)
  617. {
  618. portName.clear();
  619. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  620. {
  621. portName = pData->name;
  622. portName += ":";
  623. }
  624. portName += "events-out";
  625. portName.truncate(portNameSize);
  626. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  627. }
  628. if (forcedStereoIn || forcedStereoOut)
  629. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  630. else
  631. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  632. // plugin hints
  633. pData->hints = 0x0;
  634. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  635. pData->hints |= PLUGIN_IS_RTSAFE;
  636. #ifndef BUILD_BRIDGE
  637. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  638. pData->hints |= PLUGIN_CAN_DRYWET;
  639. if (aOuts > 0)
  640. pData->hints |= PLUGIN_CAN_VOLUME;
  641. if (aOuts >= 2 && aOuts % 2 == 0)
  642. pData->hints |= PLUGIN_CAN_BALANCE;
  643. #endif
  644. // extra plugin hints
  645. pData->extraHints = 0x0;
  646. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  647. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  648. // check latency
  649. if (fLatencyIndex >= 0)
  650. {
  651. // we need to pre-run the plugin so it can update its latency control-port
  652. float tmpIn[aIns][2];
  653. float tmpOut[aOuts][2];
  654. for (uint32_t j=0; j < aIns; ++j)
  655. {
  656. tmpIn[j][0] = 0.0f;
  657. tmpIn[j][1] = 0.0f;
  658. try {
  659. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  660. } CARLA_SAFE_EXCEPTION("LADSPA connect_port latency input");
  661. }
  662. for (uint32_t j=0; j < aOuts; ++j)
  663. {
  664. tmpOut[j][0] = 0.0f;
  665. tmpOut[j][1] = 0.0f;
  666. try {
  667. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  668. } CARLA_SAFE_EXCEPTION("LADSPA connect_port latency output");
  669. }
  670. if (fDescriptor->activate != nullptr)
  671. {
  672. try {
  673. fDescriptor->activate(fHandle);
  674. } CARLA_SAFE_EXCEPTION("LADSPA latency activate");
  675. }
  676. try {
  677. fDescriptor->run(fHandle, 2);
  678. } CARLA_SAFE_EXCEPTION("LADSPA latency run");
  679. if (fDescriptor->deactivate != nullptr)
  680. {
  681. try {
  682. fDescriptor->deactivate(fHandle);
  683. } CARLA_SAFE_EXCEPTION("LADSPA latency deactivate");
  684. }
  685. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  686. if (latency >= 0)
  687. {
  688. const uint32_t ulatency(static_cast<uint32_t>(latency));
  689. if (pData->latency != ulatency)
  690. {
  691. pData->latency = ulatency;
  692. pData->client->setLatency(ulatency);
  693. carla_stdout("latency = %i", latency);
  694. #ifndef BUILD_BRIDGE
  695. pData->recreateLatencyBuffers();
  696. #endif
  697. }
  698. }
  699. else
  700. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  701. }
  702. bufferSizeChanged(pData->engine->getBufferSize());
  703. if (pData->active)
  704. activate();
  705. carla_debug("LadspaPlugin::reload() - end");
  706. }
  707. // -------------------------------------------------------------------
  708. // Plugin processing
  709. void activate() noexcept override
  710. {
  711. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  712. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  713. if (fDescriptor->activate != nullptr)
  714. {
  715. try {
  716. fDescriptor->activate(fHandle);
  717. } CARLA_SAFE_EXCEPTION("LADSPA activate");
  718. if (fHandle2 != nullptr)
  719. {
  720. try {
  721. fDescriptor->activate(fHandle2);
  722. } CARLA_SAFE_EXCEPTION("LADSPA activate #2");
  723. }
  724. }
  725. }
  726. void deactivate() noexcept override
  727. {
  728. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  729. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  730. if (fDescriptor->deactivate != nullptr)
  731. {
  732. try {
  733. fDescriptor->deactivate(fHandle);
  734. } CARLA_SAFE_EXCEPTION("LADSPA deactivate");
  735. if (fHandle2 != nullptr)
  736. {
  737. try {
  738. fDescriptor->deactivate(fHandle2);
  739. } CARLA_SAFE_EXCEPTION("LADSPA deactivate #2");
  740. }
  741. }
  742. }
  743. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  744. {
  745. // --------------------------------------------------------------------------------------------------------
  746. // Check if active
  747. if (! pData->active)
  748. {
  749. // disable any output sound
  750. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  751. FLOAT_CLEAR(outBuffer[i], frames);
  752. return;
  753. }
  754. // --------------------------------------------------------------------------------------------------------
  755. // Check if needs reset
  756. if (pData->needsReset)
  757. {
  758. #ifndef BUILD_BRIDGE
  759. if (pData->latency > 0)
  760. {
  761. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  762. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  763. }
  764. #endif
  765. pData->needsReset = false;
  766. }
  767. // --------------------------------------------------------------------------------------------------------
  768. // Event Input and Processing
  769. if (pData->event.portIn != nullptr)
  770. {
  771. // ----------------------------------------------------------------------------------------------------
  772. // Event Input (System)
  773. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  774. uint32_t numEvents = pData->event.portIn->getEventCount();
  775. uint32_t timeOffset = 0;
  776. for (uint32_t i=0; i < numEvents; ++i)
  777. {
  778. const EngineEvent& event(pData->event.portIn->getEvent(i));
  779. if (event.time >= frames)
  780. continue;
  781. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  782. if (isSampleAccurate && event.time > timeOffset)
  783. {
  784. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset))
  785. timeOffset = event.time;
  786. }
  787. switch (event.type)
  788. {
  789. case kEngineEventTypeNull:
  790. break;
  791. case kEngineEventTypeControl: {
  792. const EngineControlEvent& ctrlEvent(event.ctrl);
  793. switch (ctrlEvent.type)
  794. {
  795. case kEngineControlEventTypeNull:
  796. break;
  797. case kEngineControlEventTypeParameter: {
  798. #ifndef BUILD_BRIDGE
  799. // Control backend stuff
  800. if (event.channel == pData->ctrlChannel)
  801. {
  802. float value;
  803. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  804. {
  805. value = ctrlEvent.value;
  806. setDryWet(value, false, false);
  807. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  808. break;
  809. }
  810. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  811. {
  812. value = ctrlEvent.value*127.0f/100.0f;
  813. setVolume(value, false, false);
  814. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  815. break;
  816. }
  817. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  818. {
  819. float left, right;
  820. value = ctrlEvent.value/0.5f - 1.0f;
  821. if (value < 0.0f)
  822. {
  823. left = -1.0f;
  824. right = (value*2.0f)+1.0f;
  825. }
  826. else if (value > 0.0f)
  827. {
  828. left = (value*2.0f)-1.0f;
  829. right = 1.0f;
  830. }
  831. else
  832. {
  833. left = -1.0f;
  834. right = 1.0f;
  835. }
  836. setBalanceLeft(left, false, false);
  837. setBalanceRight(right, false, false);
  838. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  839. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  840. break;
  841. }
  842. }
  843. #endif
  844. // Control plugin parameters
  845. for (uint32_t k=0; k < pData->param.count; ++k)
  846. {
  847. if (pData->param.data[k].midiChannel != event.channel)
  848. continue;
  849. if (pData->param.data[k].midiCC != ctrlEvent.param)
  850. continue;
  851. if (pData->param.data[k].type != PARAMETER_INPUT)
  852. continue;
  853. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  854. continue;
  855. float value;
  856. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  857. {
  858. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  859. }
  860. else
  861. {
  862. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  863. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  864. value = std::rint(value);
  865. }
  866. setParameterValue(k, value, false, false, false);
  867. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  868. break;
  869. }
  870. break;
  871. } // case kEngineControlEventTypeParameter
  872. case kEngineControlEventTypeMidiBank:
  873. case kEngineControlEventTypeMidiProgram:
  874. case kEngineControlEventTypeAllSoundOff:
  875. case kEngineControlEventTypeAllNotesOff:
  876. break;
  877. } // switch (ctrlEvent.type)
  878. break;
  879. } // case kEngineEventTypeControl
  880. case kEngineEventTypeMidi:
  881. break;
  882. } // switch (event.type)
  883. }
  884. pData->postRtEvents.trySplice();
  885. if (frames > timeOffset)
  886. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  887. } // End of Event Input and Processing
  888. // --------------------------------------------------------------------------------------------------------
  889. // Plugin processing (no events)
  890. else
  891. {
  892. processSingle(inBuffer, outBuffer, frames, 0);
  893. } // End of Plugin processing (no events)
  894. // --------------------------------------------------------------------------------------------------------
  895. // Latency, save values for next callback
  896. if (pData->latency > 0)
  897. {
  898. if (pData->latency <= frames)
  899. {
  900. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  901. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i]+(frames-pData->latency), pData->latency);
  902. }
  903. else
  904. {
  905. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  906. {
  907. for (k=0; k < pData->latency-frames; ++k)
  908. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  909. for (j=0; k < pData->latency; ++j, ++k)
  910. pData->latencyBuffers[i][k] = inBuffer[i][j];
  911. }
  912. }
  913. }
  914. // --------------------------------------------------------------------------------------------------------
  915. // Control Output
  916. if (pData->event.portOut != nullptr)
  917. {
  918. uint8_t channel;
  919. uint16_t param;
  920. float value;
  921. for (uint32_t k=0; k < pData->param.count; ++k)
  922. {
  923. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  924. continue;
  925. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  926. if (pData->param.data[k].midiCC > 0)
  927. {
  928. channel = pData->param.data[k].midiChannel;
  929. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  930. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  931. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  932. }
  933. }
  934. } // End of Control Output
  935. }
  936. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  937. {
  938. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  939. if (pData->audioIn.count > 0)
  940. {
  941. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  942. }
  943. if (pData->audioOut.count > 0)
  944. {
  945. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  946. }
  947. // --------------------------------------------------------------------------------------------------------
  948. // Try lock, silence otherwise
  949. if (pData->engine->isOffline())
  950. {
  951. pData->singleMutex.lock();
  952. }
  953. else if (! pData->singleMutex.tryLock())
  954. {
  955. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  956. {
  957. for (uint32_t k=0; k < frames; ++k)
  958. outBuffer[i][k+timeOffset] = 0.0f;
  959. }
  960. return false;
  961. }
  962. // --------------------------------------------------------------------------------------------------------
  963. // Reset audio buffers
  964. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  965. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  966. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  967. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  968. // --------------------------------------------------------------------------------------------------------
  969. // Run plugin
  970. try {
  971. fDescriptor->run(fHandle, frames);
  972. } CARLA_SAFE_EXCEPTION("LADSPA run");
  973. if (fHandle2 != nullptr)
  974. {
  975. try {
  976. fDescriptor->run(fHandle2, frames);
  977. } CARLA_SAFE_EXCEPTION("LADSPA run #2");
  978. }
  979. #ifndef BUILD_BRIDGE
  980. // --------------------------------------------------------------------------------------------------------
  981. // Post-processing (dry/wet, volume and balance)
  982. {
  983. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  984. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  985. const bool isMono = (pData->audioIn.count == 1);
  986. bool isPair;
  987. float bufValue, oldBufLeft[doBalance ? frames : 1];
  988. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  989. {
  990. // Dry/Wet
  991. if (doDryWet)
  992. {
  993. for (uint32_t k=0; k < frames; ++k)
  994. {
  995. if (k < pData->latency)
  996. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  997. else if (pData->latency < frames)
  998. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  999. else
  1000. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  1001. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1002. }
  1003. }
  1004. // Balance
  1005. if (doBalance)
  1006. {
  1007. isPair = (i % 2 == 0);
  1008. if (isPair)
  1009. {
  1010. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1011. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1012. }
  1013. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1014. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1015. for (uint32_t k=0; k < frames; ++k)
  1016. {
  1017. if (isPair)
  1018. {
  1019. // left
  1020. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1021. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1022. }
  1023. else
  1024. {
  1025. // right
  1026. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1027. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1028. }
  1029. }
  1030. }
  1031. // Volume (and buffer copy)
  1032. {
  1033. for (uint32_t k=0; k < frames; ++k)
  1034. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1035. }
  1036. }
  1037. } // End of Post-processing
  1038. #else // BUILD_BRIDGE
  1039. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1040. {
  1041. for (uint32_t k=0; k < frames; ++k)
  1042. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1043. }
  1044. #endif
  1045. // --------------------------------------------------------------------------------------------------------
  1046. pData->singleMutex.unlock();
  1047. return true;
  1048. }
  1049. void bufferSizeChanged(const uint32_t newBufferSize) override
  1050. {
  1051. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1052. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1053. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1054. {
  1055. if (fAudioInBuffers[i] != nullptr)
  1056. delete[] fAudioInBuffers[i];
  1057. fAudioInBuffers[i] = new float[newBufferSize];
  1058. }
  1059. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1060. {
  1061. if (fAudioOutBuffers[i] != nullptr)
  1062. delete[] fAudioOutBuffers[i];
  1063. fAudioOutBuffers[i] = new float[newBufferSize];
  1064. }
  1065. if (fHandle2 == nullptr)
  1066. {
  1067. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1068. {
  1069. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1070. try {
  1071. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1072. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input");
  1073. }
  1074. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1075. {
  1076. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1077. try {
  1078. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1079. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output");
  1080. }
  1081. }
  1082. else
  1083. {
  1084. if (pData->audioIn.count > 0)
  1085. {
  1086. CARLA_ASSERT(pData->audioIn.count == 2);
  1087. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1088. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1089. try {
  1090. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1091. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #1");
  1092. try {
  1093. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1094. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #2");
  1095. }
  1096. if (pData->audioOut.count > 0)
  1097. {
  1098. CARLA_ASSERT(pData->audioOut.count == 2);
  1099. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1100. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1101. try {
  1102. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1103. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #1");
  1104. try {
  1105. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1106. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #2");
  1107. }
  1108. }
  1109. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1110. }
  1111. void sampleRateChanged(const double newSampleRate) override
  1112. {
  1113. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1114. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1115. // TODO
  1116. (void)newSampleRate;
  1117. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1118. }
  1119. // -------------------------------------------------------------------
  1120. // Plugin buffers
  1121. void clearBuffers() noexcept override
  1122. {
  1123. carla_debug("LadspaPlugin::clearBuffers() - start");
  1124. if (fAudioInBuffers != nullptr)
  1125. {
  1126. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1127. {
  1128. if (fAudioInBuffers[i] != nullptr)
  1129. {
  1130. delete[] fAudioInBuffers[i];
  1131. fAudioInBuffers[i] = nullptr;
  1132. }
  1133. }
  1134. delete[] fAudioInBuffers;
  1135. fAudioInBuffers = nullptr;
  1136. }
  1137. if (fAudioOutBuffers != nullptr)
  1138. {
  1139. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1140. {
  1141. if (fAudioOutBuffers[i] != nullptr)
  1142. {
  1143. delete[] fAudioOutBuffers[i];
  1144. fAudioOutBuffers[i] = nullptr;
  1145. }
  1146. }
  1147. delete[] fAudioOutBuffers;
  1148. fAudioOutBuffers = nullptr;
  1149. }
  1150. if (fParamBuffers != nullptr)
  1151. {
  1152. delete[] fParamBuffers;
  1153. fParamBuffers = nullptr;
  1154. }
  1155. CarlaPlugin::clearBuffers();
  1156. carla_debug("LadspaPlugin::clearBuffers() - end");
  1157. }
  1158. // -------------------------------------------------------------------
  1159. const void* getExtraStuff() const noexcept override
  1160. {
  1161. return fRdfDescriptor;
  1162. }
  1163. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1164. {
  1165. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1166. // ---------------------------------------------------------------
  1167. // first checks
  1168. if (pData->client != nullptr)
  1169. {
  1170. pData->engine->setLastError("Plugin client is already registered");
  1171. return false;
  1172. }
  1173. if (filename == nullptr || filename[0] == '\0')
  1174. {
  1175. pData->engine->setLastError("null filename");
  1176. return false;
  1177. }
  1178. if (label == nullptr || label[0] == '\0')
  1179. {
  1180. pData->engine->setLastError("null label");
  1181. return false;
  1182. }
  1183. // ---------------------------------------------------------------
  1184. // open DLL
  1185. if (! pData->libOpen(filename))
  1186. {
  1187. pData->engine->setLastError(pData->libError(filename));
  1188. return false;
  1189. }
  1190. // ---------------------------------------------------------------
  1191. // get DLL main entry
  1192. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)pData->libSymbol("ladspa_descriptor");
  1193. if (descFn == nullptr)
  1194. {
  1195. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1196. return false;
  1197. }
  1198. // ---------------------------------------------------------------
  1199. // get descriptor that matches label
  1200. ulong i = 0;
  1201. for (;;)
  1202. {
  1203. try {
  1204. fDescriptor = descFn(i++);
  1205. }
  1206. catch(...) {
  1207. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1208. fDescriptor = nullptr;
  1209. break;
  1210. }
  1211. if (fDescriptor == nullptr)
  1212. break;
  1213. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1214. {
  1215. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1216. fDescriptor = nullptr;
  1217. break;
  1218. }
  1219. if (fDescriptor->run == nullptr)
  1220. {
  1221. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1222. fDescriptor = nullptr;
  1223. break;
  1224. }
  1225. if (std::strcmp(fDescriptor->Label, label) == 0)
  1226. break;
  1227. }
  1228. if (fDescriptor == nullptr)
  1229. {
  1230. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1231. return false;
  1232. }
  1233. // ---------------------------------------------------------------
  1234. // get info
  1235. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1236. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1237. if (name != nullptr && name[0] != '\0')
  1238. pData->name = pData->engine->getUniquePluginName(name);
  1239. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  1240. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1241. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1242. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1243. else
  1244. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1245. pData->filename = carla_strdup(filename);
  1246. // ---------------------------------------------------------------
  1247. // register client
  1248. pData->client = pData->engine->addClient(this);
  1249. if (pData->client == nullptr || ! pData->client->isOk())
  1250. {
  1251. pData->engine->setLastError("Failed to register plugin client");
  1252. return false;
  1253. }
  1254. // ---------------------------------------------------------------
  1255. // initialize plugin
  1256. try {
  1257. fHandle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  1258. } CARLA_SAFE_EXCEPTION("LADSPA instantiate");
  1259. if (fHandle == nullptr)
  1260. {
  1261. pData->engine->setLastError("Plugin failed to initialize");
  1262. return false;
  1263. }
  1264. // ---------------------------------------------------------------
  1265. // load plugin settings
  1266. {
  1267. #ifdef __USE_GNU
  1268. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  1269. #else
  1270. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  1271. #endif
  1272. // set default options
  1273. pData->options = 0x0;
  1274. if (fLatencyIndex >= 0 || isDssiVst)
  1275. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1276. if (pData->engine->getOptions().forceStereo)
  1277. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1278. #ifndef BUILD_BRIDGE
  1279. // set identifier string
  1280. CarlaString identifier("LADSPA/");
  1281. identifier += CarlaString(getUniqueId());
  1282. identifier += ",";
  1283. identifier += label;
  1284. pData->identifier = identifier.dup();
  1285. // load settings
  1286. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1287. // ignore settings, we need this anyway
  1288. if (fLatencyIndex >= 0 || isDssiVst)
  1289. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1290. #endif
  1291. }
  1292. return true;
  1293. }
  1294. // -------------------------------------------------------------------
  1295. private:
  1296. LADSPA_Handle fHandle;
  1297. LADSPA_Handle fHandle2;
  1298. const LADSPA_Descriptor* fDescriptor;
  1299. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1300. float** fAudioInBuffers;
  1301. float** fAudioOutBuffers;
  1302. float* fParamBuffers;
  1303. int32_t fLatencyIndex; // -1 if invalid
  1304. uint32_t getSafePortCount() const noexcept
  1305. {
  1306. if (fDescriptor->PortCount == 0)
  1307. return 0;
  1308. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1309. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1310. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1311. return static_cast<uint32_t>(fDescriptor->PortCount);
  1312. }
  1313. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1314. {
  1315. const char* const sepBracketStart = std::strstr(paramName, " [");
  1316. if (sepBracketStart == nullptr)
  1317. return false;
  1318. const char* const sepBracketEnd = std::strstr(sepBracketStart, "]");
  1319. if (sepBracketEnd == nullptr)
  1320. return false;
  1321. const size_t unitSize = static_cast<size_t>(sepBracketEnd-sepBracketStart-2);
  1322. if (unitSize > 4) // very unlikely to have such big unit
  1323. return false;
  1324. const size_t sepIndex(std::strlen(paramName) - unitSize - 3);
  1325. // just in case
  1326. if (sepIndex > STR_MAX)
  1327. return false;
  1328. if (wantName)
  1329. {
  1330. std::strncpy(strBuf, paramName, sepIndex);
  1331. strBuf[sepIndex] = '\0';
  1332. }
  1333. else
  1334. {
  1335. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  1336. strBuf[unitSize] = '\0';
  1337. }
  1338. return true;
  1339. }
  1340. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1341. };
  1342. CARLA_BACKEND_END_NAMESPACE
  1343. #endif // WANT_LADSPA
  1344. // -------------------------------------------------------------------------------------------------------------------
  1345. CARLA_BACKEND_START_NAMESPACE
  1346. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1347. {
  1348. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %p)", init.engine, init.filename, init.name, init.label, init.uniqueId, rdfDescriptor);
  1349. #ifdef WANT_LADSPA
  1350. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1351. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1352. {
  1353. delete plugin;
  1354. return nullptr;
  1355. }
  1356. plugin->reload();
  1357. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1358. {
  1359. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1360. delete plugin;
  1361. return nullptr;
  1362. }
  1363. return plugin;
  1364. #else
  1365. init.engine->setLastError("LADSPA support not available");
  1366. return nullptr;
  1367. #endif
  1368. }
  1369. CARLA_BACKEND_END_NAMESPACE
  1370. // -------------------------------------------------------------------------------------------------------------------