2004-04-12 21:40:49 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:20:17 +00:00
|
|
|
* Copyright (C) 2004-2005 The ScummVM project
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
* The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
|
|
|
|
*
|
|
|
|
* 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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Sound resource management class
|
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
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
#include "saga/rscfile.h"
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/sndres.h"
|
|
|
|
#include "saga/sound.h"
|
2004-12-15 00:24:12 +00:00
|
|
|
#include "saga/stream.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-17 16:11:04 +00:00
|
|
|
#include "common/file.h"
|
|
|
|
|
2004-12-11 23:36:17 +00:00
|
|
|
#include "sound/voc.h"
|
2005-01-09 15:57:38 +00:00
|
|
|
#include "sound/wave.h"
|
2005-07-19 19:05:52 +00:00
|
|
|
#include "sound/adpcm.h"
|
|
|
|
#include "sound/audiostream.h"
|
2004-12-11 23:36:17 +00:00
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
namespace Saga {
|
|
|
|
|
2004-07-31 23:00:48 +00:00
|
|
|
SndRes::SndRes(SagaEngine *vm) : _vm(vm) {
|
2004-04-28 22:21:51 +00:00
|
|
|
/* Load sound module resource file contexts */
|
2005-07-19 19:05:52 +00:00
|
|
|
_sfxContext = _vm->_resource->getContext(GAME_SOUNDFILE);
|
|
|
|
if (_sfxContext == NULL) {
|
|
|
|
error("SndRes::SndRes resource context not found");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-26 19:40:58 +00:00
|
|
|
_voiceSerial = -1;
|
|
|
|
|
|
|
|
setVoiceBank(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SndRes::setVoiceBank(int serial)
|
|
|
|
{
|
|
|
|
if (_voiceSerial == serial) return;
|
|
|
|
|
|
|
|
_voiceSerial = serial;
|
|
|
|
|
|
|
|
_voiceContext = _vm->_resource->getContext(GAME_VOICEFILE, _voiceSerial);
|
2005-07-19 19:05:52 +00:00
|
|
|
if (_voiceContext == NULL) {
|
|
|
|
error("SndRes::SndRes resource context not found");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void SndRes::playSound(uint32 resourceId, int volume, bool loop) {
|
|
|
|
SoundBuffer buffer;
|
2004-10-19 17:12:53 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
debug(4, "SndRes::playSound %i", resourceId);
|
2004-10-19 17:12:53 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (!load(_sfxContext, resourceId, buffer, false)) {
|
2005-07-05 16:58:36 +00:00
|
|
|
warning("Failed to load sound");
|
2005-07-19 19:05:52 +00:00
|
|
|
return;
|
2004-10-19 17:12:53 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
_vm->_sound->playSound(buffer, volume, loop);
|
2004-10-19 17:12:53 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void SndRes::playVoice(uint32 resourceId) {
|
|
|
|
SoundBuffer buffer;
|
2004-11-15 03:03:48 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
debug(4, "SndRes::playVoice %i", resourceId);
|
2004-04-30 17:49:33 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (!load(_voiceContext, resourceId, buffer, false)) {
|
|
|
|
warning("Failed to load voice");
|
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
_vm->_sound->playVoice(buffer);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
bool SndRes::load(ResourceContext *context, uint32 resourceId, SoundBuffer &buffer, bool onlyHeader) {
|
|
|
|
byte *soundResource;
|
|
|
|
AudioStream *voxStream;
|
|
|
|
size_t soundResourceLength;
|
|
|
|
bool result = false;
|
|
|
|
GameSoundTypes resourceType;
|
|
|
|
byte *data;
|
|
|
|
int rate;
|
|
|
|
int size;
|
|
|
|
byte flags;
|
|
|
|
size_t voxSize;
|
2005-07-26 19:40:58 +00:00
|
|
|
const GameSoundInfo *soundInfo;
|
2005-07-19 19:05:52 +00:00
|
|
|
|
|
|
|
if (resourceId == (int32)-1) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-26 19:40:58 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
_vm->_resource->loadResource(context, resourceId, soundResource, soundResourceLength);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-26 19:40:58 +00:00
|
|
|
if ((context->fileType & GAME_VOICEFILE) != 0) {
|
|
|
|
soundInfo = _vm->getVoiceInfo();
|
|
|
|
} else {
|
|
|
|
soundInfo = _vm->getSfxInfo();
|
|
|
|
}
|
2005-07-30 17:28:58 +00:00
|
|
|
|
|
|
|
context->table[resourceId].fillSoundPatch(soundInfo);
|
2005-07-26 19:40:58 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
MemoryReadStream readS(soundResource, soundResourceLength);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-26 19:40:58 +00:00
|
|
|
resourceType = soundInfo->resourceType;
|
|
|
|
buffer.isBigEndian = soundInfo->isBigEndian;
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (soundResourceLength >= 8) {
|
|
|
|
if (!memcmp(&soundResource, "Creative", 8)) {
|
2005-07-26 19:40:58 +00:00
|
|
|
resourceType = kSoundVOC;
|
2005-07-19 19:05:52 +00:00
|
|
|
} else if (!memcmp(soundResource, "RIFF", 4) != 0) {
|
2005-07-26 19:40:58 +00:00
|
|
|
resourceType = kSoundWAV;
|
2005-07-19 19:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-26 19:40:58 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
switch (resourceType) {
|
2005-07-26 19:40:58 +00:00
|
|
|
case kSoundPCM:
|
|
|
|
buffer.frequency = soundInfo->frequency;
|
|
|
|
buffer.isSigned = soundInfo->isSigned;
|
|
|
|
buffer.sampleBits = soundInfo->sampleBits;
|
|
|
|
buffer.size = soundResourceLength;
|
|
|
|
buffer.stereo = soundInfo->stereo;
|
|
|
|
if (onlyHeader) {
|
|
|
|
buffer.buffer = NULL;
|
|
|
|
free(soundResource);
|
|
|
|
} else {
|
|
|
|
buffer.buffer = soundResource;
|
2005-07-19 19:05:52 +00:00
|
|
|
}
|
2005-07-26 19:40:58 +00:00
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
case kSoundVOX:
|
|
|
|
buffer.frequency = soundInfo->frequency;
|
|
|
|
buffer.isSigned = soundInfo->isSigned;
|
|
|
|
buffer.sampleBits = soundInfo->sampleBits;
|
|
|
|
buffer.stereo = soundInfo->stereo;
|
|
|
|
buffer.size = soundResourceLength * 4;
|
|
|
|
if (onlyHeader) {
|
|
|
|
buffer.buffer = NULL;
|
|
|
|
free(soundResource);
|
2005-07-19 19:05:52 +00:00
|
|
|
} else {
|
2005-07-26 19:40:58 +00:00
|
|
|
voxStream = makeADPCMStream(readS, soundResourceLength, kADPCMOki);
|
|
|
|
buffer.buffer = (byte *)malloc(buffer.size);
|
|
|
|
voxSize = voxStream->readBuffer((int16*)buffer.buffer, soundResourceLength * 2);
|
|
|
|
if (voxSize != soundResourceLength * 2) {
|
|
|
|
error("SndRes::load() wrong VOX output size");
|
2005-07-19 19:05:52 +00:00
|
|
|
}
|
2005-07-26 19:40:58 +00:00
|
|
|
delete voxStream;
|
2005-07-19 19:05:52 +00:00
|
|
|
}
|
|
|
|
result = true;
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-07-26 19:40:58 +00:00
|
|
|
case kSoundVOC:
|
2005-07-19 19:05:52 +00:00
|
|
|
data = loadVOCFromStream(readS, size, rate);
|
|
|
|
if (data) {
|
|
|
|
buffer.frequency = rate;
|
|
|
|
buffer.sampleBits = 8;
|
|
|
|
buffer.stereo = false;
|
|
|
|
buffer.isSigned = false;
|
|
|
|
buffer.size = size;
|
|
|
|
if (onlyHeader) {
|
|
|
|
buffer.buffer = NULL;
|
|
|
|
free(data);
|
|
|
|
} else {
|
|
|
|
buffer.buffer = data;
|
|
|
|
}
|
|
|
|
result = true;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2005-07-19 19:05:52 +00:00
|
|
|
free(soundResource);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-07-26 19:40:58 +00:00
|
|
|
case kSoundWAV:
|
2005-07-19 19:05:52 +00:00
|
|
|
if (loadWAVFromStream(readS, size, rate, flags)) {
|
|
|
|
buffer.frequency = rate;
|
|
|
|
buffer.sampleBits = 16;
|
|
|
|
buffer.stereo = ((flags & Audio::Mixer::FLAG_STEREO) != 0);
|
|
|
|
buffer.isSigned = true;
|
|
|
|
buffer.size = size;
|
|
|
|
if (onlyHeader) {
|
|
|
|
buffer.buffer = NULL;
|
|
|
|
} else {
|
|
|
|
buffer.buffer = (byte *)malloc(size);
|
|
|
|
readS.read(buffer.buffer, size);
|
|
|
|
}
|
|
|
|
result = true;
|
2004-11-18 17:34:54 +00:00
|
|
|
}
|
2005-07-19 19:05:52 +00:00
|
|
|
free(soundResource);
|
2004-11-18 17:34:54 +00:00
|
|
|
break;
|
2004-04-12 21:40:49 +00:00
|
|
|
default:
|
2005-07-19 19:05:52 +00:00
|
|
|
error("SndRes::load Unknown sound type");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
return result;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
int SndRes::getVoiceLength(uint32 resourceId) {
|
2004-11-15 03:03:48 +00:00
|
|
|
bool voiceFile = false;
|
2005-07-19 19:05:52 +00:00
|
|
|
double msDouble;
|
|
|
|
SoundBuffer buffer;
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (!load(_voiceContext, resourceId, buffer, true)) {
|
|
|
|
return -1;
|
2004-10-10 12:27:43 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
msDouble = (double)buffer.size;
|
|
|
|
if (buffer.sampleBits == 16) {
|
|
|
|
msDouble /= 2.0;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2005-07-19 19:05:52 +00:00
|
|
|
if (buffer.stereo) {
|
|
|
|
msDouble /= 2.0;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
msDouble = msDouble / buffer.frequency * 1000.0;
|
|
|
|
return (int)msDouble;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|