Skip to content

Latest commit

 

History

History
451 lines (368 loc) · 13.6 KB

load_voc.c

File metadata and controls

451 lines (368 loc) · 13.6 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
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.
Dec 31, 2011
Dec 31, 2011
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:
Dec 31, 2011
Dec 31, 2011
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.
Dec 31, 2011
Dec 31, 2011
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.
Dec 31, 2011
Dec 31, 2011
26
This file by Ryan C. Gordon (icculus@icculus.org).
Dec 31, 2011
Dec 31, 2011
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"
Sep 11, 2001
Sep 11, 2001
33
#include "load_voc.h"
34
35
36
/* Private data for VOC file */
typedef struct vocstuff {
May 22, 2013
May 22, 2013
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;
May 22, 2013
May 22, 2013
48
/* Size field */
49
/* SJB: note that the 1st 3 are sometimes used as sizeof(type) */
May 22, 2013
May 22, 2013
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 */
May 22, 2013
May 22, 2013
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
May 22, 2013
May 22, 2013
78
#define VOC_DATA_16 9
Sep 11, 2001
Sep 11, 2001
81
static int voc_check_header(SDL_RWops *src)
82
83
84
85
86
{
/* VOC magic header */
Uint8 signature[20]; /* "Creative Voice File\032" */
Uint16 datablockofs;
Nov 8, 2009
Nov 8, 2009
87
SDL_RWseek(src, 0, RW_SEEK_SET);
88
89
90
91
if (SDL_RWread(src, signature, sizeof (signature), 1) != 1)
return(0);
Oct 18, 2017
Oct 18, 2017
92
if (SDL_memcmp(signature, "Creative Voice File\032", sizeof (signature)) != 0) {
93
94
95
96
97
98
99
100
101
102
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);
Nov 8, 2009
Nov 8, 2009
103
if (SDL_RWseek(src, datablockofs, RW_SEEK_SET) != datablockofs)
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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;
Sep 11, 2001
Sep 11, 2001
120
unsigned int i;
121
122
123
124
125
126
127
128
129
130
131
132
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. */
May 22, 2013
May 22, 2013
133
134
/* Size is an 24-bit value. Ugh. */
Oct 17, 2017
Oct 17, 2017
135
sblen = ((bits24[0]) | (bits24[1] << 8) | (bits24[2] << 16));
136
137
138
139
140
141
142
143
144
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 */
Dec 17, 2001
Dec 17, 2001
145
if (!v->has_extended)
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
if (uc == 0)
{
SDL_SetError("VOC Sample rate is zero?");
return 0;
}
if ((v->rate != -1) && (uc != v->rate))
{
SDL_SetError("VOC sample rate codes differ");
return 0;
}
v->rate = uc;
Sep 11, 2001
Sep 11, 2001
160
spec->freq = (Uint16)(1000000.0/(256 - v->rate));
161
162
163
164
165
166
167
168
169
170
171
172
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;
}
Dec 17, 2001
Dec 17, 2001
173
v->has_extended = 0;
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
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;
}
if ((v->rate != -1) && (new_rate_long != v->rate))
{
SDL_SetError("VOC sample rate codes differ");
return 0;
}
v->rate = new_rate_long;
spec->freq = new_rate_long;
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.
*/
if ((v->rate != -1) && (uc != v->rate))
May 11, 2006
May 11, 2006
239
period = (Uint16)((period * (256 - uc))/(256 - v->rate));
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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. */
Dec 17, 2001
Dec 17, 2001
260
v->has_extended = 1;
261
262
263
264
265
266
267
268
269
270
271
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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;
}
if ((v->rate != -1) && (new_rate_short != v->rate))
{
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;
/* Falling! Falling! */
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;
}
static int voc_read(SDL_RWops *src, vs_t *v, Uint8 *buf, SDL_AudioSpec *spec)
{
Oct 13, 2017
Oct 13, 2017
322
Uint32 done = 0;
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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 */
Jun 1, 2013
Jun 1, 2013
340
SDL_memset(buf, silence, v->rest);
341
342
343
344
345
346
done = v->rest;
v->rest = 0;
}
else
{
Oct 13, 2017
Oct 13, 2017
347
done = (Uint32)SDL_RWread(src, buf, 1, v->rest);
348
349
350
351
v->rest -= done;
if (v->size == ST_SIZE_WORD)
{
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
May 20, 2002
May 20, 2002
352
Uint16 *samples = (Uint16 *)buf;
353
354
for (; v->rest > 0; v->rest -= 2)
{
May 20, 2002
May 20, 2002
355
356
*samples = SDL_SwapLE16(*samples);
samples++;
357
358
}
#endif
May 20, 2002
May 20, 2002
359
done >>= 1;
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
}
}
return done;
} /* 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;
Oct 17, 2017
Oct 17, 2017
377
if ((!src) || (!audio_buf) || (!audio_len)) /* sanity checks. */
378
379
goto done;
Oct 17, 2017
Oct 17, 2017
380
if (!voc_check_header(src))
381
382
383
384
goto done;
v.rate = -1;
v.rest = 0;
Dec 17, 2001
Dec 17, 2001
385
v.has_extended = 0;
386
387
*audio_buf = NULL;
*audio_len = 0;
Jun 1, 2013
Jun 1, 2013
388
SDL_memset(spec, '\0', sizeof (SDL_AudioSpec));
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
if (!voc_get_block(src, &v, spec))
goto done;
if (v.rate == -1)
{
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;
Jan 13, 2012
Jan 13, 2012
404
*audio_buf = SDL_malloc(v.rest);
405
406
407
408
409
410
411
412
413
414
415
if (*audio_buf == NULL)
goto done;
fillptr = *audio_buf;
while (voc_read(src, &v, fillptr, spec) > 0)
{
if (!voc_get_block(src, &v, spec))
goto done;
*audio_len += v.rest;
Jan 13, 2012
Jan 13, 2012
416
ptr = SDL_realloc(*audio_buf, *audio_len);
417
418
if (ptr == NULL)
{
Jan 13, 2012
Jan 13, 2012
419
SDL_free(*audio_buf);
420
421
422
423
424
425
426
427
428
*audio_buf = NULL;
*audio_len = 0;
goto done;
}
*audio_buf = ptr;
fillptr = ((Uint8 *) ptr) + (*audio_len - v.rest);
}
May 11, 2006
May 11, 2006
429
spec->samples = (Uint16)(*audio_len / v.size);
430
431
432
433
434
435
436
437
was_error = 0; /* success, baby! */
/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF)/8)*spec->channels;
*audio_len &= ~(samplesize-1);
done:
Jun 2, 2013
Jun 2, 2013
438
439
if (freesrc && src) {
SDL_RWclose(src);
440
441
}
Jun 2, 2013
Jun 2, 2013
442
if (was_error) {
443
spec = NULL;
Jun 2, 2013
Jun 2, 2013
444
}
445
446
447
448
return(spec);
} /* Mix_LoadVOC_RW */
Sep 11, 2001
Sep 11, 2001
449
/* end of load_voc.c ... */
Oct 17, 2017
Oct 17, 2017
450
451
/* vi: set ts=4 sw=4 expandtab: */