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_t *buffer,
size_t samples)
55 std::unique_lock<std::mutex>
lock{
_midi.synth_mutex, std::try_to_lock };
57 if (!
lock.owns_lock() ||
_midi.synth ==
nullptr ||
_midi.player ==
nullptr)
return;
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 {}", sfont_name !=
nullptr ? sfont_name :
"(null)");
71 _midi.settings = new_fluid_settings();
72 if (
_midi.settings ==
nullptr)
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 ==
nullptr)
return "Could not open synth";
87 if (sfont_name ==
nullptr) {
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 };
145 if (
_midi.player ==
nullptr) {
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;
166 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
168 if (
_midi.player ==
nullptr)
return;
170 fluid_player_stop(
_midi.player);
172 delete_fluid_player(
_midi.player);
173 fluid_synth_system_reset(
_midi.synth);
174 fluid_synth_all_sounds_off(
_midi.synth, -1);
175 _midi.player =
nullptr;
180 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
181 if (
_midi.player ==
nullptr)
return false;
183 return fluid_player_get_status(
_midi.player) == FLUID_PLAYER_PLAYING;
188 std::lock_guard<std::mutex>
lock{
_midi.synth_mutex };
189 if (
_midi.settings ==
nullptr)
return;
195 double gain = (1.0 * vol) / (128.0 * 5.0);
196 if (fluid_settings_setnum(
_midi.settings,
"synth.gain", gain) != FLUID_OK) {
197 Debug(driver, 0,
"Could not set volume");