2003-12-16 02:10:15 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:09:25 +00:00
|
|
|
* Copyright (C) 2003-2005 The ScummVM project
|
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
|
|
|
|
* 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"
|
2005-01-10 22:06:49 +00:00
|
|
|
|
2004-10-21 12:43:49 +00:00
|
|
|
#include "sword1/music.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
|
2003-12-16 02:10:15 +00:00
|
|
|
#include "common/util.h"
|
|
|
|
#include "common/file.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "common/system.h"
|
|
|
|
|
|
|
|
#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"
|
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 {
|
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
WaveAudioStream *makeWaveStream(Common::File *source, uint32 size) {
|
2004-10-12 15:50:00 +00:00
|
|
|
return new WaveAudioStream(source, size);
|
|
|
|
}
|
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
WaveAudioStream::WaveAudioStream(Common::File *source, uint32 pSize) {
|
2005-01-13 16:28:20 +00:00
|
|
|
int rate, size;
|
|
|
|
byte flags;
|
2004-10-12 15:50:00 +00:00
|
|
|
|
|
|
|
_sourceFile = source;
|
2004-12-10 18:26:08 +00:00
|
|
|
_sampleBuf = (uint8*)malloc(SMP_BUFSIZE);
|
2004-10-12 15:50:00 +00:00
|
|
|
_sourceFile->incRef();
|
2005-01-13 16:28:20 +00:00
|
|
|
if (_sourceFile->isOpen() && loadWAVFromStream(*_sourceFile, size, rate, flags)) {
|
2005-05-10 23:48:48 +00:00
|
|
|
_isStereo = (flags & Audio::Mixer::FLAG_STEREO) != 0;
|
2005-01-13 16:28:20 +00:00
|
|
|
_rate = rate;
|
|
|
|
if (pSize && (int)pSize < size)
|
|
|
|
size = pSize;
|
|
|
|
assert((uint32)size <= (source->size() - source->pos()));
|
2005-05-10 23:48:48 +00:00
|
|
|
_bitsPerSample = ((flags & Audio::Mixer::FLAG_16BITS) != 0) ? 16 : 8;
|
2004-10-12 15:50:00 +00:00
|
|
|
_samplesLeft = (size * 8) / _bitsPerSample;
|
|
|
|
if ((_bitsPerSample != 16) && (_bitsPerSample != 8))
|
|
|
|
error("WaveAudioStream: unknown wave type");
|
|
|
|
} else {
|
|
|
|
_samplesLeft = 0;
|
|
|
|
_isStereo = false;
|
|
|
|
_bitsPerSample = 16;
|
|
|
|
_rate = 22050;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WaveAudioStream::~WaveAudioStream(void) {
|
2004-12-10 18:26:08 +00:00
|
|
|
free(_sampleBuf);
|
2004-10-12 15:50:00 +00:00
|
|
|
_sourceFile->decRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
int WaveAudioStream::readBuffer(int16 *buffer, const int numSamples) {
|
2005-01-13 16:28:20 +00:00
|
|
|
int samples = MIN((int)_samplesLeft, numSamples);
|
2004-12-10 18:26:08 +00:00
|
|
|
int retVal = samples;
|
|
|
|
|
|
|
|
while (samples > 0) {
|
2005-01-13 16:28:20 +00:00
|
|
|
int readBytes = MIN(samples * (_bitsPerSample >> 3), SMP_BUFSIZE);
|
2004-12-10 18:26:08 +00:00
|
|
|
_sourceFile->read(_sampleBuf, readBytes);
|
|
|
|
if (_bitsPerSample == 16) {
|
|
|
|
readBytes >>= 1;
|
|
|
|
samples -= readBytes;
|
|
|
|
int16 *src = (int16*)_sampleBuf;
|
|
|
|
while (readBytes--)
|
|
|
|
*buffer++ = (int16)READ_LE_UINT16(src++);
|
|
|
|
} else {
|
|
|
|
samples -= readBytes;
|
|
|
|
int8 *src = (int8*)_sampleBuf;
|
|
|
|
while (readBytes--)
|
|
|
|
*buffer++ = (int16)*src++ << 8;
|
|
|
|
}
|
2004-12-10 15:11:53 +00:00
|
|
|
}
|
2004-12-10 18:26:08 +00:00
|
|
|
_samplesLeft -= retVal;
|
|
|
|
return retVal;
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WaveAudioStream::endOfData(void) const {
|
|
|
|
if (_samplesLeft == 0)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
AudioStream *MusicHandle::createAudioSource(void) {
|
|
|
|
_file.seek(0);
|
|
|
|
switch (_musicMode) {
|
|
|
|
#ifdef USE_MAD
|
2005-04-07 08:42:40 +00:00
|
|
|
case MusicMp3:
|
|
|
|
return makeMP3Stream(&_file, _file.size());
|
2004-10-12 15:50:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_VORBIS
|
2005-04-07 08:42:40 +00:00
|
|
|
case MusicVorbis:
|
|
|
|
return makeVorbisStream(&_file, _file.size());
|
2004-10-12 15:50:00 +00:00
|
|
|
#endif
|
2005-04-07 08:42:40 +00:00
|
|
|
case MusicWave:
|
|
|
|
return makeWaveStream(&_file, 0);
|
|
|
|
case MusicNone: // shouldn't happen
|
|
|
|
warning("createAudioSource ran into null create\n");
|
|
|
|
return NULL;
|
|
|
|
default:
|
|
|
|
error("MusicHandle::createAudioSource: called with illegal MusicMode");
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
|
|
|
return NULL; // never reached
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MusicHandle::play(const char *fileBase, bool loop) {
|
|
|
|
char fileName[30];
|
|
|
|
stop();
|
|
|
|
_musicMode = MusicNone;
|
|
|
|
#ifdef USE_MAD
|
|
|
|
sprintf(fileName, "%s.mp3", fileBase);
|
|
|
|
if (_file.open(fileName))
|
|
|
|
_musicMode = MusicMp3;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_VORBIS
|
|
|
|
if (!_file.isOpen()) { // mp3 doesn't exist (or not compiled with MAD support)
|
|
|
|
sprintf(fileName, "%s.ogg", fileBase);
|
|
|
|
if (_file.open(fileName))
|
|
|
|
_musicMode = MusicVorbis;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!_file.isOpen()) {
|
|
|
|
sprintf(fileName, "%s.wav", fileBase);
|
|
|
|
if (_file.open(fileName))
|
|
|
|
_musicMode = MusicWave;
|
|
|
|
else {
|
|
|
|
warning("Music file %s could not be opened", fileName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_audioSource = createAudioSource();
|
|
|
|
_looping = loop;
|
|
|
|
fadeUp();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2004-10-12 15:50:00 +00:00
|
|
|
// is we don't have an audiosource, return some dummy values.
|
|
|
|
// shouldn't happen anyways.
|
|
|
|
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;
|
|
|
|
expectedSamples -= samplesReturned;
|
|
|
|
if ((expectedSamples > 0) && _audioSource->endOfData()) {
|
|
|
|
debug(2, "Music reached EOF");
|
|
|
|
_audioSource->endOfData();
|
|
|
|
if (_looping) {
|
|
|
|
delete _audioSource; // recreate same source.
|
|
|
|
_audioSource = createAudioSource();
|
2004-01-08 13:05:32 +00:00
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
if ((!_looping) || (!_audioSource))
|
|
|
|
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
|
|
|
|
bufStart[samplePos] = (bufStart[samplePos] * --_fading) / _fadeSamples;
|
|
|
|
samplePos++;
|
|
|
|
if (_fading == 0) {
|
|
|
|
stop();
|
|
|
|
// clear the rest of the buffer
|
|
|
|
memset(bufStart + samplePos, 0, (totalSamples - samplePos) * 2);
|
|
|
|
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
|
|
|
|
bufStart[samplePos] = -(bufStart[samplePos] * --_fading) / _fadeSamples;
|
|
|
|
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;
|
|
|
|
_looping = false;
|
|
|
|
}
|
|
|
|
|
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;
|
2004-12-09 16:33:54 +00:00
|
|
|
_mixer->setupPremix(this);
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
Music::~Music() {
|
2004-10-17 19:40:05 +00:00
|
|
|
_mixer->setupPremix(0);
|
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
|
|
|
|
the mutex before, to have the soundthread playing normally.
|
|
|
|
As the corresponding _converter is NULL, the handle will be ignored by the playing thread */
|
2004-10-12 15:50:00 +00:00
|
|
|
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();
|
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
|