1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/* 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.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/raw.h"
#include "toltecs/toltecs.h"
#include "toltecs/resource.h"
#include "toltecs/segmap.h"
#include "toltecs/sound.h"
namespace Toltecs {
Sound::Sound(ToltecsEngine *vm) : _vm(vm) {
for (int i = 0; i < kMaxChannels; i++) {
clearChannel(i);
}
}
Sound::~Sound() {
}
void Sound::clearChannel(int channel) {
channels[channel].type = kChannelTypeEmpty;
channels[channel].resIndex = -1;
channels[channel].volume = 0;
channels[channel].panning = 0;
}
void Sound::playSpeech(int16 resIndex) {
debug(0, "playSpeech(%d)", resIndex);
if (_vm->_cfgVoices)
internalPlaySound(resIndex, kChannelTypeSpeech, 50 /*TODO*/, 0);
}
void Sound::playSound(int16 resIndex, int16 type, int16 volume) {
debug(0, "playSound(%d, %d, %d)", resIndex, type, volume);
internalPlaySound(resIndex, type, volume, 0);
}
void Sound::playSoundAtPos(int16 resIndex, int16 x, int16 y) {
debug(0, "playSoundAtPos(%d, %d, %d)", resIndex, x, y);
int16 volume = 50 + ABS(_vm->_segmap->getScalingAtPoint(x, y)) / 2;
int16 panning = 0, deltaX = 0;
if (_vm->_cameraX > x)
deltaX = _vm->_cameraX - x;
else if (_vm->_cameraX + 640 < x)
deltaX = x - (_vm->_cameraX + 640);
if (deltaX > 600)
deltaX = 600;
volume = ((100 - deltaX / 6) * volume) / 100;
if (_vm->_cameraX + 320 != x) {
panning = CLIP(x - (_vm->_cameraX + 320), -381, 381) / 3;
}
internalPlaySound(resIndex, 1, volume, panning);
}
void Sound::internalPlaySound(int16 resIndex, int16 type, int16 volume, int16 panning) {
// Change the game's sound volume (0 - 100) to Scummvm's scale (0 - 255)
volume = (volume == -1) ? 255 : volume * 255 / 100;
if (resIndex == -1) {
// Stop all sounds
_vm->_mixer->stopAll();
_vm->_screen->keepTalkTextItemsAlive();
for (int i = 0; i < kMaxChannels; i++) {
clearChannel(i);
}
} else if (type == -2) {
// Stop sounds with specified resIndex
for (int i = 0; i < kMaxChannels; i++) {
if (channels[i].resIndex == resIndex) {
_vm->_mixer->stopHandle(channels[i].handle);
clearChannel(i);
}
}
} else {
if (type == kChannelTypeSpeech) {
// Stop speech and play new sound
stopSpeech();
}
// Play new sound in empty channel
int freeChannel = -1;
for (int i = 0; i < kMaxChannels; i++) {
if (channels[i].type == kChannelTypeEmpty || !_vm->_mixer->isSoundHandleActive(channels[i].handle)) {
freeChannel = i;
break;
}
}
// If all channels are in use no new sound will be played
if (freeChannel >= 0) {
Resource *soundResource = _vm->_res->load(resIndex);
Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
Audio::makeRawStream(soundResource->data,
soundResource->size, 22050, Audio::FLAG_UNSIGNED,
DisposeAfterUse::NO),
type == kChannelTypeBackground ? 0 : 1);
channels[freeChannel].type = type;
channels[freeChannel].resIndex = resIndex;
channels[freeChannel].volume = volume;
channels[freeChannel].panning = panning;
Audio::Mixer::SoundType soundType = getScummVMSoundType((SoundChannelType)type);
_vm->_mixer->playStream(soundType, &channels[freeChannel].handle,
stream, -1, volume, panning);
} // if (freeChannel >= 0)
} // resIndex
}
void Sound::updateSpeech() {
for (int i = 0; i < kMaxChannels; i++) {
if (channels[i].type == kChannelTypeSpeech && _vm->_mixer->isSoundHandleActive(channels[i].handle)) {
_vm->_screen->keepTalkTextItemsAlive();
break;
}
}
}
void Sound::stopSpeech() {
for (int i = 0; i < kMaxChannels; i++) {
if (channels[i].type == kChannelTypeSpeech) {
_vm->_mixer->stopHandle(channels[i].handle);
_vm->_screen->keepTalkTextItemsAlive();
clearChannel(i);
}
}
}
void Sound::stopAll() {
for (int i = 0; i < kMaxChannels; i++) {
_vm->_mixer->stopHandle(channels[i].handle);
_vm->_screen->keepTalkTextItemsAlive();
clearChannel(i);
}
}
void Sound::saveState(Common::WriteStream *out) {
for (int i = 0; i < kMaxChannels; i++) {
out->writeSint16LE(channels[i].type);
out->writeSint16LE(channels[i].resIndex);
out->writeSint16LE(channels[i].volume);
out->writeSint16LE(channels[i].panning);
}
}
void Sound::loadState(Common::ReadStream *in, int version) {
for (int i = 0; i < kMaxChannels; i++) {
channels[i].type = in->readSint16LE();
channels[i].resIndex = in->readSint16LE();
if (version < 4) {
channels[i].volume = (channels[i].type == kChannelTypeBackground) ? 50 : 100;
channels[i].panning = 0;
} else {
channels[i].volume = in->readSint16LE();
channels[i].panning = in->readSint16LE();
}
if (channels[i].type != kChannelTypeEmpty) {
Resource *soundResource = _vm->_res->load(channels[i].resIndex);
Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
Audio::makeRawStream(soundResource->data,
soundResource->size, 22050, Audio::FLAG_UNSIGNED,
DisposeAfterUse::NO),
channels[i].type == kChannelTypeBackground ? 0 : 1);
Audio::Mixer::SoundType soundType = getScummVMSoundType((SoundChannelType)channels[i].type);
_vm->_mixer->playStream(soundType, &channels[i].handle,
stream, -1, channels[i].volume, channels[i].panning);
}
}
}
Audio::Mixer::SoundType Sound::getScummVMSoundType(SoundChannelType type) const {
switch (type) {
case kChannelTypeBackground:
case kChannelTypeSfx:
return Audio::Mixer::kSFXSoundType;
case kChannelTypeSpeech:
return Audio::Mixer::kSpeechSoundType;
break;
default:
return Audio::Mixer::kSFXSoundType;
break;
}
}
} // End of namespace Toltecs
|