OpenTTD Source 20260421-master-gc2fbc6fdeb
fluidsynth.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11
12#include "fluidsynth.h"
13
14#include <filesystem>
15#include <fluidsynth.h>
16#include <mutex>
17
18#include "../debug.h"
19#include "../mixer.h"
20#include "midifile.hpp"
21
22#include "../safeguards.h"
23
24static struct {
25 fluid_settings_t *settings;
26 fluid_synth_t *synth;
27 fluid_player_t *player;
28 std::mutex synth_mutex;
29 double max_gain;
31
34
36static const char *_default_soundfonts[] = {
37 /* FluidSynth preferred */
38 /* See: https://www.fluidsynth.org/api/settings_synth.html#settings_synth_default-soundfont */
39 "/usr/share/soundfonts/default.sf2",
40
41 /* Debian/Ubuntu preferred */
42 /* See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=929185 */
43 "/usr/share/sounds/sf3/default-GM.sf3",
44
45 /* OpenSUSE preferred */
46 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
47
48 /* RedHat/Fedora/Arch preferred */
49 "/usr/share/soundfonts/FluidR3_GM.sf2",
50
51 /* Debian/Ubuntu/OpenSUSE alternatives */
52 "/usr/share/sounds/sf2/TimGM6mb.sf2",
53 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
54};
55
56static void RenderMusicStream(int16_t *buffer, size_t samples)
57{
58 std::unique_lock<std::mutex> lock{_midi.synth_mutex, std::try_to_lock};
59
60 if (!lock.owns_lock() || _midi.synth == nullptr || _midi.player == nullptr) return;
61 fluid_synth_write_s16(_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
62}
63
64static void load_and_execute_config_file(fluid_cmd_handler_t *cmd_handler, const char *config_file)
65{
66 if (std::filesystem::exists(config_file)) {
67 Debug(driver, 2, "Fluidsynth: Attempting to load config file '{}'", config_file);
68 if (fluid_source(cmd_handler, config_file) < 0) {
69 Debug(driver, 0, "Fluidsynth: Failed to execute command configuration file '{}'", config_file);
70 }
71 } else {
72 Debug(driver, 1, "Fluidsynth: Failed to load config file '{}' - file doesn't exist", config_file);
73 }
74}
75
76std::optional<std::string_view> MusicDriver_FluidSynth::Start(const StringList &param)
77{
78 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
79
80 auto sfont_name = GetDriverParam(param, "soundfont");
81 int sfont_id;
82
83 Debug(driver, 1, "Fluidsynth: sf {}", sfont_name.has_value() ? *sfont_name : "(null)");
84
85 /* Create the settings. */
86 _midi.settings = new_fluid_settings();
87 if (_midi.settings == nullptr) return "Could not create midi settings";
88
89 /* Read config file(s) */
90 fluid_cmd_handler_t *cmd_handler = new_fluid_cmd_handler2(_midi.settings, nullptr, nullptr, nullptr);
91 if (cmd_handler == NULL) return "Failed to create the early command handler";
92
93 char buf[MAX_PATH] = {};
94 const char *config_file = fluid_get_sysconf(buf, sizeof(buf)); // system-wide config file
95 load_and_execute_config_file(cmd_handler, config_file);
96 config_file = fluid_get_userconf(buf, sizeof(buf)); // user config file (potentially overrides system)
97 load_and_execute_config_file(cmd_handler, config_file);
98
99 delete_fluid_cmd_handler(cmd_handler);
100
101 /* Don't try to lock sample data in memory, OTTD usually does not run with privileges allowing that */
102 fluid_settings_setint(_midi.settings, "synth.lock-memory", 0);
103
104 /* Install the music render routine and set up the samplerate */
105 uint32_t samplerate = MxSetMusicSource(RenderMusicStream);
106 fluid_settings_setnum(_midi.settings, "synth.sample-rate", samplerate);
107 Debug(driver, 1, "Fluidsynth: samplerate {:.0f}", (float)samplerate);
108
109 /* Create the synthesizer. */
110 _midi.synth = new_fluid_synth(_midi.settings);
111 if (_midi.synth == nullptr) return "Could not open synth";
112
113 /* Load a SoundFont and reset presets (so that new instruments
114 * get used from the SoundFont) */
115 if (!sfont_name.has_value()) {
116 sfont_id = FLUID_FAILED;
117
118 /* Try loading the default soundfont registered with FluidSynth. */
119 char *default_soundfont = nullptr;
120 fluid_settings_dupstr(_midi.settings, "synth.default-soundfont", &default_soundfont);
121 if (default_soundfont != nullptr && std::filesystem::exists(default_soundfont) && fluid_is_soundfont(default_soundfont)) {
122 sfont_id = fluid_synth_sfload(_midi.synth, default_soundfont, 1);
123 }
124 fluid_free(default_soundfont);
125
126 /* If no default soundfont found, try our own list. */
127 if (sfont_id == FLUID_FAILED) {
128 for (const char *soundfont : _default_soundfonts) {
129 if (!std::filesystem::exists(soundfont) || !fluid_is_soundfont(soundfont)) continue;
130 sfont_id = fluid_synth_sfload(_midi.synth, soundfont, 1);
131 if (sfont_id != FLUID_FAILED) break;
132 }
133 }
134 if (sfont_id == FLUID_FAILED) return "Could not open any sound font";
135 } else {
136 std::string name{sfont_name.value()};
137 sfont_id = fluid_synth_sfload(_midi.synth, name.c_str(), 1);
138 if (sfont_id == FLUID_FAILED) return "Could not open sound font";
139 }
140
141 /* Treat a configured synth gain as the maximum gain to use. */
142 if (fluid_settings_getnum(_midi.settings, "synth.gain", &_midi.max_gain) != FLUID_OK) {
143 if (fluid_settings_getnum_default(_midi.settings, "synth.gain", &_midi.max_gain) != FLUID_OK) {
144 /* No synth.gain value present for some reason, use FluidSynth's default value. */
145 _midi.max_gain = 0.2;
146 }
147 }
148
149 _midi.player = nullptr;
150
151 return std::nullopt;
152}
153
155{
156 MxSetMusicSource(nullptr);
157
158 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
159
160 if (_midi.player != nullptr) delete_fluid_player(_midi.player);
161 _midi.player = nullptr;
162
163 if (_midi.synth != nullptr) delete_fluid_synth(_midi.synth);
164 _midi.synth = nullptr;
165
166 if (_midi.settings != nullptr) delete_fluid_settings(_midi.settings);
167 _midi.settings = nullptr;
168}
169
171{
172 std::string filename = MidiFile::GetSMFFile(song);
173
174 this->StopSong();
175
176 if (filename.empty()) {
177 return;
178 }
179
180 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
181
182 _midi.player = new_fluid_player(_midi.synth);
183 if (_midi.player == nullptr) {
184 Debug(driver, 0, "Could not create midi player");
185 return;
186 }
187
188 if (fluid_player_add(_midi.player, filename.c_str()) != FLUID_OK) {
189 Debug(driver, 0, "Could not open music file");
190 delete_fluid_player(_midi.player);
191 _midi.player = nullptr;
192 return;
193 }
194 if (fluid_player_play(_midi.player) != FLUID_OK) {
195 Debug(driver, 0, "Could not start midi player");
196 delete_fluid_player(_midi.player);
197 _midi.player = nullptr;
198 return;
199 }
200}
201
203{
204 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
205
206 if (_midi.player == nullptr) return;
207
208 fluid_player_stop(_midi.player);
209 /* No fluid_player_join needed */
210 delete_fluid_player(_midi.player);
211 fluid_synth_system_reset(_midi.synth);
212 fluid_synth_all_sounds_off(_midi.synth, -1);
213 _midi.player = nullptr;
214}
215
217{
218 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
219 if (_midi.player == nullptr) return false;
220
221 return fluid_player_get_status(_midi.player) == FLUID_PLAYER_PLAYING;
222}
223
225{
226 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
227 if (_midi.settings == nullptr) return;
228
229 /* Set gain using OpenTTD's volume, as a number between 0 and max_gain. */
230 double gain = (1.0 * vol) / 128.0 * _midi.max_gain;
231 if (fluid_settings_setnum(_midi.settings, "synth.gain", gain) != FLUID_OK) {
232 Debug(driver, 0, "Could not set volume");
233 }
234}
Factory for the fluidsynth driver.
Definition fluidsynth.h:33
std::optional< std::string_view > Start(const StringList &param) 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.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
std::optional< std::string_view > GetDriverParam(const StringList &parm, std::string_view name)
Get a string parameter the list of parameters.
Definition driver.cpp:47
std::mutex synth_mutex
Guard mutex for synth access.
static FMusicDriver_FluidSynth iFMusicDriver_FluidSynth
Factory for the FluidSynth driver.
static const char * _default_soundfonts[]
List of sound fonts to try by default.
fluid_player_t * player
FluidSynth MIDI player handle.
fluid_synth_t * synth
FluidSynth synthesizer handle.
double max_gain
Default max gain.
static struct @363362336073004235014321267016377305167030167165 _midi
Metadata about the midi we're playing.
fluid_settings_t * settings
FluidSynth settings handle.
Base for FluidSynth music playback.
Parser for standard MIDI files.
uint32_t MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
Definition mixer.cpp:236
Functions to mix sound samples.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
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
Definition win32_m.cpp:35