11#include "../openttd.h"
12#include "../sound_type.h"
15#include "midifile.hpp"
19#include "../safeguards.h"
35 "/usr/share/soundfonts/default.sf2",
39 "/usr/share/sounds/sf3/default-GM.sf3",
42 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
45 "/usr/share/soundfonts/FluidR3_GM.sf2",
48 "/usr/share/sounds/sf2/TimGM6mb.sf2",
49 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
54static void RenderMusicStream(int16_t *buffer,
size_t samples)
56 std::unique_lock<std::mutex>
lock{
_midi.synth_mutex, std::try_to_lock };
58 if (!
lock.owns_lock() ||
_midi.synth ==
nullptr ||
_midi.player ==
nullptr)
return;
59 fluid_synth_write_s16(
_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
64 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
69 Debug(driver, 1,
"Fluidsynth: sf {}", sfont_name.has_value() ? *sfont_name :
"(null)");
72 _midi.settings = new_fluid_settings();
73 if (
_midi.settings ==
nullptr)
return "Could not create midi settings";
75 fluid_settings_setint(
_midi.settings,
"synth.lock-memory", 0);
79 fluid_settings_setnum(
_midi.settings,
"synth.sample-rate", samplerate);
80 Debug(driver, 1,
"Fluidsynth: samplerate {:.0f}", (
float)samplerate);
84 if (
_midi.synth ==
nullptr)
return "Could not open synth";
88 if (!sfont_name.has_value()) {
89 sfont_id = FLUID_FAILED;
92 char *default_soundfont;
93 fluid_settings_dupstr(
_midi.settings,
"synth.default-soundfont", &default_soundfont);
94 if (fluid_is_soundfont(default_soundfont)) {
95 sfont_id = fluid_synth_sfload(
_midi.synth, default_soundfont, 1);
99 if (sfont_id == FLUID_FAILED) {
101 if (!fluid_is_soundfont(
default_sf[i]))
continue;
103 if (sfont_id != FLUID_FAILED)
break;
106 if (sfont_id == FLUID_FAILED)
return "Could not open any sound font";
108 std::string name{sfont_name.value()};
109 sfont_id = fluid_synth_sfload(
_midi.synth, name.c_str(), 1);
110 if (sfont_id == FLUID_FAILED)
return "Could not open sound font";
113 _midi.player =
nullptr;
122 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
124 if (
_midi.player !=
nullptr) delete_fluid_player(
_midi.player);
125 _midi.player =
nullptr;
127 if (
_midi.synth !=
nullptr) delete_fluid_synth(
_midi.synth);
128 _midi.synth =
nullptr;
130 if (
_midi.settings !=
nullptr) delete_fluid_settings(
_midi.settings);
131 _midi.settings =
nullptr;
140 if (filename.empty()) {
144 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
147 if (
_midi.player ==
nullptr) {
148 Debug(driver, 0,
"Could not create midi player");
152 if (fluid_player_add(
_midi.player, filename.c_str()) != FLUID_OK) {
153 Debug(driver, 0,
"Could not open music file");
154 delete_fluid_player(
_midi.player);
155 _midi.player =
nullptr;
158 if (fluid_player_play(
_midi.player) != FLUID_OK) {
159 Debug(driver, 0,
"Could not start midi player");
160 delete_fluid_player(
_midi.player);
161 _midi.player =
nullptr;
168 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
170 if (
_midi.player ==
nullptr)
return;
172 fluid_player_stop(
_midi.player);
174 delete_fluid_player(
_midi.player);
175 fluid_synth_system_reset(
_midi.synth);
176 fluid_synth_all_sounds_off(
_midi.synth, -1);
177 _midi.player =
nullptr;
182 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
183 if (
_midi.player ==
nullptr)
return false;
185 return fluid_player_get_status(
_midi.player) == FLUID_PLAYER_PLAYING;
190 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
191 if (
_midi.settings ==
nullptr)
return;
197 double gain = (1.0 * vol) / (128.0 * 5.0);
198 if (fluid_settings_setnum(
_midi.settings,
"synth.gain", gain) != FLUID_OK) {
199 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.
std::mutex synth_mutex
Guard mutex for synth access.
static FMusicDriver_FluidSynth iFMusicDriver_FluidSynth
Factory for the FluidSynth driver.
fluid_player_t * player
FluidSynth MIDI player handle.
fluid_synth_t * synth
FluidSynth synthesizer handle.
static struct @14 _midi
Metadata about the midi we're playing.
static const char * default_sf[]
List of sound fonts to try by default.
fluid_settings_t * settings
FluidSynth settings handle.
Base for FluidSynth music playback.
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