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.

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