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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2003-12-16 02:10:15 +00:00
|
|
|
#include "common/file.h"
|
2006-03-29 15:59:37 +00:00
|
|
|
#include "common/util.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
|
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
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/mixer.h"
|
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/decoders/aiff.h"
|
|
|
|
#include "audio/decoders/flac.h"
|
|
|
|
#include "audio/decoders/mp3.h"
|
|
|
|
#include "audio/decoders/vorbis.h"
|
|
|
|
#include "audio/decoders/wave.h"
|
2011-08-24 13:38:46 +00:00
|
|
|
#include "audio/decoders/xa.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 {
|
|
|
|
|
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.
|
|
|
|
|
2011-06-11 15:32:58 +00:00
|
|
|
bool MusicHandle::play(const Common::String &filename, bool loop) {
|
2004-10-12 15:50:00 +00:00
|
|
|
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);
|
|
|
|
|
2011-06-11 15:37:51 +00:00
|
|
|
Audio::RewindableAudioStream *stream = 0;
|
|
|
|
|
2007-06-17 14:50:49 +00:00
|
|
|
#ifdef USE_FLAC
|
2011-06-11 15:37:51 +00:00
|
|
|
if (!stream) {
|
2011-06-11 15:32:58 +00:00
|
|
|
if (_file.open(filename + ".flac")) {
|
2011-06-11 15:37:51 +00:00
|
|
|
stream = Audio::makeFLACStream(&_file, DisposeAfterUse::NO);
|
|
|
|
if (!stream)
|
2009-10-03 19:29:14 +00:00
|
|
|
_file.close();
|
|
|
|
}
|
2007-06-17 14:50:49 +00:00
|
|
|
}
|
2009-10-03 19:29:14 +00:00
|
|
|
|
2011-06-11 15:37:51 +00:00
|
|
|
if (!stream) {
|
2011-06-11 15:32:58 +00:00
|
|
|
if (_file.open(filename + ".fla")) {
|
2011-06-11 15:37:51 +00:00
|
|
|
stream = Audio::makeFLACStream(&_file, DisposeAfterUse::NO);
|
|
|
|
if (!stream)
|
2009-10-03 19:29:14 +00:00
|
|
|
_file.close();
|
|
|
|
}
|
2007-06-17 14:50:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 12:42:56 +00:00
|
|
|
#ifdef USE_VORBIS
|
2011-06-11 15:37:51 +00:00
|
|
|
if (!stream) {
|
2011-06-11 15:32:58 +00:00
|
|
|
if (_file.open(filename + ".ogg")) {
|
2011-06-11 15:37:51 +00:00
|
|
|
stream = Audio::makeVorbisStream(&_file, DisposeAfterUse::NO);
|
|
|
|
if (!stream)
|
2009-10-03 19:29:14 +00:00
|
|
|
_file.close();
|
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
2007-02-24 23:40:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_MAD
|
2011-06-11 15:37:51 +00:00
|
|
|
if (!stream) {
|
2011-06-11 15:32:58 +00:00
|
|
|
if (_file.open(filename + ".mp3")) {
|
2011-06-11 15:37:51 +00:00
|
|
|
stream = Audio::makeMP3Stream(&_file, DisposeAfterUse::NO);
|
|
|
|
if (!stream)
|
2009-10-03 19:29:14 +00:00
|
|
|
_file.close();
|
|
|
|
}
|
2007-02-24 23:40:28 +00:00
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
#endif
|
2011-06-11 15:37:51 +00:00
|
|
|
if (!stream) {
|
2011-06-11 15:32:58 +00:00
|
|
|
if (_file.open(filename + ".wav"))
|
2011-06-11 15:37:51 +00:00
|
|
|
stream = Audio::makeWAVStream(&_file, DisposeAfterUse::NO);
|
2004-10-12 15:50:00 +00:00
|
|
|
}
|
2007-04-09 09:58:41 +00:00
|
|
|
|
2011-06-11 15:37:51 +00:00
|
|
|
if (!stream) {
|
2011-06-11 15:32:58 +00:00
|
|
|
if (_file.open(filename + ".aif"))
|
2011-06-11 15:37:51 +00:00
|
|
|
stream = Audio::makeAIFFStream(&_file, DisposeAfterUse::NO);
|
2007-04-09 09:58:41 +00:00
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2011-06-11 15:37:51 +00:00
|
|
|
if (!stream)
|
2007-03-02 23:53:20 +00:00
|
|
|
return false;
|
|
|
|
|
2011-06-11 15:37:51 +00:00
|
|
|
_audioSource = Audio::makeLoopingAudioStream(stream, loop ? 0 : 1);
|
|
|
|
|
2004-10-12 15:50:00 +00:00
|
|
|
fadeUp();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-25 06:52:27 +00:00
|
|
|
bool MusicHandle::playPSX(uint16 id) {
|
2009-02-28 10:46:33 +00:00
|
|
|
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:34:01 +00:00
|
|
|
// Because of broken tunes.dat/tab in psx demo, also check that tune offset is
|
|
|
|
// not over file size
|
|
|
|
if ((size != 0) && (size != 0xffffffff) && ((int32)(offset + size) <= _file.size())) {
|
2009-02-28 10:46:33 +00:00
|
|
|
_file.seek(offset, SEEK_SET);
|
2011-08-25 06:52:27 +00:00
|
|
|
_audioSource = Audio::makeXAStream(_file.readStream(size), 11025);
|
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.
|
2009-11-02 21:54:57 +00:00
|
|
|
bool MusicHandle::streaming() const {
|
2004-10-12 15:50:00 +00:00
|
|
|
return (_audioSource) ? (!_audioSource->endOfStream()) : false;
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
bool MusicHandle::isStereo() const {
|
2004-10-12 15:50:00 +00:00
|
|
|
return (_audioSource) ? _audioSource->isStereo() : false;
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
int MusicHandle::getRate() const {
|
2004-10-12 15:50:00 +00:00
|
|
|
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() {
|
2011-06-11 15:37:51 +00:00
|
|
|
delete _audioSource;
|
|
|
|
_audioSource = NULL;
|
|
|
|
_file.close();
|
2004-01-01 15:22:15 +00:00
|
|
|
_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;
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, 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 */
|
2011-02-07 23:01:06 +00:00
|
|
|
if (SwordEngine::isPsx()) {
|
2011-08-25 06:52:27 +00:00
|
|
|
if (_handles[newStream].playPSX(tuneId)) {
|
2009-02-28 10:46:33 +00:00
|
|
|
_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
|