2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2003-05-25 03:19:21 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-05-25 03:19:21 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-01-30 05:25:17 +00:00
|
|
|
#include "common/debug.h"
|
2003-05-25 03:19:21 +00:00
|
|
|
#include "common/util.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2003-05-25 03:19:21 +00:00
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/mididrv.h"
|
|
|
|
#include "audio/midiparser.h"
|
2006-04-29 14:23:16 +00:00
|
|
|
|
2006-09-29 09:44:30 +00:00
|
|
|
namespace AGOS {
|
2003-10-03 19:42:27 +00:00
|
|
|
|
2003-10-03 23:34:06 +00:00
|
|
|
/**
|
|
|
|
* Simon 1 Demo version of MidiParser.
|
|
|
|
*/
|
2003-05-25 03:19:21 +00:00
|
|
|
class MidiParser_S1D : public MidiParser {
|
2011-08-21 02:46:44 +00:00
|
|
|
private:
|
2003-05-25 03:19:21 +00:00
|
|
|
byte *_data;
|
|
|
|
bool _no_delta;
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
struct Loop {
|
|
|
|
uint16 timer;
|
|
|
|
byte *start, *end;
|
|
|
|
} _loops[16];
|
|
|
|
|
|
|
|
uint32 readVLQ2(byte *&data);
|
|
|
|
void chainEvent(EventInfo &info);
|
2003-05-25 03:19:21 +00:00
|
|
|
protected:
|
2006-10-21 12:22:30 +00:00
|
|
|
void parseNextEvent(EventInfo &info);
|
2003-05-25 03:19:21 +00:00
|
|
|
void resetTracking();
|
|
|
|
|
|
|
|
public:
|
|
|
|
MidiParser_S1D() : _data(0), _no_delta(false) {}
|
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
bool loadMusic(byte *data, uint32 size);
|
2003-05-25 03:19:21 +00:00
|
|
|
};
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
uint32 MidiParser_S1D::readVLQ2(byte *&data) {
|
|
|
|
uint32 delta = 0;
|
2003-05-25 03:19:21 +00:00
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
// LE format VLQ, which is 2 bytes long at max.
|
|
|
|
delta = *data++;
|
|
|
|
if (delta & 0x80) {
|
|
|
|
delta &= 0x7F;
|
|
|
|
delta |= *data++ << 7;
|
2003-05-25 03:19:21 +00:00
|
|
|
}
|
2011-08-21 02:46:44 +00:00
|
|
|
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiParser_S1D::chainEvent(EventInfo &info) {
|
|
|
|
// When we chain an event, we add up the old delta.
|
|
|
|
uint32 delta = info.delta;
|
|
|
|
parseNextEvent(info);
|
|
|
|
info.delta += delta;
|
2003-05-25 03:19:21 +00:00
|
|
|
}
|
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
void MidiParser_S1D::parseNextEvent(EventInfo &info) {
|
2003-05-25 03:19:21 +00:00
|
|
|
info.start = _position._play_pos;
|
2011-08-21 02:46:44 +00:00
|
|
|
info.length = 0;
|
2003-09-24 06:33:59 +00:00
|
|
|
info.delta = _no_delta ? 0 : readVLQ2(_position._play_pos);
|
2003-05-25 03:19:21 +00:00
|
|
|
_no_delta = false;
|
2011-08-21 02:46:44 +00:00
|
|
|
|
|
|
|
info.event = *_position._play_pos++;
|
|
|
|
if (!(info.event & 0x80)) {
|
2003-05-25 03:19:21 +00:00
|
|
|
_no_delta = true;
|
2011-08-21 02:46:44 +00:00
|
|
|
info.event |= 0x80;
|
2003-05-25 03:19:21 +00:00
|
|
|
}
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
if (info.event == 0xFC) {
|
|
|
|
// This means End of Track.
|
|
|
|
// Rewrite in SMF (MIDI transmission) form.
|
|
|
|
info.event = 0xFF;
|
|
|
|
info.ext.type = 0x2F;
|
|
|
|
} else {
|
|
|
|
switch (info.command()) {
|
|
|
|
case 0x8: // note off
|
|
|
|
info.basic.param1 = *_position._play_pos++;
|
2006-10-21 12:59:23 +00:00
|
|
|
info.basic.param2 = 0;
|
|
|
|
break;
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
case 0x9: // note on
|
|
|
|
info.basic.param1 = *_position._play_pos++;
|
|
|
|
info.basic.param2 = *_position._play_pos++;
|
2008-03-23 00:34:39 +00:00
|
|
|
break;
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
case 0xA: { // loop control
|
|
|
|
// In case the stop mode(?) is set to 0x80 this will stop the
|
|
|
|
// track over here.
|
|
|
|
|
|
|
|
const int16 loopIterations = int8(*_position._play_pos++);
|
|
|
|
if (!loopIterations) {
|
|
|
|
_loops[info.channel()].start = _position._play_pos;
|
|
|
|
} else {
|
|
|
|
if (!_loops[info.channel()].timer) {
|
|
|
|
if (_loops[info.channel()].start) {
|
|
|
|
_loops[info.channel()].timer = uint16(loopIterations);
|
|
|
|
_loops[info.channel()].end = _position._play_pos;
|
|
|
|
|
|
|
|
// Go to the start of the loop
|
|
|
|
_position._play_pos = _loops[info.channel()].start;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_loops[info.channel()].timer)
|
|
|
|
_position._play_pos = _loops[info.channel()].start;
|
|
|
|
--_loops[info.channel()].timer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to read the next midi event here. Since we can not
|
|
|
|
// safely pass this event to the MIDI event processing.
|
|
|
|
chainEvent(info);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 0xB: // auto stop marker(?)
|
|
|
|
// In case the stop mode(?) is set to 0x80 this will stop the
|
|
|
|
// track.
|
|
|
|
|
|
|
|
// We need to read the next midi event here. Since we can not
|
|
|
|
// safely pass this event to the MIDI event processing.
|
|
|
|
chainEvent(info);
|
2006-10-21 12:59:23 +00:00
|
|
|
break;
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
case 0xC: // program change
|
|
|
|
info.basic.param1 = *_position._play_pos++;
|
|
|
|
info.basic.param2 = 0;
|
2008-03-23 00:34:39 +00:00
|
|
|
break;
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
case 0xD: // jump to loop end
|
|
|
|
if (_loops[info.channel()].end)
|
|
|
|
_position._play_pos = _loops[info.channel()].end;
|
2008-03-23 00:34:39 +00:00
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
// We need to read the next midi event here. Since we can not
|
|
|
|
// safely pass this event to the MIDI event processing.
|
|
|
|
chainEvent(info);
|
2006-10-21 12:59:23 +00:00
|
|
|
break;
|
2008-03-23 00:34:39 +00:00
|
|
|
|
2006-10-21 12:59:23 +00:00
|
|
|
default:
|
2011-08-21 02:46:44 +00:00
|
|
|
// The original called some other function from here, which seems
|
|
|
|
// not to be MIDI related.
|
|
|
|
warning("MidiParser_S1D: default case %d", info.channel());
|
|
|
|
|
|
|
|
// We need to read the next midi event here. Since we can not
|
|
|
|
// safely pass this event to the MIDI event processing.
|
|
|
|
chainEvent(info);
|
2006-10-21 12:59:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-05-25 03:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
bool MidiParser_S1D::loadMusic(byte *data, uint32 size) {
|
2003-05-25 03:19:21 +00:00
|
|
|
unloadMusic();
|
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
// The original actually just ignores the first two bytes.
|
2003-05-25 03:19:21 +00:00
|
|
|
byte *pos = data;
|
2006-08-27 05:02:48 +00:00
|
|
|
if (*(pos++) != 0xFC)
|
2006-10-03 00:31:24 +00:00
|
|
|
debug(1, "Expected 0xFC header but found 0x%02X instead", (int) *pos);
|
2003-05-25 03:19:21 +00:00
|
|
|
|
2011-08-21 02:46:44 +00:00
|
|
|
pos += 1;
|
2003-05-25 03:19:21 +00:00
|
|
|
|
|
|
|
// And now we're at the actual data. Only one track.
|
|
|
|
_num_tracks = 1;
|
|
|
|
_data = pos;
|
|
|
|
_tracks[0] = pos;
|
|
|
|
|
|
|
|
// Note that we assume the original data passed in
|
|
|
|
// will persist beyond this call, i.e. we do NOT
|
|
|
|
// copy the data to our own buffer. Take warning....
|
|
|
|
resetTracking();
|
2005-04-19 06:25:35 +00:00
|
|
|
setTempo(666667);
|
2003-09-24 06:33:59 +00:00
|
|
|
setTrack(0);
|
2003-05-25 03:19:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiParser_S1D::resetTracking() {
|
|
|
|
MidiParser::resetTracking();
|
2011-08-21 02:46:44 +00:00
|
|
|
// The first event never contains any delta.
|
|
|
|
_no_delta = true;
|
|
|
|
memset(_loops, 0, sizeof(_loops));
|
2003-05-25 03:19:21 +00:00
|
|
|
}
|
|
|
|
|
2003-05-25 03:35:20 +00:00
|
|
|
MidiParser *MidiParser_createS1D() { return new MidiParser_S1D; }
|
2003-10-03 19:42:27 +00:00
|
|
|
|
2006-09-29 09:44:30 +00:00
|
|
|
} // End of namespace AGOS
|