1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001-2004 The ScummVM project
*
* YM2612 tone generation code written by Tomoaki Hayasaka.
* Used under the terms of the GNU General Public License.
* Adpated to ScummVM by Jamieson Christian.
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*/
#include "stdafx.h"
#include "common/scummsys.h"
#ifdef USE_MT32EMU
#include "backends/midi/mt32/mt32emu.h"
#include "backends/midi/emumidi.h"
#include "sound/mpu401.h"
#include "common/util.h"
#include "common/file.h"
#include "common/config-manager.h"
class MidiDriver_MT32 : public MidiDriver_Emulated {
private:
MidiChannel_MPU401 _midi_channels[16];
uint16 _channel_mask;
MT32Emu::Synth *_synth;
int _outputRate;
protected:
void generate_samples(int16 *buf, int len);
public:
MidiDriver_MT32(SoundMixer *mixer);
virtual ~MidiDriver_MT32();
int open();
void close();
void send(uint32 b);
void sysEx(byte *msg, uint16 length);
uint32 property(int prop, uint32 param);
MidiChannel *allocateChannel();
MidiChannel *getPercussionChannel();
// AudioStream API
bool isStereo() const { return true; }
int getRate() const { return _outputRate; }
};
typedef File SFile;
class MT32File: public MT32Emu::File {
SFile file;
public:
bool open(const char *filename, OpenMode mode) {
SFile::AccessMode accessMode = mode == OpenMode_read ? SFile::kFileReadMode : SFile::kFileWriteMode;
return file.open(filename, accessMode);
}
void close() {
return file.close();
}
size_t read(void *ptr, size_t size) {
return file.read(ptr, size);
}
bool readLine(char *ptr, size_t size) {
return file.gets(ptr, size) != NULL;
}
size_t write(const void *ptr, size_t size) {
return file.write(ptr, size);
}
int readByte() {
byte b = file.readByte();
if (file.eof())
return -1;
return b;
}
bool writeByte(unsigned char out) {
file.writeByte(out);
if (file.ioFailed())
return false;
return true;
}
bool isEOF() {
return file.eof();
}
};
MT32Emu::File *MT32_OpenFile(void *userData, const char *filename, MT32Emu::File::OpenMode mode) {
MT32File *file = new MT32File();
if (!file->open(filename, mode)) {
delete file;
return NULL;
}
return file;
}
////////////////////////////////////////
//
// MidiDriver_MT32
//
////////////////////////////////////////
static void report(void *userData, MT32Emu::ReportType type, void *reportData) {
switch(type) {
case MT32Emu::ReportType_lcdMessage:
g_system->displayMessageOnOSD((char *)reportData);
break;
case MT32Emu::ReportType_errorPreset1:
error("Couldn't open Preset1.syx file");
break;
case MT32Emu::ReportType_errorPreset2:
error("Couldn't open Preset2.syx file");
break;
case MT32Emu::ReportType_errorDrumpat:
error("Couldn't open drumpat.rom file");
break;
case MT32Emu::ReportType_errorPatchlog:
error("Couldn't open patchlog.cfg file");
break;
case MT32Emu::ReportType_errorMT32ROM:
error("Couldn't open MT32_PCM.ROM file");
break;
default:
break;
}
}
MidiDriver_MT32::MidiDriver_MT32(SoundMixer *mixer) : MidiDriver_Emulated(mixer) {
_channel_mask = 0xFFFF; // Permit all 16 channels by default
uint i;
for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
_midi_channels [i].init (this, i);
}
_synth = NULL;
_baseFreq = 1000;
_outputRate = 44100;
}
MidiDriver_MT32::~MidiDriver_MT32() {
if (_synth != NULL)
delete _synth;
}
static void vdebug(void *data, const char *fmt, va_list list) {
// do nothing here now
}
int MidiDriver_MT32::open() {
MT32Emu::SynthProperties prop;
if (_isOpen)
return MERR_ALREADY_OPEN;
MidiDriver_Emulated::open();
memset(&prop, 0, sizeof(prop));
prop.SampleRate = getRate();
prop.UseReverb = true;
prop.UseDefault = false;
prop.RevType = 0;
prop.RevTime = 5;
prop.RevLevel = 3;
prop.userData = (void *)1;
prop.printDebug = &vdebug;
prop.report = &report;
prop.openFile = MT32_OpenFile;
_synth = new MT32Emu::Synth();
if (!_synth->open(prop))
return MERR_DEVICE_NOT_AVAILABLE;
_mixer->setupPremix(this);
return 0;
}
void MidiDriver_MT32::send(uint32 b) {
_synth->playMsg(b);
}
void MidiDriver_MT32::sysEx(byte *msg, uint16 length) {
if (msg[0] == 0xf0) {
_synth->playSysex(msg, length);
} else {
_synth->playSysexWithoutFraming(msg, length);
}
}
void MidiDriver_MT32::close() {
if (!_isOpen)
return;
_isOpen = false;
// Detach the premix callback handler
_mixer->setupPremix(0);
_synth->close();
delete _synth;
_synth = NULL;
}
void MidiDriver_MT32::generate_samples(int16 *data, int len) {
_synth->render(data, len);
}
uint32 MidiDriver_MT32::property (int prop, uint32 param) {
switch (prop) {
case PROP_CHANNEL_MASK:
_channel_mask = param & 0xFFFF;
return 1;
}
return 0;
}
MidiChannel *MidiDriver_MT32::allocateChannel() {
MidiChannel_MPU401 *chan;
uint i;
for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
if (i == 9 || !(_channel_mask & (1 << i)))
continue;
chan = &_midi_channels[i];
if (chan->allocate()) {
return chan;
}
}
return NULL;
}
MidiChannel *MidiDriver_MT32::getPercussionChannel() {
return &_midi_channels [9];
}
////////////////////////////////////////
//
// MidiDriver_MT32 factory
//
////////////////////////////////////////
MidiDriver *MidiDriver_MT32_create(SoundMixer *mixer) {
// HACK: It will stay here until engine plugin loader overhaul
if (ConfMan.hasKey("extrapath"))
File::addDefaultDirectory(ConfMan.get("extrapath"));
return new MidiDriver_MT32(mixer);
}
#endif
|