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