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.

FluidSynthPlugin.cpp 64KB

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