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 66KB

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