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.

1765 lines
61KB

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