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
65KB

  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. break;
  783. }
  784. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  785. {
  786. value = ctrlEvent.value*127.0f/100.0f;
  787. setVolume(value, false, false);
  788. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  789. break;
  790. }
  791. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  792. {
  793. float left, right;
  794. value = ctrlEvent.value/0.5f - 1.0f;
  795. if (value < 0.0f)
  796. {
  797. left = -1.0f;
  798. right = (value*2.0f)+1.0f;
  799. }
  800. else if (value > 0.0f)
  801. {
  802. left = (value*2.0f)-1.0f;
  803. right = 1.0f;
  804. }
  805. else
  806. {
  807. left = -1.0f;
  808. right = 1.0f;
  809. }
  810. setBalanceLeft(left, false, false);
  811. setBalanceRight(right, false, false);
  812. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  813. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  814. break;
  815. }
  816. }
  817. #endif
  818. // Control plugin parameters
  819. for (uint32_t k=0; k < pData->param.count; ++k)
  820. {
  821. if (pData->param.data[k].midiChannel != event.channel)
  822. continue;
  823. if (pData->param.data[k].midiCC != ctrlEvent.param)
  824. continue;
  825. if (pData->param.data[k].type != PARAMETER_INPUT)
  826. continue;
  827. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  828. continue;
  829. float value;
  830. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  831. {
  832. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  833. }
  834. else
  835. {
  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. break;
  843. }
  844. break;
  845. } // case kEngineControlEventTypeParameter
  846. case kEngineControlEventTypeMidiBank:
  847. case kEngineControlEventTypeMidiProgram:
  848. case kEngineControlEventTypeAllSoundOff:
  849. case kEngineControlEventTypeAllNotesOff:
  850. break;
  851. } // switch (ctrlEvent.type)
  852. break;
  853. } // case kEngineEventTypeControl
  854. case kEngineEventTypeMidi:
  855. break;
  856. } // switch (event.type)
  857. }
  858. pData->postRtEvents.trySplice();
  859. if (frames > timeOffset)
  860. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  861. } // End of Event Input and Processing
  862. // --------------------------------------------------------------------------------------------------------
  863. // Plugin processing (no events)
  864. else
  865. {
  866. processSingle(audioIn, audioOut, frames, 0);
  867. } // End of Plugin processing (no events)
  868. // --------------------------------------------------------------------------------------------------------
  869. // Control Output
  870. if (pData->event.portOut != nullptr)
  871. {
  872. uint8_t channel;
  873. uint16_t param;
  874. float value;
  875. for (uint32_t k=0; k < pData->param.count; ++k)
  876. {
  877. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  878. continue;
  879. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  880. if (pData->param.data[k].midiCC > 0)
  881. {
  882. channel = pData->param.data[k].midiChannel;
  883. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  884. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  885. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  886. }
  887. }
  888. } // End of Control Output
  889. }
  890. bool processSingle(const float** const audioIn, float** const audioOut, const uint32_t frames,
  891. const uint32_t timeOffset)
  892. {
  893. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  894. if (pData->audioIn.count > 0)
  895. {
  896. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  897. }
  898. if (pData->audioOut.count > 0)
  899. {
  900. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  901. }
  902. // --------------------------------------------------------------------------------------------------------
  903. // Try lock, silence otherwise
  904. if (pData->engine->isOffline())
  905. {
  906. pData->singleMutex.lock();
  907. }
  908. else if (! pData->singleMutex.tryLock())
  909. {
  910. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  911. {
  912. for (uint32_t k=0; k < frames; ++k)
  913. audioOut[i][k+timeOffset] = 0.0f;
  914. }
  915. return false;
  916. }
  917. const int iframes(static_cast<int>(frames));
  918. // --------------------------------------------------------------------------------------------------------
  919. // Handle needsReset
  920. if (pData->needsReset)
  921. {
  922. if (pData->latency.frames > 0)
  923. {
  924. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  925. FloatVectorOperations::clear(pData->latency.buffers[i], static_cast<int>(pData->latency.frames));
  926. }
  927. pData->needsReset = false;
  928. }
  929. // --------------------------------------------------------------------------------------------------------
  930. // Set audio buffers
  931. const bool customMonoOut = pData->audioOut.count == 2 && fForcedStereoOut && ! fForcedStereoIn;
  932. const bool customStereoOut = pData->audioOut.count == 2 && fForcedStereoIn && ! fForcedStereoOut;
  933. if (! customMonoOut)
  934. {
  935. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  936. FloatVectorOperations::clear(fAudioOutBuffers[i], iframes);
  937. }
  938. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  939. FloatVectorOperations::copy(fAudioInBuffers[i], audioIn[i]+timeOffset, iframes);
  940. // --------------------------------------------------------------------------------------------------------
  941. // Run plugin
  942. uint instn = 0;
  943. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next(), ++instn)
  944. {
  945. LADSPA_Handle const handle(it.getValue(nullptr));
  946. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  947. // ----------------------------------------------------------------------------------------------------
  948. // Mixdown for forced stereo
  949. if (customMonoOut)
  950. FloatVectorOperations::clear(fAudioOutBuffers[instn], iframes);
  951. // ----------------------------------------------------------------------------------------------------
  952. // Run it
  953. try {
  954. fDescriptor->run(handle, frames);
  955. } CARLA_SAFE_EXCEPTION("LADSPA run");
  956. // ----------------------------------------------------------------------------------------------------
  957. // Mixdown for forced stereo
  958. if (customMonoOut)
  959. FloatVectorOperations::multiply(fAudioOutBuffers[instn], 0.5f, iframes);
  960. else if (customStereoOut)
  961. FloatVectorOperations::copy(fExtraStereoBuffer[instn], fAudioOutBuffers[instn], iframes);
  962. }
  963. if (customStereoOut)
  964. {
  965. FloatVectorOperations::copy(fAudioOutBuffers[0], fExtraStereoBuffer[0], iframes);
  966. FloatVectorOperations::copy(fAudioOutBuffers[1], fExtraStereoBuffer[1], iframes);
  967. }
  968. #ifndef BUILD_BRIDGE
  969. // --------------------------------------------------------------------------------------------------------
  970. // Post-processing (dry/wet, volume and balance)
  971. {
  972. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  973. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  974. const bool isMono = (pData->audioIn.count == 1);
  975. bool isPair;
  976. float bufValue, oldBufLeft[doBalance ? frames : 1];
  977. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  978. {
  979. // Dry/Wet
  980. if (doDryWet)
  981. {
  982. const uint32_t c = isMono ? 0 : i;
  983. for (uint32_t k=0; k < frames; ++k)
  984. {
  985. if (k < pData->latency.frames)
  986. bufValue = pData->latency.buffers[c][k];
  987. else if (pData->latency.frames < frames)
  988. bufValue = fAudioInBuffers[c][k-pData->latency.frames];
  989. else
  990. bufValue = fAudioInBuffers[c][k];
  991. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  992. }
  993. }
  994. // Balance
  995. if (doBalance)
  996. {
  997. isPair = (i % 2 == 0);
  998. if (isPair)
  999. {
  1000. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1001. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], iframes);
  1002. }
  1003. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1004. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1005. for (uint32_t k=0; k < frames; ++k)
  1006. {
  1007. if (isPair)
  1008. {
  1009. // left
  1010. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1011. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1012. }
  1013. else
  1014. {
  1015. // right
  1016. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1017. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1018. }
  1019. }
  1020. }
  1021. // Volume (and buffer copy)
  1022. {
  1023. for (uint32_t k=0; k < frames; ++k)
  1024. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1025. }
  1026. }
  1027. } // End of Post-processing
  1028. #else // BUILD_BRIDGE
  1029. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1030. {
  1031. for (uint32_t k=0; k < frames; ++k)
  1032. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1033. }
  1034. #endif
  1035. // --------------------------------------------------------------------------------------------------------
  1036. // Save latency values for next callback
  1037. if (const uint32_t latframes = pData->latency.frames)
  1038. {
  1039. CARLA_SAFE_ASSERT(timeOffset == 0);
  1040. if (latframes <= frames)
  1041. {
  1042. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1043. FloatVectorOperations::copy(pData->latency.buffers[i], audioIn[i]+(frames-latframes), static_cast<int>(latframes));
  1044. }
  1045. else
  1046. {
  1047. const uint32_t diff = pData->latency.frames-frames;
  1048. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1049. {
  1050. // push back buffer by 'frames'
  1051. for (k=0; k < diff; ++k)
  1052. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1053. // put current input at the end
  1054. for (uint32_t j=0; k < latframes; ++j, ++k)
  1055. pData->latency.buffers[i][k] = audioIn[i][j];
  1056. }
  1057. }
  1058. }
  1059. // --------------------------------------------------------------------------------------------------------
  1060. pData->singleMutex.unlock();
  1061. return true;
  1062. }
  1063. void bufferSizeChanged(const uint32_t newBufferSize) override
  1064. {
  1065. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1066. carla_debug("CarlaPluginLADSPA::bufferSizeChanged(%i) - start", newBufferSize);
  1067. const int iBufferSize(static_cast<int>(newBufferSize));
  1068. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1069. {
  1070. if (fAudioInBuffers[i] != nullptr)
  1071. delete[] fAudioInBuffers[i];
  1072. fAudioInBuffers[i] = new float[newBufferSize];
  1073. FloatVectorOperations::clear(fAudioInBuffers[i], iBufferSize);
  1074. }
  1075. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1076. {
  1077. if (fAudioOutBuffers[i] != nullptr)
  1078. delete[] fAudioOutBuffers[i];
  1079. fAudioOutBuffers[i] = new float[newBufferSize];
  1080. FloatVectorOperations::clear(fAudioOutBuffers[i], iBufferSize);
  1081. }
  1082. if (fExtraStereoBuffer[0] != nullptr)
  1083. {
  1084. delete[] fExtraStereoBuffer[0];
  1085. fExtraStereoBuffer[0] = nullptr;
  1086. }
  1087. if (fExtraStereoBuffer[1] != nullptr)
  1088. {
  1089. delete[] fExtraStereoBuffer[1];
  1090. fExtraStereoBuffer[1] = nullptr;
  1091. }
  1092. if (fForcedStereoIn && pData->audioOut.count == 2)
  1093. {
  1094. fExtraStereoBuffer[0] = new float[newBufferSize];
  1095. fExtraStereoBuffer[1] = new float[newBufferSize];
  1096. FloatVectorOperations::clear(fExtraStereoBuffer[0], iBufferSize);
  1097. FloatVectorOperations::clear(fExtraStereoBuffer[1], iBufferSize);
  1098. }
  1099. reconnectAudioPorts();
  1100. carla_debug("CarlaPluginLADSPA::bufferSizeChanged(%i) - end", newBufferSize);
  1101. }
  1102. void sampleRateChanged(const double newSampleRate) override
  1103. {
  1104. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1105. carla_stdout("CarlaPluginLADSPA::sampleRateChanged(%g) - start", newSampleRate);
  1106. if (pData->active)
  1107. deactivate();
  1108. const std::size_t instanceCount(fHandles.count());
  1109. if (fDescriptor->cleanup == nullptr)
  1110. {
  1111. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1112. {
  1113. LADSPA_Handle const handle(it.getValue(nullptr));
  1114. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1115. try {
  1116. fDescriptor->cleanup(handle);
  1117. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  1118. }
  1119. }
  1120. fHandles.clear();
  1121. for (std::size_t i=0; i<instanceCount; ++i)
  1122. addInstance();
  1123. reconnectAudioPorts();
  1124. if (pData->active)
  1125. activate();
  1126. carla_stdout("CarlaPluginLADSPA::sampleRateChanged(%g) - end", newSampleRate);
  1127. }
  1128. void reconnectAudioPorts() const noexcept
  1129. {
  1130. if (fForcedStereoIn)
  1131. {
  1132. if (LADSPA_Handle const handle = fHandles.getAt(0, nullptr))
  1133. {
  1134. try {
  1135. fDescriptor->connect_port(handle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1136. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo input)");
  1137. }
  1138. if (LADSPA_Handle const handle = fHandles.getAt(1, nullptr))
  1139. {
  1140. try {
  1141. fDescriptor->connect_port(handle, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1142. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo input)");
  1143. }
  1144. }
  1145. else
  1146. {
  1147. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1148. {
  1149. LADSPA_Handle const handle(it.getValue(nullptr));
  1150. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1151. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1152. {
  1153. try {
  1154. fDescriptor->connect_port(handle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1155. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (audio input)");
  1156. }
  1157. }
  1158. }
  1159. if (fForcedStereoOut)
  1160. {
  1161. if (LADSPA_Handle const handle = fHandles.getAt(0, nullptr))
  1162. {
  1163. try {
  1164. fDescriptor->connect_port(handle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1165. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo output)");
  1166. }
  1167. if (LADSPA_Handle const handle = fHandles.getAt(1, nullptr))
  1168. {
  1169. try {
  1170. fDescriptor->connect_port(handle, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1171. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo output)");
  1172. }
  1173. }
  1174. else
  1175. {
  1176. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1177. {
  1178. LADSPA_Handle const handle(it.getValue(nullptr));
  1179. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1180. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1181. {
  1182. try {
  1183. fDescriptor->connect_port(handle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1184. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (audio output)");
  1185. }
  1186. }
  1187. }
  1188. }
  1189. // -------------------------------------------------------------------
  1190. // Plugin buffers
  1191. void clearBuffers() noexcept override
  1192. {
  1193. carla_debug("CarlaPluginLADSPA::clearBuffers() - start");
  1194. if (fAudioInBuffers != nullptr)
  1195. {
  1196. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1197. {
  1198. if (fAudioInBuffers[i] != nullptr)
  1199. {
  1200. delete[] fAudioInBuffers[i];
  1201. fAudioInBuffers[i] = nullptr;
  1202. }
  1203. }
  1204. delete[] fAudioInBuffers;
  1205. fAudioInBuffers = nullptr;
  1206. }
  1207. if (fAudioOutBuffers != nullptr)
  1208. {
  1209. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1210. {
  1211. if (fAudioOutBuffers[i] != nullptr)
  1212. {
  1213. delete[] fAudioOutBuffers[i];
  1214. fAudioOutBuffers[i] = nullptr;
  1215. }
  1216. }
  1217. delete[] fAudioOutBuffers;
  1218. fAudioOutBuffers = nullptr;
  1219. }
  1220. if (fExtraStereoBuffer[0] != nullptr)
  1221. {
  1222. delete[] fExtraStereoBuffer[0];
  1223. fExtraStereoBuffer[0] = nullptr;
  1224. }
  1225. if (fExtraStereoBuffer[1] != nullptr)
  1226. {
  1227. delete[] fExtraStereoBuffer[1];
  1228. fExtraStereoBuffer[1] = nullptr;
  1229. }
  1230. if (fParamBuffers != nullptr)
  1231. {
  1232. delete[] fParamBuffers;
  1233. fParamBuffers = nullptr;
  1234. }
  1235. CarlaPlugin::clearBuffers();
  1236. carla_debug("CarlaPluginLADSPA::clearBuffers() - end");
  1237. }
  1238. // -------------------------------------------------------------------
  1239. const void* getNativeDescriptor() const noexcept override
  1240. {
  1241. return fDescriptor;
  1242. }
  1243. const void* getExtraStuff() const noexcept override
  1244. {
  1245. return fRdfDescriptor;
  1246. }
  1247. // -------------------------------------------------------------------
  1248. bool init(const char* const filename, const char* const name, const char* const label, const uint options,
  1249. const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1250. {
  1251. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1252. // ---------------------------------------------------------------
  1253. // first checks
  1254. if (pData->client != nullptr)
  1255. {
  1256. pData->engine->setLastError("Plugin client is already registered");
  1257. return false;
  1258. }
  1259. if (filename == nullptr || filename[0] == '\0')
  1260. {
  1261. pData->engine->setLastError("null filename");
  1262. return false;
  1263. }
  1264. if (label == nullptr || label[0] == '\0')
  1265. {
  1266. pData->engine->setLastError("null label");
  1267. return false;
  1268. }
  1269. // ---------------------------------------------------------------
  1270. // open DLL
  1271. if (! pData->libOpen(filename))
  1272. {
  1273. pData->engine->setLastError(pData->libError(filename));
  1274. return false;
  1275. }
  1276. // ---------------------------------------------------------------
  1277. // get DLL main entry
  1278. const LADSPA_Descriptor_Function descFn = pData->libSymbol<LADSPA_Descriptor_Function>("ladspa_descriptor");
  1279. if (descFn == nullptr)
  1280. {
  1281. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1282. return false;
  1283. }
  1284. // ---------------------------------------------------------------
  1285. // get descriptor that matches label
  1286. for (ulong d=0;; ++d)
  1287. {
  1288. try {
  1289. fDescriptor = descFn(d);
  1290. }
  1291. catch(...) {
  1292. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1293. fDescriptor = nullptr;
  1294. break;
  1295. }
  1296. if (fDescriptor == nullptr)
  1297. break;
  1298. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1299. {
  1300. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1301. fDescriptor = nullptr;
  1302. break;
  1303. }
  1304. if (fDescriptor->run == nullptr)
  1305. {
  1306. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1307. fDescriptor = nullptr;
  1308. break;
  1309. }
  1310. if (std::strcmp(fDescriptor->Label, label) == 0)
  1311. break;
  1312. }
  1313. if (fDescriptor == nullptr)
  1314. {
  1315. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1316. return false;
  1317. }
  1318. // ---------------------------------------------------------------
  1319. // get info
  1320. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1321. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1322. if (name != nullptr && name[0] != '\0')
  1323. pData->name = pData->engine->getUniquePluginName(name);
  1324. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  1325. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1326. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1327. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1328. else
  1329. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1330. pData->filename = carla_strdup(filename);
  1331. // ---------------------------------------------------------------
  1332. // register client
  1333. pData->client = pData->engine->addClient(this);
  1334. if (pData->client == nullptr || ! pData->client->isOk())
  1335. {
  1336. pData->engine->setLastError("Failed to register plugin client");
  1337. return false;
  1338. }
  1339. // ---------------------------------------------------------------
  1340. // initialize plugin
  1341. if (! addInstance())
  1342. return false;
  1343. // ---------------------------------------------------------------
  1344. // find latency port index
  1345. for (uint32_t i=0, iCtrl=0, count=getSafePortCount(); i<count; ++i)
  1346. {
  1347. const int portType(fDescriptor->PortDescriptors[i]);
  1348. if (! LADSPA_IS_PORT_CONTROL(portType))
  1349. continue;
  1350. const uint32_t index(iCtrl++);
  1351. if (! LADSPA_IS_PORT_OUTPUT(portType))
  1352. continue;
  1353. const char* const portName(fDescriptor->PortNames[i]);
  1354. CARLA_SAFE_ASSERT_BREAK(portName != nullptr);
  1355. if (std::strcmp(portName, "latency") == 0 ||
  1356. std::strcmp(portName, "_latency") == 0)
  1357. {
  1358. fLatencyIndex = static_cast<int32_t>(index);
  1359. break;
  1360. }
  1361. }
  1362. // ---------------------------------------------------------------
  1363. // check if this is dssi-vst
  1364. fIsDssiVst = CarlaString(filename).contains("dssi-vst", true);
  1365. // ---------------------------------------------------------------
  1366. // set default options
  1367. pData->options = 0x0;
  1368. /**/ if (fLatencyIndex >= 0 || fIsDssiVst)
  1369. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1370. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1371. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1372. /**/ if (pData->engine->getOptions().forceStereo)
  1373. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1374. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  1375. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1376. return true;
  1377. }
  1378. // -------------------------------------------------------------------
  1379. private:
  1380. LinkedList<LADSPA_Handle> fHandles;
  1381. const LADSPA_Descriptor* fDescriptor;
  1382. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1383. float** fAudioInBuffers;
  1384. float** fAudioOutBuffers;
  1385. float* fExtraStereoBuffer[2]; // used only if forcedStereoIn and audioOut == 2
  1386. float* fParamBuffers;
  1387. int32_t fLatencyIndex; // -1 if invalid
  1388. bool fForcedStereoIn;
  1389. bool fForcedStereoOut;
  1390. bool fIsDssiVst;
  1391. // -------------------------------------------------------------------
  1392. bool addInstance()
  1393. {
  1394. LADSPA_Handle handle;
  1395. try {
  1396. handle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  1397. } CARLA_SAFE_EXCEPTION_RETURN_ERR("LADSPA instantiate", "Plugin failed to initialize");
  1398. for (uint32_t i=0, count=pData->param.count; i<count; ++i)
  1399. {
  1400. const int32_t rindex(pData->param.data[i].rindex);
  1401. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0);
  1402. try {
  1403. fDescriptor->connect_port(handle, static_cast<ulong>(rindex), &fParamBuffers[i]);
  1404. } CARLA_SAFE_EXCEPTION("LADSPA connect_port");
  1405. }
  1406. if (fHandles.append(handle))
  1407. return true;
  1408. try {
  1409. fDescriptor->cleanup(handle);
  1410. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  1411. pData->engine->setLastError("Out of memory");
  1412. return false;
  1413. }
  1414. uint32_t getSafePortCount() const noexcept
  1415. {
  1416. if (fDescriptor->PortCount == 0)
  1417. return 0;
  1418. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1419. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1420. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1421. return static_cast<uint32_t>(fDescriptor->PortCount);
  1422. }
  1423. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1424. {
  1425. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  1426. return true;
  1427. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  1428. return true;
  1429. return false;
  1430. }
  1431. static bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf,
  1432. const bool wantName, const bool useBracket) noexcept
  1433. {
  1434. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  1435. if (sepBracketStart == nullptr)
  1436. return false;
  1437. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  1438. if (sepBracketEnd == nullptr)
  1439. return false;
  1440. const std::size_t unitSize(static_cast<std::size_t>(sepBracketEnd-sepBracketStart-2));
  1441. if (unitSize > 7) // very unlikely to have such big unit
  1442. return false;
  1443. const std::size_t sepIndex(std::strlen(paramName)-unitSize-3);
  1444. // just in case
  1445. if (sepIndex+2 >= STR_MAX)
  1446. return false;
  1447. if (wantName)
  1448. {
  1449. std::strncpy(strBuf, paramName, sepIndex);
  1450. strBuf[sepIndex] = '\0';
  1451. }
  1452. else
  1453. {
  1454. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  1455. strBuf[unitSize] = '\0';
  1456. }
  1457. return true;
  1458. }
  1459. // -------------------------------------------------------------------
  1460. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginLADSPA)
  1461. };
  1462. // -------------------------------------------------------------------------------------------------------------------
  1463. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1464. {
  1465. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\", " P_INT64 ", %x}, %p)",
  1466. init.engine, init.filename, init.name, init.label, init.uniqueId, init.options, rdfDescriptor);
  1467. CarlaPluginLADSPA* const plugin(new CarlaPluginLADSPA(init.engine, init.id));
  1468. if (! plugin->init(init.filename, init.name, init.label, init.options, rdfDescriptor))
  1469. {
  1470. delete plugin;
  1471. return nullptr;
  1472. }
  1473. return plugin;
  1474. }
  1475. // -------------------------------------------------------------------------------------------------------------------
  1476. CARLA_BACKEND_END_NAMESPACE