~ubuntu-branches/ubuntu/raring/scummvm/raring

« back to all changes in this revision

Viewing changes to audio/decoders/voc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Moritz Muehlenhoff
  • Date: 2011-05-25 19:02:23 UTC
  • mto: (21.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20110525190223-fiqm0oaec714xk31
Tags: upstream-1.3.0
ImportĀ upstreamĀ versionĀ 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ScummVM - Graphic Adventure Engine
 
2
 *
 
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.
 
6
 *
 
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.
 
11
 
 
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.
 
16
 
 
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.
 
20
 *
 
21
 * $URL$
 
22
 * $Id$
 
23
 *
 
24
 */
 
25
 
 
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"
 
31
 
 
32
#include "audio/audiostream.h"
 
33
#include "audio/decoders/raw.h"
 
34
#include "audio/decoders/voc.h"
 
35
 
 
36
 
 
37
namespace Audio {
 
38
 
 
39
int getSampleRateFromVOCRate(int vocSR) {
 
40
        if (vocSR == 0xa5 || vocSR == 0xa6) {
 
41
                return 11025;
 
42
        } else if (vocSR == 0xd2 || vocSR == 0xd3) {
 
43
                return 22050;
 
44
        } else {
 
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);
 
49
                return sr;
 
50
        }
 
51
}
 
52
 
 
53
static byte *loadVOCFromStream(Common::ReadStream &stream, int &size, int &rate, int &loops, int &begin_loop, int &end_loop) {
 
54
        VocFileHeader fileHeader;
 
55
 
 
56
        debug(2, "loadVOCFromStream");
 
57
 
 
58
        if (stream.read(&fileHeader, 8) != 8)
 
59
                goto invalid;
 
60
 
 
61
        if (!memcmp(&fileHeader, "VTLK", 4)) {
 
62
                if (stream.read(&fileHeader, sizeof(VocFileHeader)) != sizeof(VocFileHeader))
 
63
                        goto invalid;
 
64
        } else if (!memcmp(&fileHeader, "Creative", 8)) {
 
65
                if (stream.read(((byte *)&fileHeader) + 8, sizeof(VocFileHeader) - 8) != sizeof(VocFileHeader) - 8)
 
66
                        goto invalid;
 
67
        } else {
 
68
        invalid:;
 
69
                warning("loadVOCFromStream: Invalid header");
 
70
                return NULL;
 
71
        }
 
72
 
 
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");
 
77
 
 
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);
 
86
 
 
87
        int len;
 
88
        byte *ret_sound = 0;
 
89
        size = 0;
 
90
        begin_loop = 0;
 
91
        end_loop = 0;
 
92
 
 
93
        while ((code = stream.readByte())) {
 
94
                len = stream.readByte();
 
95
                len |= stream.readByte() << 8;
 
96
                len |= stream.readByte() << 16;
 
97
 
 
98
                debug(2, "Block code %d, len %d", code, len);
 
99
 
 
100
                switch (code) {
 
101
                case 1:
 
102
                case 9: {
 
103
                        int packing;
 
104
                        if (code == 1) {
 
105
                                int time_constant = stream.readByte();
 
106
                                packing = stream.readByte();
 
107
                                len -= 2;
 
108
                                rate = getSampleRateFromVOCRate(time_constant);
 
109
                        } else {
 
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);
 
115
                                        break;
 
116
                                }
 
117
                                packing = stream.readUint16LE();
 
118
                                stream.readUint32LE();
 
119
                                len -= 12;
 
120
                        }
 
121
                        debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
 
122
                        if (packing == 0) {
 
123
                                if (size) {
 
124
                                        ret_sound = (byte *)realloc(ret_sound, size + len);
 
125
                                } else {
 
126
                                        ret_sound = (byte *)malloc(len);
 
127
                                }
 
128
                                stream.read(ret_sound + size, len);
 
129
                                size += len;
 
130
                                begin_loop = size;
 
131
                                end_loop = size;
 
132
                        } else {
 
133
                                warning("VOC file packing %d unsupported", packing);
 
134
                        }
 
135
                        } break;
 
136
                case 3: // silence
 
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...)
 
140
                        assert(len == 3);
 
141
                        stream.readUint16LE();
 
142
                        stream.readByte();
 
143
                        break;
 
144
                case 6: // begin of loop
 
145
                        assert(len == 2);
 
146
                        loops = stream.readUint16LE();
 
