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.

CarlaPluginFluidSynth.cpp 71KB

11 years ago
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
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
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
10 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
10 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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
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
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
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
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. /*
  2. * Carla FluidSynth Plugin
  3. * Copyright (C) 2011-2019 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. #ifdef HAVE_FLUIDSYNTH
  20. #include "CarlaBackendUtils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "water/text/StringArray.h"
  23. #include <fluidsynth.h>
  24. #define FLUID_DEFAULT_POLYPHONY 64
  25. using water::String;
  26. using water::StringArray;
  27. CARLA_BACKEND_START_NAMESPACE
  28. // -------------------------------------------------------------------------------------------------------------------
  29. // Fallback data
  30. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  31. // -------------------------------------------------------------------------------------------------------------------
  32. class CarlaPluginFluidSynth : public CarlaPlugin
  33. {
  34. public:
  35. CarlaPluginFluidSynth(CarlaEngine* const engine, const uint id, const bool use16Outs)
  36. : CarlaPlugin(engine, id),
  37. kUse16Outs(use16Outs),
  38. fSettings(nullptr),
  39. fSynth(nullptr),
  40. fSynthId(0),
  41. fAudio16Buffers(nullptr),
  42. fLabel(nullptr)
  43. {
  44. carla_debug("CarlaPluginFluidSynth::CarlaPluginFluidSynth(%p, %i, %s)", engine, id, bool2str(use16Outs));
  45. carla_zeroFloats(fParamBuffers, FluidSynthParametersMax);
  46. carla_fill<int32_t>(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  47. // create settings
  48. fSettings = new_fluid_settings();
  49. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  50. // define settings
  51. fluid_settings_setint(fSettings, "synth.audio-channels", use16Outs ? 16 : 1);
  52. fluid_settings_setint(fSettings, "synth.audio-groups", use16Outs ? 16 : 1);
  53. fluid_settings_setnum(fSettings, "synth.sample-rate", pData->engine->getSampleRate());
  54. //fluid_settings_setnum(fSettings, "synth.cpu-cores", 2);
  55. fluid_settings_setint(fSettings, "synth.ladspa.active", 0);
  56. fluid_settings_setint(fSettings, "synth.lock-memory", 1);
  57. #if FLUIDSYNTH_VERSION_MAJOR < 2
  58. fluid_settings_setint(fSettings, "synth.parallel-render", 1);
  59. #endif
  60. fluid_settings_setint(fSettings, "synth.threadsafe-api", 0);
  61. #ifdef DEBUG
  62. fluid_settings_setint(fSettings, "synth.verbose", 1);
  63. #endif
  64. // create synth
  65. fSynth = new_fluid_synth(fSettings);
  66. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  67. initializeFluidDefaultsIfNeeded();
  68. #if FLUIDSYNTH_VERSION_MAJOR < 2
  69. fluid_synth_set_sample_rate(fSynth, static_cast<float>(pData->engine->getSampleRate()));
  70. #endif
  71. // set default values
  72. fluid_synth_set_reverb_on(fSynth, 1);
  73. fluid_synth_set_reverb(fSynth,
  74. sFluidDefaults[FluidSynthReverbRoomSize],
  75. sFluidDefaults[FluidSynthReverbDamp],
  76. sFluidDefaults[FluidSynthReverbWidth],
  77. sFluidDefaults[FluidSynthReverbLevel]);
  78. fluid_synth_set_chorus_on(fSynth, 1);
  79. fluid_synth_set_chorus(fSynth,
  80. static_cast<int>(sFluidDefaults[FluidSynthChorusNr] + 0.5f),
  81. sFluidDefaults[FluidSynthChorusLevel],
  82. sFluidDefaults[FluidSynthChorusSpeedHz],
  83. sFluidDefaults[FluidSynthChorusDepthMs],
  84. static_cast<int>(sFluidDefaults[FluidSynthChorusType] + 0.5f));
  85. fluid_synth_set_polyphony(fSynth, FLUID_DEFAULT_POLYPHONY);
  86. fluid_synth_set_gain(fSynth, 1.0f);
  87. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  88. fluid_synth_set_interp_method(fSynth, i, static_cast<int>(sFluidDefaults[FluidSynthInterpolation] + 0.5f));
  89. }
  90. ~CarlaPluginFluidSynth() override
  91. {
  92. carla_debug("CarlaPluginFluidSynth::~CarlaPluginFluidSynth()");
  93. pData->singleMutex.lock();
  94. pData->masterMutex.lock();
  95. if (pData->client != nullptr && pData->client->isActive())
  96. pData->client->deactivate();
  97. if (pData->active)
  98. {
  99. deactivate();
  100. pData->active = false;
  101. }
  102. if (fSynth != nullptr)
  103. {
  104. delete_fluid_synth(fSynth);
  105. fSynth = nullptr;
  106. }
  107. if (fSettings != nullptr)
  108. {
  109. delete_fluid_settings(fSettings);
  110. fSettings = nullptr;
  111. }
  112. if (fLabel != nullptr)
  113. {
  114. delete[] fLabel;
  115. fLabel = nullptr;
  116. }
  117. clearBuffers();
  118. }
  119. // -------------------------------------------------------------------
  120. // Information (base)
  121. PluginType getType() const noexcept override
  122. {
  123. return PLUGIN_SF2;
  124. }
  125. PluginCategory getCategory() const noexcept override
  126. {
  127. return PLUGIN_CATEGORY_SYNTH;
  128. }
  129. // -------------------------------------------------------------------
  130. // Information (count)
  131. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  132. {
  133. switch (parameterId)
  134. {
  135. case FluidSynthChorusType:
  136. return 2;
  137. case FluidSynthInterpolation:
  138. return 4;
  139. default:
  140. return 0;
  141. }
  142. }
  143. // -------------------------------------------------------------------
  144. // Information (current data)
  145. // nothing
  146. // -------------------------------------------------------------------
  147. // Information (per-plugin data)
  148. uint getOptionsAvailable() const noexcept override
  149. {
  150. uint options = 0x0;
  151. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  152. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  153. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  154. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  155. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  156. return options;
  157. }
  158. float getParameterValue(const uint32_t parameterId) const noexcept override
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  161. return fParamBuffers[parameterId];
  162. }
  163. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  164. {
  165. switch (parameterId)
  166. {
  167. case FluidSynthChorusType:
  168. switch (scalePointId)
  169. {
  170. case 0:
  171. return FLUID_CHORUS_MOD_SINE;
  172. case 1:
  173. return FLUID_CHORUS_MOD_TRIANGLE;
  174. default:
  175. return sFluidDefaults[FluidSynthChorusType];
  176. }
  177. case FluidSynthInterpolation:
  178. switch (scalePointId)
  179. {
  180. case 0:
  181. return FLUID_INTERP_NONE;
  182. case 1:
  183. return FLUID_INTERP_LINEAR;
  184. case 2:
  185. return FLUID_INTERP_4THORDER;
  186. case 3:
  187. return FLUID_INTERP_7THORDER;
  188. default:
  189. return sFluidDefaults[FluidSynthInterpolation];
  190. }
  191. default:
  192. return 0.0f;
  193. }
  194. }
  195. bool getLabel(char* const strBuf) const noexcept override
  196. {
  197. if (fLabel != nullptr)
  198. {
  199. std::strncpy(strBuf, fLabel, STR_MAX);
  200. return true;
  201. }
  202. return CarlaPlugin::getLabel(strBuf);
  203. }
  204. bool getMaker(char* const strBuf) const noexcept override
  205. {
  206. std::strncpy(strBuf, "FluidSynth SF2 engine", STR_MAX);
  207. return true;
  208. }
  209. bool getCopyright(char* const strBuf) const noexcept override
  210. {
  211. std::strncpy(strBuf, "GNU GPL v2+", STR_MAX);
  212. return true;
  213. }
  214. bool getRealName(char* const strBuf) const noexcept override
  215. {
  216. return getLabel(strBuf);
  217. }
  218. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  219. {
  220. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  221. switch (parameterId)
  222. {
  223. case FluidSynthReverbOnOff:
  224. std::strncpy(strBuf, "Reverb On/Off", STR_MAX);
  225. return true;
  226. case FluidSynthReverbRoomSize:
  227. std::strncpy(strBuf, "Reverb Room Size", STR_MAX);
  228. return true;
  229. case FluidSynthReverbDamp:
  230. std::strncpy(strBuf, "Reverb Damp", STR_MAX);
  231. return true;
  232. case FluidSynthReverbLevel:
  233. std::strncpy(strBuf, "Reverb Level", STR_MAX);
  234. return true;
  235. case FluidSynthReverbWidth:
  236. std::strncpy(strBuf, "Reverb Width", STR_MAX);
  237. return true;
  238. case FluidSynthChorusOnOff:
  239. std::strncpy(strBuf, "Chorus On/Off", STR_MAX);
  240. return true;
  241. case FluidSynthChorusNr:
  242. std::strncpy(strBuf, "Chorus Voice Count", STR_MAX);
  243. return true;
  244. case FluidSynthChorusLevel:
  245. std::strncpy(strBuf, "Chorus Level", STR_MAX);
  246. return true;
  247. case FluidSynthChorusSpeedHz:
  248. std::strncpy(strBuf, "Chorus Speed", STR_MAX);
  249. return true;
  250. case FluidSynthChorusDepthMs:
  251. std::strncpy(strBuf, "Chorus Depth", STR_MAX);
  252. return true;
  253. case FluidSynthChorusType:
  254. std::strncpy(strBuf, "Chorus Type", STR_MAX);
  255. return true;
  256. case FluidSynthPolyphony:
  257. std::strncpy(strBuf, "Polyphony", STR_MAX);
  258. return true;
  259. case FluidSynthInterpolation:
  260. std::strncpy(strBuf, "Interpolation", STR_MAX);
  261. return true;
  262. case FluidSynthVoiceCount:
  263. std::strncpy(strBuf, "Voice Count", STR_MAX);
  264. return true;
  265. }
  266. return CarlaPlugin::getParameterName(parameterId, strBuf);
  267. }
  268. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  271. switch (parameterId)
  272. {
  273. case FluidSynthChorusSpeedHz:
  274. std::strncpy(strBuf, "Hz", STR_MAX);
  275. return true;
  276. case FluidSynthChorusDepthMs:
  277. std::strncpy(strBuf, "ms", STR_MAX);
  278. return true;
  279. }
  280. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  281. }
  282. bool getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  283. {
  284. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  285. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), false);
  286. switch (parameterId)
  287. {
  288. case FluidSynthChorusType:
  289. switch (scalePointId)
  290. {
  291. case 0:
  292. std::strncpy(strBuf, "Sine wave", STR_MAX);
  293. return true;
  294. case 1:
  295. std::strncpy(strBuf, "Triangle wave", STR_MAX);
  296. return true;
  297. }
  298. break;
  299. case FluidSynthInterpolation:
  300. switch (scalePointId)
  301. {
  302. case 0:
  303. std::strncpy(strBuf, "None", STR_MAX);
  304. return true;
  305. case 1:
  306. std::strncpy(strBuf, "Straight-line", STR_MAX);
  307. return true;
  308. case 2:
  309. std::strncpy(strBuf, "Fourth-order", STR_MAX);
  310. return true;
  311. case 3:
  312. std::strncpy(strBuf, "Seventh-order", STR_MAX);
  313. return true;
  314. }
  315. break;
  316. }
  317. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  318. }
  319. // -------------------------------------------------------------------
  320. // Set data (state)
  321. void prepareForSave() override
  322. {
  323. char strBuf[STR_MAX+1];
  324. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i", fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  325. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  326. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  327. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  328. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  329. }
  330. // -------------------------------------------------------------------
  331. // Set data (internal stuff)
  332. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  333. {
  334. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  335. pData->midiprog.current = fCurMidiProgs[channel];
  336. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  337. }
  338. // -------------------------------------------------------------------
  339. // Set data (plugin-specific stuff)
  340. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  341. {
  342. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  343. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
  344. float fixedValue;
  345. {
  346. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  347. fixedValue = setParameterValueInFluidSynth(parameterId, value);
  348. }
  349. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  350. }
  351. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  352. {
  353. const float fixedValue = setParameterValueInFluidSynth(parameterId, value);
  354. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  355. }
  356. float setParameterValueInFluidSynth(const uint32_t parameterId, const float value) noexcept
  357. {
  358. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, value);
  359. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  360. fParamBuffers[parameterId] = fixedValue;
  361. switch (parameterId)
  362. {
  363. case FluidSynthReverbOnOff:
  364. try {
  365. fluid_synth_set_reverb_on(fSynth, (fixedValue > 0.5f) ? 1 : 0);
  366. } CARLA_SAFE_EXCEPTION("fluid_synth_set_reverb_on")
  367. break;
  368. case FluidSynthReverbRoomSize:
  369. case FluidSynthReverbDamp:
  370. case FluidSynthReverbLevel:
  371. case FluidSynthReverbWidth:
  372. try {
  373. fluid_synth_set_reverb(fSynth,
  374. fParamBuffers[FluidSynthReverbRoomSize],
  375. fParamBuffers[FluidSynthReverbDamp],
  376. fParamBuffers[FluidSynthReverbWidth],
  377. fParamBuffers[FluidSynthReverbLevel]);
  378. } CARLA_SAFE_EXCEPTION("fluid_synth_set_reverb")
  379. break;
  380. case FluidSynthChorusOnOff:
  381. try {
  382. fluid_synth_set_chorus_on(fSynth, (value > 0.5f) ? 1 : 0);
  383. } CARLA_SAFE_EXCEPTION("fluid_synth_set_chorus_on")
  384. break;
  385. case FluidSynthChorusNr:
  386. case FluidSynthChorusLevel:
  387. case FluidSynthChorusSpeedHz:
  388. case FluidSynthChorusDepthMs:
  389. case FluidSynthChorusType:
  390. try {
  391. fluid_synth_set_chorus(fSynth,
  392. static_cast<int>(fParamBuffers[FluidSynthChorusNr] + 0.5f),
  393. fParamBuffers[FluidSynthChorusLevel],
  394. fParamBuffers[FluidSynthChorusSpeedHz],
  395. fParamBuffers[FluidSynthChorusDepthMs],
  396. static_cast<int>(fParamBuffers[FluidSynthChorusType] + 0.5f));
  397. } CARLA_SAFE_EXCEPTION("fluid_synth_set_chorus")
  398. break;
  399. case FluidSynthPolyphony:
  400. try {
  401. fluid_synth_set_polyphony(fSynth, static_cast<int>(value + 0.5f));
  402. } CARLA_SAFE_EXCEPTION("fluid_synth_set_polyphony")
  403. break;
  404. case FluidSynthInterpolation:
  405. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  406. {
  407. try {
  408. fluid_synth_set_interp_method(fSynth, i, static_cast<int>(value + 0.5f));
  409. } CARLA_SAFE_EXCEPTION_BREAK("fluid_synth_set_interp_method")
  410. }
  411. break;
  412. default:
  413. break;
  414. }
  415. return fixedValue;
  416. }
  417. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  418. {
  419. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  420. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  421. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  422. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  423. carla_debug("CarlaPluginFluidSynth::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  424. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  425. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  426. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  427. return carla_stderr2("CarlaPluginFluidSynth::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  428. if (std::strcmp(key, "midiPrograms") != 0)
  429. return carla_stderr2("CarlaPluginFluidSynth::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  430. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  431. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  432. {
  433. uint8_t channel = 0;
  434. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  435. {
  436. const int index(it->getIntValue());
  437. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  438. {
  439. const uint32_t bank = pData->midiprog.data[index].bank;
  440. const uint32_t program = pData->midiprog.data[index].program;
  441. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  442. fluid_synth_program_select(fSynth,
  443. static_cast<int>(channel),
  444. fSynthId,
  445. static_cast<int>(bank),
  446. static_cast<int>(program));
  447. #else
  448. fluid_synth_program_select(fSynth, channel, fSynthId, bank, program);
  449. #endif
  450. fCurMidiProgs[channel] = index;
  451. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  452. {
  453. pData->midiprog.current = index;
  454. pData->engine->callback(true, true,
  455. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  456. pData->id,
  457. index,
  458. 0, 0, 0.0f, nullptr);
  459. }
  460. }
  461. ++channel;
  462. }
  463. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  464. }
  465. CarlaPlugin::setCustomData(type, key, value, sendGui);
  466. }
  467. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  468. {
  469. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  470. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  471. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  472. if (index >= 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  473. {
  474. const uint32_t bank = pData->midiprog.data[index].bank;
  475. const uint32_t program = pData->midiprog.data[index].program;
  476. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  477. try {
  478. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  479. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId,
  480. static_cast<int>(bank), static_cast<int>(program));
  481. #else
  482. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  483. #endif
  484. } CARLA_SAFE_EXCEPTION("fluid_synth_program_select")
  485. fCurMidiProgs[pData->ctrlChannel] = index;
  486. }
  487. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  488. }
  489. // FIXME: this is never used
  490. void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
  491. {
  492. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  493. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  494. if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  495. {
  496. const uint32_t bank = pData->midiprog.data[uindex].bank;
  497. const uint32_t program = pData->midiprog.data[uindex].program;
  498. try {
  499. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  500. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId,
  501. static_cast<int>(bank), static_cast<int>(program));
  502. #else
  503. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  504. #endif
  505. } CARLA_SAFE_EXCEPTION("fluid_synth_program_select")
  506. fCurMidiProgs[pData->ctrlChannel] = static_cast<int32_t>(uindex);
  507. }
  508. CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
  509. }
  510. // -------------------------------------------------------------------
  511. // Set ui stuff
  512. // nothing
  513. // -------------------------------------------------------------------
  514. // Plugin state
  515. void reload() override
  516. {
  517. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  518. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  519. carla_debug("CarlaPluginFluidSynth::reload() - start");
  520. const EngineProcessMode processMode(pData->engine->getProccessMode());
  521. // Safely disable plugin for reload
  522. const ScopedDisabler sd(this);
  523. if (pData->active)
  524. deactivate();
  525. clearBuffers();
  526. uint32_t aOuts, params;
  527. aOuts = kUse16Outs ? 32 : 2;
  528. params = FluidSynthParametersMax;
  529. pData->audioOut.createNew(aOuts);
  530. pData->param.createNew(params, false);
  531. const uint portNameSize(pData->engine->getMaxPortNameSize());
  532. CarlaString portName;
  533. // ---------------------------------------
  534. // Audio Outputs
  535. if (kUse16Outs)
  536. {
  537. for (uint32_t i=0; i < 32; ++i)
  538. {
  539. portName.clear();
  540. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  541. {
  542. portName = pData->name;
  543. portName += ":";
  544. }
  545. portName += "out-";
  546. if ((i+2)/2 < 9)
  547. portName += "0";
  548. portName += CarlaString((i+2)/2);
  549. if (i % 2 == 0)
  550. portName += "L";
  551. else
  552. portName += "R";
  553. portName.truncate(portNameSize);
  554. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, i);
  555. pData->audioOut.ports[i].rindex = i;
  556. }
  557. fAudio16Buffers = new float*[aOuts];
  558. for (uint32_t i=0; i < aOuts; ++i)
  559. fAudio16Buffers[i] = nullptr;
  560. }
  561. else
  562. {
  563. // out-left
  564. portName.clear();
  565. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  566. {
  567. portName = pData->name;
  568. portName += ":";
  569. }
  570. portName += "out-left";
  571. portName.truncate(portNameSize);
  572. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 0);
  573. pData->audioOut.ports[0].rindex = 0;
  574. // out-right
  575. portName.clear();
  576. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  577. {
  578. portName = pData->name;
  579. portName += ":";
  580. }
  581. portName += "out-right";
  582. portName.truncate(portNameSize);
  583. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  584. pData->audioOut.ports[1].rindex = 1;
  585. }
  586. // ---------------------------------------
  587. // Event Input
  588. {
  589. portName.clear();
  590. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  591. {
  592. portName = pData->name;
  593. portName += ":";
  594. }
  595. portName += "events-in";
  596. portName.truncate(portNameSize);
  597. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  598. }
  599. // ---------------------------------------
  600. // Event Output
  601. {
  602. portName.clear();
  603. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  604. {
  605. portName = pData->name;
  606. portName += ":";
  607. }
  608. portName += "events-out";
  609. portName.truncate(portNameSize);
  610. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  611. }
  612. // ---------------------------------------
  613. // Parameters
  614. {
  615. int j;
  616. // ----------------------
  617. j = FluidSynthReverbOnOff;
  618. pData->param.data[j].type = PARAMETER_INPUT;
  619. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/ | PARAMETER_IS_BOOLEAN;
  620. pData->param.data[j].index = j;
  621. pData->param.data[j].rindex = j;
  622. pData->param.ranges[j].min = 0.0f;
  623. pData->param.ranges[j].max = 1.0f;
  624. pData->param.ranges[j].def = sFluidDefaults[j];
  625. pData->param.ranges[j].step = 1.0f;
  626. pData->param.ranges[j].stepSmall = 1.0f;
  627. pData->param.ranges[j].stepLarge = 1.0f;
  628. // ----------------------
  629. j = FluidSynthReverbRoomSize;
  630. pData->param.data[j].type = PARAMETER_INPUT;
  631. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  632. pData->param.data[j].index = j;
  633. pData->param.data[j].rindex = j;
  634. pData->param.ranges[j].min = 0.0f;
  635. pData->param.ranges[j].max = 1.0f;
  636. pData->param.ranges[j].def = sFluidDefaults[j];
  637. pData->param.ranges[j].step = 0.01f;
  638. pData->param.ranges[j].stepSmall = 0.0001f;
  639. pData->param.ranges[j].stepLarge = 0.1f;
  640. // ----------------------
  641. j = FluidSynthReverbDamp;
  642. pData->param.data[j].type = PARAMETER_INPUT;
  643. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  644. pData->param.data[j].index = j;
  645. pData->param.data[j].rindex = j;
  646. pData->param.ranges[j].min = 0.0f;
  647. pData->param.ranges[j].max = 1.0f;
  648. pData->param.ranges[j].def = sFluidDefaults[j];
  649. pData->param.ranges[j].step = 0.01f;
  650. pData->param.ranges[j].stepSmall = 0.0001f;
  651. pData->param.ranges[j].stepLarge = 0.1f;
  652. // ----------------------
  653. j = FluidSynthReverbLevel;
  654. pData->param.data[j].type = PARAMETER_INPUT;
  655. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  656. pData->param.data[j].index = j;
  657. pData->param.data[j].rindex = j;
  658. pData->param.data[j].mappedControlIndex = MIDI_CONTROL_REVERB_SEND_LEVEL;
  659. pData->param.ranges[j].min = 0.0f;
  660. pData->param.ranges[j].max = 1.0f;
  661. pData->param.ranges[j].def = sFluidDefaults[j];
  662. pData->param.ranges[j].step = 0.01f;
  663. pData->param.ranges[j].stepSmall = 0.0001f;
  664. pData->param.ranges[j].stepLarge = 0.1f;
  665. // ----------------------
  666. j = FluidSynthReverbWidth;
  667. pData->param.data[j].type = PARAMETER_INPUT;
  668. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  669. pData->param.data[j].index = j;
  670. pData->param.data[j].rindex = j;
  671. pData->param.ranges[j].min = 0.0f;
  672. pData->param.ranges[j].max = 10.0f; // should be 100, but that sounds too much
  673. pData->param.ranges[j].def = sFluidDefaults[j];
  674. pData->param.ranges[j].step = 0.01f;
  675. pData->param.ranges[j].stepSmall = 0.0001f;
  676. pData->param.ranges[j].stepLarge = 0.1f;
  677. // ----------------------
  678. j = FluidSynthChorusOnOff;
  679. pData->param.data[j].type = PARAMETER_INPUT;
  680. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  681. pData->param.data[j].index = j;
  682. pData->param.data[j].rindex = j;
  683. pData->param.ranges[j].min = 0.0f;
  684. pData->param.ranges[j].max = 1.0f;
  685. pData->param.ranges[j].def = sFluidDefaults[j];
  686. pData->param.ranges[j].step = 1.0f;
  687. pData->param.ranges[j].stepSmall = 1.0f;
  688. pData->param.ranges[j].stepLarge = 1.0f;
  689. // ----------------------
  690. j = FluidSynthChorusNr;
  691. pData->param.data[j].type = PARAMETER_INPUT;
  692. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  693. pData->param.data[j].index = j;
  694. pData->param.data[j].rindex = j;
  695. pData->param.ranges[j].min = 0.0f;
  696. pData->param.ranges[j].max = 99.0f;
  697. pData->param.ranges[j].def = sFluidDefaults[j];
  698. pData->param.ranges[j].step = 1.0f;
  699. pData->param.ranges[j].stepSmall = 1.0f;
  700. pData->param.ranges[j].stepLarge = 10.0f;
  701. // ----------------------
  702. j = FluidSynthChorusLevel;
  703. pData->param.data[j].type = PARAMETER_INPUT;
  704. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  705. pData->param.data[j].index = j;
  706. pData->param.data[j].rindex = j;
  707. pData->param.ranges[j].min = 0.0f;
  708. pData->param.ranges[j].max = 10.0f;
  709. pData->param.ranges[j].def = sFluidDefaults[j];
  710. pData->param.ranges[j].step = 0.01f;
  711. pData->param.ranges[j].stepSmall = 0.0001f;
  712. pData->param.ranges[j].stepLarge = 0.1f;
  713. // ----------------------
  714. j = FluidSynthChorusSpeedHz;
  715. pData->param.data[j].type = PARAMETER_INPUT;
  716. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  717. pData->param.data[j].index = j;
  718. pData->param.data[j].rindex = j;
  719. pData->param.ranges[j].min = 0.29f;
  720. pData->param.ranges[j].max = 5.0f;
  721. pData->param.ranges[j].def = sFluidDefaults[j];
  722. pData->param.ranges[j].step = 0.01f;
  723. pData->param.ranges[j].stepSmall = 0.0001f;
  724. pData->param.ranges[j].stepLarge = 0.1f;
  725. // ----------------------
  726. j = FluidSynthChorusDepthMs;
  727. pData->param.data[j].type = PARAMETER_INPUT;
  728. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  729. pData->param.data[j].index = j;
  730. pData->param.data[j].rindex = j;
  731. pData->param.ranges[j].min = 0.0f;
  732. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  733. pData->param.ranges[j].max = 256.0f;
  734. #else
  735. pData->param.ranges[j].max = float(2048.0 * 1000.0 / pData->engine->getSampleRate()); // FIXME?
  736. #endif
  737. pData->param.ranges[j].def = sFluidDefaults[j];
  738. pData->param.ranges[j].step = 0.01f;
  739. pData->param.ranges[j].stepSmall = 0.0001f;
  740. pData->param.ranges[j].stepLarge = 0.1f;
  741. // ----------------------
  742. j = FluidSynthChorusType;
  743. pData->param.data[j].type = PARAMETER_INPUT;
  744. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  745. pData->param.data[j].index = j;
  746. pData->param.data[j].rindex = j;
  747. pData->param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  748. pData->param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  749. pData->param.ranges[j].def = sFluidDefaults[j];
  750. pData->param.ranges[j].step = 1.0f;
  751. pData->param.ranges[j].stepSmall = 1.0f;
  752. pData->param.ranges[j].stepLarge = 1.0f;
  753. // ----------------------
  754. j = FluidSynthPolyphony;
  755. pData->param.data[j].type = PARAMETER_INPUT;
  756. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  757. pData->param.data[j].index = j;
  758. pData->param.data[j].rindex = j;
  759. pData->param.ranges[j].min = 1.0f;
  760. pData->param.ranges[j].max = 512.0f; // max theoric is 65535
  761. pData->param.ranges[j].def = sFluidDefaults[j];
  762. pData->param.ranges[j].step = 1.0f;
  763. pData->param.ranges[j].stepSmall = 1.0f;
  764. pData->param.ranges[j].stepLarge = 10.0f;
  765. // ----------------------
  766. j = FluidSynthInterpolation;
  767. pData->param.data[j].type = PARAMETER_INPUT;
  768. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  769. pData->param.data[j].index = j;
  770. pData->param.data[j].rindex = j;
  771. pData->param.ranges[j].min = FLUID_INTERP_NONE;
  772. pData->param.ranges[j].max = FLUID_INTERP_HIGHEST;
  773. pData->param.ranges[j].def = sFluidDefaults[j];
  774. pData->param.ranges[j].step = 1.0f;
  775. pData->param.ranges[j].stepSmall = 1.0f;
  776. pData->param.ranges[j].stepLarge = 1.0f;
  777. // ----------------------
  778. j = FluidSynthVoiceCount;
  779. pData->param.data[j].type = PARAMETER_OUTPUT;
  780. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  781. pData->param.data[j].index = j;
  782. pData->param.data[j].rindex = j;
  783. pData->param.ranges[j].min = 0.0f;
  784. pData->param.ranges[j].max = 65535.0f;
  785. pData->param.ranges[j].def = 0.0f;
  786. pData->param.ranges[j].step = 1.0f;
  787. pData->param.ranges[j].stepSmall = 1.0f;
  788. pData->param.ranges[j].stepLarge = 1.0f;
  789. for (j=0; j<FluidSynthParametersMax; ++j)
  790. fParamBuffers[j] = pData->param.ranges[j].def;
  791. }
  792. // ---------------------------------------
  793. // plugin hints
  794. pData->hints = 0x0;
  795. pData->hints |= PLUGIN_IS_SYNTH;
  796. pData->hints |= PLUGIN_CAN_VOLUME;
  797. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  798. if (! kUse16Outs)
  799. pData->hints |= PLUGIN_CAN_BALANCE;
  800. // extra plugin hints
  801. pData->extraHints = 0x0;
  802. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  803. bufferSizeChanged(pData->engine->getBufferSize());
  804. reloadPrograms(true);
  805. if (pData->active)
  806. activate();
  807. carla_debug("CarlaPluginFluidSynth::reload() - end");
  808. }
  809. void reloadPrograms(const bool doInit) override
  810. {
  811. carla_debug("CarlaPluginFluidSynth::reloadPrograms(%s)", bool2str(doInit));
  812. // save drum info in case we have one program for it
  813. bool hasDrums = false;
  814. uint32_t drumIndex, drumProg;
  815. drumIndex = drumProg = 0;
  816. // Delete old programs
  817. pData->midiprog.clear();
  818. // Query new programs
  819. uint32_t count = 0;
  820. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId))
  821. {
  822. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  823. fluid_preset_t* f_preset;
  824. // initial check to know how many midi-programs we have
  825. fluid_sfont_iteration_start(f_sfont);
  826. for (; fluid_sfont_iteration_next(f_sfont);)
  827. ++count;
  828. // sound kits must always have at least 1 midi-program
  829. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  830. pData->midiprog.createNew(count);
  831. // Update data
  832. int tmp;
  833. uint32_t i = 0;
  834. fluid_sfont_iteration_start(f_sfont);
  835. for (; (f_preset = fluid_sfont_iteration_next(f_sfont));)
  836. {
  837. CARLA_SAFE_ASSERT_BREAK(i < count);
  838. tmp = fluid_preset_get_banknum(f_preset);
  839. pData->midiprog.data[i].bank = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  840. tmp = fluid_preset_get_num(f_preset);
  841. pData->midiprog.data[i].program = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  842. pData->midiprog.data[i].name = carla_strdup(fluid_preset_get_name(f_preset));
  843. #else
  844. fluid_preset_t f_preset;
  845. // initial check to know how many midi-programs we have
  846. f_sfont->iteration_start(f_sfont);
  847. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  848. ++count;
  849. // sound kits must always have at least 1 midi-program
  850. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  851. pData->midiprog.createNew(count);
  852. // Update data
  853. int tmp;
  854. uint32_t i = 0;
  855. f_sfont->iteration_start(f_sfont);
  856. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  857. {
  858. CARLA_SAFE_ASSERT_BREAK(i < count);
  859. tmp = f_preset.get_banknum(&f_preset);
  860. pData->midiprog.data[i].bank = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  861. tmp = f_preset.get_num(&f_preset);
  862. pData->midiprog.data[i].program = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  863. pData->midiprog.data[i].name = carla_strdup(f_preset.get_name(&f_preset));
  864. #endif
  865. if (pData->midiprog.data[i].bank == 128 && ! hasDrums)
  866. {
  867. hasDrums = true;
  868. drumIndex = i;
  869. drumProg = pData->midiprog.data[i].program;
  870. }
  871. ++i;
  872. }
  873. }
  874. else
  875. {
  876. // failing means 0 midi-programs, it shouldn't happen!
  877. carla_safe_assert("fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId)", __FILE__, __LINE__);
  878. return;
  879. }
  880. if (doInit)
  881. {
  882. fluid_synth_program_reset(fSynth);
  883. // select first program, or 128 for ch10
  884. for (int i=0; i < MAX_MIDI_CHANNELS && i != 9; ++i)
  885. {
  886. fluid_synth_set_channel_type(fSynth, i, CHANNEL_TYPE_MELODIC);
  887. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  888. fluid_synth_program_select(fSynth, i, fSynthId,
  889. static_cast<int>(pData->midiprog.data[0].bank),
  890. static_cast<int>(pData->midiprog.data[0].program));
  891. #else
  892. fluid_synth_program_select(fSynth, i, fSynthId,
  893. pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  894. #endif
  895. fCurMidiProgs[i] = 0;
  896. }
  897. if (hasDrums)
  898. {
  899. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_DRUM);
  900. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  901. fluid_synth_program_select(fSynth, 9, fSynthId, 128, static_cast<int>(drumProg));
  902. #else
  903. fluid_synth_program_select(fSynth, 9, fSynthId, 128, drumProg);
  904. #endif
  905. fCurMidiProgs[9] = static_cast<int32_t>(drumIndex);
  906. }
  907. else
  908. {
  909. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_MELODIC);
  910. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  911. fluid_synth_program_select(fSynth, 9, fSynthId,
  912. static_cast<int>(pData->midiprog.data[0].bank),
  913. static_cast<int>(pData->midiprog.data[0].program));
  914. #else
  915. fluid_synth_program_select(fSynth, 9, fSynthId,
  916. pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  917. #endif
  918. fCurMidiProgs[9] = 0;
  919. }
  920. pData->midiprog.current = 0;
  921. }
  922. else
  923. {
  924. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
  925. }
  926. }
  927. // -------------------------------------------------------------------
  928. // Plugin processing
  929. void process(const float* const* const, float** const audioOut,
  930. const float* const*, float**, const uint32_t frames) override
  931. {
  932. // --------------------------------------------------------------------------------------------------------
  933. // Check if active
  934. if (! pData->active)
  935. {
  936. // disable any output sound
  937. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  938. carla_zeroFloats(audioOut[i], frames);
  939. return;
  940. }
  941. // --------------------------------------------------------------------------------------------------------
  942. // Check if needs reset
  943. if (pData->needsReset)
  944. {
  945. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  946. {
  947. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  948. {
  949. fluid_synth_all_notes_off(fSynth, i);
  950. fluid_synth_all_sounds_off(fSynth, i);
  951. }
  952. }
  953. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  954. {
  955. for (int i=0; i < MAX_MIDI_NOTE; ++i)
  956. fluid_synth_noteoff(fSynth, pData->ctrlChannel, i);
  957. }
  958. pData->needsReset = false;
  959. }
  960. // --------------------------------------------------------------------------------------------------------
  961. // Event Input and Processing
  962. {
  963. // ----------------------------------------------------------------------------------------------------
  964. // MIDI Input (External)
  965. if (pData->extNotes.mutex.tryLock())
  966. {
  967. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  968. {
  969. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  970. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  971. if (note.velo > 0)
  972. fluid_synth_noteon(fSynth, note.channel, note.note, note.velo);
  973. else
  974. fluid_synth_noteoff(fSynth,note.channel, note.note);
  975. }
  976. pData->extNotes.data.clear();
  977. pData->extNotes.mutex.unlock();
  978. } // End of MIDI Input (External)
  979. // ----------------------------------------------------------------------------------------------------
  980. // Event Input (System)
  981. #ifndef BUILD_BRIDGE
  982. bool allNotesOffSent = false;
  983. #endif
  984. uint32_t timeOffset = 0;
  985. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  986. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  987. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  988. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  989. {
  990. const EngineEvent& event(pData->event.portIn->getEvent(i));
  991. uint32_t eventTime = event.time;
  992. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  993. if (eventTime < timeOffset)
  994. {
  995. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  996. eventTime, timeOffset, pData->name);
  997. eventTime = timeOffset;
  998. }
  999. else if (eventTime > timeOffset)
  1000. {
  1001. if (processSingle(audioOut, eventTime - timeOffset, timeOffset))
  1002. {
  1003. timeOffset = eventTime;
  1004. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1005. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  1006. }
  1007. }
  1008. // Control change
  1009. switch (event.type)
  1010. {
  1011. case kEngineEventTypeNull:
  1012. break;
  1013. case kEngineEventTypeControl:
  1014. {
  1015. const EngineControlEvent& ctrlEvent = event.ctrl;
  1016. switch (ctrlEvent.type)
  1017. {
  1018. case kEngineControlEventTypeNull:
  1019. break;
  1020. case kEngineControlEventTypeParameter:
  1021. {
  1022. float value;
  1023. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1024. // Control backend stuff
  1025. if (event.channel == pData->ctrlChannel)
  1026. {
  1027. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1028. {
  1029. value = ctrlEvent.value;
  1030. setDryWetRT(value, true);
  1031. }
  1032. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1033. {
  1034. value = ctrlEvent.value*127.0f/100.0f;
  1035. setVolumeRT(value, true);
  1036. }
  1037. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1038. {
  1039. float left, right;
  1040. value = ctrlEvent.value/0.5f - 1.0f;
  1041. if (value < 0.0f)
  1042. {
  1043. left = -1.0f;
  1044. right = (value*2.0f)+1.0f;
  1045. }
  1046. else if (value > 0.0f)
  1047. {
  1048. left = (value*2.0f)-1.0f;
  1049. right = 1.0f;
  1050. }
  1051. else
  1052. {
  1053. left = -1.0f;
  1054. right = 1.0f;
  1055. }
  1056. setBalanceLeftRT(left, true);
  1057. setBalanceRightRT(right, true);
  1058. }
  1059. }
  1060. #endif
  1061. // Control plugin parameters
  1062. for (uint32_t k=0; k < pData->param.count; ++k)
  1063. {
  1064. if (pData->param.data[k].midiChannel != event.channel)
  1065. continue;
  1066. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1067. continue;
  1068. if (pData->param.data[k].hints != PARAMETER_INPUT)
  1069. continue;
  1070. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1071. continue;
  1072. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  1073. setParameterValueRT(k, value, true);
  1074. }
  1075. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1076. {
  1077. fluid_synth_cc(fSynth, event.channel, ctrlEvent.param, int(ctrlEvent.value*127.0f));
  1078. }
  1079. break;
  1080. }
  1081. case kEngineControlEventTypeMidiBank:
  1082. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1083. nextBankIds[event.channel] = ctrlEvent.param;
  1084. break;
  1085. case kEngineControlEventTypeMidiProgram:
  1086. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1087. {
  1088. const uint32_t bankId(nextBankIds[event.channel]);
  1089. const uint32_t progId(ctrlEvent.param);
  1090. // TODO int32_t midiprog.find(bank, prog)
  1091. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1092. {
  1093. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  1094. {
  1095. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1096. fluid_synth_program_select(fSynth, event.channel, fSynthId,
  1097. static_cast<int>(bankId), static_cast<int>(progId));
  1098. #else
  1099. fluid_synth_program_select(fSynth, event.channel, fSynthId, bankId, progId);
  1100. #endif
  1101. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1102. if (event.channel == pData->ctrlChannel)
  1103. {
  1104. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange,
  1105. true,
  1106. static_cast<int32_t>(k),
  1107. 0, 0, 0.0f);
  1108. }
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. break;
  1114. case kEngineControlEventTypeAllSoundOff:
  1115. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1116. fluid_synth_all_sounds_off(fSynth, event.channel);
  1117. break;
  1118. case kEngineControlEventTypeAllNotesOff:
  1119. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1120. {
  1121. #ifndef BUILD_BRIDGE
  1122. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1123. {
  1124. allNotesOffSent = true;
  1125. postponeRtAllNotesOff();
  1126. }
  1127. #endif
  1128. fluid_synth_all_notes_off(fSynth, event.channel);
  1129. }
  1130. break;
  1131. }
  1132. break;
  1133. }
  1134. case kEngineEventTypeMidi: {
  1135. const EngineMidiEvent& midiEvent(event.midi);
  1136. if (midiEvent.size > EngineMidiEvent::kDataSize)
  1137. continue;
  1138. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1139. // Fix bad note-off
  1140. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1141. status = MIDI_STATUS_NOTE_OFF;
  1142. switch (status)
  1143. {
  1144. case MIDI_STATUS_NOTE_OFF: {
  1145. const uint8_t note = midiEvent.data[1];
  1146. fluid_synth_noteoff(fSynth, event.channel, note);
  1147. pData->postponeRtEvent(kPluginPostRtEventNoteOff, true, event.channel, note, 0, 0.0f);
  1148. break;
  1149. }
  1150. case MIDI_STATUS_NOTE_ON: {
  1151. const uint8_t note = midiEvent.data[1];
  1152. const uint8_t velo = midiEvent.data[2];
  1153. fluid_synth_noteon(fSynth, event.channel, note, velo);
  1154. pData->postponeRtEvent(kPluginPostRtEventNoteOn, true, event.channel, note, velo, 0.0f);
  1155. break;
  1156. }
  1157. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1158. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1159. {
  1160. //const uint8_t note = midiEvent.data[1];
  1161. //const uint8_t pressure = midiEvent.data[2];
  1162. // not in fluidsynth API
  1163. }
  1164. break;
  1165. case MIDI_STATUS_CONTROL_CHANGE:
  1166. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1167. {
  1168. const uint8_t control = midiEvent.data[1];
  1169. const uint8_t value = midiEvent.data[2];
  1170. fluid_synth_cc(fSynth, event.channel, control, value);
  1171. }
  1172. break;
  1173. case MIDI_STATUS_CHANNEL_PRESSURE:
  1174. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1175. {
  1176. const uint8_t pressure = midiEvent.data[1];
  1177. fluid_synth_channel_pressure(fSynth, event.channel, pressure);;
  1178. }
  1179. break;
  1180. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1181. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1182. {
  1183. const uint8_t lsb = midiEvent.data[1];
  1184. const uint8_t msb = midiEvent.data[2];
  1185. const int value = ((msb << 7) | lsb);
  1186. fluid_synth_pitch_bend(fSynth, event.channel, value);
  1187. }
  1188. break;
  1189. default:
  1190. continue;
  1191. break;
  1192. } // switch (status)
  1193. } break;
  1194. } // switch (event.type)
  1195. }
  1196. pData->postRtEvents.trySplice();
  1197. if (frames > timeOffset)
  1198. processSingle(audioOut, frames - timeOffset, timeOffset);
  1199. } // End of Event Input and Processing
  1200. #ifndef BUILD_BRIDGE
  1201. // --------------------------------------------------------------------------------------------------------
  1202. // Control Output
  1203. {
  1204. uint32_t k = FluidSynthVoiceCount;
  1205. fParamBuffers[k] = float(fluid_synth_get_active_voice_count(fSynth));
  1206. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1207. if (pData->param.data[k].mappedControlIndex > 0)
  1208. {
  1209. float value(pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]));
  1210. pData->event.portOut->writeControlEvent(0,
  1211. pData->param.data[k].midiChannel,
  1212. kEngineControlEventTypeParameter,
  1213. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1214. value);
  1215. }
  1216. } // End of Control Output
  1217. #endif
  1218. }
  1219. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1220. {
  1221. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1222. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1223. // --------------------------------------------------------------------------------------------------------
  1224. // Try lock, silence otherwise
  1225. #ifndef STOAT_TEST_BUILD
  1226. if (pData->engine->isOffline())
  1227. {
  1228. pData->singleMutex.lock();
  1229. }
  1230. else
  1231. #endif
  1232. if (! pData->singleMutex.tryLock())
  1233. {
  1234. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1235. {
  1236. for (uint32_t k=0; k < frames; ++k)
  1237. outBuffer[i][k+timeOffset] = 0.0f;
  1238. }
  1239. return false;
  1240. }
  1241. // --------------------------------------------------------------------------------------------------------
  1242. // Fill plugin buffers and Run plugin
  1243. if (kUse16Outs)
  1244. {
  1245. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1246. carla_zeroFloats(fAudio16Buffers[i], frames);
  1247. // FIXME use '32' or '16' instead of outs
  1248. fluid_synth_process(fSynth, static_cast<int>(frames),
  1249. 0, nullptr,
  1250. static_cast<int>(pData->audioOut.count), fAudio16Buffers);
  1251. }
  1252. else
  1253. {
  1254. fluid_synth_write_float(fSynth, static_cast<int>(frames),
  1255. outBuffer[0] + timeOffset, 0, 1,
  1256. outBuffer[1] + timeOffset, 0, 1);
  1257. }
  1258. #ifndef BUILD_BRIDGE
  1259. // --------------------------------------------------------------------------------------------------------
  1260. // Post-processing (volume and balance)
  1261. {
  1262. // note - balance not possible with kUse16Outs, so we can safely skip fAudioOutBuffers
  1263. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1264. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1265. float oldBufLeft[doBalance ? frames : 1];
  1266. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1267. {
  1268. // Balance
  1269. if (doBalance)
  1270. {
  1271. if (i % 2 == 0)
  1272. carla_copyFloats(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1273. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1274. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1275. for (uint32_t k=0; k < frames; ++k)
  1276. {
  1277. if (i % 2 == 0)
  1278. {
  1279. // left
  1280. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1281. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1282. }
  1283. else
  1284. {
  1285. // right
  1286. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1287. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1288. }
  1289. }
  1290. }
  1291. // Volume
  1292. if (kUse16Outs)
  1293. {
  1294. for (uint32_t k=0; k < frames; ++k)
  1295. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k] * pData->postProc.volume;
  1296. }
  1297. else if (doVolume)
  1298. {
  1299. for (uint32_t k=0; k < frames; ++k)
  1300. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1301. }
  1302. }
  1303. } // End of Post-processing
  1304. #else
  1305. if (kUse16Outs)
  1306. {
  1307. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1308. {
  1309. for (uint32_t k=0; k < frames; ++k)
  1310. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k];
  1311. }
  1312. }
  1313. #endif
  1314. // --------------------------------------------------------------------------------------------------------
  1315. pData->singleMutex.unlock();
  1316. return true;
  1317. }
  1318. void bufferSizeChanged(const uint32_t newBufferSize) override
  1319. {
  1320. if (! kUse16Outs)
  1321. return;
  1322. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1323. {
  1324. if (fAudio16Buffers[i] != nullptr)
  1325. delete[] fAudio16Buffers[i];
  1326. fAudio16Buffers[i] = new float[newBufferSize];
  1327. }
  1328. }
  1329. void sampleRateChanged(const double newSampleRate) override
  1330. {
  1331. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  1332. fluid_settings_setnum(fSettings, "synth.sample-rate", newSampleRate);
  1333. #if FLUIDSYNTH_VERSION_MAJOR < 2
  1334. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  1335. fluid_synth_set_sample_rate(fSynth, static_cast<float>(newSampleRate));
  1336. #endif
  1337. }
  1338. // -------------------------------------------------------------------
  1339. // Plugin buffers
  1340. void clearBuffers() noexcept override
  1341. {
  1342. carla_debug("CarlaPluginFluidSynth::clearBuffers() - start");
  1343. if (fAudio16Buffers != nullptr)
  1344. {
  1345. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1346. {
  1347. if (fAudio16Buffers[i] != nullptr)
  1348. {
  1349. delete[] fAudio16Buffers[i];
  1350. fAudio16Buffers[i] = nullptr;
  1351. }
  1352. }
  1353. delete[] fAudio16Buffers;
  1354. fAudio16Buffers = nullptr;
  1355. }
  1356. CarlaPlugin::clearBuffers();
  1357. carla_debug("CarlaPluginFluidSynth::clearBuffers() - end");
  1358. }
  1359. // -------------------------------------------------------------------
  1360. const void* getExtraStuff() const noexcept override
  1361. {
  1362. static const char xtrue[] = "true";
  1363. static const char xfalse[] = "false";
  1364. return kUse16Outs ? xtrue : xfalse;
  1365. }
  1366. // -------------------------------------------------------------------
  1367. bool init(const char* const filename, const char* const name, const char* const label, const uint options)
  1368. {
  1369. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1370. // ---------------------------------------------------------------
  1371. // first checks
  1372. if (pData->client != nullptr)
  1373. {
  1374. pData->engine->setLastError("Plugin client is already registered");
  1375. return false;
  1376. }
  1377. if (fSynth == nullptr)
  1378. {
  1379. pData->engine->setLastError("null synth");
  1380. return false;
  1381. }
  1382. if (filename == nullptr || filename[0] == '\0')
  1383. {
  1384. pData->engine->setLastError("null filename");
  1385. return false;
  1386. }
  1387. if (label == nullptr || label[0] == '\0')
  1388. {
  1389. pData->engine->setLastError("null label");
  1390. return false;
  1391. }
  1392. // ---------------------------------------------------------------
  1393. // open soundfont
  1394. const int synthId = fluid_synth_sfload(fSynth, filename, 0);
  1395. if (synthId < 0)
  1396. {
  1397. pData->engine->setLastError("Failed to load SoundFont file");
  1398. return false;
  1399. }
  1400. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1401. fSynthId = synthId;
  1402. #else
  1403. fSynthId = static_cast<uint>(synthId);
  1404. #endif
  1405. // ---------------------------------------------------------------
  1406. // get info
  1407. CarlaString label2(label);
  1408. if (kUse16Outs && ! label2.endsWith(" (16 outs)"))
  1409. label2 += " (16 outs)";
  1410. fLabel = label2.dup();
  1411. pData->filename = carla_strdup(filename);
  1412. if (name != nullptr && name[0] != '\0')
  1413. pData->name = pData->engine->getUniquePluginName(name);
  1414. else
  1415. pData->name = pData->engine->getUniquePluginName(label);
  1416. // ---------------------------------------------------------------
  1417. // register client
  1418. pData->client = pData->engine->addClient(this);
  1419. if (pData->client == nullptr || ! pData->client->isOk())
  1420. {
  1421. pData->engine->setLastError("Failed to register plugin client");
  1422. return false;
  1423. }
  1424. // ---------------------------------------------------------------
  1425. // set options
  1426. pData->options = 0x0;
  1427. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  1428. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1429. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  1430. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1431. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  1432. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1433. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  1434. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1435. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  1436. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1437. return true;
  1438. }
  1439. private:
  1440. void initializeFluidDefaultsIfNeeded()
  1441. {
  1442. if (sFluidDefaultsStored)
  1443. return;
  1444. sFluidDefaultsStored = true;
  1445. // reverb defaults
  1446. sFluidDefaults[FluidSynthReverbOnOff] = 1.0f;
  1447. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1448. double reverbVal;
  1449. reverbVal = 0.2;
  1450. fluid_settings_getnum_default(fSettings, "synth.reverb.room-size", &reverbVal);
  1451. sFluidDefaults[FluidSynthReverbRoomSize] = static_cast<float>(reverbVal);
  1452. reverbVal = 0.0;
  1453. fluid_settings_getnum_default(fSettings, "synth.reverb.damp", &reverbVal);
  1454. sFluidDefaults[FluidSynthReverbDamp] = static_cast<float>(reverbVal);
  1455. reverbVal = 0.9;
  1456. fluid_settings_getnum_default(fSettings, "synth.reverb.level", &reverbVal);
  1457. sFluidDefaults[FluidSynthReverbLevel] = static_cast<float>(reverbVal);
  1458. reverbVal = 0.5;
  1459. fluid_settings_getnum_default(fSettings, "synth.reverb.width", &reverbVal);
  1460. sFluidDefaults[FluidSynthReverbWidth] = static_cast<float>(reverbVal);
  1461. #else
  1462. sFluidDefaults[FluidSynthReverbRoomSize] = FLUID_REVERB_DEFAULT_ROOMSIZE;
  1463. sFluidDefaults[FluidSynthReverbDamp] = FLUID_REVERB_DEFAULT_DAMP;
  1464. sFluidDefaults[FluidSynthReverbLevel] = FLUID_REVERB_DEFAULT_LEVEL;
  1465. sFluidDefaults[FluidSynthReverbWidth] = FLUID_REVERB_DEFAULT_WIDTH;
  1466. #endif
  1467. // chorus defaults
  1468. sFluidDefaults[FluidSynthChorusOnOff] = 1.0f;
  1469. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1470. double chorusVal;
  1471. chorusVal = 3.0;
  1472. fluid_settings_getnum_default(fSettings, "synth.chorus.nr", &chorusVal);
  1473. sFluidDefaults[FluidSynthChorusNr] = static_cast<float>(chorusVal);
  1474. chorusVal = 2.0;
  1475. fluid_settings_getnum_default(fSettings, "synth.chorus.level", &chorusVal);
  1476. sFluidDefaults[FluidSynthChorusLevel] = static_cast<float>(chorusVal);
  1477. chorusVal = 0.3;
  1478. fluid_settings_getnum_default(fSettings, "synth.chorus.speed", &chorusVal);
  1479. sFluidDefaults[FluidSynthChorusSpeedHz] = static_cast<float>(chorusVal);
  1480. chorusVal = 8.0;
  1481. fluid_settings_getnum_default(fSettings, "synth.chorus.depth", &chorusVal);
  1482. sFluidDefaults[FluidSynthChorusDepthMs] = static_cast<float>(chorusVal);
  1483. // There is no settings for chorus default type
  1484. sFluidDefaults[FluidSynthChorusType] = static_cast<float>(fluid_synth_get_chorus_type(fSynth));
  1485. #else
  1486. sFluidDefaults[FluidSynthChorusNr] = FLUID_CHORUS_DEFAULT_N;
  1487. sFluidDefaults[FluidSynthChorusLevel] = FLUID_CHORUS_DEFAULT_LEVEL;
  1488. sFluidDefaults[FluidSynthChorusSpeedHz] = FLUID_CHORUS_DEFAULT_SPEED;
  1489. sFluidDefaults[FluidSynthChorusDepthMs] = FLUID_CHORUS_DEFAULT_DEPTH;
  1490. sFluidDefaults[FluidSynthChorusType] = FLUID_CHORUS_DEFAULT_TYPE;
  1491. #endif
  1492. // misc. defaults
  1493. sFluidDefaults[FluidSynthInterpolation] = FLUID_INTERP_DEFAULT;
  1494. }
  1495. enum FluidSynthParameters {
  1496. FluidSynthReverbOnOff = 0,
  1497. FluidSynthReverbRoomSize = 1,
  1498. FluidSynthReverbDamp = 2,
  1499. FluidSynthReverbLevel = 3,
  1500. FluidSynthReverbWidth = 4,
  1501. FluidSynthChorusOnOff = 5,
  1502. FluidSynthChorusNr = 6,
  1503. FluidSynthChorusLevel = 7,
  1504. FluidSynthChorusSpeedHz = 8,
  1505. FluidSynthChorusDepthMs = 9,
  1506. FluidSynthChorusType = 10,
  1507. FluidSynthPolyphony = 11,
  1508. FluidSynthInterpolation = 12,
  1509. FluidSynthVoiceCount = 13,
  1510. FluidSynthParametersMax = 14
  1511. };
  1512. const bool kUse16Outs;
  1513. fluid_settings_t* fSettings;
  1514. fluid_synth_t* fSynth;
  1515. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1516. int fSynthId;
  1517. #else
  1518. uint fSynthId;
  1519. #endif
  1520. float** fAudio16Buffers;
  1521. float fParamBuffers[FluidSynthParametersMax];
  1522. static bool sFluidDefaultsStored;
  1523. static float sFluidDefaults[FluidSynthParametersMax];
  1524. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1525. const char* fLabel;
  1526. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginFluidSynth)
  1527. };
  1528. bool CarlaPluginFluidSynth::sFluidDefaultsStored = false;
  1529. float CarlaPluginFluidSynth::sFluidDefaults[FluidSynthParametersMax] = {
  1530. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  1531. };
  1532. CARLA_BACKEND_END_NAMESPACE
  1533. #endif // HAVE_FLUIDSYNTH
  1534. CARLA_BACKEND_START_NAMESPACE
  1535. // -------------------------------------------------------------------------------------------------------------------
  1536. CarlaPlugin* CarlaPlugin::newFluidSynth(const Initializer& init, PluginType ptype, bool use16Outs)
  1537. {
  1538. carla_debug("CarlaPlugin::newFluidSynth({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)",
  1539. init.engine, init.filename, init.name, init.label, init.uniqueId, bool2str(use16Outs));
  1540. #ifdef HAVE_FLUIDSYNTH
  1541. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1542. use16Outs = false;
  1543. if (ptype == PLUGIN_SF2 && ! fluid_is_soundfont(init.filename))
  1544. {
  1545. init.engine->setLastError("Requested file is not a valid SoundFont");
  1546. return nullptr;
  1547. }
  1548. #ifndef HAVE_FLUIDSYNTH_INSTPATCH
  1549. if (ptype == PLUGIN_DLS)
  1550. {
  1551. init.engine->setLastError("DLS file support not available");
  1552. return nullptr;
  1553. }
  1554. if (ptype == PLUGIN_GIG)
  1555. {
  1556. init.engine->setLastError("GIG file support not available");
  1557. return nullptr;
  1558. }
  1559. #endif
  1560. CarlaPluginFluidSynth* const plugin(new CarlaPluginFluidSynth(init.engine, init.id, use16Outs));
  1561. if (! plugin->init(init.filename, init.name, init.label, init.options))
  1562. {
  1563. delete plugin;
  1564. return nullptr;
  1565. }
  1566. return plugin;
  1567. #else
  1568. init.engine->setLastError("fluidsynth support not available");
  1569. return nullptr;
  1570. // unused
  1571. (void)use16Outs;
  1572. #endif
  1573. }
  1574. // -------------------------------------------------------------------------------------------------------------------
  1575. CARLA_BACKEND_END_NAMESPACE