OpenTTD Source: src/music/fluidsynth.cpp Source File

OpenTTD Source 13.2.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 <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
20 static struct {
21 fluid_settings_t *settings;
22 fluid_synth_t *synth;
23 fluid_player_t *player;
24 std::mutex synth_mutex;
25 } _midi;
26
28 static FMusicDriver_FluidSynth iFMusicDriver_FluidSynth;
29
31 static const char *default_sf[] = {
32 /* FluidSynth preferred */
33 /* See: https://www.fluidsynth.org/api/settings_synth.html#settings_synth_default-soundfont */
34 "/usr/share/soundfonts/default.sf2",
35
36 /* Debian/Ubuntu preferred */
37 /* See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=929185 */
38 "/usr/share/sounds/sf3/default-GM.sf3",
39
40 /* OpenSUSE preferred */
41 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
42
43 /* RedHat/Fedora/Arch preferred */
44 "/usr/share/soundfonts/FluidR3_GM.sf2",
45
46 /* Debian/Ubuntu/OpenSUSE alternatives */
47 "/usr/share/sounds/sf2/TimGM6mb.sf2",
48 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
49
50 nullptr
51 };
52
53 static void RenderMusicStream(int16 *buffer, size_t samples)
54 {
55 std::unique_lock<std::mutex> lock{ _midi.synth_mutex, std::try_to_lock };
56
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);
59 }
60
61 const char *MusicDriver_FluidSynth::Start(const StringList &param)
62 {
63 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
64
65 const char *sfont_name = GetDriverParam(param, "soundfont");
66 int sfont_id;
67
68 Debug(driver, 1, "Fluidsynth: sf {}", sfont_name != nullptr ? sfont_name : "(null)");
69
70 /* Create the settings. */
71 _midi.settings = new_fluid_settings();
72 if (_midi.settings == nullptr) return "Could not create midi settings";
73 /* Don't try to lock sample data in memory, OTTD usually does not run with privileges allowing that */
74 fluid_settings_setint(_midi.settings, "synth.lock-memory", 0);
75
76 /* Install the music render routine and set up the samplerate */
77 uint32 samplerate = MxSetMusicSource(RenderMusicStream);
78 fluid_settings_setnum(_midi.settings, "synth.sample-rate", samplerate);
79 Debug(driver, 1, "Fluidsynth: samplerate {:.0f}", (float)samplerate);
80
81 /* Create the synthesizer. */
82 _midi.synth = new_fluid_synth(_midi.settings);
83 if (_midi.synth == nullptr) return "Could not open synth";
84
85 /* Load a SoundFont and reset presets (so that new instruments
86 * get used from the SoundFont) */
87 if (sfont_name == nullptr) {
88 sfont_id = FLUID_FAILED;
89
90 /* Try loading the default soundfont registered with FluidSynth. */
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);
95 }
96
97 /* If no default soundfont found, try our own list. */
98 if (sfont_id == FLUID_FAILED) {
99 for (int i = 0; default_sf[i]; i++) {
100 if (!fluid_is_soundfont(default_sf[i])) continue;
101 sfont_id = fluid_synth_sfload(_midi.synth, default_sf[i], 1);
102 if (sfont_id != FLUID_FAILED) break;
103 }
104 }
105 if (sfont_id == FLUID_FAILED) return "Could not open any sound font";
106 } else {
107 sfont_id = fluid_synth_sfload(_midi.synth, sfont_name, 1);
108 if (sfont_id == FLUID_FAILED) return "Could not open sound font";
109 }
110
111 _midi.player = nullptr;
112
113 return nullptr;
114 }
115
116 void MusicDriver_FluidSynth::Stop()
117 {
118 MxSetMusicSource(nullptr);
119
120 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
121
122 if (_midi.player != nullptr) delete_fluid_player(_midi.player);
123 _midi.player = nullptr;
124
125 if (_midi.synth != nullptr) delete_fluid_synth(_midi.synth);
126 _midi.synth = nullptr;
127
128 if (_midi.settings != nullptr) delete_fluid_settings(_midi.settings);
129 _midi.settings = nullptr;
130 }
131
132 void MusicDriver_FluidSynth::PlaySong(const MusicSongInfo &song)
133 {
134 std::string filename = MidiFile::GetSMFFile(song);
135
136 this->StopSong();
137
138 if (filename.empty()) {
139 return;
140 }
141
142 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
143
144 _midi.player = new_fluid_player(_midi.synth);
145 if (_midi.player == nullptr) {
146 Debug(driver, 0, "Could not create midi player");
147 return;
148 }
149
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;
154 return;
155 }
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;
160 return;
161 }
162 }
163
164 void MusicDriver_FluidSynth::StopSong()
165 {
166 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
167
168 if (_midi.player == nullptr) return;
169
170 fluid_player_stop(_midi.player);
171 /* No fluid_player_join needed */
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;
176 }
177
178 bool MusicDriver_FluidSynth::IsSongPlaying()
179 {
180 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
181 if (_midi.player == nullptr) return false;
182
183 return fluid_player_get_status(_midi.player) == FLUID_PLAYER_PLAYING;
184 }
185
186 void MusicDriver_FluidSynth::SetVolume(byte vol)
187 {
188 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
189 if (_midi.settings == nullptr) return;
190
191 /* Allowed range of synth.gain is 0.0 to 10.0 */
192 /* fluidsynth's default gain is 0.2, so use this as "full
193 * volume". Set gain using OpenTTD's volume, as a number between 0
194 * and 0.2. */
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");
198 }
199 }
MusicDriver_FluidSynth::IsSongPlaying
bool IsSongPlaying() override
Are we currently playing a song?
Definition: fluidsynth.cpp:178
MusicDriver_FluidSynth::SetVolume
void SetVolume(byte vol) override
Set the volume, if possible.
Definition: fluidsynth.cpp:186
MusicDriver_FluidSynth::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: fluidsynth.cpp:61
fluidsynth.h
synth_mutex
std::mutex synth_mutex
Guard mutex for synth access.
Definition: fluidsynth.cpp:24
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
synth
fluid_synth_t * synth
FluidSynth synthesizer handle.
Definition: fluidsynth.cpp:22
MusicDriver_FluidSynth::StopSong
void StopSong() override
Stop playing the current song.
Definition: fluidsynth.cpp:164
iFMusicDriver_FluidSynth
static FMusicDriver_FluidSynth iFMusicDriver_FluidSynth
Factory for the FluidSynth driver.
Definition: fluidsynth.cpp:28
MusicDriver_FluidSynth::PlaySong
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Definition: fluidsynth.cpp:132
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:50
default_sf
static const char * default_sf[]
List of sound fonts to try by default.
Definition: fluidsynth.cpp:31
player
fluid_player_t * player
FluidSynth MIDI player handle.
Definition: fluidsynth.cpp:23
_midi
static struct @23 _midi
Metadata about the midi we're playing.
MusicSongInfo
Metadata about a music track.
Definition: base_media_base.h:291
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:59
FMusicDriver_FluidSynth
Factory for the fluidsynth driver.
Definition: fluidsynth.h:33
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
MidiFile::GetSMFFile
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1048
MxSetMusicSource
uint32 MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
Definition: mixer.cpp:244
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
MusicDriver_FluidSynth::Stop
void Stop() override
Stop this driver.
Definition: fluidsynth.cpp:116