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

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