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. }
  341. // -------------------------------------------------------------------
  342. // Plugin state
  343. void reload() override
  344. {
  345. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  346. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  347. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  348. carla_debug("LadspaPlugin::reload() - start");
  349. const EngineProcessMode processMode(pData->engine->getProccessMode());
  350. // Safely disable plugin for reload
  351. const ScopedDisabler sd(this);
  352. if (pData->active)
  353. deactivate();
  354. clearBuffers();
  355. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  356. const uint32_t portCount(getSafePortCount());
  357. uint32_t aIns, aOuts, params;
  358. aIns = aOuts = params = 0;
  359. bool forcedStereoIn, forcedStereoOut;
  360. forcedStereoIn = forcedStereoOut = false;
  361. bool needsCtrlIn, needsCtrlOut;
  362. needsCtrlIn = needsCtrlOut = false;
  363. for (uint32_t i=0; i < portCount; ++i)
  364. {
  365. const LADSPA_PortDescriptor portType(fDescriptor->PortDescriptors[i]);
  366. if (LADSPA_IS_PORT_AUDIO(portType))
  367. {
  368. if (LADSPA_IS_PORT_INPUT(portType))
  369. aIns += 1;
  370. else if (LADSPA_IS_PORT_OUTPUT(portType))
  371. aOuts += 1;
  372. }
  373. else if (LADSPA_IS_PORT_CONTROL(portType))
  374. params += 1;
  375. }
  376. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  377. {
  378. if (fHandle2 == nullptr)
  379. {
  380. try {
  381. fHandle2 = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(sampleRate));
  382. } CARLA_SAFE_EXCEPTION("LADSPA instantiate #2");
  383. }
  384. if (fHandle2 != nullptr)
  385. {
  386. if (aIns == 1)
  387. {
  388. aIns = 2;
  389. forcedStereoIn = true;
  390. }
  391. if (aOuts == 1)
  392. {
  393. aOuts = 2;
  394. forcedStereoOut = true;
  395. }
  396. }
  397. }
  398. if (aIns > 0)
  399. {
  400. pData->audioIn.createNew(aIns);
  401. fAudioInBuffers = new float*[aIns];
  402. for (uint32_t i=0; i < aIns; ++i)
  403. fAudioInBuffers[i] = nullptr;
  404. }
  405. if (aOuts > 0)
  406. {
  407. pData->audioOut.createNew(aOuts);
  408. fAudioOutBuffers = new float*[aOuts];
  409. needsCtrlIn = true;
  410. for (uint32_t i=0; i < aOuts; ++i)
  411. fAudioOutBuffers[i] = nullptr;
  412. }
  413. if (params > 0)
  414. {
  415. pData->param.createNew(params, true);
  416. fParamBuffers = new float[params];
  417. FloatVectorOperations::clear(fParamBuffers, static_cast<int>(params));
  418. }
  419. const uint portNameSize(pData->engine->getMaxPortNameSize());
  420. CarlaString portName;
  421. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  422. {
  423. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  424. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  425. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  426. if (LADSPA_IS_PORT_AUDIO(portType))
  427. {
  428. portName.clear();
  429. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  430. {
  431. portName = pData->name;
  432. portName += ":";
  433. }
  434. if (fDescriptor->PortNames[i] != nullptr && fDescriptor->PortNames[i][0] != '\0')
  435. {
  436. portName += fDescriptor->PortNames[i];
  437. }
  438. else
  439. {
  440. if (LADSPA_IS_PORT_INPUT(portType))
  441. {
  442. if (aIns > 1)
  443. {
  444. portName += "audio-in_";
  445. portName += CarlaString(iAudioIn+1);
  446. }
  447. else
  448. portName += "audio-in";
  449. }
  450. else
  451. {
  452. if (aOuts > 1)
  453. {
  454. portName += "audio-out_";
  455. portName += CarlaString(iAudioOut+1);
  456. }
  457. else
  458. portName += "audio-out";
  459. }
  460. }
  461. portName.truncate(portNameSize);
  462. if (LADSPA_IS_PORT_INPUT(portType))
  463. {
  464. const uint32_t j = iAudioIn++;
  465. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  466. pData->audioIn.ports[j].rindex = i;
  467. if (forcedStereoIn)
  468. {
  469. portName += "_2";
  470. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  471. pData->audioIn.ports[1].rindex = i;
  472. }
  473. }
  474. else if (LADSPA_IS_PORT_OUTPUT(portType))
  475. {
  476. const uint32_t j = iAudioOut++;
  477. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  478. pData->audioOut.ports[j].rindex = i;
  479. if (forcedStereoOut)
  480. {
  481. portName += "_2";
  482. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  483. pData->audioOut.ports[1].rindex = i;
  484. }
  485. }
  486. else
  487. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  488. }
  489. else if (LADSPA_IS_PORT_CONTROL(portType))
  490. {
  491. const uint32_t j = iCtrl++;
  492. pData->param.data[j].index = static_cast<int32_t>(j);
  493. pData->param.data[j].rindex = static_cast<int32_t>(i);
  494. const char* const paramName(fDescriptor->PortNames[i] != nullptr ? fDescriptor->PortNames[i] : "unknown");
  495. float min, max, def, step, stepSmall, stepLarge;
  496. // min value
  497. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  498. min = portRangeHints.LowerBound;
  499. else
  500. min = 0.0f;
  501. // max value
  502. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  503. max = portRangeHints.UpperBound;
  504. else
  505. max = 1.0f;
  506. if (min > max)
  507. {
  508. carla_stderr2("WARNING - Broken plugin parameter '%s': min > max", paramName);
  509. min = max - 0.1f;
  510. }
  511. else if (min == max)
  512. {
  513. carla_stderr2("WARNING - Broken plugin parameter '%s': min == maxf", paramName);
  514. max = min + 0.1f;
  515. }
  516. // default value
  517. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  518. def = fRdfDescriptor->Ports[i].Default;
  519. else
  520. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  521. if (def < min)
  522. def = min;
  523. else if (def > max)
  524. def = max;
  525. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  526. {
  527. min *= sampleRate;
  528. max *= sampleRate;
  529. def *= sampleRate;
  530. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  531. }
  532. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  533. {
  534. step = max - min;
  535. stepSmall = step;
  536. stepLarge = step;
  537. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  538. }
  539. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  540. {
  541. step = 1.0f;
  542. stepSmall = 1.0f;
  543. stepLarge = 10.0f;
  544. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  545. }
  546. else
  547. {
  548. const float range = max - min;
  549. step = range/100.0f;
  550. stepSmall = range/1000.0f;
  551. stepLarge = range/10.0f;
  552. }
  553. if (LADSPA_IS_PORT_INPUT(portType))
  554. {
  555. pData->param.data[j].type = PARAMETER_INPUT;
  556. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  557. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  558. needsCtrlIn = true;
  559. }
  560. else if (LADSPA_IS_PORT_OUTPUT(portType))
  561. {
  562. pData->param.data[j].type = PARAMETER_OUTPUT;
  563. if (std::strcmp(paramName, "latency") == 0 || std::strcmp(paramName, "_latency") == 0)
  564. {
  565. min = 0.0f;
  566. max = sampleRate;
  567. def = 0.0f;
  568. step = 1.0f;
  569. stepSmall = 1.0f;
  570. stepLarge = 1.0f;
  571. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  572. CARLA_SAFE_ASSERT(fLatencyIndex == -1);
  573. fLatencyIndex = static_cast<int32_t>(j);
  574. }
  575. else
  576. {
  577. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  578. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  579. needsCtrlOut = true;
  580. }
  581. }
  582. else
  583. {
  584. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  585. }
  586. // extra parameter hints
  587. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  588. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  589. // check for scalepoints, require at least 2 to make it useful
  590. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount >= 2)
  591. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  592. pData->param.ranges[j].min = min;
  593. pData->param.ranges[j].max = max;
  594. pData->param.ranges[j].def = def;
  595. pData->param.ranges[j].step = step;
  596. pData->param.ranges[j].stepSmall = stepSmall;
  597. pData->param.ranges[j].stepLarge = stepLarge;
  598. // Start parameters in their default values
  599. fParamBuffers[j] = def;
  600. try {
  601. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  602. } CARLA_SAFE_EXCEPTION("LADSPA connect_port parameter");
  603. if (fHandle2 != nullptr)
  604. {
  605. try {
  606. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  607. } CARLA_SAFE_EXCEPTION("LADSPA connect_port parameter #2");
  608. }
  609. }
  610. else
  611. {
  612. // Not Audio or Control
  613. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  614. try {
  615. fDescriptor->connect_port(fHandle, i, nullptr);
  616. } CARLA_SAFE_EXCEPTION("LADSPA connect_port null");
  617. if (fHandle2 != nullptr)
  618. {
  619. try {
  620. fDescriptor->connect_port(fHandle2, i, nullptr);
  621. } CARLA_SAFE_EXCEPTION("LADSPA connect_port null #2");
  622. }
  623. }
  624. }
  625. if (needsCtrlIn)
  626. {
  627. portName.clear();
  628. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  629. {
  630. portName = pData->name;
  631. portName += ":";
  632. }
  633. portName += "events-in";
  634. portName.truncate(portNameSize);
  635. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  636. }
  637. if (needsCtrlOut)
  638. {
  639. portName.clear();
  640. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  641. {
  642. portName = pData->name;
  643. portName += ":";
  644. }
  645. portName += "events-out";
  646. portName.truncate(portNameSize);
  647. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  648. }
  649. if (forcedStereoIn || forcedStereoOut)
  650. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  651. else
  652. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  653. // plugin hints
  654. pData->hints = 0x0;
  655. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  656. pData->hints |= PLUGIN_IS_RTSAFE;
  657. #ifndef BUILD_BRIDGE
  658. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  659. pData->hints |= PLUGIN_CAN_DRYWET;
  660. if (aOuts > 0)
  661. pData->hints |= PLUGIN_CAN_VOLUME;
  662. if (aOuts >= 2 && aOuts % 2 == 0)
  663. pData->hints |= PLUGIN_CAN_BALANCE;
  664. #endif
  665. // extra plugin hints
  666. pData->extraHints = 0x0;
  667. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  668. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  669. // check latency
  670. if (fLatencyIndex >= 0)
  671. {
  672. // we need to pre-run the plugin so it can update its latency control-port
  673. float tmpIn [(aIns > 0) ? aIns : 1][2];
  674. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  675. for (uint32_t j=0; j < aIns; ++j)
  676. {
  677. tmpIn[j][0] = 0.0f;
  678. tmpIn[j][1] = 0.0f;
  679. try {
  680. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  681. } CARLA_SAFE_EXCEPTION("LADSPA connect_port latency input");
  682. }
  683. for (uint32_t j=0; j < aOuts; ++j)
  684. {
  685. tmpOut[j][0] = 0.0f;
  686. tmpOut[j][1] = 0.0f;
  687. try {
  688. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  689. } CARLA_SAFE_EXCEPTION("LADSPA connect_port latency output");
  690. }
  691. if (fDescriptor->activate != nullptr)
  692. {
  693. try {
  694. fDescriptor->activate(fHandle);
  695. } CARLA_SAFE_EXCEPTION("LADSPA latency activate");
  696. }
  697. try {
  698. fDescriptor->run(fHandle, 2);
  699. } CARLA_SAFE_EXCEPTION("LADSPA latency run");
  700. if (fDescriptor->deactivate != nullptr)
  701. {
  702. try {
  703. fDescriptor->deactivate(fHandle);
  704. } CARLA_SAFE_EXCEPTION("LADSPA latency deactivate");
  705. }
  706. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  707. if (latency >= 0)
  708. {
  709. const uint32_t ulatency(static_cast<uint32_t>(latency));
  710. if (pData->latency != ulatency)
  711. {
  712. carla_stdout("latency = %i", latency);
  713. pData->latency = ulatency;
  714. pData->client->setLatency(ulatency);
  715. #ifndef BUILD_BRIDGE
  716. pData->recreateLatencyBuffers();
  717. #endif
  718. }
  719. }
  720. else
  721. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  722. fLatencyChanged = false;
  723. }
  724. bufferSizeChanged(pData->engine->getBufferSize());
  725. if (pData->active)
  726. activate();
  727. carla_debug("LadspaPlugin::reload() - end");
  728. }
  729. // -------------------------------------------------------------------
  730. // Plugin processing
  731. void activate() noexcept override
  732. {
  733. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  734. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  735. if (fDescriptor->activate != nullptr)
  736. {
  737. try {
  738. fDescriptor->activate(fHandle);
  739. } CARLA_SAFE_EXCEPTION("LADSPA activate");
  740. if (fHandle2 != nullptr)
  741. {
  742. try {
  743. fDescriptor->activate(fHandle2);
  744. } CARLA_SAFE_EXCEPTION("LADSPA activate #2");
  745. }
  746. }
  747. }
  748. void deactivate() noexcept override
  749. {
  750. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  751. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  752. if (fDescriptor->deactivate != nullptr)
  753. {
  754. try {
  755. fDescriptor->deactivate(fHandle);
  756. } CARLA_SAFE_EXCEPTION("LADSPA deactivate");
  757. if (fHandle2 != nullptr)
  758. {
  759. try {
  760. fDescriptor->deactivate(fHandle2);
  761. } CARLA_SAFE_EXCEPTION("LADSPA deactivate #2");
  762. }
  763. }
  764. }
  765. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  766. {
  767. // --------------------------------------------------------------------------------------------------------
  768. // Check if active
  769. if (! pData->active)
  770. {
  771. // disable any output sound
  772. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  773. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  774. return;
  775. }
  776. // --------------------------------------------------------------------------------------------------------
  777. // Check if needs reset
  778. if (pData->needsReset)
  779. {
  780. #ifndef BUILD_BRIDGE
  781. if (pData->latency > 0)
  782. {
  783. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  784. FloatVectorOperations::clear(pData->latencyBuffers[i], static_cast<int>(pData->latency));
  785. }
  786. #endif
  787. pData->needsReset = false;
  788. }
  789. // --------------------------------------------------------------------------------------------------------
  790. // Event Input and Processing
  791. if (pData->event.portIn != nullptr)
  792. {
  793. // ----------------------------------------------------------------------------------------------------
  794. // Event Input (System)
  795. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  796. uint32_t numEvents = pData->event.portIn->getEventCount();
  797. uint32_t timeOffset = 0;
  798. for (uint32_t i=0; i < numEvents; ++i)
  799. {
  800. const EngineEvent& event(pData->event.portIn->getEvent(i));
  801. if (event.time >= frames)
  802. continue;
  803. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  804. if (isSampleAccurate && event.time > timeOffset)
  805. {
  806. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset))
  807. timeOffset = event.time;
  808. }
  809. switch (event.type)
  810. {
  811. case kEngineEventTypeNull:
  812. break;
  813. case kEngineEventTypeControl: {
  814. const EngineControlEvent& ctrlEvent(event.ctrl);
  815. switch (ctrlEvent.type)
  816. {
  817. case kEngineControlEventTypeNull:
  818. break;
  819. case kEngineControlEventTypeParameter: {
  820. #ifndef BUILD_BRIDGE
  821. // Control backend stuff
  822. if (event.channel == pData->ctrlChannel)
  823. {
  824. float value;
  825. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  826. {
  827. value = ctrlEvent.value;
  828. setDryWet(value, false, false);
  829. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  830. break;
  831. }
  832. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  833. {
  834. value = ctrlEvent.value*127.0f/100.0f;
  835. setVolume(value, false, false);
  836. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  837. break;
  838. }
  839. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  840. {
  841. float left, right;
  842. value = ctrlEvent.value/0.5f - 1.0f;
  843. if (value < 0.0f)
  844. {
  845. left = -1.0f;
  846. right = (value*2.0f)+1.0f;
  847. }
  848. else if (value > 0.0f)
  849. {
  850. left = (value*2.0f)-1.0f;
  851. right = 1.0f;
  852. }
  853. else
  854. {
  855. left = -1.0f;
  856. right = 1.0f;
  857. }
  858. setBalanceLeft(left, false, false);
  859. setBalanceRight(right, false, false);
  860. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  861. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  862. break;
  863. }
  864. }
  865. #endif
  866. // Control plugin parameters
  867. for (uint32_t k=0; k < pData->param.count; ++k)
  868. {
  869. if (pData->param.data[k].midiChannel != event.channel)
  870. continue;
  871. if (pData->param.data[k].midiCC != ctrlEvent.param)
  872. continue;
  873. if (pData->param.data[k].type != PARAMETER_INPUT)
  874. continue;
  875. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  876. continue;
  877. float value;
  878. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  879. {
  880. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  881. }
  882. else
  883. {
  884. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  885. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  886. value = std::rint(value);
  887. }
  888. setParameterValue(k, value, false, false, false);
  889. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  890. break;
  891. }
  892. break;
  893. } // case kEngineControlEventTypeParameter
  894. case kEngineControlEventTypeMidiBank:
  895. case kEngineControlEventTypeMidiProgram:
  896. case kEngineControlEventTypeAllSoundOff:
  897. case kEngineControlEventTypeAllNotesOff:
  898. break;
  899. } // switch (ctrlEvent.type)
  900. break;
  901. } // case kEngineEventTypeControl
  902. case kEngineEventTypeMidi:
  903. break;
  904. } // switch (event.type)
  905. }
  906. pData->postRtEvents.trySplice();
  907. if (frames > timeOffset)
  908. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  909. } // End of Event Input and Processing
  910. // --------------------------------------------------------------------------------------------------------
  911. // Plugin processing (no events)
  912. else
  913. {
  914. processSingle(inBuffer, outBuffer, frames, 0);
  915. } // End of Plugin processing (no events)
  916. #ifndef BUILD_BRIDGE
  917. // --------------------------------------------------------------------------------------------------------
  918. // Latency, save values for next callback
  919. if (fLatencyIndex != -1)
  920. {
  921. if (pData->latency != static_cast<uint32_t>(fParamBuffers[fLatencyIndex]))
  922. {
  923. fLatencyChanged = true;
  924. }
  925. else if (pData->latency > 0)
  926. {
  927. if (pData->latency <= frames)
  928. {
  929. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  930. FloatVectorOperations::copy(pData->latencyBuffers[i], inBuffer[i]+(frames-pData->latency), static_cast<int>(pData->latency));
  931. }
  932. else
  933. {
  934. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  935. {
  936. for (k=0; k < pData->latency-frames; ++k)
  937. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  938. for (j=0; k < pData->latency; ++j, ++k)
  939. pData->latencyBuffers[i][k] = inBuffer[i][j];
  940. }
  941. }
  942. }
  943. }
  944. #endif
  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. }
  967. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  968. {
  969. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  970. if (pData->audioIn.count > 0)
  971. {
  972. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  973. }
  974. if (pData->audioOut.count > 0)
  975. {
  976. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  977. }
  978. // --------------------------------------------------------------------------------------------------------
  979. // Try lock, silence otherwise
  980. if (pData->engine->isOffline())
  981. {
  982. pData->singleMutex.lock();
  983. }
  984. else if (! pData->singleMutex.tryLock())
  985. {
  986. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  987. {
  988. for (uint32_t k=0; k < frames; ++k)
  989. outBuffer[i][k+timeOffset] = 0.0f;
  990. }
  991. return false;
  992. }
  993. // --------------------------------------------------------------------------------------------------------
  994. // Reset audio buffers
  995. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  996. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, static_cast<int>(frames));
  997. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  998. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  999. // --------------------------------------------------------------------------------------------------------
  1000. // Run plugin
  1001. try {
  1002. fDescriptor->run(fHandle, frames);
  1003. } CARLA_SAFE_EXCEPTION("LADSPA run");
  1004. if (fHandle2 != nullptr)
  1005. {
  1006. try {
  1007. fDescriptor->run(fHandle2, frames);
  1008. } CARLA_SAFE_EXCEPTION("LADSPA run #2");
  1009. }
  1010. #ifndef BUILD_BRIDGE
  1011. // --------------------------------------------------------------------------------------------------------
  1012. // Post-processing (dry/wet, volume and balance)
  1013. {
  1014. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1015. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1016. const bool isMono = (pData->audioIn.count == 1);
  1017. bool isPair;
  1018. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1019. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1020. {
  1021. // Dry/Wet
  1022. if (doDryWet)
  1023. {
  1024. for (uint32_t k=0; k < frames; ++k)
  1025. {
  1026. if (k < pData->latency)
  1027. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  1028. else if (pData->latency < frames)
  1029. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  1030. else
  1031. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  1032. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1033. }
  1034. }
  1035. // Balance
  1036. if (doBalance)
  1037. {
  1038. isPair = (i % 2 == 0);
  1039. if (isPair)
  1040. {
  1041. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1042. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  1043. }
  1044. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1045. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1046. for (uint32_t k=0; k < frames; ++k)
  1047. {
  1048. if (isPair)
  1049. {
  1050. // left
  1051. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1052. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1053. }
  1054. else
  1055. {
  1056. // right
  1057. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1058. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1059. }
  1060. }
  1061. }
  1062. // Volume (and buffer copy)
  1063. {
  1064. for (uint32_t k=0; k < frames; ++k)
  1065. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1066. }
  1067. }
  1068. } // End of Post-processing
  1069. #else // BUILD_BRIDGE
  1070. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1071. {
  1072. for (uint32_t k=0; k < frames; ++k)
  1073. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1074. }
  1075. #endif
  1076. // --------------------------------------------------------------------------------------------------------
  1077. pData->singleMutex.unlock();
  1078. return true;
  1079. }
  1080. void bufferSizeChanged(const uint32_t newBufferSize) override
  1081. {
  1082. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1083. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1084. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1085. {
  1086. if (fAudioInBuffers[i] != nullptr)
  1087. delete[] fAudioInBuffers[i];
  1088. fAudioInBuffers[i] = new float[newBufferSize];
  1089. }
  1090. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1091. {
  1092. if (fAudioOutBuffers[i] != nullptr)
  1093. delete[] fAudioOutBuffers[i];
  1094. fAudioOutBuffers[i] = new float[newBufferSize];
  1095. }
  1096. if (fHandle2 == nullptr)
  1097. {
  1098. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1099. {
  1100. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1101. try {
  1102. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1103. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input");
  1104. }
  1105. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1106. {
  1107. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1108. try {
  1109. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1110. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output");
  1111. }
  1112. }
  1113. else
  1114. {
  1115. if (pData->audioIn.count > 0)
  1116. {
  1117. CARLA_ASSERT(pData->audioIn.count == 2);
  1118. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1119. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1120. try {
  1121. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1122. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #1");
  1123. try {
  1124. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1125. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio input #2");
  1126. }
  1127. if (pData->audioOut.count > 0)
  1128. {
  1129. CARLA_ASSERT(pData->audioOut.count == 2);
  1130. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1131. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1132. try {
  1133. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1134. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #1");
  1135. try {
  1136. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1137. } CARLA_SAFE_EXCEPTION("LADSPA connect_port audio output #2");
  1138. }
  1139. }
  1140. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1141. }
  1142. void sampleRateChanged(const double newSampleRate) override
  1143. {
  1144. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1145. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1146. // TODO
  1147. (void)newSampleRate;
  1148. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1149. }
  1150. // -------------------------------------------------------------------
  1151. // Plugin buffers
  1152. void clearBuffers() noexcept override
  1153. {
  1154. carla_debug("LadspaPlugin::clearBuffers() - start");
  1155. if (fAudioInBuffers != nullptr)
  1156. {
  1157. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1158. {
  1159. if (fAudioInBuffers[i] != nullptr)
  1160. {
  1161. delete[] fAudioInBuffers[i];
  1162. fAudioInBuffers[i] = nullptr;
  1163. }
  1164. }
  1165. delete[] fAudioInBuffers;
  1166. fAudioInBuffers = nullptr;
  1167. }
  1168. if (fAudioOutBuffers != nullptr)
  1169. {
  1170. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1171. {
  1172. if (fAudioOutBuffers[i] != nullptr)
  1173. {
  1174. delete[] fAudioOutBuffers[i];
  1175. fAudioOutBuffers[i] = nullptr;
  1176. }
  1177. }
  1178. delete[] fAudioOutBuffers;
  1179. fAudioOutBuffers = nullptr;
  1180. }
  1181. if (fParamBuffers != nullptr)
  1182. {
  1183. delete[] fParamBuffers;
  1184. fParamBuffers = nullptr;
  1185. }
  1186. CarlaPlugin::clearBuffers();
  1187. carla_debug("LadspaPlugin::clearBuffers() - end");
  1188. }
  1189. // -------------------------------------------------------------------
  1190. void* getNativeHandle() const noexcept override
  1191. {
  1192. return fHandle;
  1193. }
  1194. const void* getNativeDescriptor() const noexcept override
  1195. {
  1196. return fDescriptor;
  1197. }
  1198. const void* getExtraStuff() const noexcept override
  1199. {
  1200. return fRdfDescriptor;
  1201. }
  1202. // -------------------------------------------------------------------
  1203. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1204. {
  1205. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1206. // ---------------------------------------------------------------
  1207. // first checks
  1208. if (pData->client != nullptr)
  1209. {
  1210. pData->engine->setLastError("Plugin client is already registered");
  1211. return false;
  1212. }
  1213. if (filename == nullptr || filename[0] == '\0')
  1214. {
  1215. pData->engine->setLastError("null filename");
  1216. return false;
  1217. }
  1218. if (label == nullptr || label[0] == '\0')
  1219. {
  1220. pData->engine->setLastError("null label");
  1221. return false;
  1222. }
  1223. // ---------------------------------------------------------------
  1224. // open DLL
  1225. if (! pData->libOpen(filename))
  1226. {
  1227. pData->engine->setLastError(pData->libError(filename));
  1228. return false;
  1229. }
  1230. // ---------------------------------------------------------------
  1231. // get DLL main entry
  1232. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)pData->libSymbol("ladspa_descriptor");
  1233. if (descFn == nullptr)
  1234. {
  1235. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1236. return false;
  1237. }
  1238. // ---------------------------------------------------------------
  1239. // get descriptor that matches label
  1240. ulong i = 0;
  1241. for (;;)
  1242. {
  1243. try {
  1244. fDescriptor = descFn(i++);
  1245. }
  1246. catch(...) {
  1247. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1248. fDescriptor = nullptr;
  1249. break;
  1250. }
  1251. if (fDescriptor == nullptr)
  1252. break;
  1253. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1254. {
  1255. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1256. fDescriptor = nullptr;
  1257. break;
  1258. }
  1259. if (fDescriptor->run == nullptr)
  1260. {
  1261. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1262. fDescriptor = nullptr;
  1263. break;
  1264. }
  1265. if (std::strcmp(fDescriptor->Label, label) == 0)
  1266. break;
  1267. }
  1268. if (fDescriptor == nullptr)
  1269. {
  1270. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1271. return false;
  1272. }
  1273. // ---------------------------------------------------------------
  1274. // get info
  1275. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1276. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1277. if (name != nullptr && name[0] != '\0')
  1278. pData->name = pData->engine->getUniquePluginName(name);
  1279. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  1280. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1281. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1282. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1283. else
  1284. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1285. pData->filename = carla_strdup(filename);
  1286. // ---------------------------------------------------------------
  1287. // register client
  1288. pData->client = pData->engine->addClient(this);
  1289. if (pData->client == nullptr || ! pData->client->isOk())
  1290. {
  1291. pData->engine->setLastError("Failed to register plugin client");
  1292. return false;
  1293. }
  1294. // ---------------------------------------------------------------
  1295. // initialize plugin
  1296. try {
  1297. fHandle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  1298. } CARLA_SAFE_EXCEPTION("LADSPA instantiate");
  1299. if (fHandle == nullptr)
  1300. {
  1301. pData->engine->setLastError("Plugin failed to initialize");
  1302. return false;
  1303. }
  1304. // ---------------------------------------------------------------
  1305. // set default options
  1306. #ifdef __USE_GNU
  1307. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst") != nullptr);
  1308. #else
  1309. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst") != nullptr);
  1310. #endif
  1311. pData->options = 0x0;
  1312. if (fLatencyIndex >= 0 || isDssiVst)
  1313. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1314. if (pData->engine->getOptions().forceStereo)
  1315. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1316. return true;
  1317. }
  1318. // -------------------------------------------------------------------
  1319. private:
  1320. LADSPA_Handle fHandle;
  1321. LADSPA_Handle fHandle2;
  1322. const LADSPA_Descriptor* fDescriptor;
  1323. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1324. float** fAudioInBuffers;
  1325. float** fAudioOutBuffers;
  1326. float* fParamBuffers;
  1327. bool fLatencyChanged;
  1328. int32_t fLatencyIndex; // -1 if invalid
  1329. // -------------------------------------------------------------------
  1330. uint32_t getSafePortCount() const noexcept
  1331. {
  1332. if (fDescriptor->PortCount == 0)
  1333. return 0;
  1334. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1335. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1336. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1337. return static_cast<uint32_t>(fDescriptor->PortCount);
  1338. }
  1339. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1340. {
  1341. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  1342. return true;
  1343. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  1344. return true;
  1345. return false;
  1346. }
  1347. bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf, const bool wantName, const bool useBracket) const noexcept
  1348. {
  1349. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  1350. if (sepBracketStart == nullptr)
  1351. return false;
  1352. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  1353. if (sepBracketEnd == nullptr)
  1354. return false;
  1355. const size_t unitSize(static_cast<size_t>(sepBracketEnd-sepBracketStart-2));
  1356. if (unitSize > 7) // very unlikely to have such big unit
  1357. return false;
  1358. const size_t sepIndex(std::strlen(paramName)-unitSize-3);
  1359. // just in case
  1360. if (sepIndex > STR_MAX)
  1361. return false;
  1362. if (wantName)
  1363. {
  1364. std::strncpy(strBuf, paramName, sepIndex);
  1365. strBuf[sepIndex] = '\0';
  1366. }
  1367. else
  1368. {
  1369. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  1370. strBuf[unitSize] = '\0';
  1371. }
  1372. return true;
  1373. }
  1374. // -------------------------------------------------------------------
  1375. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1376. };
  1377. // -------------------------------------------------------------------------------------------------------------------
  1378. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1379. {
  1380. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %p)", init.engine, init.filename, init.name, init.label, init.uniqueId, rdfDescriptor);
  1381. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1382. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1383. {
  1384. delete plugin;
  1385. return nullptr;
  1386. }
  1387. plugin->reload();
  1388. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1389. {
  1390. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1391. delete plugin;
  1392. return nullptr;
  1393. }
  1394. return plugin;
  1395. }
  1396. // -------------------------------------------------------------------------------------------------------------------
  1397. CARLA_BACKEND_END_NAMESPACE