2003-09-10 12:19:57 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2004-01-06 12:45:34 +00:00
|
|
|
* Copyright (C) 2001-2004 The ScummVM project
|
2003-09-10 12:19:57 +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.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "common/util.h"
|
2003-12-26 12:15:23 +00:00
|
|
|
#include "common/file.h"
|
2004-01-03 00:33:14 +00:00
|
|
|
|
|
|
|
#include "sound/audiostream.h"
|
|
|
|
#include "sound/mixer.h"
|
2003-09-10 12:19:57 +00:00
|
|
|
#include "sound/voc.h"
|
|
|
|
|
|
|
|
|
2004-01-03 00:33:14 +00:00
|
|
|
int getSampleRateFromVOCRate(int vocSR) {
|
2004-05-05 10:58:07 +00:00
|
|
|
if (vocSR == 0xa5 || vocSR == 0xa6) {
|
2004-01-03 00:33:14 +00:00
|
|
|
return 11025;
|
|
|
|
} else if (vocSR == 0xd2 || vocSR == 0xd3) {
|
|
|
|
return 22050;
|
|
|
|
} else {
|
|
|
|
int sr = 1000000L / (256L - vocSR);
|
2004-05-05 10:58:07 +00:00
|
|
|
// inexact sampling rates occur e.g. in the kitchen in Monkey Island,
|
2004-05-02 22:33:28 +00:00
|
|
|
// very easy to reach right from the start of the game.
|
|
|
|
//warning("inexact sample rate used: %i (0x%x)", sr, vocSR);
|
2004-01-03 00:33:14 +00:00
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-14 15:14:30 +00:00
|
|
|
byte *readVOCFromMemory(byte *ptr, int &size, int &rate, int &loops, int &begin_loop, int &end_loop) {
|
2003-12-26 22:53:30 +00:00
|
|
|
|
2004-05-05 10:58:07 +00:00
|
|
|
// Verify the VOC header. We are a little bit lenient here to work around
|
|
|
|
// some invalid VOC headers used in various SCUMM games (they have 0x0
|
|
|
|
// instead of 0x1A after the "Creative Voice File" string).
|
|
|
|
if (memcmp(ptr, "Creative Voice File", 19) != 0)
|
|
|
|
error("readVOCFromMemory: Invalid header");
|
|
|
|
if (ptr[19] != 0x1A)
|
|
|
|
debug(3, "readVOCFromMemory: Partially invalid header");
|
|
|
|
|
2003-12-26 12:15:23 +00:00
|
|
|
int32 offset = READ_LE_UINT16(ptr + 20);
|
|
|
|
int16 version = READ_LE_UINT16(ptr + 22);
|
|
|
|
int16 code = READ_LE_UINT16(ptr + 24);
|
|
|
|
assert(version == 0x010A || version == 0x0114);
|
|
|
|
assert(code == ~version + 0x1234);
|
2003-12-26 22:53:30 +00:00
|
|
|
|
2004-04-10 22:34:07 +00:00
|
|
|
int len;
|
2003-12-26 22:53:30 +00:00
|
|
|
byte *ret_sound = 0;
|
|
|
|
size = 0;
|
2004-02-14 15:14:30 +00:00
|
|
|
begin_loop = 0;
|
|
|
|
end_loop = 0;
|
2004-04-10 22:34:07 +00:00
|
|
|
|
|
|
|
ptr += offset;
|
|
|
|
while ((code = *ptr++)) {
|
|
|
|
len = *ptr++;
|
|
|
|
len |= *ptr++ << 8;
|
|
|
|
len |= *ptr++ << 16;
|
2003-12-26 22:53:30 +00:00
|
|
|
|
2003-12-26 12:15:23 +00:00
|
|
|
switch(code) {
|
|
|
|
case 1: {
|
2004-04-10 22:34:07 +00:00
|
|
|
int time_constant = *ptr++;
|
|
|
|
int packing = *ptr++;
|
2003-12-26 12:15:23 +00:00
|
|
|
len -= 2;
|
|
|
|
rate = getSampleRateFromVOCRate(time_constant);
|
2004-04-10 01:46:38 +00:00
|
|
|
debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
|
2003-12-26 12:15:23 +00:00
|
|
|
if (packing == 0) {
|
|
|
|
if (size) {
|
|
|
|
ret_sound = (byte *)realloc(ret_sound, size + len);
|
|
|
|
} else {
|
|
|
|
ret_sound = (byte *)malloc(len);
|
|
|
|
}
|
2004-04-10 22:34:07 +00:00
|
|
|
memcpy(ret_sound + size, ptr, len);
|
2004-02-14 15:14:30 +00:00
|
|
|
begin_loop = size;
|
2003-12-26 12:15:23 +00:00
|
|
|
size += len;
|
2004-02-14 15:14:30 +00:00
|
|
|
end_loop = size;
|
2003-12-26 12:15:23 +00:00
|
|
|
} else {
|
|
|
|
warning("VOC file packing %d unsupported", packing);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 6: // begin of loop
|
2004-04-10 22:34:07 +00:00
|
|
|
loops = (uint16)READ_LE_UINT16(ptr);
|
2003-12-26 12:15:23 +00:00
|
|
|
break;
|
|
|
|
case 7: // end of loop
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warning("Invalid code in VOC file : %d", code);
|
2004-04-10 22:34:07 +00:00
|
|
|
return ret_sound;
|
2003-12-26 12:15:23 +00:00
|
|
|
}
|
|
|
|
// FIXME some FT samples (ex. 362) has bad length, 2 bytes too short
|
2004-04-10 22:34:07 +00:00
|
|
|
ptr += len;
|
2003-12-26 12:15:23 +00:00
|
|
|
}
|
2004-04-10 01:46:38 +00:00
|
|
|
debug(4, "VOC Data Size : %d", size);
|
2003-12-26 12:15:23 +00:00
|
|
|
return ret_sound;
|
|
|
|
}
|
|
|
|
|
2003-12-27 14:10:45 +00:00
|
|
|
// FIXME/TODO: loadVOCFile() essentially duplicates all the code from
|
2004-04-10 01:46:38 +00:00
|
|
|
// readCreativeVoc(). Obviously this is bad, they should share as much
|
|
|
|
// code as possible. One way to do that would be to abstract the
|
|
|
|
// reading from memory / from file into a stream class (similar to the
|
|
|
|
// RWOps of SDL, for example).
|
2003-12-27 18:29:21 +00:00
|
|
|
byte *loadVOCFile(File *file, int &size, int &rate) {
|
2004-04-10 01:46:38 +00:00
|
|
|
VocFileHeader fileHeader;
|
2003-12-26 12:15:23 +00:00
|
|
|
|
2004-04-10 01:46:38 +00:00
|
|
|
if (file->read(&fileHeader, 8) != 8)
|
2003-12-26 12:15:23 +00:00
|
|
|
goto invalid;
|
|
|
|
|
2004-04-10 01:46:38 +00:00
|
|
|
if (!memcmp(&fileHeader, "VTLK", 4)) {
|
|
|
|
if (file->read(&fileHeader, sizeof(VocFileHeader)) != sizeof(VocFileHeader))
|
|
|
|
goto invalid;
|
|
|
|
} else if (!memcmp(&fileHeader, "Creative", 8)) {
|
|
|
|
if (file->read(((byte *)&fileHeader) + 8, sizeof(VocFileHeader) - 8) != sizeof(VocFileHeader) - 8)
|
|
|
|
goto invalid;
|
2003-12-26 12:15:23 +00:00
|
|
|
} else {
|
|
|
|
invalid:;
|
2004-05-05 10:58:07 +00:00
|
|
|
warning("loadVOCFile: Invalid header");
|
2003-12-26 12:15:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-05 10:58:07 +00:00
|
|
|
if (memcmp(fileHeader.desc, "Creative Voice File", 19) != 0)
|
|
|
|
error("loadVOCFile: Invalid header");
|
|
|
|
if (fileHeader.desc[19] != 0x1A)
|
|
|
|
debug(3, "loadVOCFile: Partially invalid header");
|
|
|
|
|
2004-04-10 01:46:38 +00:00
|
|
|
//int32 offset = FROM_LE_16(fileHeader.datablock_offset);
|
|
|
|
int16 version = FROM_LE_16(fileHeader.version);
|
|
|
|
int16 code = FROM_LE_16(fileHeader.id);
|
|
|
|
assert(version == 0x010A || version == 0x0114);
|
|
|
|
assert(code == ~version + 0x1234);
|
2003-12-26 12:15:23 +00:00
|
|
|
|
2004-04-10 22:34:07 +00:00
|
|
|
int len;
|
2004-04-10 01:46:38 +00:00
|
|
|
byte *ret_sound = 0;
|
|
|
|
size = 0;
|
2003-12-26 12:15:23 +00:00
|
|
|
|
2004-04-10 22:34:07 +00:00
|
|
|
while ((code = file->readByte())) {
|
|
|
|
len = file->readByte();
|
|
|
|
len |= file->readByte() << 8;
|
|
|
|
len |= file->readByte() << 16;
|
|
|
|
|
2004-04-10 01:46:38 +00:00
|
|
|
switch(code) {
|
|
|
|
case 1: {
|
|
|
|
int time_constant = file->readByte();
|
|
|
|
int packing = file->readByte();
|
|
|
|
len -= 2;
|
|
|
|
rate = getSampleRateFromVOCRate(time_constant);
|
|
|
|
debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
|
|
|
|
if (packing == 0) {
|
|
|
|
if (size) {
|
|
|
|
ret_sound = (byte *)realloc(ret_sound, size + len);
|
|
|
|
} else {
|
|
|
|
ret_sound = (byte *)malloc(len);
|
|
|
|
}
|
|
|
|
file->read(ret_sound + size, len);
|
|
|
|
size += len;
|
|
|
|
} else {
|
|
|
|
warning("VOC file packing %d unsupported", packing);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
warning("Invalid code in VOC file : %d", code);
|
2004-04-10 22:34:07 +00:00
|
|
|
return ret_sound;
|
2004-04-10 01:46:38 +00:00
|
|
|
}
|
2003-12-26 12:15:23 +00:00
|
|
|
}
|
2004-04-10 01:46:38 +00:00
|
|
|
debug(4, "VOC Data Size : %d", size);
|
|
|
|
return ret_sound;
|
2003-12-26 12:15:23 +00:00
|
|
|
}
|
|
|
|
|
2004-01-03 14:10:13 +00:00
|
|
|
AudioStream *makeVOCStream(byte *ptr) {
|
2004-02-14 15:14:30 +00:00
|
|
|
int size, rate, loops, begin_loop, end_loop;
|
|
|
|
byte *data = readVOCFromMemory(ptr, size, rate, loops, begin_loop, end_loop);
|
2004-01-03 00:33:14 +00:00
|
|
|
|
2004-02-03 08:53:13 +00:00
|
|
|
if (!data)
|
|
|
|
return 0;
|
|
|
|
|
2004-01-03 00:33:14 +00:00
|
|
|
return makeLinearInputStream(rate, SoundMixer::FLAG_AUTOFREE | SoundMixer::FLAG_UNSIGNED, data, size, 0, 0);
|
2003-09-10 12:19:57 +00:00
|
|
|
}
|
2004-01-03 00:33:14 +00:00
|
|
|
|
2004-01-03 14:10:13 +00:00
|
|
|
AudioStream *makeVOCStream(File *file) {
|
2004-01-03 00:33:14 +00:00
|
|
|
int size, rate;
|
|
|
|
byte *data = loadVOCFile(file, size, rate);
|
|
|
|
|
2004-02-03 08:53:13 +00:00
|
|
|
if (!data)
|
|
|
|
return 0;
|
|
|
|
|
2004-01-03 00:33:14 +00:00
|
|
|
return makeLinearInputStream(rate, SoundMixer::FLAG_AUTOFREE | SoundMixer::FLAG_UNSIGNED, data, size, 0, 0);
|
|
|
|
}
|
|
|
|
|