1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "common/scummsys.h"
24
25 #ifdef USE_FLUIDSYNTH
26
27 #include "common/config-manager.h"
28 #include "common/error.h"
29 #include "common/system.h"
30 #include "common/textconsole.h"
31 #include "audio/musicplugin.h"
32 #include "audio/mpu401.h"
33 #include "audio/softsynth/emumidi.h"
34 #if defined(IPHONE_IOS7) && defined(IPHONE_SANDBOXED)
35 #include "backends/platform/ios7/ios7_common.h"
36 #endif
37
38 #ifdef __LIBRETRO__
39 #include <fluidlite.h>
40 #else
41 #include <fluidsynth.h>
42 #endif
43
44 class MidiDriver_FluidSynth : public MidiDriver_Emulated {
45 private:
46 MidiChannel_MPU401 _midiChannels[16];
47 fluid_settings_t *_settings;
48 fluid_synth_t *_synth;
49 int _soundFont;
50 int _outputRate;
51
52 protected:
53 // Because GCC complains about casting from const to non-const...
54 void setInt(const char *name, int val);
55 void setNum(const char *name, double num);
56 void setStr(const char *name, const char *str);
57
58 void generateSamples(int16 *buf, int len);
59
60 public:
61 MidiDriver_FluidSynth(Audio::Mixer *mixer);
62
63 int open();
64 void close();
65 void send(uint32 b);
66
67 MidiChannel *allocateChannel();
68 MidiChannel *getPercussionChannel();
69
70 // AudioStream API
isStereo() const71 bool isStereo() const { return true; }
getRate() const72 int getRate() const { return _outputRate; }
73 };
74
75 // MidiDriver method implementations
76
MidiDriver_FluidSynth(Audio::Mixer * mixer)77 MidiDriver_FluidSynth::MidiDriver_FluidSynth(Audio::Mixer *mixer)
78 : MidiDriver_Emulated(mixer) {
79
80 for (int i = 0; i < ARRAYSIZE(_midiChannels); i++) {
81 _midiChannels[i].init(this, i);
82 }
83
84 // It ought to be possible to get FluidSynth to generate samples at
85 // lower
86
87 _outputRate = _mixer->getOutputRate();
88 if (_outputRate < 22050)
89 _outputRate = 22050;
90 else if (_outputRate > 96000)
91 _outputRate = 96000;
92 }
93
94 // The string duplication below is there only because older versions (1.1.6
95 // and earlier?) of FluidSynth expected the string parameters to be non-const.
96
setInt(const char * name,int val)97 void MidiDriver_FluidSynth::setInt(const char *name, int val) {
98 char *name2 = scumm_strdup(name);
99
100 fluid_settings_setint(_settings, name2, val);
101 delete[] name2;
102 }
103
setNum(const char * name,double val)104 void MidiDriver_FluidSynth::setNum(const char *name, double val) {
105 char *name2 = scumm_strdup(name);
106
107 fluid_settings_setnum(_settings, name2, val);
108 delete[] name2;
109 }
110
setStr(const char * name,const char * val)111 void MidiDriver_FluidSynth::setStr(const char *name, const char *val) {
112 char *name2 = scumm_strdup(name);
113 char *val2 = scumm_strdup(val);
114
115 fluid_settings_setstr(_settings, name2, val2);
116 delete[] name2;
117 delete[] val2;
118 }
119
open()120 int MidiDriver_FluidSynth::open() {
121 if (_isOpen)
122 return MERR_ALREADY_OPEN;
123
124 if (!ConfMan.hasKey("soundfont"))
125 error("FluidSynth requires a 'soundfont' setting");
126
127 _settings = new_fluid_settings();
128
129 // The default gain setting is ridiculously low - at least for me. This
130 // cannot be fixed by ScummVM's volume settings because they can only
131 // soften the sound, not amplify it, so instead we add an option to
132 // adjust the gain of FluidSynth itself.
133
134 double gain = (double)ConfMan.getInt("midi_gain") / 100.0;
135
136 setNum("synth.gain", gain);
137 setNum("synth.sample-rate", _outputRate);
138
139 _synth = new_fluid_synth(_settings);
140
141 if (ConfMan.getBool("fluidsynth_chorus_activate")) {
142 fluid_synth_set_chorus_on(_synth, 1);
143
144 int chorusNr = ConfMan.getInt("fluidsynth_chorus_nr");
145 double chorusLevel = (double)ConfMan.getInt("fluidsynth_chorus_level") / 100.0;
146 double chorusSpeed = (double)ConfMan.getInt("fluidsynth_chorus_speed") / 100.0;
147 double chorusDepthMs = (double)ConfMan.getInt("fluidsynth_chorus_depth") / 10.0;
148
149 Common::String chorusWaveForm = ConfMan.get("fluidsynth_chorus_waveform");
150 int chorusType = FLUID_CHORUS_MOD_SINE;
151 if (chorusWaveForm == "sine") {
152 chorusType = FLUID_CHORUS_MOD_SINE;
153 } else {
154 chorusType = FLUID_CHORUS_MOD_TRIANGLE;
155 }
156
157 fluid_synth_set_chorus(_synth, chorusNr, chorusLevel, chorusSpeed, chorusDepthMs, chorusType);
158 } else {
159 fluid_synth_set_chorus_on(_synth, 0);
160 }
161
162 if (ConfMan.getBool("fluidsynth_reverb_activate")) {
163 fluid_synth_set_reverb_on(_synth, 1);
164
165 double reverbRoomSize = (double)ConfMan.getInt("fluidsynth_reverb_roomsize") / 100.0;
166 double reverbDamping = (double)ConfMan.getInt("fluidsynth_reverb_damping") / 100.0;
167 double reverbWidth = (double)ConfMan.getInt("fluidsynth_reverb_width") / 100.0;
168 double reverbLevel = (double)ConfMan.getInt("fluidsynth_reverb_level") / 100.0;
169
170 fluid_synth_set_reverb(_synth, reverbRoomSize, reverbDamping, reverbWidth, reverbLevel);
171 } else {
172 fluid_synth_set_reverb_on(_synth, 0);
173 }
174
175 Common::String interpolation = ConfMan.get("fluidsynth_misc_interpolation");
176 int interpMethod = FLUID_INTERP_4THORDER;
177
178 if (interpolation == "none") {
179 interpMethod = FLUID_INTERP_NONE;
180 } else if (interpolation == "linear") {
181 interpMethod = FLUID_INTERP_LINEAR;
182 } else if (interpolation == "4th") {
183 interpMethod = FLUID_INTERP_4THORDER;
184 } else if (interpolation == "7th") {
185 interpMethod = FLUID_INTERP_7THORDER;
186 }
187
188 fluid_synth_set_interp_method(_synth, -1, interpMethod);
189
190 const char *soundfont = ConfMan.get("soundfont").c_str();
191
192 #if defined(IPHONE_IOS7) && defined(IPHONE_SANDBOXED)
193 // HACK: Due to the sandbox on non-jailbroken iOS devices, we need to deal
194 // with the chroot filesystem. All the path selected by the user are
195 // relative to the Document directory. So, we need to adjust the path to
196 // reflect that.
197 Common::String soundfont_fullpath = iOS7_getDocumentsDir();
198 soundfont_fullpath += soundfont;
199 _soundFont = fluid_synth_sfload(_synth, soundfont_fullpath.c_str(), 1);
200 #else
201 _soundFont = fluid_synth_sfload(_synth, soundfont, 1);
202 #endif
203
204 if (_soundFont == -1)
205 error("Failed loading custom sound font '%s'", soundfont);
206
207 MidiDriver_Emulated::open();
208
209 _mixer->playStream(Audio::Mixer::kPlainSoundType, &_mixerSoundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true);
210 return 0;
211 }
212
close()213 void MidiDriver_FluidSynth::close() {
214 if (!_isOpen)
215 return;
216 _isOpen = false;
217
218 _mixer->stopHandle(_mixerSoundHandle);
219
220 if (_soundFont != -1)
221 fluid_synth_sfunload(_synth, _soundFont, 1);
222
223 delete_fluid_synth(_synth);
224 delete_fluid_settings(_settings);
225 }
226
send(uint32 b)227 void MidiDriver_FluidSynth::send(uint32 b) {
228 //byte param3 = (byte) ((b >> 24) & 0xFF);
229 uint param2 = (byte) ((b >> 16) & 0xFF);
230 uint param1 = (byte) ((b >> 8) & 0xFF);
231 byte cmd = (byte) (b & 0xF0);
232 byte chan = (byte) (b & 0x0F);
233
234 switch (cmd) {
235 case 0x80: // Note Off
236 fluid_synth_noteoff(_synth, chan, param1);
237 break;
238 case 0x90: // Note On
239 fluid_synth_noteon(_synth, chan, param1, param2);
240 break;
241 case 0xA0: // Aftertouch
242 break;
243 case 0xB0: // Control Change
244 fluid_synth_cc(_synth, chan, param1, param2);
245 break;
246 case 0xC0: // Program Change
247 fluid_synth_program_change(_synth, chan, param1);
248 break;
249 case 0xD0: // Channel Pressure
250 break;
251 case 0xE0: // Pitch Bend
252 fluid_synth_pitch_bend(_synth, chan, (param2 << 7) | param1);
253 break;
254 case 0xF0: // SysEx
255 // We should never get here! SysEx information has to be
256 // sent via high-level semantic methods.
257 warning("MidiDriver_FluidSynth: Receiving SysEx command on a send() call");
258 break;
259 default:
260 warning("MidiDriver_FluidSynth: Unknown send() command 0x%02X", cmd);
261 break;
262 }
263 }
264
allocateChannel()265 MidiChannel *MidiDriver_FluidSynth::allocateChannel() {
266 for (int i = 0; i < ARRAYSIZE(_midiChannels); i++) {
267 if (i != 9 && _midiChannels[i].allocate())
268 return &_midiChannels[i];
269 }
270 return NULL;
271 }
272
getPercussionChannel()273 MidiChannel *MidiDriver_FluidSynth::getPercussionChannel() {
274 return &_midiChannels[9];
275 }
276
generateSamples(int16 * data,int len)277 void MidiDriver_FluidSynth::generateSamples(int16 *data, int len) {
278 fluid_synth_write_s16(_synth, len, data, 0, 2, data, 1, 2);
279 }
280
281
282 // Plugin interface
283
284 class FluidSynthMusicPlugin : public MusicPluginObject {
285 public:
getName() const286 const char *getName() const {
287 return "FluidSynth";
288 }
289
getId() const290 const char *getId() const {
291 return "fluidsynth";
292 }
293
294 MusicDevices getDevices() const;
295 Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const;
296 };
297
getDevices() const298 MusicDevices FluidSynthMusicPlugin::getDevices() const {
299 MusicDevices devices;
300 devices.push_back(MusicDevice(this, "", MT_GM));
301 return devices;
302 }
303
createInstance(MidiDriver ** mididriver,MidiDriver::DeviceHandle) const304 Common::Error FluidSynthMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle) const {
305 *mididriver = new MidiDriver_FluidSynth(g_system->getMixer());
306
307 return Common::kNoError;
308 }
309
310 //#if PLUGIN_ENABLED_DYNAMIC(FLUIDSYNTH)
311 //REGISTER_PLUGIN_DYNAMIC(FLUIDSYNTH, PLUGIN_TYPE_MUSIC, FluidSynthMusicPlugin);
312 //#else
313 REGISTER_PLUGIN_STATIC(FLUIDSYNTH, PLUGIN_TYPE_MUSIC, FluidSynthMusicPlugin);
314 //#endif
315
316 #endif
317