/
load_voc.c
452 lines (368 loc) · 13.7 KB
1
/*
2
SDL_mixer: An audio mixer library based on the SDL library
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
8
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
12
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
24
This is the source needed to decode a Creative Labs VOC file into a
waveform. It's pretty straightforward once you get going. The only
externally-callable function is Mix_LoadVOC_RW(), which is meant to
act as identically to SDL_LoadWAV_RW() as possible.
25
26
This file by Ryan C. Gordon (icculus@icculus.org).
27
28
Heavily borrowed from sox v12.17.1's voc.c.
29
30
31
32
(http://www.freshmeat.net/projects/sox/)
*/
#include "SDL_mixer.h"
33
#include "load_voc.h"
34
35
36
/* Private data for VOC file */
typedef struct vocstuff {
37
38
39
40
41
42
43
44
45
Uint32 rest; /* bytes remaining in current block */
Uint32 rate; /* rate code (byte) of this chunk */
int silent; /* sound or silence? */
Uint32 srate; /* rate code (byte) of silence */
Uint32 blockseek; /* start of current output block */
Uint32 samples; /* number of samples output */
Uint32 size; /* word length of data */
Uint8 channels; /* number of sound channels */
int has_extended; /* Has an extended block been read? */
46
47
} vs_t;
48
/* Size field */
49
/* SJB: note that the 1st 3 are sometimes used as sizeof(type) */
50
51
52
53
54
55
56
57
58
#define ST_SIZE_BYTE 1
#define ST_SIZE_8BIT 1
#define ST_SIZE_WORD 2
#define ST_SIZE_16BIT 2
#define ST_SIZE_DWORD 4
#define ST_SIZE_32BIT 4
#define ST_SIZE_FLOAT 5
#define ST_SIZE_DOUBLE 6
#define ST_SIZE_IEEE 7 /* IEEE 80-bit floats. */
59
60
/* Style field */
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#define ST_ENCODING_UNSIGNED 1 /* unsigned linear: Sound Blaster */
#define ST_ENCODING_SIGN2 2 /* signed linear 2's comp: Mac */
#define ST_ENCODING_ULAW 3 /* U-law signed logs: US telephony, SPARC */
#define ST_ENCODING_ALAW 4 /* A-law signed logs: non-US telephony */
#define ST_ENCODING_ADPCM 5 /* Compressed PCM */
#define ST_ENCODING_IMA_ADPCM 6 /* Compressed PCM */
#define ST_ENCODING_GSM 7 /* GSM 6.10 33-byte frame lossy compression */
#define VOC_TERM 0
#define VOC_DATA 1
#define VOC_CONT 2
#define VOC_SILENCE 3
#define VOC_MARKER 4
#define VOC_TEXT 5
#define VOC_LOOP 6
#define VOC_LOOPEND 7
77
#define VOC_EXTENDED 8
78
#define VOC_DATA_16 9
79
80
81
#define VOC_BAD_RATE ~((Uint32)0)
82
83
static int voc_check_header(SDL_RWops *src)
84
85
86
87
88
{
/* VOC magic header */
Uint8 signature[20]; /* "Creative Voice File\032" */
Uint16 datablockofs;
89
SDL_RWseek(src, 0, RW_SEEK_SET);
90
91
92
93
if (SDL_RWread(src, signature, sizeof (signature), 1) != 1)
return(0);
94
if (SDL_memcmp(signature, "Creative Voice File\032", sizeof (signature)) != 0) {
95
96
97
98
99
100
101
102
103
104
SDL_SetError("Unrecognized file type (not VOC)");
return(0);
}
/* get the offset where the first datablock is located */
if (SDL_RWread(src, &datablockofs, sizeof (Uint16), 1) != 1)
return(0);
datablockofs = SDL_SwapLE16(datablockofs);
105
if (SDL_RWseek(src, datablockofs, RW_SEEK_SET) != datablockofs)
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
return(0);
return(1); /* success! */
} /* voc_check_header */
/* Read next block header, save info, leave position at start of data */
static int voc_get_block(SDL_RWops *src, vs_t *v, SDL_AudioSpec *spec)
{
Uint8 bits24[3];
Uint8 uc, block;
Uint32 sblen;
Uint16 new_rate_short;
Uint32 new_rate_long;
Uint8 trash[6];
Uint16 period;
122
unsigned int i;
123
124
125
126
127
128
129
130
131
132
133
134
v->silent = 0;
while (v->rest == 0)
{
if (SDL_RWread(src, &block, sizeof (block), 1) != 1)
return 1; /* assume that's the end of the file. */
if (block == VOC_TERM)
return 1;
if (SDL_RWread(src, bits24, sizeof (bits24), 1) != 1)
return 1; /* assume that's the end of the file. */
135
136
/* Size is an 24-bit value. Ugh. */
137
sblen = (Uint32)((bits24[0]) | (bits24[1] << 8) | (bits24[2] << 16));
138
139
140
141
142
143
144
145
146
switch(block)
{
case VOC_DATA:
if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
return 0;
/* When DATA block preceeded by an EXTENDED */
/* block, the DATA blocks rate value is invalid */
147
if (!v->has_extended)
148
149
150
151
152
153
154
{
if (uc == 0)
{
SDL_SetError("VOC Sample rate is zero?");
return 0;
}
155
if ((v->rate != VOC_BAD_RATE) && (uc != v->rate))
156
157
158
159
160
161
{
SDL_SetError("VOC sample rate codes differ");
return 0;
}
v->rate = uc;
162
spec->freq = (Uint16)(1000000.0/(256 - v->rate));
163
164
165
166
167
168
169
170
171
172
173
174
v->channels = 1;
}
if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
return 0;
if (uc != 0)
{
SDL_SetError("VOC decoder only interprets 8-bit data");
return 0;
}
175
v->has_extended = 0;
176
177
178
179
180
181
182
183
184
185
186
187
188
v->rest = sblen - 2;
v->size = ST_SIZE_BYTE;
return 1;
case VOC_DATA_16:
if (SDL_RWread(src, &new_rate_long, sizeof (new_rate_long), 1) != 1)
return 0;
new_rate_long = SDL_SwapLE32(new_rate_long);
if (new_rate_long == 0)
{
SDL_SetError("VOC Sample rate is zero?");
return 0;
}
189
if ((v->rate != VOC_BAD_RATE) && (new_rate_long != v->rate))
190
191
192
193
194
{
SDL_SetError("VOC sample rate codes differ");
return 0;
}
v->rate = new_rate_long;
195
spec->freq = (int)new_rate_long;
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
return 0;
switch (uc)
{
case 8: v->size = ST_SIZE_BYTE; break;
case 16: v->size = ST_SIZE_WORD; break;
default:
SDL_SetError("VOC with unknown data size");
return 0;
}
if (SDL_RWread(src, &v->channels, sizeof (Uint8), 1) != 1)
return 0;
if (SDL_RWread(src, trash, sizeof (Uint8), 6) != 6)
return 0;
v->rest = sblen - 12;
return 1;
case VOC_CONT:
v->rest = sblen;
return 1;
case VOC_SILENCE:
if (SDL_RWread(src, &period, sizeof (period), 1) != 1)
return 0;
period = SDL_SwapLE16(period);
if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
return 0;
if (uc == 0)
{
SDL_SetError("VOC silence sample rate is zero");
return 0;
}
/*
* Some silence-packed files have gratuitously
* different sample rate codes in silence.
* Adjust period.
*/
240
if ((v->rate != VOC_BAD_RATE) && (uc != v->rate))
241
period = (Uint16)((period * (256 - uc))/(256 - v->rate));
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
else
v->rate = uc;
v->rest = period;
v->silent = 1;
return 1;
case VOC_LOOP:
case VOC_LOOPEND:
for(i = 0; i < sblen; i++) /* skip repeat loops. */
{
if (SDL_RWread(src, trash, sizeof (Uint8), 1) != 1)
return 0;
}
break;
case VOC_EXTENDED:
/* An Extended block is followed by a data block */
/* Set this byte so we know to use the rate */
/* value from the extended block and not the */
/* data block. */
262
v->has_extended = 1;
263
264
265
266
267
268
269
270
if (SDL_RWread(src, &new_rate_short, sizeof (new_rate_short), 1) != 1)
return 0;
new_rate_short = SDL_SwapLE16(new_rate_short);
if (new_rate_short == 0)
{
SDL_SetError("VOC sample rate is zero");
return 0;
}
271
if ((v->rate != VOC_BAD_RATE) && (new_rate_short != v->rate))
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{
SDL_SetError("VOC sample rate codes differ");
return 0;
}
v->rate = new_rate_short;
if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
return 0;
if (uc != 0)
{
SDL_SetError("VOC decoder only interprets 8-bit data");
return 0;
}
if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
return 0;
if (uc)
spec->channels = 2; /* Stereo */
/* Needed number of channels before finishing
compute for rate */
spec->freq = (256000000L/(65536L - v->rate))/spec->channels;
/* An extended block must be followed by a data */
/* block to be valid so loop back to top so it */
/* can be grabed. */
continue;
case VOC_MARKER:
if (SDL_RWread(src, trash, sizeof (Uint8), 2) != 2)
return 0;
304
/* fallthrough */
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
default: /* text block or other krapola. */
for(i = 0; i < sblen; i++)
{
if (SDL_RWread(src, &trash, sizeof (Uint8), 1) != 1)
return 0;
}
if (block == VOC_TEXT)
continue; /* get next block */
}
}
return 1;
}
322
static Uint32 voc_read(SDL_RWops *src, vs_t *v, Uint8 *buf, SDL_AudioSpec *spec)
323
{
324
Uint32 done = 0;
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
Uint8 silence = 0x80;
if (v->rest == 0)
{
if (!voc_get_block(src, v, spec))
return 0;
}
if (v->rest == 0)
return 0;
if (v->silent)
{
if (v->size == ST_SIZE_WORD)
silence = 0x00;
/* Fill in silence */
342
SDL_memset(buf, silence, v->rest);
343
344
345
346
347
348
done = v->rest;
v->rest = 0;
}
else
{
349
done = (Uint32)SDL_RWread(src, buf, 1, v->rest);
350
351
352
353
v->rest -= done;
if (v->size == ST_SIZE_WORD)
{
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
354
355
356
357
358
359
Uint16 *samples = (Uint16 *)buf;
for (; v->rest > 0; v->rest -= 2)
{
*samples = SDL_SwapLE16(*samples);
samples++;
}
360
#endif
361
done >>= 1;
362
363
364
}
}
365
return done;
366
367
368
369
370
371
372
373
374
375
376
377
378
} /* voc_read */
/* don't call this directly; use Mix_LoadWAV_RW() for now. */
SDL_AudioSpec *Mix_LoadVOC_RW (SDL_RWops *src, int freesrc,
SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
{
vs_t v;
int was_error = 1;
int samplesize;
Uint8 *fillptr;
void *ptr;
379
if ((!src) || (!audio_buf) || (!audio_len)) /* sanity checks. */
380
381
goto done;
382
if (!voc_check_header(src))
383
384
goto done;
385
v.rate = VOC_BAD_RATE;
386
v.rest = 0;
387
v.has_extended = 0;
388
389
*audio_buf = NULL;
*audio_len = 0;
390
SDL_memset(spec, '\0', sizeof (SDL_AudioSpec));
391
392
393
394
if (!voc_get_block(src, &v, spec))
goto done;
395
if (v.rate == VOC_BAD_RATE) {
396
397
398
399
400
401
402
403
404
SDL_SetError("VOC data had no sound!");
goto done;
}
spec->format = ((v.size == ST_SIZE_WORD) ? AUDIO_S16 : AUDIO_U8);
if (spec->channels == 0)
spec->channels = v.channels;
*audio_len = v.rest;
405
*audio_buf = SDL_malloc(v.rest);
406
407
408
409
410
if (*audio_buf == NULL)
goto done;
fillptr = *audio_buf;
411
while (voc_read(src, &v, fillptr, spec))
412
413
414
415
416
{
if (!voc_get_block(src, &v, spec))
goto done;
*audio_len += v.rest;
417
ptr = SDL_realloc(*audio_buf, *audio_len);
418
419
if (ptr == NULL)
{
420
SDL_free(*audio_buf);
421
422
423
424
425
426
427
428
429
*audio_buf = NULL;
*audio_len = 0;
goto done;
}
*audio_buf = ptr;
fillptr = ((Uint8 *) ptr) + (*audio_len - v.rest);
}
430
spec->samples = (Uint16)(*audio_len / v.size);
431
432
433
434
435
was_error = 0; /* success, baby! */
/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF)/8)*spec->channels;
436
*audio_len &= (Uint32) ~(samplesize-1);
437
438
done:
439
440
if (freesrc && src) {
SDL_RWclose(src);
441
442
}
443
if (was_error) {
444
spec = NULL;
445
}
446
447
448
449
return(spec);
} /* Mix_LoadVOC_RW */
450
/* end of load_voc.c ... */
451
452
/* vi: set ts=4 sw=4 expandtab: */