2003-05-19 18:48:18 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001-2003 The ScummVM project
|
|
|
|
*
|
|
|
|
* 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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-06-18 21:10:05 +00:00
|
|
|
#include "stdafx.h"
|
2003-05-19 18:48:18 +00:00
|
|
|
#include "midiparser.h"
|
|
|
|
#include "mididrv.h"
|
|
|
|
#include "common/util.h"
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MidiParser implementation
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
|
|
|
MidiParser::MidiParser() :
|
2003-05-23 04:30:50 +00:00
|
|
|
_hanging_notes_count (0),
|
2003-05-19 18:48:18 +00:00
|
|
|
_driver (0),
|
|
|
|
_timer_rate (0x4A0000),
|
|
|
|
_ppqn (96),
|
|
|
|
_tempo (500000),
|
|
|
|
_psec_per_tick (5208), // 500000 / 96
|
|
|
|
_autoLoop (false),
|
2003-05-22 15:34:30 +00:00
|
|
|
_smartJump (false),
|
2003-05-19 18:48:18 +00:00
|
|
|
_num_tracks (0),
|
|
|
|
_active_track (255),
|
2003-05-23 04:30:50 +00:00
|
|
|
_abort_parse (0)
|
2003-05-22 15:34:30 +00:00
|
|
|
{
|
2003-05-23 04:19:47 +00:00
|
|
|
memset (_active_notes, 0, sizeof(_active_notes));
|
2003-05-22 15:34:30 +00:00
|
|
|
}
|
2003-05-19 18:48:18 +00:00
|
|
|
|
|
|
|
void MidiParser::property (int prop, int value) {
|
|
|
|
switch (prop) {
|
|
|
|
case mpAutoLoop:
|
|
|
|
_autoLoop = (value != 0);
|
2003-05-22 15:34:30 +00:00
|
|
|
case mpSmartJump:
|
|
|
|
_smartJump = (value != 0);
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
void MidiParser::setTempo (uint32 tempo) {
|
|
|
|
_tempo = tempo;
|
|
|
|
if (_ppqn)
|
|
|
|
_psec_per_tick = (tempo + (_ppqn >> 2)) / _ppqn;
|
|
|
|
}
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
// This is the conventional (i.e. SMF) variable length quantity
|
|
|
|
uint32 MidiParser::readVLQ (byte * &data) {
|
|
|
|
byte str;
|
|
|
|
uint32 value = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
str = data[0];
|
|
|
|
++data;
|
|
|
|
value = (value << 7) | (str & 0x7F);
|
|
|
|
if (!(str & 0x80))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2003-05-22 15:34:30 +00:00
|
|
|
void MidiParser::activeNote (byte channel, byte note, bool active) {
|
|
|
|
if (note >= 128 || channel >= 16)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (active)
|
|
|
|
_active_notes[note] |= (1 << channel);
|
|
|
|
else
|
|
|
|
_active_notes[note] &= ~(1 << channel);
|
|
|
|
|
|
|
|
// See if there are hanging notes that we can cancel
|
|
|
|
NoteTimer *ptr = _hanging_notes;
|
|
|
|
int i;
|
|
|
|
for (i = ARRAYSIZE(_hanging_notes); i; --i, ++ptr) {
|
|
|
|
if (ptr->channel == channel && ptr->note == note && ptr->time_left) {
|
|
|
|
ptr->time_left = 0;
|
|
|
|
--_hanging_notes_count;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-10 04:34:44 +00:00
|
|
|
void MidiParser::hangingNote (byte channel, byte note, uint32 time_left, bool recycle) {
|
2003-05-22 15:34:30 +00:00
|
|
|
NoteTimer *best = 0;
|
|
|
|
NoteTimer *ptr = _hanging_notes;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (_hanging_notes_count >= ARRAYSIZE(_hanging_notes)) {
|
|
|
|
printf ("WARNING! MidiParser::hangingNote(): Exceeded polyphony!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = ARRAYSIZE(_hanging_notes); i; --i, ++ptr) {
|
|
|
|
if (ptr->channel == channel && ptr->note == note) {
|
2003-07-10 04:34:44 +00:00
|
|
|
if (ptr->time_left && ptr->time_left < time_left && recycle)
|
2003-05-22 15:34:30 +00:00
|
|
|
return;
|
|
|
|
best = ptr;
|
2003-05-23 15:04:41 +00:00
|
|
|
if (ptr->time_left) {
|
2003-07-10 04:34:44 +00:00
|
|
|
if (recycle) _driver->send (0x80 | channel | note << 8);
|
2003-05-22 15:34:30 +00:00
|
|
|
--_hanging_notes_count;
|
2003-05-23 15:04:41 +00:00
|
|
|
}
|
2003-05-22 15:34:30 +00:00
|
|
|
break;
|
|
|
|
} else if (!best && ptr->time_left == 0) {
|
|
|
|
best = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-23 15:04:41 +00:00
|
|
|
// Occassionally we might get a zero or negative note
|
|
|
|
// length, if the note should be turned on and off in
|
|
|
|
// the same iteration. For now just set it to 1 and
|
|
|
|
// we'll turn it off in the next cycle.
|
|
|
|
if (!time_left || time_left & 0x80000000)
|
|
|
|
time_left = 1;
|
|
|
|
|
2003-05-22 15:34:30 +00:00
|
|
|
if (best) {
|
|
|
|
best->channel = channel;
|
|
|
|
best->note = note;
|
|
|
|
best->time_left = time_left;
|
|
|
|
++_hanging_notes_count;
|
|
|
|
} else {
|
|
|
|
// We checked this up top. We should never get here!
|
|
|
|
printf ("WARNING! MidiParser::hangingNote(): Internal error!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
void MidiParser::onTimer() {
|
|
|
|
uint32 end_time;
|
|
|
|
uint32 event_time;
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
if (!_position._play_pos || !_driver)
|
2003-05-19 18:48:18 +00:00
|
|
|
return;
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
_abort_parse = false;
|
|
|
|
end_time = _position._play_time + _timer_rate;
|
2003-05-19 18:48:18 +00:00
|
|
|
|
2003-05-22 15:34:30 +00:00
|
|
|
// Scan our hanging notes for any
|
|
|
|
// that should be turned off.
|
|
|
|
if (_hanging_notes_count) {
|
|
|
|
NoteTimer *ptr = &_hanging_notes[0];
|
|
|
|
int i;
|
|
|
|
for (i = ARRAYSIZE(_hanging_notes); i; --i, ++ptr) {
|
|
|
|
if (ptr->time_left) {
|
|
|
|
if (ptr->time_left <= _timer_rate) {
|
|
|
|
_driver->send (0x80 | ptr->channel | ptr->note << 8);
|
|
|
|
ptr->time_left = 0;
|
|
|
|
--_hanging_notes_count;
|
|
|
|
} else {
|
|
|
|
ptr->time_left -= _timer_rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
while (!_abort_parse) {
|
2003-05-19 18:48:18 +00:00
|
|
|
EventInfo &info = _next_event;
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
event_time = _position._last_event_time + info.delta * _psec_per_tick;
|
2003-05-19 18:48:18 +00:00
|
|
|
if (event_time > end_time)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Process the next info.
|
2003-05-23 04:19:47 +00:00
|
|
|
_position._last_event_tick += info.delta;
|
2003-05-19 18:48:18 +00:00
|
|
|
if (info.event < 0x80) {
|
2003-05-20 16:43:53 +00:00
|
|
|
printf ("ERROR! Bad command or running status %02X\n", info.event);
|
2003-05-23 04:19:47 +00:00
|
|
|
_position._play_pos = 0;
|
2003-05-19 18:48:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.event == 0xF0) {
|
|
|
|
// SysEx event
|
2003-05-22 15:34:30 +00:00
|
|
|
_driver->sysEx (info.ext.data, (uint16) info.length);
|
2003-05-19 18:48:18 +00:00
|
|
|
} else if (info.event == 0xFF) {
|
|
|
|
// META event
|
2003-05-19 19:24:22 +00:00
|
|
|
if (info.ext.type == 0x2F) {
|
2003-05-19 18:48:18 +00:00
|
|
|
// End of Track must be processed by us,
|
|
|
|
// as well as sending it to the output device.
|
|
|
|
if (_autoLoop) {
|
2003-05-23 15:04:41 +00:00
|
|
|
jumpToTick (0);
|
2003-05-19 18:48:18 +00:00
|
|
|
parseNextEvent (_next_event);
|
|
|
|
} else {
|
2003-05-23 15:04:41 +00:00
|
|
|
allNotesOff();
|
|
|
|
resetTracking();
|
2003-05-22 15:34:30 +00:00
|
|
|
_driver->metaEvent (info.ext.type, info.ext.data, (uint16) info.length);
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
return;
|
2003-05-19 19:24:22 +00:00
|
|
|
} else if (info.ext.type == 0x51) {
|
2003-05-22 15:34:30 +00:00
|
|
|
if (info.length >= 3) {
|
2003-05-23 04:19:47 +00:00
|
|
|
setTempo (info.ext.data[0] << 16 | info.ext.data[1] << 8 | info.ext.data[2]);
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-22 15:34:30 +00:00
|
|
|
_driver->metaEvent (info.ext.type, info.ext.data, (uint16) info.length);
|
2003-05-19 18:48:18 +00:00
|
|
|
} else {
|
2003-05-22 15:34:30 +00:00
|
|
|
if (info.command() == 0x8) {
|
|
|
|
activeNote (info.channel(), info.basic.param1, false);
|
|
|
|
} else if (info.command() == 0x9) {
|
|
|
|
if (info.length > 0)
|
|
|
|
hangingNote (info.channel(), info.basic.param1, info.length * _psec_per_tick - (end_time - event_time));
|
|
|
|
else
|
|
|
|
activeNote (info.channel(), info.basic.param1, true);
|
|
|
|
}
|
2003-05-19 19:24:22 +00:00
|
|
|
_driver->send (info.event | info.basic.param1 << 8 | info.basic.param2 << 16);
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
if (!_abort_parse) {
|
|
|
|
_position._last_event_time = event_time;
|
|
|
|
parseNextEvent (_next_event);
|
|
|
|
}
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
if (!_abort_parse) {
|
|
|
|
_position._play_time = end_time;
|
|
|
|
_position._play_tick = (_position._play_time - _position._last_event_time) / _psec_per_tick + _position._last_event_tick;
|
|
|
|
}
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MidiParser::allNotesOff() {
|
|
|
|
if (!_driver)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int i;
|
2003-05-24 19:11:49 +00:00
|
|
|
for (i = 0; i < 16; ++i)
|
2003-05-19 18:48:18 +00:00
|
|
|
_driver->send (0x007BB0 | i);
|
2003-05-22 15:34:30 +00:00
|
|
|
for (i = 0; i < ARRAYSIZE(_hanging_notes); ++i)
|
|
|
|
_hanging_notes[i].time_left = 0;
|
|
|
|
_hanging_notes_count = 0;
|
2003-05-23 04:19:47 +00:00
|
|
|
memset (_active_notes, 0, sizeof(_active_notes));
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MidiParser::resetTracking() {
|
2003-05-23 04:19:47 +00:00
|
|
|
_position.clear();
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
2003-05-21 06:14:14 +00:00
|
|
|
bool MidiParser::setTrack (int track) {
|
2003-07-31 06:50:13 +00:00
|
|
|
if (track < 0 || track >= _num_tracks)
|
2003-05-21 06:14:14 +00:00
|
|
|
return false;
|
|
|
|
else if (track == _active_track)
|
|
|
|
return true;
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
if (_smartJump)
|
|
|
|
hangAllActiveNotes();
|
|
|
|
else
|
|
|
|
allNotesOff();
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
resetTracking();
|
2003-07-26 19:37:54 +00:00
|
|
|
memset (_active_notes, 0, sizeof(_active_notes));
|
2003-05-19 18:48:18 +00:00
|
|
|
_active_track = track;
|
2003-05-23 04:19:47 +00:00
|
|
|
_position._play_pos = _tracks[track];
|
2003-05-19 18:48:18 +00:00
|
|
|
parseNextEvent (_next_event);
|
2003-05-21 06:14:14 +00:00
|
|
|
return true;
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
void MidiParser::hangAllActiveNotes() {
|
|
|
|
// Search for note off events until we have
|
|
|
|
// accounted for every active note.
|
2003-07-10 04:34:44 +00:00
|
|
|
uint16 temp_active [128];
|
|
|
|
memcpy (temp_active, _active_notes, sizeof (temp_active));
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
uint32 advance_tick = _position._last_event_tick;
|
|
|
|
while (true) {
|
2003-06-01 18:24:10 +00:00
|
|
|
int i, j;
|
2003-05-23 04:19:47 +00:00
|
|
|
for (i = 0; i < 128; ++i)
|
2003-07-10 04:34:44 +00:00
|
|
|
if (temp_active[i] != 0) break;
|
2003-05-23 04:19:47 +00:00
|
|
|
if (i == 128) break;
|
|
|
|
parseNextEvent (_next_event);
|
|
|
|
advance_tick += _next_event.delta;
|
|
|
|
if (_next_event.command() == 0x8) {
|
2003-07-10 04:34:44 +00:00
|
|
|
if (temp_active[_next_event.basic.param1] & (1 << _next_event.channel())) {
|
|
|
|
hangingNote (_next_event.channel(), _next_event.basic.param1, (advance_tick - _position._last_event_tick) * _psec_per_tick, false);
|
|
|
|
temp_active[_next_event.basic.param1] &= ~ (1 << _next_event.channel());
|
2003-05-22 15:34:30 +00:00
|
|
|
}
|
2003-05-23 04:19:47 +00:00
|
|
|
} else if (_next_event.event == 0xFF && _next_event.ext.type == 0x2F) {
|
2003-05-23 04:35:50 +00:00
|
|
|
// printf ("MidiParser::hangAllActiveNotes(): Hit End of Track with active notes left!\n");
|
2003-06-01 18:24:10 +00:00
|
|
|
for (i = 0; i < 128; ++i) {
|
|
|
|
for (j = 0; j < 16; ++j) {
|
2003-07-10 04:34:44 +00:00
|
|
|
if (temp_active[i] & (1 << j)) {
|
|
|
|
activeNote (j, i, false);
|
2003-06-01 18:24:10 +00:00
|
|
|
_driver->send (0x80 | j | i << 8);
|
2003-07-10 04:34:44 +00:00
|
|
|
}
|
2003-06-01 18:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-23 04:19:47 +00:00
|
|
|
break;
|
2003-05-22 15:34:30 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-23 04:19:47 +00:00
|
|
|
}
|
2003-05-22 15:34:30 +00:00
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
bool MidiParser::jumpToTick (uint32 tick, bool fireEvents) {
|
|
|
|
if (_active_track >= _num_tracks)
|
|
|
|
return false;
|
2003-05-19 18:48:18 +00:00
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
Tracker currentPos (_position);
|
|
|
|
EventInfo currentEvent (_next_event);
|
2003-05-19 18:48:18 +00:00
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
resetTracking();
|
|
|
|
_position._play_pos = _tracks[_active_track];
|
|
|
|
parseNextEvent (_next_event);
|
|
|
|
if (tick > 0) {
|
|
|
|
while (true) {
|
|
|
|
EventInfo &info = _next_event;
|
|
|
|
if (_position._last_event_tick + info.delta >= tick) {
|
|
|
|
_position._play_time += (tick - _position._last_event_tick) * _psec_per_tick;
|
|
|
|
_position._play_tick = tick;
|
2003-05-19 18:48:18 +00:00
|
|
|
break;
|
2003-05-23 04:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_position._last_event_tick += info.delta;
|
|
|
|
_position._last_event_time += info.delta * _psec_per_tick;
|
|
|
|
_position._play_tick = _position._last_event_tick;
|
|
|
|
_position._play_time = _position._last_event_time;
|
|
|
|
|
|
|
|
if (info.event == 0xFF) {
|
|
|
|
if (info.ext.type == 0x2F) { // End of track
|
2003-08-07 23:42:15 +00:00
|
|
|
_position = currentPos;
|
|
|
|
_next_event = currentEvent;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (info.ext.type == 0x51 && info.length >= 3) // Tempo
|
2003-05-23 04:19:47 +00:00
|
|
|
setTempo (info.ext.data[0] << 16 | info.ext.data[1] << 8 | info.ext.data[2]);
|
2003-08-07 23:42:15 +00:00
|
|
|
if (fireEvents)
|
|
|
|
_driver->metaEvent (info.ext.type, info.ext.data, (uint16) info.length);
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
2003-05-23 04:19:47 +00:00
|
|
|
} else if (fireEvents) {
|
|
|
|
if (info.event == 0xF0)
|
|
|
|
_driver->sysEx (info.ext.data, (uint16) info.length);
|
|
|
|
else
|
|
|
|
_driver->send (info.event | info.basic.param1 << 8 | info.basic.param2 << 16);
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
2003-05-23 04:19:47 +00:00
|
|
|
|
|
|
|
parseNextEvent (_next_event);
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
2003-05-23 04:19:47 +00:00
|
|
|
}
|
2003-05-19 18:48:18 +00:00
|
|
|
|
2003-05-25 15:47:06 +00:00
|
|
|
if (!_smartJump || !currentPos._play_pos) {
|
2003-05-23 04:19:47 +00:00
|
|
|
allNotesOff();
|
|
|
|
} else {
|
|
|
|
EventInfo targetEvent (_next_event);
|
|
|
|
Tracker targetPosition (_position);
|
|
|
|
|
|
|
|
_position = currentPos;
|
|
|
|
_next_event = currentEvent;
|
|
|
|
hangAllActiveNotes();
|
|
|
|
|
|
|
|
_next_event = targetEvent;
|
|
|
|
_position = targetPosition;
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
2003-05-23 04:19:47 +00:00
|
|
|
|
|
|
|
_abort_parse = true;
|
|
|
|
return true;
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
2003-05-25 15:47:06 +00:00
|
|
|
|
|
|
|
void MidiParser::unloadMusic() {
|
|
|
|
resetTracking();
|
|
|
|
allNotesOff();
|
|
|
|
_num_tracks = 0;
|
|
|
|
_active_track = 255;
|
|
|
|
_abort_parse = true;
|
|
|
|
}
|