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.

1740 lines
60KB

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