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