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.

CarlaPluginLADSPA.cpp 65KB

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