22#include "../safeguards.h"
38 "/usr/share/soundfonts/default.sf2",
42 "/usr/share/sounds/sf3/default-GM.sf3",
45 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
48 "/usr/share/soundfonts/FluidR3_GM.sf2",
51 "/usr/share/sounds/sf2/TimGM6mb.sf2",
52 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
55static void RenderMusicStream(int16_t *buffer,
size_t samples)
57 std::unique_lock<std::mutex>
lock{
_midi.synth_mutex, std::try_to_lock};
59 if (!
lock.owns_lock() ||
_midi.synth ==
nullptr ||
_midi.player ==
nullptr)
return;
60 fluid_synth_write_s16(
_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
63static void load_and_execute_config_file(fluid_cmd_handler_t *cmd_handler,
const char *config_file)
65 if (std::filesystem::exists(config_file)) {
66 Debug(driver, 2,
"Fluidsynth: Attempting to load config file '{}'", config_file);
67 if (fluid_source(cmd_handler, config_file) < 0) {
68 Debug(driver, 0,
"Fluidsynth: Failed to execute command configuration file '{}'", config_file);
71 Debug(driver, 1,
"Fluidsynth: Failed to load config file '{}' - file doesn't exist", config_file);
77 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex};
82 Debug(driver, 1,
"Fluidsynth: sf {}", sfont_name.has_value() ? *sfont_name :
"(null)");
85 _midi.settings = new_fluid_settings();
86 if (
_midi.settings ==
nullptr)
return "Could not create midi settings";
89 fluid_cmd_handler_t *cmd_handler = new_fluid_cmd_handler2(
_midi.settings,
nullptr,
nullptr,
nullptr);
90 if (cmd_handler == NULL)
return "Failed to create the early command handler";
92 char buf[MAX_PATH] = {};
93 const char *config_file = fluid_get_sysconf(buf,
sizeof(buf));
94 load_and_execute_config_file(cmd_handler, config_file);
95 config_file = fluid_get_userconf(buf,
sizeof(buf));
96 load_and_execute_config_file(cmd_handler, config_file);
98 delete_fluid_cmd_handler(cmd_handler);
101 fluid_settings_setint(
_midi.settings,
"synth.lock-memory", 0);
105 fluid_settings_setnum(
_midi.settings,
"synth.sample-rate", samplerate);
106 Debug(driver, 1,
"Fluidsynth: samplerate {:.0f}", (
float)samplerate);
109 _midi.synth = new_fluid_synth(
_midi.settings);
110 if (
_midi.synth ==
nullptr)
return "Could not open synth";
114 if (!sfont_name.has_value()) {
115 sfont_id = FLUID_FAILED;
118 char *default_soundfont =
nullptr;
119 fluid_settings_dupstr(
_midi.settings,
"synth.default-soundfont", &default_soundfont);
120 if (default_soundfont !=
nullptr && std::filesystem::exists(default_soundfont) && fluid_is_soundfont(default_soundfont)) {
121 sfont_id = fluid_synth_sfload(
_midi.synth, default_soundfont, 1);
123 fluid_free(default_soundfont);
126 if (sfont_id == FLUID_FAILED) {
128 if (!std::filesystem::exists(soundfont) || !fluid_is_soundfont(soundfont))
continue;
129 sfont_id = fluid_synth_sfload(
_midi.synth, soundfont, 1);
130 if (sfont_id != FLUID_FAILED)
break;
133 if (sfont_id == FLUID_FAILED)
return "Could not open any sound font";
135 std::string name{sfont_name.value()};
136 sfont_id = fluid_synth_sfload(
_midi.synth, name.c_str(), 1);
137 if (sfont_id == FLUID_FAILED)
return "Could not open sound font";
140 _midi.player =
nullptr;
149 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex};
151 if (
_midi.player !=
nullptr) delete_fluid_player(
_midi.player);
152 _midi.player =
nullptr;
154 if (
_midi.synth !=
nullptr) delete_fluid_synth(
_midi.synth);
155 _midi.synth =
nullptr;
157 if (
_midi.settings !=
nullptr) delete_fluid_settings(
_midi.settings);
158 _midi.settings =
nullptr;
167 if (filename.empty()) {
171 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex};
174 if (
_midi.player ==
nullptr) {
175 Debug(driver, 0,
"Could not create midi player");
179 if (fluid_player_add(
_midi.player, filename.c_str()) != FLUID_OK) {
180 Debug(driver, 0,
"Could not open music file");
181 delete_fluid_player(
_midi.player);
182 _midi.player =
nullptr;
185 if (fluid_player_play(
_midi.player) != FLUID_OK) {
186 Debug(driver, 0,
"Could not start midi player");
187 delete_fluid_player(
_midi.player);
188 _midi.player =
nullptr;
195 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex};
197 if (
_midi.player ==
nullptr)
return;
199 fluid_player_stop(
_midi.player);
201 delete_fluid_player(
_midi.player);
202 fluid_synth_system_reset(
_midi.synth);
203 fluid_synth_all_sounds_off(
_midi.synth, -1);
204 _midi.player =
nullptr;
209 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex};
210 if (
_midi.player ==
nullptr)
return false;
212 return fluid_player_get_status(
_midi.player) == FLUID_PLAYER_PLAYING;
217 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex};
218 if (
_midi.settings ==
nullptr)
return;
224 double gain = (1.0 * vol) / (128.0 * 5.0);
225 if (fluid_settings_setnum(
_midi.settings,
"synth.gain", gain) != FLUID_OK) {
226 Debug(driver, 0,
"Could not set volume");
Factory for the fluidsynth driver.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
bool IsSongPlaying() override
Are we currently playing a song?
void StopSong() override
Stop playing the current song.
void SetVolume(uint8_t vol) override
Set the volume, if possible.
void Stop() override
Stop this driver.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
std::optional< std::string_view > GetDriverParam(const StringList &parm, std::string_view name)
Get a string parameter the list of parameters.
static struct @13 _midi
Metadata about the midi we're playing.
std::mutex synth_mutex
Guard mutex for synth access.
static FMusicDriver_FluidSynth iFMusicDriver_FluidSynth
Factory for the FluidSynth driver.
static const char * _default_soundfonts[]
List of sound fonts to try by default.
fluid_player_t * player
FluidSynth MIDI player handle.
fluid_synth_t * synth
FluidSynth synthesizer handle.
fluid_settings_t * settings
FluidSynth settings handle.
Base for FluidSynth music playback.
Parser for standard MIDI files.
uint32_t MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
std::vector< std::string > StringList
Type for a list of strings.
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Metadata about a music track.
std::mutex lock
synchronization for playback status fields