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

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