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.

1842 lines
66KB

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