1
/* ScummVM - Graphic Adventure Engine
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.
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.
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.
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.
25
#include "common/scummsys.h"
29
#include "common/config-manager.h"
30
#include "common/error.h"
31
#include "common/system.h"
32
#include "common/textconsole.h"
33
#include "audio/musicplugin.h"
34
#include "audio/mpu401.h"
35
#include "audio/softsynth/emumidi.h"
37
#include <fluidsynth.h>
39
class MidiDriver_FluidSynth : public MidiDriver_Emulated {
41
MidiChannel_MPU401 _midiChannels[16];
42
fluid_settings_t *_settings;
43
fluid_synth_t *_synth;
48
// Because GCC complains about casting from const to non-const...
49
void setInt(const char *name, int val);
50
void setNum(const char *name, double num);
51
void setStr(const char *name, const char *str);
53
void generateSamples(int16 *buf, int len);
56
MidiDriver_FluidSynth(Audio::Mixer *mixer);
62
MidiChannel *allocateChannel();
63
MidiChannel *getPercussionChannel();
66
bool isStereo() const { return true; }
67
int getRate() const { return _outputRate; }
70
// MidiDriver method implementations
72
MidiDriver_FluidSynth::MidiDriver_FluidSynth(Audio::Mixer *mixer)
73
: MidiDriver_Emulated(mixer) {
75
for (int i = 0; i < ARRAYSIZE(_midiChannels); i++) {
76
_midiChannels[i].init(this, i);
79
// It ought to be possible to get FluidSynth to generate samples at
82
_outputRate = _mixer->getOutputRate();
83
if (_outputRate < 22050)
85
else if (_outputRate > 96000)
89
void MidiDriver_FluidSynth::setInt(const char *name, int val) {
90
char *name2 = strdup(name);
92
fluid_settings_setint(_settings, name2, val);
96
void MidiDriver_FluidSynth::setNum(const char *name, double val) {
97
char *name2 = strdup(name);
99
fluid_settings_setnum(_settings, name2, val);
103
void MidiDriver_FluidSynth::setStr(const char *name, const char *val) {
104
char *name2 = strdup(name);
105
char *val2 = strdup(val);
107
fluid_settings_setstr(_settings, name2, val2);
112
int MidiDriver_FluidSynth::open() {
114
return MERR_ALREADY_OPEN;
116
if (!ConfMan.hasKey("soundfont"))
117
error("FluidSynth requires a 'soundfont' setting");
119
_settings = new_fluid_settings();
121
// The default gain setting is ridiculously low - at least for me. This
122
// cannot be fixed by ScummVM's volume settings because they can only
123
// soften the sound, not amplify it, so instead we add an option to
124
// adjust the gain of FluidSynth itself.
126
double gain = (double)ConfMan.getInt("midi_gain") / 100.0;
128
setNum("synth.gain", gain);
129
setNum("synth.sample-rate", _outputRate);
131
_synth = new_fluid_synth(_settings);
133
// In theory, this ought to reduce CPU load... but it doesn't make any
134
// noticeable difference for me, so disable it for now.
136
// fluid_synth_set_interp_method(_synth, -1, FLUID_INTERP_LINEAR);
137
// fluid_synth_set_reverb_on(_synth, 0);
138
// fluid_synth_set_chorus_on(_synth, 0);
140
const char *soundfont = ConfMan.get("soundfont").c_str();
142
_soundFont = fluid_synth_sfload(_synth, soundfont, 1);
143
if (_soundFont == -1)
144
error("Failed loading custom sound font '%s'", soundfont);
146
MidiDriver_Emulated::open();
148
// The MT-32 emulator uses kSFXSoundType here. I don't know why.
149
_mixer->playStream(Audio::Mixer::kMusicSoundType, &_mixerSoundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true);
153
void MidiDriver_FluidSynth::close() {
158
_mixer->stopHandle(_mixerSoundHandle);
160
if (_soundFont != -1)
161
fluid_synth_sfunload(_synth, _soundFont, 1);
163
delete_fluid_synth(_synth);
164
delete_fluid_settings(_settings);
167
void MidiDriver_FluidSynth::send(uint32 b) {
168
//byte param3 = (byte) ((b >> 24) & 0xFF);
169
uint param2 = (byte) ((b >> 16) & 0xFF);
170
uint param1 = (byte) ((b >> 8) & 0xFF);
171
byte cmd = (byte) (b & 0xF0);
172
byte chan = (byte) (b & 0x0F);
175
case 0x80: // Note Off
176
fluid_synth_noteoff(_synth, chan, param1);
178
case 0x90: // Note On
179
fluid_synth_noteon(_synth, chan, param1, param2);
181
case 0xA0: // Aftertouch
183
case 0xB0: // Control Change
184
fluid_synth_cc(_synth, chan, param1, param2);
186
case 0xC0: // Program Change
187
fluid_synth_program_change(_synth, chan, param1);
189
case 0xD0: // Channel Pressure
191
case 0xE0: // Pitch Bend
192
fluid_synth_pitch_bend(_synth, chan, (param2 << 7) | param1);
195
// We should never get here! SysEx information has to be
196
// sent via high-level semantic methods.
197
warning("MidiDriver_FluidSynth: Receiving SysEx command on a send() call");
200
warning("MidiDriver_FluidSynth: Unknown send() command 0x%02X", cmd);
205
MidiChannel *MidiDriver_FluidSynth::allocateChannel() {
206
for (int i = 0; i < ARRAYSIZE(_midiChannels); i++) {
207
if (i != 9 && _midiChannels[i].allocate())
208
return &_midiChannels[i];
213
MidiChannel *MidiDriver_FluidSynth::getPercussionChannel() {
214
return &_midiChannels[9];
217
void MidiDriver_FluidSynth::generateSamples(int16 *data, int len) {
218
fluid_synth_write_s16(_synth, len, data, 0, 2, data, 1, 2);
224
class FluidSynthMusicPlugin : public MusicPluginObject {
226
const char *getName() const {
230
const char *getId() const {
234
MusicDevices getDevices() const;
235
Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const;
238
MusicDevices FluidSynthMusicPlugin::getDevices() const {
239
MusicDevices devices;
240
devices.push_back(MusicDevice(this, "", MT_GM));
244
Common::Error FluidSynthMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle) const {
245
*mididriver = new MidiDriver_FluidSynth(g_system->getMixer());
247
return Common::kNoError;
250
//#if PLUGIN_ENABLED_DYNAMIC(FLUIDSYNTH)
251
//REGISTER_PLUGIN_DYNAMIC(FLUIDSYNTH, PLUGIN_TYPE_MUSIC, FluidSynthMusicPlugin);
253
REGISTER_PLUGIN_STATIC(FLUIDSYNTH, PLUGIN_TYPE_MUSIC, FluidSynthMusicPlugin);