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-12-16 02:10:15 +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
|
2008-01-05 12:45:14 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-12-16 02:10:15 +00:00
|
|
|
* 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.
|
2003-12-16 02:10:15 +00:00
|
|
|
*
|
2006-02-11 10:01:01 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2003-12-16 02:10:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-29 15:59:37 +00:00
|
|
|
#include "common/endian.h"
|
2003-12-16 02:10:15 +00:00
|
|
|
#include "common/file.h"
|
2006-03-29 15:59:37 +00:00
|
|
|
#include "common/util.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "common/system.h"
|
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
#include "sword1/sword1.h"
|
2006-03-29 15:59:37 +00:00
|
|
|
#include "sword1/music.h"
|
2009-02-28 10:46:33 +00:00
|
|
|
|
2007-04-09 09:58:41 +00:00
|
|
|
#include "sound/aiff.h"
|
2007-06-17 14:50:49 +00:00
|
|
|
#include "sound/flac.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "sound/mixer.h"
|
2004-10-12 15:50:00 +00:00
|
|
|
#include "sound/mp3.h"
|
|
|
|
#include "sound/vorbis.h"
|
2005-01-09 15:57:38 +00:00
|
|
|
#include "sound/wave.h"
|
2009-03-15 10:25:22 +00:00
|
|
|
#include "sound/vag.h"
|
2003-12-16 02:10:15 +00:00
|
|
|
|
2004-12-10 18:26:08 +00:00
|
|
|
#define SMP_BUFSIZE 8192
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
namespace Sword1 {
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
class BaseAudioStream : public Audio::AudioStream {
|
2007-03-02 23:53:20 +00:00
|
|
|
public:
|
2007-04-09 15:50:20 +00:00
|
|
|
BaseAudioStream(Common::SeekableReadStream *source, bool loop);
|
|
|
|
virtual ~BaseAudioStream();
|
2007-03-02 23:53:20 +00:00
|
|
|
virtual int readBuffer(int16 *buffer, const int numSamples);
|
|
|
|
virtual bool isStereo() const { return _isStereo; }
|
|
|
|
virtual bool endOfData() const { return (_samplesLeft == 0); }
|
|
|
|
virtual int getRate() const { return _rate; }
|
2007-04-09 15:50:20 +00:00
|
|
|
protected:
|
2007-03-02 23:53:20 +00:00
|
|
|
Common::SeekableReadStream *_sourceStream;
|
|
|
|
uint8 *_sampleBuf;
|
2007-04-09 15:50:20 +00:00
|
|
|
uint32 _rate;
|
|
|
|
bool _isStereo;
|
|
|
|
uint32 _samplesLeft;
|
|
|
|
uint16 _bitsPerSample;
|
|
|
|
bool _loop;
|
|
|
|
|
|
|
|
virtual void rewind() = 0;
|
|
|
|
virtual void reinit(int size, int rate, byte flags);
|
2007-03-02 23:53:20 +00:00
|
|
|
};
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
BaseAudioStream::BaseAudioStream(Common::SeekableReadStream *source, bool loop) {
|
2007-03-02 23:53:20 +00:00
|
|
|
_sourceStream = source;
|
|
|
|
_sampleBuf = (uint8*)malloc(SMP_BUFSIZE);
|
|
|
|
|
|
|
|
_samplesLeft = 0;
|
|
|
|
_isStereo = false;
|
|
|
|
_bitsPerSample = 16;
|
|
|
|
_rate = 22050;
|
|
|
|
_loop = loop;
|
|
|
|
}
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
BaseAudioStream::~BaseAudioStream() {
|
2007-03-02 23:53:20 +00:00
|
|
|
free(_sampleBuf);
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
void BaseAudioStream::reinit(int size, int rate, byte flags) {
|
|
|
|
_isStereo = (flags & Audio::Mixer::FLAG_STEREO) != 0;
|
|
|
|
_rate = rate;
|
2008-09-13 16:51:46 +00:00
|
|
|
assert(size <= (_sourceStream->size() - _sourceStream->pos()));
|
2007-04-09 15:50:20 +00:00
|
|
|
_bitsPerSample = ((flags & Audio::Mixer::FLAG_16BITS) != 0) ? 16 : 8;
|
|
|
|
_samplesLeft = (size * 8) / _bitsPerSample;
|
|
|
|
if ((_bitsPerSample != 16) && (_bitsPerSample != 8))
|
|
|
|
error("BaseAudioStream: unknown sound type");
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
int BaseAudioStream::readBuffer(int16 *buffer, const int numSamples) {
|
2007-03-02 23:53:20 +00:00
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
while (retVal < numSamples && _samplesLeft > 0) {
|
2007-04-09 10:07:36 +00:00
|
|
|
int samples = MIN((int)_samplesLeft, numSamples - retVal);
|
2007-03-02 23:53:20 +00:00
|
|
|
retVal += samples;
|
|
|
|
_samplesLeft -= samples;
|
|
|
|
while (samples > 0) {
|
|
|
|
int readBytes = MIN(samples * (_bitsPerSample >> 3), SMP_BUFSIZE);
|
|
|
|
_sourceStream->read(_sampleBuf, readBytes);
|
|
|
|
if (_bitsPerSample == 16) {
|
2007-04-09 15:50:20 +00:00
|
|
|
samples -= (readBytes / 2);
|
|
|
|
memcpy(buffer, _sampleBuf, readBytes);
|
|
|
|
buffer += (readBytes / 2);
|
2007-03-02 23:53:20 +00:00
|
|
|
} else {
|
|
|
|
samples -= readBytes;
|
|
|
|
int8 *src = (int8*)_sampleBuf;
|
|
|
|
while (readBytes--)
|
|
|
|
*buffer++ = (int16)*src++ << 8;
|
|
|
|
}
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-03-02 23:53:20 +00:00
|
|
|
if (!_samplesLeft && _loop) {
|
|
|
|
rewind();
|
2004-12-10 18:26:08 +00:00
|
|
|
}
|
2004-12-10 15:11:53 +00:00
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2004-12-10 18:26:08 +00:00
|
|
|
return retVal;
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
class WaveAudioStream : public BaseAudioStream {
|
2007-04-09 09:58:41 +00:00
|
|
|
public:
|
2007-04-09 15:50:20 +00:00
|
|
|
WaveAudioStream(Common::SeekableReadStream *source, bool loop);
|
2007-04-09 09:58:41 +00:00
|
|
|
virtual int readBuffer(int16 *buffer, const int numSamples);
|
|
|
|
private:
|
2007-04-09 15:50:20 +00:00
|
|
|
virtual void rewind();
|
2007-04-09 09:58:41 +00:00
|
|
|
};
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
WaveAudioStream::WaveAudioStream(Common::SeekableReadStream *source, bool loop) : BaseAudioStream(source, loop) {
|
2007-04-09 09:58:41 +00:00
|
|
|
rewind();
|
2007-04-09 15:50:20 +00:00
|
|
|
|
2007-04-09 09:58:41 +00:00
|
|
|
if (_samplesLeft == 0)
|
|
|
|
_loop = false;
|
|
|
|
}
|
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
void WaveAudioStream::rewind() {
|
|
|
|
int rate, size;
|
|
|
|
byte flags;
|
|
|
|
|
|
|
|
_sourceStream->seek(0);
|
|
|
|
|
|
|
|
if (Audio::loadWAVFromStream(*_sourceStream, size, rate, flags)) {
|
|
|
|
reinit(size, rate, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int WaveAudioStream::readBuffer(int16 *buffer, const int numSamples) {
|
|
|
|
int retVal = BaseAudioStream::readBuffer(buffer, numSamples);
|
|
|
|
|
|
|
|
if (_bitsPerSample == 16) {
|
|
|
|
for (int i = 0; i < retVal; i++) {
|
|
|
|
buffer[i] = (int16)READ_LE_UINT16(buffer + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AiffAudioStream : public BaseAudioStream {
|
|
|
|
public:
|
|
|
|
AiffAudioStream(Common::SeekableReadStream *source, bool loop);
|
|
|
|
virtual int readBuffer(int16 *buffer, const int numSamples);
|
|
|
|
private:
|
|
|
|
void rewind();
|
|
|
|
};
|
|
|
|
|
|
|
|
AiffAudioStream::AiffAudioStream(Common::SeekableReadStream *source, bool loop) : BaseAudioStream(source, loop) {
|
|
|
|
rewind();
|
|
|
|
|
|
|
|
if (_samplesLeft == 0)
|
|
|
|
_loop = false;
|
2007-04-09 09:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AiffAudioStream::rewind() {
|
|
|
|
int rate, size;
|
|
|
|
byte flags;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-04-09 09:58:41 +00:00
|
|
|
_sourceStream->seek(0);
|
|
|
|
|
|
|
|
if (Audio::loadAIFFFromStream(*_sourceStream, size, rate, flags)) {
|
2007-04-09 15:50:20 +00:00
|
|
|
reinit(size, rate, flags);
|
2007-04-09 09:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AiffAudioStream::readBuffer(int16 *buffer, const int numSamples) {
|
2007-04-09 15:50:20 +00:00
|
|
|
int retVal = BaseAudioStream::readBuffer(buffer, numSamples);
|
2007-04-09 09:58:41 +00:00
|
|
|
|
2007-04-09 15:50:20 +00:00
|
|
|
if (_bitsPerSample == 16) {
|
|
|
|
for (int i = 0; i < retVal; i++) {
|
|
|
|
buffer[i] = (int16)READ_BE_UINT16(buffer + i);
|
2007-04-09 09:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-04-09 09:58:41 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2004-01-01 16:56:00 +00:00
|
|
|
// This means fading takes 3 seconds.
|
|
|
|
#define FADE_LENGTH 3
|
2004-01-01 15:22:15 +00:00
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
// These functions are only called from Music, so I'm just going to
|
2004-01-01 15:22:15 +00:00
|
|
|
// assume that if locking is needed it has already been taken care of.
|
|
|
|
|
2004-10-12 15:50:00 +00:00
|
|
|
bool MusicHandle::play(const char *fileBase, bool loop) {
|
|
|
|
char fileName[30];
|
|
|
|
stop();
|
2007-06-17 14:50:49 +00:00
|
|
|
|
2007-07-15 19:26:00 +00:00
|
|
|
// FIXME: How about using AudioStream::openStreamFile instead of the code below?
|
|
|
|
// I.e.:
|
|
|
|
//_audioSource = Audio::AudioStream::openStreamFile(fileBase, 0, 0, loop ? 0 : 1);
|
|
|
|
|
2007-06-17 14:50:49 +00:00
|
|
|
#ifdef USE_FLAC
|
|
|
|
if (!_audioSource) {
|
|
|
|
sprintf(fileName, "%s.flac", fileBase);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (_file.open(fileName)) {
|
2007-06-17 14:50:49 +00:00
|
|
|
_audioSource = Audio::makeFlacStream(&_file, false, 0, 0, loop ? 0 : 1);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (!_audioSource)
|
|
|
|
_file.close();
|
|
|
|
}
|
2007-06-17 14:50:49 +00:00
|
|
|
}
|
2009-10-03 19:29:14 +00:00
|
|
|
|
2007-06-17 14:50:49 +00:00
|
|
|
if (!_audioSource) {
|
|
|
|
sprintf(fileName, "%s.fla", fileBase);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (_file.open(fileName)) {
|
2007-06-17 14:50:49 +00:00
|
|
|
_audioSource = Audio::makeFlacStream(&_file, false, 0, 0, loop ? 0 : 1);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (!_audioSource)
|
|
|
|
_file.close();
|
|
|
|
}
|
2007-06-17 14:50:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 12:42:56 +00:00
|
|
|
#ifdef USE_VORBIS
|
2007-03-02 23:53:20 +00:00
|
|
|
if (!_audioSource) {
|
2004-10-12 15:50:00 +00:00
|
|
|
sprintf(fileName, "%s.ogg", fileBase);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (_file.open(fileName)) {
|
2007-03-02 23:53:20 +00:00
|
|
|
_audioSource = Audio::makeVorbisStream(&_file, false, 0, 0, loop ? 0 : 1);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (!_audioSource)
|
|
|
|
_file.close();
|
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
2007-02-24 23:40:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_MAD
|
2007-03-02 23:53:20 +00:00
|
|
|
if (!_audioSource) {
|
2007-02-24 23:40:28 +00:00
|
|
|
sprintf(fileName, "%s.mp3", fileBase);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (_file.open(fileName)) {
|
2007-03-02 23:53:20 +00:00
|
|
|
_audioSource = Audio::makeMP3Stream(&_file, false, 0, 0, loop ? 0 : 1);
|
2009-10-03 19:29:14 +00:00
|
|
|
if (!_audioSource)
|
|
|
|
_file.close();
|
|
|
|
}
|
2007-02-24 23:40:28 +00:00
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
#endif
|
2007-03-02 23:53:20 +00:00
|
|
|
if (!_audioSource) {
|
2004-10-12 15:50:00 +00:00
|
|
|
sprintf(fileName, "%s.wav", fileBase);
|
|
|
|
if (_file.open(fileName))
|
2007-03-02 23:53:20 +00:00
|
|
|
_audioSource = new WaveAudioStream(&_file, loop);
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
2007-04-09 09:58:41 +00:00
|
|
|
|
|
|
|
if (!_audioSource) {
|
|
|
|
sprintf(fileName, "%s.aif", fileBase);
|
|
|
|
if (_file.open(fileName))
|
|
|
|
_audioSource = new AiffAudioStream(&_file, loop);
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-03-02 23:53:20 +00:00
|
|
|
if (!_audioSource)
|
|
|
|
return false;
|
|
|
|
|
2004-10-12 15:50:00 +00:00
|
|
|
fadeUp();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
bool MusicHandle::playPSX(uint16 id, bool loop) {
|
|
|
|
stop();
|
|
|
|
|
|
|
|
if (!_file.isOpen())
|
|
|
|
if (!_file.open("tunes.dat"))
|
|
|
|
return false;
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
Common::File tableFile;
|
|
|
|
if (!tableFile.open("tunes.tab"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tableFile.seek((id - 1) * 8, SEEK_SET);
|
|
|
|
uint32 offset = tableFile.readUint32LE() * 0x800;
|
|
|
|
uint32 size = tableFile.readUint32LE();
|
|
|
|
|
|
|
|
tableFile.close();
|
|
|
|
|
2009-10-11 22:20:58 +00:00
|
|
|
if ((size != 0) && (size != 0xffffffff) && ((offset + size) <= _file.size())) {
|
2009-02-28 10:46:33 +00:00
|
|
|
_file.seek(offset, SEEK_SET);
|
2009-03-15 10:25:22 +00:00
|
|
|
_audioSource = new Audio::VagStream(_file.readStream(size), loop);
|
2009-02-28 10:46:33 +00:00
|
|
|
fadeUp();
|
|
|
|
} else {
|
|
|
|
_audioSource = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MusicHandle::fadeDown() {
|
2004-01-14 18:39:24 +00:00
|
|
|
if (streaming()) {
|
|
|
|
if (_fading < 0)
|
|
|
|
_fading = -_fading;
|
|
|
|
else if (_fading == 0)
|
|
|
|
_fading = FADE_LENGTH * getRate();
|
|
|
|
_fadeSamples = FADE_LENGTH * getRate();
|
|
|
|
}
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MusicHandle::fadeUp() {
|
2004-01-14 18:39:24 +00:00
|
|
|
if (streaming()) {
|
|
|
|
if (_fading > 0)
|
|
|
|
_fading = -_fading;
|
|
|
|
else if (_fading == 0)
|
|
|
|
_fading = -1;
|
|
|
|
_fadeSamples = FADE_LENGTH * getRate();
|
|
|
|
}
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
bool MusicHandle::endOfData() const {
|
2004-01-01 15:22:15 +00:00
|
|
|
return !streaming();
|
|
|
|
}
|
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
// if we don't have an audiosource, return some dummy values.
|
2004-10-12 15:50:00 +00:00
|
|
|
bool MusicHandle::streaming(void) const {
|
|
|
|
return (_audioSource) ? (!_audioSource->endOfStream()) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MusicHandle::isStereo(void) const {
|
|
|
|
return (_audioSource) ? _audioSource->isStereo() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MusicHandle::getRate(void) const {
|
|
|
|
return (_audioSource) ? _audioSource->getRate() : 11025;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
int MusicHandle::readBuffer(int16 *buffer, const int numSamples) {
|
2004-10-12 15:50:00 +00:00
|
|
|
int totalSamples = 0;
|
|
|
|
int16 *bufStart = buffer;
|
|
|
|
if (!_audioSource)
|
|
|
|
return 0;
|
|
|
|
int expectedSamples = numSamples;
|
|
|
|
while ((expectedSamples > 0) && _audioSource) { // _audioSource becomes NULL if we reach EOF and aren't looping
|
|
|
|
int samplesReturned = _audioSource->readBuffer(buffer, expectedSamples);
|
|
|
|
buffer += samplesReturned;
|
|
|
|
totalSamples += samplesReturned;
|
2005-10-20 13:08:43 +00:00
|
|
|
expectedSamples -= samplesReturned;
|
2004-10-12 15:50:00 +00:00
|
|
|
if ((expectedSamples > 0) && _audioSource->endOfData()) {
|
|
|
|
debug(2, "Music reached EOF");
|
2007-03-02 23:53:20 +00:00
|
|
|
stop();
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
|
|
|
// buffer was filled, now do the fading (if necessary)
|
|
|
|
int samplePos = 0;
|
|
|
|
while ((_fading > 0) && (samplePos < totalSamples)) { // fade down
|
2007-03-02 23:53:20 +00:00
|
|
|
--_fading;
|
|
|
|
bufStart[samplePos] = (bufStart[samplePos] * _fading) / _fadeSamples;
|
2004-10-12 15:50:00 +00:00
|
|
|
samplePos++;
|
|
|
|
if (_fading == 0) {
|
|
|
|
stop();
|
|
|
|
// clear the rest of the buffer
|
2005-10-20 13:08:43 +00:00
|
|
|
memset(bufStart + samplePos, 0, (totalSamples - samplePos) * 2);
|
2004-10-12 15:50:00 +00:00
|
|
|
return samplePos;
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
while ((_fading < 0) && (samplePos < totalSamples)) { // fade up
|
2005-10-20 13:08:43 +00:00
|
|
|
bufStart[samplePos] = -(bufStart[samplePos] * --_fading) / _fadeSamples;
|
2004-10-12 15:50:00 +00:00
|
|
|
if (_fading <= -_fadeSamples)
|
|
|
|
_fading = 0;
|
2004-01-04 16:19:15 +00:00
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
return totalSamples;
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MusicHandle::stop() {
|
2004-10-12 15:50:00 +00:00
|
|
|
if (_audioSource) {
|
|
|
|
delete _audioSource;
|
|
|
|
_audioSource = NULL;
|
|
|
|
}
|
2004-01-01 15:22:15 +00:00
|
|
|
if (_file.isOpen())
|
|
|
|
_file.close();
|
|
|
|
_fading = 0;
|
|
|
|
}
|
|
|
|
|
2005-05-10 23:48:48 +00:00
|
|
|
Music::Music(Audio::Mixer *pMixer) {
|
2003-12-16 02:10:15 +00:00
|
|
|
_mixer = pMixer;
|
2004-11-27 16:18:25 +00:00
|
|
|
_sampleRate = pMixer->getOutputRate();
|
2004-01-01 15:22:15 +00:00
|
|
|
_converter[0] = NULL;
|
|
|
|
_converter[1] = NULL;
|
2004-01-07 17:47:46 +00:00
|
|
|
_volumeL = _volumeR = 192;
|
2007-02-20 18:50:17 +00:00
|
|
|
_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, false, true);
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
Music::~Music() {
|
2007-02-20 18:50:17 +00:00
|
|
|
_mixer->stopHandle(_soundHandle);
|
2004-01-01 15:22:15 +00:00
|
|
|
delete _converter[0];
|
|
|
|
delete _converter[1];
|
2003-12-19 01:08:30 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::mixer(int16 *buf, uint32 len) {
|
2004-01-01 15:22:15 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2004-11-27 16:18:25 +00:00
|
|
|
memset(buf, 0, 2 * len * sizeof(int16));
|
2004-01-06 11:48:30 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_handles); i++)
|
|
|
|
if (_handles[i].streaming() && _converter[i])
|
2004-01-07 17:47:46 +00:00
|
|
|
_converter[i]->flow(_handles[i], buf, len, _volumeL, _volumeR);
|
2004-01-06 11:48:30 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::setVolume(uint8 volL, uint8 volR) {
|
2005-05-11 00:01:44 +00:00
|
|
|
_volumeL = (Audio::st_volume_t)volL;
|
|
|
|
_volumeR = (Audio::st_volume_t)volR;
|
2004-01-07 17:47:46 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::giveVolume(uint8 *volL, uint8 *volR) {
|
2004-01-07 17:47:46 +00:00
|
|
|
*volL = (uint8)_volumeL;
|
|
|
|
*volR = (uint8)_volumeR;
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::startMusic(int32 tuneId, int32 loopFlag) {
|
2004-01-01 15:22:15 +00:00
|
|
|
if (strlen(_tuneList[tuneId]) > 0) {
|
|
|
|
int newStream = 0;
|
2005-02-20 19:06:09 +00:00
|
|
|
_mutex.lock();
|
2004-01-01 15:22:15 +00:00
|
|
|
if (_handles[0].streaming() && _handles[1].streaming()) {
|
2004-03-13 12:04:18 +00:00
|
|
|
int streamToStop;
|
|
|
|
// Both streams playing - one must be forced to stop.
|
|
|
|
if (!_handles[0].fading() && !_handles[1].fading()) {
|
|
|
|
// None of them are fading. Shouldn't happen,
|
|
|
|
// so it doesn't matter which one we pick.
|
|
|
|
streamToStop = 0;
|
|
|
|
} else if (_handles[0].fading() && !_handles[1].fading()) {
|
|
|
|
// Stream 0 is fading, so pick that one.
|
|
|
|
streamToStop = 0;
|
|
|
|
} else if (!_handles[0].fading() && _handles[1].fading()) {
|
|
|
|
// Stream 1 is fading, so pick that one.
|
|
|
|
streamToStop = 1;
|
|
|
|
} else {
|
|
|
|
// Both streams are fading. Pick the one that
|
|
|
|
// is closest to silent.
|
|
|
|
if (ABS(_handles[0].fading()) < ABS(_handles[1].fading()))
|
|
|
|
streamToStop = 0;
|
|
|
|
else
|
|
|
|
streamToStop = 1;
|
|
|
|
}
|
|
|
|
_handles[streamToStop].stop();
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
if (_handles[0].streaming()) {
|
|
|
|
_handles[0].fadeDown();
|
|
|
|
newStream = 1;
|
|
|
|
} else if (_handles[1].streaming()) {
|
|
|
|
_handles[1].fadeDown();
|
|
|
|
newStream = 0;
|
|
|
|
}
|
2005-02-20 19:06:09 +00:00
|
|
|
delete _converter[newStream];
|
|
|
|
_converter[newStream] = NULL;
|
|
|
|
_mutex.unlock();
|
|
|
|
|
|
|
|
/* The handle will load the music file now. It can take a while, so unlock
|
2005-07-30 21:11:48 +00:00
|
|
|
the mutex before, to have the soundthread playing normally.
|
2005-02-20 19:06:09 +00:00
|
|
|
As the corresponding _converter is NULL, the handle will be ignored by the playing thread */
|
2009-02-28 10:46:33 +00:00
|
|
|
if (SwordEngine::isPsx()) { ;
|
|
|
|
if (_handles[newStream].playPSX(tuneId, loopFlag != 0)) {
|
|
|
|
_mutex.lock();
|
|
|
|
_converter[newStream] = Audio::makeRateConverter(_handles[newStream].getRate(), _mixer->getOutputRate(), _handles[newStream].isStereo(), false);
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
} else if (_handles[newStream].play(_tuneList[tuneId], loopFlag != 0)) {
|
2005-02-20 19:06:09 +00:00
|
|
|
_mutex.lock();
|
2005-05-11 00:01:44 +00:00
|
|
|
_converter[newStream] = Audio::makeRateConverter(_handles[newStream].getRate(), _mixer->getOutputRate(), _handles[newStream].isStereo(), false);
|
2005-02-20 19:06:09 +00:00
|
|
|
_mutex.unlock();
|
2005-11-12 11:26:24 +00:00
|
|
|
} else {
|
|
|
|
if (tuneId != 81) // file 81 was apparently removed from BS.
|
|
|
|
warning("Can't find music file %s", _tuneList[tuneId]);
|
2004-01-04 16:19:15 +00:00
|
|
|
}
|
2004-01-01 16:56:00 +00:00
|
|
|
} else {
|
2005-02-20 19:06:09 +00:00
|
|
|
_mutex.lock();
|
2004-01-01 16:56:00 +00:00
|
|
|
if (_handles[0].streaming())
|
|
|
|
_handles[0].fadeDown();
|
|
|
|
if (_handles[1].streaming())
|
|
|
|
_handles[1].fadeDown();
|
2005-02-20 19:06:09 +00:00
|
|
|
_mutex.unlock();
|
2003-12-30 22:57:52 +00:00
|
|
|
}
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::fadeDown() {
|
2004-01-01 15:22:15 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_handles); i++)
|
|
|
|
if (_handles[i].streaming())
|
|
|
|
_handles[i].fadeDown();
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
2004-01-11 15:47:41 +00:00
|
|
|
|
|
|
|
} // End of namespace Sword1
|