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.

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