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