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.

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