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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "igor/igor.h"
#include "igor/midi.h"
namespace Igor {
MidiParser_CTMF::MidiParser_CTMF()
: _instrumentsCount(0) {
memset(_instruments, 0, sizeof(_instruments));
}
void MidiParser_CTMF::decodeHeader(const uint8 *p) {
_instrumentsDataOffset = READ_LE_UINT16(p); p += 2;
_midiDataOffset = READ_LE_UINT16(p); p += 2;
_ticksPerQuarter = READ_LE_UINT16(p); p += 2;
_ticksPerSecond = READ_LE_UINT16(p); p += 2;
p += 22;
_instrumentsCount = READ_LE_UINT16(p); p += 2;
_basicTempo = READ_LE_UINT16(p); p += 2;
}
void MidiParser_CTMF::decodeAdlibInstrument(struct AdlibInstrument *ins, const uint8 *p) {
ins->chr[kAdlibCarrier] = p[0];
ins->chr[kAdlibModulator] = p[1];
ins->scale[kAdlibCarrier] = p[2];
ins->scale[kAdlibModulator] = p[3];
ins->attack[kAdlibCarrier] = p[4];
ins->attack[kAdlibModulator] = p[5];
ins->sustain[kAdlibCarrier] = p[6];
ins->sustain[kAdlibModulator] = p[7];
ins->waveSel[kAdlibCarrier] = p[8];
ins->waveSel[kAdlibModulator] = p[9];
ins->feedback = p[10];
}
bool MidiParser_CTMF::loadMusic(byte *data, uint32 size) {
if (memcmp(data, "CTMF", 4) == 0 && READ_LE_UINT16(data + 4) == 0x101) {
decodeHeader(data + 6);
assert(_instrumentsCount <= kMaxInstruments);
for (int i = 0; i < _instrumentsCount; ++i) {
decodeAdlibInstrument(&_instruments[i], data + _instrumentsDataOffset + i * 16);
}
// reset parser
_num_tracks = 1;
_tracks[0] = data + _midiDataOffset;
_ppqn = _ticksPerQuarter;
setTempo(500000);
setTrack(0);
return true;
}
return false;
}
void MidiParser_CTMF::parseNextEvent(EventInfo &info) {
info.start = _position._play_pos;
info.delta = readVLQ(_position._play_pos);
if ((_position._play_pos[0] & 0xF0) >= 0x80) {
info.event = *_position._play_pos++;
} else {
info.event = _position._running_status;
}
if ((info.event & 0x80) == 0) {
return;
}
_position._running_status = info.event;
switch (info.command()) {
case 0x8: // Note Off
case 0x9: // Note On
case 0xB: // Control Mode Change
info.basic.param1 = *_position._play_pos++;
info.basic.param2 = *_position._play_pos++;
if (info.command() == 0x9 && info.basic.param2 == 0) {
info.event = info.channel() | 0x80; // Note Off
}
return;
case 0xC: // Program Change
info.basic.param1 = *(_position._play_pos++);
info.basic.param2 = 0;
return;
case 0xF:
switch (info.event & 15) {
case 0xF:
info.ext.type = *(_position._play_pos++);
info.length = readVLQ(_position._play_pos);
info.ext.data = _position._play_pos;
_position._play_pos += info.length;
return;
}
}
warning("MidiParser_CTMF::parseNextEvent: Unhandled event code %x", info.event);
}
int AdlibMidiDriver::open() {
MidiDriver_Emulated::open();
_opl = makeAdlibOPL(getRate());
memset(_adlibData, 0, sizeof(_adlibData));
_adlibRhythmMode = false;
for (int i = 0; i < kAdlibChannelsCount; ++i) {
_adlibChannels[i].ch = -1;
_adlibChannels[i].lt = _adlibChannels[i].note = 0;
}
memset(_adlibInstrumentsMappingTable, 0, sizeof(_adlibInstrumentsMappingTable));
adlibSetupCard();
_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_mixerSoundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, false, true);
return 0;
}
void AdlibMidiDriver::close() {
_mixer->stopHandle(_mixerSoundHandle);
OPLDestroy(_opl);
}
void AdlibMidiDriver::send(uint32 b) {
int channel = b & 15;
int cmd = (b >> 4) & 7;
int param1 = (b >> 8) & 255;
int param2 = (b >> 16) & 255;
switch (cmd) {
case 0:
adlibTurnNoteOff(channel, param1);
break;
case 1:
adlibTurnNoteOn(channel, param1, param2);
break;
case 3:
adlibControlChange(channel, param1, param2);
break;
case 4:
adlibProgramChange(channel, param1);
break;
default:
warning("Unhandled cmd %d channel %d (0x%X)", cmd, channel, b);
break;
}
}
void AdlibMidiDriver::generateSamples(int16 *data, int len) {
memset(data, 0, sizeof(int16) * len);
YM3812UpdateOne(_opl, data, len);
}
void AdlibMidiDriver::adlibWrite(int port, int value) {
OPLWriteReg(_opl, port, value);
_adlibData[port & 255] = value & 255;
}
void AdlibMidiDriver::adlibSetupCard() {
for (int i = 0; i < 256; ++i) {
adlibWrite(i, 0);
}
adlibWrite(1, 0x20);
adlibWrite(0xBD, 0xC0);
}
void AdlibMidiDriver::adlibTurnNoteOff(int channel, int note) {
for (int i = 0; i < kAdlibChannelsCount; ++i) {
if (_adlibChannels[i].ch == channel && _adlibChannels[i].note == note) {
adlibEndNote(i);
_adlibChannels[i].ch = -1;
}
}
}
void AdlibMidiDriver::adlibTurnNoteOn(int channel, int note, int velocity) {
assert(velocity != 0);
for (int i = 0; i < kAdlibChannelsCount; ++i) {
if (_adlibChannels[i].ch != -1) {
++_adlibChannels[i].lt;
}
}
int ch = -1;
if (!_adlibRhythmMode || channel < 11) {
int maxLt = -1;
int maxCh = -1;
for (int i = 0; i < (_adlibRhythmMode ? 6 : 9); ++i) {
if (_adlibChannels[i].ch == -1) {
ch = i;
break;
}
if (_adlibChannels[i].lt > maxLt) {
maxLt = _adlibChannels[i].lt;
maxCh = i;
}
}
if (ch == -1) {
assert(maxCh != -1);
ch = maxCh;
adlibEndNote(ch);
}
} else {
ch = _adlibPercussionsMappingTable[channel - 11];
}
const AdlibInstrument &ins = _adlibInstruments[_adlibInstrumentsMappingTable[channel]];
if (!_adlibRhythmMode || channel < 12) {
adlibSetupInstrument(ch, ins);
} else {
adlibSetupPercussion(channel, ins);
}
adlibSetupNote(ch, note - 13, velocity);
_adlibChannels[ch].ch = channel;
_adlibChannels[ch].note = note;
_adlibChannels[ch].lt = 0;
}
void AdlibMidiDriver::adlibSetupInstrument(int channel, const AdlibInstrument &ins) {
adlibWrite(0x20 + _adlibOperatorsTable[channel], ins.chr[kAdlibCarrier]);
adlibWrite(0x23 + _adlibOperatorsTable[channel], ins.chr[kAdlibModulator]);
adlibWrite(0x40 + _adlibOperatorsTable[channel], ins.scale[kAdlibCarrier]);
if ((ins.feedback & 1) == 0) {
adlibWrite(0x43 + _adlibOperatorsTable[channel], ins.scale[kAdlibModulator]);
} else {
adlibWrite(0x43 + _adlibOperatorsTable[channel], 0);
}
adlibWrite(0x60 + _adlibOperatorsTable[channel], ins.attack[kAdlibCarrier]);
adlibWrite(0x63 + _adlibOperatorsTable[channel], ins.attack[kAdlibModulator]);
adlibWrite(0x80 + _adlibOperatorsTable[channel], ins.sustain[kAdlibCarrier]);
adlibWrite(0x83 + _adlibOperatorsTable[channel], ins.sustain[kAdlibModulator]);
adlibWrite(0xE0 + _adlibOperatorsTable[channel], ins.waveSel[kAdlibCarrier]);
adlibWrite(0xE3 + _adlibOperatorsTable[channel], ins.waveSel[kAdlibModulator]);
adlibWrite(0xC0 + channel, ins.feedback);
}
void AdlibMidiDriver::adlibSetupPercussion(int channel, const AdlibInstrument &ins) {
channel = _adlibChannelsMappingTable[channel - 12];
adlibWrite(0x20 + channel, ins.chr[kAdlibCarrier]);
adlibWrite(0x40 + channel, ins.scale[kAdlibCarrier]);
adlibWrite(0x60 + channel, ins.attack[kAdlibCarrier]);
adlibWrite(0x80 + channel, ins.sustain[kAdlibCarrier]);
adlibWrite(0xE0 + channel, ins.waveSel[kAdlibCarrier]);
adlibWrite(0xC0 + channel, ins.feedback);
}
void AdlibMidiDriver::adlibSetupNote(int channel, int note, int velocity) {
adlibSetVolume(channel, velocity);
int f = _adlibNoteFreqTable[note % 12];
adlibWrite(0xA0 + channel, f);
int oct = note / 12;
int c = ((f & 0x300) >> 8) + (oct << 2);
if (!_adlibRhythmMode || channel < 6) {
c |= 0x20;
}
adlibWrite(0xB0 + channel, c);
}
void AdlibMidiDriver::adlibEndNote(int channel) {
adlibWrite(0xB0 + channel, _adlibData[0xB0 + channel] & ~0x20);
}
void AdlibMidiDriver::adlibSetVolume(int channel, int volume) {
volume = 63 - (volume >> 1);
if ((_adlibData[0xC0 + channel] & 1) == 1) {
adlibWrite(0x40 + _adlibOperatorsTable[channel], volume | (_adlibData[0x40 + _adlibOperatorsTable[channel]] & 0xC0));
}
adlibWrite(0x43 + _adlibOperatorsTable[channel], volume | (_adlibData[0x43 + _adlibOperatorsTable[channel]] & 0xC0));
}
void AdlibMidiDriver::adlibControlChange(int channel, int control, int param) {
switch (control) {
case 0x67:
_adlibRhythmMode = param != 0;
if (_adlibRhythmMode) {
adlibWrite(0xBD, _adlibData[0xBD] | 0x20);
} else {
adlibWrite(0xBD, _adlibData[0xBD] & ~0x20);
}
break;
case 0x7B:
adlibTurnNoteOff(channel, -1);
break;
default:
warning("Unhandled adlibControlChange 0x%X %d", control, param);
break;
}
}
void AdlibMidiDriver::adlibProgramChange(int channel, int num) {
_adlibInstrumentsMappingTable[channel] = num;
}
const uint8 AdlibMidiDriver::_adlibOperatorsTable[] = { 0, 1, 2, 8, 9, 10, 16, 17, 18 };
const uint8 AdlibMidiDriver::_adlibChannelsMappingTable[] = { 20, 18, 21, 17 };
const int16 AdlibMidiDriver::_adlibNoteFreqTable[] = { 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647, 686 };
const uint8 AdlibMidiDriver::_adlibPercussionsMappingTable[] = { 6, 7, 8, 8, 7 };
MidiPlayer::MidiPlayer(IgorEngine *vm) : _isPlaying(false) {
_driver = new AdlibMidiDriver(vm->_mixer);
_driver->open();
_parser = new MidiParser_CTMF;
_parser->setMidiDriver(_driver);
_parser->setTimerRate(_driver->getBaseTempo());
_driver->setTimerCallback(this, &MidiPlayer::updateTimerCallback);
}
MidiPlayer::~MidiPlayer() {
stopMusic();
_driver->setTimerCallback(0, 0);
_driver->close();
delete _parser;
delete _driver;
}
void MidiPlayer::playMusic(uint8 *data, uint32 size) {
stopMusic();
_mutex.lock();
_isPlaying = true;
_parser->loadMusic(data, size);
_parser->setTrack(0);
_driver->setInstruments(&_parser->_instruments[0]);
_mutex.unlock();
}
void MidiPlayer::stopMusic() {
_mutex.lock();
if (_isPlaying) {
_isPlaying = false;
_parser->unloadMusic();
}
_mutex.unlock();
}
void MidiPlayer::updateTimer() {
_mutex.lock();
if (_isPlaying) {
_parser->onTimer();
}
_mutex.unlock();
}
} // namespace Igor
|