Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / music / fluidsynth.cpp
blob4ffd90e62097341b02d126f2f0113274485fab3c
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 */
8 /** @file fluidsynth.cpp Playing music via the fluidsynth library. */
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>
20 static struct {
21 fluid_settings_t *settings; ///< FluidSynth settings handle
22 fluid_synth_t *synth; ///< FluidSynth synthesizer handle
23 fluid_player_t *player; ///< FluidSynth MIDI player handle
24 std::mutex synth_mutex; ///< Guard mutex for synth access
25 } _midi; ///< Metadata about the midi we're playing.
27 /** Factory for the FluidSynth driver. */
28 static FMusicDriver_FluidSynth iFMusicDriver_FluidSynth;
30 /** List of sound fonts to try by default. */
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",
36 /* Debian/Ubuntu preferred */
37 /* See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=929185 */
38 "/usr/share/sounds/sf3/default-GM.sf3",
40 /* OpenSUSE preferred */
41 "/usr/share/sounds/sf2/FluidR3_GM.sf2",
43 /* RedHat/Fedora/Arch preferred */
44 "/usr/share/soundfonts/FluidR3_GM.sf2",
46 /* Debian/Ubuntu/OpenSUSE alternatives */
47 "/usr/share/sounds/sf2/TimGM6mb.sf2",
48 "/usr/share/sounds/sf2/FluidR3_GS.sf2",
50 nullptr
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);
61 std::optional<std::string_view> MusicDriver_FluidSynth::Start(const StringList &param)
63 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
65 const char *sfont_name = GetDriverParam(param, "soundfont");
66 int sfont_id;
68 Debug(driver, 1, "Fluidsynth: sf {}", sfont_name != nullptr ? sfont_name : "(null)");
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);
76 /* Install the music render routine and set up the samplerate */
77 uint32_t samplerate = MxSetMusicSource(RenderMusicStream);
78 fluid_settings_setnum(_midi.settings, "synth.sample-rate", samplerate);
79 Debug(driver, 1, "Fluidsynth: samplerate {:.0f}", (float)samplerate);
81 /* Create the synthesizer. */
82 _midi.synth = new_fluid_synth(_midi.settings);
83 if (_midi.synth == nullptr) return "Could not open synth";
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;
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);
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;
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";
111 _midi.player = nullptr;
113 return std::nullopt;
116 void MusicDriver_FluidSynth::Stop()
118 MxSetMusicSource(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;
132 void MusicDriver_FluidSynth::PlaySong(const MusicSongInfo &song)
134 std::string filename = MidiFile::GetSMFFile(song);
136 this->StopSong();
138 if (filename.empty()) {
139 return;
142 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
144 _midi.player = new_fluid_player(_midi.synth);
145 if (_midi.player == nullptr) {
146 Debug(driver, 0, "Could not create midi player");
147 return;
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;
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;
164 void MusicDriver_FluidSynth::StopSong()
166 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
168 if (_midi.player == nullptr) return;
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;
178 bool MusicDriver_FluidSynth::IsSongPlaying()
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;
186 void MusicDriver_FluidSynth::SetVolume(uint8_t vol)
188 std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
189 if (_midi.settings == nullptr) return;
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");