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.
|
2014-02-18 01:34:24 +00:00
|
|
|
*
|
2004-04-12 21:40:49 +00:00
|
|
|
* 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
|
2004-04-12 21:40:49 +00:00
|
|
|
* GNU General Public License for more details.
|
2014-02-18 01:34:24 +00:00
|
|
|
*
|
2004-04-12 21:40:49 +00:00
|
|
|
* 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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-07-21 19:15:28 +00:00
|
|
|
#include "common/config-manager.h"
|
|
|
|
|
|
|
|
#include "saga/saga.h"
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/sound.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/mixer.h"
|
|
|
|
#include "audio/decoders/adpcm.h"
|
|
|
|
#include "audio/decoders/raw.h"
|
2004-04-29 01:24:18 +00:00
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
namespace Saga {
|
|
|
|
|
2008-07-21 19:15:28 +00:00
|
|
|
Sound::Sound(SagaEngine *vm, Audio::Mixer *mixer) :
|
2010-10-24 20:22:21 +00:00
|
|
|
_vm(vm), _mixer(mixer) {
|
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
|
|
|
|
2008-07-21 19:15:28 +00:00
|
|
|
setVolume();
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-28 23:54:40 +00:00
|
|
|
Sound::~Sound() {
|
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");
|
|
|
|
|
2009-09-24 17:52:53 +00:00
|
|
|
return NULL; // for compilers that don't support NORETURN
|
2005-08-15 23:26:50 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 23:50:33 +00:00
|
|
|
void Sound::playSoundBuffer(Audio::SoundHandle *handle, const SoundBuffer &buffer, int volume,
|
2008-07-21 19:15:28 +00:00
|
|
|
sndHandleType handleType, bool loop) {
|
2004-10-19 17:12:53 +00:00
|
|
|
|
2010-01-25 01:39:44 +00:00
|
|
|
Audio::Mixer::SoundType soundType = (handleType == kVoiceHandle) ?
|
2009-08-17 11:49:07 +00:00
|
|
|
Audio::Mixer::kSpeechSoundType : Audio::Mixer::kSFXSoundType;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2011-12-24 00:26:25 +00:00
|
|
|
if (buffer.stream)
|
|
|
|
_mixer->playStream(soundType, handle, Audio::makeLoopingAudioStream(buffer.stream, loop ? 0 : 1), -1, volume);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2010-06-15 10:25:34 +00:00
|
|
|
void Sound::playSound(SoundBuffer &buffer, int volume, bool loop, int resId) {
|
|
|
|
// WORKAROUND
|
|
|
|
// Prevent playing same looped sound for several times
|
|
|
|
// Fixes bug #2886141: "ITE: Cumulative Snoring sounds in Prince's Bedroom"
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kEffectHandle && _handles[i].resId == resId) {
|
|
|
|
debug(1, "Skipped playing SFX #%d", resId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-08-15 23:26:50 +00:00
|
|
|
SndHandle *handle = getHandle();
|
|
|
|
|
|
|
|
handle->type = kEffectHandle;
|
2010-06-15 10:25:34 +00:00
|
|
|
handle->resId = resId;
|
2008-07-21 19:15:28 +00:00
|
|
|
playSoundBuffer(&handle->handle, buffer, 2 * volume, handle->type, 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;
|
2010-06-15 10:25:34 +00:00
|
|
|
_handles[i].resId = -1;
|
2005-08-15 23:26:50 +00:00
|
|
|
}
|
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;
|
2008-07-21 19:15:28 +00:00
|
|
|
playSoundBuffer(&handle->handle, buffer, 255, handle->type, 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
|
|
|
}
|
|
|
|
|
2008-07-21 19:15:28 +00:00
|
|
|
void Sound::setVolume() {
|
2011-03-19 14:07:06 +00:00
|
|
|
bool mute = false;
|
|
|
|
if (ConfMan.hasKey("mute"))
|
|
|
|
mute = ConfMan.getBool("mute");
|
|
|
|
|
|
|
|
_vm->_soundVolume = mute ? 0 : ConfMan.getInt("sfx_volume");
|
|
|
|
_vm->_speechVolume = mute ? 0 : ConfMan.getInt("speech_volume");
|
2008-07-21 19:15:28 +00:00
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, _vm->_soundVolume);
|
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, _vm->_speechVolume);
|
2005-09-02 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
} // End of namespace Saga
|