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.
|
2002-08-08 21:54:49 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
* 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"
|
2002-09-13 12:20:55 +00:00
|
|
|
#include "scumm.h"
|
2002-08-29 23:45:15 +00:00
|
|
|
#include "sound.h"
|
2002-04-19 11:12:27 +00:00
|
|
|
#include "sound/mididrv.h"
|
2002-09-13 12:20:55 +00:00
|
|
|
#include "imuse.h"
|
|
|
|
#include "actor.h"
|
|
|
|
#include "bundle.h"
|
|
|
|
#include "common/config-file.h"
|
|
|
|
#include "common/util.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-08-14 20:43:56 +00:00
|
|
|
Sound::Sound(Scumm *parent) {
|
|
|
|
_scumm = parent;
|
|
|
|
_numberBundleMusic = -1;
|
|
|
|
_musicBundleBufFinal = NULL;
|
|
|
|
_musicBundleBufOutput = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sound::~Sound() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::addSoundToQueue(int sound) {
|
|
|
|
if (!(_scumm->_features & GF_AFTER_V7)) {
|
|
|
|
_scumm->_vars[_scumm->VAR_LAST_SOUND] = sound;
|
|
|
|
_scumm->ensureResourceLoaded(rtSound, sound);
|
2002-03-06 00:18:22 +00:00
|
|
|
addSoundToQueue2(sound);
|
2002-08-20 09:58:35 +00:00
|
|
|
} else {
|
|
|
|
// WARNING ! This may break something, maybe this sould be put inside if(_gameID == GID_FT) ?
|
|
|
|
// But why addSoundToQueue should not queue sound ?
|
|
|
|
_scumm->ensureResourceLoaded(rtSound, sound);
|
|
|
|
addSoundToQueue2(sound);
|
2002-03-06 00:18:22 +00:00
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 14:15:27 +00:00
|
|
|
// if (_features & GF_AUDIOTRACKS)
|
|
|
|
// warning("Requesting audio track: %d", sound);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::addSoundToQueue2(int sound) {
|
2001-10-09 14:30:12 +00:00
|
|
|
if (_soundQue2Pos < 10) {
|
|
|
|
_soundQue2[_soundQue2Pos++] = sound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::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-08-14 20:43:56 +00:00
|
|
|
se = _scumm->_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-08-04 16:30:59 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if ((_scumm->_gameId == GID_DIG) && (data[0] == 4096)){
|
2002-09-15 05:38:04 +00:00
|
|
|
// playBundleMusic(data[1] - 1);
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
2002-08-25 15:06:53 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (!(_scumm->_features & GF_AFTER_V7)) {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (se)
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->_vars[_scumm->VAR_SOUNDRESULT] =
|
2002-04-11 17:19:16 +00:00
|
|
|
(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-08-20 09:58:35 +00:00
|
|
|
static char * read_creative_voc_file(byte * ptr, int & size, int & rate) {
|
|
|
|
assert(strncmp((char*)ptr, "Creative Voice File\x1A", 20) == 0);
|
|
|
|
int offset = READ_LE_UINT16(ptr+20);
|
|
|
|
short version = READ_LE_UINT16(ptr+22);
|
|
|
|
short code = READ_LE_UINT16(ptr+24);
|
|
|
|
assert(version == 0x010A || version == 0x0114);
|
|
|
|
assert(code == ~version + 0x1234);
|
|
|
|
bool quit = 0;
|
|
|
|
char * ret_sound = 0; size = 0;
|
|
|
|
while(!quit) {
|
|
|
|
int len = READ_LE_UINT32(ptr + offset);
|
|
|
|
offset += 4;
|
|
|
|
int code = len & 0xFF; // FIXME not sure this is endian correct
|
|
|
|
len >>= 8;
|
|
|
|
switch(code) {
|
|
|
|
case 0: quit = 1; break;
|
|
|
|
case 1: {
|
|
|
|
int time_constant = ptr[offset++];
|
|
|
|
int packing = ptr[offset++];
|
|
|
|
len -= 2;
|
|
|
|
rate = 1000000L / (256L - time_constant);
|
|
|
|
debug(9, "VOC Data Bloc : %d, %d, %d", rate, packing, len);
|
|
|
|
if(packing == 0) {
|
|
|
|
if(size) {
|
|
|
|
ret_sound = (char*)realloc(ret_sound, size + len);
|
|
|
|
} else {
|
|
|
|
ret_sound = (char*)malloc(len);
|
|
|
|
}
|
|
|
|
memcpy(ret_sound + size, ptr + offset, len);
|
|
|
|
size += len;
|
|
|
|
} else {
|
|
|
|
warning("VOC file packing %d unsupported", packing);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 6: // begin of loop
|
|
|
|
debug(3, "loops in Creative files not supported");
|
|
|
|
break;
|
|
|
|
case 7: // end of loop
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warning("Invalid code in VOC file : %d", code);
|
|
|
|
//~ quit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
debug(9, "VOC Data Size : %d", size);
|
|
|
|
return ret_sound;
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::playSound(int sound) {
|
2002-03-16 05:33:25 +00:00
|
|
|
byte *ptr;
|
2002-09-09 11:15:44 +00:00
|
|
|
int size;
|
|
|
|
int rate;
|
2002-08-20 09:58:35 +00:00
|
|
|
|
2002-09-21 13:48:03 +00:00
|
|
|
debug(3,"playSound #%d (room %d)",
|
|
|
|
sound, _scumm->getResourceRoomNr(rtSound, sound));
|
2002-08-14 20:43:56 +00:00
|
|
|
ptr = _scumm->getResourceAddress(rtSound, sound);
|
2002-08-29 23:45:15 +00:00
|
|
|
if (ptr) {
|
|
|
|
if (READ_UINT32_UNALIGNED(ptr) == MKID('SOUN')) {
|
|
|
|
ptr += 8;
|
|
|
|
_scumm->_vars[_scumm->VAR_MI1_TIMER] = 0;
|
|
|
|
playCDTrack(ptr[16], ptr[17] == 0xff ? -1 : ptr[17],
|
|
|
|
(ptr[18] * 60 + ptr[19]) * 75 + ptr[20], 0);
|
|
|
|
|
|
|
|
_scumm->current_cd_sound = sound;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Support for SFX in Monkey Island 1, Mac version
|
|
|
|
// This is rather hackish right now, but works OK. SFX are not sounding
|
|
|
|
// 100% correct, though, not sure right now what is causing this.
|
|
|
|
else if (READ_UINT32_UNALIGNED(ptr) == MKID('Mac1')) {
|
|
|
|
|
|
|
|
// Read info from the header
|
2002-09-09 11:15:44 +00:00
|
|
|
size = READ_UINT32_UNALIGNED(ptr+0x60);
|
|
|
|
rate = READ_UINT32_UNALIGNED(ptr+0x64) >> 16;
|
2002-08-29 23:45:15 +00:00
|
|
|
|
|
|
|
// Skip over the header (fixed size)
|
|
|
|
ptr += 0x72;
|
|
|
|
|
|
|
|
// Allocate a sound buffer, copy the data into it, and play
|
|
|
|
char *sound = (char*)malloc(size);
|
|
|
|
memcpy(sound, ptr, size);
|
2002-08-24 15:31:37 +00:00
|
|
|
_scumm->_mixer->playRaw(NULL, sound, size, rate, SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE);
|
2002-08-29 23:45:15 +00:00
|
|
|
return;
|
2002-08-20 09:58:35 +00:00
|
|
|
}
|
2002-08-29 23:45:15 +00:00
|
|
|
// Support for Putt-Putt sounds - very hackish, too 8-)
|
|
|
|
else if (READ_UINT32_UNALIGNED(ptr) == MKID('DIGI')) {
|
|
|
|
// TODO - discover what data the first chunk, HSHD, contains
|
|
|
|
// it might be useful here.
|
|
|
|
ptr += 8 + READ_BE_UINT32_UNALIGNED(ptr+12);
|
|
|
|
if (READ_UINT32_UNALIGNED(ptr) != MKID('SDAT'))
|
|
|
|
return; // abort
|
|
|
|
|
2002-09-09 11:15:44 +00:00
|
|
|
size = READ_BE_UINT32_UNALIGNED(ptr+4);
|
|
|
|
rate = 8000; // FIXME - what value here ?!? 8000 is just a guess
|
2002-08-29 23:45:15 +00:00
|
|
|
|
|
|
|
// Allocate a sound buffer, copy the data into it, and play
|
|
|
|
char *sound = (char*)malloc(size);
|
|
|
|
memcpy(sound, ptr + 8, size);
|
|
|
|
_scumm->_mixer->playRaw(NULL, sound, size, rate, SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE);
|
|
|
|
return;
|
2002-07-25 16:29:07 +00:00
|
|
|
}
|
2002-08-29 23:45:15 +00:00
|
|
|
else if (READ_UINT32_UNALIGNED(ptr) == MKID('Crea')) {
|
|
|
|
char * sound = read_creative_voc_file(ptr, size, rate);
|
|
|
|
if(sound != NULL) {
|
|
|
|
_scumm->_mixer->playRaw(NULL, sound, size, rate, SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE);
|
|
|
|
}
|
2002-07-25 15:37:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-09-21 13:48:03 +00:00
|
|
|
else if (READ_UINT32_UNALIGNED(ptr) == MKID('ADL ')) {
|
|
|
|
// played as MIDI, just to make perhaps the later use
|
|
|
|
// of WA possible (see "else if" with GF_OLD256 below)
|
|
|
|
}
|
2002-08-29 23:45:15 +00:00
|
|
|
// Support for sampled sound effects in Monkey1 and Monkey2
|
|
|
|
else if (READ_UINT32_UNALIGNED(ptr) == MKID('SBL ')) {
|
|
|
|
debug(2, "Using SBL sound effect");
|
|
|
|
|
|
|
|
// TODO - Figuring out how the SBL chunk works. Here's an
|
|
|
|
// example:
|
|
|
|
//
|
|
|
|
// 53 42 4c 20 00 00 11 ae |SBL ....|
|
|
|
|
// 41 55 68 64 00 00 00 03 |AUhd....|
|
|
|
|
// 00 00 80 41 55 64 74 00 |...AUdt.|
|
|
|
|
// 00 11 9b 01 96 11 00 a6 |........|
|
|
|
|
// 00 7f 7f 7e 7e 7e 7e 7e |...~~~~~|
|
|
|
|
// 7e 7f 7f 80 80 7f 7f 7f |~.......|
|
|
|
|
// 7f 80 80 7f 7e 7d 7d 7e |....~}}~|
|
|
|
|
// 7e 7e 7e 7e 7e 7e 7e 7f |~~~~~~~.|
|
|
|
|
// 7f 7f 7f 80 80 80 80 80 |........|
|
|
|
|
// 80 81 80 80 7f 7f 80 85 |........|
|
|
|
|
// 8b 8b 83 78 72 6d 6f 75 |...xrmou|
|
|
|
|
// 7a 78 77 7d 83 84 83 81 |zxw}....|
|
|
|
|
//
|
|
|
|
// The length of the AUhd chunk always seems to be 3 bytes.
|
|
|
|
// Let's skip that for now.
|
|
|
|
//
|
|
|
|
// The starting offset, length and sample rate is all pure
|
|
|
|
// guesswork. The result sounds reasonable to me, but I've
|
|
|
|
// never heard the original.
|
|
|
|
|
2002-09-09 11:15:44 +00:00
|
|
|
size = READ_BE_UINT32_UNALIGNED(ptr + 4) - 27;
|
|
|
|
rate = 8000;
|
2002-08-29 23:45:15 +00:00
|
|
|
|
|
|
|
// Allocate a sound buffer, copy the data into it, and play
|
|
|
|
char *sound = (char*)malloc(size);
|
|
|
|
memcpy(sound, ptr + 33, size);
|
|
|
|
_scumm->_mixer->playRaw(NULL, sound, size, rate, SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE);
|
|
|
|
return;
|
|
|
|
} else if (_scumm->_features & GF_OLD256) {
|
|
|
|
char *sound;
|
2002-09-09 11:15:44 +00:00
|
|
|
size = READ_LE_UINT32(ptr);
|
2002-08-29 23:45:15 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// FIXME - this is just some debug output for Zak256
|
|
|
|
if (size != 30) {
|
|
|
|
char name[9];
|
|
|
|
memcpy(name, ptr+22, 8);
|
|
|
|
name[8] = 0;
|
|
|
|
printf("Going to play Zak256 sound '%s':\n", name);
|
|
|
|
hexdump(ptr, 0x40);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
There seems to be some pattern in the Zak256 sound data. Two typical
|
|
|
|
examples are these:
|
|
|
|
|
|
|
|
d7 10 00 00 53 4f d1 10 |....SO..|
|
|
|
|
00 00 00 00 04 00 ff 00 |........|
|
|
|
|
64 00 00 00 01 00 64 6f |d.....do|
|
|
|
|
6f 72 6f 70 65 6e 40 a8 |oropen@.|
|
|
|
|
57 14 a1 10 00 00 50 08 |W.....P.|
|
|
|
|
00 00 00 00 00 00 b3 07 |........|
|
|
|
|
00 00 3c 00 00 00 04 80 |..<.....|
|
|
|
|
03 02 0a 01 8c 82 87 81 |........|
|
|
|
|
|
|
|
|
5b 07 00 00 53 4f 55 07 |[...SOU.|
|
|
|
|
00 00 00 00 04 00 ff 00 |........|
|
|
|
|
64 00 00 00 01 00 64 72 |d.....dr|
|
|
|
|
77 6f 70 65 6e 00 53 a8 |wopen.S.|
|
|
|
|
57 14 25 07 00 00 92 03 |W.%.....|
|
|
|
|
00 00 00 00 00 00 88 03 |........|
|
|
|
|
00 00 3c 00 00 00 82 82 |..<.....|
|
|
|
|
83 84 86 88 89 8b 89 89 |........|
|
|
|
|
|
|
|
|
As you can see, there are quite some patterns, e.g.
|
|
|
|
the 00 00 00 3c - the sound data seems to start at
|
|
|
|
offset 54.
|
2002-09-09 11:15:44 +00:00
|
|
|
|
|
|
|
Indy 3 seems to use a different format. The very first sound played
|
|
|
|
in Indy 3 looks as follows:
|
|
|
|
5a 25 00 00 53 4f 54 25 |Z%..SOT%|
|
|
|
|
00 00 53 4f db 0a 00 00 |..SO....|
|
|
|
|
57 41 c8 00 18 00 00 00 |WA......|
|
|
|
|
00 00 00 00 9e 05 00 00 |........|
|
|
|
|
00 00 00 00 fd 32 00 f8 |.....2..|
|
|
|
|
02 f9 08 ff 22 08 00 ff |...."...|
|
|
|
|
20 5c 00 ff 10 03 00 fd | \......|
|
|
|
|
64 00 f8 00 f9 00 ff 22 |d......"|
|
|
|
|
|
|
|
|
Indy 3, opening a door:
|
|
|
|
d1 00 00 00 53 4f cb 00 |....SO..|
|
|
|
|
00 00 53 4f a2 00 00 00 |..SO....|
|
|
|
|
57 41 64 00 18 00 00 00 |WAd.....|
|
|
|
|
00 00 00 00 00 00 00 00 |........|
|
|
|
|
00 00 7e 00 f9 0c ff 20 |..~.... |
|
|
|
|
90 01 ff 22 c2 01 ff 0a |..."....|
|
|
|
|
03 00 ff 04 57 06 ff 00 |....W...|
|
|
|
|
04 00 ff 0a 00 00 ff 00 |........|
|
|
|
|
|
|
|
|
So there seems to be a "SO" chunk which contains again a SO chunk and a WA chunk.
|
|
|
|
WA probably again contains audio data?
|
2002-08-29 23:45:15 +00:00
|
|
|
*/
|
|
|
|
#endif
|
2002-09-09 11:15:44 +00:00
|
|
|
rate = 11000;
|
2002-08-29 23:45:15 +00:00
|
|
|
|
|
|
|
ptr += 0x16;
|
|
|
|
if (size == 30) {
|
|
|
|
int track = *ptr;
|
|
|
|
|
|
|
|
if (track == _scumm->current_cd_sound)
|
2002-09-16 12:10:19 +00:00
|
|
|
if (pollCD() == 1) return;
|
|
|
|
|
|
|
|
playCDTrack(track, 1, 0, 0);
|
|
|
|
_scumm->current_cd_sound = track;
|
2002-08-29 23:45:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= 0x36;
|
|
|
|
sound = (char*)malloc(size);
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
int bit = *ptr++;
|
2002-09-09 11:15:44 +00:00
|
|
|
if (_scumm->_gameId == GID_INDY3_256) {
|
|
|
|
// FIXME - this is an (obviously incorrect, just listen to it)
|
|
|
|
// test hack for the Indy3 music format.... it doesn't work better
|
|
|
|
// but at least the generated data "looks" somewhat OK :-)
|
|
|
|
sound[x] = bit ^ 0x80;
|
|
|
|
} else {
|
|
|
|
if (bit < 0x80)
|
|
|
|
sound[x] = 0x7F-bit;
|
|
|
|
else
|
|
|
|
sound[x] = bit;
|
|
|
|
}
|
2002-08-29 23:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Maybe something in the header signifies looping? Need to
|
|
|
|
// track it down and add a mixer flag or something (see also bug .
|
|
|
|
_scumm->_mixer->playRaw(NULL, sound, size, 11000, SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE);
|
|
|
|
return;
|
2002-07-25 15:37:31 +00:00
|
|
|
}
|
2002-08-29 23:45:15 +00:00
|
|
|
|
2002-07-25 15:37:31 +00:00
|
|
|
}
|
|
|
|
|
2002-08-20 09:58:35 +00:00
|
|
|
IMuse *se = _scumm->_imuse;
|
2001-11-05 19:21:49 +00:00
|
|
|
if (se) {
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->getResourceAddress(rtSound, sound);
|
2001-11-05 19:21:49 +00:00
|
|
|
se->start_sound(sound);
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::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) {
|
2002-09-13 11:59:43 +00:00
|
|
|
if (_talk_sound_mode & 1)
|
|
|
|
|
|
|
|
startTalkSound(_talk_sound_a1, _talk_sound_b1, 1);
|
|
|
|
if (_talk_sound_mode & 2)
|
|
|
|
_talkChannel = startTalkSound(_talk_sound_a2, _talk_sound_b2, 2);
|
2001-11-05 19:21:49 +00:00
|
|
|
_talk_sound_mode = 0;
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (_scumm->_vars[_scumm->VAR_TALK_ACTOR]) { //_sfxMode == 2) {
|
|
|
|
act = _scumm->_vars[_scumm->VAR_TALK_ACTOR];
|
2002-04-22 08:16:16 +00:00
|
|
|
if (_talkChannel < 0)
|
|
|
|
finished = false;
|
2002-08-14 20:43:56 +00:00
|
|
|
else if (_scumm->_mixer->_channels[_talkChannel] == NULL)
|
2002-04-22 08:16:16 +00:00
|
|
|
finished = true;
|
|
|
|
else
|
|
|
|
finished = false;
|
|
|
|
|
2001-11-05 19:21:49 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (act != 0 && (uint) act < 0x80 && !_scumm->string[0].no_talk_anim) {
|
|
|
|
a = _scumm->derefActorSafe(act, "processSfxQueues");
|
|
|
|
if (a->room == _scumm->_currentRoom && (finished || !_endOfMouthSync)) {
|
2001-11-05 19:21:49 +00:00
|
|
|
b = true;
|
|
|
|
if (!finished)
|
|
|
|
b = isMouthSyncOff(_curSoundPos);
|
|
|
|
if (_mouthSyncMode != b) {
|
|
|
|
_mouthSyncMode = b;
|
2002-09-11 13:28:34 +00:00
|
|
|
if (_talk_sound_frame != -1) {
|
|
|
|
a->startAnimActor(_talk_sound_frame);
|
|
|
|
_talk_sound_frame = -1;
|
|
|
|
} else
|
|
|
|
a->startAnimActor(b ? a->talkFrame2 : a->talkFrame1);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-22 08:16:16 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (finished && _scumm->_talkDelay == 0) {
|
|
|
|
_scumm->stopTalk();
|
2001-11-05 19:21:49 +00:00
|
|
|
_sfxMode = 0;
|
2002-04-22 08:16:16 +00:00
|
|
|
_talkChannel = -1;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
2002-04-22 08:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-14 20:43:56 +00:00
|
|
|
int Sound::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
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
if (_sfxFile->isOpen() == false) {
|
2001-11-05 19:21:49 +00:00
|
|
|
warning("startTalkSound: SFX file is not open");
|
2002-04-22 08:16:16 +00:00
|
|
|
return -1;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2002-04-22 11:36:41 +00:00
|
|
|
return -1;
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
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-09-02 07:53:43 +00:00
|
|
|
_sfxFile->seek(offset, SEEK_SET);
|
2002-03-06 23:17:16 +00:00
|
|
|
i = 0;
|
|
|
|
while (num > 0) {
|
2002-09-02 07:53:43 +00:00
|
|
|
_sfxFile->read(&file_byte, sizeof(file_byte));
|
|
|
|
_sfxFile->read(&file_byte_2, sizeof(file_byte_2));
|
2002-04-11 17:19:16 +00:00
|
|
|
_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-04-22 08:16:16 +00:00
|
|
|
return startSfxSound(_sfxFile, size);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::stopTalkSound() {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (_sfxMode == 2) {
|
2001-11-14 18:40:39 +00:00
|
|
|
stopSfxSound();
|
|
|
|
_sfxMode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
bool Sound::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-08-14 20:43:56 +00:00
|
|
|
int Sound::isSoundRunning(int sound) {
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se;
|
2001-11-05 19:21:49 +00:00
|
|
|
int i;
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (sound == _scumm->current_cd_sound)
|
2002-08-29 23:45:15 +00:00
|
|
|
return pollCD();
|
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;
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (!_scumm->isResourceLoaded(rtSound, sound))
|
2001-11-05 19:21:49 +00:00
|
|
|
return 0;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
se = _scumm->_imuse;
|
2001-11-05 19:21:49 +00:00
|
|
|
if (!se)
|
|
|
|
return 0;
|
|
|
|
return se->get_sound_status(sound);
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
bool Sound::isSoundInQueue(int sound) {
|
2002-04-11 17:19:16 +00:00
|
|
|
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-08-14 20:43:56 +00:00
|
|
|
void Sound::stopSound(int a) {
|
2002-04-14 18:13:08 +00:00
|
|
|
IMuse *se;
|
2001-11-05 19:21:49 +00:00
|
|
|
int i;
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (a != 0 && a == _scumm->current_cd_sound) {
|
|
|
|
_scumm->current_cd_sound = 0;
|
2002-08-29 23:45:15 +00:00
|
|
|
stopCD();
|
2002-03-16 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
se = _scumm->_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-08-14 20:43:56 +00:00
|
|
|
void Sound::stopAllSounds()
|
2002-04-11 17:19:16 +00:00
|
|
|
{
|
2002-08-14 20:43:56 +00:00
|
|
|
IMuse *se = _scumm->_imuse;
|
2002-03-16 05:33:25 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (_scumm->current_cd_sound != 0) {
|
|
|
|
_scumm->current_cd_sound = 0;
|
2002-08-29 23:45:15 +00:00
|
|
|
stopCD();
|
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-08-14 20:43:56 +00:00
|
|
|
void Sound::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-08-14 20:43:56 +00:00
|
|
|
void Sound::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-09-11 13:28:34 +00:00
|
|
|
void Sound::talkSound(uint32 a, uint32 b, int mode, int frame) {
|
2002-09-13 11:59:43 +00:00
|
|
|
if (mode == 1) {
|
|
|
|
_talk_sound_a1 = a;
|
|
|
|
_talk_sound_b1 = b;
|
|
|
|
} else {
|
|
|
|
_talk_sound_a2 = a;
|
|
|
|
_talk_sound_b2 = b;
|
|
|
|
}
|
|
|
|
|
2002-09-11 13:28:34 +00:00
|
|
|
_talk_sound_frame = frame;
|
2002-09-13 11:59:43 +00:00
|
|
|
_talk_sound_mode |= mode;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
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-08-14 20:43:56 +00:00
|
|
|
void Sound::setupSound() {
|
|
|
|
if (_scumm->_imuse) {
|
|
|
|
_scumm->_imuse->setBase(_scumm->res.address[rtSound]);
|
2002-07-27 14:55:40 +00:00
|
|
|
|
2002-07-28 15:03:45 +00:00
|
|
|
_sound_volume_music = scummcfg->getInt("music_volume", kDefaultMusicVolume);
|
|
|
|
_sound_volume_master = scummcfg->getInt("master_volume", kDefaultMasterVolume);
|
|
|
|
_sound_volume_sfx = scummcfg->getInt("sfx_volume", kDefaultSFXVolume);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->_imuse->set_master_volume(_sound_volume_master);
|
|
|
|
_scumm->_imuse->set_music_volume(_sound_volume_music);
|
2002-08-24 15:31:37 +00:00
|
|
|
_scumm->_mixer->setVolume(_sound_volume_sfx);
|
|
|
|
_scumm->_mixer->setMusicVolume(_sound_volume_music);
|
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-08-14 20:43:56 +00:00
|
|
|
void Sound::pauseSounds(bool pause) {
|
|
|
|
IMuse *se = _scumm->_imuse;
|
2001-11-14 18:40:39 +00:00
|
|
|
if (se)
|
|
|
|
se->pause(pause);
|
2002-07-22 12:44:57 +00:00
|
|
|
|
2001-11-14 18:40:39 +00:00
|
|
|
_soundsPaused = pause;
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->_mixer->pause(pause);
|
2002-09-21 13:48:03 +00:00
|
|
|
|
|
|
|
if ((_scumm->_features & GF_AUDIOTRACKS) && _scumm->_vars[_scumm->VAR_MI1_TIMER] > 0) {
|
|
|
|
if (pause)
|
|
|
|
stopCDTimer();
|
|
|
|
else
|
|
|
|
startCDTimer();
|
|
|
|
}
|
|
|
|
|
2001-11-14 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
int Sound::startSfxSound(File *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-08-14 16:18:45 +00:00
|
|
|
uint size = 0;
|
2002-04-11 17:19:16 +00:00
|
|
|
int rate, comp;
|
2001-11-05 19:21:49 +00:00
|
|
|
byte *data;
|
|
|
|
|
2002-08-29 23:45:15 +00:00
|
|
|
// FIXME: Day of the Tentacle frequently assumes that starting one sound
|
|
|
|
// effect will automatically stop any other that may be playing at that
|
|
|
|
// time. Do any other games need this?
|
|
|
|
|
|
|
|
if (_scumm->_gameId == GID_TENTACLE)
|
|
|
|
stopSfxSound();
|
|
|
|
|
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-09-22 00:04:04 +00:00
|
|
|
if (file->read(data, file_size) != (uint)file_size) {
|
2002-04-11 17:19:16 +00:00
|
|
|
/* no need to free the memory since error will shut down */
|
|
|
|
error("startSfxSound: cannot read %d bytes", size);
|
2002-04-22 11:36:41 +00:00
|
|
|
return -1;
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2002-04-22 11:36:41 +00:00
|
|
|
return playSfxSound_MP3(data, file_size);
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-09-02 07:53:43 +00:00
|
|
|
if (file->read(ident, 8) != 8)
|
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-09-02 07:53:43 +00:00
|
|
|
file->seek(SOUND_HEADER_BIG_SIZE - 8, SEEK_CUR);
|
2002-01-02 11:50:28 +00:00
|
|
|
} else if (!memcmp(ident, "Creative", 8)) {
|
2002-09-02 07:53:43 +00:00
|
|
|
file->seek(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");
|
2002-04-22 08:16:16 +00:00
|
|
|
return -1;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
block_type = file->readByte();
|
2001-11-05 19:21:49 +00:00
|
|
|
if (block_type != 1) {
|
|
|
|
warning("startSfxSound: Expecting block_type == 1, got %d", block_type);
|
2002-04-22 08:16:16 +00:00
|
|
|
return -1;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
file->read(work, 3);
|
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;
|
2002-09-02 07:53:43 +00:00
|
|
|
rate = file->readByte();
|
|
|
|
comp = file->readByte();
|
2001-11-05 19:21:49 +00:00
|
|
|
|
|
|
|
if (comp != 0) {
|
|
|
|
warning("startSfxSound: Unsupported compression type %d", comp);
|
2002-04-22 08:16:16 +00:00
|
|
|
return -1;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
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");
|
2002-04-22 08:16:16 +00:00
|
|
|
return -1;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
if (file->read(data, size) != size) {
|
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);
|
2002-04-22 08:16:16 +00:00
|
|
|
return -1;
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
2002-03-14 16:49:59 +00:00
|
|
|
|
2002-08-03 11:57:10 +00:00
|
|
|
return playSfxSound(data, size, 1000000 / (256 - rate), true);
|
2001-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
File * Sound::openSfxFile() {
|
2002-05-15 19:29:36 +00:00
|
|
|
char buf[256];
|
2002-09-02 07:53:43 +00:00
|
|
|
File * file = new File();
|
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-09-15 19:28:34 +00:00
|
|
|
sprintf(buf, "%s.so3", _scumm->_exe_name);
|
|
|
|
if (!file->open(buf, _scumm->getGameDataPath())) {
|
|
|
|
file->open("monster.so3", _scumm->getGameDataPath());
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
2002-09-02 07:53:43 +00:00
|
|
|
if (file->isOpen() == true) {
|
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;
|
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
compressed_offset = file->readDwordBE();
|
2002-04-11 17:19:16 +00:00
|
|
|
offset_table = (MP3OffsetTable *) malloc(compressed_offset);
|
|
|
|
num_sound_effects = compressed_offset / 16;
|
|
|
|
|
|
|
|
size = compressed_offset;
|
|
|
|
cur = offset_table;
|
|
|
|
while (size > 0) {
|
2002-09-02 07:53:43 +00:00
|
|
|
cur[0].org_offset = file->readDwordBE();
|
|
|
|
cur[0].new_offset = file->readDwordBE() + compressed_offset + 4; /* The + 4 is to take into accound the 'size' field */
|
|
|
|
cur[0].num_tags = file->readDwordBE();
|
|
|
|
cur[0].compressed_size = file->readDwordBE();
|
2002-04-11 17:19:16 +00:00
|
|
|
size -= 4 * 4;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
return file;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-09-15 19:28:34 +00:00
|
|
|
sprintf(buf, "%s.sou", _scumm->_exe_name);
|
|
|
|
if (!file->open(buf, _scumm->getGameDataPath())) {
|
|
|
|
file->open("monster.sou", _scumm->getGameDataPath());
|
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-08-14 20:43:56 +00:00
|
|
|
void Sound::stopSfxSound() {
|
2002-08-24 15:31:37 +00:00
|
|
|
_scumm->_mixer->stopAll();
|
2001-11-14 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
bool Sound::isSfxFinished() {
|
2002-08-24 15:31:37 +00:00
|
|
|
return !_scumm->_mixer->hasActiveChannel();
|
2002-08-14 20:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Sound::decode12BitsSample(byte * src, byte ** dst, uint32 size) {
|
|
|
|
uint32 s_size = (size * 4) / 3;
|
|
|
|
byte * ptr = *dst = (byte*)malloc (s_size + 4);
|
|
|
|
|
|
|
|
uint32 r = 0, tmp, l;
|
|
|
|
for (l = 0; l < size; l += 3) {
|
|
|
|
tmp = (src[l + 1] & 0x0f) << 8;
|
|
|
|
tmp = (tmp | src[l + 0]) << 4;
|
|
|
|
tmp -= 0x8000;
|
|
|
|
ptr[r++] = (byte)((tmp >> 8) & 0xff);
|
|
|
|
ptr[r++] = (byte)(tmp & 0xff);
|
|
|
|
|
|
|
|
tmp = (src[l + 1] & 0xf0) << 4;
|
|
|
|
tmp = (tmp | src[l + 2]) << 4;
|
|
|
|
tmp -= 0x8000;
|
|
|
|
ptr[r++] = (byte)((tmp >> 8) & 0xff);
|
|
|
|
ptr[r++] = (byte)(tmp & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2002-03-06 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
2002-09-18 10:22:36 +00:00
|
|
|
static void music_handler (void * engine) {
|
|
|
|
g_scumm->_sound->bundleMusicHandler(g_scumm);
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define OUTPUT_SIZE 66150 // ((22050 * 2 * 2) / 4) * 3
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::playBundleMusic(int32 song) {
|
2002-08-04 16:30:59 +00:00
|
|
|
if (_numberBundleMusic == -1) {
|
2002-09-15 19:28:34 +00:00
|
|
|
if (_scumm->_bundle->openMusicFile("digmusic.bun", _scumm->getGameDataPath()) == false) {
|
2002-08-04 16:30:59 +00:00
|
|
|
return;
|
2002-08-31 09:50:36 +00:00
|
|
|
}
|
|
|
|
|
2002-08-04 16:30:59 +00:00
|
|
|
_musicBundleBufFinal = (byte*)malloc(OUTPUT_SIZE);
|
|
|
|
_musicBundleBufOutput = (byte*)malloc(10 * 0x2000);
|
|
|
|
_currentSampleBundleMusic = 0;
|
|
|
|
_offsetSampleBundleMusic = 0;
|
|
|
|
_offsetBufBundleMusic = 0;
|
2002-08-08 07:48:57 +00:00
|
|
|
_pauseBundleMusic = false;
|
2002-08-14 20:43:56 +00:00
|
|
|
_numberSamplesBundleMusic = _scumm->_bundle->getNumberOfMusicSamplesByIndex(song);
|
2002-08-04 16:30:59 +00:00
|
|
|
_numberBundleMusic = song;
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->_timer->installProcedure(&music_handler, 1000);
|
2002-08-04 16:30:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_numberBundleMusic != song) {
|
2002-08-14 20:43:56 +00:00
|
|
|
_numberSamplesBundleMusic = _scumm->_bundle->getNumberOfMusicSamplesByIndex(song);
|
2002-08-04 16:30:59 +00:00
|
|
|
_numberBundleMusic = song;
|
|
|
|
_currentSampleBundleMusic = 0;
|
|
|
|
_offsetSampleBundleMusic = 0;
|
|
|
|
_offsetBufBundleMusic = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::pauseBundleMusic(bool state) {
|
2002-08-08 07:48:57 +00:00
|
|
|
_pauseBundleMusic = state;
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::stopBundleMusic() {
|
|
|
|
_scumm->_timer->releaseProcedure(&music_handler);
|
2002-08-04 16:30:59 +00:00
|
|
|
_numberBundleMusic = -1;
|
2002-08-08 07:48:57 +00:00
|
|
|
if (_musicBundleBufFinal) {
|
2002-08-04 16:30:59 +00:00
|
|
|
free(_musicBundleBufFinal);
|
2002-08-08 07:48:57 +00:00
|
|
|
_musicBundleBufFinal = NULL;
|
|
|
|
}
|
|
|
|
if (_musicBundleBufOutput) {
|
2002-08-04 16:30:59 +00:00
|
|
|
free(_musicBundleBufOutput);
|
2002-08-08 07:48:57 +00:00
|
|
|
_musicBundleBufOutput = NULL;
|
|
|
|
}
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::bundleMusicHandler(Scumm * scumm) {
|
2002-08-04 16:30:59 +00:00
|
|
|
byte * ptr;
|
|
|
|
int32 l, num = _numberSamplesBundleMusic, length, k;
|
|
|
|
int32 rate = 22050;
|
|
|
|
int32 tag, size = -1, header_size = 0;
|
|
|
|
|
|
|
|
ptr = _musicBundleBufOutput;
|
|
|
|
|
2002-08-08 07:48:57 +00:00
|
|
|
if (_pauseBundleMusic)
|
2002-08-14 16:18:45 +00:00
|
|
|
return;
|
2002-08-04 16:30:59 +00:00
|
|
|
|
|
|
|
for (k = 0, l = _currentSampleBundleMusic; l < num; k++) {
|
2002-08-14 20:43:56 +00:00
|
|
|
length = _scumm->_bundle->decompressMusicSampleByIndex(_numberBundleMusic, l, (_musicBundleBufOutput + ((k * 0x2000) + _offsetBufBundleMusic)));
|
2002-08-04 16:30:59 +00:00
|
|
|
_offsetSampleBundleMusic += length;
|
|
|
|
|
|
|
|
if (l == 0) {
|
|
|
|
tag = READ_BE_UINT32(ptr); ptr += 4;
|
|
|
|
if (tag != MKID_BE('iMUS')) {
|
|
|
|
warning("Decompression of bundle sound failed");
|
|
|
|
_numberBundleMusic = -1;
|
2002-08-14 16:18:45 +00:00
|
|
|
return;
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr += 12;
|
|
|
|
while(tag != MKID_BE('DATA')) {
|
|
|
|
tag = READ_BE_UINT32(ptr); ptr += 4;
|
|
|
|
switch(tag) {
|
|
|
|
case MKID_BE('FRMT'):
|
|
|
|
size = READ_BE_UINT32(ptr); ptr += 24;
|
|
|
|
break;
|
|
|
|
case MKID_BE('TEXT'):
|
|
|
|
case MKID_BE('REGN'):
|
|
|
|
case MKID_BE('STOP'):
|
|
|
|
case MKID_BE('JUMP'):
|
|
|
|
size = READ_BE_UINT32(ptr); ptr += size + 4;
|
|
|
|
break;
|
|
|
|
case MKID_BE('DATA'):
|
|
|
|
size = READ_BE_UINT32(ptr); ptr += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Unknown sound header %c%c%c%c", tag>>24, tag>>16, tag>>8, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (size < 0) {
|
|
|
|
warning("Decompression sound failed (no size field)");
|
|
|
|
_numberBundleMusic = -1;
|
2002-08-14 16:18:45 +00:00
|
|
|
return;
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
header_size = (ptr - _musicBundleBufOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
l++;
|
|
|
|
_currentSampleBundleMusic = l;
|
|
|
|
|
|
|
|
if (_offsetSampleBundleMusic >= OUTPUT_SIZE + header_size) {
|
|
|
|
memcpy(_musicBundleBufFinal, (_musicBundleBufOutput + header_size), OUTPUT_SIZE);
|
|
|
|
_offsetBufBundleMusic = _offsetSampleBundleMusic - OUTPUT_SIZE - header_size;
|
|
|
|
memcpy(_musicBundleBufOutput, (_musicBundleBufOutput + (OUTPUT_SIZE + header_size)), _offsetBufBundleMusic);
|
|
|
|
_offsetSampleBundleMusic = _offsetBufBundleMusic;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-23 20:27:42 +00:00
|
|
|
if (_currentSampleBundleMusic == num)
|
|
|
|
_currentSampleBundleMusic = 0;
|
2002-08-04 16:30:59 +00:00
|
|
|
|
|
|
|
size = OUTPUT_SIZE;
|
|
|
|
ptr = _musicBundleBufFinal;
|
2002-08-18 22:47:11 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
byte * buffer = NULL;
|
|
|
|
uint32 final_size = decode12BitsSample(ptr, &buffer, size);
|
2002-08-24 15:31:37 +00:00
|
|
|
_scumm->_mixer->playRaw(NULL, buffer, final_size, rate, SoundMixer::FLAG_AUTOFREE | SoundMixer::FLAG_16BITS | SoundMixer::FLAG_STEREO);
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
void Sound::playBundleSound(char *sound) {
|
2002-07-28 15:35:26 +00:00
|
|
|
byte * ptr;
|
2002-05-09 03:51:51 +00:00
|
|
|
|
2002-09-15 19:28:34 +00:00
|
|
|
if (_scumm->_bundle->openVoiceFile("digvoice.bun", _scumm->getGameDataPath()) == false) {
|
2002-08-31 09:50:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-07-28 15:35:26 +00:00
|
|
|
ptr = (byte *)malloc(1000000);
|
2002-08-14 20:43:56 +00:00
|
|
|
if (_scumm->_bundle->decompressVoiceSampleByName(sound, ptr) == 0) {
|
2002-07-28 15:35:26 +00:00
|
|
|
delete ptr;
|
2002-04-29 08:28:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-07-28 15:35:26 +00:00
|
|
|
int rate = 22050;
|
|
|
|
int tag, size = -1;
|
2002-04-29 08:28:27 +00:00
|
|
|
|
2002-07-28 15:35:26 +00:00
|
|
|
tag = READ_BE_UINT32(ptr); ptr+=4;
|
|
|
|
if (tag != MKID_BE('iMUS')) {
|
|
|
|
warning("Decompression of bundle sound failed");
|
|
|
|
free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2002-04-29 08:28:27 +00:00
|
|
|
|
2002-07-28 15:35:26 +00:00
|
|
|
ptr += 12;
|
|
|
|
while(tag != MKID_BE('DATA')) {
|
|
|
|
tag = READ_BE_UINT32(ptr); ptr+=4;
|
|
|
|
switch(tag) {
|
|
|
|
case MKID_BE('FRMT'):
|
|
|
|
size = READ_BE_UINT32(ptr); ptr+=16;
|
|
|
|
rate = READ_BE_UINT32(ptr); ptr+=8;
|
2002-04-29 08:28:27 +00:00
|
|
|
break;
|
2002-07-28 15:35:26 +00:00
|
|
|
case MKID_BE('TEXT'):
|
|
|
|
case MKID_BE('REGN'):
|
|
|
|
case MKID_BE('STOP'):
|
|
|
|
case MKID_BE('JUMP'):
|
|
|
|
size = READ_BE_UINT32(ptr); ptr+=size+4;
|
2002-04-29 08:28:27 +00:00
|
|
|
break;
|
|
|
|
|
2002-07-28 15:35:26 +00:00
|
|
|
case MKID_BE('DATA'):
|
|
|
|
size = READ_BE_UINT32(ptr); ptr+=4;
|
2002-07-19 17:00:27 +00:00
|
|
|
break;
|
|
|
|
|
2002-04-29 08:28:27 +00:00
|
|
|
default:
|
2002-07-28 15:35:26 +00:00
|
|
|
error("Unknown sound header %c%c%c%c", tag>>24, tag>>16, tag>>8, tag);
|
2002-05-01 12:57:12 +00:00
|
|
|
}
|
2002-04-29 08:28:27 +00:00
|
|
|
}
|
|
|
|
|
2002-07-28 15:35:26 +00:00
|
|
|
if (size < 0) {
|
|
|
|
warning("Decompression sound failed (no size field)");
|
|
|
|
free(ptr);
|
2002-04-30 07:40:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-07-28 15:35:26 +00:00
|
|
|
|
|
|
|
byte * final = (byte *)malloc(size);
|
|
|
|
memcpy(final, ptr, size);
|
2002-08-24 15:31:37 +00:00
|
|
|
_scumm->_mixer->playRaw(NULL, final, size, rate, SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE);
|
2002-04-08 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
int Sound::playSfxSound(void *sound, uint32 size, uint rate, bool isUnsigned) {
|
2002-04-21 21:58:21 +00:00
|
|
|
if (_soundsPaused)
|
2002-04-22 08:16:16 +00:00
|
|
|
return -1;
|
2002-08-03 11:57:10 +00:00
|
|
|
byte flags = SoundMixer::FLAG_AUTOFREE;
|
|
|
|
if (isUnsigned)
|
|
|
|
flags |= SoundMixer::FLAG_UNSIGNED;
|
2002-08-24 15:31:37 +00:00
|
|
|
return _scumm->_mixer->playRaw(NULL, sound, size, rate, flags);
|
2001-11-14 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
int Sound::playSfxSound_MP3(void *sound, uint32 size) {
|
2002-04-16 18:33:04 +00:00
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
2002-04-21 21:58:21 +00:00
|
|
|
if (_soundsPaused)
|
2002-04-22 11:36:41 +00:00
|
|
|
return -1;
|
2002-08-24 15:31:37 +00:00
|
|
|
return _scumm->_mixer->playMP3(NULL, sound, size, SoundMixer::FLAG_AUTOFREE);
|
2002-04-16 18:33:04 +00:00
|
|
|
#endif
|
2002-04-22 11:36:41 +00:00
|
|
|
return -1;
|
2002-04-15 20:22:47 +00:00
|
|
|
}
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2002-09-21 13:48:03 +00:00
|
|
|
// We use a real timer in an attempt to get better sync with CD tracks. This is
|
|
|
|
// necessary for games like Loom CD.
|
|
|
|
|
|
|
|
static void cd_timer_handler(void *ptr)
|
|
|
|
{
|
|
|
|
Scumm *scumm = (Scumm *) ptr;
|
|
|
|
|
|
|
|
// Maybe I could simply update _vars[VAR_MI1_TIMER] directly here, but
|
|
|
|
// I don't feel comfortable just doing that from what might be a
|
|
|
|
// separate thread. If someone tells me it's safe, I'll make the
|
|
|
|
// change right away.
|
|
|
|
|
|
|
|
// FIXME: Turn off the timer when it's no longer needed. In theory, it
|
|
|
|
// should be possible to check with pollCD(), but since CD sound isn't
|
|
|
|
// properly restarted when reloading a saved game, I don't dare to.
|
|
|
|
|
|
|
|
scumm->_sound->_cd_timer_value += 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Sound::readCDTimer()
|
|
|
|
{
|
|
|
|
return _cd_timer_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::startCDTimer()
|
|
|
|
{
|
|
|
|
int timer_interval;
|
|
|
|
|
|
|
|
// The timer interval has been tuned for Loom CD and the Monkey 1
|
|
|
|
// intro. I have to use 100 for Loom, or there will be a nasty stutter
|
|
|
|
// when Chaos first appears, and I have to use 101 for Monkey 1 or the
|
|
|
|
// intro music will be cut short.
|
|
|
|
|
|
|
|
if (_scumm->_gameId == GID_LOOM256)
|
|
|
|
timer_interval = 100;
|
|
|
|
else
|
|
|
|
timer_interval = 101;
|
|
|
|
|
|
|
|
_scumm->_timer->releaseProcedure(&cd_timer_handler);
|
|
|
|
_cd_timer_value = _scumm->_vars[_scumm->VAR_MI1_TIMER];
|
|
|
|
_scumm->_timer->installProcedure(&cd_timer_handler, timer_interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::stopCDTimer()
|
|
|
|
{
|
|
|
|
_scumm->_timer->releaseProcedure(&cd_timer_handler);
|
|
|
|
}
|
|
|
|
|
2002-08-29 23:45:15 +00:00
|
|
|
void Sound::playCDTrack(int track, int num_loops, int start, int delay)
|
|
|
|
{
|
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
if (playMP3CDTrack(track, num_loops, start, delay) == -1)
|
|
|
|
#endif
|
|
|
|
_scumm->_system->play_cdrom(track, num_loops, start, delay);
|
2002-09-21 13:48:03 +00:00
|
|
|
|
|
|
|
// Start the timer after starting the track. Starting an MP3 track is
|
|
|
|
// almost instantaneous, but a CD player may take some time. Hopefully
|
|
|
|
// play_cdrom() will block during that delay.
|
|
|
|
|
|
|
|
startCDTimer();
|
2002-08-29 23:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::stopCD()
|
|
|
|
{
|
2002-09-21 13:48:03 +00:00
|
|
|
stopCDTimer();
|
2002-08-29 23:45:15 +00:00
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
if (stopMP3CD() == -1)
|
|
|
|
#endif
|
|
|
|
_scumm->_system->stop_cdrom();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Sound::pollCD()
|
|
|
|
{
|
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
if (pollMP3CD())
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
return _scumm->_system->poll_cdrom();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::updateCD()
|
|
|
|
{
|
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
if (updateMP3CD() == -1)
|
|
|
|
#endif
|
|
|
|
_scumm->_system->update_cdrom();
|
|
|
|
}
|
|
|
|
|
2002-04-17 20:23:45 +00:00
|
|
|
#ifdef COMPRESSED_SOUND_FILE
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
int Sound::getCachedTrack(int track) {
|
2002-04-17 20:23:45 +00:00
|
|
|
int i;
|
|
|
|
char track_name[1024];
|
2002-09-02 07:53:43 +00:00
|
|
|
File * file = new File();
|
2002-04-17 20:23:45 +00:00
|
|
|
int current_index;
|
|
|
|
struct mad_stream stream;
|
|
|
|
struct mad_frame frame;
|
|
|
|
unsigned char buffer[8192];
|
|
|
|
unsigned int buflen = 0;
|
2002-04-19 17:06:08 +00:00
|
|
|
int count = 0;
|
2002-04-17 20:23:45 +00:00
|
|
|
|
|
|
|
// See if we find the track in the cache
|
2002-09-02 07:53:43 +00:00
|
|
|
for (i = 0; i < CACHE_TRACKS; i++)
|
2002-04-17 20:23:45 +00:00
|
|
|
if (_cached_tracks[i] == track) {
|
|
|
|
if (_mp3_tracks[i])
|
|
|
|
return i;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
current_index = _current_cache++;
|
|
|
|
_current_cache %= CACHE_TRACKS;
|
2002-07-27 13:12:23 +00:00
|
|
|
|
2002-04-17 20:23:45 +00:00
|
|
|
// Not found, see if it exists
|
2002-09-15 19:28:34 +00:00
|
|
|
sprintf(track_name, "track%d.mp3", track);
|
|
|
|
file->open(track_name, _scumm->getGameDataPath());
|
2002-04-17 20:23:45 +00:00
|
|
|
_cached_tracks[current_index] = track;
|
2002-07-27 13:12:23 +00:00
|
|
|
|
2002-05-03 21:11:16 +00:00
|
|
|
/* First, close the previous file */
|
|
|
|
if (_mp3_tracks[current_index])
|
2002-09-02 07:53:43 +00:00
|
|
|
_mp3_tracks[current_index]->close();
|
|
|
|
|
2002-04-17 20:23:45 +00:00
|
|
|
_mp3_tracks[current_index] = NULL;
|
2002-09-02 07:53:43 +00:00
|
|
|
if (file->isOpen() == false) {
|
2002-04-24 07:19:00 +00:00
|
|
|
// This warning is pretty pointless.
|
|
|
|
debug(1, "Track %d not available in mp3 format", track);
|
2002-04-17 20:23:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-09-02 07:53:43 +00:00
|
|
|
|
2002-04-17 20:23:45 +00:00
|
|
|
// Check the format and bitrate
|
|
|
|
mad_stream_init(&stream);
|
|
|
|
mad_frame_init(&frame);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (buflen < sizeof(buffer)) {
|
2002-04-19 16:56:58 +00:00
|
|
|
int bytes;
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2002-09-02 07:53:43 +00:00
|
|
|
bytes = file->read(buffer + buflen, sizeof(buffer) - buflen);
|
2002-04-17 20:23:45 +00:00
|
|
|
if (bytes <= 0) {
|
|
|
|
if (bytes == -1) {
|
|
|
|
warning("Invalid format for track %d", track);
|
2002-04-26 17:11:05 +00:00
|
|
|
goto error;
|
2002-04-17 20:23:45 +00:00
|
|
|
}
|
|
|
|
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,
|
2002-04-26 17:11:05 +00:00
|
|
|
buflen = &buffer[buflen] - stream.next_frame);
|
2002-04-17 20:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
memcpy(&_mad_header[current_index], &frame.header, sizeof(mad_header));
|
|
|
|
else {
|
|
|
|
warning("Invalid format for track %d", track);
|
2002-04-26 17:11:05 +00:00
|
|
|
goto error;
|
2002-04-17 20:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mad_frame_finish(&frame);
|
|
|
|
mad_stream_finish(&stream);
|
|
|
|
// Get file size
|
2002-09-02 07:53:43 +00:00
|
|
|
file->seek(0, SEEK_END);
|
|
|
|
_mp3_size[current_index] = file->pos();
|
2002-04-17 20:23:45 +00:00
|
|
|
_mp3_tracks[current_index] = file;
|
|
|
|
|
|
|
|
return current_index;
|
2002-04-26 17:11:05 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
mad_frame_finish(&frame);
|
|
|
|
mad_stream_finish(&stream);
|
2002-09-02 07:53:43 +00:00
|
|
|
delete file;
|
2002-04-26 17:11:05 +00:00
|
|
|
|
|
|
|
return -1;
|
2002-04-17 20:23:45 +00:00
|
|
|
}
|
2002-04-22 11:36:41 +00:00
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
int Sound::playMP3CDTrack(int track, int num_loops, int start, int delay) {
|
2002-04-17 20:23:45 +00:00
|
|
|
int index;
|
2002-05-03 21:37:49 +00:00
|
|
|
unsigned int offset;
|
2002-04-17 20:23:45 +00:00
|
|
|
mad_timer_t duration;
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->_vars[_scumm->VAR_MI1_TIMER] = 0;
|
2002-04-28 14:01:40 +00:00
|
|
|
|
2002-04-21 21:58:21 +00:00
|
|
|
if (_soundsPaused)
|
2002-04-24 07:19:00 +00:00
|
|
|
return 0;
|
2002-04-21 21:58:21 +00:00
|
|
|
|
2002-04-28 14:01:40 +00:00
|
|
|
if ((num_loops == 0) && (start == 0)) {
|
2002-04-24 07:19:00 +00:00
|
|
|
return 0;
|
2002-04-17 20:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index = getCachedTrack(track);
|
|
|
|
if (index < 0)
|
2002-04-24 07:19:00 +00:00
|
|
|
return -1;
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2002-05-03 21:37:49 +00:00
|
|
|
// Calc offset. As all bitrates are in kilobit per seconds, the division by 200 is always exact
|
|
|
|
offset = (start * (_mad_header[index].bitrate / (8 * 25))) / 3;
|
2002-05-03 21:11:16 +00:00
|
|
|
|
2002-04-17 20:23:45 +00:00
|
|
|
// Calc delay
|
2002-04-22 11:18:11 +00:00
|
|
|
if (!delay) {
|
2002-05-03 21:11:16 +00:00
|
|
|
mad_timer_set(&duration, (_mp3_size[index] * 8) / _mad_header[index].bitrate,
|
|
|
|
(_mp3_size[index] * 8) % _mad_header[index].bitrate, _mad_header[index].bitrate);
|
2002-04-22 11:18:11 +00:00
|
|
|
} else {
|
2002-05-03 21:11:16 +00:00
|
|
|
mad_timer_set(&duration, delay / 75, delay % 75, 75);
|
2002-04-22 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
2002-04-17 20:23:45 +00:00
|
|
|
// Go
|
2002-09-02 07:53:43 +00:00
|
|
|
_mp3_tracks[index]->seek(offset, SEEK_SET);
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2002-04-28 14:01:40 +00:00
|
|
|
if (_mp3_cd_playing == true)
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->_mixer->stop(_mp3_index);
|
2002-08-24 15:31:37 +00:00
|
|
|
_mp3_index = _scumm->_mixer->playMP3CDTrack(NULL, _mp3_tracks[index], duration);
|
2002-04-28 14:01:40 +00:00
|
|
|
_mp3_cd_playing = true;
|
2002-04-24 07:19:00 +00:00
|
|
|
return 0;
|
2002-04-17 20:23:45 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
int Sound::stopMP3CD() {
|
2002-04-28 14:01:40 +00:00
|
|
|
if (_mp3_cd_playing == true) {
|
2002-08-14 20:43:56 +00:00
|
|
|
_scumm->_mixer->stop(_mp3_index);
|
2002-04-28 14:01:40 +00:00
|
|
|
_mp3_cd_playing = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
int Sound::pollMP3CD() {
|
2002-04-28 14:01:40 +00:00
|
|
|
if (_mp3_cd_playing == true)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
int Sound::updateMP3CD() {
|
2002-04-28 14:01:40 +00:00
|
|
|
if (_mp3_cd_playing == false)
|
|
|
|
return -1;
|
|
|
|
|
2002-08-14 20:43:56 +00:00
|
|
|
if (_scumm->_mixer->_channels[_mp3_index] == NULL) {
|
2002-04-28 14:01:40 +00:00
|
|
|
warning("Error in MP3 decoding");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
if (_scumm->_mixer->_channels[_mp3_index]->soundFinished())
|
2002-04-28 14:01:40 +00:00
|
|
|
stopMP3CD();
|
|
|
|
return 0;
|
|
|
|
}
|
2002-04-19 16:56:58 +00:00
|
|
|
#endif
|