2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2003-12-16 02:10:15 +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
|
2008-01-05 12:45:14 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-12-16 02:10:15 +00:00
|
|
|
* 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.
|
2003-12-16 02:10:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-29 15:59:37 +00:00
|
|
|
#include "common/endian.h"
|
|
|
|
|
2003-12-16 02:10:15 +00:00
|
|
|
#include "common/util.h"
|
2010-11-19 17:03:07 +00:00
|
|
|
#include "common/memstream.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2006-03-29 15:59:37 +00:00
|
|
|
|
|
|
|
#include "sword1/sound.h"
|
2004-10-21 12:43:49 +00:00
|
|
|
#include "sword1/resman.h"
|
|
|
|
#include "sword1/logic.h"
|
|
|
|
#include "sword1/sword1.h"
|
2003-12-16 02:10:15 +00:00
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/decoders/flac.h"
|
|
|
|
#include "audio/decoders/mp3.h"
|
|
|
|
#include "audio/decoders/raw.h"
|
|
|
|
#include "audio/decoders/vorbis.h"
|
|
|
|
#include "audio/decoders/wave.h"
|
|
|
|
#include "audio/decoders/vag.h"
|
2004-11-27 15:58:18 +00:00
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
namespace Sword1 {
|
|
|
|
|
2003-12-16 02:10:15 +00:00
|
|
|
#define SOUND_SPEECH_ID 1
|
2010-01-19 22:30:33 +00:00
|
|
|
#define SPEECH_FLAGS (Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN)
|
2003-12-16 02:10:15 +00:00
|
|
|
|
2011-05-16 14:35:10 +00:00
|
|
|
Sound::Sound(const char *searchPath, Audio::Mixer *mixer, ResMan *pResMan)
|
|
|
|
: _rnd("sword1sound") {
|
2003-12-16 02:10:15 +00:00
|
|
|
strcpy(_filePath, searchPath);
|
|
|
|
_mixer = mixer;
|
|
|
|
_resMan = pResMan;
|
2009-07-12 18:52:38 +00:00
|
|
|
_bigEndianSpeech = false;
|
2003-12-16 02:10:15 +00:00
|
|
|
_cowHeader = NULL;
|
|
|
|
_endOfQueue = 0;
|
2003-12-20 20:20:53 +00:00
|
|
|
_currentCowFile = 0;
|
2004-01-07 17:47:46 +00:00
|
|
|
_speechVolL = _speechVolR = _sfxVolL = _sfxVolR = 192;
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
Sound::~Sound() {
|
2004-11-09 04:06:10 +00:00
|
|
|
// clean up fx queue
|
|
|
|
_mixer->stopAll();
|
|
|
|
for (uint8 cnt = 0; cnt < _endOfQueue; cnt++)
|
|
|
|
if (_fxQueue[cnt].delay == 0)
|
|
|
|
_resMan->resClose(_fxList[_fxQueue[cnt].id].sampleId);
|
|
|
|
_endOfQueue = 0;
|
|
|
|
closeCowSystem();
|
|
|
|
}
|
|
|
|
|
2009-07-12 18:52:38 +00:00
|
|
|
void Sound::checkSpeechFileEndianness() {
|
|
|
|
// Some mac versions (not all of them) use big endian wav, although
|
|
|
|
// the wav header doesn't indicate it.
|
|
|
|
// Use heuristic to determine endianness of speech.
|
|
|
|
// The heuristic consist in computing the sum of the absolute difference for
|
|
|
|
// every two consecutive samples. This is done both with a big endian and a
|
|
|
|
// little endian assumption. The one with the smallest sum should be the
|
|
|
|
// correct one (the sound wave is supposed to be relatively smooth).
|
|
|
|
// It needs at least 1000 samples to get stable result (the code below is
|
2009-07-14 13:48:31 +00:00
|
|
|
// using the first 2000 samples of the wav sound).
|
2009-07-12 18:52:38 +00:00
|
|
|
|
2009-07-14 13:48:31 +00:00
|
|
|
// Init speech file if not already done.
|
2009-07-12 18:52:38 +00:00
|
|
|
if (!_currentCowFile) {
|
2009-07-14 13:48:31 +00:00
|
|
|
// Open one of the speech files. It uses SwordEngine::_systemVars.currentCD
|
2009-07-12 18:52:38 +00:00
|
|
|
// to decide which file to open, therefore if it is currently set to zero
|
|
|
|
// we have to set it to either 1 or 2 (I decided to set it to 1 as this is
|
|
|
|
// more likely to be the first file that will be needed).
|
|
|
|
bool no_current_cd = false;
|
|
|
|
if (SwordEngine::_systemVars.currentCD == 0) {
|
|
|
|
SwordEngine::_systemVars.currentCD = 1;
|
|
|
|
no_current_cd = true;
|
|
|
|
}
|
|
|
|
initCowSystem();
|
|
|
|
if (no_current_cd) {
|
2009-07-14 13:48:31 +00:00
|
|
|
// In case it fails with CD1 retry with CD2
|
2009-07-12 18:52:38 +00:00
|
|
|
if (!_currentCowFile) {
|
|
|
|
SwordEngine::_systemVars.currentCD = 2;
|
|
|
|
initCowSystem();
|
|
|
|
}
|
2009-07-14 13:48:31 +00:00
|
|
|
// Reset currentCD flag
|
2009-07-12 18:52:38 +00:00
|
|
|
SwordEngine::_systemVars.currentCD = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-14 13:48:31 +00:00
|
|
|
// Testing for endianness makes sense only if using the uncompressed files.
|
2009-07-12 18:52:38 +00:00
|
|
|
if (_cowHeader == NULL || (_cowMode != CowWave && _cowMode != CowDemo))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// I picked the sample to use randomly (I just made sure it is long enough so that there is
|
2009-07-14 13:48:31 +00:00
|
|
|
// a fair change of the heuristic to have a stable result and work for every language).
|
2009-07-12 18:52:38 +00:00
|
|
|
int roomNo = _currentCowFile == 1 ? 1 : 129;
|
|
|
|
int localNo = _currentCowFile == 1 ? 2 : 933;
|
|
|
|
// Get the speech data and apply the heuristic
|
|
|
|
uint32 locIndex = _cowHeader[roomNo] >> 2;
|
|
|
|
uint32 sampleSize = _cowHeader[locIndex + (localNo * 2)];
|
|
|
|
uint32 index = _cowHeader[locIndex + (localNo * 2) - 1];
|
|
|
|
if (sampleSize) {
|
|
|
|
uint32 size;
|
|
|
|
double be_diff_sum = 0., le_diff_sum = 0.;
|
|
|
|
_bigEndianSpeech = false;
|
|
|
|
int16 *data = uncompressSpeech(index + _cowHeaderSize, sampleSize, &size);
|
2009-07-14 13:48:31 +00:00
|
|
|
// Compute average of difference between two consecutive samples for both BE and LE
|
2009-07-12 18:52:38 +00:00
|
|
|
if (data) {
|
|
|
|
if (size > 4000)
|
|
|
|
size = 2000;
|
|
|
|
else
|
|
|
|
size /= 2;
|
|
|
|
int16 prev_be_value = (int16)SWAP_BYTES_16(*((uint16*)(data)));
|
2011-02-07 23:01:06 +00:00
|
|
|
for (uint32 i = 1; i < size; ++i) {
|
2009-07-13 17:09:40 +00:00
|
|
|
le_diff_sum += fabs((double)(data[i] - data[i-1]));
|
2009-07-12 18:52:38 +00:00
|
|
|
int16 be_value = (int16)SWAP_BYTES_16(*((uint16*)(data + i)));
|
2009-07-13 17:09:40 +00:00
|
|
|
be_diff_sum += fabs((double)(be_value - prev_be_value));
|
2009-07-12 18:52:38 +00:00
|
|
|
prev_be_value = be_value;
|
|
|
|
}
|
|
|
|
delete [] data;
|
|
|
|
}
|
|
|
|
// Set the big endian flag
|
|
|
|
_bigEndianSpeech = (be_diff_sum < le_diff_sum);
|
|
|
|
if (_bigEndianSpeech)
|
|
|
|
debug(6, "Mac version: using big endian speech file");
|
|
|
|
else
|
|
|
|
debug(6, "Mac version: using little endian speech file");
|
|
|
|
debug(8, "Speech endianness heuristic: average = %f for BE and %f for LE, computed on %d samples)", be_diff_sum / (size - 1), le_diff_sum / (size - 1), size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
int Sound::addToQueue(int32 fxNo) {
|
2003-12-16 02:10:15 +00:00
|
|
|
bool alreadyInQueue = false;
|
|
|
|
for (uint8 cnt = 0; (cnt < _endOfQueue) && (!alreadyInQueue); cnt++)
|
2003-12-16 09:43:08 +00:00
|
|
|
if (_fxQueue[cnt].id == (uint32)fxNo)
|
2003-12-16 02:10:15 +00:00
|
|
|
alreadyInQueue = true;
|
|
|
|
if (!alreadyInQueue) {
|
2003-12-17 05:16:37 +00:00
|
|
|
if (_endOfQueue == MAX_FXQ_LENGTH) {
|
|
|
|
warning("Sound queue overflow");
|
|
|
|
return 0;
|
|
|
|
}
|
2003-12-16 02:10:15 +00:00
|
|
|
_resMan->resOpen(_fxList[fxNo].sampleId);
|
|
|
|
_fxQueue[_endOfQueue].id = fxNo;
|
|
|
|
if (_fxList[fxNo].type == FX_SPOT)
|
|
|
|
_fxQueue[_endOfQueue].delay = _fxList[fxNo].delay + 1;
|
|
|
|
else
|
|
|
|
_fxQueue[_endOfQueue].delay = 1;
|
|
|
|
_endOfQueue++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
void Sound::engine() {
|
2003-12-16 02:10:15 +00:00
|
|
|
// first of all, add any random sfx to the queue...
|
|
|
|
for (uint16 cnt = 0; cnt < TOTAL_FX_PER_ROOM; cnt++) {
|
2004-01-11 15:47:41 +00:00
|
|
|
uint16 fxNo = _roomsFixedFx[Logic::_scriptVars[SCREEN]][cnt];
|
2003-12-16 05:02:34 +00:00
|
|
|
if (fxNo) {
|
2003-12-16 02:10:15 +00:00
|
|
|
if (_fxList[fxNo].type == FX_RANDOM) {
|
|
|
|
if (_rnd.getRandomNumber(_fxList[fxNo].delay) == 0)
|
|
|
|
addToQueue(fxNo);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// now process the queue
|
2003-12-23 00:59:18 +00:00
|
|
|
for (uint8 cnt2 = 0; cnt2 < _endOfQueue; cnt2++) {
|
|
|
|
if (_fxQueue[cnt2].delay > 0) {
|
|
|
|
_fxQueue[cnt2].delay--;
|
|
|
|
if (_fxQueue[cnt2].delay == 0)
|
|
|
|
playSample(&_fxQueue[cnt2]);
|
2003-12-16 02:10:15 +00:00
|
|
|
} else {
|
2005-03-12 18:56:09 +00:00
|
|
|
if (!_mixer->isSoundHandleActive(_fxQueue[cnt2].handle)) { // sound finished
|
2003-12-23 00:59:18 +00:00
|
|
|
_resMan->resClose(_fxList[_fxQueue[cnt2].id].sampleId);
|
|
|
|
if (cnt2 != _endOfQueue-1)
|
|
|
|
_fxQueue[cnt2] = _fxQueue[_endOfQueue - 1];
|
2003-12-16 02:10:15 +00:00
|
|
|
_endOfQueue--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Sound::fnStopFx(int32 fxNo) {
|
2003-12-17 05:16:37 +00:00
|
|
|
_mixer->stopID(fxNo);
|
|
|
|
for (uint8 cnt = 0; cnt < _endOfQueue; cnt++)
|
|
|
|
if (_fxQueue[cnt].id == (uint32)fxNo) {
|
|
|
|
if (!_fxQueue[cnt].delay) // sound was started
|
|
|
|
_resMan->resClose(_fxList[_fxQueue[cnt].id].sampleId);
|
2003-12-19 14:07:12 +00:00
|
|
|
if (cnt != _endOfQueue-1)
|
2003-12-17 05:16:37 +00:00
|
|
|
_fxQueue[cnt] = _fxQueue[_endOfQueue-1];
|
|
|
|
_endOfQueue--;
|
2011-02-07 23:01:06 +00:00
|
|
|
return;
|
2003-12-17 05:16:37 +00:00
|
|
|
}
|
|
|
|
debug(8, "fnStopFx: id not found in queue");
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
bool Sound::amISpeaking() {
|
2003-12-28 23:24:03 +00:00
|
|
|
_waveVolPos++;
|
2003-12-29 14:04:57 +00:00
|
|
|
return _waveVolume[_waveVolPos - 1];
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
bool Sound::speechFinished() {
|
2005-03-12 18:56:09 +00:00
|
|
|
return !_mixer->isSoundHandleActive(_speechHandle);
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Sound::newScreen(uint32 screen) {
|
2003-12-20 20:20:53 +00:00
|
|
|
if (_currentCowFile != SwordEngine::_systemVars.currentCD) {
|
2010-01-08 19:58:47 +00:00
|
|
|
if (_cowFile.isOpen())
|
2003-12-20 20:20:53 +00:00
|
|
|
closeCowSystem();
|
2005-07-30 21:11:48 +00:00
|
|
|
initCowSystem();
|
2003-12-20 20:20:53 +00:00
|
|
|
}
|
2009-09-13 21:38:47 +00:00
|
|
|
|
|
|
|
// Start the room's looping sounds.
|
|
|
|
for (uint16 cnt = 0; cnt < TOTAL_FX_PER_ROOM; cnt++) {
|
|
|
|
uint16 fxNo = _roomsFixedFx[screen][cnt];
|
|
|
|
if (fxNo) {
|
|
|
|
if (_fxList[fxNo].type == FX_LOOP)
|
|
|
|
addToQueue(fxNo);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2003-12-20 20:20:53 +00:00
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
void Sound::quitScreen() {
|
2003-12-17 05:16:37 +00:00
|
|
|
// stop all running SFX
|
|
|
|
while (_endOfQueue)
|
|
|
|
fnStopFx(_fxQueue[0].id);
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Sound::playSample(QueueElement *elem) {
|
2003-12-19 06:51:32 +00:00
|
|
|
uint8 *sampleData = (uint8*)_resMan->fetchRes(_fxList[elem->id].sampleId);
|
2003-12-16 02:10:15 +00:00
|
|
|
for (uint16 cnt = 0; cnt < MAX_ROOMS_PER_FX; cnt++) {
|
2003-12-19 06:51:32 +00:00
|
|
|
if (_fxList[elem->id].roomVolList[cnt].roomNo) {
|
2004-01-11 15:47:41 +00:00
|
|
|
if ((_fxList[elem->id].roomVolList[cnt].roomNo == (int)Logic::_scriptVars[SCREEN]) ||
|
2003-12-19 06:51:32 +00:00
|
|
|
(_fxList[elem->id].roomVolList[cnt].roomNo == -1)) {
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2004-01-07 17:47:46 +00:00
|
|
|
uint8 volL = (_fxList[elem->id].roomVolList[cnt].leftVol * 10 * _sfxVolL) / 255;
|
|
|
|
uint8 volR = (_fxList[elem->id].roomVolList[cnt].rightVol * 10 * _sfxVolR) / 255;
|
2003-12-16 02:10:15 +00:00
|
|
|
int8 pan = (volR - volL) / 2;
|
|
|
|
uint8 volume = (volR + volL) / 2;
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2010-01-25 01:39:44 +00:00
|
|
|
if (SwordEngine::isPsx()) {
|
2009-02-28 10:46:33 +00:00
|
|
|
uint32 size = READ_LE_UINT32(sampleData);
|
2010-01-28 09:38:21 +00:00
|
|
|
Audio::AudioStream *audStream = Audio::makeLoopingAudioStream(Audio::makeVagStream(new Common::MemoryReadStream(sampleData + 4, size-4)), (_fxList[elem->id].type == FX_LOOP) ? 0 : 1);
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kSFXSoundType, &elem->handle, audStream, elem->id, volume, pan);
|
2009-02-28 10:46:33 +00:00
|
|
|
} else {
|
|
|
|
uint32 size = READ_LE_UINT32(sampleData + 0x28);
|
|
|
|
uint8 flags;
|
|
|
|
if (READ_LE_UINT16(sampleData + 0x22) == 16)
|
2010-01-19 22:30:33 +00:00
|
|
|
flags = Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN;
|
2009-02-28 10:46:33 +00:00
|
|
|
else
|
2010-01-19 22:30:33 +00:00
|
|
|
flags = Audio::FLAG_UNSIGNED;
|
2009-02-28 10:46:33 +00:00
|
|
|
if (READ_LE_UINT16(sampleData + 0x16) == 2)
|
2010-01-19 22:30:33 +00:00
|
|
|
flags |= Audio::FLAG_STEREO;
|
2010-01-19 23:50:33 +00:00
|
|
|
Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
|
2010-01-30 15:26:54 +00:00
|
|
|
Audio::makeRawStream(sampleData + 0x2C, size, 11025, flags, DisposeAfterUse::NO),
|
2010-01-19 23:50:33 +00:00
|
|
|
(_fxList[elem->id].type == FX_LOOP) ? 0 : 1);
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kSFXSoundType, &elem->handle, stream, elem->id, volume, pan);
|
2009-02-28 10:46:33 +00:00
|
|
|
}
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
bool Sound::startSpeech(uint16 roomNo, uint16 localNo) {
|
2003-12-16 02:10:15 +00:00
|
|
|
if (_cowHeader == NULL) {
|
2006-04-04 22:21:10 +00:00
|
|
|
warning("Sound::startSpeech: COW file isn't open");
|
2003-12-16 02:10:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
uint32 locIndex = 0xFFFFFFFF;
|
|
|
|
uint32 sampleSize = 0;
|
|
|
|
uint32 index = 0;
|
2003-12-16 02:10:15 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
if (_cowMode == CowPSX) {
|
|
|
|
Common::File file;
|
|
|
|
uint16 i;
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
if (!file.open("speech.lis")) {
|
|
|
|
warning ("Could not open speech.lis");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-24 15:17:42 +00:00
|
|
|
for (i = 0; !file.eos() && !file.err(); i++)
|
2009-02-28 10:46:33 +00:00
|
|
|
if (file.readUint16LE() == roomNo) {
|
|
|
|
locIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
file.close();
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
if (locIndex == 0xFFFFFFFF) {
|
|
|
|
warning ("Could not find room %d in speech.lis", roomNo);
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
if (!file.open("speech.inf")) {
|
|
|
|
warning ("Could not open speech.inf");
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-06-08 17:54:44 +00:00
|
|
|
uint16 numRooms = file.readUint16LE(); // Read number of rooms referenced in this file
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
file.seek(locIndex * 4 + 2); // 4 bytes per room, skip first 2 bytes
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
uint16 numLines = file.readUint16LE();
|
2009-05-24 15:17:42 +00:00
|
|
|
uint16 roomOffset = file.readUint16LE();
|
2009-02-28 10:46:33 +00:00
|
|
|
|
2009-06-08 17:54:44 +00:00
|
|
|
file.seek(2 + numRooms * 4 + roomOffset * 2); // The offset is in terms of uint16's, so multiply by 2. Skip the room indexes too.
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
locIndex = 0xFFFFFFFF;
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
for (i = 0; i < numLines; i++)
|
|
|
|
if (file.readUint16LE() == localNo) {
|
|
|
|
locIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
if (locIndex == 0xFFFFFFFF) {
|
|
|
|
warning ("Could not find local number %d in room %d in speech.inf", roomNo, localNo);
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
file.close();
|
|
|
|
|
|
|
|
index = _cowHeader[(roomOffset + locIndex) * 2];
|
|
|
|
sampleSize = _cowHeader[(roomOffset + locIndex) * 2 + 1];
|
|
|
|
} else {
|
|
|
|
locIndex = _cowHeader[roomNo] >> 2;
|
|
|
|
sampleSize = _cowHeader[locIndex + (localNo * 2)];
|
|
|
|
index = _cowHeader[locIndex + (localNo * 2) - 1];
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2003-12-21 15:55:55 +00:00
|
|
|
debug(6, "startSpeech(%d, %d): locIndex %d, sampleSize %d, index %d", roomNo, localNo, locIndex, sampleSize, index);
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2010-01-19 11:22:14 +00:00
|
|
|
Audio::AudioStream *stream = 0;
|
|
|
|
|
2003-12-16 02:10:15 +00:00
|
|
|
if (sampleSize) {
|
2004-01-07 17:47:46 +00:00
|
|
|
uint8 speechVol = (_speechVolR + _speechVolL) / 2;
|
|
|
|
int8 speechPan = (_speechVolR - _speechVolL) / 2;
|
2004-10-13 00:59:27 +00:00
|
|
|
if ((_cowMode == CowWave) || (_cowMode == CowDemo)) {
|
2004-10-12 15:50:00 +00:00
|
|
|
uint32 size;
|
|
|
|
int16 *data = uncompressSpeech(index + _cowHeaderSize, sampleSize, &size);
|
2010-01-19 11:22:14 +00:00
|
|
|
if (data) {
|
2010-01-30 15:26:54 +00:00
|
|
|
stream = Audio::makeRawStream((byte *)data, size, 11025, SPEECH_FLAGS);
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kSpeechSoundType, &_speechHandle, stream, SOUND_SPEECH_ID, speechVol, speechPan);
|
2010-01-19 11:22:14 +00:00
|
|
|
}
|
2009-02-28 10:46:33 +00:00
|
|
|
} else if (_cowMode == CowPSX && sampleSize != 0xffffffff) {
|
|
|
|
_cowFile.seek(index * 2048);
|
2010-11-19 17:03:07 +00:00
|
|
|
Common::SeekableReadStream *tmp = _cowFile.readStream(sampleSize);
|
2010-01-19 11:22:14 +00:00
|
|
|
assert(tmp);
|
2010-01-28 09:38:21 +00:00
|
|
|
stream = Audio::makeVagStream(tmp);
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kSpeechSoundType, &_speechHandle, stream, SOUND_SPEECH_ID, speechVol, speechPan);
|
2009-02-28 10:46:33 +00:00
|
|
|
// with compressed audio, we can't calculate the wave volume.
|
|
|
|
// so default to talking.
|
|
|
|
for (int cnt = 0; cnt < 480; cnt++)
|
|
|
|
_waveVolume[cnt] = true;
|
|
|
|
_waveVolPos = 0;
|
2005-07-30 21:11:48 +00:00
|
|
|
}
|
2007-06-26 03:08:03 +00:00
|
|
|
#ifdef USE_FLAC
|
2010-02-03 09:42:40 +00:00
|
|
|
else if (_cowMode == CowFLAC) {
|
2004-10-12 15:50:00 +00:00
|
|
|
_cowFile.seek(index);
|
2010-11-19 17:03:07 +00:00
|
|
|
Common::SeekableReadStream *tmp = _cowFile.readStream(sampleSize);
|
2007-07-15 19:24:00 +00:00
|
|
|
assert(tmp);
|
2010-02-03 09:42:11 +00:00
|
|
|
stream = Audio::makeFLACStream(tmp, DisposeAfterUse::YES);
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kSpeechSoundType, &_speechHandle, stream, SOUND_SPEECH_ID, speechVol, speechPan);
|
2004-10-12 15:50:00 +00:00
|
|
|
// with compressed audio, we can't calculate the wave volume.
|
|
|
|
// so default to talking.
|
|
|
|
for (int cnt = 0; cnt < 480; cnt++)
|
|
|
|
_waveVolume[cnt] = true;
|
|
|
|
_waveVolPos = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 12:42:56 +00:00
|
|
|
#ifdef USE_VORBIS
|
2004-10-12 15:50:00 +00:00
|
|
|
else if (_cowMode == CowVorbis) {
|
|
|
|
_cowFile.seek(index);
|
2010-11-19 17:03:07 +00:00
|
|
|
Common::SeekableReadStream *tmp = _cowFile.readStream(sampleSize);
|
2007-07-15 19:24:00 +00:00
|
|
|
assert(tmp);
|
2010-01-19 11:22:14 +00:00
|
|
|
stream = Audio::makeVorbisStream(tmp, DisposeAfterUse::YES);
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kSpeechSoundType, &_speechHandle, stream, SOUND_SPEECH_ID, speechVol, speechPan);
|
2007-06-26 03:08:03 +00:00
|
|
|
// with compressed audio, we can't calculate the wave volume.
|
|
|
|
// so default to talking.
|
|
|
|
for (int cnt = 0; cnt < 480; cnt++)
|
|
|
|
_waveVolume[cnt] = true;
|
|
|
|
_waveVolPos = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MAD
|
2010-02-03 09:42:40 +00:00
|
|
|
else if (_cowMode == CowMP3) {
|
2007-06-26 03:08:03 +00:00
|
|
|
_cowFile.seek(index);
|
2010-11-19 17:03:07 +00:00
|
|
|
Common::SeekableReadStream *tmp = _cowFile.readStream(sampleSize);
|
2007-07-15 19:24:00 +00:00
|
|
|
assert(tmp);
|
2010-01-19 11:22:14 +00:00
|
|
|
stream = Audio::makeMP3Stream(tmp, DisposeAfterUse::YES);
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(Audio::Mixer::kSpeechSoundType, &_speechHandle, stream, SOUND_SPEECH_ID, speechVol, speechPan);
|
2007-06-26 03:08:03 +00:00
|
|
|
// with compressed audio, we can't calculate the wave volume.
|
|
|
|
// so default to talking.
|
2004-10-12 15:50:00 +00:00
|
|
|
for (int cnt = 0; cnt < 480; cnt++)
|
2005-07-30 21:11:48 +00:00
|
|
|
_waveVolume[cnt] = true;
|
2004-10-12 15:50:00 +00:00
|
|
|
_waveVolPos = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2003-12-16 02:10:15 +00:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
int16 *Sound::uncompressSpeech(uint32 index, uint32 cSize, uint32 *size) {
|
2003-12-21 15:55:55 +00:00
|
|
|
uint8 *fBuf = (uint8*)malloc(cSize);
|
|
|
|
_cowFile.seek(index);
|
|
|
|
_cowFile.read(fBuf, cSize);
|
|
|
|
uint32 headerPos = 0;
|
2005-01-09 15:57:38 +00:00
|
|
|
|
2003-12-21 15:55:55 +00:00
|
|
|
while ((READ_BE_UINT32(fBuf + headerPos) != 'data') && (headerPos < 100))
|
|
|
|
headerPos++;
|
2009-10-14 23:29:14 +00:00
|
|
|
|
2003-12-21 15:55:55 +00:00
|
|
|
if (headerPos < 100) {
|
2004-08-31 07:52:47 +00:00
|
|
|
int32 resSize;
|
2008-10-26 15:20:24 +00:00
|
|
|
int16 *srcData;
|
|
|
|
uint32 srcPos;
|
|
|
|
int16 length;
|
|
|
|
cSize /= 2;
|
2004-11-15 02:48:30 +00:00
|
|
|
headerPos += 4; // skip 'data' tag
|
|
|
|
if (_cowMode != CowDemo) {
|
|
|
|
resSize = READ_LE_UINT32(fBuf + headerPos) >> 1;
|
|
|
|
headerPos += 4;
|
|
|
|
} else {
|
2008-10-26 15:20:24 +00:00
|
|
|
// the demo speech files have the uncompressed size
|
|
|
|
// embedded in the compressed stream *sigh*
|
|
|
|
//
|
|
|
|
// But not always, apparently. See bug #2182450. Is
|
|
|
|
// there any way to figure out the size other than
|
|
|
|
// decoding the sound in that case?
|
|
|
|
|
|
|
|
if (fBuf[headerPos + 1] == 0) {
|
|
|
|
if (READ_LE_UINT16(fBuf + headerPos) == 1) {
|
|
|
|
resSize = READ_LE_UINT16(fBuf + headerPos + 2);
|
|
|
|
resSize |= READ_LE_UINT16(fBuf + headerPos + 6) << 16;
|
|
|
|
} else
|
|
|
|
resSize = READ_LE_UINT32(fBuf + headerPos + 2);
|
|
|
|
resSize >>= 1;
|
|
|
|
} else {
|
|
|
|
resSize = 0;
|
|
|
|
srcData = (int16*)fBuf;
|
|
|
|
srcPos = headerPos >> 1;
|
|
|
|
while (srcPos < cSize) {
|
|
|
|
length = (int16)READ_LE_UINT16(srcData + srcPos);
|
|
|
|
srcPos++;
|
|
|
|
if (length < 0) {
|
|
|
|
resSize -= length;
|
|
|
|
srcPos++;
|
|
|
|
} else {
|
|
|
|
resSize += length;
|
|
|
|
srcPos += length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-10-30 10:35:39 +00:00
|
|
|
}
|
2004-11-15 02:48:30 +00:00
|
|
|
assert(!(headerPos & 1));
|
2008-10-26 15:20:24 +00:00
|
|
|
srcData = (int16*)fBuf;
|
|
|
|
srcPos = headerPos >> 1;
|
2004-11-15 02:48:30 +00:00
|
|
|
uint32 dstPos = 0;
|
2006-09-17 15:25:59 +00:00
|
|
|
int16 *dstData = (int16*)malloc(resSize * 2);
|
|
|
|
int32 samplesLeft = resSize;
|
|
|
|
while (srcPos < cSize && samplesLeft > 0) {
|
2009-07-12 18:52:38 +00:00
|
|
|
length = (int16)(_bigEndianSpeech ? READ_BE_UINT16(srcData + srcPos) : READ_LE_UINT16(srcData + srcPos));
|
2003-12-21 15:55:55 +00:00
|
|
|
srcPos++;
|
|
|
|
if (length < 0) {
|
|
|
|
length = -length;
|
2006-09-17 15:25:59 +00:00
|
|
|
if (length > samplesLeft)
|
|
|
|
length = samplesLeft;
|
2009-07-12 18:52:38 +00:00
|
|
|
int16 value;
|
|
|
|
if (_bigEndianSpeech) {
|
|
|
|
value = (int16)SWAP_BYTES_16(*((uint16*)(srcData + srcPos)));
|
|
|
|
} else {
|
|
|
|
value = srcData[srcPos];
|
|
|
|
}
|
2003-12-21 15:55:55 +00:00
|
|
|
for (uint16 cnt = 0; cnt < (uint16)length; cnt++)
|
2009-07-12 18:52:38 +00:00
|
|
|
dstData[dstPos++] = value;
|
2003-12-21 15:55:55 +00:00
|
|
|
srcPos++;
|
|
|
|
} else {
|
2006-09-17 15:25:59 +00:00
|
|
|
if (length > samplesLeft)
|
|
|
|
length = samplesLeft;
|
2009-07-12 18:52:38 +00:00
|
|
|
if (_bigEndianSpeech) {
|
|
|
|
for (uint16 cnt = 0; cnt < (uint16)length; cnt++)
|
|
|
|
dstData[dstPos++] = (int16)SWAP_BYTES_16(*((uint16*)(srcData + (srcPos++))));
|
|
|
|
} else {
|
|
|
|
memcpy(dstData + dstPos, srcData + srcPos, length * 2);
|
|
|
|
dstPos += length;
|
|
|
|
srcPos += length;
|
|
|
|
}
|
2003-12-21 15:55:55 +00:00
|
|
|
}
|
2006-09-17 15:25:59 +00:00
|
|
|
samplesLeft -= length;
|
|
|
|
}
|
|
|
|
if (samplesLeft > 0) {
|
|
|
|
memset(dstData + dstPos, 0, samplesLeft * 2);
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
2004-11-15 02:48:30 +00:00
|
|
|
if (_cowMode == CowDemo) // demo has wave output size embedded in the compressed data
|
|
|
|
*(uint32*)dstData = 0;
|
2003-12-21 15:55:55 +00:00
|
|
|
free(fBuf);
|
|
|
|
*size = resSize * 2;
|
2003-12-28 23:24:03 +00:00
|
|
|
calcWaveVolume(dstData, resSize);
|
2003-12-21 15:55:55 +00:00
|
|
|
return dstData;
|
|
|
|
} else {
|
|
|
|
free(fBuf);
|
2004-01-11 15:47:41 +00:00
|
|
|
warning("Sound::uncompressSpeech(): DATA tag not found in wave header");
|
2003-12-21 15:55:55 +00:00
|
|
|
*size = 0;
|
|
|
|
return NULL;
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Sound::calcWaveVolume(int16 *data, uint32 length) {
|
2003-12-28 23:24:03 +00:00
|
|
|
int16 *blkPos = data + 918;
|
2004-01-17 11:16:38 +00:00
|
|
|
uint32 cnt;
|
|
|
|
for (cnt = 0; cnt < WAVE_VOL_TAB_LENGTH; cnt++)
|
2003-12-28 23:24:03 +00:00
|
|
|
_waveVolume[cnt] = false;
|
|
|
|
_waveVolPos = 0;
|
|
|
|
for (uint32 blkCnt = 1; blkCnt < length / 918; blkCnt++) {
|
|
|
|
if (blkCnt >= WAVE_VOL_TAB_LENGTH) {
|
2010-09-18 10:55:16 +00:00
|
|
|
warning("Wave vol tab too small");
|
2003-12-28 23:24:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
int32 average = 0;
|
2004-01-17 11:16:38 +00:00
|
|
|
for (cnt = 0; cnt < 918; cnt++)
|
2003-12-28 23:24:03 +00:00
|
|
|
average += blkPos[cnt];
|
2003-12-29 14:04:57 +00:00
|
|
|
average /= 918;
|
2003-12-28 23:24:03 +00:00
|
|
|
uint32 diff = 0;
|
2004-01-17 11:16:38 +00:00
|
|
|
for (cnt = 0; cnt < 918; cnt++) {
|
2003-12-28 23:24:03 +00:00
|
|
|
int16 smpDiff = *blkPos - average;
|
|
|
|
diff += (uint32)ABS(smpDiff);
|
|
|
|
blkPos++;
|
|
|
|
}
|
|
|
|
if (diff > WAVE_VOL_THRESHOLD)
|
|
|
|
_waveVolume[blkCnt - 1] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
void Sound::stopSpeech() {
|
2003-12-16 02:10:15 +00:00
|
|
|
_mixer->stopID(SOUND_SPEECH_ID);
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
void Sound::initCowSystem() {
|
2010-01-08 19:58:47 +00:00
|
|
|
if (SwordEngine::_systemVars.currentCD == 0)
|
|
|
|
return;
|
|
|
|
|
2003-12-20 20:20:53 +00:00
|
|
|
char cowName[25];
|
2003-12-26 03:07:01 +00:00
|
|
|
/* look for speech1/2.clu in the data dir
|
|
|
|
and speech/speech.clu (running from cd or using cd layout)
|
|
|
|
*/
|
2007-06-26 03:08:03 +00:00
|
|
|
#ifdef USE_FLAC
|
|
|
|
if (!_cowFile.isOpen()) {
|
|
|
|
sprintf(cowName, "SPEECH%d.CLF", SwordEngine::_systemVars.currentCD);
|
|
|
|
_cowFile.open(cowName);
|
|
|
|
if (_cowFile.isOpen()) {
|
2010-02-03 09:42:40 +00:00
|
|
|
debug(1, "Using FLAC compressed Speech Cluster");
|
|
|
|
_cowMode = CowFLAC;
|
2007-06-26 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 12:42:56 +00:00
|
|
|
#ifdef USE_VORBIS
|
2004-10-12 15:50:00 +00:00
|
|
|
if (!_cowFile.isOpen()) {
|
|
|
|
sprintf(cowName, "SPEECH%d.CLV", SwordEngine::_systemVars.currentCD);
|
|
|
|
_cowFile.open(cowName);
|
|
|
|
if (_cowFile.isOpen()) {
|
|
|
|
debug(1, "Using Vorbis compressed Speech Cluster");
|
|
|
|
_cowMode = CowVorbis;
|
|
|
|
}
|
|
|
|
}
|
2007-02-24 23:40:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_MAD
|
2007-02-28 10:44:52 +00:00
|
|
|
if (!_cowFile.isOpen()) {
|
|
|
|
sprintf(cowName, "SPEECH%d.CL3", SwordEngine::_systemVars.currentCD);
|
|
|
|
_cowFile.open(cowName);
|
|
|
|
if (_cowFile.isOpen()) {
|
|
|
|
debug(1, "Using MP3 compressed Speech Cluster");
|
2010-02-03 09:42:40 +00:00
|
|
|
_cowMode = CowMP3;
|
2007-02-28 10:44:52 +00:00
|
|
|
}
|
2007-02-24 23:40:28 +00:00
|
|
|
}
|
2004-10-12 15:50:00 +00:00
|
|
|
#endif
|
|
|
|
if (!_cowFile.isOpen()) {
|
|
|
|
sprintf(cowName, "SPEECH%d.CLU", SwordEngine::_systemVars.currentCD);
|
|
|
|
_cowFile.open(cowName);
|
|
|
|
if (!_cowFile.isOpen()) {
|
|
|
|
_cowFile.open("speech.clu");
|
|
|
|
}
|
|
|
|
debug(1, "Using uncompressed Speech Cluster");
|
|
|
|
_cowMode = CowWave;
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2009-02-28 10:46:33 +00:00
|
|
|
if (SwordEngine::isPsx()) {
|
|
|
|
// There's only one file on the PSX, so set it to the current disc.
|
|
|
|
_currentCowFile = SwordEngine::_systemVars.currentCD;
|
|
|
|
if (!_cowFile.isOpen()) {
|
|
|
|
if (!_cowFile.open("speech.dat"))
|
|
|
|
error ("Could not open speech.dat");
|
|
|
|
_cowMode = CowPSX;
|
|
|
|
}
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2004-07-27 03:47:56 +00:00
|
|
|
if (!_cowFile.isOpen())
|
2004-01-06 12:28:24 +00:00
|
|
|
_cowFile.open("speech.clu");
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2004-10-12 15:50:00 +00:00
|
|
|
if (!_cowFile.isOpen()) {
|
|
|
|
_cowFile.open("cows.mad");
|
|
|
|
if (_cowFile.isOpen())
|
|
|
|
_cowMode = CowDemo;
|
|
|
|
}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2003-12-16 02:10:15 +00:00
|
|
|
if (_cowFile.isOpen()) {
|
2009-02-28 10:46:33 +00:00
|
|
|
if (SwordEngine::isPsx()) {
|
|
|
|
// Get data from the external table file
|
|
|
|
Common::File tableFile;
|
|
|
|
if (!tableFile.open("speech.tab"))
|
|
|
|
error ("Could not open speech.tab");
|
|
|
|
_cowHeaderSize = tableFile.size();
|
|
|
|
_cowHeader = (uint32 *)malloc(_cowHeaderSize);
|
|
|
|
if (_cowHeaderSize & 3)
|
|
|
|
error("Unexpected cow header size %d", _cowHeaderSize);
|
|
|
|
for (uint32 cnt = 0; cnt < _cowHeaderSize / 4; cnt++)
|
|
|
|
_cowHeader[cnt] = tableFile.readUint32LE();
|
|
|
|
} else {
|
|
|
|
_cowHeaderSize = _cowFile.readUint32LE();
|
|
|
|
_cowHeader = (uint32*)malloc(_cowHeaderSize);
|
|
|
|
if (_cowHeaderSize & 3)
|
|
|
|
error("Unexpected cow header size %d", _cowHeaderSize);
|
|
|
|
for (uint32 cnt = 0; cnt < (_cowHeaderSize / 4) - 1; cnt++)
|
|
|
|
_cowHeader[cnt] = _cowFile.readUint32LE();
|
|
|
|
_currentCowFile = SwordEngine::_systemVars.currentCD;
|
|
|
|
}
|
2003-12-16 02:10:15 +00:00
|
|
|
} else
|
2005-07-30 21:11:48 +00:00
|
|
|
warning("Sound::initCowSystem: Can't open SPEECH%d.CLU", SwordEngine::_systemVars.currentCD);
|
2003-12-20 20:20:53 +00:00
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
void Sound::closeCowSystem() {
|
2007-02-28 14:48:26 +00:00
|
|
|
_cowFile.close();
|
|
|
|
free(_cowHeader);
|
2003-12-20 20:20:53 +00:00
|
|
|
_cowHeader = NULL;
|
2003-12-21 17:16:07 +00:00
|
|
|
_currentCowFile = 0;
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
2003-12-20 20:20:53 +00:00
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
} // End of namespace Sword1
|