OpenTTD Source 20250529-master-g10c159a79f
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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../openttd.h"
12#include "../sound_type.h"
13#include "../debug.h"
14#include "fluidsynth.h"
15#include "midifile.hpp"
16#include <fluidsynth.h>
17#include "../mixer.h"
18#include <mutex>
19#include "../safeguards.h"
20
21static struct {
22 fluid_settings_t *settings;
23 fluid_synth_t *synth;
24 fluid_player_t *player;
25 std::mutex synth_mutex;
27
30
32static const char *default_sf[] = {
33 /* FluidSynth preferred */
34 /* See: https://www.fluidsynth.org/api/settings_synth.html#settings_synth_default-soundfont */
35 "/usr/share/soundfonts/default.sf2",
36
37 /* Debian/Ubuntu preferred */
38 /* See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=929185 */
39 "/usr/share/sounds/sf3/default-GM.sf3",
40
41 /* OpenSUSE preferred */
42 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
43
44 /* RedHat/Fedora/Arch preferred */
45 "/usr/share/soundfonts/FluidR3_GM.sf2",
46
47 /* Debian/Ubuntu/OpenSUSE alternatives */
48 "/usr/share/sounds/sf2/TimGM6mb.sf2",
49 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
50
51 nullptr
52};
53
54static void RenderMusicStream(int16_t *buffer, size_t samples)
55{
56 std::unique_lock<std::mutex> lock{ _midi.synth_mutex, std::try_to_lock };
57
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);
60}
61
62std::optional<std::string_view> MusicDriver_FluidSynth::Start(const StringList &param)
63{
64 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
65
66 auto sfont_name = GetDriverParam(param, "soundfont");
67 int sfont_id;
68
69 Debug(driver, 1, "Fluidsynth: sf {}", sfont_name.has_value() ? *sfont_name : "(null)");
70
71 /* Create the settings. */
72 _midi.settings = new_fluid_settings();
73 if (_midi.settings == nullptr) return "Could not create midi settings";
74 /* Don't try to lock sample data in memory, OTTD usually does not run with privileges allowing that */
75 fluid_settings_setint(_midi.settings, "synth.lock-memory", 0);
76
77 /* Install the music render routine and set up the samplerate */
78 uint32_t samplerate = MxSetMusicSource(RenderMusicStream);
79 fluid_settings_setnum(_midi.settings, "synth.sample-rate", samplerate);
80 Debug(driver, 1, "Fluidsynth: samplerate {:.0f}", (float)samplerate);
81
82 /* Create the synthesizer. */
83 _midi.synth = new_fluid_synth(_midi.settings);
84 if (_midi.synth == nullptr) return "Could not open synth";
85
86 /* Load a SoundFont and reset presets (so that new instruments
87 * get used from the SoundFont) */
88 if (!sfont_name.has_value()) {
89 sfont_id = FLUID_FAILED;
90
91 /* Try loading the default soundfont registered with FluidSynth. */
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);
96 }
97
98 /* If no default soundfont found, try our own list. */
99 if (sfont_id == FLUID_FAILED) {
100 for (int i = 0; default_sf[i]; i++) {
101 if (!fluid_is_soundfont(default_sf[i])) continue;
102 sfont_id = fluid_synth_sfload(_midi.synth, default_sf[i], 1);
103 if (sfont_id != FLUID_FAILED) break;
104 }
105 }
106 if (sfont_id == FLUID_FAILED) return "Could not open any sound font";
107 } else {
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";
111 }
112
113 _midi.player = nullptr;
114
115 return std::nullopt;
116}
117
119{
120 MxSetMusicSource(nullptr);
121
122 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
123
124 if (_midi.player != nullptr) delete_fluid_player(_midi.player);
125 _midi.player = nullptr;
126
127 if (_midi.synth != nullptr) delete_fluid_synth(_midi.synth);
128 _midi.synth = nullptr;
129
130 if (_midi.settings != nullptr) delete_fluid_settings(_midi.settings);
131 _midi.settings = nullptr;
132}
133
135{
136 std::string filename = MidiFile::GetSMFFile(song);
137
138 this->StopSong();
139
140 if (filename.empty()) {
141 return;
142 }
143
144 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
145
146 _midi.player = new_fluid_player(_midi.synth);
147 if (_midi.player == nullptr) {
148 Debug(driver, 0, "Could not create midi player");
149 return;
150 }
151
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;
156 return;
157 }
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;
162 return;
163 }
164}
165
167{
168 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
169
170 if (_midi.player == nullptr) return;
171
172 fluid_player_stop(_midi.player);
173 /* No fluid_player_join needed */
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;
178}
179
181{
182 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
183 if (_midi.player == nullptr) return false;
184
185 return fluid_player_get_status(_midi.player) == FLUID_PLAYER_PLAYING;
186}
187
189{
190 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
191 if (_midi.settings == nullptr) return;
192
193 /* Allowed range of synth.gain is 0.0 to 10.0 */
194 /* fluidsynth's default gain is 0.2, so use this as "full
195 * volume". Set gain using OpenTTD's volume, as a number between 0
196 * and 0.2. */
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");
200 }
201}
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.
#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.
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.
Definition mixer.cpp:234
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