OpenTTD Source 15.1
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
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;
30
33
35static const char *_default_soundfonts[] = {
36 /* FluidSynth preferred */
37 /* See: https://www.fluidsynth.org/api/settings_synth.html#settings_synth_default-soundfont */
38 "/usr/share/soundfonts/default.sf2",
39
40 /* Debian/Ubuntu preferred */
41 /* See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=929185 */
42 "/usr/share/sounds/sf3/default-GM.sf3",
43
44 /* OpenSUSE preferred */
45 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
46
47 /* RedHat/Fedora/Arch preferred */
48 "/usr/share/soundfonts/FluidR3_GM.sf2",
49
50 /* Debian/Ubuntu/OpenSUSE alternatives */
51 "/usr/share/sounds/sf2/TimGM6mb.sf2",
52 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
53};
54
55static void RenderMusicStream(int16_t *buffer, size_t samples)
56{
57 std::unique_lock<std::mutex> lock{_midi.synth_mutex, std::try_to_lock};
58
59 if (!lock.owns_lock() || _midi.synth == nullptr || _midi.player == nullptr) return;
60 fluid_synth_write_s16(_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
61}
62
63std::optional<std::string_view> MusicDriver_FluidSynth::Start(const StringList &param)
64{
65 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
66
67 auto sfont_name = GetDriverParam(param, "soundfont");
68 int sfont_id;
69
70 Debug(driver, 1, "Fluidsynth: sf {}", sfont_name.has_value() ? *sfont_name : "(null)");
71
72 /* Create the settings. */
73 _midi.settings = new_fluid_settings();
74 if (_midi.settings == nullptr) return "Could not create midi settings";
75 /* Don't try to lock sample data in memory, OTTD usually does not run with privileges allowing that */
76 fluid_settings_setint(_midi.settings, "synth.lock-memory", 0);
77
78 /* Install the music render routine and set up the samplerate */
79 uint32_t samplerate = MxSetMusicSource(RenderMusicStream);
80 fluid_settings_setnum(_midi.settings, "synth.sample-rate", samplerate);
81 Debug(driver, 1, "Fluidsynth: samplerate {:.0f}", (float)samplerate);
82
83 /* Create the synthesizer. */
84 _midi.synth = new_fluid_synth(_midi.settings);
85 if (_midi.synth == nullptr) return "Could not open synth";
86
87 /* Load a SoundFont and reset presets (so that new instruments
88 * get used from the SoundFont) */
89 if (!sfont_name.has_value()) {
90 sfont_id = FLUID_FAILED;
91
92 /* Try loading the default soundfont registered with FluidSynth. */
93 char *default_soundfont = nullptr;
94 fluid_settings_dupstr(_midi.settings, "synth.default-soundfont", &default_soundfont);
95 if (default_soundfont != nullptr && std::filesystem::exists(default_soundfont) && fluid_is_soundfont(default_soundfont)) {
96 sfont_id = fluid_synth_sfload(_midi.synth, default_soundfont, 1);
97 }
98 fluid_free(default_soundfont);
99
100 /* If no default soundfont found, try our own list. */
101 if (sfont_id == FLUID_FAILED) {
102 for (const char *soundfont : _default_soundfonts) {
103 if (!std::filesystem::exists(soundfont) || !fluid_is_soundfont(soundfont)) continue;
104 sfont_id = fluid_synth_sfload(_midi.synth, soundfont, 1);
105 if (sfont_id != FLUID_FAILED) break;
106 }
107 }
108 if (sfont_id == FLUID_FAILED) return "Could not open any sound font";
109 } else {
110 std::string name{sfont_name.value()};
111 sfont_id = fluid_synth_sfload(_midi.synth, name.c_str(), 1);
112 if (sfont_id == FLUID_FAILED) return "Could not open sound font";
113 }
114
115 _midi.player = nullptr;
116
117 return std::nullopt;
118}
119
121{
122 MxSetMusicSource(nullptr);
123
124 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
125
126 if (_midi.player != nullptr) delete_fluid_player(_midi.player);
127 _midi.player = nullptr;
128
129 if (_midi.synth != nullptr) delete_fluid_synth(_midi.synth);
130 _midi.synth = nullptr;
131
132 if (_midi.settings != nullptr) delete_fluid_settings(_midi.settings);
133 _midi.settings = nullptr;
134}
135
137{
138 std::string filename = MidiFile::GetSMFFile(song);
139
140 this->StopSong();
141
142 if (filename.empty()) {
143 return;
144 }
145
146 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
147
148 _midi.player = new_fluid_player(_midi.synth);
149 if (_midi.player == nullptr) {
150 Debug(driver, 0, "Could not create midi player");
151 return;
152 }
153
154 if (fluid_player_add(_midi.player, filename.c_str()) != FLUID_OK) {
155 Debug(driver, 0, "Could not open music file");
156 delete_fluid_player(_midi.player);
157 _midi.player = nullptr;
158 return;
159 }
160 if (fluid_player_play(_midi.player) != FLUID_OK) {
161 Debug(driver, 0, "Could not start midi player");
162 delete_fluid_player(_midi.player);
163 _midi.player = nullptr;
164 return;
165 }
166}
167
169{
170 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
171
172 if (_midi.player == nullptr) return;
173
174 fluid_player_stop(_midi.player);
175 /* No fluid_player_join needed */
176 delete_fluid_player(_midi.player);
177 fluid_synth_system_reset(_midi.synth);
178 fluid_synth_all_sounds_off(_midi.synth, -1);
179 _midi.player = nullptr;
180}
181
183{
184 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
185 if (_midi.player == nullptr) return false;
186
187 return fluid_player_get_status(_midi.player) == FLUID_PLAYER_PLAYING;
188}
189
191{
192 std::lock_guard<std::mutex> lock{_midi.synth_mutex};
193 if (_midi.settings == nullptr) return;
194
195 /* Allowed range of synth.gain is 0.0 to 10.0 */
196 /* fluidsynth's default gain is 0.2, so use this as "full
197 * volume". Set gain using OpenTTD's volume, as a number between 0
198 * and 0.2. */
199 double gain = (1.0 * vol) / (128.0 * 5.0);
200 if (fluid_settings_setnum(_midi.settings, "synth.gain", gain) != FLUID_OK) {
201 Debug(driver, 0, "Could not set volume");
202 }
203}
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
static struct @13 _midi
Metadata about the midi we're playing.
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.
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