2003-05-18 14:25:33 +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-05-31 22:04:21 +00:00
|
|
|
//! \brief Declarations related to the MidiParser class
|
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
#ifndef INCLUDED_MIDIPARSER
|
|
|
|
#define INCLUDED_MIDIPARSER
|
|
|
|
|
|
|
|
class MidiParser;
|
|
|
|
|
|
|
|
#include "common/scummsys.h"
|
|
|
|
|
|
|
|
class MidiDriver;
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Support entities
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
//! Maintains time and position state within a MIDI stream.
|
|
|
|
/*! A single Tracker struct is used by MidiParser to keep track
|
|
|
|
* of its current position in the MIDI stream. The Tracker
|
|
|
|
* struct, however, allows alternative locations to be cached.
|
|
|
|
* See MidiParser::jumpToTick() for an example of tracking
|
|
|
|
* multiple locations within a MIDI stream. NOTE: It is
|
|
|
|
* important to also maintain pre-parsed EventInfo data for
|
|
|
|
* each Tracker location.
|
|
|
|
*/
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
struct Tracker {
|
2003-05-31 22:04:21 +00:00
|
|
|
byte * _play_pos; //!< A pointer to the next event to be parsed
|
|
|
|
uint32 _play_time; //!< Current time in microseconds; may be in between event times
|
|
|
|
uint32 _play_tick; //!< Current MIDI tick; may be in between event ticks
|
|
|
|
uint32 _last_event_time; //!< The time, in microseconds, of the last event that was parsed
|
|
|
|
uint32 _last_event_tick; //!< The tick at which the last parsed event occurs
|
|
|
|
byte _running_status; //!< Cached MIDI command, for MIDI streams that rely on implied event codes
|
2003-05-23 04:19:47 +00:00
|
|
|
|
|
|
|
Tracker() { clear(); }
|
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
//! Copy constructor for each duplication of Tracker information.
|
2003-05-23 04:19:47 +00:00
|
|
|
Tracker (const Tracker ©) :
|
|
|
|
_play_pos (copy._play_pos),
|
|
|
|
_play_time (copy._play_time),
|
|
|
|
_play_tick (copy._play_tick),
|
|
|
|
_last_event_time (copy._last_event_time),
|
|
|
|
_last_event_tick (copy._last_event_tick),
|
|
|
|
_running_status (copy._running_status)
|
|
|
|
{ }
|
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
//! Clears all data; used by the constructor for initialization.
|
2003-05-23 04:19:47 +00:00
|
|
|
void clear() {
|
|
|
|
_play_pos = 0;
|
|
|
|
_play_time = 0;
|
|
|
|
_play_tick = 0;
|
|
|
|
_last_event_time = 0;
|
|
|
|
_last_event_tick = 0;
|
|
|
|
_running_status = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
//! Provides comprehensive information on the next event in the MIDI stream.
|
|
|
|
/*! An EventInfo struct is instantiated by format-specific implementations
|
|
|
|
* of MidiParser::parseNextEvent() each time another event is needed.
|
|
|
|
*/
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
struct EventInfo {
|
2003-05-31 22:04:21 +00:00
|
|
|
byte * start; //!< Position in the MIDI stream where the event starts.
|
|
|
|
//!< For delta-based MIDI streams (e.g. SMF and XMIDI), this points to the delta.
|
|
|
|
uint32 delta; //!< The number of ticks after the previous event that this event should occur.
|
|
|
|
byte event; //!< Upper 4 bits are the command code, lower 4 bits are the MIDI channel.
|
|
|
|
//!< For META, event == 0xFF. For SysEx, event == 0xF0.
|
2003-05-19 18:48:18 +00:00
|
|
|
union {
|
|
|
|
struct {
|
2003-05-31 22:04:21 +00:00
|
|
|
byte param1; //!< The first parameter in a simple MIDI message.
|
|
|
|
byte param2; //!< The second parameter in a simple MIDI message.
|
2003-05-19 19:24:22 +00:00
|
|
|
} basic;
|
2003-05-19 18:48:18 +00:00
|
|
|
struct {
|
2003-05-31 22:04:21 +00:00
|
|
|
byte type; //!< For META events, this indicates the META type.
|
|
|
|
byte * data; //!< For META and SysEx events, this points to the start of the data.
|
2003-05-19 19:24:22 +00:00
|
|
|
} ext;
|
2003-05-19 18:48:18 +00:00
|
|
|
};
|
2003-05-31 22:04:21 +00:00
|
|
|
uint32 length; //!< For META and SysEx blocks, this indicates the length of the data.
|
|
|
|
//!< For Note On events, a non-zero value indicates that no Note Off event
|
|
|
|
//!< will occur, and the MidiParser will have to generate one itself.
|
|
|
|
//!< For all other events, this value should always be zero.
|
2003-05-19 18:48:18 +00:00
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
byte channel() { return event & 0x0F; } //!< Separates the MIDI channel from the event.
|
|
|
|
byte command() { return event >> 4; } //!< Separates the command code from the event.
|
2003-05-19 18:48:18 +00:00
|
|
|
};
|
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
//! Provides expiration tracking for hanging notes.
|
|
|
|
/*! Hanging notes are used when a MIDI format does not include explicit Note Off
|
|
|
|
* events, or when "Smart Jump" is enabled so that active notes are intelligently
|
|
|
|
* expired when a jump occurs. The NoteTimer struct keeps track of how much
|
|
|
|
* longer a note should remain active before being turned off.
|
|
|
|
*/
|
|
|
|
|
2003-05-22 15:34:30 +00:00
|
|
|
struct NoteTimer {
|
2003-05-31 22:04:21 +00:00
|
|
|
byte channel; //!< The MIDI channel on which the note was played
|
|
|
|
byte note; //!< The note number for the active note
|
|
|
|
uint32 time_left; //!< The time, in microseconds, remaining before the note should be turned off
|
2003-05-22 15:34:30 +00:00
|
|
|
NoteTimer() : channel(0), note(0), time_left(0) {}
|
|
|
|
};
|
|
|
|
|
2003-05-23 04:19:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MidiParser declaration
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
//! A framework and common functionality for parsing MIDI streams.
|
|
|
|
/*! The MidiParser provides a framework in which to load
|
|
|
|
* parse and traverse MIDI streams. Loading and parsing
|
|
|
|
* events for specific formats requires a class to be
|
|
|
|
* derived from MidiParser.
|
|
|
|
*
|
|
|
|
* The MidiParser must be provided with a MidiDriver interface,
|
|
|
|
* which it uses to transmit events. However, it does NOT
|
|
|
|
* automatically hook into the MidiDriver's timer callback
|
|
|
|
* or set up its timer rate from the MidiDriver. The client
|
2003-06-01 04:06:24 +00:00
|
|
|
* using the MidiParser must set the timer rate and provide
|
2003-05-31 22:04:21 +00:00
|
|
|
* a means of firing the MidiParser's onTimer() method at
|
|
|
|
* appropriate intervals. The onTimer() event may be called
|
|
|
|
* by the client or by manually hooking and unhooking the
|
|
|
|
* MidiParser to the MidiDriver's timer callback.
|
|
|
|
*/
|
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
class MidiParser {
|
2003-05-22 15:34:30 +00:00
|
|
|
private:
|
2003-05-31 22:04:21 +00:00
|
|
|
uint16 _active_notes[128]; //!< Each uint16 is a bit mask for channels that have that note on.
|
|
|
|
NoteTimer _hanging_notes[32]; //!< Maintains expiration info for up to 32 notes.
|
|
|
|
//!< Used for "Smart Jump" and MIDI formats that do not include explicit Note Off events.
|
|
|
|
byte _hanging_notes_count; //!< Count of hanging notes, used to optimize expiration.
|
2003-05-22 15:34:30 +00:00
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
protected:
|
2003-05-31 22:04:21 +00:00
|
|
|
MidiDriver *_driver; //!< The device to which all events will be transmitted.
|
|
|
|
uint32 _timer_rate; //!< The time in microseconds between onTimer() calls. Obtained from the MidiDriver.
|
|
|
|
uint32 _ppqn; //!< Pulses Per Quarter Note. (We refer to "pulses" as "ticks".)
|
|
|
|
uint32 _tempo; //!< Microseconds per quarter note.
|
|
|
|
uint32 _psec_per_tick; //!< Microseconds per tick (_tempo / _ppqn).
|
|
|
|
bool _autoLoop; //!< For lightweight clients that don't provide their own flow control.
|
|
|
|
bool _smartJump; //!< Support smart expiration of hanging notes when jumping
|
|
|
|
|
|
|
|
byte * _tracks[32]; //!< Multi-track MIDI formats are supported, up to 32 tracks.
|
|
|
|
byte _num_tracks; //!< Count of total tracks for multi-track MIDI formats. 1 for single-track formats.
|
|
|
|
byte _active_track; //!< Keeps track of the currently active track, in multi-track formats.
|
|
|
|
|
|
|
|
Tracker _position; //!< The current time/position in the active track.
|
|
|
|
EventInfo _next_event; //!< The next event to transmit. Events are preparsed
|
|
|
|
//!< so each event is parsed only once; this permits
|
|
|
|
//!< simulated events in certain formats.
|
|
|
|
bool _abort_parse; //!< If a jump or other operation interrupts parsing, flag to abort.
|
2003-05-19 18:48:18 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static uint32 readVLQ (byte * &data);
|
2003-05-20 03:27:45 +00:00
|
|
|
virtual void resetTracking();
|
|
|
|
virtual void allNotesOff();
|
2003-05-19 18:48:18 +00:00
|
|
|
virtual void parseNextEvent (EventInfo &info) = 0;
|
|
|
|
|
2003-05-22 15:34:30 +00:00
|
|
|
void activeNote (byte channel, byte note, bool active);
|
2003-07-10 04:34:44 +00:00
|
|
|
void hangingNote (byte channel, byte note, uint32 ticks_left, bool recycle = true);
|
2003-05-23 04:19:47 +00:00
|
|
|
void hangAllActiveNotes();
|
2003-05-22 15:34:30 +00:00
|
|
|
|
2003-05-31 22:04:21 +00:00
|
|
|
//! Platform independent BE uint32 read-and-advance.
|
2003-06-01 04:06:24 +00:00
|
|
|
/*! This helper function reads Big Endian 32-bit numbers
|
2003-05-31 22:04:21 +00:00
|
|
|
* from a memory pointer, at the same time advancing
|
|
|
|
* the pointer.
|
|
|
|
*/
|
2003-05-19 18:48:18 +00:00
|
|
|
uint32 read4high (byte * &data) {
|
|
|
|
uint32 val = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; ++i) val = (val << 8) | *data++;
|
|
|
|
return val;
|
|
|
|
}
|
2003-05-31 22:04:21 +00:00
|
|
|
|
|
|
|
//! Platform independent LE uint16 read-and-advance.
|
2003-06-01 04:06:24 +00:00
|
|
|
/*! This helper function reads Little Endian 16-bit numbers
|
2003-05-31 22:04:21 +00:00
|
|
|
* from a memory pointer, at the same time advancing
|
|
|
|
* the pointer.
|
|
|
|
*/
|
2003-05-19 18:48:18 +00:00
|
|
|
uint16 read2low (byte * &data) {
|
|
|
|
uint16 val = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 2; ++i) val |= (*data++) << (i * 8);
|
|
|
|
return val;
|
|
|
|
}
|
2003-05-18 14:25:33 +00:00
|
|
|
|
2003-05-19 05:00:13 +00:00
|
|
|
public:
|
2003-05-31 22:04:21 +00:00
|
|
|
//! Configuration options for MidiParser
|
|
|
|
/*! The following options can be set to modify MidiParser's
|
|
|
|
* behavior.
|
|
|
|
*
|
|
|
|
* \b mpMalformedPitchBends - Events containing a pitch bend
|
|
|
|
* command should be treated as single-byte padding before the
|
|
|
|
* real event. This allows the MidiParser to work with some
|
|
|
|
* malformed SMF files from Simon 1/2.
|
|
|
|
*
|
|
|
|
* \b mpAutoLoop - Sets auto-looping, which can be used by
|
|
|
|
* lightweight clients that don't provide their own flow control.
|
|
|
|
*
|
|
|
|
* \b mpSmartJump - Sets smart jumping, which intelligently
|
|
|
|
* expires notes that are active when a jump is made, rather
|
|
|
|
* than just cutting them off.
|
|
|
|
*/
|
2003-05-19 05:00:13 +00:00
|
|
|
enum {
|
2003-05-19 18:48:18 +00:00
|
|
|
mpMalformedPitchBends = 1,
|
2003-05-22 15:34:30 +00:00
|
|
|
mpAutoLoop = 2,
|
|
|
|
mpSmartJump = 3
|
2003-05-19 05:00:13 +00:00
|
|
|
};
|
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
public:
|
2003-05-19 18:48:18 +00:00
|
|
|
MidiParser();
|
2003-06-18 22:46:17 +00:00
|
|
|
virtual ~MidiParser() { allNotesOff(); }
|
2003-05-19 00:12:16 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
virtual bool loadMusic (byte *data, uint32 size) = 0;
|
2003-05-25 15:47:06 +00:00
|
|
|
virtual void unloadMusic();
|
2003-05-19 18:48:18 +00:00
|
|
|
virtual void property (int prop, int value);
|
2003-05-18 14:25:33 +00:00
|
|
|
|
|
|
|
void setMidiDriver (MidiDriver *driver) { _driver = driver; }
|
2003-05-23 04:19:47 +00:00
|
|
|
void setTimerRate (uint32 rate) { _timer_rate = rate; }
|
|
|
|
void setTempo (uint32 tempo);
|
2003-05-19 18:48:18 +00:00
|
|
|
void onTimer();
|
2003-05-18 14:25:33 +00:00
|
|
|
|
2003-05-21 06:14:14 +00:00
|
|
|
bool setTrack (int track);
|
2003-05-23 04:19:47 +00:00
|
|
|
bool jumpToTick (uint32 tick, bool fireEvents = false);
|
|
|
|
uint32 getTick() { return _position._play_tick; }
|
2003-08-16 09:34:19 +00:00
|
|
|
virtual uint32 getTime() { return _position._play_time; }
|
2003-05-18 14:25:33 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
static MidiParser *createParser_SMF();
|
2003-05-18 14:25:33 +00:00
|
|
|
static MidiParser *createParser_XMIDI();
|
2003-05-19 05:00:13 +00:00
|
|
|
static void timerCallback (void *data) { ((MidiParser *) data)->onTimer(); }
|
2003-05-18 14:25:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|