2001-10-09 14:30:12 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2002-03-20 17:51:07 +00:00
|
|
|
* Copyright (C) 2001/2002 The ScummVM project
|
2001-10-09 14:30:12 +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
|
|
|
|
* 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.
|
|
|
|
*
|
2001-11-06 20:00:47 +00:00
|
|
|
* $Header$
|
2001-10-09 14:30:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "scumm.h"
|
2002-04-14 18:13:08 +00:00
|
|
|
#include "mididrv.h"
|
|
|
|
#include "imuse.h"
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2002-03-15 02:23:44 +00:00
|
|
|
#ifdef _WIN32_WCE
|
2002-04-11 17:19:16 +00:00
|
|
|
extern void *bsearch(const void *, const void *, size_t,
|
|
|
|
size_t, int (*x) (const void *, const void *));
|
2002-03-15 02:23:44 +00:00
|
|
|
#endif
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::addSoundToQueue(int sound)
|
|
|
|
{
|
|
|
|
if (!(_features & GF_AFTER_V7)) {
|
2002-03-06 00:18:22 +00:00
|
|
|
_vars[VAR_LAST_SOUND] = sound;
|
|
|
|
ensureResourceLoaded(rtSound, sound);
|
|
|
|
addSoundToQueue2(sound);
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
if (_features & GF_AUDIOTRACKS)
|
|
|
|
warning("Requesting audio track: %d", sound);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::addSoundToQueue2(int sound)
|
|
|
|
{
|
2001-10-09 14:30:12 +00:00
|
|
|
if (_soundQue2Pos < 10) {
|
|
|
|
_soundQue2[_soundQue2Pos++] = sound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::processSoundQues()
|
|
|
|
{
|
2001-10-09 14:30:12 +00:00
|
|
|
byte d;
|
2002-04-11 17:19:16 +00:00
|
|
|
int i, j;
|
2001-10-09 14:30:12 +00:00
|
|
|
int num;
|
|
|
|
int16 data[16];
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
|
|
|
processSfxQueues();
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
while (_soundQue2Pos) {
|
|
|
|
d = _soundQue2[--_soundQue2Pos];
|
2001-10-09 14:30:12 +00:00
|
|
|
if (d)
|
|
|
|
playSound(d);
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < _soundQuePos;) {
|
2001-10-09 14:30:12 +00:00
|
|
|
num = _soundQue[i++];
|
2001-11-05 19:21:49 +00:00
|
|
|
if (i + num > _soundQuePos) {
|
|
|
|
warning("processSoundQues: invalid num value");
|
|
|
|
break;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
for (j = 0; j < 16; j++)
|
2001-10-09 14:30:12 +00:00
|
|
|
data[j] = 0;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (num > 0) {
|
|
|
|
for (j = 0; j < num; j++)
|
|
|
|
data[j] = _soundQue[i + j];
|
2001-10-09 14:30:12 +00:00
|
|
|
i += num;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2002-04-14 18:13:08 +00:00
|
|
|
se = _imuse;
|
2001-11-05 19:21:49 +00:00
|
|
|
#if 0
|
2002-04-11 17:19:16 +00:00
|
|
|
debug(1, "processSoundQues(%d,%d,%d,%d,%d,%d,%d,%d,%d)",
|
|
|
|
data[0] >> 8,
|
|
|
|
data[0] & 0xFF,
|
|
|
|
data[1], data[2], data[3], data[4], data[5], data[6], data[7]
|
2001-11-05 19:21:49 +00:00
|
|
|
);
|
|
|
|
#endif
|
2002-04-11 17:19:16 +00:00
|
|
|
if (!(_features & GF_AFTER_V7)) {
|
|
|
|
if (se)
|
|
|
|
_vars[VAR_SOUNDRESULT] =
|
|
|
|
(short)se->do_command(data[0], data[1], data[2], data[3], data[4],
|
|
|
|
data[5], data[6], data[7]);
|
2002-03-06 00:18:22 +00:00
|
|
|
}
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_soundQuePos = 0;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::playSound(int sound)
|
|
|
|
{
|
2002-03-16 05:33:25 +00:00
|
|
|
byte *ptr;
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se = _imuse;
|
2002-03-16 05:33:25 +00:00
|
|
|
|
|
|
|
ptr = getResourceAddress(rtSound, sound);
|
|
|
|
if (ptr != NULL && READ_UINT32_UNALIGNED(ptr) == MKID('SOUN')) {
|
|
|
|
ptr += 8;
|
2002-04-16 12:18:50 +00:00
|
|
|
_system->play_cdrom(ptr[16], ptr[17] == 0xff ? -1 : ptr[17],
|
2002-04-11 17:19:16 +00:00
|
|
|
(ptr[18] * 60 + ptr[19]) * 75 + ptr[20], 0);
|
2002-03-16 05:33:25 +00:00
|
|
|
current_cd_sound = sound;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (_features & GF_OLD256)
|
|
|
|
return; /* FIXME */
|
2002-02-12 21:28:07 +00:00
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
if (se) {
|
|
|
|
getResourceAddress(rtSound, sound);
|
|
|
|
se->start_sound(sound);
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::processSfxQueues()
|
|
|
|
{
|
2001-11-05 19:21:49 +00:00
|
|
|
Actor *a;
|
|
|
|
int act;
|
2002-04-11 17:19:16 +00:00
|
|
|
bool b, finished;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
|
|
|
if (_talk_sound_mode != 0) {
|
|
|
|
startTalkSound(_talk_sound_a, _talk_sound_b, _talk_sound_mode);
|
|
|
|
_talk_sound_mode = 0;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
if (_sfxMode == 2) {
|
2001-11-05 19:21:49 +00:00
|
|
|
act = _vars[VAR_TALK_ACTOR];
|
|
|
|
finished = isSfxFinished();
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (act != 0 && (uint) act < 0x80 && !string[0].no_talk_anim) {
|
2001-11-05 19:21:49 +00:00
|
|
|
a = derefActorSafe(act, "processSfxQueues");
|
2002-04-11 17:19:16 +00:00
|
|
|
if (a->room == _currentRoom && (finished || !_endOfMouthSync)) {
|
2001-11-05 19:21:49 +00:00
|
|
|
b = true;
|
|
|
|
if (!finished)
|
|
|
|
b = isMouthSyncOff(_curSoundPos);
|
|
|
|
if (_mouthSyncMode != b) {
|
|
|
|
_mouthSyncMode = b;
|
2001-11-26 19:57:57 +00:00
|
|
|
startAnimActor(a, b ? a->talkFrame2 : a->talkFrame1);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
if (finished && _talkDelay == 0) {
|
2001-11-05 19:21:49 +00:00
|
|
|
stopTalk();
|
|
|
|
_sfxMode = 0;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
} else if (_sfxMode == 1) {
|
2001-11-05 19:21:49 +00:00
|
|
|
if (isSfxFinished()) {
|
|
|
|
_sfxMode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-06 23:17:16 +00:00
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
2002-04-11 17:19:16 +00:00
|
|
|
static int compar(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return ((MP3OffsetTable *) a)->org_offset -
|
|
|
|
((MP3OffsetTable *) b)->org_offset;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::startTalkSound(uint32 offset, uint32 b, int mode)
|
|
|
|
{
|
2002-03-06 23:17:16 +00:00
|
|
|
int num = 0, i;
|
2002-04-11 17:19:16 +00:00
|
|
|
byte file_byte, file_byte_2;
|
2002-03-06 23:17:16 +00:00
|
|
|
int size;
|
2001-11-05 19:21:49 +00:00
|
|
|
|
|
|
|
if (!_sfxFile) {
|
|
|
|
warning("startTalkSound: SFX file is not open");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (b > 8) {
|
|
|
|
num = (b - 8) >> 1;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
if (offset_table != NULL) {
|
2002-04-11 17:19:16 +00:00
|
|
|
MP3OffsetTable *result = NULL, key;
|
|
|
|
|
|
|
|
key.org_offset = offset;
|
2002-04-15 20:22:47 +00:00
|
|
|
result = (MP3OffsetTable *) bsearch(&key, offset_table, num_sound_effects,
|
|
|
|
sizeof(MP3OffsetTable), compar);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
warning("startTalkSound: did not find sound at offset %d !", offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (2 * num != result->num_tags) {
|
|
|
|
warning("startTalkSound: number of tags do not match (%d - %d) !", b,
|
|
|
|
result->num_tags);
|
|
|
|
num = result->num_tags;
|
|
|
|
}
|
|
|
|
offset = result->new_offset;
|
|
|
|
size = result->compressed_size;
|
2002-03-06 23:17:16 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2002-04-11 17:19:16 +00:00
|
|
|
offset += 8;
|
|
|
|
size = -1;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
fileSeek((FILE *) _sfxFile, offset, SEEK_SET);
|
2002-03-06 23:17:16 +00:00
|
|
|
i = 0;
|
|
|
|
while (num > 0) {
|
2002-04-11 17:19:16 +00:00
|
|
|
fileRead((FILE *) _sfxFile, &file_byte, sizeof(file_byte));
|
|
|
|
fileRead((FILE *) _sfxFile, &file_byte_2, sizeof(file_byte_2));
|
|
|
|
_mouthSyncTimes[i++] = file_byte | (file_byte_2 << 8);
|
|
|
|
num--;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
_mouthSyncTimes[i] = 0xFFFF;
|
|
|
|
_sfxMode = mode;
|
|
|
|
_curSoundPos = 0;
|
|
|
|
_mouthSyncMode = true;
|
|
|
|
|
2002-03-06 23:17:16 +00:00
|
|
|
startSfxSound(_sfxFile, size);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::stopTalkSound()
|
|
|
|
{
|
|
|
|
if (_sfxMode == 2) {
|
2001-11-14 18:40:39 +00:00
|
|
|
stopSfxSound();
|
|
|
|
_sfxMode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
bool Scumm::isMouthSyncOff(uint pos)
|
|
|
|
{
|
2001-11-05 19:21:49 +00:00
|
|
|
uint j;
|
|
|
|
bool val = true;
|
|
|
|
uint16 *ms = _mouthSyncTimes;
|
|
|
|
|
|
|
|
_endOfMouthSync = false;
|
|
|
|
do {
|
|
|
|
val ^= 1;
|
|
|
|
j = *ms++;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (j == 0xFFFF) {
|
2001-11-05 19:21:49 +00:00
|
|
|
_endOfMouthSync = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (pos > j);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
int Scumm::isSoundRunning(int sound)
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se;
|
2001-11-05 19:21:49 +00:00
|
|
|
int i;
|
|
|
|
|
2002-03-16 05:33:25 +00:00
|
|
|
if (sound == current_cd_sound)
|
2002-04-16 12:18:50 +00:00
|
|
|
return _system->poll_cdrom();
|
2002-03-16 05:33:25 +00:00
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
i = _soundQue2Pos;
|
|
|
|
while (i--) {
|
|
|
|
if (_soundQue2[i] == sound)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSoundInQueue(sound))
|
|
|
|
return 1;
|
|
|
|
|
2001-11-12 20:50:36 +00:00
|
|
|
if (!isResourceLoaded(rtSound, sound))
|
2001-11-05 19:21:49 +00:00
|
|
|
return 0;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-04-14 18:13:08 +00:00
|
|
|
se = _imuse;
|
2001-11-05 19:21:49 +00:00
|
|
|
if (!se)
|
|
|
|
return 0;
|
|
|
|
return se->get_sound_status(sound);
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
bool Scumm::isSoundInQueue(int sound)
|
|
|
|
{
|
|
|
|
int i = 0, j, num;
|
2001-11-05 19:21:49 +00:00
|
|
|
int16 table[16];
|
|
|
|
|
|
|
|
while (i < _soundQuePos) {
|
|
|
|
num = _soundQue[i++];
|
|
|
|
|
|
|
|
memset(table, 0, sizeof(table));
|
|
|
|
|
|
|
|
if (num > 0) {
|
2002-04-11 17:19:16 +00:00
|
|
|
for (j = 0; j < num; j++)
|
|
|
|
table[j] = _soundQue[i + j];
|
2001-11-05 19:21:49 +00:00
|
|
|
i += num;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (table[0] == 0x10F && table[1] == 8 && table[2] == sound)
|
2001-11-05 19:21:49 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::stopSound(int a)
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se;
|
2001-11-05 19:21:49 +00:00
|
|
|
int i;
|
|
|
|
|
2002-03-16 05:33:25 +00:00
|
|
|
if (a == current_cd_sound) {
|
|
|
|
current_cd_sound = 0;
|
2002-04-16 12:18:50 +00:00
|
|
|
_system->stop_cdrom();
|
2002-03-16 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2002-04-14 18:13:08 +00:00
|
|
|
se = _imuse;
|
2001-11-05 19:21:49 +00:00
|
|
|
if (se)
|
|
|
|
se->stop_sound(a);
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < 10; i++)
|
2001-11-05 19:21:49 +00:00
|
|
|
if (_soundQue2[i] == (byte)a)
|
|
|
|
_soundQue2[i] = 0;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::stopAllSounds()
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se = _imuse;
|
2002-03-16 05:33:25 +00:00
|
|
|
|
|
|
|
if (current_cd_sound != 0) {
|
|
|
|
current_cd_sound = 0;
|
2002-04-16 12:18:50 +00:00
|
|
|
_system->stop_cdrom();
|
2002-03-16 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
if (se) {
|
|
|
|
se->stop_all_sounds();
|
|
|
|
se->clear_queue();
|
|
|
|
}
|
|
|
|
clearSoundQue();
|
2001-11-14 18:40:39 +00:00
|
|
|
stopSfxSound();
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::clearSoundQue()
|
|
|
|
{
|
2001-11-05 19:21:49 +00:00
|
|
|
_soundQue2Pos = 0;
|
|
|
|
memset(_soundQue2, 0, sizeof(_soundQue2));
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-10-16 10:01:48 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::soundKludge(int16 * list)
|
|
|
|
{
|
2001-10-16 10:01:48 +00:00
|
|
|
int16 *ptr;
|
|
|
|
int i;
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (list[0] == -1) {
|
2001-11-05 19:21:49 +00:00
|
|
|
processSoundQues();
|
2001-10-16 10:01:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_soundQue[_soundQuePos++] = 8;
|
|
|
|
|
|
|
|
ptr = _soundQue + _soundQuePos;
|
|
|
|
_soundQuePos += 8;
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < 8; i++)
|
2001-10-16 10:01:48 +00:00
|
|
|
*ptr++ = list[i];
|
|
|
|
if (_soundQuePos > 0x100)
|
|
|
|
error("Sound que buffer overflow");
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::talkSound(uint32 a, uint32 b, int mode)
|
|
|
|
{
|
2001-11-05 19:21:49 +00:00
|
|
|
_talk_sound_a = a;
|
|
|
|
_talk_sound_b = b;
|
|
|
|
_talk_sound_mode = mode;
|
|
|
|
}
|
|
|
|
|
2001-11-14 18:40:39 +00:00
|
|
|
/* The sound code currently only supports General Midi.
|
|
|
|
* General Midi is used in Day Of The Tentacle.
|
|
|
|
* Roland music is also playable, but doesn't sound well.
|
|
|
|
* A mapping between roland instruments and GM instruments
|
|
|
|
* is needed.
|
|
|
|
*/
|
2002-03-06 23:17:16 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::setupSound()
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se = _imuse;
|
2002-03-07 08:11:48 +00:00
|
|
|
if (se) {
|
2001-12-01 17:06:13 +00:00
|
|
|
se->setBase(res.address[rtSound]);
|
2002-04-11 17:19:16 +00:00
|
|
|
if (se->get_music_volume() == 0)
|
|
|
|
se->set_music_volume(60);
|
2002-03-07 08:25:59 +00:00
|
|
|
se->set_master_volume(125);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
_sound_volume_music = se->get_music_volume();
|
2002-03-14 17:36:13 +00:00
|
|
|
_sound_volume_master = (se->get_master_volume() / 127);
|
2002-03-07 08:11:48 +00:00
|
|
|
}
|
2001-11-06 20:25:08 +00:00
|
|
|
_sfxFile = openSfxFile();
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::pauseSounds(bool pause)
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se = _imuse;
|
2001-11-14 18:40:39 +00:00
|
|
|
if (se)
|
|
|
|
se->pause(pause);
|
|
|
|
_soundsPaused = pause;
|
|
|
|
}
|
|
|
|
|
2002-01-02 11:50:28 +00:00
|
|
|
enum {
|
|
|
|
SOUND_HEADER_SIZE = 26,
|
2002-04-11 17:19:16 +00:00
|
|
|
SOUND_HEADER_BIG_SIZE = 26 + 8,
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2002-01-02 11:50:28 +00:00
|
|
|
};
|
2001-11-14 18:40:39 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::startSfxSound(void *file, int file_size)
|
|
|
|
{
|
2002-01-02 11:50:28 +00:00
|
|
|
char ident[8];
|
2001-11-05 19:21:49 +00:00
|
|
|
int block_type;
|
|
|
|
byte work[8];
|
2002-04-11 17:19:16 +00:00
|
|
|
uint size = 0, i;
|
|
|
|
int rate, comp;
|
2001-11-05 19:21:49 +00:00
|
|
|
byte *data;
|
|
|
|
|
2002-03-06 23:17:16 +00:00
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
if (file_size > 0) {
|
2002-04-11 17:19:16 +00:00
|
|
|
data = (byte *)calloc(file_size + MAD_BUFFER_GUARD, 1);
|
2002-03-06 23:17:16 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (fread(data, file_size, 1, (FILE *) file) != 1) {
|
|
|
|
/* no need to free the memory since error will shut down */
|
|
|
|
error("startSfxSound: cannot read %d bytes", size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
playSfxSound_MP3(data, file_size);
|
|
|
|
return;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-04-11 17:19:16 +00:00
|
|
|
if (fread(ident, 8, 1, (FILE *) file) != 1)
|
2002-01-02 11:50:28 +00:00
|
|
|
goto invalid;
|
2001-12-27 17:51:58 +00:00
|
|
|
|
2002-01-02 11:50:28 +00:00
|
|
|
if (!memcmp(ident, "VTLK", 4)) {
|
2002-04-11 17:19:16 +00:00
|
|
|
fseek((FILE *) file, SOUND_HEADER_BIG_SIZE - 8, SEEK_CUR);
|
2002-01-02 11:50:28 +00:00
|
|
|
} else if (!memcmp(ident, "Creative", 8)) {
|
2002-04-11 17:19:16 +00:00
|
|
|
fseek((FILE *) file, SOUND_HEADER_SIZE - 8, SEEK_CUR);
|
2002-01-02 11:50:28 +00:00
|
|
|
} else {
|
2002-04-11 17:19:16 +00:00
|
|
|
invalid:;
|
2001-11-05 19:21:49 +00:00
|
|
|
warning("startSfxSound: invalid header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
block_type = getc((FILE *) file);
|
2001-11-05 19:21:49 +00:00
|
|
|
if (block_type != 1) {
|
|
|
|
warning("startSfxSound: Expecting block_type == 1, got %d", block_type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
fread(work, 3, 1, (FILE *) file);
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
size = (work[0] | (work[1] << 8) | (work[2] << 16)) - 2;
|
|
|
|
rate = getc((FILE *) file);
|
|
|
|
comp = getc((FILE *) file);
|
2001-11-05 19:21:49 +00:00
|
|
|
|
|
|
|
if (comp != 0) {
|
|
|
|
warning("startSfxSound: Unsupported compression type %d", comp);
|
|
|
|
return;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
data = (byte *)malloc(size);
|
|
|
|
if (data == NULL) {
|
2001-11-05 19:21:49 +00:00
|
|
|
error("startSfxSound: out of memory");
|
|
|
|
return;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
if (fread(data, size, 1, (FILE *) file) != 1) {
|
2001-11-05 19:21:49 +00:00
|
|
|
/* no need to free the memory since error will shut down */
|
|
|
|
error("startSfxSound: cannot read %d bytes", size);
|
|
|
|
return;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < size; i++) {
|
2002-03-14 16:49:59 +00:00
|
|
|
// Fixme: From WinCE port
|
|
|
|
if (_sound_volume_sfx != 100)
|
2002-04-11 17:19:16 +00:00
|
|
|
data[i] = _sound_volume_sfx * data[i] / 100;
|
2002-03-14 16:49:59 +00:00
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
data[i] ^= 0x80;
|
2002-03-14 16:49:59 +00:00
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
playSfxSound(data, size, 1000000 / (256 - rate));
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-03-06 23:17:16 +00:00
|
|
|
|
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
2002-04-11 17:19:16 +00:00
|
|
|
static int get_int(FILE * f)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
for (int size = 0; size < 4; size++) {
|
|
|
|
int c = fgetc(f);
|
|
|
|
if (c == EOF) {
|
|
|
|
error("Unexpected end of file !!!");
|
|
|
|
}
|
|
|
|
ret <<= 8;
|
|
|
|
ret |= c;
|
|
|
|
}
|
|
|
|
return ret;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void *Scumm::openSfxFile()
|
|
|
|
{
|
2001-11-29 22:29:38 +00:00
|
|
|
char buf[50];
|
2002-04-08 13:55:03 +00:00
|
|
|
FILE *file = NULL;
|
2001-11-29 22:29:38 +00:00
|
|
|
|
2002-04-08 13:55:03 +00:00
|
|
|
if (_gameId == GID_DIG) {
|
|
|
|
sprintf(buf, "%s%svoice.bun", _gameDataPath, _exe_name);
|
|
|
|
file = fopen(buf, "rb");
|
|
|
|
if (!file)
|
|
|
|
warning("Unable to open DIG voice bundle: %s", buf);
|
|
|
|
return file;
|
|
|
|
}
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2001-11-29 22:29:38 +00:00
|
|
|
/* Try opening the file <_exe_name>.sou first, eg tentacle.sou.
|
|
|
|
* That way, you can keep .sou files for multiple games in the
|
|
|
|
* same directory */
|
2002-03-06 23:17:16 +00:00
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
offset_table = NULL;
|
2001-11-29 22:29:38 +00:00
|
|
|
|
2002-03-06 23:17:16 +00:00
|
|
|
sprintf(buf, "%s%s.so3", _gameDataPath, _exe_name);
|
|
|
|
file = fopen(buf, "rb");
|
|
|
|
if (!file) {
|
2002-04-11 17:19:16 +00:00
|
|
|
sprintf(buf, "%smonster.so3", _gameDataPath);
|
|
|
|
file = fopen(buf, "rb");
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
if (file != NULL) {
|
2002-04-11 17:19:16 +00:00
|
|
|
/* Now load the 'offset' index in memory to be able to find the MP3 data
|
|
|
|
|
|
|
|
The format of the .SO3 file is easy :
|
|
|
|
- number of bytes of the 'index' part
|
|
|
|
- N times the following fields (4 bytes each) :
|
|
|
|
+ offset in the original sound file
|
|
|
|
+ offset of the MP3 data in the .SO3 file WITHOUT taking into account
|
|
|
|
the index field and the 'size' field
|
|
|
|
+ the number of 'tags'
|
|
|
|
+ the size of the MP3 data
|
|
|
|
- and then N times :
|
|
|
|
+ the tags
|
|
|
|
+ the MP3 data
|
|
|
|
*/
|
|
|
|
int size, compressed_offset;
|
|
|
|
MP3OffsetTable *cur;
|
|
|
|
|
|
|
|
compressed_offset = get_int(file);
|
|
|
|
offset_table = (MP3OffsetTable *) malloc(compressed_offset);
|
|
|
|
num_sound_effects = compressed_offset / 16;
|
|
|
|
|
|
|
|
size = compressed_offset;
|
|
|
|
cur = offset_table;
|
|
|
|
while (size > 0) {
|
|
|
|
cur[0].org_offset = get_int(file);
|
2002-04-15 20:22:47 +00:00
|
|
|
cur[0].new_offset = get_int(file) + compressed_offset + 4; /* The + 4 is to take into accound the 'size' field */
|
2002-04-11 17:19:16 +00:00
|
|
|
cur[0].num_tags = get_int(file);
|
|
|
|
cur[0].compressed_size = get_int(file);
|
|
|
|
size -= 4 * 4;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
return file;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-02-27 22:48:55 +00:00
|
|
|
sprintf(buf, "%s%s.sou", _gameDataPath, _exe_name);
|
2001-11-29 22:29:38 +00:00
|
|
|
file = fopen(buf, "rb");
|
2002-02-27 22:48:55 +00:00
|
|
|
if (!file) {
|
2002-04-11 17:19:16 +00:00
|
|
|
sprintf(buf, "%smonster.sou", _gameDataPath);
|
|
|
|
file = fopen(buf, "rb");
|
2002-02-27 22:48:55 +00:00
|
|
|
}
|
2001-11-29 22:29:38 +00:00
|
|
|
return file;
|
2001-11-14 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::stopSfxSound()
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
_mixer->stop_all();
|
2001-11-14 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
bool Scumm::isSfxFinished()
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
return !_mixer->has_active_channel();
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::playBundleSound(char *sound)
|
|
|
|
{
|
2002-04-08 13:55:03 +00:00
|
|
|
warning("playBundleSound: %s", sound);
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
void Scumm::playSfxSound(void *sound, uint32 size, uint rate)
|
|
|
|
{
|
2002-04-14 18:13:08 +00:00
|
|
|
_mixer->play_raw(NULL, sound, size, rate, SoundMixer::FLAG_AUTOFREE);
|
2001-11-14 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
2002-04-15 20:22:47 +00:00
|
|
|
void Scumm::playSfxSound_MP3(void *sound, uint32 size)
|
|
|
|
{
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2002-04-16 18:33:04 +00:00
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2002-04-16 18:33:04 +00:00
|
|
|
_mixer->play_mp3(NULL, sound, size, SoundMixer::FLAG_AUTOFREE);
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2002-04-16 18:33:04 +00:00
|
|
|
#endif
|
2002-04-15 20:22:47 +00:00
|
|
|
}
|
2002-04-17 20:23:45 +00:00
|
|
|
|
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
|
|
|
|
int Scumm::getCachedTrack(int track) {
|
|
|
|
int i;
|
|
|
|
char track_name[1024];
|
|
|
|
FILE* file;
|
|
|
|
int current_index;
|
|
|
|
struct mad_stream stream;
|
|
|
|
struct mad_frame frame;
|
|
|
|
unsigned char buffer[8192];
|
|
|
|
unsigned int buflen = 0;
|
|
|
|
int count = 0, result = 0;
|
|
|
|
|
|
|
|
// See if we find the track in the cache
|
|
|
|
for (i=0; i<CACHE_TRACKS; i++)
|
|
|
|
if (_cached_tracks[i] == track) {
|
|
|
|
if (_mp3_tracks[i])
|
|
|
|
return i;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
current_index = _current_cache++;
|
|
|
|
_current_cache %= CACHE_TRACKS;
|
|
|
|
// Not found, see if it exists
|
|
|
|
sprintf(track_name, "%strack%d.mp3", _gameDataPath, track);
|
|
|
|
file = fopen(track_name, "rb");
|
|
|
|
_cached_tracks[current_index] = track;
|
|
|
|
_mp3_tracks[current_index] = NULL;
|
|
|
|
if (!file) {
|
|
|
|
warning("Track %d not available in mp3 format", track);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Check the format and bitrate
|
|
|
|
mad_stream_init(&stream);
|
|
|
|
mad_frame_init(&frame);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (buflen < sizeof(buffer)) {
|
|
|
|
uint16 bytes;
|
|
|
|
|
|
|
|
bytes = fread(buffer + buflen, 1, sizeof(buffer) - buflen, file);
|
|
|
|
if (bytes <= 0) {
|
|
|
|
if (bytes == -1) {
|
|
|
|
warning("Invalid format for track %d", track);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buflen += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
mad_stream_buffer(&stream, buffer, buflen);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (mad_frame_decode(&frame, &stream) == -1) {
|
|
|
|
if (!MAD_RECOVERABLE(stream.error))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (stream.error != MAD_ERROR_BADCRC)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count++)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count || stream.error != MAD_ERROR_BUFLEN)
|
|
|
|
break;
|
|
|
|
|
|
|
|
memmove(buffer, stream.next_frame,
|
|
|
|
buflen = &buffer[buflen] - stream.next_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
memcpy(&_mad_header[current_index], &frame.header, sizeof(mad_header));
|
|
|
|
else {
|
|
|
|
warning("Invalid format for track %d", track);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mad_frame_finish(&frame);
|
|
|
|
mad_stream_finish(&stream);
|
|
|
|
// Get file size
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
_mp3_size[current_index] = ftell(file);
|
|
|
|
_mp3_tracks[current_index] = file;
|
|
|
|
if (!_mp3_buffer)
|
|
|
|
_mp3_buffer = malloc(MP3_BUFFER_SIZE);
|
|
|
|
|
|
|
|
return current_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::playMP3CDTrack(int track, int num_loops, int start, int delay) {
|
|
|
|
int index;
|
|
|
|
long offset;
|
|
|
|
float frame_size;
|
|
|
|
mad_timer_t duration;
|
|
|
|
|
|
|
|
if (!start && !delay) {
|
|
|
|
_mixer->stop(_mp3_handle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = getCachedTrack(track);
|
|
|
|
if (index < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Calc offset
|
|
|
|
frame_size = 144 * _mad_header[index].bitrate / _mad_header[index].samplerate;
|
|
|
|
offset = (long)((float)start / (float)75 * 1000 /
|
|
|
|
(float)((float)1152 / (float)_mad_header[index].samplerate *
|
|
|
|
1000) * (float)(frame_size + 0.5));
|
|
|
|
// Calc delay
|
|
|
|
mad_timer_set(&duration, 0, delay, 75);
|
|
|
|
// Go
|
|
|
|
fseek(_mp3_tracks[index], offset, SEEK_SET);
|
|
|
|
|
|
|
|
_mixer->play_mp3_cdtrack(&_mp3_handle, _mp3_tracks[index], _mp3_buffer, MP3_BUFFER_SIZE, duration);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|