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.

1737 lines
60KB

  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. carla_stdout("latency = %i", latency);
  692. pData->latency = ulatency;
  693. pData->client->setLatency(ulatency);
  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. if (fLatencyIndex < 0)
  726. return;
  727. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  728. CARLA_SAFE_ASSERT_RETURN(latency >= 0,);
  729. const uint32_t ulatency(static_cast<uint32_t>(latency));
  730. if (pData->latency != ulatency)
  731. {
  732. carla_stdout("latency changed to %i", latency);
  733. pData->latency = ulatency;
  734. pData->client->setLatency(ulatency);
  735. #ifndef BUILD_BRIDGE
  736. pData->recreateLatencyBuffers();
  737. #endif
  738. }
  739. else
  740. carla_stdout("latency still the same %i", latency);
  741. }
  742. void deactivate() noexcept override
  743. {
  744. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  745. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  746. if (fDescriptor->deactivate != nullptr)
  747. {
  748. try {
  749. fDescriptor->deactivate(fHandle);
  750. } CARLA_SAFE_EXCEPTION("LADSPA deactivate");
  751. if (fHandle2 != nullptr)
  752. {
  753. try {
  754. fDescriptor->deactivate(fHandle2);
  755. } CARLA_SAFE_EXCEPTION("LADSPA deactivate #2");
  756. }
  757. }
  758. }
  759. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  760. {
  761. // --------------------------------------------------------------------------------------------------------
  762. // Check if active
  763. if (! pData->active)
  764. {
  765. // disable any output sound
  766. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  767. FLOAT_CLEAR(outBuffer[i], frames);
  768. return;
  769. }
  770. // --------------------------------------------------------------------------------------------------------
  771. // Check if needs reset
  772. if (pData->needsReset)
  773. {
  774. #ifndef BUILD_BRIDGE
  775. if (pData->latency > 0)
  776. {
  777. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  778. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  779. }
  780. #endif
  781. pData->needsReset = false;
  782. }
  783. // --------------------------------------------------------------------------------------------------------
  784. // Event Input and Processing
  785. if (pData->event.portIn != nullptr)
  786. {
  787. // ----------------------------------------------------------------------------------------------------
  788. // Event Input (System)
  789. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  790. uint32_t numEvents = pData->event.portIn->getEventCount();
  791. uint32_t timeOffset = 0;
  792. for (uint32_t i=0; i < numEvents; ++i)
  793. {
  794. const EngineEvent& event(pData->event.portIn->getEvent(i));
  795. if (event.time >= frames)
  796. continue;
  797. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  798. if (isSampleAccurate && event.time > timeOffset)
  799. {
  800. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset))
  801. timeOffset = event.time;
  802. }
  803. switch (event.type)
  804. {
  805. case kEngineEventTypeNull:
  806. break;
  807. case kEngineEventTypeControl: {
  808. const EngineControlEvent& ctrlEvent(event.ctrl);
  809. switch (ctrlEvent.type)
  810. {
  811. case kEngineControlEventTypeNull:
  812. break;
  813. case kEngineControlEventTypeParameter: {
  814. #ifndef BUILD_BRIDGE
  815. // Control backend stuff
  816. if (event.channel == pData->ctrlChannel)
  817. {
  818. float value;
  819. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  820. {
  821. value = ctrlEvent.value;
  822. setDryWet(value, false, false);
  823. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  824. break;
  825. }
  826. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  827. {
  828. value = ctrlEvent.value*127.0f/100.0f;
  829. setVolume(value, false, false);
  830. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  831. break;
  832. }
  833. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  834. {
  835. float left, right;
  836. value = ctrlEvent.value/0.5f - 1.0f;
  837. if (value < 0.0f)
  838. {
  839. left = -1.0f;
  840. right = (value*2.0f)+1.0f;
  841. }
  842. else if (value > 0.0f)
  843. {
  844. left = (value*2.0f)-1.0f;
  845. right = 1.0f;
  846. }
  847. else
  848. {
  849. left = -1.0f;
  850. right = 1.0f;
  851. }
  852. setBalanceLeft(left, false, false);
  853. setBalanceRight(right, false, false);
  854. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  855. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  856. break;
  857. }
  858. }
  859. #endif
  860. // Control plugin parameters
  861. for (uint32_t k=0; k < pData->param.count; ++k)
  862. {
  863. if (pData->param.data[k].midiChannel != event.channel)
  864. continue;
  865. if (pData->param.data[k].midiCC != ctrlEvent.param)
  866. continue;
  867. if (pData->param.data[k].type != PARAMETER_INPUT)
  868. continue;
  869. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  870. continue;
  871. float value;
  872. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  873. {
  874. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  875. }
  876. else
  877. {
  878. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  879. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  880. value = std::rint(value);
  881. }
  882. setParameterValue(k, value, false, false, false);
  883. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  884. break;
  885. }
  886. break;
  887. } // case kEngineControlEventTypeParameter
  888. case kEngineControlEventTypeMidiBank:
  889. case kEngineControlEventTypeMidiProgram:
  890. case kEngineControlEventTypeAllSoundOff:
  891. case kEngineControlEventTypeAllNotesOff:
  892. break;
  893. } // switch (ctrlEvent.type)
  894. break;
  895. } // case kEngineEventTypeControl
  896. case kEngineEventTypeMidi:
  897. break;
  898. } // switch (event.type)
  899. }
  900. pData->postRtEvents.trySplice();
  901. if (frames > timeOffset)
  902. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  903. } // End of Event Input and Processing
  904. // --------------------------------------------------------------------------------------------------------
  905. // Plugin processing (no events)
  906. else
  907. {
  908. processSingle(inBuffer, outBuffer, frames, 0);
  909. } // End of Plugin processing (no events)
  910. // --------------------------------------------------------------------------------------------------------
  911. // Latency, save values for next callback
  912. if (pData->latency > 0)
  913. {
  914. if (pData->latency <= frames)
  915. {
  916. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  917. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i]+(frames-pData->latency), pData->latency);
  918. }
  919. else
  920. {
  921. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  922. {
  923. for (k=0; k < pData->latency-frames; ++k)
  924. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  925. for (j=0; k < pData->latency; ++j, ++k)
  926. pData->latencyBuffers[i][k] = inBuffer[i][j];
  927. }
  928. }
  929. }
  930. // --------------------------------------------------------------------------------------------------------
  931. // Control Output
  932. if (pData->event.portOut != nullptr)
  933. {
  934. uint8_t channel;
  935. uint16_t param;
  936. float value;
  937. for (uint32_t k=0; k < pData->param.count; ++k)
  938. {
  939. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  940. continue;
  941. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  942. if (pData->param.data[k].midiCC > 0)
  943. {
  944. channel = pData->param.data[k].midiChannel;
  945. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  946. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  947. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  948. }
  949. }
  950. } // End of Control Output
  951. }
  952. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  953. {
  954. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  955. if (pData->audioIn.count > 0)
  956. {
  957. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  958. }
  959. if (pData->audioOut.count > 0)
  960. {
  961. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  962. }
  963. // --------------------------------------------------------------------------------------------------------
  964. // Try lock, silence otherwise
  965. if (pData->engine->isOffline())
  966. {
  967. pData->singleMutex.lock();
  968. }
  969. else if (! pData->singleMutex.tryLock())
  970. {
  971. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  972. {
  973. for (uint32_t k=0; k < frames; ++k)
  974. outBuffer[i][k+timeOffset] = 0.0f;
  975. }
  976. return false;
  977. }
  978. // --------------------------------------------------------------------------------------------------------
  979. // Reset audio buffers
  980. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  981. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  982. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  983. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  984. // --------------------------------------------------------------------------------------------------------
  985. // Run plugin
  986. try {
  987. fDescriptor->run(fHandle, frames);
  988. } CARLA_SAFE_EXCEPTION("LADSPA run");
  989. if (fHandle2 != nullptr)
  990. {
  991. try {
  992. fDescriptor->run(fHandle2, frames);
  993. } CARLA_SAFE_EXCEPTION("LADSPA run #2");
  994. }
  995. #ifndef BUILD_BRIDGE
  996. // --------------------------------------------------------------------------------------------------------
  997. // Post-processing (dry/wet, volume and balance)
  998. {
  999. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1000. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1001. const bool isMono = (pData->audioIn.count == 1);
  1002. bool isPair;
  1003. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1004. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1005. {
  1006. // Dry/Wet
  1007. if (doDryWet)
  1008. {
  1009. for (uint32_t k=0; k < frames; ++k)
  1010. {
  1011. if (k < pData->latency)
  1012. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  1013. else if (pData->latency < frames)
  1014. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  1015. else
  1016. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  1017. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1018. }
  1019. }
  1020. // Balance
  1021. if (doBalance)
  1022. {
  1023. isPair = (i % 2 == 0);
  1024. if (isPair)
  1025. {
  1026. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1027. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1028. }
  1029. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1030. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1031. for (uint32_t k=0; k < frames; ++k)
  1032. {
  1033. if (isPair)
  1034. {
  1035. // left
  1036. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1037. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1038. }
  1039. else
  1040. {
  1041. // right
  1042. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1043. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1044. }
  1045. }
  1046. }
  1047. // Volume (and buffer copy)
  1048. {
  1049. for (uint32_t k=0; k < frames; ++k)
  1050. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1051. }
  1052. }
  1053. } // End of Post-processing
  1054. #else // BUILD_BRIDGE
  1055. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1056. {
  1057. for (uint32_t k=0; k < frames; ++k)
  1058. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1059. }
  1060. #endif
  1061. // --------------------------------------------------------------------------------------------------------
  1062. pData->singleMutex.unlock();
  1063. return true;
  1064. }
  1065. void bufferSizeChanged(const uint32_t newBufferSize) override
  1066. {
  1067. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1068. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1069. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1070. {
  1071. if (fAudioInBuffers[i] != nullptr)
  1072. delete[] fAudioInBuffers[i];
  1073. fAudioInBuffers[i] = new float[newBufferSize];
  1074. }
  1075. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1076. {
  1077. if (fAudioOutBuffers[i] != nullptr)
  1078. delete[] fAudioOutBuffers[i];
  1079. fAudioOutBuffers[i] = new float[newBufferSize];
  1080. }
  1081. if (fHandle2 == nullptr)
  1082. {
  1083. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1084. {
  1085. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1086. try {
  1087. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1088. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input");
  1089. }
  1090. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1091. {
  1092. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1093. try {
  1094. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1095. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output");
  1096. }
  1097. }
  1098. else
  1099. {
  1100. if (pData->audioIn.count > 0)
  1101. {
  1102. CARLA_ASSERT(pData->audioIn.count == 2);
  1103. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1104. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1105. try {
  1106. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1107. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #1");
  1108. try {
  1109. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1110. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #2");
  1111. }
  1112. if (pData->audioOut.count > 0)
  1113. {
  1114. CARLA_ASSERT(pData->audioOut.count == 2);
  1115. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1116. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1117. try {
  1118. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1119. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #1");
  1120. try {
  1121. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1122. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #2");
  1123. }
  1124. }
  1125. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1126. }
  1127. void sampleRateChanged(const double newSampleRate) override
  1128. {
  1129. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1130. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1131. // TODO
  1132. (void)newSampleRate;
  1133. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1134. }
  1135. // -------------------------------------------------------------------
  1136. // Plugin buffers
  1137. void clearBuffers() noexcept override
  1138. {
  1139. carla_debug("LadspaPlugin::clearBuffers() - start");
  1140. if (fAudioInBuffers != nullptr)
  1141. {
  1142. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1143. {
  1144. if (fAudioInBuffers[i] != nullptr)
  1145. {
  1146. delete[] fAudioInBuffers[i];
  1147. fAudioInBuffers[i] = nullptr;
  1148. }
  1149. }
  1150. delete[] fAudioInBuffers;
  1151. fAudioInBuffers = nullptr;
  1152. }
  1153. if (fAudioOutBuffers != nullptr)
  1154. {
  1155. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1156. {
  1157. if (fAudioOutBuffers[i] != nullptr)
  1158. {
  1159. delete[] fAudioOutBuffers[i];
  1160. fAudioOutBuffers[i] = nullptr;
  1161. }
  1162. }
  1163. delete[] fAudioOutBuffers;
  1164. fAudioOutBuffers = nullptr;
  1165. }
  1166. if (fParamBuffers != nullptr)
  1167. {
  1168. delete[] fParamBuffers;
  1169. fParamBuffers = nullptr;
  1170. }
  1171. CarlaPlugin::clearBuffers();
  1172. carla_debug("LadspaPlugin::clearBuffers() - end");
  1173. }
  1174. // -------------------------------------------------------------------
  1175. const void* getExtraStuff() const noexcept override
  1176. {
  1177. return fRdfDescriptor;
  1178. }
  1179. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1180. {
  1181. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1182. // ---------------------------------------------------------------
  1183. // first checks
  1184. if (pData->client != nullptr)
  1185. {
  1186. pData->engine->setLastError("Plugin client is already registered");
  1187. return false;
  1188. }
  1189. if (filename == nullptr || filename[0] == '\0')
  1190. {
  1191. pData->engine->setLastError("null filename");
  1192. return false;
  1193. }
  1194. if (label == nullptr || label[0] == '\0')
  1195. {
  1196. pData->engine->setLastError("null label");
  1197. return false;
  1198. }
  1199. // ---------------------------------------------------------------
  1200. // open DLL
  1201. if (! pData->libOpen(filename))
  1202. {
  1203. pData->engine->setLastError(pData->libError(filename));
  1204. return false;
  1205. }
  1206. // ---------------------------------------------------------------
  1207. // get DLL main entry
  1208. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)pData->libSymbol("ladspa_descriptor");
  1209. if (descFn == nullptr)
  1210. {
  1211. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1212. return false;
  1213. }
  1214. // ---------------------------------------------------------------
  1215. // get descriptor that matches label
  1216. ulong i = 0;
  1217. for (;;)
  1218. {
  1219. try {
  1220. fDescriptor = descFn(i++);
  1221. }
  1222. catch(...) {
  1223. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1224. fDescriptor = nullptr;
  1225. break;
  1226. }
  1227. if (fDescriptor == nullptr)
  1228. break;
  1229. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1230. {
  1231. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1232. fDescriptor = nullptr;
  1233. break;
  1234. }
  1235. if (fDescriptor->run == nullptr)
  1236. {
  1237. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1238. fDescriptor = nullptr;
  1239. break;
  1240. }
  1241. if (std::strcmp(fDescriptor->Label, label) == 0)
  1242. break;
  1243. }
  1244. if (fDescriptor == nullptr)
  1245. {
  1246. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1247. return false;
  1248. }
  1249. // ---------------------------------------------------------------
  1250. // get info
  1251. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1252. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1253. if (name != nullptr && name[0] != '\0')
  1254. pData->name = pData->engine->getUniquePluginName(name);
  1255. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  1256. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1257. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1258. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1259. else
  1260. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1261. pData->filename = carla_strdup(filename);
  1262. // ---------------------------------------------------------------
  1263. // register client
  1264. pData->client = pData->engine->addClient(this);
  1265. if (pData->client == nullptr || ! pData->client->isOk())
  1266. {
  1267. pData->engine->setLastError("Failed to register plugin client");
  1268. return false;
  1269. }
  1270. // ---------------------------------------------------------------
  1271. // initialize plugin
  1272. try {
  1273. fHandle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  1274. } CARLA_SAFE_EXCEPTION("LADSPA instantiate");
  1275. if (fHandle == nullptr)
  1276. {
  1277. pData->engine->setLastError("Plugin failed to initialize");
  1278. return false;
  1279. }
  1280. // ---------------------------------------------------------------
  1281. // load plugin settings
  1282. {
  1283. #ifdef __USE_GNU
  1284. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  1285. #else
  1286. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  1287. #endif
  1288. // set default options
  1289. pData->options = 0x0;
  1290. if (fLatencyIndex >= 0 || isDssiVst)
  1291. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1292. if (pData->engine->getOptions().forceStereo)
  1293. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1294. #ifndef BUILD_BRIDGE
  1295. // set identifier string
  1296. CarlaString identifier("LADSPA/");
  1297. identifier += CarlaString(getUniqueId());
  1298. identifier += ",";
  1299. identifier += label;
  1300. pData->identifier = identifier.dup();
  1301. // load settings
  1302. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1303. // ignore settings, we need this anyway
  1304. if (fLatencyIndex >= 0 || isDssiVst)
  1305. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1306. #endif
  1307. }
  1308. return true;
  1309. }
  1310. // -------------------------------------------------------------------
  1311. private:
  1312. LADSPA_Handle fHandle;
  1313. LADSPA_Handle fHandle2;
  1314. const LADSPA_Descriptor* fDescriptor;
  1315. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1316. float** fAudioInBuffers;
  1317. float** fAudioOutBuffers;
  1318. float* fParamBuffers;
  1319. int32_t fLatencyIndex; // -1 if invalid
  1320. uint32_t getSafePortCount() const noexcept
  1321. {
  1322. if (fDescriptor->PortCount == 0)
  1323. return 0;
  1324. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1325. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1326. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1327. return static_cast<uint32_t>(fDescriptor->PortCount);
  1328. }
  1329. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1330. {
  1331. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  1332. return true;
  1333. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  1334. return true;
  1335. return false;
  1336. }
  1337. bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf, const bool wantName, const bool useBracket) const noexcept
  1338. {
  1339. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  1340. if (sepBracketStart == nullptr)
  1341. return false;
  1342. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  1343. if (sepBracketEnd == nullptr)
  1344. return false;
  1345. const size_t unitSize(static_cast<size_t>(sepBracketEnd-sepBracketStart-2));
  1346. if (unitSize > 7) // very unlikely to have such big unit
  1347. return false;
  1348. const size_t sepIndex(std::strlen(paramName)-unitSize-3);
  1349. // just in case
  1350. if (sepIndex > STR_MAX)
  1351. return false;
  1352. if (wantName)
  1353. {
  1354. std::strncpy(strBuf, paramName, sepIndex);
  1355. strBuf[sepIndex] = '\0';
  1356. }
  1357. else
  1358. {
  1359. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  1360. strBuf[unitSize] = '\0';
  1361. }
  1362. return true;
  1363. }
  1364. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1365. };
  1366. CARLA_BACKEND_END_NAMESPACE
  1367. #endif // WANT_LADSPA
  1368. // -------------------------------------------------------------------------------------------------------------------
  1369. CARLA_BACKEND_START_NAMESPACE
  1370. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1371. {
  1372. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %p)", init.engine, init.filename, init.name, init.label, init.uniqueId, rdfDescriptor);
  1373. #ifdef WANT_LADSPA
  1374. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1375. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1376. {
  1377. delete plugin;
  1378. return nullptr;
  1379. }
  1380. plugin->reload();
  1381. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1382. {
  1383. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1384. delete plugin;
  1385. return nullptr;
  1386. }
  1387. return plugin;
  1388. #else
  1389. init.engine->setLastError("LADSPA support not available");
  1390. return nullptr;
  1391. #endif
  1392. }
  1393. CARLA_BACKEND_END_NAMESPACE
  1394. // -------------------------------------------------------------------------------------------------------------------