2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* 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.
|
2004-04-12 21:40:49 +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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
2006-02-11 12:44:16 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
*/
|
2007-07-31 18:08:40 +00:00
|
|
|
|
|
|
|
// MIDI and digital music class
|
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
#include "saga/rscfile.h"
|
2006-05-13 10:30:38 +00:00
|
|
|
#include "saga/sagaresnames.h"
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/music.h"
|
2004-12-15 00:24:12 +00:00
|
|
|
#include "saga/stream.h"
|
2004-10-12 11:45:53 +00:00
|
|
|
#include "sound/audiostream.h"
|
2004-05-01 13:04:31 +00:00
|
|
|
#include "sound/mididrv.h"
|
2004-04-29 03:52:59 +00:00
|
|
|
#include "sound/midiparser.h"
|
2004-04-29 04:24:55 +00:00
|
|
|
#include "common/config-manager.h"
|
2004-04-30 07:13:26 +00:00
|
|
|
#include "common/file.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
namespace Saga {
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-12 11:45:53 +00:00
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
|
2007-07-14 13:41:03 +00:00
|
|
|
class DigitalMusicInputStream : public Audio::AudioStream {
|
2004-10-12 11:45:53 +00:00
|
|
|
private:
|
2007-07-14 15:09:30 +00:00
|
|
|
Audio::AudioStream *_compressedStream;
|
2005-07-19 19:05:52 +00:00
|
|
|
ResourceContext *_context;
|
2007-07-14 15:06:06 +00:00
|
|
|
ResourceData * resourceData;
|
|
|
|
GameSoundTypes soundType;
|
2005-05-10 22:56:25 +00:00
|
|
|
Common::File *_file;
|
2005-07-19 19:05:52 +00:00
|
|
|
uint32 _filePos;
|
|
|
|
uint32 _startPos;
|
|
|
|
uint32 _endPos;
|
2004-10-12 11:45:53 +00:00
|
|
|
bool _finished;
|
|
|
|
bool _looping;
|
|
|
|
int16 _buf[BUFFER_SIZE];
|
|
|
|
const int16 *_bufferEnd;
|
|
|
|
const int16 *_pos;
|
2005-07-26 19:40:58 +00:00
|
|
|
const GameSoundInfo *_musicInfo;
|
2007-07-15 15:12:24 +00:00
|
|
|
Common::MemoryReadStream *_memoryStream;
|
2004-10-12 11:45:53 +00:00
|
|
|
|
|
|
|
void refill();
|
2005-07-19 19:05:52 +00:00
|
|
|
bool eosIntern() const {
|
|
|
|
return _pos >= _bufferEnd;
|
|
|
|
}
|
2004-10-12 11:45:53 +00:00
|
|
|
|
|
|
|
public:
|
2007-07-14 13:41:03 +00:00
|
|
|
DigitalMusicInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart);
|
|
|
|
~DigitalMusicInputStream();
|
2004-10-12 11:45:53 +00:00
|
|
|
|
2007-07-14 18:26:43 +00:00
|
|
|
void createCompressedStream();
|
2007-07-14 15:06:06 +00:00
|
|
|
|
2004-10-12 11:45:53 +00:00
|
|
|
int readBuffer(int16 *buffer, const int numSamples);
|
|
|
|
|
|
|
|
bool endOfData() const { return eosIntern(); }
|
2005-07-26 19:40:58 +00:00
|
|
|
bool isStereo() const { return _musicInfo->stereo; }
|
|
|
|
int getRate() const { return _musicInfo->frequency; }
|
2004-10-12 11:45:53 +00:00
|
|
|
};
|
|
|
|
|
2007-07-14 13:41:03 +00:00
|
|
|
DigitalMusicInputStream::DigitalMusicInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart)
|
2005-07-19 19:05:52 +00:00
|
|
|
: _context(context), _finished(false), _looping(looping), _bufferEnd(_buf + BUFFER_SIZE) {
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2007-07-14 13:41:03 +00:00
|
|
|
byte compressedHeader[10];
|
2004-10-12 11:45:53 +00:00
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
resourceData = vm->_resource->getResourceData(context, resourceId);
|
2005-07-19 19:05:52 +00:00
|
|
|
_file = context->getFile(resourceData);
|
2005-07-26 19:40:58 +00:00
|
|
|
_musicInfo = vm->getMusicInfo();
|
|
|
|
|
|
|
|
if (_musicInfo == NULL) {
|
2007-07-14 13:41:03 +00:00
|
|
|
error("DigitalMusicInputStream() wrong musicInfo");
|
|
|
|
}
|
|
|
|
|
2007-07-14 15:09:30 +00:00
|
|
|
_compressedStream = NULL;
|
2007-07-14 13:41:03 +00:00
|
|
|
|
2007-08-26 19:14:18 +00:00
|
|
|
if (Common::File::exists("music.cmp") || Common::File::exists("musicd.cmp")) {
|
2007-07-14 13:41:03 +00:00
|
|
|
// Read compressed header to determine compression type
|
|
|
|
_file->seek((long)resourceData->offset, SEEK_SET);
|
|
|
|
_file->read(compressedHeader, 9);
|
|
|
|
|
|
|
|
if (compressedHeader[0] == char(0)) {
|
|
|
|
soundType = kSoundMP3;
|
|
|
|
} else if (compressedHeader[0] == char(1)) {
|
|
|
|
soundType = kSoundOGG;
|
|
|
|
} else if (compressedHeader[0] == char(2)) {
|
|
|
|
soundType = kSoundFLAC;
|
|
|
|
}
|
|
|
|
|
2007-07-14 15:06:06 +00:00
|
|
|
createCompressedStream();
|
2007-07-14 13:41:03 +00:00
|
|
|
resourceData->offset += 9; // Skip compressed header
|
2005-07-26 19:40:58 +00:00
|
|
|
}
|
2004-10-12 11:45:53 +00:00
|
|
|
|
|
|
|
// Determine the end position
|
2006-03-24 18:15:57 +00:00
|
|
|
_filePos = resourceData->offset;
|
|
|
|
_endPos = _filePos + resourceData->size;
|
|
|
|
_startPos = _filePos + loopStart;
|
|
|
|
if (_startPos >= _endPos)
|
|
|
|
_startPos = _filePos;
|
2004-10-12 11:45:53 +00:00
|
|
|
|
|
|
|
// Read in initial data
|
|
|
|
refill();
|
|
|
|
}
|
|
|
|
|
2007-07-14 13:41:03 +00:00
|
|
|
DigitalMusicInputStream::~DigitalMusicInputStream() {
|
2007-07-14 15:09:30 +00:00
|
|
|
delete _compressedStream;
|
2007-07-14 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2007-07-14 15:06:06 +00:00
|
|
|
void DigitalMusicInputStream::createCompressedStream() {
|
2007-07-15 15:27:42 +00:00
|
|
|
uint numLoops = _looping ? 0 : 1;
|
2007-07-15 15:12:24 +00:00
|
|
|
_memoryStream = _file->readStream(resourceData->size);
|
|
|
|
|
2007-07-14 15:06:06 +00:00
|
|
|
switch (soundType) {
|
|
|
|
#ifdef USE_MAD
|
|
|
|
case kSoundMP3:
|
|
|
|
debug(1, "Playing MP3 compressed digital music");
|
2007-07-15 15:12:24 +00:00
|
|
|
_compressedStream = Audio::makeMP3Stream(_memoryStream, true, 0, 0, numLoops);
|
2007-07-14 15:06:06 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_VORBIS
|
|
|
|
case kSoundOGG:
|
|
|
|
debug(1, "Playing OGG compressed digital music");
|
2007-07-15 15:12:24 +00:00
|
|
|
_compressedStream = Audio::makeVorbisStream(_memoryStream, true, 0, 0, numLoops);
|
2007-07-14 15:06:06 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_FLAC
|
|
|
|
case kSoundFLAC:
|
|
|
|
debug(1, "Playing FLAC compressed digital music");
|
2007-07-15 15:12:24 +00:00
|
|
|
_compressedStream = Audio::makeFlacStream(_memoryStream, true, 0, 0, numLoops);
|
2007-07-14 15:06:06 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
// Unknown compression
|
|
|
|
error("Trying to play a compressed digital music, but the compression is not known");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-14 13:41:03 +00:00
|
|
|
int DigitalMusicInputStream::readBuffer(int16 *buffer, const int numSamples) {
|
2007-07-15 15:12:24 +00:00
|
|
|
if (_compressedStream != NULL)
|
2007-07-14 15:09:30 +00:00
|
|
|
return _compressedStream->readBuffer(buffer, numSamples);
|
2007-07-14 13:41:03 +00:00
|
|
|
|
2004-10-12 11:45:53 +00:00
|
|
|
int samples = 0;
|
|
|
|
while (samples < numSamples && !eosIntern()) {
|
2007-07-14 13:41:03 +00:00
|
|
|
int len = 0;
|
2007-07-15 15:12:24 +00:00
|
|
|
len = MIN(numSamples - samples, (int) (_bufferEnd - _pos));
|
|
|
|
memcpy(buffer, _pos, len * 2);
|
|
|
|
buffer += len;
|
|
|
|
_pos += len;
|
|
|
|
samples += len;
|
|
|
|
if (_pos >= _bufferEnd)
|
|
|
|
refill();
|
2004-10-12 11:45:53 +00:00
|
|
|
}
|
|
|
|
return samples;
|
|
|
|
}
|
|
|
|
|
2007-07-14 13:41:03 +00:00
|
|
|
void DigitalMusicInputStream::refill() {
|
2004-10-12 11:45:53 +00:00
|
|
|
if (_finished)
|
|
|
|
return;
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
uint32 lengthLeft;
|
2004-10-12 11:45:53 +00:00
|
|
|
byte *ptr = (byte *) _buf;
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2004-10-12 11:45:53 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
_file->seek(_filePos, SEEK_SET);
|
2004-10-12 11:45:53 +00:00
|
|
|
|
|
|
|
if (_looping)
|
2005-07-19 19:05:52 +00:00
|
|
|
lengthLeft = 2 * BUFFER_SIZE;
|
2004-10-12 11:45:53 +00:00
|
|
|
else
|
2005-07-19 19:05:52 +00:00
|
|
|
lengthLeft = MIN((uint32) (2 * BUFFER_SIZE), _endPos - _filePos);
|
2004-10-12 11:45:53 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
while (lengthLeft > 0) {
|
|
|
|
uint32 len = _file->read(ptr, MIN(lengthLeft, _endPos - _file->pos()));
|
2004-10-12 11:45:53 +00:00
|
|
|
|
|
|
|
if (len & 1)
|
|
|
|
len--;
|
|
|
|
|
2005-12-07 20:57:04 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
|
|
|
if (!_context->isBigEndian) {
|
|
|
|
#else
|
2005-07-29 17:58:00 +00:00
|
|
|
if (_context->isBigEndian) {
|
2005-12-07 20:57:04 +00:00
|
|
|
#endif
|
2004-12-15 00:24:12 +00:00
|
|
|
uint16 *ptr16 = (uint16 *)ptr;
|
|
|
|
for (uint32 i = 0; i < (len / 2); i++)
|
2005-12-07 20:57:04 +00:00
|
|
|
ptr16[i] = SWAP_BYTES_16(ptr16[i]);
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
lengthLeft -= len;
|
2004-10-12 11:45:53 +00:00
|
|
|
ptr += len;
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (lengthLeft > 0)
|
|
|
|
_file->seek(_startPos);
|
2004-10-12 11:45:53 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
_filePos = _file->pos();
|
2004-10-12 11:45:53 +00:00
|
|
|
_pos = _buf;
|
|
|
|
_bufferEnd = (int16 *)ptr;
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (!_looping && _filePos >= _endPos) {
|
2004-10-12 11:45:53 +00:00
|
|
|
_finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
|
|
|
|
MusicPlayer::MusicPlayer(MidiDriver *driver) : _parser(0), _driver(driver), _looping(false), _isPlaying(false), _passThrough(false), _isGM(false) {
|
2004-04-29 04:50:45 +00:00
|
|
|
memset(_channel, 0, sizeof(_channel));
|
2004-08-01 07:56:08 +00:00
|
|
|
_masterVolume = 0;
|
2004-04-29 03:52:59 +00:00
|
|
|
this->open();
|
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
MusicPlayer::~MusicPlayer() {
|
|
|
|
_driver->setTimerCallback(NULL, NULL);
|
2004-05-31 08:46:36 +00:00
|
|
|
stopMusic();
|
2004-04-29 03:52:59 +00:00
|
|
|
this->close();
|
2004-05-01 13:04:31 +00:00
|
|
|
}
|
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
void MusicPlayer::setVolume(int volume) {
|
|
|
|
if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
else if (volume > 255)
|
|
|
|
volume = 255;
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
if (_masterVolume == volume)
|
|
|
|
return;
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
_masterVolume = volume;
|
2004-04-29 04:24:55 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
for (int i = 0; i < 16; ++i) {
|
2004-04-29 04:24:55 +00:00
|
|
|
if (_channel[i]) {
|
2004-04-29 03:52:59 +00:00
|
|
|
_channel[i]->volume(_channelVolume[i] * _masterVolume / 255);
|
2004-04-29 04:24:55 +00:00
|
|
|
}
|
2004-04-29 03:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
int MusicPlayer::open() {
|
|
|
|
// Don't ever call open without first setting the output driver!
|
|
|
|
if (!_driver)
|
|
|
|
return 255;
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
int ret = _driver->open();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2004-04-29 04:50:45 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
_driver->setTimerCallback(this, &onTimer);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
void MusicPlayer::close() {
|
|
|
|
stopMusic();
|
|
|
|
if (_driver)
|
|
|
|
_driver->close();
|
|
|
|
_driver = 0;
|
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
void MusicPlayer::send(uint32 b) {
|
2004-10-21 22:40:06 +00:00
|
|
|
if (_passThrough) {
|
|
|
|
_driver->send(b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
byte channel = (byte)(b & 0x0F);
|
|
|
|
if ((b & 0xFFF0) == 0x07B0) {
|
|
|
|
// Adjust volume changes by master volume
|
|
|
|
byte volume = (byte)((b >> 16) & 0x7F);
|
|
|
|
_channelVolume[channel] = volume;
|
|
|
|
volume = volume * _masterVolume / 255;
|
|
|
|
b = (b & 0xFF00FFFF) | (volume << 16);
|
2004-04-30 07:13:26 +00:00
|
|
|
} else if ((b & 0xF0) == 0xC0 && !_isGM && !_nativeMT32) {
|
2005-04-03 22:01:38 +00:00
|
|
|
b = (b & 0xFFFF00FF) | MidiDriver::_mt32ToGm[(b >> 8) & 0xFF] << 8;
|
2004-05-01 13:04:31 +00:00
|
|
|
}
|
2004-04-29 03:52:59 +00:00
|
|
|
else if ((b & 0xFFF0) == 0x007BB0) {
|
|
|
|
//Only respond to All Notes Off if this channel
|
|
|
|
//has currently been allocated
|
|
|
|
if (_channel[b & 0x0F])
|
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
if (!_channel[channel])
|
|
|
|
_channel[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
if (_channel[channel])
|
|
|
|
_channel[channel]->send(b);
|
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
void MusicPlayer::metaEvent(byte type, byte *data, uint16 length) {
|
2004-10-12 11:45:53 +00:00
|
|
|
// FIXME: The "elkfanfare" is played much too quickly. There are some
|
|
|
|
// meta events that we don't handle. Perhaps there is a
|
|
|
|
// connection...?
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 0x2F: // End of Track
|
|
|
|
if (_looping)
|
|
|
|
_parser->jumpToTick(0);
|
|
|
|
else
|
|
|
|
stopMusic();
|
|
|
|
break;
|
|
|
|
default:
|
2005-04-03 15:32:04 +00:00
|
|
|
//warning("Unhandled meta event: %02x", type);
|
2004-10-12 11:45:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-04-29 03:52:59 +00:00
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
void MusicPlayer::onTimer(void *refCon) {
|
|
|
|
MusicPlayer *music = (MusicPlayer *)refCon;
|
2007-08-22 15:28:31 +00:00
|
|
|
Common::StackLock lock(music->_mutex);
|
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
if (music->_isPlaying)
|
|
|
|
music->_parser->onTimer();
|
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
void MusicPlayer::playMusic() {
|
|
|
|
_isPlaying = true;
|
|
|
|
}
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
void MusicPlayer::stopMusic() {
|
2007-08-22 15:28:31 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
_isPlaying = false;
|
2004-05-31 08:46:36 +00:00
|
|
|
if (_parser) {
|
|
|
|
_parser->unloadMusic();
|
2004-05-31 08:59:11 +00:00
|
|
|
_parser = NULL;
|
2004-05-31 08:46:36 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
Music::Music(SagaEngine *vm, Audio::Mixer *mixer, MidiDriver *driver, int enabled) : _vm(vm), _mixer(mixer), _enabled(enabled), _adlib(false) {
|
2004-04-29 03:52:59 +00:00
|
|
|
_player = new MusicPlayer(driver);
|
2005-06-19 21:18:00 +00:00
|
|
|
_currentVolume = 0;
|
2004-10-12 11:45:53 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
xmidiParser = MidiParser::createParser_XMIDI();
|
2005-08-04 10:23:49 +00:00
|
|
|
smfParser = MidiParser::createParser_SMF();
|
2005-07-19 19:05:52 +00:00
|
|
|
|
2007-07-12 08:52:07 +00:00
|
|
|
_digitalMusicContext = _vm->_resource->getContext(GAME_MUSICFILE);
|
2005-08-30 11:16:11 +00:00
|
|
|
|
|
|
|
_songTableLen = 0;
|
|
|
|
_songTable = 0;
|
2005-09-20 18:04:26 +00:00
|
|
|
|
2006-03-02 18:25:56 +00:00
|
|
|
_midiMusicData = NULL;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
Music::~Music() {
|
2007-09-01 11:13:57 +00:00
|
|
|
_vm->_timer->removeTimerProc(&musicVolumeGaugeCallback);
|
2005-07-19 21:48:59 +00:00
|
|
|
_mixer->stopHandle(_musicHandle);
|
2004-04-29 03:52:59 +00:00
|
|
|
delete _player;
|
2005-07-19 19:44:20 +00:00
|
|
|
xmidiParser->setMidiDriver(NULL);
|
|
|
|
smfParser->setMidiDriver(NULL);
|
2005-07-19 19:05:52 +00:00
|
|
|
delete xmidiParser;
|
|
|
|
delete smfParser;
|
2005-08-30 11:16:11 +00:00
|
|
|
|
|
|
|
free(_songTable);
|
2006-03-02 18:25:56 +00:00
|
|
|
free(_midiMusicData);
|
2004-04-29 03:52:59 +00:00
|
|
|
}
|
|
|
|
|
2005-06-19 21:18:00 +00:00
|
|
|
void Music::musicVolumeGaugeCallback(void *refCon) {
|
|
|
|
((Music *)refCon)->musicVolumeGauge();
|
2005-07-29 17:58:00 +00:00
|
|
|
}
|
2005-06-19 21:18:00 +00:00
|
|
|
|
|
|
|
void Music::musicVolumeGauge() {
|
|
|
|
int volume;
|
|
|
|
|
|
|
|
_currentVolumePercent += 10;
|
|
|
|
|
|
|
|
if (_currentVolume - _targetVolume > 0) { // Volume decrease
|
|
|
|
volume = _targetVolume + (_currentVolume - _targetVolume) * (100 - _currentVolumePercent) / 100;
|
|
|
|
} else {
|
|
|
|
volume = _currentVolume + (_targetVolume - _currentVolume) * _currentVolumePercent / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (volume < 0)
|
|
|
|
volume = 1;
|
|
|
|
|
2005-07-19 21:48:59 +00:00
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, volume);
|
2005-06-19 21:18:00 +00:00
|
|
|
_player->setVolume(volume);
|
|
|
|
|
|
|
|
if (_currentVolumePercent == 100) {
|
2006-10-21 12:44:10 +00:00
|
|
|
_vm->_timer->removeTimerProc(&musicVolumeGaugeCallback);
|
2005-06-19 21:18:00 +00:00
|
|
|
_currentVolume = _targetVolume;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Music::setVolume(int volume, int time) {
|
|
|
|
_targetVolume = volume * 2; // ScummVM has different volume scale
|
|
|
|
_currentVolumePercent = 0;
|
|
|
|
|
|
|
|
if (volume == -1) // Set Full volume
|
2005-09-02 20:17:52 +00:00
|
|
|
volume = 255;
|
2005-06-19 21:18:00 +00:00
|
|
|
|
|
|
|
if (time == 1) {
|
2005-07-19 21:48:59 +00:00
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, volume);
|
2005-09-02 20:17:52 +00:00
|
|
|
_player->setVolume(volume);
|
2006-10-21 12:44:10 +00:00
|
|
|
_vm->_timer->removeTimerProc(&musicVolumeGaugeCallback);
|
2005-06-19 21:18:00 +00:00
|
|
|
_currentVolume = volume;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-21 12:44:10 +00:00
|
|
|
_vm->_timer->installTimerProc(&musicVolumeGaugeCallback, time * 100L, this);
|
2005-06-19 21:18:00 +00:00
|
|
|
}
|
|
|
|
|
2005-01-17 14:19:17 +00:00
|
|
|
bool Music::isPlaying() {
|
2005-03-12 18:56:09 +00:00
|
|
|
return _mixer->isSoundHandleActive(_musicHandle) || _player->isPlaying();
|
2005-01-17 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Music::play(uint32 resourceId, MusicFlags flags) {
|
2006-04-29 22:33:31 +00:00
|
|
|
Audio::AudioStream *audioStream = NULL;
|
2005-07-19 19:05:52 +00:00
|
|
|
MidiParser *parser;
|
2007-06-26 23:40:22 +00:00
|
|
|
ResourceContext *context = NULL;
|
2005-07-19 19:05:52 +00:00
|
|
|
byte *resourceData;
|
2006-03-24 18:15:57 +00:00
|
|
|
size_t resourceSize;
|
|
|
|
uint32 loopStart;
|
2005-09-20 18:04:26 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
debug(2, "Music::play %d, %d", resourceId, flags);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 07:13:26 +00:00
|
|
|
if (!_enabled) {
|
2005-07-19 19:05:52 +00:00
|
|
|
return;
|
2004-04-30 07:13:26 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (isPlaying() && _trackNumber == resourceId) {
|
|
|
|
return;
|
2005-01-17 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
_trackNumber = resourceId;
|
2004-10-12 11:45:53 +00:00
|
|
|
_player->stopMusic();
|
2005-03-12 18:56:09 +00:00
|
|
|
_mixer->stopHandle(_musicHandle);
|
2004-10-12 11:45:53 +00:00
|
|
|
|
2007-07-31 00:52:00 +00:00
|
|
|
if (!_vm->_musicVolume) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-21 10:46:20 +00:00
|
|
|
int realTrackNumber;
|
|
|
|
|
|
|
|
if (_vm->getGameType() == GType_ITE) {
|
|
|
|
if (flags == MUSIC_DEFAULT) {
|
|
|
|
if (resourceId == 13 || resourceId == 19) {
|
|
|
|
flags = MUSIC_NORMAL;
|
|
|
|
} else {
|
|
|
|
flags = MUSIC_LOOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
realTrackNumber = resourceId - 8;
|
|
|
|
} else {
|
|
|
|
realTrackNumber = resourceId + 1;
|
|
|
|
}
|
|
|
|
|
2005-09-20 18:04:26 +00:00
|
|
|
// Try to open standalone digital track
|
2007-04-14 17:35:28 +00:00
|
|
|
char trackName[2][16];
|
|
|
|
sprintf(trackName[0], "track%d", realTrackNumber);
|
|
|
|
sprintf(trackName[1], "track%02d", realTrackNumber);
|
|
|
|
Audio::AudioStream *stream = 0;
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2007-04-14 18:44:15 +00:00
|
|
|
// We multiply by 40 / 3 = 1000 / 75 to convert frames to milliseconds
|
2007-04-14 17:35:28 +00:00
|
|
|
// FIXME: Do we really want a duration of 10000 frames = 133 seconds, or is that just a random value?
|
2007-04-14 18:44:15 +00:00
|
|
|
stream = Audio::AudioStream::openStreamFile(trackName[i], 0, 10000 * 40 / 3, (flags == MUSIC_LOOP) ? 0 : 1);
|
2007-04-14 17:35:28 +00:00
|
|
|
if (stream) {
|
|
|
|
_mixer->playInputStream(Audio::Mixer::kMusicSoundType, &_musicHandle, stream);
|
|
|
|
return;
|
2005-09-20 18:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (_vm->getGameType() == GType_ITE) {
|
2005-07-19 19:05:52 +00:00
|
|
|
if (resourceId >= 9 && resourceId <= 34) {
|
2007-07-12 08:52:07 +00:00
|
|
|
if (_digitalMusicContext != NULL) {
|
2005-07-19 19:05:52 +00:00
|
|
|
//TODO: check resource size
|
2006-03-24 18:15:57 +00:00
|
|
|
loopStart = 0;
|
|
|
|
// fix ITE sunstatm score
|
|
|
|
if ((_vm->getGameType() == GType_ITE) && (resourceId == MUSIC_SUNSPOT)) {
|
|
|
|
loopStart = 4 * 18727;
|
|
|
|
}
|
|
|
|
|
2007-07-14 13:41:03 +00:00
|
|
|
// digital music
|
|
|
|
audioStream = new DigitalMusicInputStream(_vm, _digitalMusicContext, resourceId - 9, flags == MUSIC_LOOP, loopStart);
|
2004-11-15 03:03:48 +00:00
|
|
|
}
|
2004-05-16 12:57:49 +00:00
|
|
|
}
|
2004-04-30 07:13:26 +00:00
|
|
|
}
|
|
|
|
|
2004-10-12 11:45:53 +00:00
|
|
|
if (audioStream) {
|
2005-06-05 09:28:40 +00:00
|
|
|
debug(2, "Playing digitized music");
|
2005-05-10 23:48:48 +00:00
|
|
|
_mixer->playInputStream(Audio::Mixer::kMusicSoundType, &_musicHandle, audioStream);
|
2005-07-19 19:05:52 +00:00
|
|
|
return;
|
2004-10-12 11:45:53 +00:00
|
|
|
}
|
2004-05-31 08:46:36 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (flags == MUSIC_DEFAULT) {
|
|
|
|
flags = MUSIC_NORMAL;
|
|
|
|
}
|
2004-04-30 07:13:26 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
// Load MIDI/XMI resource data
|
2004-04-30 07:13:26 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (_vm->getGameType() == GType_ITE) {
|
2005-08-04 10:23:49 +00:00
|
|
|
context = _vm->_resource->getContext(GAME_MUSICFILE_GM);
|
|
|
|
if (context == NULL) {
|
|
|
|
context = _vm->_resource->getContext(GAME_RESOURCEFILE);
|
|
|
|
}
|
2004-04-30 07:13:26 +00:00
|
|
|
} else {
|
2005-07-19 19:05:52 +00:00
|
|
|
// I've listened to music from both the FM and the GM
|
|
|
|
// file, and I've tentatively reached the conclusion
|
|
|
|
// that they are both General MIDI. My guess is that
|
2005-07-29 17:58:00 +00:00
|
|
|
// the FM file has been reorchestrated to sound better
|
2005-07-19 19:05:52 +00:00
|
|
|
// on Adlib and other FM synths.
|
|
|
|
//
|
|
|
|
// Sev says the Adlib music does not sound like in the
|
|
|
|
// original, but I still think assuming General MIDI is
|
|
|
|
// the right thing to do. Some music, like the End
|
|
|
|
// Title (song 0) sound absolutely atrocious when piped
|
|
|
|
// through our MT-32 to GM mapping.
|
|
|
|
//
|
|
|
|
// It is, however, quite possible that the original
|
|
|
|
// used a different GM to FM mapping. If the original
|
|
|
|
// sounded markedly better, perhaps we should add some
|
|
|
|
// way of replacing our stock mapping in adlib.cpp?
|
|
|
|
//
|
|
|
|
// For the composer's own recording of the End Title,
|
|
|
|
// see http://www.johnottman.com/
|
2004-11-26 10:04:36 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
// Oddly enough, the intro music (song 1) is very
|
|
|
|
// different in the two files. I have no idea why.
|
2007-07-30 18:26:56 +00:00
|
|
|
// Note that the IHNM demo has only got one music file
|
|
|
|
// (music.rsc). It is assumed that it contains FM music
|
2004-05-01 13:04:31 +00:00
|
|
|
|
2007-07-30 18:26:56 +00:00
|
|
|
if (hasAdlib() || _vm->getGameId() == GID_IHNM_DEMO) {
|
2005-07-19 19:05:52 +00:00
|
|
|
context = _vm->_resource->getContext(GAME_MUSICFILE_FM);
|
|
|
|
} else {
|
|
|
|
context = _vm->_resource->getContext(GAME_MUSICFILE_GM);
|
2004-04-30 07:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
_player->setGM(true);
|
2004-11-15 03:03:48 +00:00
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
_vm->_resource->loadResource(context, resourceId, resourceData, resourceSize);
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (resourceSize < 4) {
|
|
|
|
error("Music::play() wrong music resource size");
|
2004-04-29 03:52:59 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (xmidiParser->loadMusic(resourceData, resourceSize)) {
|
2006-02-09 04:58:21 +00:00
|
|
|
if (_vm->getGameType() == GType_ITE)
|
|
|
|
_player->setGM(false);
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
parser = xmidiParser;
|
|
|
|
} else {
|
|
|
|
if (smfParser->loadMusic(resourceData, resourceSize)) {
|
2005-07-29 17:58:00 +00:00
|
|
|
parser = smfParser;
|
2005-07-19 19:05:52 +00:00
|
|
|
} else {
|
|
|
|
error("Music::play() wrong music resource");
|
|
|
|
}
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-29 12:27:17 +00:00
|
|
|
parser->setTrack(0);
|
2004-04-29 04:24:55 +00:00
|
|
|
parser->setMidiDriver(_player);
|
2004-04-29 13:45:10 +00:00
|
|
|
parser->setTimerRate(_player->getBaseTempo());
|
2005-10-12 06:57:25 +00:00
|
|
|
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
|
2004-04-29 12:27:17 +00:00
|
|
|
|
2004-04-29 03:52:59 +00:00
|
|
|
_player->_parser = parser;
|
2007-09-02 21:40:09 +00:00
|
|
|
setVolume(_vm->_musicVolume == 10 ? 255 : _vm->_musicVolume * 25);
|
2005-07-19 19:05:52 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (flags & MUSIC_LOOP)
|
2004-05-29 21:04:44 +00:00
|
|
|
_player->setLoop(true);
|
|
|
|
else
|
|
|
|
_player->setLoop(false);
|
|
|
|
|
2004-04-29 04:24:55 +00:00
|
|
|
_player->playMusic();
|
2006-03-02 18:25:56 +00:00
|
|
|
free(_midiMusicData);
|
|
|
|
_midiMusicData = resourceData;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Music::pause(void) {
|
2007-08-22 16:00:45 +00:00
|
|
|
_player->setVolume(-1);
|
|
|
|
_player->setPlaying(false);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Music::resume(void) {
|
2007-08-22 16:00:45 +00:00
|
|
|
_player->setVolume(_vm->_musicVolume == 10 ? 255 : _vm->_musicVolume * 25);
|
|
|
|
_player->setPlaying(true);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Music::stop(void) {
|
2007-08-22 11:28:11 +00:00
|
|
|
_player->stopMusic();
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|
|
|
|
|