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.

1745 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. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  283. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, false))
  284. return;
  285. nullStrBuf(strBuf);
  286. }
  287. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  288. {
  289. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, nullStrBuf(strBuf));
  290. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  291. const int32_t rindex(pData->param.data[parameterId].rindex);
  292. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  293. {
  294. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  295. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, nullStrBuf(strBuf));
  296. const LADSPA_RDF_ScalePoint* const scalePoint(&port->ScalePoints[scalePointId]);
  297. CARLA_SAFE_ASSERT_RETURN(scalePoint->Label != nullptr, nullStrBuf(strBuf));
  298. std::strncpy(strBuf, scalePoint->Label, STR_MAX);
  299. return;
  300. }
  301. nullStrBuf(strBuf);
  302. }
  303. // -------------------------------------------------------------------
  304. // Set data (state)
  305. // nothing
  306. // -------------------------------------------------------------------
  307. // Set data (internal stuff)
  308. // nothing
  309. // -------------------------------------------------------------------
  310. // Set data (plugin-specific stuff)
  311. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  312. {
  313. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  314. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  315. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  316. fParamBuffers[parameterId] = fixedValue;
  317. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  318. }
  319. // -------------------------------------------------------------------
  320. // Set ui stuff
  321. // nothing
  322. // -------------------------------------------------------------------
  323. // Plugin state
  324. void reload() override
  325. {
  326. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  327. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  328. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  329. carla_debug("LadspaPlugin::reload() - start");
  330. const EngineProcessMode processMode(pData->engine->getProccessMode());
  331. // Safely disable plugin for reload
  332. const ScopedDisabler sd(this);
  333. if (pData->active)
  334. deactivate();
  335. clearBuffers();
  336. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  337. const uint32_t portCount(getSafePortCount());
  338. uint32_t aIns, aOuts, params;
  339. aIns = aOuts = params = 0;
  340. bool forcedStereoIn, forcedStereoOut;
  341. forcedStereoIn = forcedStereoOut = false;
  342. bool needsCtrlIn, needsCtrlOut;
  343. needsCtrlIn = needsCtrlOut = false;
  344. for (uint32_t i=0; i < portCount; ++i)
  345. {
  346. const LADSPA_PortDescriptor portType(fDescriptor->PortDescriptors[i]);
  347. if (LADSPA_IS_PORT_AUDIO(portType))
  348. {
  349. if (LADSPA_IS_PORT_INPUT(portType))
  350. aIns += 1;
  351. else if (LADSPA_IS_PORT_OUTPUT(portType))
  352. aOuts += 1;
  353. }
  354. else if (LADSPA_IS_PORT_CONTROL(portType))
  355. params += 1;
  356. }
  357. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  358. {
  359. if (fHandle2 == nullptr)
  360. {
  361. try {
  362. fHandle2 = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(sampleRate));
  363. } CARLA_SAFE_EXCEPTION("LADSPA instantiate #2");
  364. }
  365. if (fHandle2 != nullptr)
  366. {
  367. if (aIns == 1)
  368. {
  369. aIns = 2;
  370. forcedStereoIn = true;
  371. }
  372. if (aOuts == 1)
  373. {
  374. aOuts = 2;
  375. forcedStereoOut = true;
  376. }
  377. }
  378. }
  379. if (aIns > 0)
  380. {
  381. pData->audioIn.createNew(aIns);
  382. fAudioInBuffers = new float*[aIns];
  383. for (uint32_t i=0; i < aIns; ++i)
  384. fAudioInBuffers[i] = nullptr;
  385. }
  386. if (aOuts > 0)
  387. {
  388. pData->audioOut.createNew(aOuts);
  389. fAudioOutBuffers = new float*[aOuts];
  390. needsCtrlIn = true;
  391. for (uint32_t i=0; i < aOuts; ++i)
  392. fAudioOutBuffers[i] = nullptr;
  393. }
  394. if (params > 0)
  395. {
  396. pData->param.createNew(params, true);
  397. fParamBuffers = new float[params];
  398. FLOAT_CLEAR(fParamBuffers, params);
  399. }
  400. const uint portNameSize(pData->engine->getMaxPortNameSize());
  401. CarlaString portName;
  402. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  403. {
  404. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  405. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  406. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  407. if (LADSPA_IS_PORT_AUDIO(portType))
  408. {
  409. portName.clear();
  410. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  411. {
  412. portName = pData->name;
  413. portName += ":";
  414. }
  415. if (fDescriptor->PortNames[i] != nullptr && fDescriptor->PortNames[i][0] != '\0')
  416. {
  417. portName += fDescriptor->PortNames[i];
  418. }
  419. else
  420. {
  421. if (LADSPA_IS_PORT_INPUT(portType))
  422. {
  423. if (aIns > 1)
  424. {
  425. portName += "audio-in_";
  426. portName += CarlaString(iAudioIn+1);
  427. }
  428. else
  429. portName += "audio-in";
  430. }
  431. else
  432. {
  433. if (aOuts > 1)
  434. {
  435. portName += "audio-out_";
  436. portName += CarlaString(iAudioOut+1);
  437. }
  438. else
  439. portName += "audio-out";
  440. }
  441. }
  442. portName.truncate(portNameSize);
  443. if (LADSPA_IS_PORT_INPUT(portType))
  444. {
  445. const uint32_t j = iAudioIn++;
  446. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  447. pData->audioIn.ports[j].rindex = i;
  448. if (forcedStereoIn)
  449. {
  450. portName += "_2";
  451. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  452. pData->audioIn.ports[1].rindex = i;
  453. }
  454. }
  455. else if (LADSPA_IS_PORT_OUTPUT(portType))
  456. {
  457. const uint32_t j = iAudioOut++;
  458. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  459. pData->audioOut.ports[j].rindex = i;
  460. if (forcedStereoOut)
  461. {
  462. portName += "_2";
  463. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  464. pData->audioOut.ports[1].rindex = i;
  465. }
  466. }
  467. else
  468. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  469. }
  470. else if (LADSPA_IS_PORT_CONTROL(portType))
  471. {
  472. const uint32_t j = iCtrl++;
  473. pData->param.data[j].index = static_cast<int32_t>(j);
  474. pData->param.data[j].rindex = static_cast<int32_t>(i);
  475. const char* const paramName(fDescriptor->PortNames[i] != nullptr ? fDescriptor->PortNames[i] : "unknown");
  476. float min, max, def, step, stepSmall, stepLarge;
  477. // min value
  478. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  479. min = portRangeHints.LowerBound;
  480. else
  481. min = 0.0f;
  482. // max value
  483. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  484. max = portRangeHints.UpperBound;
  485. else
  486. max = 1.0f;
  487. if (min > max)
  488. {
  489. carla_stderr2("WARNING - Broken plugin parameter '%s': min > max", paramName);
  490. min = max - 0.1f;
  491. }
  492. else if (min == max)
  493. {
  494. carla_stderr2("WARNING - Broken plugin parameter '%s': min == maxf", paramName);
  495. max = min + 0.1f;
  496. }
  497. // default value
  498. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  499. def = fRdfDescriptor->Ports[i].Default;
  500. else
  501. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  502. if (def < min)
  503. def = min;
  504. else if (def > max)
  505. def = max;
  506. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  507. {
  508. min *= sampleRate;
  509. max *= sampleRate;
  510. def *= sampleRate;
  511. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  512. }
  513. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  514. {
  515. step = max - min;
  516. stepSmall = step;
  517. stepLarge = step;
  518. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  519. }
  520. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  521. {
  522. step = 1.0f;
  523. stepSmall = 1.0f;
  524. stepLarge = 10.0f;
  525. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  526. }
  527. else
  528. {
  529. const float range = max - min;
  530. step = range/100.0f;
  531. stepSmall = range/1000.0f;
  532. stepLarge = range/10.0f;
  533. }
  534. if (LADSPA_IS_PORT_INPUT(portType))
  535. {
  536. pData->param.data[j].type = PARAMETER_INPUT;
  537. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  538. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  539. needsCtrlIn = true;
  540. }
  541. else if (LADSPA_IS_PORT_OUTPUT(portType))
  542. {
  543. pData->param.data[j].type = PARAMETER_OUTPUT;
  544. if (std::strcmp(paramName, "latency") == 0 || std::strcmp(paramName, "_latency") == 0)
  545. {
  546. min = 0.0f;
  547. max = sampleRate;
  548. def = 0.0f;
  549. step = 1.0f;
  550. stepSmall = 1.0f;
  551. stepLarge = 1.0f;
  552. fLatencyIndex = static_cast<int32_t>(j);
  553. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  554. }
  555. else
  556. {
  557. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  558. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  559. needsCtrlOut = true;
  560. }
  561. }
  562. else
  563. {
  564. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  565. }
  566. // extra parameter hints
  567. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  568. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  569. // check for scalepoints, require at least 2 to make it useful
  570. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount >= 2)
  571. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  572. pData->param.ranges[j].min = min;
  573. pData->param.ranges[j].max = max;
  574. pData->param.ranges[j].def = def;
  575. pData->param.ranges[j].step = step;
  576. pData->param.ranges[j].stepSmall = stepSmall;
  577. pData->param.ranges[j].stepLarge = stepLarge;
  578. // Start parameters in their default values
  579. fParamBuffers[j] = def;
  580. try {
  581. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  582. } CARLA_SAFE_EXCEPTION("LADSPA connect_port parameter");
  583. if (fHandle2 != nullptr)
  584. {
  585. try {
  586. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  587. } CARLA_SAFE_EXCEPTION("LADSPA connect_port parameter #2");
  588. }
  589. }
  590. else
  591. {
  592. // Not Audio or Control
  593. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  594. try {
  595. fDescriptor->connect_port(fHandle, i, nullptr);
  596. } CARLA_SAFE_EXCEPTION("LADSPA connect_port null");
  597. if (fHandle2 != nullptr)
  598. {
  599. try {
  600. fDescriptor->connect_port(fHandle2, i, nullptr);
  601. } CARLA_SAFE_EXCEPTION("LADSPA connect_port null #2");
  602. }
  603. }
  604. }
  605. if (needsCtrlIn)
  606. {
  607. portName.clear();
  608. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  609. {
  610. portName = pData->name;
  611. portName += ":";
  612. }
  613. portName += "events-in";
  614. portName.truncate(portNameSize);
  615. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  616. }
  617. if (needsCtrlOut)
  618. {
  619. portName.clear();
  620. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  621. {
  622. portName = pData->name;
  623. portName += ":";
  624. }
  625. portName += "events-out";
  626. portName.truncate(portNameSize);
  627. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  628. }
  629. if (forcedStereoIn || forcedStereoOut)
  630. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  631. else
  632. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  633. // plugin hints
  634. pData->hints = 0x0;
  635. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  636. pData->hints |= PLUGIN_IS_RTSAFE;
  637. #ifndef BUILD_BRIDGE
  638. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  639. pData->hints |= PLUGIN_CAN_DRYWET;
  640. if (aOuts > 0)
  641. pData->hints |= PLUGIN_CAN_VOLUME;
  642. if (aOuts >= 2 && aOuts % 2 == 0)
  643. pData->hints |= PLUGIN_CAN_BALANCE;
  644. #endif
  645. // extra plugin hints
  646. pData->extraHints = 0x0;
  647. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  648. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  649. // check latency
  650. if (fLatencyIndex >= 0)
  651. {
  652. // we need to pre-run the plugin so it can update its latency control-port
  653. float tmpIn[aIns][2];
  654. float tmpOut[aOuts][2];
  655. for (uint32_t j=0; j < aIns; ++j)
  656. {
  657. tmpIn[j][0] = 0.0f;
  658. tmpIn[j][1] = 0.0f;
  659. try {
  660. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  661. } CARLA_SAFE_EXCEPTION("LADSPA connect_port latency input");
  662. }
  663. for (uint32_t j=0; j < aOuts; ++j)
  664. {
  665. tmpOut[j][0] = 0.0f;
  666. tmpOut[j][1] = 0.0f;
  667. try {
  668. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  669. } CARLA_SAFE_EXCEPTION("LADSPA connect_port latency output");
  670. }
  671. if (fDescriptor->activate != nullptr)
  672. {
  673. try {
  674. fDescriptor->activate(fHandle);
  675. } CARLA_SAFE_EXCEPTION("LADSPA latency activate");
  676. }
  677. try {
  678. fDescriptor->run(fHandle, 2);
  679. } CARLA_SAFE_EXCEPTION("LADSPA latency run");
  680. if (fDescriptor->deactivate != nullptr)
  681. {
  682. try {
  683. fDescriptor->deactivate(fHandle);
  684. } CARLA_SAFE_EXCEPTION("LADSPA latency deactivate");
  685. }
  686. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  687. if (latency >= 0)
  688. {
  689. const uint32_t ulatency(static_cast<uint32_t>(latency));
  690. if (pData->latency != ulatency)
  691. {
  692. carla_stdout("latency = %i", latency);
  693. pData->latency = ulatency;
  694. pData->client->setLatency(ulatency);
  695. #ifndef BUILD_BRIDGE
  696. pData->recreateLatencyBuffers();
  697. #endif
  698. }
  699. }
  700. else
  701. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  702. }
  703. bufferSizeChanged(pData->engine->getBufferSize());
  704. if (pData->active)
  705. activate();
  706. carla_debug("LadspaPlugin::reload() - end");
  707. }
  708. // -------------------------------------------------------------------
  709. // Plugin processing
  710. void activate() noexcept override
  711. {
  712. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  713. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  714. if (fDescriptor->activate != nullptr)
  715. {
  716. try {
  717. fDescriptor->activate(fHandle);
  718. } CARLA_SAFE_EXCEPTION("LADSPA activate");
  719. if (fHandle2 != nullptr)
  720. {
  721. try {
  722. fDescriptor->activate(fHandle2);
  723. } CARLA_SAFE_EXCEPTION("LADSPA activate #2");
  724. }
  725. }
  726. if (fLatencyIndex < 0)
  727. return;
  728. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  729. CARLA_SAFE_ASSERT_RETURN(latency >= 0,);
  730. const uint32_t ulatency(static_cast<uint32_t>(latency));
  731. if (pData->latency != ulatency)
  732. {
  733. carla_stdout("latency changed to %i", latency);
  734. pData->latency = ulatency;
  735. pData->client->setLatency(ulatency);
  736. #ifndef BUILD_BRIDGE
  737. try {
  738. pData->recreateLatencyBuffers(); // FIXME
  739. } CARLA_SAFE_EXCEPTION("LADSPA recreateLatencyBuffers()");
  740. #endif
  741. }
  742. else
  743. carla_stdout("latency still the same %i", latency);
  744. }
  745. void deactivate() noexcept override
  746. {
  747. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  748. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  749. if (fDescriptor->deactivate != nullptr)
  750. {
  751. try {
  752. fDescriptor->deactivate(fHandle);
  753. } CARLA_SAFE_EXCEPTION("LADSPA deactivate");
  754. if (fHandle2 != nullptr)
  755. {
  756. try {
  757. fDescriptor->deactivate(fHandle2);
  758. } CARLA_SAFE_EXCEPTION("LADSPA deactivate #2");
  759. }
  760. }
  761. }
  762. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  763. {
  764. // --------------------------------------------------------------------------------------------------------
  765. // Check if active
  766. if (! pData->active)
  767. {
  768. // disable any output sound
  769. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  770. FLOAT_CLEAR(outBuffer[i], frames);
  771. return;
  772. }
  773. // --------------------------------------------------------------------------------------------------------
  774. // Check if needs reset
  775. if (pData->needsReset)
  776. {
  777. #ifndef BUILD_BRIDGE
  778. if (pData->latency > 0)
  779. {
  780. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  781. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  782. }
  783. #endif
  784. pData->needsReset = false;
  785. }
  786. // --------------------------------------------------------------------------------------------------------
  787. // Event Input and Processing
  788. if (pData->event.portIn != nullptr)
  789. {
  790. // ----------------------------------------------------------------------------------------------------
  791. // Event Input (System)
  792. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  793. uint32_t numEvents = pData->event.portIn->getEventCount();
  794. uint32_t timeOffset = 0;
  795. for (uint32_t i=0; i < numEvents; ++i)
  796. {
  797. const EngineEvent& event(pData->event.portIn->getEvent(i));
  798. if (event.time >= frames)
  799. continue;
  800. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  801. if (isSampleAccurate && event.time > timeOffset)
  802. {
  803. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset))
  804. timeOffset = event.time;
  805. }
  806. switch (event.type)
  807. {
  808. case kEngineEventTypeNull:
  809. break;
  810. case kEngineEventTypeControl: {
  811. const EngineControlEvent& ctrlEvent(event.ctrl);
  812. switch (ctrlEvent.type)
  813. {
  814. case kEngineControlEventTypeNull:
  815. break;
  816. case kEngineControlEventTypeParameter: {
  817. #ifndef BUILD_BRIDGE
  818. // Control backend stuff
  819. if (event.channel == pData->ctrlChannel)
  820. {
  821. float value;
  822. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  823. {
  824. value = ctrlEvent.value;
  825. setDryWet(value, false, false);
  826. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  827. break;
  828. }
  829. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  830. {
  831. value = ctrlEvent.value*127.0f/100.0f;
  832. setVolume(value, false, false);
  833. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  834. break;
  835. }
  836. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  837. {
  838. float left, right;
  839. value = ctrlEvent.value/0.5f - 1.0f;
  840. if (value < 0.0f)
  841. {
  842. left = -1.0f;
  843. right = (value*2.0f)+1.0f;
  844. }
  845. else if (value > 0.0f)
  846. {
  847. left = (value*2.0f)-1.0f;
  848. right = 1.0f;
  849. }
  850. else
  851. {
  852. left = -1.0f;
  853. right = 1.0f;
  854. }
  855. setBalanceLeft(left, false, false);
  856. setBalanceRight(right, false, false);
  857. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  858. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  859. break;
  860. }
  861. }
  862. #endif
  863. // Control plugin parameters
  864. for (uint32_t k=0; k < pData->param.count; ++k)
  865. {
  866. if (pData->param.data[k].midiChannel != event.channel)
  867. continue;
  868. if (pData->param.data[k].midiCC != ctrlEvent.param)
  869. continue;
  870. if (pData->param.data[k].type != PARAMETER_INPUT)
  871. continue;
  872. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  873. continue;
  874. float value;
  875. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  876. {
  877. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  878. }
  879. else
  880. {
  881. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  882. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  883. value = std::rint(value);
  884. }
  885. setParameterValue(k, value, false, false, false);
  886. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  887. break;
  888. }
  889. break;
  890. } // case kEngineControlEventTypeParameter
  891. case kEngineControlEventTypeMidiBank:
  892. case kEngineControlEventTypeMidiProgram:
  893. case kEngineControlEventTypeAllSoundOff:
  894. case kEngineControlEventTypeAllNotesOff:
  895. break;
  896. } // switch (ctrlEvent.type)
  897. break;
  898. } // case kEngineEventTypeControl
  899. case kEngineEventTypeMidi:
  900. break;
  901. } // switch (event.type)
  902. }
  903. pData->postRtEvents.trySplice();
  904. if (frames > timeOffset)
  905. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  906. } // End of Event Input and Processing
  907. // --------------------------------------------------------------------------------------------------------
  908. // Plugin processing (no events)
  909. else
  910. {
  911. processSingle(inBuffer, outBuffer, frames, 0);
  912. } // End of Plugin processing (no events)
  913. // --------------------------------------------------------------------------------------------------------
  914. // Latency, save values for next callback
  915. if (pData->latency > 0)
  916. {
  917. if (pData->latency <= frames)
  918. {
  919. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  920. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i]+(frames-pData->latency), pData->latency);
  921. }
  922. else
  923. {
  924. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  925. {
  926. for (k=0; k < pData->latency-frames; ++k)
  927. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  928. for (j=0; k < pData->latency; ++j, ++k)
  929. pData->latencyBuffers[i][k] = inBuffer[i][j];
  930. }
  931. }
  932. }
  933. // --------------------------------------------------------------------------------------------------------
  934. // Control Output
  935. if (pData->event.portOut != nullptr)
  936. {
  937. uint8_t channel;
  938. uint16_t param;
  939. float value;
  940. for (uint32_t k=0; k < pData->param.count; ++k)
  941. {
  942. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  943. continue;
  944. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  945. if (pData->param.data[k].midiCC > 0)
  946. {
  947. channel = pData->param.data[k].midiChannel;
  948. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  949. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  950. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  951. }
  952. }
  953. } // End of Control Output
  954. }
  955. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  956. {
  957. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  958. if (pData->audioIn.count > 0)
  959. {
  960. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  961. }
  962. if (pData->audioOut.count > 0)
  963. {
  964. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  965. }
  966. // --------------------------------------------------------------------------------------------------------
  967. // Try lock, silence otherwise
  968. if (pData->engine->isOffline())
  969. {
  970. pData->singleMutex.lock();
  971. }
  972. else if (! pData->singleMutex.tryLock())
  973. {
  974. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  975. {
  976. for (uint32_t k=0; k < frames; ++k)
  977. outBuffer[i][k+timeOffset] = 0.0f;
  978. }
  979. return false;
  980. }
  981. // --------------------------------------------------------------------------------------------------------
  982. // Reset audio buffers
  983. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  984. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  985. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  986. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  987. // --------------------------------------------------------------------------------------------------------
  988. // Run plugin
  989. try {
  990. fDescriptor->run(fHandle, frames);
  991. } CARLA_SAFE_EXCEPTION("LADSPA run");
  992. if (fHandle2 != nullptr)
  993. {
  994. try {
  995. fDescriptor->run(fHandle2, frames);
  996. } CARLA_SAFE_EXCEPTION("LADSPA run #2");
  997. }
  998. #ifndef BUILD_BRIDGE
  999. // --------------------------------------------------------------------------------------------------------
  1000. // Post-processing (dry/wet, volume and balance)
  1001. {
  1002. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1003. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1004. const bool isMono = (pData->audioIn.count == 1);
  1005. bool isPair;
  1006. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1007. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1008. {
  1009. // Dry/Wet
  1010. if (doDryWet)
  1011. {
  1012. for (uint32_t k=0; k < frames; ++k)
  1013. {
  1014. if (k < pData->latency)
  1015. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  1016. else if (pData->latency < frames)
  1017. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  1018. else
  1019. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  1020. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1021. }
  1022. }
  1023. // Balance
  1024. if (doBalance)
  1025. {
  1026. isPair = (i % 2 == 0);
  1027. if (isPair)
  1028. {
  1029. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1030. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1031. }
  1032. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1033. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1034. for (uint32_t k=0; k < frames; ++k)
  1035. {
  1036. if (isPair)
  1037. {
  1038. // left
  1039. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1040. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1041. }
  1042. else
  1043. {
  1044. // right
  1045. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1046. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1047. }
  1048. }
  1049. }
  1050. // Volume (and buffer copy)
  1051. {
  1052. for (uint32_t k=0; k < frames; ++k)
  1053. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1054. }
  1055. }
  1056. } // End of Post-processing
  1057. #else // BUILD_BRIDGE
  1058. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1059. {
  1060. for (uint32_t k=0; k < frames; ++k)
  1061. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1062. }
  1063. #endif
  1064. // --------------------------------------------------------------------------------------------------------
  1065. pData->singleMutex.unlock();
  1066. return true;
  1067. }
  1068. void bufferSizeChanged(const uint32_t newBufferSize) override
  1069. {
  1070. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1071. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1072. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1073. {
  1074. if (fAudioInBuffers[i] != nullptr)
  1075. delete[] fAudioInBuffers[i];
  1076. fAudioInBuffers[i] = new float[newBufferSize];
  1077. }
  1078. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1079. {
  1080. if (fAudioOutBuffers[i] != nullptr)
  1081. delete[] fAudioOutBuffers[i];
  1082. fAudioOutBuffers[i] = new float[newBufferSize];
  1083. }
  1084. if (fHandle2 == nullptr)
  1085. {
  1086. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1087. {
  1088. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1089. try {
  1090. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1091. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input");
  1092. }
  1093. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1094. {
  1095. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1096. try {
  1097. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1098. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output");
  1099. }
  1100. }
  1101. else
  1102. {
  1103. if (pData->audioIn.count > 0)
  1104. {
  1105. CARLA_ASSERT(pData->audioIn.count == 2);
  1106. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1107. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1108. try {
  1109. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1110. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #1");
  1111. try {
  1112. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1113. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #2");
  1114. }
  1115. if (pData->audioOut.count > 0)
  1116. {
  1117. CARLA_ASSERT(pData->audioOut.count == 2);
  1118. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1119. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1120. try {
  1121. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1122. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #1");
  1123. try {
  1124. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1125. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #2");
  1126. }
  1127. }
  1128. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1129. }
  1130. void sampleRateChanged(const double newSampleRate) override
  1131. {
  1132. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1133. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1134. // TODO
  1135. (void)newSampleRate;
  1136. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1137. }
  1138. // -------------------------------------------------------------------
  1139. // Plugin buffers
  1140. void clearBuffers() noexcept override
  1141. {
  1142. carla_debug("LadspaPlugin::clearBuffers() - start");
  1143. if (fAudioInBuffers != nullptr)
  1144. {
  1145. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1146. {
  1147. if (fAudioInBuffers[i] != nullptr)
  1148. {
  1149. delete[] fAudioInBuffers[i];
  1150. fAudioInBuffers[i] = nullptr;
  1151. }
  1152. }
  1153. delete[] fAudioInBuffers;
  1154. fAudioInBuffers = nullptr;
  1155. }
  1156. if (fAudioOutBuffers != nullptr)
  1157. {
  1158. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1159. {
  1160. if (fAudioOutBuffers[i] != nullptr)
  1161. {
  1162. delete[] fAudioOutBuffers[i];
  1163. fAudioOutBuffers[i] = nullptr;
  1164. }
  1165. }
  1166. delete[] fAudioOutBuffers;
  1167. fAudioOutBuffers = nullptr;
  1168. }
  1169. if (fParamBuffers != nullptr)
  1170. {
  1171. delete[] fParamBuffers;
  1172. fParamBuffers = nullptr;
  1173. }
  1174. CarlaPlugin::clearBuffers();
  1175. carla_debug("LadspaPlugin::clearBuffers() - end");
  1176. }
  1177. // -------------------------------------------------------------------
  1178. const void* getExtraStuff() const noexcept override
  1179. {
  1180. return fRdfDescriptor;
  1181. }
  1182. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1183. {
  1184. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1185. // ---------------------------------------------------------------
  1186. // first checks
  1187. if (pData->client != nullptr)
  1188. {
  1189. pData->engine->setLastError("Plugin client is already registered");
  1190. return false;
  1191. }
  1192. if (filename == nullptr || filename[0] == '\0')
  1193. {
  1194. pData->engine->setLastError("null filename");
  1195. return false;
  1196. }
  1197. if (label == nullptr || label[0] == '\0')
  1198. {
  1199. pData->engine->setLastError("null label");
  1200. return false;
  1201. }
  1202. // ---------------------------------------------------------------
  1203. // open DLL
  1204. if (! pData->libOpen(filename))
  1205. {
  1206. pData->engine->setLastError(pData->libError(filename));
  1207. return false;
  1208. }
  1209. // ---------------------------------------------------------------
  1210. // get DLL main entry
  1211. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)pData->libSymbol("ladspa_descriptor");
  1212. if (descFn == nullptr)
  1213. {
  1214. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1215. return false;
  1216. }
  1217. // ---------------------------------------------------------------
  1218. // get descriptor that matches label
  1219. ulong i = 0;
  1220. for (;;)
  1221. {
  1222. try {
  1223. fDescriptor = descFn(i++);
  1224. }
  1225. catch(...) {
  1226. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1227. fDescriptor = nullptr;
  1228. break;
  1229. }
  1230. if (fDescriptor == nullptr)
  1231. break;
  1232. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1233. {
  1234. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1235. fDescriptor = nullptr;
  1236. break;
  1237. }
  1238. if (fDescriptor->run == nullptr)
  1239. {
  1240. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1241. fDescriptor = nullptr;
  1242. break;
  1243. }
  1244. if (std::strcmp(fDescriptor->Label, label) == 0)
  1245. break;
  1246. }
  1247. if (fDescriptor == nullptr)
  1248. {
  1249. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1250. return false;
  1251. }
  1252. // ---------------------------------------------------------------
  1253. // get info
  1254. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1255. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1256. if (name != nullptr && name[0] != '\0')
  1257. pData->name = pData->engine->getUniquePluginName(name);
  1258. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  1259. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1260. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1261. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1262. else
  1263. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1264. pData->filename = carla_strdup(filename);
  1265. // ---------------------------------------------------------------
  1266. // register client
  1267. pData->client = pData->engine->addClient(this);
  1268. if (pData->client == nullptr || ! pData->client->isOk())
  1269. {
  1270. pData->engine->setLastError("Failed to register plugin client");
  1271. return false;
  1272. }
  1273. // ---------------------------------------------------------------
  1274. // initialize plugin
  1275. try {
  1276. fHandle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  1277. } CARLA_SAFE_EXCEPTION("LADSPA instantiate");
  1278. if (fHandle == nullptr)
  1279. {
  1280. pData->engine->setLastError("Plugin failed to initialize");
  1281. return false;
  1282. }
  1283. // ---------------------------------------------------------------
  1284. // load plugin settings
  1285. {
  1286. #ifdef __USE_GNU
  1287. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  1288. #else
  1289. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  1290. #endif
  1291. // set default options
  1292. pData->options = 0x0;
  1293. if (fLatencyIndex >= 0 || isDssiVst)
  1294. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1295. if (pData->engine->getOptions().forceStereo)
  1296. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1297. #ifndef BUILD_BRIDGE
  1298. // set identifier string
  1299. CarlaString identifier("LADSPA/");
  1300. identifier += CarlaString(getUniqueId());
  1301. identifier += ",";
  1302. identifier += label;
  1303. pData->identifier = identifier.dup();
  1304. // load settings
  1305. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1306. // ignore settings, we need this anyway
  1307. if (fLatencyIndex >= 0 || isDssiVst)
  1308. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1309. #endif
  1310. }
  1311. return true;
  1312. }
  1313. // -------------------------------------------------------------------
  1314. private:
  1315. LADSPA_Handle fHandle;
  1316. LADSPA_Handle fHandle2;
  1317. const LADSPA_Descriptor* fDescriptor;
  1318. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1319. float** fAudioInBuffers;
  1320. float** fAudioOutBuffers;
  1321. float* fParamBuffers;
  1322. int32_t fLatencyIndex; // -1 if invalid
  1323. // -------------------------------------------------------------------
  1324. uint32_t getSafePortCount() const noexcept
  1325. {
  1326. if (fDescriptor->PortCount == 0)
  1327. return 0;
  1328. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1329. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1330. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1331. return static_cast<uint32_t>(fDescriptor->PortCount);
  1332. }
  1333. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1334. {
  1335. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  1336. return true;
  1337. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  1338. return true;
  1339. return false;
  1340. }
  1341. bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf, const bool wantName, const bool useBracket) const noexcept
  1342. {
  1343. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  1344. if (sepBracketStart == nullptr)
  1345. return false;
  1346. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  1347. if (sepBracketEnd == nullptr)
  1348. return false;
  1349. const size_t unitSize(static_cast<size_t>(sepBracketEnd-sepBracketStart-2));
  1350. if (unitSize > 7) // very unlikely to have such big unit
  1351. return false;
  1352. const size_t sepIndex(std::strlen(paramName)-unitSize-3);
  1353. // just in case
  1354. if (sepIndex > STR_MAX)
  1355. return false;
  1356. if (wantName)
  1357. {
  1358. std::strncpy(strBuf, paramName, sepIndex);
  1359. strBuf[sepIndex] = '\0';
  1360. }
  1361. else
  1362. {
  1363. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  1364. strBuf[unitSize] = '\0';
  1365. }
  1366. return true;
  1367. }
  1368. // -------------------------------------------------------------------
  1369. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1370. };
  1371. CARLA_BACKEND_END_NAMESPACE
  1372. #endif // WANT_LADSPA
  1373. // -------------------------------------------------------------------------------------------------------------------
  1374. CARLA_BACKEND_START_NAMESPACE
  1375. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1376. {
  1377. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %p)", init.engine, init.filename, init.name, init.label, init.uniqueId, rdfDescriptor);
  1378. #ifdef WANT_LADSPA
  1379. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1380. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1381. {
  1382. delete plugin;
  1383. return nullptr;
  1384. }
  1385. plugin->reload();
  1386. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1387. {
  1388. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1389. delete plugin;
  1390. return nullptr;
  1391. }
  1392. return plugin;
  1393. #else
  1394. init.engine->setLastError("LADSPA support not available");
  1395. return nullptr;
  1396. #endif
  1397. }
  1398. CARLA_BACKEND_END_NAMESPACE
  1399. // -------------------------------------------------------------------------------------------------------------------