2002-04-12 21:26:59 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:09:25 +00:00
|
|
|
* Copyright (C) 2001-2005 The ScummVM project
|
2002-04-12 21:26:59 +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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/scummsys.h"
|
2002-08-21 16:07:07 +00:00
|
|
|
#include "common/system.h"
|
2002-11-10 15:24:17 +00:00
|
|
|
#include "common/file.h"
|
2002-08-21 16:07:07 +00:00
|
|
|
#include "sound/mixer.h"
|
|
|
|
#include "simon/simon.h"
|
2002-04-12 21:26:59 +00:00
|
|
|
|
2003-10-03 19:42:27 +00:00
|
|
|
namespace Simon {
|
|
|
|
|
|
|
|
|
2003-05-25 03:19:21 +00:00
|
|
|
// MidiParser_S1D is not considered part of the standard
|
|
|
|
// MidiParser suite, but we still try to mask its details
|
|
|
|
// and just provide a factory function.
|
|
|
|
extern MidiParser *MidiParser_createS1D();
|
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
MidiPlayer::MidiPlayer(OSystem *system) {
|
2002-11-18 09:08:45 +00:00
|
|
|
// Since initialize() is called every time the music changes,
|
|
|
|
// this is where we'll initialize stuff that must persist
|
|
|
|
// between songs.
|
2003-05-20 13:20:18 +00:00
|
|
|
_driver = 0;
|
2003-07-14 08:29:17 +00:00
|
|
|
_map_mt32_to_gm = false;
|
2004-10-23 00:02:38 +00:00
|
|
|
_passThrough = false;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
_enable_sfx = true;
|
|
|
|
_current = 0;
|
|
|
|
|
2002-11-18 09:08:45 +00:00
|
|
|
_masterVolume = 255;
|
2003-08-11 05:25:33 +00:00
|
|
|
resetVolumeTable();
|
2003-05-20 13:20:18 +00:00
|
|
|
_paused = false;
|
2003-05-24 01:26:05 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
_currentTrack = 255;
|
2003-05-21 04:36:09 +00:00
|
|
|
_loopTrack = 0;
|
|
|
|
_queuedTrack = 255;
|
|
|
|
_loopQueuedTrack = 0;
|
2002-11-18 09:08:45 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
MidiPlayer::~MidiPlayer() {
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.lock();
|
2003-05-18 23:55:53 +00:00
|
|
|
close();
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.unlock();
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
int MidiPlayer::open() {
|
|
|
|
// Don't ever call open without first setting the output driver!
|
|
|
|
if (!_driver)
|
|
|
|
return 255;
|
2002-07-07 19:06:48 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
int ret = _driver->open();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2003-09-24 06:33:59 +00:00
|
|
|
_driver->setTimerCallback(this, &onTimer);
|
2003-05-18 23:55:53 +00:00
|
|
|
return 0;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::close() {
|
|
|
|
stop();
|
2004-02-28 12:58:13 +00:00
|
|
|
// _system->lockMutex (_mutex);
|
2003-05-18 23:55:53 +00:00
|
|
|
if (_driver)
|
|
|
|
_driver->close();
|
|
|
|
_driver = NULL;
|
|
|
|
clearConstructs();
|
2004-02-28 12:58:13 +00:00
|
|
|
// _system->unlockMutex (_mutex);
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
void MidiPlayer::send(uint32 b) {
|
2003-05-24 01:26:05 +00:00
|
|
|
if (!_current)
|
|
|
|
return;
|
|
|
|
|
2004-10-23 00:02:38 +00:00
|
|
|
if (_passThrough) {
|
|
|
|
_driver->send(b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-08-11 05:25:33 +00:00
|
|
|
byte channel = (byte) (b & 0x0F);
|
2003-05-18 23:55:53 +00:00
|
|
|
if ((b & 0xFFF0) == 0x07B0) {
|
2003-05-19 05:00:13 +00:00
|
|
|
// Adjust volume changes by master volume.
|
2003-08-11 05:25:33 +00:00
|
|
|
byte volume = (byte) ((b >> 16) & 0x7F);
|
2003-09-24 06:33:59 +00:00
|
|
|
_current->volume[channel] = volume;
|
2003-08-11 04:33:17 +00:00
|
|
|
volume = volume * _masterVolume / 255;
|
2003-05-18 23:55:53 +00:00
|
|
|
b = (b & 0xFF00FFFF) | (volume << 16);
|
2003-07-14 08:29:17 +00:00
|
|
|
} else if ((b & 0xF0) == 0xC0 && _map_mt32_to_gm) {
|
2005-04-03 22:01:38 +00:00
|
|
|
b = (b & 0xFFFF00FF) | (MidiDriver::_mt32ToGm[(b >> 8) & 0xFF] << 8);
|
2003-05-24 01:26:05 +00:00
|
|
|
} else if ((b & 0xFFF0) == 0x007BB0) {
|
|
|
|
// Only respond to an All Notes Off if this channel
|
2003-05-24 04:39:47 +00:00
|
|
|
// has already been allocated.
|
2003-09-24 06:33:59 +00:00
|
|
|
if (!_current->channel[b & 0x0F])
|
2003-05-24 01:26:05 +00:00
|
|
|
return;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
if (!_current->channel[channel])
|
2003-05-24 03:55:37 +00:00
|
|
|
_current->channel[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
|
2003-10-12 14:31:21 +00:00
|
|
|
if (_current->channel[channel]) {
|
|
|
|
if (channel == 9)
|
|
|
|
_current->channel[9]->volume (_current->volume[9] * _masterVolume / 255);
|
2003-09-24 06:33:59 +00:00
|
|
|
_current->channel[channel]->send(b);
|
2003-10-12 14:31:21 +00:00
|
|
|
}
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::metaEvent (byte type, byte *data, uint16 length) {
|
|
|
|
// Only thing we care about is End of Track.
|
2003-07-24 04:19:42 +00:00
|
|
|
if (!_current || type != 0x2F) {
|
2003-05-18 23:55:53 +00:00
|
|
|
return;
|
2003-07-24 04:19:42 +00:00
|
|
|
} else if (_current == &_sfx) {
|
2003-09-24 06:33:59 +00:00
|
|
|
clearConstructs(_sfx);
|
2003-07-24 04:19:42 +00:00
|
|
|
} else if (_loopTrack) {
|
2003-09-24 06:33:59 +00:00
|
|
|
_current->parser->jumpToTick(0);
|
2003-05-21 04:36:09 +00:00
|
|
|
} else if (_queuedTrack != 255) {
|
|
|
|
_currentTrack = 255;
|
|
|
|
byte destination = _queuedTrack;
|
|
|
|
_queuedTrack = 255;
|
|
|
|
_loopTrack = _loopQueuedTrack;
|
|
|
|
_loopQueuedTrack = false;
|
|
|
|
|
|
|
|
// Remember, we're still inside the locked mutex.
|
|
|
|
// Have to unlock it before calling jump()
|
|
|
|
// (which locks it itself), and then relock it
|
|
|
|
// upon returning.
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.unlock();
|
2003-05-21 06:13:47 +00:00
|
|
|
startTrack (destination);
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.lock();
|
2003-05-21 04:36:09 +00:00
|
|
|
} else {
|
|
|
|
stop();
|
|
|
|
}
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::onTimer (void *data) {
|
2003-05-24 01:26:05 +00:00
|
|
|
MidiPlayer *p = (MidiPlayer *) data;
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(p->_mutex);
|
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
if (!p->_paused) {
|
|
|
|
if (p->_music.parser && p->_currentTrack != 255) {
|
|
|
|
p->_current = &p->_music;
|
|
|
|
p->_music.parser->onTimer();
|
|
|
|
}
|
|
|
|
}
|
2003-07-14 12:15:17 +00:00
|
|
|
if (p->_sfx.parser) {
|
|
|
|
p->_current = &p->_sfx;
|
|
|
|
p->_sfx.parser->onTimer();
|
|
|
|
}
|
|
|
|
p->_current = 0;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-21 06:13:47 +00:00
|
|
|
void MidiPlayer::startTrack (int track) {
|
2003-05-20 17:10:15 +00:00
|
|
|
if (track == _currentTrack)
|
2003-05-18 23:55:53 +00:00
|
|
|
return;
|
2002-04-12 21:26:59 +00:00
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
if (_music.num_songs > 0) {
|
|
|
|
if (track >= _music.num_songs)
|
2003-05-18 23:55:53 +00:00
|
|
|
return;
|
2002-11-18 09:08:45 +00:00
|
|
|
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.lock();
|
2002-04-12 21:26:59 +00:00
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
if (_music.parser) {
|
2003-05-24 04:39:47 +00:00
|
|
|
_current = &_music;
|
2003-05-24 01:26:05 +00:00
|
|
|
delete _music.parser;
|
2003-05-24 04:39:47 +00:00
|
|
|
_current = 0;
|
2003-05-24 01:26:05 +00:00
|
|
|
_music.parser = 0;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
MidiParser *parser = MidiParser::createParser_SMF();
|
2003-05-19 05:00:13 +00:00
|
|
|
parser->property (MidiParser::mpMalformedPitchBends, 1);
|
2003-05-18 23:55:53 +00:00
|
|
|
parser->setMidiDriver (this);
|
|
|
|
parser->setTimerRate (_driver->getBaseTempo());
|
2003-05-24 01:26:05 +00:00
|
|
|
if (!parser->loadMusic (_music.songs[track], _music.song_sizes[track])) {
|
2003-05-18 23:55:53 +00:00
|
|
|
printf ("Error reading track!\n");
|
|
|
|
delete parser;
|
|
|
|
parser = 0;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
_currentTrack = (byte) track;
|
2003-05-24 01:26:05 +00:00
|
|
|
_music.parser = parser; // That plugs the power cord into the wall
|
|
|
|
} else if (_music.parser) {
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.lock();
|
2003-09-24 06:33:59 +00:00
|
|
|
if (!_music.parser->setTrack(track)) {
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.unlock();
|
2003-05-21 06:13:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-05-18 23:55:53 +00:00
|
|
|
_currentTrack = (byte) track;
|
2003-05-24 04:39:47 +00:00
|
|
|
_current = &_music;
|
2003-09-24 06:33:59 +00:00
|
|
|
_music.parser->jumpToTick(0);
|
2003-05-24 04:39:47 +00:00
|
|
|
_current = 0;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
2003-05-20 16:42:25 +00:00
|
|
|
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.unlock();
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::stop() {
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2003-05-24 04:39:47 +00:00
|
|
|
if (_music.parser) {
|
|
|
|
_current = &_music;
|
2003-05-24 01:26:05 +00:00
|
|
|
_music.parser->jumpToTick(0);
|
2003-05-24 04:39:47 +00:00
|
|
|
}
|
|
|
|
_current = 0;
|
2003-05-18 23:55:53 +00:00
|
|
|
_currentTrack = 255;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::pause (bool b) {
|
2003-05-20 16:42:25 +00:00
|
|
|
if (_paused == b || !_driver)
|
2003-05-18 23:55:53 +00:00
|
|
|
return;
|
|
|
|
_paused = b;
|
2002-04-12 21:26:59 +00:00
|
|
|
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-08-11 05:25:33 +00:00
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
if (_music.channel[i])
|
|
|
|
_music.channel[i]->volume (_paused ? 0 : (_music.volume[i] * _masterVolume / 255));
|
|
|
|
if (_sfx.channel[i])
|
|
|
|
_sfx.channel[i]->volume (_paused ? 0 : (_sfx.volume[i] * _masterVolume / 255));
|
|
|
|
}
|
2003-05-18 23:55:53 +00:00
|
|
|
}
|
2002-07-07 19:06:48 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::set_volume (int volume) {
|
|
|
|
if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
else if (volume > 255)
|
|
|
|
volume = 255;
|
2002-07-07 19:06:48 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
if (_masterVolume == volume)
|
|
|
|
return;
|
|
|
|
_masterVolume = volume;
|
2002-07-07 19:06:48 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
// Now tell all the channels this.
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-05-18 23:55:53 +00:00
|
|
|
if (_driver && !_paused) {
|
2003-08-11 05:25:33 +00:00
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
if (_music.channel[i])
|
2003-09-24 06:33:59 +00:00
|
|
|
_music.channel[i]->volume(_music.volume[i] * _masterVolume / 255);
|
2003-08-11 05:25:33 +00:00
|
|
|
if (_sfx.channel[i])
|
2003-09-24 06:33:59 +00:00
|
|
|
_sfx.channel[i]->volume(_sfx.volume[i] * _masterVolume / 255);
|
2003-05-18 23:55:53 +00:00
|
|
|
}
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::set_driver(MidiDriver *md) {
|
|
|
|
// Don't try to set this more than once.
|
|
|
|
if (_driver)
|
2003-01-03 12:06:30 +00:00
|
|
|
return;
|
2003-05-18 23:55:53 +00:00
|
|
|
_driver = md;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-07-14 08:29:17 +00:00
|
|
|
void MidiPlayer::mapMT32toGM (bool map) {
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2003-07-14 08:29:17 +00:00
|
|
|
_map_mt32_to_gm = map;
|
|
|
|
}
|
|
|
|
|
2003-05-21 04:36:09 +00:00
|
|
|
void MidiPlayer::setLoop (bool loop) {
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2003-05-21 04:36:09 +00:00
|
|
|
_loopTrack = loop;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:13:47 +00:00
|
|
|
void MidiPlayer::queueTrack (int track, bool loop) {
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.lock();
|
2003-05-21 04:36:09 +00:00
|
|
|
if (_currentTrack == 255) {
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.unlock();
|
2003-09-24 06:33:59 +00:00
|
|
|
setLoop(loop);
|
|
|
|
startTrack(track);
|
2003-05-21 04:36:09 +00:00
|
|
|
} else {
|
|
|
|
_queuedTrack = track;
|
|
|
|
_loopQueuedTrack = loop;
|
2005-01-28 22:05:51 +00:00
|
|
|
_mutex.unlock();
|
2003-05-21 04:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
void MidiPlayer::clearConstructs() {
|
2003-09-24 06:33:59 +00:00
|
|
|
clearConstructs(_music);
|
|
|
|
clearConstructs(_sfx);
|
2003-05-24 01:26:05 +00:00
|
|
|
}
|
2002-04-12 21:26:59 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
void MidiPlayer::clearConstructs(MusicInfo &info) {
|
2003-05-24 01:42:57 +00:00
|
|
|
int i;
|
2003-05-24 01:26:05 +00:00
|
|
|
if (info.num_songs > 0) {
|
|
|
|
for (i = 0; i < info.num_songs; ++i)
|
2003-09-24 06:33:59 +00:00
|
|
|
free (info.songs[i]);
|
2003-07-24 04:19:42 +00:00
|
|
|
info.num_songs = 0;
|
2003-05-18 23:55:53 +00:00
|
|
|
}
|
2003-07-24 04:19:42 +00:00
|
|
|
|
|
|
|
if (info.data) {
|
2003-09-24 06:33:59 +00:00
|
|
|
free(info.data);
|
2003-07-24 04:19:42 +00:00
|
|
|
info.data = 0;
|
|
|
|
} // end if
|
|
|
|
|
|
|
|
if (info.parser) {
|
2003-05-24 01:26:05 +00:00
|
|
|
delete info.parser;
|
2003-07-24 04:19:42 +00:00
|
|
|
info.parser = 0;
|
|
|
|
}
|
|
|
|
|
2003-05-24 01:42:57 +00:00
|
|
|
if (_driver) {
|
|
|
|
for (i = 0; i < 16; ++i) {
|
2003-05-24 03:55:37 +00:00
|
|
|
if (info.channel[i]) {
|
2003-08-11 05:25:33 +00:00
|
|
|
info.channel[i]->allNotesOff();
|
2003-05-24 03:55:37 +00:00
|
|
|
info.channel[i]->release();
|
|
|
|
}
|
2003-05-24 01:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-24 01:26:05 +00:00
|
|
|
info.clear();
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-08-11 05:25:33 +00:00
|
|
|
void MidiPlayer::resetVolumeTable() {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
_music.volume[i] = _sfx.volume[i] = 127;
|
|
|
|
if (_driver)
|
|
|
|
_driver->send (((_masterVolume >> 1) << 16) | 0x7B0 | i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-19 05:00:13 +00:00
|
|
|
static int simon1_gmf_size[] = {
|
|
|
|
8900, 12166, 2848, 3442, 4034, 4508, 7064, 9730, 6014, 4742, 3138,
|
|
|
|
6570, 5384, 8909, 6457, 16321, 2742, 8968, 4804, 8442, 7717,
|
|
|
|
9444, 5800, 1381, 5660, 6684, 2456, 4744, 2455, 1177, 1232,
|
|
|
|
17256, 5103, 8794, 4884, 16
|
|
|
|
};
|
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
void MidiPlayer::loadSMF (Common::File *in, int song, bool sfx) {
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
MusicInfo *p = sfx ? &_sfx : &_music;
|
|
|
|
clearConstructs (*p);
|
2003-05-20 16:50:16 +00:00
|
|
|
|
2003-07-24 04:19:42 +00:00
|
|
|
uint32 startpos = in->pos();
|
|
|
|
byte header[4];
|
2003-09-24 06:33:59 +00:00
|
|
|
in->read(header, 4);
|
2003-07-24 04:19:42 +00:00
|
|
|
bool isGMF = !memcmp (header, "GMF\x1", 4);
|
2003-09-24 06:33:59 +00:00
|
|
|
in->seek(startpos, SEEK_SET);
|
2003-07-24 04:19:42 +00:00
|
|
|
|
2003-05-20 17:23:15 +00:00
|
|
|
uint32 size = in->size() - in->pos();
|
2003-07-24 04:19:42 +00:00
|
|
|
if (isGMF) {
|
|
|
|
if (sfx) {
|
|
|
|
// Multiple GMF resources are stored in the SFX files,
|
|
|
|
// but each one is referenced by a pointer at the
|
|
|
|
// beginning of the file. Those pointers can be used
|
|
|
|
// to determine file size.
|
2003-09-24 06:33:59 +00:00
|
|
|
in->seek(0, SEEK_SET);
|
2003-07-24 04:19:42 +00:00
|
|
|
uint16 value = in->readUint16LE() >> 2; // Number of resources
|
|
|
|
if (song != value - 1) {
|
2003-09-24 06:33:59 +00:00
|
|
|
in->seek(song * 2 + 2, SEEK_SET);
|
2003-07-24 04:19:42 +00:00
|
|
|
value = in->readUint16LE();
|
|
|
|
size = value - startpos;
|
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
in->seek(startpos, SEEK_SET);
|
2003-07-24 04:19:42 +00:00
|
|
|
} else if (size >= 64000) {
|
|
|
|
// For GMF resources not in separate
|
|
|
|
// files, we're going to have to use
|
|
|
|
// hardcoded size tables.
|
2003-09-24 06:33:59 +00:00
|
|
|
size = simon1_gmf_size[song];
|
2003-07-24 04:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-20 17:23:15 +00:00
|
|
|
|
|
|
|
// When allocating space, add 4 bytes in case
|
|
|
|
// this is a GMF and we have to tack on our own
|
|
|
|
// End of Track event.
|
2003-09-24 06:33:59 +00:00
|
|
|
p->data = (byte *) calloc(size + 4, 1);
|
|
|
|
in->read(p->data, size);
|
2003-05-18 23:55:53 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
if (!memcmp(p->data, "GMF\x1", 4)) {
|
2003-05-24 03:55:37 +00:00
|
|
|
// BTW, here's what we know about the GMF header,
|
|
|
|
// the 7 bytes preceding the actual MIDI events.
|
|
|
|
// 3 BYTES: 'GMF'
|
|
|
|
// 1 BYTE : Always seems to be 0x01
|
|
|
|
// 1 BYTE : Always seems to be 0x00
|
|
|
|
// 1 BYTE : Ranges from 0x02 to 0x08 (always 0x02 for SFX, though)
|
|
|
|
// 1 BYTE : Loop control. 0 = no loop, 1 = loop
|
2004-09-08 09:24:18 +00:00
|
|
|
if (!sfx) {
|
|
|
|
// According to bug #1004919 calling setLoop() from
|
|
|
|
// within a lock causes a lockup, though I have no
|
|
|
|
// idea when this actually happens.
|
|
|
|
_loopTrack = (p->data[6] != 0);
|
|
|
|
}
|
2003-05-24 03:10:14 +00:00
|
|
|
}
|
2003-05-19 05:00:13 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
MidiParser *parser = MidiParser::createParser_SMF();
|
2003-09-24 06:33:59 +00:00
|
|
|
parser->property(MidiParser::mpMalformedPitchBends, 1);
|
|
|
|
parser->setMidiDriver(this);
|
|
|
|
parser->setTimerRate(_driver->getBaseTempo());
|
|
|
|
if (!parser->loadMusic(p->data, size)) {
|
2003-05-18 23:55:53 +00:00
|
|
|
printf ("Error reading track!\n");
|
|
|
|
delete parser;
|
|
|
|
parser = 0;
|
2002-04-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
if (!sfx) {
|
|
|
|
_currentTrack = 255;
|
2003-08-11 05:25:33 +00:00
|
|
|
resetVolumeTable();
|
2003-05-24 01:26:05 +00:00
|
|
|
}
|
|
|
|
p->parser = parser; // That plugs the power cord into the wall
|
2002-04-13 10:41:27 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
void MidiPlayer::loadMultipleSMF (Common::File *in, bool sfx) {
|
2003-05-18 23:55:53 +00:00
|
|
|
// This is a special case for Simon 2 Windows.
|
|
|
|
// Instead of having multiple sequences as
|
|
|
|
// separate tracks in a Type 2 file, simon2win
|
|
|
|
// has multiple songs, each of which is a Type 1
|
|
|
|
// file. Thus, preceding the songs is a single
|
|
|
|
// byte specifying how many songs are coming.
|
|
|
|
// We need to load ALL the songs and then
|
|
|
|
// treat them as separate tracks -- for the
|
|
|
|
// purpose of jumps, anyway.
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
MusicInfo *p = sfx ? &_sfx : &_music;
|
2003-09-24 06:33:59 +00:00
|
|
|
clearConstructs(*p);
|
2003-05-24 01:26:05 +00:00
|
|
|
|
|
|
|
p->num_songs = in->readByte();
|
|
|
|
if (p->num_songs > 16) {
|
|
|
|
printf ("playMultipleSMF: %d is too many songs to keep track of!\n", (int) p->num_songs);
|
2002-11-18 09:08:45 +00:00
|
|
|
return;
|
2003-05-18 23:55:53 +00:00
|
|
|
}
|
2002-11-18 09:08:45 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
byte i;
|
2003-05-24 01:26:05 +00:00
|
|
|
for (i = 0; i < p->num_songs; ++i) {
|
2003-05-18 23:55:53 +00:00
|
|
|
byte buf[5];
|
|
|
|
uint32 pos = in->pos();
|
|
|
|
|
|
|
|
// Make sure there's a MThd
|
2003-09-24 06:33:59 +00:00
|
|
|
in->read(buf, 4);
|
2003-05-18 23:55:53 +00:00
|
|
|
if (memcmp (buf, "MThd", 4)) {
|
|
|
|
printf ("Expected MThd but found '%c%c%c%c' instead!\n", buf[0], buf[1], buf[2], buf[3]);
|
|
|
|
return;
|
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
in->seek(in->readUint32BE() + in->pos(), SEEK_SET);
|
2003-05-18 23:55:53 +00:00
|
|
|
|
|
|
|
// Now skip all the MTrk blocks
|
|
|
|
while (true) {
|
|
|
|
in->read (buf, 4);
|
2003-09-24 06:33:59 +00:00
|
|
|
if (memcmp(buf, "MTrk", 4))
|
2003-05-18 23:55:53 +00:00
|
|
|
break;
|
2003-09-24 06:33:59 +00:00
|
|
|
in->seek(in->readUint32BE() + in->pos(), SEEK_SET);
|
2003-05-18 23:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 pos2 = in->pos() - 4;
|
|
|
|
uint32 size = pos2 - pos;
|
2003-09-24 06:33:59 +00:00
|
|
|
p->songs[i] = (byte *) calloc(size, 1);
|
|
|
|
in->seek(pos, SEEK_SET);
|
|
|
|
in->read(p->songs[i], size);
|
2003-05-24 01:26:05 +00:00
|
|
|
p->song_sizes[i] = size;
|
2002-11-18 09:08:45 +00:00
|
|
|
}
|
2002-11-17 17:15:29 +00:00
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
if (!sfx) {
|
|
|
|
_currentTrack = 255;
|
2003-08-11 05:25:33 +00:00
|
|
|
resetVolumeTable();
|
2003-05-24 01:26:05 +00:00
|
|
|
}
|
2002-11-17 17:15:29 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
void MidiPlayer::loadXMIDI(Common::File *in, bool sfx) {
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-05-24 01:26:05 +00:00
|
|
|
MusicInfo *p = sfx ? &_sfx : &_music;
|
2003-09-24 06:33:59 +00:00
|
|
|
clearConstructs(*p);
|
2003-05-18 23:55:53 +00:00
|
|
|
|
|
|
|
char buf[4];
|
|
|
|
uint32 pos = in->pos();
|
|
|
|
uint32 size = 4;
|
2003-09-24 06:33:59 +00:00
|
|
|
in->read(buf, 4);
|
|
|
|
if (!memcmp(buf, "FORM", 4)) {
|
2003-05-20 16:42:25 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 16; ++i) {
|
2003-09-24 06:33:59 +00:00
|
|
|
if (!memcmp(buf, "CAT ", 4))
|
2003-05-20 16:42:25 +00:00
|
|
|
break;
|
|
|
|
size += 2;
|
2003-09-24 06:33:59 +00:00
|
|
|
memcpy(buf, &buf[2], 2);
|
|
|
|
in->read(&buf[2], 2);
|
2003-05-20 16:42:25 +00:00
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
if (memcmp(buf, "CAT ", 4)) {
|
|
|
|
warning("Could not find 'CAT ' tag to determine resource size!");
|
2003-05-20 16:42:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-05-18 23:55:53 +00:00
|
|
|
size += 4 + in->readUint32BE();
|
2003-09-24 06:33:59 +00:00
|
|
|
in->seek(pos, 0);
|
|
|
|
p->data = (byte *) calloc(size, 1);
|
|
|
|
in->read(p->data, size);
|
2003-05-18 23:55:53 +00:00
|
|
|
} else {
|
2003-09-24 06:33:59 +00:00
|
|
|
warning("Expected 'FORM' tag but found '%c%c%c%c' instead!", buf[0], buf[1], buf[2], buf[3]);
|
2002-11-18 09:08:45 +00:00
|
|
|
return;
|
2003-05-18 23:55:53 +00:00
|
|
|
}
|
2002-11-18 09:08:45 +00:00
|
|
|
|
2003-05-18 23:55:53 +00:00
|
|
|
MidiParser *parser = MidiParser::createParser_XMIDI();
|
2003-09-24 06:33:59 +00:00
|
|
|
parser->setMidiDriver(this);
|
|
|
|
parser->setTimerRate(_driver->getBaseTempo());
|
|
|
|
if (!parser->loadMusic(p->data, size)) {
|
|
|
|
warning("Error reading track!");
|
2003-05-18 23:55:53 +00:00
|
|
|
delete parser;
|
|
|
|
parser = 0;
|
2002-11-18 09:08:45 +00:00
|
|
|
}
|
2002-04-13 10:41:27 +00:00
|
|
|
|
2003-05-24 01:26:05 +00:00
|
|
|
if (!sfx) {
|
|
|
|
_currentTrack = 255;
|
2003-08-11 05:25:33 +00:00
|
|
|
resetVolumeTable();
|
2003-05-24 01:26:05 +00:00
|
|
|
}
|
|
|
|
p->parser = parser; // That plugs the power cord into the wall
|
2002-04-13 18:34:11 +00:00
|
|
|
}
|
2003-05-25 03:19:21 +00:00
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
void MidiPlayer::loadS1D (Common::File *in, bool sfx) {
|
2004-09-04 00:04:36 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-05-25 03:19:21 +00:00
|
|
|
MusicInfo *p = sfx ? &_sfx : &_music;
|
2003-09-24 06:33:59 +00:00
|
|
|
clearConstructs(*p);
|
2003-05-25 03:19:21 +00:00
|
|
|
|
2003-05-25 21:32:11 +00:00
|
|
|
uint16 size = in->readUint16LE();
|
2003-05-25 03:19:21 +00:00
|
|
|
if (size != in->size() - 2) {
|
2003-09-24 06:33:59 +00:00
|
|
|
warning("Size mismatch in simon1demo MUS file (%ld versus reported %d)", (long) in->size() - 2, (int) size);
|
2003-05-25 03:19:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
p->data = (byte *) calloc(size, 1);
|
|
|
|
in->read(p->data, size);
|
2003-05-25 03:19:21 +00:00
|
|
|
|
|
|
|
MidiParser *parser = MidiParser_createS1D();
|
2003-09-24 06:33:59 +00:00
|
|
|
parser->setMidiDriver(this);
|
|
|
|
parser->setTimerRate(_driver->getBaseTempo());
|
|
|
|
if (!parser->loadMusic(p->data, size)) {
|
|
|
|
warning("Error reading track!");
|
2003-05-25 03:19:21 +00:00
|
|
|
delete parser;
|
|
|
|
parser = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sfx) {
|
|
|
|
_currentTrack = 255;
|
2003-08-11 05:25:33 +00:00
|
|
|
resetVolumeTable();
|
2003-05-25 03:19:21 +00:00
|
|
|
}
|
|
|
|
p->parser = parser; // That plugs the power cord into the wall
|
|
|
|
}
|
2003-10-03 19:42:27 +00:00
|
|
|
|
|
|
|
} // End of namespace Simon
|