10 #include "../stdafx.h"
11 #include "../openttd.h"
12 #include "../sound_type.h"
15 #include "midifile.hpp"
33 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
36 "/usr/share/soundfonts/FluidR3_GM.sf2",
39 "/usr/share/sounds/sf2/TimGM6mb.sf2",
40 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
45 static void RenderMusicStream(int16 *buffer,
size_t samples)
47 std::unique_lock<std::mutex>
lock{
_midi.synth_mutex, std::try_to_lock };
50 fluid_synth_write_s16(
_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
55 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
60 DEBUG(driver, 1,
"Fluidsynth: sf %s", sfont_name);
63 _midi.settings = new_fluid_settings();
64 if (!
_midi.settings)
return "Could not create midi settings";
66 fluid_settings_setint(
_midi.settings,
"synth.lock-memory", 0);
70 fluid_settings_setnum(
_midi.settings,
"synth.sample-rate", samplerate);
71 DEBUG(driver, 1,
"Fluidsynth: samplerate %.0f", (
float)samplerate);
75 if (!
_midi.synth)
return "Could not open synth";
81 sfont_id = FLUID_FAILED;
83 if (!fluid_is_soundfont(
default_sf[i]))
continue;
85 if (sfont_id != FLUID_FAILED)
break;
87 if (sfont_id == FLUID_FAILED)
return "Could not open any sound font";
89 sfont_id = fluid_synth_sfload(
_midi.synth, sfont_name, 1);
90 if (sfont_id == FLUID_FAILED)
return "Could not open sound font";
93 _midi.player =
nullptr;
102 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
104 if (
_midi.player !=
nullptr) delete_fluid_player(
_midi.player);
105 _midi.player =
nullptr;
107 if (
_midi.synth !=
nullptr) delete_fluid_synth(
_midi.synth);
108 _midi.synth =
nullptr;
110 if (
_midi.settings !=
nullptr) delete_fluid_settings(
_midi.settings);
111 _midi.settings =
nullptr;
120 if (filename.empty()) {
124 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
128 DEBUG(driver, 0,
"Could not create midi player");
132 if (fluid_player_add(
_midi.player, filename.c_str()) != FLUID_OK) {
133 DEBUG(driver, 0,
"Could not open music file");
134 delete_fluid_player(
_midi.player);
135 _midi.player =
nullptr;
138 if (fluid_player_play(
_midi.player) != FLUID_OK) {
139 DEBUG(driver, 0,
"Could not start midi player");
140 delete_fluid_player(
_midi.player);
141 _midi.player =
nullptr;
148 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
150 if (!
_midi.player)
return;
152 fluid_player_stop(
_midi.player);
153 if (fluid_player_join(
_midi.player) != FLUID_OK) {
154 DEBUG(driver, 0,
"Could not join player");
156 delete_fluid_player(
_midi.player);
157 fluid_synth_system_reset(
_midi.synth);
158 fluid_synth_all_sounds_off(
_midi.synth, -1);
159 _midi.player =
nullptr;
164 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
165 if (!
_midi.player)
return false;
167 return fluid_player_get_status(
_midi.player) == FLUID_PLAYER_PLAYING;
172 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
173 if (
_midi.settings ==
nullptr)
return;
179 double gain = (1.0 * vol) / (128.0 * 5.0);
180 if (fluid_settings_setnum(
_midi.settings,
"synth.gain", gain) != FLUID_OK) {
181 DEBUG(driver, 0,
"Could not set volume");