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.

LadspaPlugin.cpp 49KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * Carla LADSPA Plugin
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_LADSPA
  19. #include "CarlaLadspaUtils.hpp"
  20. CARLA_BACKEND_START_NAMESPACE
  21. class LadspaPlugin : public CarlaPlugin
  22. {
  23. public:
  24. LadspaPlugin(CarlaEngine* const engine, const unsigned int id)
  25. : CarlaPlugin(engine, id),
  26. fHandle(nullptr),
  27. fHandle2(nullptr),
  28. fDescriptor(nullptr),
  29. fRdfDescriptor(nullptr),
  30. fAudioInBuffers(nullptr),
  31. fAudioOutBuffers(nullptr),
  32. fParamBuffers(nullptr)
  33. {
  34. carla_debug("LadspaPlugin::LadspaPlugin(%p, %i)", engine, id);
  35. }
  36. ~LadspaPlugin()
  37. {
  38. carla_debug("LadspaPlugin::~LadspaPlugin()");
  39. kData->singleMutex.lock();
  40. kData->masterMutex.lock();
  41. if (fDescriptor != nullptr)
  42. {
  43. if (fDescriptor->deactivate != nullptr && kData->activeBefore)
  44. {
  45. if (fHandle != nullptr)
  46. fDescriptor->deactivate(fHandle);
  47. if (fHandle2 != nullptr)
  48. fDescriptor->deactivate(fHandle2);
  49. }
  50. if (fDescriptor->cleanup != nullptr)
  51. {
  52. if (fHandle != nullptr)
  53. fDescriptor->cleanup(fHandle);
  54. if (fHandle2 != nullptr)
  55. fDescriptor->cleanup(fHandle2);
  56. }
  57. fHandle = nullptr;
  58. fHandle2 = nullptr;
  59. fDescriptor = nullptr;
  60. }
  61. if (fRdfDescriptor != nullptr)
  62. {
  63. delete fRdfDescriptor;
  64. fRdfDescriptor = nullptr;
  65. }
  66. deleteBuffers();
  67. }
  68. // -------------------------------------------------------------------
  69. // Information (base)
  70. PluginType type() const
  71. {
  72. return PLUGIN_LADSPA;
  73. }
  74. PluginCategory category()
  75. {
  76. if (fRdfDescriptor != nullptr)
  77. {
  78. const LADSPA_PluginType category = fRdfDescriptor->Type;
  79. // Specific Types
  80. if (category & (LADSPA_PLUGIN_DELAY|LADSPA_PLUGIN_REVERB))
  81. return PLUGIN_CATEGORY_DELAY;
  82. if (category & (LADSPA_PLUGIN_PHASER|LADSPA_PLUGIN_FLANGER|LADSPA_PLUGIN_CHORUS))
  83. return PLUGIN_CATEGORY_MODULATOR;
  84. if (category & (LADSPA_PLUGIN_AMPLIFIER))
  85. return PLUGIN_CATEGORY_DYNAMICS;
  86. if (category & (LADSPA_PLUGIN_UTILITY|LADSPA_PLUGIN_SPECTRAL|LADSPA_PLUGIN_FREQUENCY_METER))
  87. return PLUGIN_CATEGORY_UTILITY;
  88. // Pre-set LADSPA Types
  89. if (LADSPA_IS_PLUGIN_DYNAMICS(category))
  90. return PLUGIN_CATEGORY_DYNAMICS;
  91. if (LADSPA_IS_PLUGIN_AMPLITUDE(category))
  92. return PLUGIN_CATEGORY_MODULATOR;
  93. if (LADSPA_IS_PLUGIN_EQ(category))
  94. return PLUGIN_CATEGORY_EQ;
  95. if (LADSPA_IS_PLUGIN_FILTER(category))
  96. return PLUGIN_CATEGORY_FILTER;
  97. if (LADSPA_IS_PLUGIN_FREQUENCY(category))
  98. return PLUGIN_CATEGORY_UTILITY;
  99. if (LADSPA_IS_PLUGIN_SIMULATOR(category))
  100. return PLUGIN_CATEGORY_OTHER;
  101. if (LADSPA_IS_PLUGIN_TIME(category))
  102. return PLUGIN_CATEGORY_DELAY;
  103. if (LADSPA_IS_PLUGIN_GENERATOR(category))
  104. return PLUGIN_CATEGORY_SYNTH;
  105. }
  106. return getPluginCategoryFromName(fName);
  107. }
  108. long uniqueId() const
  109. {
  110. CARLA_ASSERT(fDescriptor != nullptr);
  111. return (fDescriptor != nullptr) ? static_cast<long>(fDescriptor->UniqueID) : 0;
  112. }
  113. // -------------------------------------------------------------------
  114. // Information (count)
  115. uint32_t parameterScalePointCount(const uint32_t parameterId) const
  116. {
  117. CARLA_ASSERT(parameterId < kData->param.count);
  118. const int32_t rindex = kData->param.data[parameterId].rindex;
  119. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  120. {
  121. const LADSPA_RDF_Port& port = fRdfDescriptor->Ports[rindex];
  122. return static_cast<uint32_t>(port.ScalePointCount);
  123. }
  124. return 0;
  125. }
  126. // -------------------------------------------------------------------
  127. // Information (per-plugin data)
  128. unsigned int availableOptions()
  129. {
  130. #ifdef __USE_GNU
  131. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  132. #else
  133. const bool isDssiVst = fFilename.contains("dssi-vst");
  134. #endif
  135. unsigned int options = 0x0;
  136. if (! isDssiVst)
  137. options |= PLUGIN_OPTION_FIXED_BUFFER;
  138. //if ((kData->audioIns.count() == 1 || kData->audioOuts.count() == 0) || (kData->audioIns.count() == 0 || kData->audioOuts.count() == 1))
  139. // options |= PLUGIN_OPTION_FORCE_STEREO;
  140. return options;
  141. }
  142. float getParameterValue(const uint32_t parameterId)
  143. {
  144. CARLA_ASSERT(parameterId < kData->param.count);
  145. return fParamBuffers[parameterId];
  146. }
  147. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  148. {
  149. CARLA_ASSERT(fRdfDescriptor != nullptr);
  150. CARLA_ASSERT(parameterId < kData->param.count);
  151. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  152. const int32_t rindex = kData->param.data[parameterId].rindex;
  153. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  154. {
  155. const LADSPA_RDF_Port& port = fRdfDescriptor->Ports[rindex];
  156. if (scalePointId < port.ScalePointCount)
  157. {
  158. const LADSPA_RDF_ScalePoint& scalePoint = port.ScalePoints[scalePointId];
  159. return scalePoint.Value;
  160. }
  161. }
  162. return 0.0f;
  163. }
  164. void getLabel(char* const strBuf)
  165. {
  166. CARLA_ASSERT(fDescriptor != nullptr);
  167. if (fDescriptor != nullptr && fDescriptor->Label != nullptr)
  168. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  169. else
  170. CarlaPlugin::getLabel(strBuf);
  171. }
  172. void getMaker(char* const strBuf)
  173. {
  174. CARLA_ASSERT(fDescriptor != nullptr);
  175. if (fRdfDescriptor != nullptr && fRdfDescriptor->Creator != nullptr)
  176. std::strncpy(strBuf, fRdfDescriptor->Creator, STR_MAX);
  177. else if (fDescriptor != nullptr && fDescriptor->Maker != nullptr)
  178. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  179. else
  180. CarlaPlugin::getMaker(strBuf);
  181. }
  182. void getCopyright(char* const strBuf)
  183. {
  184. CARLA_ASSERT(fDescriptor != nullptr);
  185. if (fDescriptor != nullptr && fDescriptor->Copyright != nullptr)
  186. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  187. else
  188. CarlaPlugin::getCopyright(strBuf);
  189. }
  190. void getRealName(char* const strBuf)
  191. {
  192. CARLA_ASSERT(fDescriptor != nullptr);
  193. if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  194. std::strncpy(strBuf, fRdfDescriptor->Title, STR_MAX);
  195. else if (fDescriptor != nullptr && fDescriptor->Name != nullptr)
  196. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  197. else
  198. CarlaPlugin::getRealName(strBuf);
  199. }
  200. void getParameterName(const uint32_t parameterId, char* const strBuf)
  201. {
  202. CARLA_ASSERT(fDescriptor != nullptr);
  203. CARLA_ASSERT(parameterId < kData->param.count);
  204. const int32_t rindex = kData->param.data[parameterId].rindex;
  205. if (fDescriptor != nullptr && rindex < static_cast<int32_t>(fDescriptor->PortCount))
  206. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  207. else
  208. CarlaPlugin::getParameterName(parameterId, strBuf);
  209. }
  210. void getParameterSymbol(const uint32_t parameterId, char* const strBuf)
  211. {
  212. CARLA_ASSERT(parameterId < kData->param.count);
  213. const int32_t rindex = kData->param.data[parameterId].rindex;
  214. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  215. {
  216. const LADSPA_RDF_Port& port = fRdfDescriptor->Ports[rindex];
  217. if (LADSPA_PORT_HAS_LABEL(port.Hints) && port.Label != nullptr)
  218. {
  219. std::strncpy(strBuf, port.Label, STR_MAX);
  220. return;
  221. }
  222. }
  223. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  224. }
  225. void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  226. {
  227. CARLA_ASSERT(parameterId < kData->param.count);
  228. const int32_t rindex = kData->param.data[parameterId].rindex;
  229. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  230. {
  231. const LADSPA_RDF_Port& port = fRdfDescriptor->Ports[rindex];
  232. if (LADSPA_PORT_HAS_UNIT(port.Hints))
  233. {
  234. switch (port.Unit)
  235. {
  236. case LADSPA_UNIT_DB:
  237. std::strncpy(strBuf, "dB", STR_MAX);
  238. return;
  239. case LADSPA_UNIT_COEF:
  240. std::strncpy(strBuf, "(coef)", STR_MAX);
  241. return;
  242. case LADSPA_UNIT_HZ:
  243. std::strncpy(strBuf, "Hz", STR_MAX);
  244. return;
  245. case LADSPA_UNIT_S:
  246. std::strncpy(strBuf, "s", STR_MAX);
  247. return;
  248. case LADSPA_UNIT_MS:
  249. std::strncpy(strBuf, "ms", STR_MAX);
  250. return;
  251. case LADSPA_UNIT_MIN:
  252. std::strncpy(strBuf, "min", STR_MAX);
  253. return;
  254. }
  255. }
  256. }
  257. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  258. }
  259. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  260. {
  261. CARLA_ASSERT(fRdfDescriptor != nullptr);
  262. CARLA_ASSERT(parameterId < kData->param.count);
  263. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  264. const int32_t rindex = kData->param.data[parameterId].rindex;
  265. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  266. {
  267. const LADSPA_RDF_Port& port = fRdfDescriptor->Ports[rindex];
  268. if (scalePointId < port.ScalePointCount)
  269. {
  270. const LADSPA_RDF_ScalePoint& scalePoint = port.ScalePoints[scalePointId];
  271. if (scalePoint.Label != nullptr)
  272. {
  273. std::strncpy(strBuf, scalePoint.Label, STR_MAX);
  274. return;
  275. }
  276. }
  277. }
  278. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  279. }
  280. // -------------------------------------------------------------------
  281. // Set data (plugin-specific stuff)
  282. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  283. {
  284. CARLA_ASSERT(parameterId < kData->param.count);
  285. const float fixedValue = kData->param.fixValue(parameterId, value);
  286. fParamBuffers[parameterId] = fixedValue;
  287. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  288. }
  289. // -------------------------------------------------------------------
  290. // Plugin state
  291. void reload()
  292. {
  293. carla_debug("LadspaPlugin::reload() - start");
  294. CARLA_ASSERT(kData->engine != nullptr);
  295. CARLA_ASSERT(fDescriptor != nullptr);
  296. CARLA_ASSERT(fHandle != nullptr);
  297. const ProcessMode processMode(kData->engine->getProccessMode());
  298. // Safely disable plugin for reload
  299. const ScopedDisabler sd(this);
  300. deleteBuffers();
  301. const float sampleRate = (float)kData->engine->getSampleRate();
  302. const uint32_t portCount = static_cast<uint32_t>(fDescriptor->PortCount);
  303. uint32_t aIns, aOuts, params, j;
  304. aIns = aOuts = params = 0;
  305. bool forcedStereoIn, forcedStereoOut;
  306. forcedStereoIn = forcedStereoOut = false;
  307. bool needsCtrlIn, needsCtrlOut;
  308. needsCtrlIn = needsCtrlOut = false;
  309. if (portCount > 0)
  310. {
  311. CARLA_ASSERT(fDescriptor->PortDescriptors != nullptr);
  312. CARLA_ASSERT(fDescriptor->PortRangeHints != nullptr);
  313. CARLA_ASSERT(fDescriptor->PortNames != nullptr);
  314. for (uint32_t i=0; i < portCount; i++)
  315. {
  316. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  317. if (LADSPA_IS_PORT_AUDIO(portType))
  318. {
  319. if (LADSPA_IS_PORT_INPUT(portType))
  320. aIns += 1;
  321. else if (LADSPA_IS_PORT_OUTPUT(portType))
  322. aOuts += 1;
  323. }
  324. else if (LADSPA_IS_PORT_CONTROL(portType))
  325. params += 1;
  326. }
  327. }
  328. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  329. {
  330. if (fHandle2 == nullptr)
  331. fHandle2 = fDescriptor->instantiate(fDescriptor, (unsigned long)sampleRate);
  332. if (aIns == 1)
  333. {
  334. aIns = 2;
  335. forcedStereoIn = true;
  336. }
  337. if (aOuts == 1)
  338. {
  339. aOuts = 2;
  340. forcedStereoOut = true;
  341. }
  342. }
  343. if (aIns > 0)
  344. {
  345. kData->audioIn.createNew(aIns);
  346. fAudioInBuffers = new float*[aIns];
  347. for (uint32_t i=0; i < aIns; i++)
  348. fAudioInBuffers[i] = nullptr;
  349. }
  350. if (aOuts > 0)
  351. {
  352. kData->audioOut.createNew(aOuts);
  353. fAudioOutBuffers = new float*[aOuts];
  354. needsCtrlIn = true;
  355. for (uint32_t i=0; i < aOuts; i++)
  356. fAudioOutBuffers[i] = nullptr;
  357. }
  358. if (params > 0)
  359. {
  360. kData->param.createNew(params);
  361. fParamBuffers = new float[params];
  362. for (uint32_t i=0; i < params; i++)
  363. fParamBuffers[i] = 0.0f;
  364. }
  365. const uint portNameSize = kData->engine->maxPortNameSize();
  366. CarlaString portName;
  367. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; i++)
  368. {
  369. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  370. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  371. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  372. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  373. if (LADSPA_IS_PORT_AUDIO(portType))
  374. {
  375. portName.clear();
  376. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  377. {
  378. portName = fName;
  379. portName += ":";
  380. }
  381. portName += fDescriptor->PortNames[i];
  382. portName.truncate(portNameSize);
  383. if (LADSPA_IS_PORT_INPUT(portType))
  384. {
  385. j = iAudioIn++;
  386. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  387. kData->audioIn.ports[j].rindex = i;
  388. if (forcedStereoIn)
  389. {
  390. portName += "_2";
  391. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  392. kData->audioIn.ports[1].rindex = i;
  393. }
  394. }
  395. else if (LADSPA_IS_PORT_OUTPUT(portType))
  396. {
  397. j = iAudioOut++;
  398. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  399. kData->audioOut.ports[j].rindex = i;
  400. if (forcedStereoOut)
  401. {
  402. portName += "_2";
  403. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  404. kData->audioOut.ports[1].rindex = i;
  405. }
  406. }
  407. else
  408. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  409. }
  410. else if (LADSPA_IS_PORT_CONTROL(portType))
  411. {
  412. j = iCtrl++;
  413. kData->param.data[j].index = j;
  414. kData->param.data[j].rindex = i;
  415. kData->param.data[j].hints = 0x0;
  416. kData->param.data[j].midiChannel = 0;
  417. kData->param.data[j].midiCC = -1;
  418. float min, max, def, step, stepSmall, stepLarge;
  419. // min value
  420. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  421. min = portRangeHints.LowerBound;
  422. else
  423. min = 0.0f;
  424. // max value
  425. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  426. max = portRangeHints.UpperBound;
  427. else
  428. max = 1.0f;
  429. if (min > max)
  430. max = min;
  431. else if (max < min)
  432. min = max;
  433. if (max - min == 0.0f)
  434. {
  435. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  436. max = min + 0.1f;
  437. }
  438. // default value
  439. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  440. def = fRdfDescriptor->Ports[i].Default;
  441. else
  442. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  443. if (def < min)
  444. def = min;
  445. else if (def > max)
  446. def = max;
  447. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  448. {
  449. min *= sampleRate;
  450. max *= sampleRate;
  451. def *= sampleRate;
  452. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  453. }
  454. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  455. {
  456. step = max - min;
  457. stepSmall = step;
  458. stepLarge = step;
  459. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  460. }
  461. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  462. {
  463. step = 1.0f;
  464. stepSmall = 1.0f;
  465. stepLarge = 10.0f;
  466. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  467. }
  468. else
  469. {
  470. float range = max - min;
  471. step = range/100.0f;
  472. stepSmall = range/1000.0f;
  473. stepLarge = range/10.0f;
  474. }
  475. if (LADSPA_IS_PORT_INPUT(portType))
  476. {
  477. kData->param.data[j].type = PARAMETER_INPUT;
  478. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  479. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  480. needsCtrlIn = true;
  481. }
  482. else if (LADSPA_IS_PORT_OUTPUT(portType))
  483. {
  484. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  485. {
  486. min = 0.0f;
  487. max = sampleRate;
  488. def = 0.0f;
  489. step = 1.0f;
  490. stepSmall = 1.0f;
  491. stepLarge = 1.0f;
  492. kData->param.data[j].type = PARAMETER_LATENCY;
  493. kData->param.data[j].hints = 0;
  494. }
  495. else if (std::strcmp(fDescriptor->PortNames[i], "_sample-rate") == 0)
  496. {
  497. def = sampleRate;
  498. step = 1.0f;
  499. stepSmall = 1.0f;
  500. stepLarge = 1.0f;
  501. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  502. kData->param.data[j].hints = 0;
  503. }
  504. else
  505. {
  506. kData->param.data[j].type = PARAMETER_OUTPUT;
  507. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  508. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  509. needsCtrlOut = true;
  510. }
  511. }
  512. else
  513. {
  514. kData->param.data[j].type = PARAMETER_UNKNOWN;
  515. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  516. }
  517. // extra parameter hints
  518. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  519. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  520. // check for scalepoints, require at least 2 to make it useful
  521. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount > 1)
  522. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  523. kData->param.ranges[j].min = min;
  524. kData->param.ranges[j].max = max;
  525. kData->param.ranges[j].def = def;
  526. kData->param.ranges[j].step = step;
  527. kData->param.ranges[j].stepSmall = stepSmall;
  528. kData->param.ranges[j].stepLarge = stepLarge;
  529. // Start parameters in their default values
  530. fParamBuffers[j] = def;
  531. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  532. if (fHandle2 != nullptr)
  533. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  534. }
  535. else
  536. {
  537. // Not Audio or Control
  538. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  539. fDescriptor->connect_port(fHandle, i, nullptr);
  540. if (fHandle2 != nullptr)
  541. fDescriptor->connect_port(fHandle2, i, nullptr);
  542. }
  543. }
  544. if (needsCtrlIn)
  545. {
  546. portName.clear();
  547. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  548. {
  549. portName = fName;
  550. portName += ":";
  551. }
  552. portName += "event-in";
  553. portName.truncate(portNameSize);
  554. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  555. }
  556. if (needsCtrlOut)
  557. {
  558. portName.clear();
  559. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  560. {
  561. portName = fName;
  562. portName += ":";
  563. }
  564. portName += "event-out";
  565. portName.truncate(portNameSize);
  566. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  567. }
  568. // plugin hints
  569. fHints = 0x0;
  570. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  571. fHints |= PLUGIN_CAN_DRYWET;
  572. if (aOuts > 0)
  573. fHints |= PLUGIN_CAN_VOLUME;
  574. if (aOuts >= 2 && aOuts % 2 == 0)
  575. fHints |= PLUGIN_CAN_BALANCE;
  576. // extra plugin hints
  577. kData->extraHints = 0x0;
  578. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  579. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  580. // plugin options
  581. fOptions = 0x0;
  582. if (forcedStereoIn || forcedStereoOut)
  583. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  584. // check latency
  585. if (fHints & PLUGIN_CAN_DRYWET)
  586. {
  587. for (uint32_t i=0; i < kData->param.count; i++)
  588. {
  589. if (kData->param.data[i].type != PARAMETER_LATENCY)
  590. continue;
  591. // we need to pre-run the plugin so it can update its latency control-port
  592. float tmpIn[aIns][2];
  593. float tmpOut[aOuts][2];
  594. for (j=0; j < aIns; j++)
  595. {
  596. tmpIn[j][0] = 0.0f;
  597. tmpIn[j][1] = 0.0f;
  598. fDescriptor->connect_port(fHandle, kData->audioIn.ports[j].rindex, tmpIn[j]);
  599. }
  600. for (j=0; j < aOuts; j++)
  601. {
  602. tmpOut[j][0] = 0.0f;
  603. tmpOut[j][1] = 0.0f;
  604. fDescriptor->connect_port(fHandle, kData->audioOut.ports[j].rindex, tmpOut[j]);
  605. }
  606. if (fDescriptor->activate != nullptr)
  607. fDescriptor->activate(fHandle);
  608. fDescriptor->run(fHandle, 2);
  609. if (fDescriptor->deactivate != nullptr)
  610. fDescriptor->deactivate(fHandle);
  611. const uint32_t latency = (uint32_t)fParamBuffers[i];
  612. if (kData->latency != latency)
  613. {
  614. kData->latency = latency;
  615. kData->client->setLatency(latency);
  616. recreateLatencyBuffers();
  617. }
  618. break;
  619. }
  620. }
  621. bufferSizeChanged(kData->engine->getBufferSize());
  622. carla_debug("LadspaPlugin::reload() - end");
  623. }
  624. // -------------------------------------------------------------------
  625. // Plugin processing
  626. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  627. {
  628. uint32_t i, k;
  629. // --------------------------------------------------------------------------------------------------------
  630. // Check if active
  631. if (! kData->active)
  632. {
  633. // disable any output sound
  634. for (i=0; i < kData->audioOut.count; i++)
  635. carla_zeroFloat(outBuffer[i], frames);
  636. if (kData->activeBefore)
  637. {
  638. if (fDescriptor->deactivate != nullptr)
  639. {
  640. fDescriptor->deactivate(fHandle);
  641. if (fHandle2 != nullptr)
  642. fDescriptor->deactivate(fHandle2);
  643. }
  644. }
  645. kData->activeBefore = kData->active;
  646. return;
  647. }
  648. // --------------------------------------------------------------------------------------------------------
  649. // Check if not active before
  650. if (kData->needsReset || ! kData->activeBefore)
  651. {
  652. if (kData->latency > 0)
  653. {
  654. for (i=0; i < kData->audioIn.count; i++)
  655. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  656. }
  657. if (kData->activeBefore)
  658. {
  659. if (fDescriptor->deactivate != nullptr)
  660. {
  661. fDescriptor->deactivate(fHandle);
  662. if (fHandle2 != nullptr)
  663. fDescriptor->deactivate(fHandle2);
  664. }
  665. }
  666. if (fDescriptor->activate != nullptr)
  667. {
  668. fDescriptor->activate(fHandle);
  669. if (fHandle2 != nullptr)
  670. fDescriptor->activate(fHandle2);
  671. }
  672. kData->needsReset = false;
  673. }
  674. // --------------------------------------------------------------------------------------------------------
  675. // Event Input and Processing
  676. if (kData->event.portIn != nullptr && kData->activeBefore)
  677. {
  678. // ----------------------------------------------------------------------------------------------------
  679. // Event Input (System)
  680. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  681. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  682. uint32_t timeOffset = 0;
  683. for (i=0; i < nEvents; i++)
  684. {
  685. const EngineEvent& event = kData->event.portIn->getEvent(i);
  686. time = event.time;
  687. if (time >= frames)
  688. continue;
  689. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  690. if (time > timeOffset && sampleAccurate)
  691. {
  692. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  693. timeOffset = time;
  694. }
  695. // Control change
  696. switch (event.type)
  697. {
  698. case kEngineEventTypeNull:
  699. break;
  700. case kEngineEventTypeControl:
  701. {
  702. const EngineControlEvent& ctrlEvent = event.ctrl;
  703. switch (ctrlEvent.type)
  704. {
  705. case kEngineControlEventTypeNull:
  706. break;
  707. case kEngineControlEventTypeParameter:
  708. {
  709. // Control backend stuff
  710. if (event.channel == kData->ctrlChannel)
  711. {
  712. float value;
  713. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  714. {
  715. value = ctrlEvent.value;
  716. setDryWet(value, false, false);
  717. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  718. continue;
  719. }
  720. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  721. {
  722. value = ctrlEvent.value*127.0f/100.0f;
  723. setVolume(value, false, false);
  724. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  725. continue;
  726. }
  727. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  728. {
  729. float left, right;
  730. value = ctrlEvent.value/0.5f - 1.0f;
  731. if (value < 0.0f)
  732. {
  733. left = -1.0f;
  734. right = (value*2.0f)+1.0f;
  735. }
  736. else if (value > 0.0f)
  737. {
  738. left = (value*2.0f)-1.0f;
  739. right = 1.0f;
  740. }
  741. else
  742. {
  743. left = -1.0f;
  744. right = 1.0f;
  745. }
  746. setBalanceLeft(left, false, false);
  747. setBalanceRight(right, false, false);
  748. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  749. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  750. continue;
  751. }
  752. }
  753. // Control plugin parameters
  754. for (k=0; k < kData->param.count; k++)
  755. {
  756. if (kData->param.data[k].midiChannel != event.channel)
  757. continue;
  758. if (kData->param.data[k].midiCC != ctrlEvent.param)
  759. continue;
  760. if (kData->param.data[k].type != PARAMETER_INPUT)
  761. continue;
  762. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  763. continue;
  764. float value;
  765. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  766. {
  767. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  768. }
  769. else
  770. {
  771. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  772. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  773. value = std::rint(value);
  774. }
  775. setParameterValue(k, value, false, false, false);
  776. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  777. }
  778. break;
  779. }
  780. case kEngineControlEventTypeMidiBank:
  781. case kEngineControlEventTypeMidiProgram:
  782. break;
  783. case kEngineControlEventTypeAllSoundOff:
  784. if (event.channel == kData->ctrlChannel)
  785. {
  786. if (fDescriptor->deactivate != nullptr)
  787. {
  788. fDescriptor->deactivate(fHandle);
  789. if (fHandle2 != nullptr)
  790. fDescriptor->deactivate(fHandle2);
  791. }
  792. if (fDescriptor->activate != nullptr)
  793. {
  794. fDescriptor->activate(fHandle);
  795. if (fHandle2 != nullptr)
  796. fDescriptor->activate(fHandle2);
  797. }
  798. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  799. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  800. }
  801. break;
  802. case kEngineControlEventTypeAllNotesOff:
  803. break;
  804. }
  805. break;
  806. }
  807. case kEngineEventTypeMidi:
  808. // ignored in LADSPA
  809. break;
  810. }
  811. }
  812. kData->postRtEvents.trySplice();
  813. if (frames > timeOffset)
  814. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  815. } // End of Event Input and Processing
  816. // --------------------------------------------------------------------------------------------------------
  817. // Plugin processing (no events)
  818. else
  819. {
  820. processSingle(inBuffer, outBuffer, frames, 0);
  821. } // End of Plugin processing (no events)
  822. // --------------------------------------------------------------------------------------------------------
  823. // Special Parameters
  824. #if 0
  825. CARLA_PROCESS_CONTINUE_CHECK;
  826. for (k=0; k < param.count; k++)
  827. {
  828. if (param.data[k].type == PARAMETER_LATENCY)
  829. {
  830. // TODO
  831. }
  832. }
  833. CARLA_PROCESS_CONTINUE_CHECK;
  834. #endif
  835. CARLA_PROCESS_CONTINUE_CHECK;
  836. // --------------------------------------------------------------------------------------------------------
  837. // Control Output
  838. if (kData->event.portOut != nullptr)
  839. {
  840. uint8_t channel;
  841. uint16_t param;
  842. float value;
  843. for (k=0; k < kData->param.count; k++)
  844. {
  845. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  846. continue;
  847. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  848. if (kData->param.data[k].midiCC > 0)
  849. {
  850. channel = kData->param.data[k].midiChannel;
  851. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  852. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  853. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  854. }
  855. }
  856. } // End of Control Output
  857. // --------------------------------------------------------------------------------------------------------
  858. kData->activeBefore = kData->active;
  859. }
  860. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  861. {
  862. uint32_t i, k;
  863. // --------------------------------------------------------------------------------------------------------
  864. // Try lock, silence otherwise
  865. if (kData->engine->isOffline())
  866. {
  867. kData->singleMutex.lock();
  868. }
  869. else if (! kData->singleMutex.tryLock())
  870. {
  871. for (i=0; i < kData->audioOut.count; i++)
  872. {
  873. for (k=0; k < frames; k++)
  874. outBuffer[i][k+timeOffset] = 0.0f;
  875. }
  876. return false;
  877. }
  878. // --------------------------------------------------------------------------------------------------------
  879. // Fill plugin buffers
  880. for (i=0; i < kData->audioIn.count; i++)
  881. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  882. for (i=0; i < kData->audioOut.count; i++)
  883. carla_zeroFloat(fAudioOutBuffers[i], frames);
  884. // --------------------------------------------------------------------------------------------------------
  885. // Run plugin
  886. fDescriptor->run(fHandle, frames);
  887. if (fHandle2 != nullptr)
  888. fDescriptor->run(fHandle2, frames);
  889. // --------------------------------------------------------------------------------------------------------
  890. // Post-processing (dry/wet, volume and balance)
  891. {
  892. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  893. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  894. float bufValue, oldBufLeft[doBalance ? frames : 1];
  895. for (i=0; i < kData->audioOut.count; i++)
  896. {
  897. // Dry/Wet
  898. if (doDryWet)
  899. {
  900. for (k=0; k < frames; k++)
  901. {
  902. // TODO
  903. //if (k < kData->latency && kData->latency < frames)
  904. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  905. //else
  906. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  907. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  908. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  909. }
  910. }
  911. // Balance
  912. if (doBalance)
  913. {
  914. if (i % 2 == 0)
  915. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  916. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  917. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  918. for (k=0; k < frames; k++)
  919. {
  920. if (i % 2 == 0)
  921. {
  922. // left
  923. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  924. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  925. }
  926. else
  927. {
  928. // right
  929. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  930. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  931. }
  932. }
  933. }
  934. // Volume (and buffer copy)
  935. {
  936. for (k=0; k < frames; k++)
  937. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  938. }
  939. }
  940. #if 0
  941. // Latency, save values for next callback, TODO
  942. if (kData->latency > 0 && kData->latency < frames)
  943. {
  944. for (i=0; i < kData->audioIn.count; i++)
  945. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  946. }
  947. #endif
  948. } // End of Post-processing
  949. // --------------------------------------------------------------------------------------------------------
  950. kData->singleMutex.unlock();
  951. return true;
  952. }
  953. void bufferSizeChanged(const uint32_t newBufferSize)
  954. {
  955. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  956. for (uint32_t i=0; i < kData->audioIn.count; i++)
  957. {
  958. if (fAudioInBuffers[i] != nullptr)
  959. delete[] fAudioInBuffers[i];
  960. fAudioInBuffers[i] = new float[newBufferSize];
  961. }
  962. for (uint32_t i=0; i < kData->audioOut.count; i++)
  963. {
  964. if (fAudioOutBuffers[i] != nullptr)
  965. delete[] fAudioOutBuffers[i];
  966. fAudioOutBuffers[i] = new float[newBufferSize];
  967. }
  968. if (fHandle2 == nullptr)
  969. {
  970. for (uint32_t i=0; i < kData->audioIn.count; i++)
  971. {
  972. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  973. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  974. }
  975. for (uint32_t i=0; i < kData->audioOut.count; i++)
  976. {
  977. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  978. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  979. }
  980. }
  981. else
  982. {
  983. if (kData->audioIn.count > 0)
  984. {
  985. CARLA_ASSERT(kData->audioIn.count == 2);
  986. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  987. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  988. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  989. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  990. }
  991. if (kData->audioOut.count > 0)
  992. {
  993. CARLA_ASSERT(kData->audioOut.count == 2);
  994. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  995. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  996. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  997. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  998. }
  999. }
  1000. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1001. }
  1002. // -------------------------------------------------------------------
  1003. // Cleanup
  1004. void deleteBuffers()
  1005. {
  1006. carla_debug("LadspaPlugin::deleteBuffers() - start");
  1007. if (fAudioInBuffers != nullptr)
  1008. {
  1009. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1010. {
  1011. if (fAudioInBuffers[i] != nullptr)
  1012. {
  1013. delete[] fAudioInBuffers[i];
  1014. fAudioInBuffers[i] = nullptr;
  1015. }
  1016. }
  1017. delete[] fAudioInBuffers;
  1018. fAudioInBuffers = nullptr;
  1019. }
  1020. if (fAudioOutBuffers != nullptr)
  1021. {
  1022. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1023. {
  1024. if (fAudioOutBuffers[i] != nullptr)
  1025. {
  1026. delete[] fAudioOutBuffers[i];
  1027. fAudioOutBuffers[i] = nullptr;
  1028. }
  1029. }
  1030. delete[] fAudioOutBuffers;
  1031. fAudioOutBuffers = nullptr;
  1032. }
  1033. if (fParamBuffers != nullptr)
  1034. {
  1035. delete[] fParamBuffers;
  1036. fParamBuffers = nullptr;
  1037. }
  1038. CarlaPlugin::deleteBuffers();
  1039. carla_debug("LadspaPlugin::deleteBuffers() - end");
  1040. }
  1041. // -------------------------------------------------------------------
  1042. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1043. {
  1044. CARLA_ASSERT(kData->engine != nullptr);
  1045. CARLA_ASSERT(kData->client == nullptr);
  1046. CARLA_ASSERT(filename != nullptr);
  1047. CARLA_ASSERT(label != nullptr);
  1048. // ---------------------------------------------------------------
  1049. // open DLL
  1050. if (! libOpen(filename))
  1051. {
  1052. kData->engine->setLastError(libError(filename));
  1053. return false;
  1054. }
  1055. // ---------------------------------------------------------------
  1056. // get DLL main entry
  1057. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)libSymbol("ladspa_descriptor");
  1058. if (descFn == nullptr)
  1059. {
  1060. kData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1061. return false;
  1062. }
  1063. // ---------------------------------------------------------------
  1064. // get descriptor that matches label
  1065. unsigned long i = 0;
  1066. while ((fDescriptor = descFn(i++)) != nullptr)
  1067. {
  1068. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1069. break;
  1070. }
  1071. if (fDescriptor == nullptr)
  1072. {
  1073. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1074. return false;
  1075. }
  1076. // ---------------------------------------------------------------
  1077. // get info
  1078. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1079. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1080. if (name != nullptr)
  1081. fName = kData->engine->getNewUniquePluginName(name);
  1082. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  1083. fName = kData->engine->getNewUniquePluginName(fRdfDescriptor->Title);
  1084. else if (fDescriptor->Name != nullptr)
  1085. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1086. else
  1087. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1088. fFilename = filename;
  1089. // ---------------------------------------------------------------
  1090. // register client
  1091. kData->client = kData->engine->addClient(this);
  1092. if (kData->client == nullptr || ! kData->client->isOk())
  1093. {
  1094. kData->engine->setLastError("Failed to register plugin client");
  1095. return false;
  1096. }
  1097. // ---------------------------------------------------------------
  1098. // initialize plugin
  1099. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1100. if (fHandle == nullptr)
  1101. {
  1102. kData->engine->setLastError("Plugin failed to initialize");
  1103. return false;
  1104. }
  1105. return true;
  1106. }
  1107. private:
  1108. LADSPA_Handle fHandle;
  1109. LADSPA_Handle fHandle2;
  1110. const LADSPA_Descriptor* fDescriptor;
  1111. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1112. float** fAudioInBuffers;
  1113. float** fAudioOutBuffers;
  1114. float* fParamBuffers;
  1115. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1116. };
  1117. CARLA_BACKEND_END_NAMESPACE
  1118. #else // WANT_LADSPA
  1119. # warning Building without LADSPA support
  1120. #endif
  1121. CARLA_BACKEND_START_NAMESPACE
  1122. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1123. {
  1124. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1125. #ifdef WANT_LADSPA
  1126. LadspaPlugin* const plugin = new LadspaPlugin(init.engine, init.id);
  1127. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1128. {
  1129. delete plugin;
  1130. return nullptr;
  1131. }
  1132. plugin->reload();
  1133. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1134. {
  1135. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1136. delete plugin;
  1137. return nullptr;
  1138. }
  1139. return plugin;
  1140. #else
  1141. init.engine->setLastError("LADSPA support not available");
  1142. return nullptr;
  1143. #endif
  1144. }
  1145. CARLA_BACKEND_END_NAMESPACE