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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. /*
  2. * Carla FluidSynth Plugin
  3. * Copyright (C) 2011-2020 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(true);
  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 CarlaPluginPtr plugin,
  1368. const char* const filename, const char* const name, const char* const label, const uint options)
  1369. {
  1370. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1371. // ---------------------------------------------------------------
  1372. // first checks
  1373. if (pData->client != nullptr)
  1374. {
  1375. pData->engine->setLastError("Plugin client is already registered");
  1376. return false;
  1377. }
  1378. if (fSynth == nullptr)
  1379. {
  1380. pData->engine->setLastError("null synth");
  1381. return false;
  1382. }
  1383. if (filename == nullptr || filename[0] == '\0')
  1384. {
  1385. pData->engine->setLastError("null filename");
  1386. return false;
  1387. }
  1388. if (label == nullptr || label[0] == '\0')
  1389. {
  1390. pData->engine->setLastError("null label");
  1391. return false;
  1392. }
  1393. // ---------------------------------------------------------------
  1394. // open soundfont
  1395. const int synthId = fluid_synth_sfload(fSynth, filename, 0);
  1396. if (synthId < 0)
  1397. {
  1398. pData->engine->setLastError("Failed to load SoundFont file");
  1399. return false;
  1400. }
  1401. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1402. fSynthId = synthId;
  1403. #else
  1404. fSynthId = static_cast<uint>(synthId);
  1405. #endif
  1406. // ---------------------------------------------------------------
  1407. // get info
  1408. CarlaString label2(label);
  1409. if (kUse16Outs && ! label2.endsWith(" (16 outs)"))
  1410. label2 += " (16 outs)";
  1411. fLabel = label2.dup();
  1412. pData->filename = carla_strdup(filename);
  1413. if (name != nullptr && name[0] != '\0')
  1414. pData->name = pData->engine->getUniquePluginName(name);
  1415. else
  1416. pData->name = pData->engine->getUniquePluginName(label);
  1417. // ---------------------------------------------------------------
  1418. // register client
  1419. pData->client = pData->engine->addClient(plugin);
  1420. if (pData->client == nullptr || ! pData->client->isOk())
  1421. {
  1422. pData->engine->setLastError("Failed to register plugin client");
  1423. return false;
  1424. }
  1425. // ---------------------------------------------------------------
  1426. // set options
  1427. pData->options = 0x0;
  1428. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  1429. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1430. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  1431. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1432. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  1433. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1434. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  1435. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1436. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  1437. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1438. return true;
  1439. }
  1440. private:
  1441. void initializeFluidDefaultsIfNeeded()
  1442. {
  1443. if (sFluidDefaultsStored)
  1444. return;
  1445. sFluidDefaultsStored = true;
  1446. // reverb defaults
  1447. sFluidDefaults[FluidSynthReverbOnOff] = 1.0f;
  1448. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1449. double reverbVal;
  1450. reverbVal = 0.2;
  1451. fluid_settings_getnum_default(fSettings, "synth.reverb.room-size", &reverbVal);
  1452. sFluidDefaults[FluidSynthReverbRoomSize] = static_cast<float>(reverbVal);
  1453. reverbVal = 0.0;
  1454. fluid_settings_getnum_default(fSettings, "synth.reverb.damp", &reverbVal);
  1455. sFluidDefaults[FluidSynthReverbDamp] = static_cast<float>(reverbVal);
  1456. reverbVal = 0.9;
  1457. fluid_settings_getnum_default(fSettings, "synth.reverb.level", &reverbVal);
  1458. sFluidDefaults[FluidSynthReverbLevel] = static_cast<float>(reverbVal);
  1459. reverbVal = 0.5;
  1460. fluid_settings_getnum_default(fSettings, "synth.reverb.width", &reverbVal);
  1461. sFluidDefaults[FluidSynthReverbWidth] = static_cast<float>(reverbVal);
  1462. #else
  1463. sFluidDefaults[FluidSynthReverbRoomSize] = FLUID_REVERB_DEFAULT_ROOMSIZE;
  1464. sFluidDefaults[FluidSynthReverbDamp] = FLUID_REVERB_DEFAULT_DAMP;
  1465. sFluidDefaults[FluidSynthReverbLevel] = FLUID_REVERB_DEFAULT_LEVEL;
  1466. sFluidDefaults[FluidSynthReverbWidth] = FLUID_REVERB_DEFAULT_WIDTH;
  1467. #endif
  1468. // chorus defaults
  1469. sFluidDefaults[FluidSynthChorusOnOff] = 1.0f;
  1470. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1471. double chorusVal;
  1472. chorusVal = 3.0;
  1473. fluid_settings_getnum_default(fSettings, "synth.chorus.nr", &chorusVal);
  1474. sFluidDefaults[FluidSynthChorusNr] = static_cast<float>(chorusVal);
  1475. chorusVal = 2.0;
  1476. fluid_settings_getnum_default(fSettings, "synth.chorus.level", &chorusVal);
  1477. sFluidDefaults[FluidSynthChorusLevel] = static_cast<float>(chorusVal);
  1478. chorusVal = 0.3;
  1479. fluid_settings_getnum_default(fSettings, "synth.chorus.speed", &chorusVal);
  1480. sFluidDefaults[FluidSynthChorusSpeedHz] = static_cast<float>(chorusVal);
  1481. chorusVal = 8.0;
  1482. fluid_settings_getnum_default(fSettings, "synth.chorus.depth", &chorusVal);
  1483. sFluidDefaults[FluidSynthChorusDepthMs] = static_cast<float>(chorusVal);
  1484. // There is no settings for chorus default type
  1485. sFluidDefaults[FluidSynthChorusType] = static_cast<float>(fluid_synth_get_chorus_type(fSynth));
  1486. #else
  1487. sFluidDefaults[FluidSynthChorusNr] = FLUID_CHORUS_DEFAULT_N;
  1488. sFluidDefaults[FluidSynthChorusLevel] = FLUID_CHORUS_DEFAULT_LEVEL;
  1489. sFluidDefaults[FluidSynthChorusSpeedHz] = FLUID_CHORUS_DEFAULT_SPEED;
  1490. sFluidDefaults[FluidSynthChorusDepthMs] = FLUID_CHORUS_DEFAULT_DEPTH;
  1491. sFluidDefaults[FluidSynthChorusType] = FLUID_CHORUS_DEFAULT_TYPE;
  1492. #endif
  1493. // misc. defaults
  1494. sFluidDefaults[FluidSynthInterpolation] = FLUID_INTERP_DEFAULT;
  1495. sFluidDefaults[FluidSynthPolyphony] = FLUID_DEFAULT_POLYPHONY;
  1496. }
  1497. enum FluidSynthParameters {
  1498. FluidSynthReverbOnOff = 0,
  1499. FluidSynthReverbRoomSize = 1,
  1500. FluidSynthReverbDamp = 2,
  1501. FluidSynthReverbLevel = 3,
  1502. FluidSynthReverbWidth = 4,
  1503. FluidSynthChorusOnOff = 5,
  1504. FluidSynthChorusNr = 6,
  1505. FluidSynthChorusLevel = 7,
  1506. FluidSynthChorusSpeedHz = 8,
  1507. FluidSynthChorusDepthMs = 9,
  1508. FluidSynthChorusType = 10,
  1509. FluidSynthPolyphony = 11,
  1510. FluidSynthInterpolation = 12,
  1511. FluidSynthVoiceCount = 13,
  1512. FluidSynthParametersMax = 14
  1513. };
  1514. const bool kUse16Outs;
  1515. fluid_settings_t* fSettings;
  1516. fluid_synth_t* fSynth;
  1517. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1518. int fSynthId;
  1519. #else
  1520. uint fSynthId;
  1521. #endif
  1522. float** fAudio16Buffers;
  1523. float fParamBuffers[FluidSynthParametersMax];
  1524. static bool sFluidDefaultsStored;
  1525. static float sFluidDefaults[FluidSynthParametersMax];
  1526. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1527. const char* fLabel;
  1528. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginFluidSynth)
  1529. };
  1530. bool CarlaPluginFluidSynth::sFluidDefaultsStored = false;
  1531. float CarlaPluginFluidSynth::sFluidDefaults[FluidSynthParametersMax] = {
  1532. 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,
  1533. };
  1534. CARLA_BACKEND_END_NAMESPACE
  1535. #endif // HAVE_FLUIDSYNTH
  1536. CARLA_BACKEND_START_NAMESPACE
  1537. // -------------------------------------------------------------------------------------------------------------------
  1538. CarlaPluginPtr CarlaPlugin::newFluidSynth(const Initializer& init, PluginType ptype, bool use16Outs)
  1539. {
  1540. carla_debug("CarlaPlugin::newFluidSynth({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)",
  1541. init.engine, init.filename, init.name, init.label, init.uniqueId, bool2str(use16Outs));
  1542. #ifdef HAVE_FLUIDSYNTH
  1543. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1544. use16Outs = false;
  1545. if (ptype == PLUGIN_SF2 && ! fluid_is_soundfont(init.filename))
  1546. {
  1547. init.engine->setLastError("Requested file is not a valid SoundFont");
  1548. return nullptr;
  1549. }
  1550. #ifndef HAVE_FLUIDSYNTH_INSTPATCH
  1551. if (ptype == PLUGIN_DLS)
  1552. {
  1553. init.engine->setLastError("DLS file support not available");
  1554. return nullptr;
  1555. }
  1556. if (ptype == PLUGIN_GIG)
  1557. {
  1558. init.engine->setLastError("GIG file support not available");
  1559. return nullptr;
  1560. }
  1561. #endif
  1562. std::shared_ptr<CarlaPluginFluidSynth> plugin(new CarlaPluginFluidSynth(init.engine, init.id, use16Outs));
  1563. if (! plugin->init(plugin, init.filename, init.name, init.label, init.options))
  1564. return nullptr;
  1565. return plugin;
  1566. #else
  1567. init.engine->setLastError("fluidsynth support not available");
  1568. return nullptr;
  1569. // unused
  1570. (void)ptype;
  1571. (void)use16Outs;
  1572. #endif
  1573. }
  1574. // -------------------------------------------------------------------------------------------------------------------
  1575. CARLA_BACKEND_END_NAMESPACE