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.

1849 lines
65KB

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