Mercurial > SDL_sound_CoreAudio
annotate decoders/voc.c @ 95:4436f4587c9a
Updated.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 02 Oct 2001 17:23:08 +0000 |
parents | 40006625142a |
children | 6d9fdec2f708 |
rev | line source |
---|---|
14 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /* | |
21 * VOC decoder for SDL_sound. | |
22 * | |
23 * This driver handles Creative Labs VOC audio data...this is a legacy format, | |
24 * but there's some game ports that could make use of such a decoder. Plus, | |
25 * VOC is fairly straightforward to decode, so this is a more complex, but | |
26 * still palatable example of an SDL_sound decoder. Y'know, in case the | |
27 * RAW decoder didn't do it for you. :) | |
28 * | |
29 * This code was ripped from a decoder I had written for SDL_mixer, which was | |
30 * largely ripped from sox v12.17.1's voc.c. | |
31 * | |
32 * SDL_mixer: http://www.libsdl.org/projects/SDL_mixer/ | |
33 * sox: http://www.freshmeat.net/projects/sox/ | |
34 * | |
35 * Please see the file LICENSE in the source's root directory. | |
36 * | |
37 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
38 */ | |
39 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
40 #if (defined SOUND_SUPPORTS_VOC) |
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
41 |
14 | 42 #include <stdio.h> |
43 #include <stdlib.h> | |
44 #include <string.h> | |
45 #include <assert.h> | |
46 #include "SDL_sound.h" | |
47 | |
48 #define __SDL_SOUND_INTERNAL__ | |
49 #include "SDL_sound_internal.h" | |
50 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
51 static int VOC_init(void); |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
52 static void VOC_quit(void); |
14 | 53 static int VOC_open(Sound_Sample *sample, const char *ext); |
54 static void VOC_close(Sound_Sample *sample); | |
55 static Uint32 VOC_read(Sound_Sample *sample); | |
56 | |
57 const Sound_DecoderFunctions __Sound_DecoderFunctions_VOC = | |
58 { | |
59 { | |
60 "VOC", | |
61 "Creative Labs Voice format", | |
62 "Ryan C. Gordon <icculus@clutteredmind.org>", | |
63 "http://www.icculus.org/SDL_sound/" | |
64 }, | |
65 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
66 VOC_init, /* init() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
67 VOC_quit, /* quit() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
68 VOC_open, /* open() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
69 VOC_close, /* close() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
70 VOC_read /* read() method */ |
14 | 71 }; |
72 | |
73 | |
74 /* Private data for VOC file */ | |
75 typedef struct vocstuff { | |
76 Uint32 rest; /* bytes remaining in current block */ | |
77 Uint32 rate; /* rate code (byte) of this chunk */ | |
78 int silent; /* sound or silence? */ | |
79 Uint32 srate; /* rate code (byte) of silence */ | |
80 Uint32 blockseek; /* start of current output block */ | |
81 Uint32 samples; /* number of samples output */ | |
82 Uint32 size; /* word length of data */ | |
32
4a60ee42ca9a
Fixed a byte-ordering issue.
Ryan C. Gordon <icculus@icculus.org>
parents:
22
diff
changeset
|
83 Uint8 channels; /* number of sound channels */ |
14 | 84 int extended; /* Has an extended block been read? */ |
85 Uint32 bufpos; /* byte position in internal->buffer. */ | |
86 } vs_t; | |
87 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
88 /* Size field */ |
14 | 89 /* SJB: note that the 1st 3 are sometimes used as sizeof(type) */ |
90 #define ST_SIZE_BYTE 1 | |
91 #define ST_SIZE_8BIT 1 | |
92 #define ST_SIZE_WORD 2 | |
93 #define ST_SIZE_16BIT 2 | |
94 #define ST_SIZE_DWORD 4 | |
95 #define ST_SIZE_32BIT 4 | |
96 #define ST_SIZE_FLOAT 5 | |
97 #define ST_SIZE_DOUBLE 6 | |
98 #define ST_SIZE_IEEE 7 /* IEEE 80-bit floats. */ | |
99 | |
100 /* Style field */ | |
101 #define ST_ENCODING_UNSIGNED 1 /* unsigned linear: Sound Blaster */ | |
102 #define ST_ENCODING_SIGN2 2 /* signed linear 2's comp: Mac */ | |
103 #define ST_ENCODING_ULAW 3 /* U-law signed logs: US telephony, SPARC */ | |
104 #define ST_ENCODING_ALAW 4 /* A-law signed logs: non-US telephony */ | |
105 #define ST_ENCODING_ADPCM 5 /* Compressed PCM */ | |
106 #define ST_ENCODING_IMA_ADPCM 6 /* Compressed PCM */ | |
107 #define ST_ENCODING_GSM 7 /* GSM 6.10 33-byte frame lossy compression */ | |
108 | |
109 #define VOC_TERM 0 | |
110 #define VOC_DATA 1 | |
111 #define VOC_CONT 2 | |
112 #define VOC_SILENCE 3 | |
113 #define VOC_MARKER 4 | |
114 #define VOC_TEXT 5 | |
115 #define VOC_LOOP 6 | |
116 #define VOC_LOOPEND 7 | |
117 #define VOC_EXTENDED 8 | |
118 #define VOC_DATA_16 9 | |
119 | |
120 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
121 static int VOC_init(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
122 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
123 return(1); /* always succeeds. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
124 } /* VOC_init */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
125 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
126 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
127 static void VOC_quit(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
128 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
129 /* it's a no-op. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
130 } /* VOC_quit */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
131 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
32
diff
changeset
|
132 |
14 | 133 static __inline__ int voc_check_header(SDL_RWops *src) |
134 { | |
135 /* VOC magic header */ | |
136 Uint8 signature[20]; /* "Creative Voice File\032" */ | |
137 Uint16 datablockofs; | |
138 | |
139 if (SDL_RWread(src, signature, sizeof (signature), 1) != 1) | |
140 return(0); | |
141 | |
142 if (memcmp(signature, "Creative Voice File\032", sizeof (signature)) != 0) { | |
143 Sound_SetError("Unrecognized file type (not VOC)"); | |
144 return(0); | |
145 } | |
146 | |
147 /* get the offset where the first datablock is located */ | |
148 if (SDL_RWread(src, &datablockofs, sizeof (Uint16), 1) != 1) | |
149 return(0); | |
150 | |
151 datablockofs = SDL_SwapLE16(datablockofs); | |
152 | |
153 if (SDL_RWseek(src, datablockofs, SEEK_SET) != datablockofs) | |
154 return(0); | |
155 | |
156 return(1); /* success! */ | |
157 } /* voc_check_header */ | |
158 | |
159 /* !!! FIXME : Add a flag to vs_t that distinguishes EOF and Error conditions. */ | |
160 | |
161 /* Read next block header, save info, leave position at start of data */ | |
162 static int voc_get_block(Sound_Sample *sample) | |
163 { | |
164 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
165 SDL_RWops *src = internal->rw; | |
166 vs_t *v = (vs_t *) internal->decoder_private; | |
167 Uint8 bits24[3]; | |
168 Uint8 uc, block; | |
169 Uint32 sblen; | |
170 Uint16 new_rate_short; | |
171 Uint32 new_rate_long; | |
172 Uint8 trash[6]; | |
173 Uint16 period; | |
174 int i; | |
175 | |
176 v->silent = 0; | |
177 while (v->rest == 0) | |
178 { | |
179 if (SDL_RWread(src, &block, sizeof (block), 1) != 1) | |
180 return 1; /* assume that's the end of the file. */ | |
181 | |
182 if (block == VOC_TERM) | |
183 return 1; | |
184 | |
185 if (SDL_RWread(src, bits24, sizeof (bits24), 1) != 1) | |
186 return 1; /* assume that's the end of the file. */ | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
187 |
14 | 188 /* Size is an 24-bit value. Ugh. */ |
189 sblen = ( (bits24[0]) | (bits24[1] << 8) | (bits24[2] << 16) ); | |
190 | |
191 switch(block) | |
192 { | |
193 case VOC_DATA: | |
194 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1) | |
195 return 0; | |
196 | |
197 /* When DATA block preceeded by an EXTENDED */ | |
198 /* block, the DATA blocks rate value is invalid */ | |
199 if (!v->extended) | |
200 { | |
201 if (uc == 0) | |
202 { | |
203 Sound_SetError("VOC Sample rate is zero?"); | |
204 return 0; | |
205 } | |
206 | |
207 if ((v->rate != -1) && (uc != v->rate)) | |
208 { | |
209 Sound_SetError("VOC sample rate codes differ"); | |
210 return 0; | |
211 } | |
212 | |
213 v->rate = uc; | |
214 sample->actual.rate = 1000000.0/(256 - v->rate); | |
215 v->channels = 1; | |
216 } | |
217 | |
218 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1) | |
219 return 0; | |
220 | |
221 if (uc != 0) | |
222 { | |
223 Sound_SetError("VOC decoder only interprets 8-bit data"); | |
224 return 0; | |
225 } | |
226 | |
227 v->extended = 0; | |
228 v->rest = sblen - 2; | |
229 v->size = ST_SIZE_BYTE; | |
230 return 1; | |
231 | |
232 case VOC_DATA_16: | |
233 if (SDL_RWread(src, &new_rate_long, sizeof (new_rate_long), 1) != 1) | |
234 return 0; | |
235 new_rate_long = SDL_SwapLE32(new_rate_long); | |
236 if (new_rate_long == 0) | |
237 { | |
238 Sound_SetError("VOC Sample rate is zero?"); | |
239 return 0; | |
240 } | |
241 if ((v->rate != -1) && (new_rate_long != v->rate)) | |
242 { | |
243 Sound_SetError("VOC sample rate codes differ"); | |
244 return 0; | |
245 } | |
246 v->rate = new_rate_long; | |
247 sample->actual.rate = new_rate_long; | |
248 | |
249 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1) | |
250 return 0; | |
251 | |
252 switch (uc) | |
253 { | |
254 case 8: v->size = ST_SIZE_BYTE; break; | |
255 case 16: v->size = ST_SIZE_WORD; break; | |
256 default: | |
257 Sound_SetError("VOC with unknown data size"); | |
258 return 0; | |
259 } | |
260 | |
261 if (SDL_RWread(src, &v->channels, sizeof (Uint8), 1) != 1) | |
262 return 0; | |
263 | |
264 if (SDL_RWread(src, trash, sizeof (Uint8), 6) != 6) | |
265 return 0; | |
266 | |
267 v->rest = sblen - 12; | |
268 return 1; | |
269 | |
270 case VOC_CONT: | |
271 v->rest = sblen; | |
272 return 1; | |
273 | |
274 case VOC_SILENCE: | |
275 if (SDL_RWread(src, &period, sizeof (period), 1) != 1) | |
276 return 0; | |
277 period = SDL_SwapLE16(period); | |
278 | |
279 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1) | |
280 return 0; | |
281 if (uc == 0) | |
282 { | |
283 Sound_SetError("VOC silence sample rate is zero"); | |
284 return 0; | |
285 } | |
286 | |
287 /* | |
288 * Some silence-packed files have gratuitously | |
289 * different sample rate codes in silence. | |
290 * Adjust period. | |
291 */ | |
292 if ((v->rate != -1) && (uc != v->rate)) | |
293 period = (period * (256 - uc))/(256 - v->rate); | |
294 else | |
295 v->rate = uc; | |
296 v->rest = period; | |
297 v->silent = 1; | |
298 return 1; | |
299 | |
300 case VOC_LOOP: | |
301 case VOC_LOOPEND: | |
302 for(i = 0; i < sblen; i++) /* skip repeat loops. */ | |
303 { | |
304 if (SDL_RWread(src, trash, sizeof (Uint8), 1) != 1) | |
305 return 0; | |
306 } | |
307 break; | |
308 | |
309 case VOC_EXTENDED: | |
310 /* An Extended block is followed by a data block */ | |
311 /* Set this byte so we know to use the rate */ | |
312 /* value from the extended block and not the */ | |
313 /* data block. */ | |
314 v->extended = 1; | |
315 if (SDL_RWread(src, &new_rate_short, sizeof (new_rate_short), 1) != 1) | |
316 return 0; | |
317 new_rate_short = SDL_SwapLE16(new_rate_short); | |
318 if (new_rate_short == 0) | |
319 { | |
320 Sound_SetError("VOC sample rate is zero"); | |
321 return 0; | |
322 } | |
323 if ((v->rate != -1) && (new_rate_short != v->rate)) | |
324 { | |
325 Sound_SetError("VOC sample rate codes differ"); | |
326 return 0; | |
327 } | |
328 v->rate = new_rate_short; | |
329 | |
330 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1) | |
331 return 0; | |
332 | |
333 if (uc != 0) | |
334 { | |
335 Sound_SetError("VOC decoder only interprets 8-bit data"); | |
336 return 0; | |
337 } | |
338 | |
339 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1) | |
340 return 0; | |
341 | |
342 if (uc) | |
343 sample->actual.channels = 2; /* Stereo */ | |
344 /* Needed number of channels before finishing | |
345 compute for rate */ | |
346 sample->actual.rate = | |
347 (256000000L/(65536L - v->rate)) / sample->actual.channels; | |
348 /* An extended block must be followed by a data */ | |
349 /* block to be valid so loop back to top so it */ | |
350 /* can be grabed. */ | |
351 continue; | |
352 | |
353 case VOC_MARKER: | |
354 if (SDL_RWread(src, trash, sizeof (Uint8), 2) != 2) | |
355 return 0; | |
356 | |
357 /* Falling! Falling! */ | |
358 | |
359 default: /* text block or other krapola. */ | |
360 for(i = 0; i < sblen; i++) | |
361 { | |
362 if (SDL_RWread(src, &trash, sizeof (Uint8), 1) != 1) | |
363 return 0; | |
364 } | |
365 | |
366 if (block == VOC_TEXT) | |
367 continue; /* get next block */ | |
368 } | |
369 } | |
370 | |
371 return 1; | |
372 } | |
373 | |
374 | |
22
f6e679afe88b
Byte ordering fix, changed voc_read to voc_read_waveform, and cleaned up
Ryan C. Gordon <icculus@icculus.org>
parents:
14
diff
changeset
|
375 static int voc_read_waveform(Sound_Sample *sample) |
14 | 376 { |
377 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
378 SDL_RWops *src = internal->rw; | |
379 vs_t *v = (vs_t *) internal->decoder_private; | |
380 int done = 0; | |
381 Uint8 silence = 0x80; | |
382 Uint8 *buf = internal->buffer; | |
383 | |
384 if (v->rest == 0) | |
385 { | |
386 if (!voc_get_block(sample)) | |
387 return 0; | |
388 } | |
389 | |
390 if (v->rest == 0) | |
391 return 0; | |
392 | |
393 if (v->silent) | |
394 { | |
395 if (v->size == ST_SIZE_WORD) | |
396 silence = 0x00; | |
397 | |
398 /* Fill in silence */ | |
399 memset(buf, silence, v->rest); | |
400 done = v->rest; | |
401 v->rest = 0; | |
402 } | |
403 | |
404 else | |
405 { | |
406 Uint32 max = (v->rest < internal->buffer_size) ? | |
407 v->rest : internal->buffer_size; | |
408 done = SDL_RWread(src, buf + v->bufpos, 1, max); | |
409 v->rest -= done; | |
410 v->bufpos += done; | |
411 } | |
412 | |
413 return done; | |
22
f6e679afe88b
Byte ordering fix, changed voc_read to voc_read_waveform, and cleaned up
Ryan C. Gordon <icculus@icculus.org>
parents:
14
diff
changeset
|
414 } /* voc_read_waveform */ |
14 | 415 |
416 | |
417 static int VOC_open(Sound_Sample *sample, const char *ext) | |
418 { | |
419 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
420 vs_t *v = NULL; | |
421 | |
422 if (!voc_check_header(internal->rw)) | |
423 return(0); | |
424 | |
425 v = (vs_t *) malloc(sizeof (vs_t)); | |
426 BAIL_IF_MACRO(v == NULL, ERR_OUT_OF_MEMORY, 0); | |
427 memset(v, '\0', sizeof (vs_t)); | |
428 internal->decoder_private = v; | |
429 | |
430 v->rate = -1; | |
431 if (!voc_get_block(sample)) | |
432 { | |
433 free(v); | |
434 return(0); | |
435 } /* if */ | |
436 | |
437 if (v->rate == -1) | |
438 { | |
439 Sound_SetError("VOC data had no sound!"); | |
440 free(v); | |
441 return(0); | |
442 } /* if */ | |
443 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
444 SNDDBG(("VOC: Accepting data stream.\n")); |
14 | 445 sample->actual.format = (v->size == ST_SIZE_WORD) ? AUDIO_S16LSB:AUDIO_U8; |
446 sample->actual.channels = v->channels; | |
447 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
448 return(1); | |
449 } /* VOC_open */ | |
450 | |
451 | |
452 static void VOC_close(Sound_Sample *sample) | |
453 { | |
454 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
455 free(internal->decoder_private); | |
456 } /* VOC_close */ | |
457 | |
458 | |
459 static Uint32 VOC_read(Sound_Sample *sample) | |
460 { | |
461 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
462 vs_t *v = (vs_t *) internal->decoder_private; | |
463 | |
464 v->bufpos = 0; | |
465 while (v->bufpos < internal->buffer_size) | |
466 { | |
22
f6e679afe88b
Byte ordering fix, changed voc_read to voc_read_waveform, and cleaned up
Ryan C. Gordon <icculus@icculus.org>
parents:
14
diff
changeset
|
467 Uint32 rc = voc_read_waveform(sample); |
14 | 468 if (rc == 0) /* !!! FIXME: Could be an error... */ |
469 { | |
470 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
471 break; | |
472 } /* if */ | |
473 | |
474 if (!voc_get_block(sample)) | |
475 { | |
476 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
477 break; | |
478 } /* if */ | |
479 } /* while */ | |
480 | |
481 return(v->bufpos); | |
482 } /* VOC_read */ | |
483 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
484 #endif /* SOUND_SUPPORTS_VOC */ |
14 | 485 |
486 /* end of voc.c ... */ |