1
/* ScummVM - Graphic Adventure Engine
3
* ScummVM is the legal property of its developers, whose names
4
* are too numerous to list here. Please refer to the COPYRIGHT
5
* file distributed with this source distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26
#include "common/debug.h"
27
#include "common/endian.h"
28
#include "common/util.h"
29
#include "common/stream.h"
30
#include "common/textconsole.h"
32
#include "audio/audiostream.h"
33
#include "audio/decoders/raw.h"
34
#include "audio/decoders/voc.h"
39
int getSampleRateFromVOCRate(int vocSR) {
40
if (vocSR == 0xa5 || vocSR == 0xa6) {
42
} else if (vocSR == 0xd2 || vocSR == 0xd3) {
45
int sr = 1000000L / (256L - vocSR);
46
// inexact sampling rates occur e.g. in the kitchen in Monkey Island,
47
// very easy to reach right from the start of the game.
48
//warning("inexact sample rate used: %i (0x%x)", sr, vocSR);
53
static byte *loadVOCFromStream(Common::ReadStream &stream, int &size, int &rate, int &loops, int &begin_loop, int &end_loop) {
54
VocFileHeader fileHeader;
56
debug(2, "loadVOCFromStream");
58
if (stream.read(&fileHeader, 8) != 8)
61
if (!memcmp(&fileHeader, "VTLK", 4)) {
62
if (stream.read(&fileHeader, sizeof(VocFileHeader)) != sizeof(VocFileHeader))
64
} else if (!memcmp(&fileHeader, "Creative", 8)) {
65
if (stream.read(((byte *)&fileHeader) + 8, sizeof(VocFileHeader) - 8) != sizeof(VocFileHeader) - 8)
69
warning("loadVOCFromStream: Invalid header");
73
if (memcmp(fileHeader.desc, "Creative Voice File", 19) != 0)
74
error("loadVOCFromStream: Invalid header");
75
if (fileHeader.desc[19] != 0x1A)
76
debug(3, "loadVOCFromStream: Partially invalid header");
78
int32 offset = FROM_LE_16(fileHeader.datablock_offset);
79
int16 version = FROM_LE_16(fileHeader.version);
80
int16 code = FROM_LE_16(fileHeader.id);
81
assert(offset == sizeof(VocFileHeader));
82
// 0x100 is an invalid VOC version used by German version of DOTT (Disk) and
83
// French version of Simon the Sorcerer 2 (CD)
84
assert(version == 0x010A || version == 0x0114 || version == 0x0100);
85
assert(code == ~version + 0x1234);
93
while ((code = stream.readByte())) {
94
len = stream.readByte();
95
len |= stream.readByte() << 8;
96
len |= stream.readByte() << 16;
98
debug(2, "Block code %d, len %d", code, len);
105
int time_constant = stream.readByte();
106
packing = stream.readByte();
108
rate = getSampleRateFromVOCRate(time_constant);
110
rate = stream.readUint32LE();
111
int bits = stream.readByte();
112
int channels = stream.readByte();
113
if (bits != 8 || channels != 1) {
114
warning("Unsupported VOC file format (%d bits per sample, %d channels)", bits, channels);
117
packing = stream.readUint16LE();
118
stream.readUint32LE();
121
debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
124
ret_sound = (byte *)realloc(ret_sound, size + len);
126
ret_sound = (byte *)malloc(len);
128
stream.read(ret_sound + size, len);
133
warning("VOC file packing %d unsupported", packing);
137
// occur with a few Igor sounds, voc file starts with a silence block with a
138
// frequency different from the data block. Just ignore fow now (implementing
139
// it wouldn't make a big difference anyway...)
141
stream.readUint16LE();
144
case 6: // begin of loop
146
loops = stream.readUint16LE();
148
case 7: // end of loop
151
case 8: { // "Extended"
152
// This occures in the LoL Intro demo.
153
// This block overwrites the next parameters of a block 1 "Sound data".
154
// To assure we never get any bad data here, we will assert in case
155
// this tries to define a stereo sound block or tries to use something
156
// different than 8bit unsigned sound data.
157
// TODO: Actually we would need to check the frequency divisor (the
158
// first word) here too. It is used in the following equation:
159
// sampleRate = 256000000/(channels * (65536 - frequencyDivisor))
161
stream.readUint16LE();
162
uint8 codec = stream.readByte();
163
uint8 channels = stream.readByte() + 1;
164
assert(codec == 0 && channels == 1);
167
warning("Unhandled code %d in VOC file (len %d)", code, len);
171
debug(4, "VOC Data Size : %d", size);
175
byte *loadVOCFromStream(Common::ReadStream &stream, int &size, int &rate) {
176
int loops, begin_loop, end_loop;
177
return loadVOCFromStream(stream, size, rate, loops, begin_loop, end_loop);
181
#ifdef STREAM_AUDIO_FROM_DISK
183
int parseVOCFormat(Common::SeekableReadStream& stream, RawStreamBlock* block, int &rate, int &loops, int &begin_loop, int &end_loop) {
184
VocFileHeader fileHeader;
185
int currentBlock = 0;
188
debug(2, "parseVOCFormat");
190
if (stream.read(&fileHeader, 8) != 8)
193
if (!memcmp(&fileHeader, "VTLK", 4)) {
194
if (stream.read(&fileHeader, sizeof(VocFileHeader)) != sizeof(VocFileHeader))
196
} else if (!memcmp(&fileHeader, "Creative", 8)) {
197
if (stream.read(((byte *)&fileHeader) + 8, sizeof(VocFileHeader) - 8) != sizeof(VocFileHeader) - 8)
201
warning("loadVOCFromStream: Invalid header");
205
if (memcmp(fileHeader.desc, "Creative Voice File", 19) != 0)
206
error("loadVOCFromStream: Invalid header");
207
if (fileHeader.desc[19] != 0x1A)
208
debug(3, "loadVOCFromStream: Partially invalid header");
210
int32 offset = FROM_LE_16(fileHeader.datablock_offset);
211
int16 version = FROM_LE_16(fileHeader.version);
212
int16 code = FROM_LE_16(fileHeader.id);
213
assert(offset == sizeof(VocFileHeader));
214
// 0x100 is an invalid VOC version used by German version of DOTT (Disk) and
215
// French version of Simon the Sorcerer 2 (CD)
216
assert(version == 0x010A || version == 0x0114 || version == 0x0100);
217
assert(code == ~version + 0x1234);
224
while ((code = stream.readByte())) {
225
len = stream.readByte();
226
len |= stream.readByte() << 8;
227
len |= stream.readByte() << 16;
229
debug(2, "Block code %d, len %d", code, len);
236
int time_constant = stream.readByte();
237
packing = stream.readByte();
239
rate = getSampleRateFromVOCRate(time_constant);
241
rate = stream.readUint32LE();
242
int bits = stream.readByte();
243
int channels = stream.readByte();
244
if (bits != 8 || channels != 1) {
245
warning("Unsupported VOC file format (%d bits per sample, %d channels)", bits, channels);
248
packing = stream.readUint16LE();
249
stream.readUint32LE();
252
debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
255
// Found a data block - so add it to the block list
256
block[currentBlock].pos = stream.pos();
257
block[currentBlock].len = len;
260
stream.seek(len, SEEK_CUR);
266
warning("VOC file packing %d unsupported", packing);
270
// occur with a few Igor sounds, voc file starts with a silence block with a
271
// frequency different from the data block. Just ignore fow now (implementing
272
// it wouldn't make a big difference anyway...)
274
stream.readUint16LE();
277
case 6: // begin of loop
279
loops = stream.readUint16LE();
281
case 7: // end of loop
284
case 8: // "Extended"
285
// This occures in the LoL Intro demo. This block can usually be used to create stereo
286
// sound, but the LoL intro has only an empty block, thus this dummy implementation will
289
stream.readUint16LE();
294
warning("Unhandled code %d in VOC file (len %d)", code, len);
298
debug(4, "VOC Data Size : %d", size);
302
AudioStream *makeVOCDiskStream(Common::SeekableReadStream *stream, byte flags, DisposeAfterUse::Flag disposeAfterUse) {
303
const int MAX_AUDIO_BLOCKS = 256;
305
RawStreamBlock *block = new RawStreamBlock[MAX_AUDIO_BLOCKS];
306
int rate, loops, begin_loop, end_loop;
308
int numBlocks = parseVOCFormat(*stream, block, rate, loops, begin_loop, end_loop);
310
AudioStream *audioStream = 0;
312
// Create an audiostream from the data. Note the numBlocks may be 0,
313
// e.g. when invalid data is encountered. See bug #2890038.
315
audioStream = makeRawDiskStream_OLD(stream, block, numBlocks, rate, flags, disposeAfterUse/*, begin_loop, end_loop*/);
322
SeekableAudioStream *makeVOCDiskStreamNoLoop(Common::SeekableReadStream *stream, byte flags, DisposeAfterUse::Flag disposeAfterUse) {
323
const int MAX_AUDIO_BLOCKS = 256;
325
RawStreamBlock *block = new RawStreamBlock[MAX_AUDIO_BLOCKS];
326
int rate, loops, begin_loop, end_loop;
328
int numBlocks = parseVOCFormat(*stream, block, rate, loops, begin_loop, end_loop);
330
SeekableAudioStream *audioStream = 0;
332
// Create an audiostream from the data. Note the numBlocks may be 0,
333
// e.g. when invalid data is encountered. See bug #2890038.
335
audioStream = makeRawDiskStream_OLD(stream, block, numBlocks, rate, flags, disposeAfterUse);
345
AudioStream *makeVOCStream(Common::SeekableReadStream *stream, byte flags, uint loopStart, uint loopEnd, DisposeAfterUse::Flag disposeAfterUse) {
346
#ifdef STREAM_AUDIO_FROM_DISK
347
return makeVOCDiskStream(stream, flags, disposeAfterUse);
351
byte *data = loadVOCFromStream(*stream, size, rate);
354
if (disposeAfterUse == DisposeAfterUse::YES)
359
SeekableAudioStream *s = Audio::makeRawStream(data, size, rate, flags);
361
if (loopStart != loopEnd) {
362
const bool isStereo = (flags & Audio::FLAG_STEREO) != 0;
363
const bool is16Bit = (flags & Audio::FLAG_16BITS) != 0;
367
assert(loopStart <= loopEnd);
368
assert(loopEnd <= (uint)size);
370
// Verify the buffer sizes are sane
371
if (is16Bit && isStereo)
372
assert((loopStart & 3) == 0 && (loopEnd & 3) == 0);
373
else if (is16Bit || isStereo)
374
assert((loopStart & 1) == 0 && (loopEnd & 1) == 0);
376
const uint32 extRate = s->getRate() * (is16Bit ? 2 : 1) * (isStereo ? 2 : 1);
378
return new SubLoopingAudioStream(s, 0, Timestamp(0, loopStart, extRate), Timestamp(0, loopEnd, extRate));
385
SeekableAudioStream *makeVOCStream(Common::SeekableReadStream *stream, byte flags, DisposeAfterUse::Flag disposeAfterUse) {
386
#ifdef STREAM_AUDIO_FROM_DISK
387
return makeVOCDiskStreamNoLoop(stream, flags, disposeAfterUse);
391
byte *data = loadVOCFromStream(*stream, size, rate);
394
if (disposeAfterUse == DisposeAfterUse::YES)
399
return makeRawStream(data, size, rate, flags);
403
} // End of namespace Audio