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
|
|
|
*
|
|
|
|
*/
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/sound.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-11-15 03:03:48 +00:00
|
|
|
#include "sound/audiostream.h"
|
2004-04-29 01:24:18 +00:00
|
|
|
#include "sound/mixer.h"
|
2005-05-03 20:36:07 +00:00
|
|
|
#include "sound/adpcm.h"
|
2004-04-29 01:24:18 +00:00
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
namespace Saga {
|
|
|
|
|
2005-09-02 20:17:52 +00:00
|
|
|
Sound::Sound(SagaEngine *vm, Audio::Mixer *mixer, int volume) :
|
|
|
|
_vm(vm), _mixer(mixer), _voxStream(0) {
|
2005-08-15 23:26:50 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
_handles[i].type = kFreeHandle;
|
2005-09-02 20:17:52 +00:00
|
|
|
|
|
|
|
setVolume(volume == 10 ? 255 : volume * 25);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-28 23:54:40 +00:00
|
|
|
Sound::~Sound() {
|
2005-05-03 20:36:07 +00:00
|
|
|
delete _voxStream;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-08-15 23:26:50 +00:00
|
|
|
SndHandle *Sound::getHandle() {
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++) {
|
|
|
|
if (_handles[i].type == kFreeHandle)
|
|
|
|
return &_handles[i];
|
|
|
|
|
|
|
|
if (!_mixer->isSoundHandleActive(_handles[i].handle)) {
|
|
|
|
_handles[i].type = kFreeHandle;
|
|
|
|
return &_handles[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error("Sound::getHandle(): Too many sound handles");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-07-26 19:40:58 +00:00
|
|
|
void Sound::playSoundBuffer(Audio::SoundHandle *handle, SoundBuffer &buffer, int volume, bool loop) {
|
2004-10-19 17:12:53 +00:00
|
|
|
byte flags;
|
|
|
|
|
2005-05-10 23:48:48 +00:00
|
|
|
flags = Audio::Mixer::FLAG_AUTOFREE;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-19 17:12:53 +00:00
|
|
|
if (loop)
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= Audio::Mixer::FLAG_LOOP;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (buffer.sampleBits == 16) {
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= Audio::Mixer::FLAG_16BITS;
|
2005-07-26 19:40:58 +00:00
|
|
|
|
|
|
|
if (!buffer.isBigEndian)
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= Audio::Mixer::FLAG_LITTLE_ENDIAN;
|
2004-12-22 19:34:41 +00:00
|
|
|
}
|
2005-07-19 19:05:52 +00:00
|
|
|
if (buffer.stereo)
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= Audio::Mixer::FLAG_STEREO;
|
2005-07-19 19:05:52 +00:00
|
|
|
if (!buffer.isSigned)
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= Audio::Mixer::FLAG_UNSIGNED;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2007-06-25 20:20:59 +00:00
|
|
|
if (!(_vm->getFeatures() & GF_COMPRESSED_SOUNDS)) {
|
2007-06-25 18:23:01 +00:00
|
|
|
_mixer->playRaw(Audio::Mixer::kSFXSoundType, handle, buffer.buffer, buffer.size, buffer.frequency, flags, -1, volume);
|
|
|
|
} else {
|
|
|
|
Audio::AudioStream *stream = NULL;
|
2007-07-15 19:24:00 +00:00
|
|
|
Common::MemoryReadStream *tmp = NULL;
|
2007-06-25 18:23:01 +00:00
|
|
|
|
|
|
|
switch (buffer.soundType) {
|
|
|
|
#ifdef USE_MAD
|
|
|
|
case kSoundMP3:
|
|
|
|
debug(1, "Playing MP3 compressed sound");
|
2007-07-10 23:22:44 +00:00
|
|
|
buffer.soundFile->seek((long)buffer.fileOffset, SEEK_SET);
|
2007-07-15 19:24:00 +00:00
|
|
|
tmp = buffer.soundFile->readStream(buffer.size);
|
|
|
|
assert(tmp);
|
|
|
|
stream = Audio::makeMP3Stream(tmp, true);
|
2007-06-25 18:23:01 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_VORBIS
|
|
|
|
case kSoundOGG:
|
|
|
|
debug(1, "Playing OGG compressed sound");
|
2007-07-10 23:22:44 +00:00
|
|
|
buffer.soundFile->seek((long)buffer.fileOffset, SEEK_SET);
|
2007-07-15 19:24:00 +00:00
|
|
|
tmp = buffer.soundFile->readStream(buffer.size);
|
|
|
|
assert(tmp);
|
|
|
|
stream = Audio::makeVorbisStream(tmp, true);
|
2007-06-25 18:23:01 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_FLAC
|
|
|
|
case kSoundFLAC:
|
|
|
|
debug(1, "Playing FLAC compressed sound");
|
2007-07-10 23:22:44 +00:00
|
|
|
buffer.soundFile->seek((long)buffer.fileOffset, SEEK_SET);
|
2007-07-15 19:24:00 +00:00
|
|
|
tmp = buffer.soundFile->readStream(buffer.size);
|
|
|
|
assert(tmp);
|
|
|
|
stream = Audio::makeFlacStream(tmp, true);
|
2007-06-25 18:23:01 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2007-07-10 23:22:44 +00:00
|
|
|
// No compression, play it as raw sound
|
|
|
|
_mixer->playRaw(Audio::Mixer::kSFXSoundType, handle, buffer.buffer, buffer.size, buffer.frequency, flags, -1, volume);
|
2007-06-25 18:23:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream != NULL)
|
|
|
|
_mixer->playInputStream(Audio::Mixer::kSFXSoundType, handle, stream, -1, volume, 0, true, false);
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::playSound(SoundBuffer &buffer, int volume, bool loop) {
|
2005-08-15 23:26:50 +00:00
|
|
|
SndHandle *handle = getHandle();
|
|
|
|
|
|
|
|
handle->type = kEffectHandle;
|
|
|
|
playSoundBuffer(&handle->handle, buffer, 2 * volume, loop);
|
2004-10-19 17:12:53 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::pauseSound() {
|
2005-08-15 23:26:50 +00:00
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kEffectHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, true);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::resumeSound() {
|
2005-08-15 23:26:50 +00:00
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kEffectHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, false);
|
2004-10-19 17:12:53 +00:00
|
|
|
}
|
2004-05-17 16:11:04 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::stopSound() {
|
2005-08-15 23:26:50 +00:00
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kEffectHandle) {
|
|
|
|
_mixer->stopHandle(_handles[i].handle);
|
|
|
|
_handles[i].type = kFreeHandle;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::playVoice(SoundBuffer &buffer) {
|
2005-08-15 23:26:50 +00:00
|
|
|
SndHandle *handle = getHandle();
|
|
|
|
|
|
|
|
handle->type = kVoiceHandle;
|
|
|
|
playSoundBuffer(&handle->handle, buffer, 255, false);
|
2004-10-19 17:12:53 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::pauseVoice() {
|
2005-08-15 23:26:50 +00:00
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kVoiceHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, true);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::resumeVoice() {
|
2005-08-15 23:26:50 +00:00
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kVoiceHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, false);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void Sound::stopVoice() {
|
2005-08-15 23:26:50 +00:00
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kVoiceHandle) {
|
|
|
|
_mixer->stopHandle(_handles[i].handle);
|
|
|
|
_handles[i].type = kFreeHandle;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-08-13 01:35:52 +00:00
|
|
|
void Sound::stopAll() {
|
2005-08-15 23:26:50 +00:00
|
|
|
stopVoice();
|
|
|
|
stopSound();
|
2005-08-13 01:35:52 +00:00
|
|
|
}
|
|
|
|
|
2005-09-02 20:17:52 +00:00
|
|
|
void Sound::setVolume(int volume) {
|
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, volume);
|
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, volume);
|
|
|
|
}
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
} // End of namespace Saga
|