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.

1853 lines
66KB

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