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
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/rscfile_mod.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
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"
|
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 */
|
2004-12-22 13:09:47 +00:00
|
|
|
_sfx_ctxt = _vm->getFileContext(GAME_SOUNDFILE, 0);
|
2004-11-15 03:03:48 +00:00
|
|
|
if (_sfx_ctxt == NULL) {
|
2004-04-28 23:10:59 +00:00
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-22 13:09:47 +00:00
|
|
|
_voice_ctxt = _vm->getFileContext(GAME_VOICEFILE, 0);
|
2004-11-15 03:03:48 +00:00
|
|
|
if (_voice_ctxt == NULL) {
|
2004-04-28 23:10:59 +00:00
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Grab sound resource information for the current game
|
2005-01-09 23:41:22 +00:00
|
|
|
_snd_info = *_vm->getSoundInfo();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-28 23:10:59 +00:00
|
|
|
_init = 1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-11-26 13:28:00 +00:00
|
|
|
int SndRes::playSound(uint32 sound_rn, int volume, bool loop) {
|
2004-10-27 21:32:28 +00:00
|
|
|
SOUNDBUFFER snd_buffer;
|
2004-10-19 17:12:53 +00:00
|
|
|
|
2005-07-05 16:58:36 +00:00
|
|
|
debug(4, "SndRes::playSound(%ld)", sound_rn);
|
2004-10-19 17:12:53 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (load(_sfx_ctxt, sound_rn, &snd_buffer) != SUCCESS) {
|
2005-07-05 16:58:36 +00:00
|
|
|
warning("Failed to load sound");
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-10-19 17:12:53 +00:00
|
|
|
}
|
|
|
|
|
2004-11-26 13:28:00 +00:00
|
|
|
_vm->_sound->playSound(&snd_buffer, volume, loop);
|
2004-10-19 17:12:53 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-10-19 17:12:53 +00:00
|
|
|
}
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
int SndRes::playVoice(uint32 voice_rn) {
|
2004-10-27 21:32:28 +00:00
|
|
|
SOUNDBUFFER snd_buffer;
|
2004-11-15 03:03:48 +00:00
|
|
|
int result = FAILURE;
|
|
|
|
bool voiceFile = false;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-05 16:58:36 +00:00
|
|
|
debug(4, "SndRes::playVoice(%ld)", voice_rn);
|
2004-04-29 01:24:18 +00:00
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (_vm->getGameType() == GType_ITE && voice_rn == 4) {
|
2004-10-10 12:27:43 +00:00
|
|
|
// The Wyrmkeep release of Inherit the Earth provides a
|
|
|
|
// separate file (p2_a.voc or P2_A.iaf), to correct voice 4 in
|
|
|
|
// the intro. Use that, if available.
|
2004-10-01 06:45:13 +00:00
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
Common::File f;
|
2004-10-01 06:45:13 +00:00
|
|
|
uint32 size;
|
|
|
|
bool voc = false;
|
|
|
|
|
|
|
|
if (f.open("p2_a.voc"))
|
|
|
|
voc = true;
|
|
|
|
else
|
|
|
|
f.open("P2_A.iaf");
|
|
|
|
|
2004-11-15 03:03:48 +00:00
|
|
|
if (f.isOpen()) {
|
|
|
|
size = f.size();
|
|
|
|
byte *snd_res = (byte *)malloc(size);
|
|
|
|
f.read(snd_res, size);
|
|
|
|
f.close();
|
2004-10-01 06:45:13 +00:00
|
|
|
|
2004-11-15 03:03:48 +00:00
|
|
|
if (!voc) {
|
|
|
|
snd_buffer.s_stereo = 0;
|
|
|
|
snd_buffer.s_samplebits = 16;
|
|
|
|
snd_buffer.s_freq = 22050;
|
|
|
|
snd_buffer.s_buf = snd_res;
|
|
|
|
snd_buffer.s_buf_len = size;
|
|
|
|
snd_buffer.s_signed = 1;
|
|
|
|
result = SUCCESS;
|
|
|
|
} else {
|
|
|
|
result = loadVocSound(snd_res, size, &snd_buffer);
|
|
|
|
RSC_FreeResource(snd_res);
|
|
|
|
}
|
|
|
|
voiceFile = true;
|
2004-10-10 12:27:43 +00:00
|
|
|
}
|
2004-11-15 03:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// File is not present. It is DOS CD or Floppy
|
|
|
|
if (result != SUCCESS)
|
2004-05-17 16:11:04 +00:00
|
|
|
result = load(_voice_ctxt, voice_rn, &snd_buffer);
|
2004-04-30 17:49:33 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (result != SUCCESS) {
|
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (_vm->getFeatures() & GF_VOX_VOICES && !voiceFile)
|
2004-11-15 03:03:48 +00:00
|
|
|
_vm->_sound->playVoxVoice(&snd_buffer);
|
|
|
|
else
|
|
|
|
_vm->_sound->playVoice(&snd_buffer);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int SndRes::load(RSCFILE_CONTEXT *snd_ctxt, uint32 snd_rn, SOUNDBUFFER *snd_buf_i) {
|
2004-04-30 23:02:23 +00:00
|
|
|
byte *snd_res;
|
2004-04-12 21:40:49 +00:00
|
|
|
size_t snd_res_len;
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
assert((snd_ctxt != NULL) && (snd_buf_i != NULL));
|
|
|
|
|
|
|
|
result = RSC_LoadResource(snd_ctxt, snd_rn, &snd_res, &snd_res_len);
|
2004-10-27 21:32:28 +00:00
|
|
|
if (result != SUCCESS) {
|
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-12-24 12:47:16 +00:00
|
|
|
|
2004-04-28 23:10:59 +00:00
|
|
|
switch (_snd_info.res_type) {
|
2004-10-27 21:32:28 +00:00
|
|
|
case GAME_SOUND_PCM:
|
2004-04-28 23:10:59 +00:00
|
|
|
snd_buf_i->s_freq = _snd_info.freq;
|
|
|
|
snd_buf_i->s_samplebits = _snd_info.sample_size;
|
|
|
|
snd_buf_i->s_stereo = _snd_info.stereo;
|
2004-04-12 21:40:49 +00:00
|
|
|
snd_buf_i->s_buf = snd_res;
|
|
|
|
snd_buf_i->s_buf_len = snd_res_len;
|
|
|
|
snd_buf_i->s_signed = 1;
|
|
|
|
break;
|
2004-10-27 21:32:28 +00:00
|
|
|
case GAME_SOUND_VOC:
|
2004-10-10 12:27:43 +00:00
|
|
|
result = loadVocSound(snd_res, snd_res_len, snd_buf_i);
|
|
|
|
RSC_FreeResource(snd_res);
|
2004-10-27 21:32:28 +00:00
|
|
|
if (result != SUCCESS) {
|
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-11-18 17:34:54 +00:00
|
|
|
case GAME_SOUND_WAV:
|
|
|
|
result = loadWavSound(snd_res, snd_res_len, snd_buf_i);
|
|
|
|
RSC_FreeResource(snd_res);
|
|
|
|
if (result != SUCCESS) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
2004-04-12 21:40:49 +00:00
|
|
|
default:
|
|
|
|
/* Unknown sound type */
|
|
|
|
RSC_FreeResource(snd_res);
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int SndRes::loadVocSound(byte *snd_res, size_t snd_res_len, SOUNDBUFFER *snd_buf_i) {
|
2004-12-15 00:24:12 +00:00
|
|
|
MemoryReadStreamEndian readS(snd_res, snd_res_len, IS_BIG_ENDIAN);
|
2004-12-11 23:36:17 +00:00
|
|
|
byte *data;
|
|
|
|
int rate;
|
|
|
|
int len;
|
2004-05-04 03:33:03 +00:00
|
|
|
|
2004-12-11 23:36:17 +00:00
|
|
|
data = loadVOCFromStream(readS, len, rate);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-11 23:36:17 +00:00
|
|
|
if (!data) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-11 23:36:17 +00:00
|
|
|
snd_buf_i->s_freq = rate;
|
|
|
|
snd_buf_i->s_samplebits = 8;
|
2004-12-24 12:47:16 +00:00
|
|
|
snd_buf_i->s_stereo = 0;
|
2004-12-11 23:36:17 +00:00
|
|
|
snd_buf_i->s_signed = 0;
|
|
|
|
snd_buf_i->s_buf = data;
|
|
|
|
snd_buf_i->s_buf_len = len;
|
2004-12-15 00:24:12 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-12-11 23:36:17 +00:00
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-11-18 17:34:54 +00:00
|
|
|
int SndRes::loadWavSound(byte *snd_res, size_t snd_res_len, SOUNDBUFFER *snd_buf_i) {
|
2005-01-11 08:32:10 +00:00
|
|
|
Common::MemoryReadStream readS(snd_res, snd_res_len);
|
2005-01-09 15:57:38 +00:00
|
|
|
int rate, size;
|
2005-01-11 08:32:10 +00:00
|
|
|
byte flags;
|
2004-11-18 17:34:54 +00:00
|
|
|
|
2005-01-11 08:32:10 +00:00
|
|
|
if (!loadWAVFromStream(readS, size, rate, flags)) {
|
2004-11-18 17:34:54 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2005-05-10 23:48:48 +00:00
|
|
|
snd_buf_i->s_stereo = ((flags & Audio::Mixer::FLAG_STEREO) != 0);
|
2005-01-11 08:32:10 +00:00
|
|
|
snd_buf_i->s_freq = rate;
|
2004-11-18 17:34:54 +00:00
|
|
|
snd_buf_i->s_samplebits = 16;
|
|
|
|
snd_buf_i->s_signed = 1;
|
2005-01-11 08:32:10 +00:00
|
|
|
snd_buf_i->s_buf_len = size;
|
2004-11-18 17:34:54 +00:00
|
|
|
|
|
|
|
byte *data = (byte *)malloc(snd_buf_i->s_buf_len);
|
|
|
|
if (!data) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
readS.read(data, snd_buf_i->s_buf_len);
|
|
|
|
snd_buf_i->s_buf = data;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
int SndRes::getVoiceLength(uint32 voice_rn) {
|
2004-05-17 16:11:04 +00:00
|
|
|
int res_type = _snd_info.res_type;
|
2004-10-16 20:38:37 +00:00
|
|
|
uint32 length = 0;
|
2004-11-15 03:03:48 +00:00
|
|
|
bool voiceFile = false;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
double ms_f;
|
|
|
|
int ms_i = -1;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int result = FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-28 23:10:59 +00:00
|
|
|
assert(_init);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
Common::File f;
|
2004-05-17 16:11:04 +00:00
|
|
|
|
2004-10-10 12:27:43 +00:00
|
|
|
// The Wyrmkeep release of Inherit the Earth provides a separate file
|
|
|
|
// (p2_a.voc or P2_A.iaf), to correct voice 4 in the intro. Use that,
|
|
|
|
// if available.
|
2004-05-17 16:11:04 +00:00
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (_vm->getGameType() == GType_ITE && voice_rn == 4) {
|
2004-10-10 12:27:43 +00:00
|
|
|
if (f.open("p2_a.voc")) {
|
2004-10-27 21:32:28 +00:00
|
|
|
result = SUCCESS;
|
2004-10-10 12:27:43 +00:00
|
|
|
length = f.size();
|
2004-10-27 21:32:28 +00:00
|
|
|
res_type = GAME_SOUND_VOC;
|
2004-10-10 12:27:43 +00:00
|
|
|
f.close();
|
2004-11-15 03:03:48 +00:00
|
|
|
voiceFile = true;
|
2004-10-10 12:27:43 +00:00
|
|
|
} else if (f.open("P2_A.iaf")) {
|
2004-10-27 21:32:28 +00:00
|
|
|
result = SUCCESS;
|
2004-10-10 12:27:43 +00:00
|
|
|
length = f.size();
|
2004-10-27 21:32:28 +00:00
|
|
|
res_type = GAME_SOUND_PCM;
|
2004-10-10 12:27:43 +00:00
|
|
|
f.close();
|
2004-11-15 03:03:48 +00:00
|
|
|
voiceFile = true;
|
2004-10-10 12:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (result == FAILURE) {
|
2004-05-17 16:11:04 +00:00
|
|
|
result = RSC_GetResourceSize(_voice_ctxt, voice_rn, &length);
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (result != SUCCESS) {
|
2004-05-17 16:11:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (_vm->getFeatures() & GF_VOX_VOICES && !voiceFile) {
|
2004-11-15 03:03:48 +00:00
|
|
|
// Rough hack, fix this to be accurate
|
|
|
|
ms_f = (double)length / 22050 * 2000.0;
|
|
|
|
ms_i = (int)ms_f;
|
|
|
|
} else if (res_type == GAME_SOUND_PCM) {
|
2005-04-06 12:19:15 +00:00
|
|
|
ms_f = (double)length / (_snd_info.sample_size / 8) / (_snd_info.freq) * 1000.0;
|
2004-04-12 21:40:49 +00:00
|
|
|
ms_i = (int)ms_f;
|
2004-12-24 12:47:16 +00:00
|
|
|
} else if (res_type == GAME_SOUND_VOC) {
|
2004-05-01 16:15:55 +00:00
|
|
|
// Rough hack, fix this to be accurate
|
2004-04-12 21:40:49 +00:00
|
|
|
ms_f = (double)length / 14705 * 1000.0;
|
|
|
|
ms_i = (int)ms_f;
|
2004-11-18 17:34:54 +00:00
|
|
|
} else if (res_type == GAME_SOUND_WAV) {
|
2004-11-24 15:02:00 +00:00
|
|
|
// IHNM does not use the same format for all its WAV sounds, so
|
|
|
|
// we just have to load the stupid thing and see what that will
|
|
|
|
// tell us about it.
|
|
|
|
|
|
|
|
SOUNDBUFFER snd_buffer;
|
|
|
|
|
|
|
|
result = load(_voice_ctxt, voice_rn, &snd_buffer);
|
|
|
|
if (result != SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-25 07:15:08 +00:00
|
|
|
free(snd_buffer.s_buf);
|
|
|
|
|
2004-11-24 15:02:00 +00:00
|
|
|
ms_f = (double)snd_buffer.s_buf_len;
|
|
|
|
if (snd_buffer.s_samplebits == 16) {
|
|
|
|
ms_f /= 2.0;
|
|
|
|
}
|
|
|
|
if (snd_buffer.s_stereo) {
|
|
|
|
ms_f /= 2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ms_f = ms_f / snd_buffer.s_freq * 1000.0;
|
|
|
|
ms_i = (int)ms_f;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ms_i;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|