10 #include "../stdafx.h"
11 #include "../openttd.h"
12 #include "../sound_type.h"
15 #include "midifile.hpp"
34 "/usr/share/soundfonts/default.sf2",
38 "/usr/share/sounds/sf3/default-GM.sf3",
41 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
44 "/usr/share/soundfonts/FluidR3_GM.sf2",
47 "/usr/share/sounds/sf2/TimGM6mb.sf2",
48 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
53 static void RenderMusicStream(int16 *buffer,
size_t samples)
55 std::unique_lock<std::mutex>
lock{
_midi.synth_mutex, std::try_to_lock };
58 fluid_synth_write_s16(
_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
63 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
68 DEBUG(driver, 1,
"Fluidsynth: sf %s", sfont_name);
71 _midi.settings = new_fluid_settings();
72 if (!
_midi.settings)
return "Could not create midi settings";
74 fluid_settings_setint(
_midi.settings,
"synth.lock-memory", 0);
78 fluid_settings_setnum(
_midi.settings,
"synth.sample-rate", samplerate);
79 DEBUG(driver, 1,
"Fluidsynth: samplerate %.0f", (
float)samplerate);
83 if (!
_midi.synth)
return "Could not open synth";
88 sfont_id = FLUID_FAILED;
91 char *default_soundfont;
92 fluid_settings_dupstr(
_midi.settings,
"synth.default-soundfont", &default_soundfont);
93 if (fluid_is_soundfont(default_soundfont)) {
94 sfont_id = fluid_synth_sfload(
_midi.synth, default_soundfont, 1);
98 if (sfont_id == FLUID_FAILED) {
100 if (!fluid_is_soundfont(
default_sf[i]))
continue;
102 if (sfont_id != FLUID_FAILED)
break;
105 if (sfont_id == FLUID_FAILED)
return "Could not open any sound font";
107 sfont_id = fluid_synth_sfload(
_midi.synth, sfont_name, 1);
108 if (sfont_id == FLUID_FAILED)
return "Could not open sound font";
111 _midi.player =
nullptr;
120 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
122 if (
_midi.player !=
nullptr) delete_fluid_player(
_midi.player);
123 _midi.player =
nullptr;
125 if (
_midi.synth !=
nullptr) delete_fluid_synth(
_midi.synth);
126 _midi.synth =
nullptr;
128 if (
_midi.settings !=
nullptr) delete_fluid_settings(
_midi.settings);
129 _midi.settings =
nullptr;
138 if (filename.empty()) {
142 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
146 DEBUG(driver, 0,
"Could not create midi player");
150 if (fluid_player_add(
_midi.player, filename.c_str()) != FLUID_OK) {
151 DEBUG(driver, 0,
"Could not open music file");
152 delete_fluid_player(
_midi.player);
153 _midi.player =
nullptr;
156 if (fluid_player_play(
_midi.player) != FLUID_OK) {
157 DEBUG(driver, 0,
"Could not start midi player");
158 delete_fluid_player(
_midi.player);
159 _midi.player =
nullptr;
167 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
169 if (!
_midi.player)
return;
171 fluid_player_stop(
_midi.player);
176 if (fluid_player_join(
_midi.player) != FLUID_OK) {
177 DEBUG(driver, 0,
"Could not join player");
180 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
181 delete_fluid_player(
_midi.player);
182 fluid_synth_system_reset(
_midi.synth);
183 fluid_synth_all_sounds_off(
_midi.synth, -1);
184 _midi.player =
nullptr;
189 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
190 if (!
_midi.player)
return false;
192 return fluid_player_get_status(
_midi.player) == FLUID_PLAYER_PLAYING;
197 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
198 if (
_midi.settings ==
nullptr)
return;
204 double gain = (1.0 * vol) / (128.0 * 5.0);
205 if (fluid_settings_setnum(
_midi.settings,
"synth.gain", gain) != FLUID_OK) {
206 DEBUG(driver, 0,
"Could not set volume");