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-18 14:25:33 +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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2003-05-18 14:25:33 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2003-05-18 14:25:33 +00:00
|
|
|
* 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-18 14:25:33 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/midiparser.h"
|
2020-05-29 15:50:14 +00:00
|
|
|
#include "audio/mididrv.h"
|
2020-06-26 20:23:08 +00:00
|
|
|
#include "audio/miles.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2003-05-18 14:25:33 +00:00
|
|
|
#include "common/util.h"
|
|
|
|
|
2003-10-03 23:34:06 +00:00
|
|
|
/**
|
|
|
|
* The XMIDI version of MidiParser.
|
2005-07-30 21:11:48 +00:00
|
|
|
*
|
2003-10-03 23:34:06 +00:00
|
|
|
* Much of this code is adapted from the XMIDI implementation from the exult
|
|
|
|
* project.
|
|
|
|
*/
|
2003-05-18 14:25:33 +00:00
|
|
|
class MidiParser_XMIDI : public MidiParser {
|
|
|
|
protected:
|
2020-05-28 18:45:43 +00:00
|
|
|
static const uint8 MAXIMUM_TRACK_BRANCHES = 128;
|
|
|
|
|
2008-07-06 18:37:52 +00:00
|
|
|
struct Loop {
|
|
|
|
byte *pos;
|
|
|
|
byte repeat;
|
|
|
|
};
|
|
|
|
|
|
|
|
Loop _loop[4];
|
|
|
|
int _loopCount;
|
|
|
|
|
2020-05-29 15:50:14 +00:00
|
|
|
/**
|
|
|
|
* The source number to use when sending MIDI messages to the driver.
|
|
|
|
* When using multiple sources, use source 0 and higher. This must be
|
|
|
|
* used when source volume or channel locking is used.
|
|
|
|
* By default this is -1, which means the parser is the only source
|
|
|
|
* of MIDI messages and multiple source functionality is disabled.
|
|
|
|
*/
|
|
|
|
int8 _source;
|
2020-05-28 18:45:43 +00:00
|
|
|
/**
|
|
|
|
* The sequence branches defined for each track. These point to
|
|
|
|
* positions in the MIDI data.
|
|
|
|
*/
|
2020-05-28 19:41:02 +00:00
|
|
|
byte *_trackBranches[MAXIMUM_TRACKS][MAXIMUM_TRACK_BRANCHES];
|
2020-05-29 15:50:14 +00:00
|
|
|
|
2008-08-10 17:59:42 +00:00
|
|
|
XMidiCallbackProc _callbackProc;
|
|
|
|
void *_callbackData;
|
|
|
|
|
2015-06-30 14:05:01 +00:00
|
|
|
// We need to support XMIDI TIMB for 7th guest, which uses
|
|
|
|
// Miles Audio drivers. The MT32 driver needs to get the TIMB chunk, so that it
|
|
|
|
// can install all required timbres before the song starts playing.
|
2020-06-26 20:23:08 +00:00
|
|
|
// This contains a pointer to _driver if it supports the required
|
|
|
|
// interface; otherwise it is null.
|
|
|
|
Audio::MidiDriver_Miles_Xmidi_Timbres *_newTimbreListDriver;
|
2015-06-30 14:05:01 +00:00
|
|
|
|
|
|
|
byte *_tracksTimbreList[120]; ///< Timbre-List for each track.
|
|
|
|
uint32 _tracksTimbreListSize[120]; ///< Size of the Timbre-List for each track.
|
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
protected:
|
2004-04-29 11:51:11 +00:00
|
|
|
uint32 readVLQ2(byte * &data);
|
2020-05-28 18:45:43 +00:00
|
|
|
/**
|
|
|
|
* Platform independent LE uint32 read-and-advance.
|
|
|
|
* This helper function reads Little Endian 32-bit numbers
|
|
|
|
* from a memory pointer, at the same time advancing
|
|
|
|
* the pointer.
|
|
|
|
*/
|
|
|
|
uint32 read4low(byte *&data);
|
|
|
|
|
2020-06-12 19:53:49 +00:00
|
|
|
void parseNextEvent(EventInfo &info) override;
|
2003-05-18 23:55:53 +00:00
|
|
|
|
2020-06-12 19:53:49 +00:00
|
|
|
virtual void resetTracking() override {
|
2013-05-04 19:57:00 +00:00
|
|
|
MidiParser::resetTracking();
|
|
|
|
_loopCount = -1;
|
|
|
|
}
|
2020-06-04 16:19:40 +00:00
|
|
|
void onTrackStart(uint8 track) override;
|
2013-05-04 19:57:00 +00:00
|
|
|
|
2020-05-29 15:50:14 +00:00
|
|
|
void sendToDriver(uint32 b) override;
|
|
|
|
void sendMetaEventToDriver(byte type, byte *data, uint16 length) override;
|
2003-05-18 14:25:33 +00:00
|
|
|
public:
|
2020-06-26 20:23:08 +00:00
|
|
|
MidiParser_XMIDI(XMidiCallbackProc proc, void *data, int8 source = -1) :
|
2020-05-29 15:50:14 +00:00
|
|
|
_callbackProc(proc),
|
|
|
|
_callbackData(data),
|
2020-06-26 20:23:08 +00:00
|
|
|
_newTimbreListDriver(0),
|
2020-05-29 15:50:14 +00:00
|
|
|
_source(source),
|
2020-06-04 16:19:40 +00:00
|
|
|
_loopCount(-1) {
|
2020-05-29 15:50:14 +00:00
|
|
|
memset(_loop, 0, sizeof(_loop));
|
2020-05-28 18:45:43 +00:00
|
|
|
memset(_trackBranches, 0, sizeof(_trackBranches));
|
2015-06-30 14:05:01 +00:00
|
|
|
memset(_tracksTimbreList, 0, sizeof(_tracksTimbreList));
|
|
|
|
memset(_tracksTimbreListSize, 0, sizeof(_tracksTimbreListSize));
|
|
|
|
}
|
2020-06-27 20:16:43 +00:00
|
|
|
~MidiParser_XMIDI() { stopPlaying(); }
|
2003-05-19 00:12:16 +00:00
|
|
|
|
2020-06-26 20:23:08 +00:00
|
|
|
void setMidiDriver(MidiDriver_BASE *driver) override;
|
2020-06-12 19:53:49 +00:00
|
|
|
bool loadMusic(byte *data, uint32 size) override;
|
2020-05-28 18:45:43 +00:00
|
|
|
bool hasJumpIndex(uint8 index) override;
|
|
|
|
bool jumpToIndex(uint8 index, bool stopNotes) override;
|
2003-05-18 14:25:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is a special XMIDI variable length quantity
|
2004-04-29 11:51:11 +00:00
|
|
|
uint32 MidiParser_XMIDI::readVLQ2(byte * &pos) {
|
2003-05-18 14:25:33 +00:00
|
|
|
uint32 value = 0;
|
2005-09-11 14:35:34 +00:00
|
|
|
while (!(pos[0] & 0x80)) {
|
2003-05-18 14:25:33 +00:00
|
|
|
value += *pos++;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-05-28 18:45:43 +00:00
|
|
|
uint32 MidiParser_XMIDI::read4low(byte *&data) {
|
|
|
|
uint32 val = READ_LE_UINT32(data);
|
|
|
|
data += 4;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MidiParser_XMIDI::hasJumpIndex(uint8 index) {
|
2020-06-13 17:31:32 +00:00
|
|
|
if (_activeTrack >= _numTracks)
|
2020-05-28 18:45:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return index < MAXIMUM_TRACK_BRANCHES && _trackBranches[_activeTrack][index] != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MidiParser_XMIDI::jumpToIndex(uint8 index, bool stopNotes) {
|
KYRA/MIDI: Fix minor MT-32/GM issues (#2333)
* KYRA: Added GM initialization for Lands of Lore
The original interpreter of Lands of Lore uses two executables, which
both do initialization of MIDI devices. The main executable runs a
sysex file for GM devices; the intro executable does not.
ScummVM only does MIDI initialization once and it performs this like
the intro executable. I've added the GM initialization of the main
executable. Note that the initialization file consists mostly of
sysexes which alter the display of the SC-55, so it's not particulary
useful. Not many games did this though, so I thought it would be a
fun feature to include.
I also noticed that the check which distinguishes between the two
demo versions of LoL did not work properly; the useAltShapeHeader
flag was true for both versions. I've changed it to check for the
existence of a PAK file which was only included with one of the two
demos.
* MIDI: Delay parser after handling SysEx events
This changes the way delays between SysEx events in MIDI data are handled from
delaying the backend to delaying the MidiParser.
SysEx events require a delay between events to ensure correct processing by the
MIDI device. This is usually implemented by calling OSystem::delayMillis.
Some games use some form of MIDI sequence filled with SysEx messages to
initialize the MIDI device. This is handled by the MidiParser. Using the
delayMillis method causes the following MIDI events to be "bunched up" and be
executed in a shorter timespan than intended.
I've altered this by making the MidiParser stop parsing when a SysEx event is
encountered and not enough time has passed since the last SysEx. After enough
time has passed, the next SysEx is sent and parsing resumes. To facilitate
this, I've introduced an alternate sysExNoDelay fuction on the MidiDiver. This
does not execute the delay itself, but instead returns the required delay time,
so the parser can handle this instead.
I've currently only implemented this method for the Miles MT-32/GM driver. For
other driver implementations, this will call the regular sysEx method and
return a 0 delay time, so there will be no change in behavior.
This restores a sound effect at the end of the Legend of Kyrandia MT-32 MIDI
initialization. Before, the Note On and Note Off events would be transmitted
instantly after each other, causing the notes not to play.
* MIDI: Add instrument bank fallback
Some games rely on a feature of the Roland SC-55 v1.xx that corrects invalid
instrument banks. This feature was removed in later versions of the device and
not implemented in most other GM devices, causing some MIDI data (sound effects
mostly) to play incorrectly.
Specifically, this applies to Lands of Lore. For example, in the intro, the
sound effect of the ring sparkling uses an incorrect instrument bank. Depending
on how the MIDI device handles this, the sound will play correctly, with the
wrong instrument, or not at all.
This commit emulates the SC-55 functionality that corrects the invalid bank
numbers. I've implemented this (partially) on the MidiDriver, so that it can be
re-used for other games (Xeen 4 and 5 also have this issue with some sound
effects).
* KYRA: Start MIDI playback after selecting track
The MIDI parser would automatically start playback after loading MIDI data, but
KYRA engine games use MIDI files with multiple tracks. Because of this it was
necessary to immediately stop playback after loading MIDI data, and then select
the track that should be played for correct playback. The parser now has a
feature to disable automatically starting playback, so I've implemented this
for KYRA.
* KYRA: Improve stopping MIDI playback
In two places All Notes Off events were sent on all channels to stop MIDI
playback of the background music. However, this will also cut off any MIDI
sound effects which are playing. Where needed I've replaced this with simply
stopping playback of the background music; the parser will turn off any active
notes.
I've also made sure playback is stopped before freeing the music data
memory to prevent any issues.
I've added sending All Notes Off events when the game quits, just in case any
hanging notes are not ended by the parsers (should not really be a problem
though).
* MIDI/KYRA: Add pausing playback to MIDI parser
* KYRA: Fix invalid track selection
If a game would attempt to play an invalid track, the parser would start
playing the previously selected track. Fixed this so the parser will not start
playing.
2020-06-20 21:27:30 +00:00
|
|
|
if (_activeTrack >= _numTracks || _pause)
|
2020-05-28 18:45:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (index >= MAXIMUM_TRACK_BRANCHES || _trackBranches[_activeTrack][index] == 0) {
|
|
|
|
warning("MidiParser-XMIDI: jumpToIndex called with invalid sequence branch index %x", index);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent concurrent execution of multiple jumps
|
|
|
|
assert(!_jumpingToTick);
|
|
|
|
_jumpingToTick = true;
|
|
|
|
|
|
|
|
if (stopNotes) {
|
|
|
|
if (!_smartJump || !_position._playPos) {
|
|
|
|
allNotesOff();
|
|
|
|
} else {
|
|
|
|
hangAllActiveNotes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resetTracking();
|
|
|
|
_position._playPos = _trackBranches[_activeTrack][index];
|
|
|
|
parseNextEvent(_nextEvent);
|
|
|
|
|
|
|
|
_jumpingToTick = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-04-29 11:51:11 +00:00
|
|
|
void MidiParser_XMIDI::parseNextEvent(EventInfo &info) {
|
2012-09-07 20:57:14 +00:00
|
|
|
info.start = _position._playPos;
|
|
|
|
info.delta = readVLQ2(_position._playPos);
|
2003-05-18 23:55:53 +00:00
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
// Process the next event.
|
2012-09-07 20:57:14 +00:00
|
|
|
info.event = *(_position._playPos++);
|
2003-05-19 18:48:18 +00:00
|
|
|
switch (info.event >> 4) {
|
|
|
|
case 0x9: // Note On
|
2012-09-07 20:57:14 +00:00
|
|
|
info.basic.param1 = *(_position._playPos++);
|
|
|
|
info.basic.param2 = *(_position._playPos++);
|
|
|
|
info.length = readVLQ(_position._playPos);
|
2020-05-29 20:34:30 +00:00
|
|
|
if (info.length == 0) {
|
|
|
|
// Notes with length 0 are played with a very short duration by the AIL driver.
|
|
|
|
// However, the MidiParser will treat notes with length 0 as "active notes"; i.e.
|
|
|
|
// they will only get turned off when a corresponding Note Off event is encountered.
|
|
|
|
// Because XMIDI does not contain Note Off events, this will cause the note to hang.
|
|
|
|
// Set length to 1 to prevent this from happening.
|
|
|
|
info.length = 1;
|
|
|
|
}
|
2003-05-22 15:34:30 +00:00
|
|
|
if (info.basic.param2 == 0) {
|
|
|
|
info.event = info.channel() | 0x80;
|
|
|
|
info.length = 0;
|
2003-05-19 18:48:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-07-09 10:42:47 +00:00
|
|
|
case 0xC:
|
|
|
|
case 0xD:
|
2012-09-07 20:57:14 +00:00
|
|
|
info.basic.param1 = *(_position._playPos++);
|
2003-05-19 19:24:22 +00:00
|
|
|
info.basic.param2 = 0;
|
2003-05-19 18:48:18 +00:00
|
|
|
break;
|
|
|
|
|
2008-07-09 10:42:47 +00:00
|
|
|
case 0x8:
|
|
|
|
case 0xA:
|
|
|
|
case 0xE:
|
2012-09-07 20:57:14 +00:00
|
|
|
info.basic.param1 = *(_position._playPos++);
|
|
|
|
info.basic.param2 = *(_position._playPos++);
|
2008-07-09 10:42:47 +00:00
|
|
|
break;
|
|
|
|
|
2008-07-06 18:37:52 +00:00
|
|
|
case 0xB:
|
2012-09-07 20:57:14 +00:00
|
|
|
info.basic.param1 = *(_position._playPos++);
|
|
|
|
info.basic.param2 = *(_position._playPos++);
|
2008-07-06 18:37:52 +00:00
|
|
|
|
2008-08-10 17:59:42 +00:00
|
|
|
// This isn't a full XMIDI implementation, but it should
|
|
|
|
// hopefully be "good enough" for most things.
|
|
|
|
|
|
|
|
switch (info.basic.param1) {
|
2008-07-06 18:37:52 +00:00
|
|
|
// Simplified XMIDI looping.
|
2008-08-10 17:59:42 +00:00
|
|
|
case 0x74: { // XMIDI_CONTROLLER_FOR_LOOP
|
2012-09-07 20:57:14 +00:00
|
|
|
byte *pos = _position._playPos;
|
2008-08-10 17:59:42 +00:00
|
|
|
if (_loopCount < ARRAYSIZE(_loop) - 1)
|
|
|
|
_loopCount++;
|
2009-08-08 13:57:21 +00:00
|
|
|
else
|
|
|
|
warning("XMIDI: Exceeding maximum loop count %d", ARRAYSIZE(_loop));
|
2008-08-10 17:59:42 +00:00
|
|
|
|
|
|
|
_loop[_loopCount].pos = pos;
|
|
|
|
_loop[_loopCount].repeat = info.basic.param2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:51:34 +00:00
|
|
|
case 0x75: // XMIDI_CONTROLLER_NEXT_BREAK
|
2008-07-06 18:37:52 +00:00
|
|
|
if (_loopCount >= 0) {
|
|
|
|
if (info.basic.param2 < 64) {
|
2008-07-08 16:25:39 +00:00
|
|
|
// End the current loop.
|
2008-07-06 18:37:52 +00:00
|
|
|
_loopCount--;
|
2008-07-08 16:25:39 +00:00
|
|
|
} else {
|
2008-07-06 18:37:52 +00:00
|
|
|
// Repeat 0 means "loop forever".
|
|
|
|
if (_loop[_loopCount].repeat) {
|
|
|
|
if (--_loop[_loopCount].repeat == 0)
|
|
|
|
_loopCount--;
|
2009-08-08 13:57:21 +00:00
|
|
|
else
|
2012-09-07 20:57:14 +00:00
|
|
|
_position._playPos = _loop[_loopCount].pos;
|
2009-08-08 13:57:21 +00:00
|
|
|
} else {
|
2012-09-07 20:57:14 +00:00
|
|
|
_position._playPos = _loop[_loopCount].pos;
|
2008-07-06 18:37:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-10 17:59:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x77: // XMIDI_CONTROLLER_CALLBACK_TRIG
|
|
|
|
if (_callbackProc)
|
|
|
|
_callbackProc(info.basic.param2, _callbackData);
|
|
|
|
break;
|
|
|
|
|
2020-05-28 18:45:43 +00:00
|
|
|
case 0x78: // XMIDI_CONTROLLER_SEQ_BRANCH_INDEX
|
|
|
|
// This controller marks a branch point. It is converted
|
|
|
|
// to an entry in the RBRN header by the XMIDI conversion
|
|
|
|
// tool. For playback it is unnecessary.
|
|
|
|
break;
|
|
|
|
|
2009-05-06 14:22:13 +00:00
|
|
|
case 0x6e: // XMIDI_CONTROLLER_CHAN_LOCK
|
|
|
|
case 0x6f: // XMIDI_CONTROLLER_CHAN_LOCK_PROT
|
|
|
|
case 0x70: // XMIDI_CONTROLLER_VOICE_PROT
|
|
|
|
case 0x71: // XMIDI_CONTROLLER_TIMBRE_PROT
|
|
|
|
case 0x72: // XMIDI_CONTROLLER_BANK_CHANGE
|
2020-05-29 15:50:14 +00:00
|
|
|
// These controllers are handled in the Miles drivers
|
|
|
|
break;
|
|
|
|
|
2009-05-06 14:22:13 +00:00
|
|
|
case 0x73: // XMIDI_CONTROLLER_IND_CTRL_PREFIX
|
|
|
|
case 0x76: // XMIDI_CONTROLLER_CLEAR_BB_COUNT
|
2008-08-10 17:59:42 +00:00
|
|
|
default:
|
2020-05-28 18:45:43 +00:00
|
|
|
if (info.basic.param1 >= 0x73 && info.basic.param1 <= 0x76) {
|
2008-08-10 17:59:42 +00:00
|
|
|
warning("Unsupported XMIDI controller %d (0x%2x)",
|
|
|
|
info.basic.param1, info.basic.param1);
|
|
|
|
}
|
2019-11-17 08:20:01 +00:00
|
|
|
break;
|
2008-07-06 18:37:52 +00:00
|
|
|
}
|
2008-08-10 17:59:42 +00:00
|
|
|
|
|
|
|
// Should we really keep passing the XMIDI controller events to
|
|
|
|
// the MIDI driver, or should we turn them into some kind of
|
|
|
|
// NOP events? (Dummy meta events, perhaps?) Ah well, it has
|
|
|
|
// worked so far, so it shouldn't cause any damage...
|
|
|
|
|
2008-07-06 18:37:52 +00:00
|
|
|
break;
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
case 0xF: // Meta or SysEx event
|
|
|
|
switch (info.event & 0x0F) {
|
|
|
|
case 0x2: // Song Position Pointer
|
2012-09-07 20:57:14 +00:00
|
|
|
info.basic.param1 = *(_position._playPos++);
|
|
|
|
info.basic.param2 = *(_position._playPos++);
|
2003-05-18 14:25:33 +00:00
|
|
|
break;
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
case 0x3: // Song Select
|
2012-09-07 20:57:14 +00:00
|
|
|
info.basic.param1 = *(_position._playPos++);
|
2003-05-19 19:24:22 +00:00
|
|
|
info.basic.param2 = 0;
|
2003-05-18 14:25:33 +00:00
|
|
|
break;
|
|
|
|
|
2008-07-09 10:42:47 +00:00
|
|
|
case 0x6:
|
|
|
|
case 0x8:
|
|
|
|
case 0xA:
|
|
|
|
case 0xB:
|
|
|
|
case 0xC:
|
|
|
|
case 0xE:
|
2003-05-19 19:24:22 +00:00
|
|
|
info.basic.param1 = info.basic.param2 = 0;
|
2003-05-18 14:25:33 +00:00
|
|
|
break;
|
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
case 0x0: // SysEx
|
2012-09-07 20:57:14 +00:00
|
|
|
info.length = readVLQ(_position._playPos);
|
|
|
|
info.ext.data = _position._playPos;
|
|
|
|
_position._playPos += info.length;
|
2003-05-19 18:48:18 +00:00
|
|
|
break;
|
2003-05-18 14:50:10 +00:00
|
|
|
|
2003-05-19 18:48:18 +00:00
|
|
|
case 0xF: // META event
|
2012-09-07 20:57:14 +00:00
|
|
|
info.ext.type = *(_position._playPos++);
|
|
|
|
info.length = readVLQ(_position._playPos);
|
|
|
|
info.ext.data = _position._playPos;
|
|
|
|
_position._playPos += info.length;
|
2003-05-22 15:34:30 +00:00
|
|
|
if (info.ext.type == 0x51 && info.length == 3) {
|
2003-05-19 18:48:18 +00:00
|
|
|
// Tempo event. We want to make these constant 500,000.
|
2003-05-19 19:24:22 +00:00
|
|
|
info.ext.data[0] = 0x07;
|
|
|
|
info.ext.data[1] = 0xA1;
|
|
|
|
info.ext.data[2] = 0x20;
|
2003-05-18 14:25:33 +00:00
|
|
|
}
|
2003-05-19 18:48:18 +00:00
|
|
|
break;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-02-16 13:30:41 +00:00
|
|
|
default:
|
|
|
|
warning("MidiParser_XMIDI::parseNextEvent: Unsupported event code %x", info.event);
|
2019-11-17 08:20:01 +00:00
|
|
|
break;
|
2003-05-18 14:25:33 +00:00
|
|
|
}
|
2019-11-17 08:20:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2003-05-18 14:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 20:23:08 +00:00
|
|
|
void MidiParser_XMIDI::setMidiDriver(MidiDriver_BASE *driver) {
|
|
|
|
MidiParser::setMidiDriver(driver);
|
|
|
|
_newTimbreListDriver = dynamic_cast<Audio::MidiDriver_Miles_Xmidi_Timbres *>(driver);
|
|
|
|
}
|
|
|
|
|
2004-04-29 11:51:11 +00:00
|
|
|
bool MidiParser_XMIDI::loadMusic(byte *data, uint32 size) {
|
2003-05-18 14:25:33 +00:00
|
|
|
uint32 i = 0;
|
|
|
|
byte *start;
|
|
|
|
uint32 len;
|
2012-09-07 20:57:14 +00:00
|
|
|
uint32 chunkLen;
|
2003-05-18 14:25:33 +00:00
|
|
|
char buf[32];
|
|
|
|
|
2008-07-06 18:37:52 +00:00
|
|
|
_loopCount = -1;
|
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
unloadMusic();
|
|
|
|
byte *pos = data;
|
|
|
|
|
2004-04-29 11:51:11 +00:00
|
|
|
if (!memcmp(pos, "FORM", 4)) {
|
2003-05-18 14:25:33 +00:00
|
|
|
pos += 4;
|
|
|
|
|
2005-07-30 21:11:48 +00:00
|
|
|
// Read length of
|
2004-04-29 11:51:11 +00:00
|
|
|
len = read4high(pos);
|
2003-05-18 14:25:33 +00:00
|
|
|
start = pos;
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
// XDIRless XMIDI, we can handle them here.
|
2005-07-30 21:11:48 +00:00
|
|
|
if (!memcmp(pos, "XMID", 4)) {
|
2004-04-29 11:51:11 +00:00
|
|
|
warning("XMIDI doesn't have XDIR");
|
2003-05-18 14:25:33 +00:00
|
|
|
pos += 4;
|
2012-09-07 20:57:14 +00:00
|
|
|
_numTracks = 1;
|
2004-04-29 11:51:11 +00:00
|
|
|
} else if (memcmp(pos, "XDIR", 4)) {
|
2011-05-25 14:48:51 +00:00
|
|
|
// Not an XMIDI that we recognize
|
2004-04-29 11:51:11 +00:00
|
|
|
warning("Expected 'XDIR' but found '%c%c%c%c'", pos[0], pos[1], pos[2], pos[3]);
|
2003-05-18 14:25:33 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Seems Valid
|
|
|
|
pos += 4;
|
2012-09-07 20:57:14 +00:00
|
|
|
_numTracks = 0;
|
2003-05-18 14:25:33 +00:00
|
|
|
|
|
|
|
for (i = 4; i < len; i++) {
|
|
|
|
// Read 4 bytes of type
|
2004-04-29 11:51:11 +00:00
|
|
|
memcpy(buf, pos, 4);
|
2003-05-18 14:25:33 +00:00
|
|
|
pos += 4;
|
|
|
|
|
|
|
|
// Read length of chunk
|
2012-09-07 20:57:14 +00:00
|
|
|
chunkLen = read4high(pos);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
// Add eight bytes
|
|
|
|
i += 8;
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2011-06-09 16:16:14 +00:00
|
|
|
if (memcmp(buf, "INFO", 4) == 0) {
|
|
|
|
// Must be at least 2 bytes long
|
2012-09-07 20:57:14 +00:00
|
|
|
if (chunkLen < 2) {
|
|
|
|
warning("Invalid chunk length %d for 'INFO' block", (int)chunkLen);
|
2011-06-09 16:16:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2012-09-07 20:57:14 +00:00
|
|
|
_numTracks = (byte)read2low(pos);
|
2003-05-18 14:25:33 +00:00
|
|
|
|
2012-09-07 20:57:14 +00:00
|
|
|
if (chunkLen > 2) {
|
|
|
|
warning("Chunk length %d is greater than 2", (int)chunkLen);
|
|
|
|
//pos += chunkLen - 2;
|
2011-06-09 16:16:14 +00:00
|
|
|
}
|
|
|
|
break;
|
2003-05-18 14:25:33 +00:00
|
|
|
}
|
2011-06-09 16:16:14 +00:00
|
|
|
|
|
|
|
// Must align
|
2012-09-07 20:57:14 +00:00
|
|
|
pos += (chunkLen + 1) & ~1;
|
|
|
|
i += (chunkLen + 1) & ~1;
|
2003-05-18 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't get to fill the header
|
2012-09-07 20:57:14 +00:00
|
|
|
if (_numTracks == 0) {
|
2004-04-29 11:51:11 +00:00
|
|
|
warning("Didn't find a valid track count");
|
2003-05-18 14:25:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
// Ok now to start part 2
|
|
|
|
// Goto the right place
|
|
|
|
pos = start + ((len + 1) & ~1);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2004-04-29 11:51:11 +00:00
|
|
|
if (memcmp(pos, "CAT ", 4)) {
|
2003-05-18 14:25:33 +00:00
|
|
|
// Not an XMID
|
2004-04-29 11:51:11 +00:00
|
|
|
warning("Expected 'CAT ' but found '%c%c%c%c'", pos[0], pos[1], pos[2], pos[3]);
|
2003-05-18 14:25:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pos += 4;
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
// Now read length of this track
|
2004-04-29 11:51:11 +00:00
|
|
|
len = read4high(pos);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2004-04-29 11:51:11 +00:00
|
|
|
if (memcmp(pos, "XMID", 4)) {
|
2003-05-18 14:25:33 +00:00
|
|
|
// Not an XMID
|
2004-04-29 11:51:11 +00:00
|
|
|
warning("Expected 'XMID' but found '%c%c%c%c'", pos[0], pos[1], pos[2], pos[3]);
|
2003-05-18 14:25:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pos += 4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok it's an XMIDI.
|
|
|
|
// We're going to identify and store the location for each track.
|
2012-09-07 20:57:14 +00:00
|
|
|
if (_numTracks > ARRAYSIZE(_tracks)) {
|
|
|
|
warning("Can only handle %d tracks but was handed %d", (int)ARRAYSIZE(_tracks), (int)_numTracks);
|
2003-05-18 14:25:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:57:14 +00:00
|
|
|
int tracksRead = 0;
|
2020-05-28 18:45:43 +00:00
|
|
|
uint32 branchOffsets[128];
|
|
|
|
memset(branchOffsets, 0, sizeof(branchOffsets));
|
2020-06-04 16:19:40 +00:00
|
|
|
memset(_trackBranches, 0, sizeof(_trackBranches));
|
|
|
|
memset(_tracksTimbreList, 0, sizeof(_tracksTimbreList));
|
|
|
|
memset(_tracksTimbreListSize, 0, sizeof(_tracksTimbreListSize));
|
2012-09-07 20:57:14 +00:00
|
|
|
while (tracksRead < _numTracks) {
|
2004-04-29 11:51:11 +00:00
|
|
|
if (!memcmp(pos, "FORM", 4)) {
|
2003-05-18 14:25:33 +00:00
|
|
|
// Skip this plus the 4 bytes after it.
|
|
|
|
pos += 8;
|
2004-04-29 11:51:11 +00:00
|
|
|
} else if (!memcmp(pos, "XMID", 4)) {
|
2003-05-18 14:25:33 +00:00
|
|
|
// Skip this.
|
|
|
|
pos += 4;
|
2004-04-29 11:51:11 +00:00
|
|
|
} else if (!memcmp(pos, "TIMB", 4)) {
|
2015-06-30 14:05:01 +00:00
|
|
|
// Custom timbres
|
|
|
|
// chunk data is as follows:
|
|
|
|
// UINT16LE timbre count (amount of custom timbres used by this track)
|
|
|
|
// BYTE patchId
|
|
|
|
// BYTE bankId
|
|
|
|
// * timbre count
|
2003-05-18 14:25:33 +00:00
|
|
|
pos += 4;
|
2004-04-29 11:51:11 +00:00
|
|
|
len = read4high(pos);
|
2015-06-30 14:05:01 +00:00
|
|
|
_tracksTimbreList[tracksRead] = pos; // Skip the length bytes
|
|
|
|
_tracksTimbreListSize[tracksRead] = len;
|
2003-05-18 14:25:33 +00:00
|
|
|
pos += (len + 1) & ~1;
|
2004-04-29 11:51:11 +00:00
|
|
|
} else if (!memcmp(pos, "EVNT", 4)) {
|
2003-05-18 14:25:33 +00:00
|
|
|
// Ahh! What we're looking for at last.
|
2012-09-07 20:57:14 +00:00
|
|
|
_tracks[tracksRead] = pos + 8; // Skip the EVNT and length bytes
|
2003-05-18 14:25:33 +00:00
|
|
|
pos += 4;
|
2004-04-29 11:51:11 +00:00
|
|
|
len = read4high(pos);
|
2003-05-18 14:25:33 +00:00
|
|
|
pos += (len + 1) & ~1;
|
2020-05-28 18:45:43 +00:00
|
|
|
// Calculate branch index positions using the track position we just found
|
|
|
|
for (int j = 0; j < MAXIMUM_TRACK_BRANCHES; ++j) {
|
|
|
|
if (branchOffsets[j] != 0) {
|
|
|
|
byte *branchPos = _tracks[tracksRead] + branchOffsets[j];
|
|
|
|
if (branchPos >= pos) {
|
|
|
|
warning("Invalid sequence branch position (after track end)");
|
|
|
|
branchPos = _tracks[tracksRead];
|
|
|
|
}
|
|
|
|
_trackBranches[tracksRead][j] = branchPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Clear the branch offsets for the next track
|
|
|
|
memset(branchOffsets, 0, sizeof(branchOffsets));
|
2012-09-07 20:57:14 +00:00
|
|
|
++tracksRead;
|
2020-01-27 00:40:36 +00:00
|
|
|
} else if (!memcmp(pos, "RBRN", 4)) {
|
2020-05-28 18:45:43 +00:00
|
|
|
// optional branch point offsets
|
2020-01-27 00:40:36 +00:00
|
|
|
pos += 4;
|
|
|
|
len = read4high(pos);
|
2020-05-28 18:45:43 +00:00
|
|
|
uint16 numBranches = (len - 2) / 6;
|
|
|
|
uint16 numBranches2 = read2low(pos);
|
|
|
|
if (numBranches != numBranches2) {
|
|
|
|
warning("Number of sequence branch definitions %d does not match RBRN block length %d", numBranches2, len);
|
|
|
|
numBranches = 0;
|
|
|
|
}
|
|
|
|
for (int j = 0; j < numBranches; ++j) {
|
|
|
|
uint16 index = read2low(pos);
|
|
|
|
if (index >= MAXIMUM_TRACK_BRANCHES) {
|
|
|
|
warning("Invalid sequence branch index value %x", index);
|
|
|
|
pos += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// This is the offset from the start of the track
|
|
|
|
branchOffsets[index] = read4low(pos);
|
|
|
|
}
|
2003-05-18 14:25:33 +00:00
|
|
|
} else {
|
2004-04-29 11:51:11 +00:00
|
|
|
warning("Hit invalid block '%c%c%c%c' while scanning for track locations", pos[0], pos[1], pos[2], pos[3]);
|
2003-05-18 14:25:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, we successfully established
|
|
|
|
// the locations for each of our tracks.
|
|
|
|
// 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....
|
2003-05-19 18:48:18 +00:00
|
|
|
_ppqn = 60;
|
|
|
|
resetTracking();
|
2004-04-29 11:51:11 +00:00
|
|
|
setTempo(500000);
|
2015-06-30 14:05:01 +00:00
|
|
|
|
2020-06-04 16:19:40 +00:00
|
|
|
// Start playback of the first track.
|
|
|
|
setTrack(0);
|
2015-06-30 14:05:01 +00:00
|
|
|
|
2003-05-18 14:25:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-04 16:19:40 +00:00
|
|
|
void MidiParser_XMIDI::onTrackStart(uint8 track) {
|
|
|
|
// Load custom timbres
|
2020-06-26 20:23:08 +00:00
|
|
|
if (_newTimbreListDriver && _tracksTimbreListSize[track] > 0)
|
|
|
|
_newTimbreListDriver->processXMIDITimbreChunk(_tracksTimbreList[track], _tracksTimbreListSize[track]);
|
2020-06-04 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 15:50:14 +00:00
|
|
|
void MidiParser_XMIDI::sendToDriver(uint32 b) {
|
|
|
|
if (_source < 0) {
|
|
|
|
MidiParser::sendToDriver(b);
|
|
|
|
} else {
|
|
|
|
_driver->send(_source, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiParser_XMIDI::sendMetaEventToDriver(byte type, byte *data, uint16 length) {
|
|
|
|
if (_source < 0) {
|
|
|
|
MidiParser::sendMetaEventToDriver(type, data, length);
|
|
|
|
} else {
|
|
|
|
_driver->metaEvent(_source, type, data, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-10 17:59:42 +00:00
|
|
|
void MidiParser::defaultXMidiCallback(byte eventData, void *data) {
|
|
|
|
warning("MidiParser: defaultXMidiCallback(%d)", eventData);
|
|
|
|
}
|
|
|
|
|
2020-06-26 20:23:08 +00:00
|
|
|
MidiParser *MidiParser::createParser_XMIDI(XMidiCallbackProc proc, void *data, int source) {
|
|
|
|
return new MidiParser_XMIDI(proc, data, source);
|
2008-08-10 17:59:42 +00:00
|
|
|
}
|