147
                        break;
 
148
                case 7: // end of loop
 
149
                        assert(len == 0);
 
150
                        break;
 
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))
 
160
                        assert(len == 4);
 
161
                        stream.readUint16LE();
 
162
                        uint8 codec = stream.readByte();
 
163
                        uint8 channels = stream.readByte() + 1;
 
164
                        assert(codec == 0 && channels == 1);
 
165
                        } break;
 
166
                default:
 
167
                        warning("Unhandled code %d in VOC file (len %d)", code, len);
 
168
                        return ret_sound;
 
169
                }
 
170
        }
 
171
        debug(4, "VOC Data Size : %d", size);
 
172
        return ret_sound;
 
173
}
 
174
 
 
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);
 
178
}
 
179
 
 
180
 
 
181
#ifdef STREAM_AUDIO_FROM_DISK
 
182
 
 
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;
 
186
        int size = 0;
 
187
 
 
188
        debug(2, "parseVOCFormat");
 
189
 
 
190
        if (stream.read(&fileHeader, 8) != 8)
 
191
                goto invalid;
 
192
 
 
193
        if (!memcmp(&fileHeader, "VTLK", 4)) {
 
194
                if (stream.read(&fileHeader, sizeof(VocFileHeader)) != sizeof(VocFileHeader))
 
195
                        goto invalid;
 
196
        } else if (!memcmp(&fileHeader, "Creative", 8)) {
 
197
                if (stream.read(((byte *)&fileHeader) + 8, sizeof(VocFileHeader) - 8) != sizeof(VocFileHeader) - 8)
 
198
                        goto invalid;
 
199
        } else {
 
200
        invalid:;
 
201
                warning("loadVOCFromStream: Invalid header");
 
202
                return 0;
 
203
        }
 
204
 
 
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");
 
209
 
 
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);
 
218
 
 
219
        int len;
 
220
        size = 0;
 
221
        begin_loop = 0;
 
222
        end_loop = 0;
 
223
 
 
224
        while ((code = stream.readByte())) {
 
225
                len = stream.readByte();
 
226
                len |= stream.readByte() << 8;
 
227
                len |= stream.readByte() << 16;
 
228
 
 
229
                debug(2, "Block code %d, len %d", code, len);
 
230
 
 
231
                switch (code) {
 
232
                case 1:
 
233
                case 9: {
 
234
                        int packing;
 
235
                        if (code == 1) {
 
236
                                int time_constant = stream.readByte();
 
237
                                packing = stream.readByte();
 
238
                                len -= 2;
 
239
                                rate = getSampleRateFromVOCRate(time_constant);
 
240
                        } else {
 
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);
 
246
                                        break;
 
247
                                }
 
248
                                packing = stream.readUint16LE();
 
249
                                stream.readUint32LE();
 
250
                                len -= 12;
 
251
                        }
 
252
                        debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
 
253
                        if (packing == 0) {
 
254
 
 
255
                                // Found a data block - so add it to the block list
 
256
                                block[currentBlock].pos = stream.pos();
 
257
                                block[currentBlock].len = len;
 
258
                                currentBlock++;
 
259
 
 
260
                                stream.seek(len, SEEK_CUR);
 
261
 
 
262
                                size += len;
 
263
                                begin_loop = size;
 
264
                                end_loop = size;
 
265
                        } else {
 
266
                                warning("VOC file packing %d unsupported", packing);
 
267
                        }
 
268
                        } break;
 
269
                case 3: // silence
 
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...)
 
273
                        assert(len == 3);
 
274
                        stream.readUint16LE();
 
275
                        stream.readByte();
 
276
                        break;
 
277
                case 6: // begin of loop
 
278
                        assert(len == 2);
 
279
                        loops = stream.readUint16LE();
 
280
                        break;
 
281
                case 7: // end of loop
 
282
                        assert(len == 0);
 
283
                        break;
 
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
 
287
                        // work.
 
288
                        assert(len == 4);
 
289
                        stream.readUint16LE();
 
290
                        stream.readByte();
 
291
                        stream.readByte();
 
292
                        break;
 
293
                default:
 
294
                        warning("Unhandled code %d in VOC file (len %d)", code, len);
 
295
                        return 0;
 
296
                }
 
297
        }
 
298
        debug(4, "VOC Data Size : %d", size);
 
299
        return currentBlock;
 
300
}
 
