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.

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