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