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.

1794 lines
63KB

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