10 #include "../stdafx.h" 11 #include "../openttd.h" 12 #include "../sound_type.h" 15 #include "midifile.hpp" 31 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
34 "/usr/share/soundfonts/FluidR3_GM.sf2",
37 "/usr/share/sounds/sf2/TimGM6mb.sf2",
38 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
43 static void RenderMusicStream(int16 *buffer,
size_t samples)
46 fluid_synth_write_s16(
_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
54 DEBUG(driver, 1,
"Fluidsynth: sf %s", sfont_name);
57 _midi.settings = new_fluid_settings();
58 if (!
_midi.settings)
return "Could not create midi settings";
60 fluid_settings_setint(
_midi.settings,
"synth.lock-memory", 0);
64 if (!
_midi.synth)
return "Could not open synth";
70 sfont_id = FLUID_FAILED;
72 if (!fluid_is_soundfont(
default_sf[i]))
continue;
74 if (sfont_id != FLUID_FAILED)
break;
76 if (sfont_id == FLUID_FAILED)
return "Could not open any sound font";
78 sfont_id = fluid_synth_sfload(
_midi.synth, sfont_name, 1);
79 if (sfont_id == FLUID_FAILED)
return "Could not open sound font";
82 _midi.player =
nullptr;
85 fluid_synth_set_sample_rate(
_midi.synth, samplerate);
86 DEBUG(driver, 1,
"Fluidsynth: samplerate %.0f", (
float)samplerate);
95 delete_fluid_synth(
_midi.synth);
96 delete_fluid_settings(
_midi.settings);
105 if (filename.empty()) {
111 DEBUG(driver, 0,
"Could not create midi player");
115 if (fluid_player_add(
_midi.player, filename.c_str()) != FLUID_OK) {
116 DEBUG(driver, 0,
"Could not open music file");
117 delete_fluid_player(
_midi.player);
118 _midi.player =
nullptr;
121 if (fluid_player_play(
_midi.player) != FLUID_OK) {
122 DEBUG(driver, 0,
"Could not start midi player");
123 delete_fluid_player(
_midi.player);
124 _midi.player =
nullptr;
131 if (!
_midi.player)
return;
133 fluid_player_stop(
_midi.player);
134 if (fluid_player_join(
_midi.player) != FLUID_OK) {
135 DEBUG(driver, 0,
"Could not join player");
137 delete_fluid_player(
_midi.player);
138 fluid_synth_system_reset(
_midi.synth);
139 _midi.player =
nullptr;
144 if (!
_midi.player)
return false;
146 return fluid_player_get_status(
_midi.player) == FLUID_PLAYER_PLAYING;
155 double gain = (1.0 * vol) / (128.0 * 5.0);
156 if (fluid_settings_setnum(
_midi.settings,
"synth.gain", gain) != 1) {
157 DEBUG(driver, 0,
"Could not set volume");
const char * GetDriverParam(const char *const *parm, const char *name)
Get a string parameter the list of parameters.
Metadata about a music track.
bool IsSongPlaying() override
Are we currently playing a song?
void StopSong() override
Stop playing the current song.
fluid_settings_t * settings
FluidSynth settings handle.
const char * Start(const char *const *param) override
Start this driver.
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Base for FluidSynth music playback.
uint32 MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
#define DEBUG(name, level,...)
Output a line of debugging information.
fluid_synth_t * synth
FluidSynth synthesizer handle.
static FMusicDriver_FluidSynth iFMusicDriver_FluidSynth
Factory for the FluidSynth driver.
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
static struct @24 _midi
Metadata about the midi we're playing.
void Stop() override
Stop this driver.
static const char * default_sf[]
List of sound fonts to try by default.
Factory for the fluidsynth driver.
fluid_player_t * player
FluidSynth MIDI player handle.
void SetVolume(byte vol) override
Set the volume, if possible.