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
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  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. // check initial latency
  638. findInitialLatencyValue(aIns, aOuts);
  639. fForcedStereoIn = forcedStereoIn;
  640. fForcedStereoOut = forcedStereoOut;
  641. bufferSizeChanged(pData->engine->getBufferSize());
  642. if (pData->active)
  643. activate();
  644. carla_debug("CarlaPluginLADSPA::reload() - end");
  645. }
  646. void findInitialLatencyValue(const uint32_t aIns, const uint32_t aOuts) const
  647. {
  648. if (fLatencyIndex < 0 || fHandles.count() == 0)
  649. return;
  650. // we need to pre-run the plugin so it can update its latency control-port
  651. const LADSPA_Handle handle(fHandles.getFirst(nullptr));
  652. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  653. float tmpIn [(aIns > 0) ? aIns : 1][2];
  654. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  655. for (uint32_t j=0; j < aIns; ++j)
  656. {
  657. tmpIn[j][0] = 0.0f;
  658. tmpIn[j][1] = 0.0f;
  659. try {
  660. fDescriptor->connect_port(handle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  661. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (latency input)");
  662. }
  663. for (uint32_t j=0; j < aOuts; ++j)
  664. {
  665. tmpOut[j][0] = 0.0f;
  666. tmpOut[j][1] = 0.0f;
  667. try {
  668. fDescriptor->connect_port(handle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  669. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (latency output)");
  670. }
  671. if (fDescriptor->activate != nullptr)
  672. {
  673. try {
  674. fDescriptor->activate(handle);
  675. } CARLA_SAFE_EXCEPTION("LADSPA latency activate");
  676. }
  677. try {
  678. fDescriptor->run(handle, 2);
  679. } CARLA_SAFE_EXCEPTION("LADSPA latency run");
  680. if (fDescriptor->deactivate != nullptr)
  681. {
  682. try {
  683. fDescriptor->deactivate(handle);
  684. } CARLA_SAFE_EXCEPTION("LADSPA latency deactivate");
  685. }
  686. // done, let's get the value
  687. if (const uint32_t latency = getLatencyInFrames())
  688. {
  689. pData->client->setLatency(latency);
  690. #ifndef BUILD_BRIDGE
  691. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  692. #endif
  693. }
  694. }
  695. // -------------------------------------------------------------------
  696. // Plugin processing
  697. void activate() noexcept override
  698. {
  699. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  700. if (fDescriptor->activate != nullptr)
  701. {
  702. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  703. {
  704. LADSPA_Handle const handle(it.getValue(nullptr));
  705. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  706. try {
  707. fDescriptor->activate(handle);
  708. } CARLA_SAFE_EXCEPTION("LADSPA activate");
  709. }
  710. }
  711. }
  712. void deactivate() noexcept override
  713. {
  714. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  715. if (fDescriptor->deactivate != nullptr)
  716. {
  717. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  718. {
  719. LADSPA_Handle const handle(it.getValue(nullptr));
  720. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  721. try {
  722. fDescriptor->deactivate(handle);
  723. } CARLA_SAFE_EXCEPTION("LADSPA deactivate");
  724. }
  725. }
  726. }
  727. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  728. {
  729. // --------------------------------------------------------------------------------------------------------
  730. // Check if active
  731. if (! pData->active)
  732. {
  733. // disable any output sound
  734. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  735. carla_zeroFloats(audioOut[i], frames);
  736. return;
  737. }
  738. // --------------------------------------------------------------------------------------------------------
  739. // Check if needs reset
  740. if (pData->needsReset)
  741. {
  742. // nothing to do
  743. pData->needsReset = false;
  744. }
  745. // --------------------------------------------------------------------------------------------------------
  746. // Event Input and Processing
  747. if (pData->event.portIn != nullptr)
  748. {
  749. // ----------------------------------------------------------------------------------------------------
  750. // Event Input (System)
  751. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  752. uint32_t timeOffset = 0;
  753. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  754. {
  755. const EngineEvent& event(pData->event.portIn->getEvent(i));
  756. uint32_t eventTime = event.time;
  757. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  758. if (eventTime < timeOffset)
  759. {
  760. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  761. eventTime, timeOffset, pData->name);
  762. eventTime = timeOffset;
  763. }
  764. if (isSampleAccurate && eventTime > timeOffset)
  765. {
  766. if (processSingle(audioIn, audioOut, eventTime - timeOffset, timeOffset))
  767. timeOffset = eventTime;
  768. }
  769. switch (event.type)
  770. {
  771. case kEngineEventTypeNull:
  772. break;
  773. case kEngineEventTypeControl: {
  774. const EngineControlEvent& ctrlEvent(event.ctrl);
  775. switch (ctrlEvent.type)
  776. {
  777. case kEngineControlEventTypeNull:
  778. break;
  779. case kEngineControlEventTypeParameter: {
  780. #ifndef BUILD_BRIDGE
  781. // Control backend stuff
  782. if (event.channel == pData->ctrlChannel)
  783. {
  784. float value;
  785. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  786. {
  787. value = ctrlEvent.value;
  788. setDryWetRT(value);
  789. }
  790. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  791. {
  792. value = ctrlEvent.value*127.0f/100.0f;
  793. setVolumeRT(value);
  794. }
  795. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  796. {
  797. float left, right;
  798. value = ctrlEvent.value/0.5f - 1.0f;
  799. if (value < 0.0f)
  800. {
  801. left = -1.0f;
  802. right = (value*2.0f)+1.0f;
  803. }
  804. else if (value > 0.0f)
  805. {
  806. left = (value*2.0f)-1.0f;
  807. right = 1.0f;
  808. }
  809. else
  810. {
  811. left = -1.0f;
  812. right = 1.0f;
  813. }
  814. setBalanceLeftRT(left);
  815. setBalanceRightRT(right);
  816. }
  817. }
  818. #endif
  819. // Control plugin parameters
  820. for (uint32_t k=0; k < pData->param.count; ++k)
  821. {
  822. if (pData->param.data[k].midiChannel != event.channel)
  823. continue;
  824. if (pData->param.data[k].midiCC != ctrlEvent.param)
  825. continue;
  826. if (pData->param.data[k].type != PARAMETER_INPUT)
  827. continue;
  828. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  829. continue;
  830. float value;
  831. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  832. {
  833. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  834. }
  835. else
  836. {
  837. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  838. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  839. else
  840. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  841. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  842. value = std::rint(value);
  843. }
  844. setParameterValueRT(k, value);
  845. }
  846. break;
  847. } // case kEngineControlEventTypeParameter
  848. case kEngineControlEventTypeMidiBank:
  849. case kEngineControlEventTypeMidiProgram:
  850. case kEngineControlEventTypeAllSoundOff:
  851. case kEngineControlEventTypeAllNotesOff:
  852. break;
  853. } // switch (ctrlEvent.type)
  854. break;
  855. } // case kEngineEventTypeControl
  856. case kEngineEventTypeMidi:
  857. break;
  858. } // switch (event.type)
  859. }
  860. pData->postRtEvents.trySplice();
  861. if (frames > timeOffset)
  862. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  863. } // End of Event Input and Processing
  864. // --------------------------------------------------------------------------------------------------------
  865. // Plugin processing (no events)
  866. else
  867. {
  868. processSingle(audioIn, audioOut, frames, 0);
  869. } // End of Plugin processing (no events)
  870. // --------------------------------------------------------------------------------------------------------
  871. // Control Output
  872. if (pData->event.portOut != nullptr)
  873. {
  874. uint8_t channel;
  875. uint16_t param;
  876. float value;
  877. for (uint32_t k=0; k < pData->param.count; ++k)
  878. {
  879. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  880. continue;
  881. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  882. if (pData->param.data[k].midiCC > 0)
  883. {
  884. channel = pData->param.data[k].midiChannel;
  885. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  886. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  887. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  888. }
  889. }
  890. } // End of Control Output
  891. }
  892. bool processSingle(const float** const audioIn, float** const audioOut, const uint32_t frames,
  893. const uint32_t timeOffset)
  894. {
  895. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  896. if (pData->audioIn.count > 0)
  897. {
  898. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  899. }
  900. if (pData->audioOut.count > 0)
  901. {
  902. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  903. }
  904. // --------------------------------------------------------------------------------------------------------
  905. // Try lock, silence otherwise
  906. #ifndef STOAT_TEST_BUILD
  907. if (pData->engine->isOffline())
  908. {
  909. pData->singleMutex.lock();
  910. }
  911. else
  912. #endif
  913. if (! pData->singleMutex.tryLock())
  914. {
  915. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  916. {
  917. for (uint32_t k=0; k < frames; ++k)
  918. audioOut[i][k+timeOffset] = 0.0f;
  919. }
  920. return false;
  921. }
  922. // --------------------------------------------------------------------------------------------------------
  923. // Set audio buffers
  924. const bool customMonoOut = pData->audioOut.count == 2 && fForcedStereoOut && ! fForcedStereoIn;
  925. const bool customStereoOut = pData->audioOut.count == 2 && fForcedStereoIn && ! fForcedStereoOut;
  926. if (! customMonoOut)
  927. {
  928. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  929. carla_zeroFloats(fAudioOutBuffers[i], frames);
  930. }
  931. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  932. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  933. // --------------------------------------------------------------------------------------------------------
  934. // Run plugin
  935. uint instn = 0;
  936. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next(), ++instn)
  937. {
  938. LADSPA_Handle const handle(it.getValue(nullptr));
  939. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  940. // ----------------------------------------------------------------------------------------------------
  941. // Mixdown for forced stereo
  942. if (customMonoOut)
  943. carla_zeroFloats(fAudioOutBuffers[instn], frames);
  944. // ----------------------------------------------------------------------------------------------------
  945. // Run it
  946. try {
  947. fDescriptor->run(handle, frames);
  948. } CARLA_SAFE_EXCEPTION("LADSPA run");
  949. // ----------------------------------------------------------------------------------------------------
  950. // Mixdown for forced stereo
  951. if (customMonoOut)
  952. carla_multiply(fAudioOutBuffers[instn], 0.5f, frames);
  953. else if (customStereoOut)
  954. carla_copyFloats(fExtraStereoBuffer[instn], fAudioOutBuffers[instn], frames);
  955. }
  956. if (customStereoOut)
  957. {
  958. carla_copyFloats(fAudioOutBuffers[0], fExtraStereoBuffer[0], frames);
  959. carla_copyFloats(fAudioOutBuffers[1], fExtraStereoBuffer[1], frames);
  960. }
  961. #ifndef BUILD_BRIDGE
  962. // --------------------------------------------------------------------------------------------------------
  963. // Post-processing (dry/wet, volume and balance)
  964. {
  965. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  966. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  967. const bool isMono = (pData->audioIn.count == 1);
  968. bool isPair;
  969. float bufValue, oldBufLeft[doBalance ? frames : 1];
  970. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  971. {
  972. // Dry/Wet
  973. if (doDryWet)
  974. {
  975. const uint32_t c = isMono ? 0 : i;
  976. for (uint32_t k=0; k < frames; ++k)
  977. {
  978. if (k < pData->latency.frames)
  979. bufValue = pData->latency.buffers[c][k];
  980. else if (pData->latency.frames < frames)
  981. bufValue = fAudioInBuffers[c][k-pData->latency.frames];
  982. else
  983. bufValue = fAudioInBuffers[c][k];
  984. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  985. }
  986. }
  987. // Balance
  988. if (doBalance)
  989. {
  990. isPair = (i % 2 == 0);
  991. if (isPair)
  992. {
  993. CARLA_ASSERT(i+1 < pData->audioOut.count);
  994. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  995. }
  996. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  997. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  998. for (uint32_t k=0; k < frames; ++k)
  999. {
  1000. if (isPair)
  1001. {
  1002. // left
  1003. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1004. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1005. }
  1006. else
  1007. {
  1008. // right
  1009. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1010. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1011. }
  1012. }
  1013. }
  1014. // Volume (and buffer copy)
  1015. {
  1016. for (uint32_t k=0; k < frames; ++k)
  1017. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1018. }
  1019. }
  1020. } // End of Post-processing
  1021. // --------------------------------------------------------------------------------------------------------
  1022. // Save latency values for next callback
  1023. if (const uint32_t latframes = pData->latency.frames)
  1024. {
  1025. CARLA_SAFE_ASSERT(timeOffset == 0);
  1026. if (latframes <= frames)
  1027. {
  1028. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1029. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  1030. }
  1031. else
  1032. {
  1033. const uint32_t diff = pData->latency.frames-frames;
  1034. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1035. {
  1036. // push back buffer by 'frames'
  1037. for (k=0; k < diff; ++k)
  1038. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1039. // put current input at the end
  1040. for (uint32_t j=0; k < latframes; ++j, ++k)
  1041. pData->latency.buffers[i][k] = audioIn[i][j];
  1042. }
  1043. }
  1044. }
  1045. #else // BUILD_BRIDGE
  1046. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1047. {
  1048. for (uint32_t k=0; k < frames; ++k)
  1049. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1050. }
  1051. #endif
  1052. // --------------------------------------------------------------------------------------------------------
  1053. pData->singleMutex.unlock();
  1054. return true;
  1055. }
  1056. void bufferSizeChanged(const uint32_t newBufferSize) override
  1057. {
  1058. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1059. carla_debug("CarlaPluginLADSPA::bufferSizeChanged(%i) - start", newBufferSize);
  1060. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1061. {
  1062. if (fAudioInBuffers[i] != nullptr)
  1063. delete[] fAudioInBuffers[i];
  1064. fAudioInBuffers[i] = new float[newBufferSize];
  1065. carla_zeroFloats(fAudioInBuffers[i], newBufferSize);
  1066. }
  1067. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1068. {
  1069. if (fAudioOutBuffers[i] != nullptr)
  1070. delete[] fAudioOutBuffers[i];
  1071. fAudioOutBuffers[i] = new float[newBufferSize];
  1072. carla_zeroFloats(fAudioOutBuffers[i], newBufferSize);
  1073. }
  1074. if (fExtraStereoBuffer[0] != nullptr)
  1075. {
  1076. delete[] fExtraStereoBuffer[0];
  1077. fExtraStereoBuffer[0] = nullptr;
  1078. }
  1079. if (fExtraStereoBuffer[1] != nullptr)
  1080. {
  1081. delete[] fExtraStereoBuffer[1];
  1082. fExtraStereoBuffer[1] = nullptr;
  1083. }
  1084. if (fForcedStereoIn && pData->audioOut.count == 2)
  1085. {
  1086. fExtraStereoBuffer[0] = new float[newBufferSize];
  1087. fExtraStereoBuffer[1] = new float[newBufferSize];
  1088. carla_zeroFloats(fExtraStereoBuffer[0], newBufferSize);
  1089. carla_zeroFloats(fExtraStereoBuffer[1], newBufferSize);
  1090. }
  1091. reconnectAudioPorts();
  1092. carla_debug("CarlaPluginLADSPA::bufferSizeChanged(%i) - end", newBufferSize);
  1093. }
  1094. void sampleRateChanged(const double newSampleRate) override
  1095. {
  1096. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1097. carla_stdout("CarlaPluginLADSPA::sampleRateChanged(%g) - start", newSampleRate);
  1098. if (pData->active)
  1099. deactivate();
  1100. const std::size_t instanceCount(fHandles.count());
  1101. if (fDescriptor->cleanup != nullptr)
  1102. {
  1103. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1104. {
  1105. LADSPA_Handle const handle(it.getValue(nullptr));
  1106. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1107. try {
  1108. fDescriptor->cleanup(handle);
  1109. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  1110. }
  1111. }
  1112. fHandles.clear();
  1113. for (std::size_t i=0; i<instanceCount; ++i)
  1114. addInstance();
  1115. reconnectAudioPorts();
  1116. if (pData->active)
  1117. activate();
  1118. carla_stdout("CarlaPluginLADSPA::sampleRateChanged(%g) - end", newSampleRate);
  1119. }
  1120. void reconnectAudioPorts() const noexcept
  1121. {
  1122. if (fForcedStereoIn)
  1123. {
  1124. if (LADSPA_Handle const handle = fHandles.getFirst(nullptr))
  1125. {
  1126. try {
  1127. fDescriptor->connect_port(handle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1128. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo input, first)");
  1129. }
  1130. if (LADSPA_Handle const handle = fHandles.getLast(nullptr))
  1131. {
  1132. try {
  1133. fDescriptor->connect_port(handle, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1134. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo input, last)");
  1135. }
  1136. }
  1137. else
  1138. {
  1139. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1140. {
  1141. LADSPA_Handle const handle(it.getValue(nullptr));
  1142. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1143. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1144. {
  1145. try {
  1146. fDescriptor->connect_port(handle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1147. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (audio input)");
  1148. }
  1149. }
  1150. }
  1151. if (fForcedStereoOut)
  1152. {
  1153. if (LADSPA_Handle const handle = fHandles.getFirst(nullptr))
  1154. {
  1155. try {
  1156. fDescriptor->connect_port(handle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1157. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo output, first)");
  1158. }
  1159. if (LADSPA_Handle const handle = fHandles.getLast(nullptr))
  1160. {
  1161. try {
  1162. fDescriptor->connect_port(handle, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1163. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (forced stereo output, last)");
  1164. }
  1165. }
  1166. else
  1167. {
  1168. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1169. {
  1170. LADSPA_Handle const handle(it.getValue(nullptr));
  1171. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1172. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1173. {
  1174. try {
  1175. fDescriptor->connect_port(handle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1176. } CARLA_SAFE_EXCEPTION("LADSPA connect_port (audio output)");
  1177. }
  1178. }
  1179. }
  1180. }
  1181. // -------------------------------------------------------------------
  1182. // Plugin buffers
  1183. void clearBuffers() noexcept override
  1184. {
  1185. carla_debug("CarlaPluginLADSPA::clearBuffers() - start");
  1186. if (fAudioInBuffers != nullptr)
  1187. {
  1188. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1189. {
  1190. if (fAudioInBuffers[i] != nullptr)
  1191. {
  1192. delete[] fAudioInBuffers[i];
  1193. fAudioInBuffers[i] = nullptr;
  1194. }
  1195. }
  1196. delete[] fAudioInBuffers;
  1197. fAudioInBuffers = nullptr;
  1198. }
  1199. if (fAudioOutBuffers != nullptr)
  1200. {
  1201. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1202. {
  1203. if (fAudioOutBuffers[i] != nullptr)
  1204. {
  1205. delete[] fAudioOutBuffers[i];
  1206. fAudioOutBuffers[i] = nullptr;
  1207. }
  1208. }
  1209. delete[] fAudioOutBuffers;
  1210. fAudioOutBuffers = nullptr;
  1211. }
  1212. if (fExtraStereoBuffer[0] != nullptr)
  1213. {
  1214. delete[] fExtraStereoBuffer[0];
  1215. fExtraStereoBuffer[0] = nullptr;
  1216. }
  1217. if (fExtraStereoBuffer[1] != nullptr)
  1218. {
  1219. delete[] fExtraStereoBuffer[1];
  1220. fExtraStereoBuffer[1] = nullptr;
  1221. }
  1222. if (fParamBuffers != nullptr)
  1223. {
  1224. delete[] fParamBuffers;
  1225. fParamBuffers = nullptr;
  1226. }
  1227. CarlaPlugin::clearBuffers();
  1228. carla_debug("CarlaPluginLADSPA::clearBuffers() - end");
  1229. }
  1230. // -------------------------------------------------------------------
  1231. const void* getNativeDescriptor() const noexcept override
  1232. {
  1233. return fDescriptor;
  1234. }
  1235. const void* getExtraStuff() const noexcept override
  1236. {
  1237. return fRdfDescriptor;
  1238. }
  1239. // -------------------------------------------------------------------
  1240. bool init(const char* const filename, const char* name, const char* const label, const uint options,
  1241. const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1242. {
  1243. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1244. // ---------------------------------------------------------------
  1245. // first checks
  1246. if (pData->client != nullptr)
  1247. {
  1248. pData->engine->setLastError("Plugin client is already registered");
  1249. return false;
  1250. }
  1251. if (filename == nullptr || filename[0] == '\0')
  1252. {
  1253. pData->engine->setLastError("null filename");
  1254. return false;
  1255. }
  1256. // ---------------------------------------------------------------
  1257. // open DLL
  1258. if (! pData->libOpen(filename))
  1259. {
  1260. pData->engine->setLastError(pData->libError(filename));
  1261. return false;
  1262. }
  1263. // ---------------------------------------------------------------
  1264. // get DLL main entry
  1265. const LADSPA_Descriptor_Function descFn = pData->libSymbol<LADSPA_Descriptor_Function>("ladspa_descriptor");
  1266. if (descFn == nullptr)
  1267. {
  1268. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1269. return false;
  1270. }
  1271. // ---------------------------------------------------------------
  1272. // get descriptor that matches label
  1273. // if label is null, get first valid plugin
  1274. const bool nullLabel = (label == nullptr || label[0] == '\0');
  1275. for (ulong d=0;; ++d)
  1276. {
  1277. try {
  1278. fDescriptor = descFn(d);
  1279. }
  1280. catch(...) {
  1281. carla_stderr2("Caught exception when trying to get LADSPA descriptor");
  1282. fDescriptor = nullptr;
  1283. break;
  1284. }
  1285. if (fDescriptor == nullptr)
  1286. break;
  1287. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  1288. {
  1289. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  1290. fDescriptor = nullptr;
  1291. break;
  1292. }
  1293. if (fDescriptor->run == nullptr)
  1294. {
  1295. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  1296. fDescriptor = nullptr;
  1297. break;
  1298. }
  1299. if (nullLabel || std::strcmp(fDescriptor->Label, label) == 0)
  1300. break;
  1301. }
  1302. if (fDescriptor == nullptr)
  1303. {
  1304. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1305. return false;
  1306. }
  1307. // ---------------------------------------------------------------
  1308. // check for fixed buffer size requirement
  1309. fNeedsFixedBuffers = CarlaString(filename).contains("dssi-vst", true);
  1310. if (fNeedsFixedBuffers && ! pData->engine->usesConstantBufferSize())
  1311. {
  1312. pData->engine->setLastError("Cannot use this plugin under the current engine.\n"
  1313. "The plugin requires a fixed block size which is not possible right now.");
  1314. return false;
  1315. }
  1316. // ---------------------------------------------------------------
  1317. // get info
  1318. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1319. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1320. if (name == nullptr || name[0] == '\0')
  1321. {
  1322. /**/ if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  1323. name = fRdfDescriptor->Title;
  1324. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1325. name = fDescriptor->Name;
  1326. else
  1327. name = fDescriptor->Label;
  1328. }
  1329. pData->name = pData->engine->getUniquePluginName(name);
  1330. pData->filename = carla_strdup(filename);
  1331. // ---------------------------------------------------------------
  1332. // register client
  1333. pData->client = pData->engine->addClient(this);
  1334. if (pData->client == nullptr || ! pData->client->isOk())
  1335. {
  1336. pData->engine->setLastError("Failed to register plugin client");
  1337. return false;
  1338. }
  1339. // ---------------------------------------------------------------
  1340. // initialize plugin
  1341. if (! addInstance())
  1342. return false;
  1343. // ---------------------------------------------------------------
  1344. // find latency port index
  1345. for (uint32_t i=0, iCtrl=0, count=getSafePortCount(); i<count; ++i)
  1346. {
  1347. const int portType(fDescriptor->PortDescriptors[i]);
  1348. if (! LADSPA_IS_PORT_CONTROL(portType))
  1349. continue;
  1350. const uint32_t index(iCtrl++);
  1351. if (! LADSPA_IS_PORT_OUTPUT(portType))
  1352. continue;
  1353. const char* const portName(fDescriptor->PortNames[i]);
  1354. CARLA_SAFE_ASSERT_BREAK(portName != nullptr);
  1355. if (std::strcmp(portName, "latency") == 0 ||
  1356. std::strcmp(portName, "_latency") == 0)
  1357. {
  1358. fLatencyIndex = static_cast<int32_t>(index);
  1359. break;
  1360. }
  1361. }
  1362. // ---------------------------------------------------------------
  1363. // set default options
  1364. pData->options = 0x0;
  1365. /**/ if (fLatencyIndex >= 0 || fNeedsFixedBuffers)
  1366. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1367. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1368. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1369. /**/ if (pData->engine->getOptions().forceStereo)
  1370. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1371. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  1372. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1373. return true;
  1374. }
  1375. // -------------------------------------------------------------------
  1376. private:
  1377. LinkedList<LADSPA_Handle> fHandles;
  1378. const LADSPA_Descriptor* fDescriptor;
  1379. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1380. float** fAudioInBuffers;
  1381. float** fAudioOutBuffers;
  1382. float* fExtraStereoBuffer[2]; // used only if forcedStereoIn and audioOut == 2
  1383. float* fParamBuffers;
  1384. int32_t fLatencyIndex; // -1 if invalid
  1385. bool fForcedStereoIn;
  1386. bool fForcedStereoOut;
  1387. bool fNeedsFixedBuffers;
  1388. // -------------------------------------------------------------------
  1389. bool addInstance()
  1390. {
  1391. LADSPA_Handle handle;
  1392. try {
  1393. handle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  1394. } CARLA_SAFE_EXCEPTION_RETURN_ERR("LADSPA instantiate", "Plugin failed to initialize");
  1395. for (uint32_t i=0, count=pData->param.count; i<count; ++i)
  1396. {
  1397. const int32_t rindex(pData->param.data[i].rindex);
  1398. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0);
  1399. try {
  1400. fDescriptor->connect_port(handle, static_cast<ulong>(rindex), &fParamBuffers[i]);
  1401. } CARLA_SAFE_EXCEPTION("LADSPA connect_port");
  1402. }
  1403. if (fHandles.append(handle))
  1404. return true;
  1405. try {
  1406. fDescriptor->cleanup(handle);
  1407. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  1408. pData->engine->setLastError("Out of memory");
  1409. return false;
  1410. }
  1411. uint32_t getSafePortCount() const noexcept
  1412. {
  1413. if (fDescriptor->PortCount == 0)
  1414. return 0;
  1415. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  1416. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  1417. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  1418. return static_cast<uint32_t>(fDescriptor->PortCount);
  1419. }
  1420. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  1421. {
  1422. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  1423. return true;
  1424. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  1425. return true;
  1426. return false;
  1427. }
  1428. static bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf,
  1429. const bool wantName, const bool useBracket) noexcept
  1430. {
  1431. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  1432. if (sepBracketStart == nullptr)
  1433. return false;
  1434. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  1435. if (sepBracketEnd == nullptr)
  1436. return false;
  1437. const std::size_t unitSize(static_cast<std::size_t>(sepBracketEnd-sepBracketStart-2));
  1438. if (unitSize > 7) // very unlikely to have such big unit
  1439. return false;
  1440. const std::size_t sepIndex(std::strlen(paramName)-unitSize-3);
  1441. // just in case
  1442. if (sepIndex+2 >= STR_MAX)
  1443. return false;
  1444. if (wantName)
  1445. {
  1446. std::strncpy(strBuf, paramName, sepIndex);
  1447. strBuf[sepIndex] = '\0';
  1448. }
  1449. else
  1450. {
  1451. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  1452. strBuf[unitSize] = '\0';
  1453. }
  1454. return true;
  1455. }
  1456. // -------------------------------------------------------------------
  1457. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginLADSPA)
  1458. };
  1459. // -------------------------------------------------------------------------------------------------------------------
  1460. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1461. {
  1462. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\", " P_INT64 ", %x}, %p)",
  1463. init.engine, init.filename, init.name, init.label, init.uniqueId, init.options, rdfDescriptor);
  1464. CarlaPluginLADSPA* const plugin(new CarlaPluginLADSPA(init.engine, init.id));
  1465. if (! plugin->init(init.filename, init.name, init.label, init.options, rdfDescriptor))
  1466. {
  1467. delete plugin;
  1468. return nullptr;
  1469. }
  1470. return plugin;
  1471. }
  1472. // -------------------------------------------------------------------------------------------------------------------
  1473. CARLA_BACKEND_END_NAMESPACE