301
 
 
302
AudioStream *makeVOCDiskStream(Common::SeekableReadStream *stream, byte flags, DisposeAfterUse::Flag disposeAfterUse) {
 
303
        const int MAX_AUDIO_BLOCKS = 256;
 
304
 
 
305
        RawStreamBlock *block = new RawStreamBlock[MAX_AUDIO_BLOCKS];
 
306
        int rate, loops, begin_loop, end_loop;
 
307
 
 
308
        int numBlocks = parseVOCFormat(*stream, block, rate, loops, begin_loop, end_loop);
 
309
 
 
310
        AudioStream *audioStream = 0;
 
311
 
 
312
        // Create an audiostream from the data. Note the numBlocks may be 0,
 
313
        // e.g. when invalid data is encountered. See bug #2890038.
 
314
        if (numBlocks)
 
315
                audioStream = makeRawDiskStream_OLD(stream, block, numBlocks, rate, flags, disposeAfterUse/*, begin_loop, end_loop*/);
 
316
 
 
317
        delete[] block;
 
318
 
 
319
        return audioStream;
 
320
}
 
321
 
 
322
SeekableAudioStream *makeVOCDiskStreamNoLoop(Common::SeekableReadStream *stream, byte flags, DisposeAfterUse::Flag disposeAfterUse) {
 
323
        const int MAX_AUDIO_BLOCKS = 256;
 
324
 
 
325
        RawStreamBlock *block = new RawStreamBlock[MAX_AUDIO_BLOCKS];
 
326
        int rate, loops, begin_loop, end_loop;
 
327
 
 
328
        int numBlocks = parseVOCFormat(*stream, block, rate, loops, begin_loop, end_loop);
 
329
 
 
330
        SeekableAudioStream *audioStream = 0;
 
331
 
 
332
        // Create an audiostream from the data. Note the numBlocks may be 0,
 
333
        // e.g. when invalid data is encountered. See bug #2890038.
 
334
        if (numBlocks)
 
335
                audioStream = makeRawDiskStream_OLD(stream, block, numBlocks, rate, flags, disposeAfterUse);
 
336
 
 
337
        delete[] block;
 
338
 
 
339
        return audioStream;
 
340
}
 
341
 
 
342
#endif
 
343
 
 
344
 
 
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);
 
348
#else
 
349
        int size, rate;
 
350
 
 
351
        byte *data = loadVOCFromStream(*stream, size, rate);
 
352
 
 
353
        if (!data) {
 
354
                if (disposeAfterUse == DisposeAfterUse::YES)
 
355
                        delete stream;
 
356
                return 0;
 
357
        }
 
358
 
 
359
        SeekableAudioStream *s = Audio::makeRawStream(data, size, rate, flags);
 
360
 
 
361
        if (loopStart != loopEnd) {
 
362
                const bool isStereo   = (flags & Audio::FLAG_STEREO) != 0;
 
363
                const bool is16Bit    = (flags & Audio::FLAG_16BITS) != 0;
 
364
 
 
365
                if (loopEnd == 0)
 
366
                        loopEnd = size;
 
367
                assert(loopStart <= loopEnd);
 
368
                assert(loopEnd <= (uint)size);
 
369
 
 
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);
 
375
 
 
376
                const uint32 extRate = s->getRate() * (is16Bit ? 2 : 1) * (isStereo ? 2 : 1);
 
377
 
 
378
                return new SubLoopingAudioStream(s, 0, Timestamp(0, loopStart, extRate), Timestamp(0, loopEnd, extRate));
 
379
        } else {
 
380
                return s;
 
381
        }
 
382
#endif
 
383
}
 
384
 
 
385
SeekableAudioStream *makeVOCStream(Common::SeekableReadStream *stream, byte flags, DisposeAfterUse::Flag disposeAfterUse) {
 
386
#ifdef STREAM_AUDIO_FROM_DISK
 
387
        return makeVOCDiskStreamNoLoop(stream, flags, disposeAfterUse);
 
388
#else
 
389
        int size, rate;
 
390
 
 
391
        byte *data = loadVOCFromStream(*stream, size, rate);
 
392
 
 
393
        if (!data) {
 
394
                if (disposeAfterUse == DisposeAfterUse::YES)
 
395
                        delete stream;
 
396
                return 0;
 
397
        }
 
398
 
 
399
        return makeRawStream(data, size, rate, flags);
 
400
#endif
 
401
}
 
402
 
 
403
} // End of namespace Audio