forked from native-toolkit/libsdl-mixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_voc.c
459 lines (374 loc) · 13.7 KB
/
load_voc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2016 Sam Lantinga <[email protected]>
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.
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:
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.
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.
This file by Ryan C. Gordon ([email protected]).
Heavily borrowed from sox v12.17.1's voc.c.
(http://www.freshmeat.net/projects/sox/)
*/
/* $Id$ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL_mutex.h"
#include "SDL_endian.h"
#include "SDL_timer.h"
#include "SDL_mixer.h"
#include "load_voc.h"
/* Private data for VOC file */
typedef struct vocstuff {
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? */
} vs_t;
/* Size field */
/* SJB: note that the 1st 3 are sometimes used as sizeof(type) */
#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. */
/* Style field */
#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
#define VOC_EXTENDED 8
#define VOC_DATA_16 9
static int voc_check_header(SDL_RWops *src)
{
/* VOC magic header */
Uint8 signature[20]; /* "Creative Voice File\032" */
Uint16 datablockofs;
SDL_RWseek(src, 0, RW_SEEK_SET);
if (SDL_RWread(src, signature, sizeof (signature), 1) != 1)
return(0);
if (memcmp(signature, "Creative Voice File\032", sizeof (signature)) != 0) {
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);
if (SDL_RWseek(src, datablockofs, RW_SEEK_SET) != datablockofs)
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;
unsigned int i;
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. */
/* Size is an 24-bit value. Ugh. */
sblen = ( (bits24[0]) | (bits24[1] << 8) | (bits24[2] << 16) );
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 */
if (!v->has_extended)
{
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;
spec->freq = (Uint16)(1000000.0/(256 - v->rate));
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;
}
v->has_extended = 0;
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))
period = (Uint16)((period * (256 - uc))/(256 - v->rate));
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. */
v->has_extended = 1;
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)
{
int done = 0;
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 */
SDL_memset(buf, silence, v->rest);
done = v->rest;
v->rest = 0;
}
else
{
done = SDL_RWread(src, buf, 1, v->rest);
v->rest -= done;
if (v->size == ST_SIZE_WORD)
{
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
Uint16 *samples = (Uint16 *)buf;
for (; v->rest > 0; v->rest -= 2)
{
*samples = SDL_SwapLE16(*samples);
samples++;
}
#endif
done >>= 1;
}
}
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;
if ( (!src) || (!audio_buf) || (!audio_len) ) /* sanity checks. */
goto done;
if ( !voc_check_header(src) )
goto done;
v.rate = -1;
v.rest = 0;
v.has_extended = 0;
*audio_buf = NULL;
*audio_len = 0;
SDL_memset(spec, '\0', sizeof (SDL_AudioSpec));
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;
*audio_buf = SDL_malloc(v.rest);
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;
ptr = SDL_realloc(*audio_buf, *audio_len);
if (ptr == NULL)
{
SDL_free(*audio_buf);
*audio_buf = NULL;
*audio_len = 0;
goto done;
}
*audio_buf = ptr;
fillptr = ((Uint8 *) ptr) + (*audio_len - v.rest);
}
spec->samples = (Uint16)(*audio_len / v.size);
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:
if (freesrc && src) {
SDL_RWclose(src);
}
if (was_error) {
spec = NULL;
}
return(spec);
} /* Mix_LoadVOC_RW */
/* end of load_voc.c ... */