2005-01-09 16:06:29 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2004-2005 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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-24 16:01:42 +00:00
|
|
|
#include "kyra/sound.h"
|
|
|
|
#include "kyra/resource.h"
|
2004-11-11 13:37:35 +00:00
|
|
|
|
|
|
|
namespace Kyra {
|
|
|
|
MusicPlayer::MusicPlayer(MidiDriver* driver, KyraEngine* engine) {
|
|
|
|
_engine = engine;
|
|
|
|
_driver = driver;
|
2004-11-14 14:11:54 +00:00
|
|
|
_passThrough = false;
|
2004-11-11 13:37:35 +00:00
|
|
|
_isPlaying = _nativeMT32 = false;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
memset(_channel, 0, sizeof(MidiChannel*) * 16);
|
|
|
|
memset(_channelVolume, 255, sizeof(uint8) * 16);
|
|
|
|
_volume = 0;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
int ret = open();
|
|
|
|
if (ret != MERR_ALREADY_OPEN && ret != 0) {
|
|
|
|
error("couldn't open midi driver");
|
|
|
|
}
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
MusicPlayer::~MusicPlayer() {
|
|
|
|
_driver->setTimerCallback(NULL, NULL);
|
|
|
|
close();
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
void MusicPlayer::setVolume(int volume) {
|
|
|
|
if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
else if (volume > 255)
|
|
|
|
volume = 255;
|
|
|
|
|
|
|
|
if (_volume == volume)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_volume = volume;
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
if (_channel[i]) {
|
|
|
|
_channel[i]->volume(_channelVolume[i] * _volume / 255);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
int MusicPlayer::open() {
|
|
|
|
// Don't ever call open without first setting the output driver!
|
|
|
|
if (!_driver)
|
|
|
|
return 255;
|
|
|
|
|
|
|
|
int ret = _driver->open();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
_driver->setTimerCallback(this, &onTimer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::close() {
|
|
|
|
if (_driver)
|
|
|
|
_driver->close();
|
|
|
|
_driver = 0;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
void MusicPlayer::send(uint32 b) {
|
2004-11-14 14:11:54 +00:00
|
|
|
if (_passThrough) {
|
|
|
|
_driver->send(b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
uint8 channel = (byte)(b & 0x0F);
|
|
|
|
if ((b & 0xFFF0) == 0x07B0) {
|
|
|
|
// Adjust volume changes by master volume
|
|
|
|
uint8 volume = (uint8)((b >> 16) & 0x7F);
|
|
|
|
_channelVolume[channel] = volume;
|
|
|
|
volume = volume * _volume / 255;
|
|
|
|
b = (b & 0xFF00FFFF) | (volume << 16);
|
|
|
|
} else if ((b & 0xF0) == 0xC0 && !_nativeMT32) {
|
2005-04-03 22:01:38 +00:00
|
|
|
b = (b & 0xFFFF00FF) | MidiDriver::_mt32ToGm[(b >> 8) & 0xFF] << 8;
|
2004-11-11 13:37:35 +00:00
|
|
|
} else if ((b & 0xFFF0) == 0x007BB0) {
|
|
|
|
//Only respond to All Notes Off if this channel
|
|
|
|
//has currently been allocated
|
|
|
|
if (!_channel[channel])
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_channel[channel])
|
|
|
|
_channel[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
|
|
|
|
|
|
|
|
if (_channel[channel])
|
|
|
|
_channel[channel]->send(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::metaEvent(byte type, byte *data, uint16 length) {
|
|
|
|
switch (type) {
|
|
|
|
case 0x2F: // End of Track
|
|
|
|
_parser->jumpToTick(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warning("Unhandled meta event: 0x%02x", type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
void MusicPlayer::playMusic(const char* file) {
|
|
|
|
uint32 size;
|
|
|
|
uint8* data = 0;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
data = (_engine->resManager())->fileData(file, &size);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
if (!data) {
|
|
|
|
warning("couldn't load '%s'", file);
|
|
|
|
return;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
playMusic(data, size);
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
void MusicPlayer::playMusic(uint8* data, uint32 size) {
|
|
|
|
if (_isPlaying)
|
|
|
|
stopMusic();
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
_parser = MidiParser::createParser_XMIDI();
|
|
|
|
assert(_parser);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
if (!_parser->loadMusic(data, size)) {
|
|
|
|
warning("Error reading track!");
|
|
|
|
delete _parser;
|
|
|
|
_parser = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_parser->setTrack(0);
|
|
|
|
_parser->setMidiDriver(this);
|
|
|
|
_parser->setTimerRate(getBaseTempo());
|
|
|
|
|
|
|
|
_isPlaying = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::stopMusic() {
|
|
|
|
_isPlaying = false;
|
|
|
|
if (_parser) {
|
|
|
|
_parser->unloadMusic();
|
|
|
|
delete _parser;
|
|
|
|
_parser = NULL;
|
|
|
|
}
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
void MusicPlayer::onTimer(void *refCon) {
|
|
|
|
MusicPlayer *music = (MusicPlayer *)refCon;
|
|
|
|
if (music->_isPlaying)
|
|
|
|
music->_parser->onTimer();
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
void MusicPlayer::playTrack(uint8 track) {
|
|
|
|
if (_parser) {
|
|
|
|
_isPlaying = true;
|
|
|
|
_parser->setTrack(track);
|
|
|
|
_parser->jumpToTick(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end of namespace Kyra
|