Debian Codegraph @

addons/acodec/wav.c

raw
1 /*
2 * Allegro5 WAV reader
3 * author: Matthew Leverton
4 */
5
6 #include <stdio.h>
7
8 #include "allegro5/allegro_audio.h"
9 #include "allegro5/internal/aintern_audio.h"
10 #include "acodec.h"
11 #include "helper.h"
12
13 ALLEGRO_DEBUG_CHANNEL("wav")
14
15
16 typedef struct WAVFILE
17 {
18 ALLEGRO_FILE *f;
19 size_t dpos; /* the starting position of the data chunk */
20 int freq; /* e.g., 44100 */
21 short bits; /* 8 (unsigned char) or 16 (signed short) */
22 short channels; /* 1 (mono) or 2 (stereo) */
23 int sample_size; /* channels * bits/8 */
24 int samples; /* # of samples. size = samples * sample_size */
25 double loop_start;
26 double loop_end;
27 } WAVFILE;
28
29
30 /* wav_open:
31 * Opens f and prepares a WAVFILE struct with the WAV format info.
32 * On a successful return, the ALLEGRO_FILE is at the beginning of the sample data.
33 * returns the WAVFILE on success, or NULL on failure.
34 */
35 static WAVFILE *wav_open(ALLEGRO_FILE *f)
36 {
37 WAVFILE *wavfile = NULL;
38 char buffer[12];
39
40 if (!f)
41 goto wav_open_error;
42
43 /* prepare default values */
44 wavfile = al_malloc(sizeof(WAVFILE));
45 if (!wavfile) {
46 ALLEGRO_ERROR("Failed to allocate WAVFILE.\n");
47 return NULL;
48 }
49 wavfile->f = f;
50 wavfile->freq = 22050;
51 wavfile->bits = 8;
52 wavfile->channels = 1;
53
54 /* check the header */
55 if (al_fread(f, buffer, 12) != 12) {
56 ALLEGRO_ERROR("Unexpected EOF while reading the header.\n");
57 goto wav_open_error;
58 }
59
60 if (memcmp(buffer, "RIFF", 4) || memcmp(buffer+8, "WAVE", 4)) {
61 ALLEGRO_ERROR("Bad magic number.\n");
62 goto wav_open_error;
63 }
64
65 /* Read as many leading fmt chunks as exist, then read until a data chunk
66 * is found.
67 */
68 while (true) {
69 int length = 0;
70 short pcm = 0;
71
72 if (al_fread(f, buffer, 4) != 4) {
73 ALLEGRO_ERROR("Unexpected EOF while reading RIFF type.\n");
74 goto wav_open_error;
75 }
76
77 /* check to see if it's a fmt chunk */
78 if (!memcmp(buffer, "fmt ", 4)) {
79
80 length = al_fread32le(f);
81 if (length < 16) {
82 ALLEGRO_ERROR("Bad length: %d.\n", length);
83 goto wav_open_error;
84 }
85
86 /* should be 1 for PCM data */
87 pcm = al_fread16le(f);
88 if (pcm != 1) {
89 ALLEGRO_ERROR("Bad PCM value: %d.\n", pcm);
90 goto wav_open_error;
91 }
92
93 /* mono or stereo data */
94 wavfile->channels = al_fread16le(f);
95
96 if ((wavfile->channels != 1) && (wavfile->channels != 2)) {
97 ALLEGRO_ERROR("Bad number of channels: %d.\n", wavfile->channels);
98 goto wav_open_error;
99 }
100
101 /* sample frequency */
102 wavfile->freq = al_fread32le(f);
103
104 /* skip six bytes */
105 al_fseek(f, 6, ALLEGRO_SEEK_CUR);
106
107 /* 8 or 16 bit data? */
108 wavfile->bits = al_fread16le(f);
109 if ((wavfile->bits != 8) && (wavfile->bits != 16)) {
110 ALLEGRO_ERROR("Bad number of bits: %d.\n", wavfile->bits);
111 goto wav_open_error;
112 }
113
114 /* Skip remainder of chunk */
115 length -= 16;
116 if (length > 0)
117 al_fseek(f, length, ALLEGRO_SEEK_CUR);
118 }
119 else {
120 if (!memcmp(buffer, "data", 4)) {
121 ALLEGRO_ERROR("Bad RIFF type.\n");
122 break;
123 }
124 ALLEGRO_INFO("Ignoring chunk: %c%c%c%c\n", buffer[0], buffer[1],
125 buffer[2], buffer[3]);
126 length = al_fread32le(f);
127 al_fseek(f, length, ALLEGRO_SEEK_CUR);
128 }
129 }
130
131 /* find out how many samples exist */
132 wavfile->samples = al_fread32le(f);
133
134 if (wavfile->channels == 2) {
135 wavfile->samples = (wavfile->samples + 1) / 2;
136 }
137
138 if (wavfile->bits == 16) {
139 wavfile->samples /= 2;
140 }
141
142 wavfile->sample_size = wavfile->channels * wavfile->bits / 8;
143
144 wavfile->dpos = al_ftell(f);
145
146 return wavfile;
147
148 wav_open_error:
149
150 if (wavfile)
151 al_free(wavfile);
152
153 return NULL;
154 }
155
156 /* wav_read:
157 * Reads up to 'samples' number of samples from the wav ALLEGRO_FILE into 'data'.
158 * Returns the actual number of samples written to 'data'.
159 */
160 static size_t wav_read(WAVFILE *wavfile, void *data, size_t samples)
161 {
162 size_t bytes_read;
163 size_t cur_samples;
164
165 ASSERT(wavfile);
166
167 cur_samples = (al_ftell(wavfile->f) - wavfile->dpos) / wavfile->sample_size;
168 if (cur_samples + samples > (size_t)wavfile->samples)
169 samples = wavfile->samples - cur_samples;
170
171 bytes_read = al_fread(wavfile->f, data, samples * wavfile->sample_size);
172
173 /* PCM data in RIFF WAV files is little endian.
174 * PCM data in RIFX WAV files is big endian (which we don't support).
175 */
176 #ifdef ALLEGRO_BIG_ENDIAN
177 if (wavfile->bits == 16) {
178 uint16_t *p = data;
179 const uint16_t *const end = p + (bytes_read >> 1);
180
181 /* swap high/low bytes */
182 while (p < end) {
183 *p = ((*p << 8) | (*p >> 8));
184 p++;
185 }
186 }
187 #endif
188
189 return bytes_read / wavfile->sample_size;
190 }
191
192
193 /* wav_close:
194 * Closes the ALLEGRO_FILE and frees the WAVFILE struct.
195 */
196 static void wav_close(WAVFILE *wavfile)
197 {
198 ASSERT(wavfile);
199
200 al_free(wavfile);
201 }
202
203
204 static bool wav_stream_seek(ALLEGRO_AUDIO_STREAM * stream, double time)
205 {
206 WAVFILE *wavfile = (WAVFILE *) stream->extra;
207 int align = (wavfile->bits / 8) * wavfile->channels;
208 unsigned long cpos = time * (double)(wavfile->freq * (wavfile->bits / 8) * wavfile->channels);
209 if (time >= wavfile->loop_end)
210 return false;
211 cpos += cpos % align;
212 return al_fseek(wavfile->f, wavfile->dpos + cpos, ALLEGRO_SEEK_SET);
213 }
214
215
216 /* wav_stream_rewind:
217 * Rewinds 'stream' to the beginning of the data chunk.
218 * Returns true on success, false on failure.
219 */
220 static bool wav_stream_rewind(ALLEGRO_AUDIO_STREAM *stream)
221 {
222 WAVFILE *wavfile = (WAVFILE *) stream->extra;
223 return wav_stream_seek(stream, wavfile->loop_start);
224 }
225
226
227 static double wav_stream_get_position(ALLEGRO_AUDIO_STREAM * stream)
228 {
229 WAVFILE *wavfile = (WAVFILE *) stream->extra;
230 double samples_per = (double)((wavfile->bits / 8) * wavfile->channels) * (double)(wavfile->freq);
231 return ((double)(al_ftell(wavfile->f) - wavfile->dpos) / samples_per);
232 }
233
234
235 static double wav_stream_get_length(ALLEGRO_AUDIO_STREAM * stream)
236 {
237 WAVFILE *wavfile = (WAVFILE *) stream->extra;
238 double total_time = (double)(wavfile->samples) / (double)(wavfile->freq);
239 return total_time;
240 }
241
242
243 static bool wav_stream_set_loop(ALLEGRO_AUDIO_STREAM * stream, double start, double end)
244 {
245 WAVFILE *wavfile = (WAVFILE *) stream->extra;
246 wavfile->loop_start = start;
247 wavfile->loop_end = end;
248 return true;
249 }
250
251
252 /* wav_stream_update:
253 * Updates 'stream' with the next chunk of data.
254 * Returns the actual number of bytes written.
255 */
256 static size_t wav_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
257 size_t buf_size)
258 {
259 int bytes_per_sample, samples, samples_read;
260 double ctime, btime;
261
262 WAVFILE *wavfile = (WAVFILE *) stream->extra;
263 bytes_per_sample = (wavfile->bits / 8) * wavfile->channels;
264 ctime = wav_stream_get_position(stream);
265 btime = ((double)buf_size / (double)bytes_per_sample) / (double)(wavfile->freq);
266
267 if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && ctime + btime > wavfile->loop_end) {
268 samples = ((wavfile->loop_end - ctime) * (double)(wavfile->freq));
269 }
270 else {
271 samples = buf_size / bytes_per_sample;
272 }
273 if (samples < 0)
274 return 0;
275
276 samples_read = wav_read(wavfile, data, samples);
277
278 return samples_read * bytes_per_sample;
279 }
280
281
282 /* wav_stream_close:
283 * Closes the 'stream'.
284 */
285 static void wav_stream_close(ALLEGRO_AUDIO_STREAM *stream)
286 {
287 WAVFILE *wavfile = (WAVFILE *) stream->extra;
288
289 _al_acodec_stop_feed_thread(stream);
290
291 al_fclose(wavfile->f);
292 wav_close(wavfile);
293 stream->extra = NULL;
294 stream->feed_thread = NULL;
295 }
296
297
298 /* _al_load_wav:
299 * Reads a RIFF WAV format sample ALLEGRO_FILE, returning an ALLEGRO_SAMPLE
300 * structure, or NULL on error.
301 */
302 ALLEGRO_SAMPLE *_al_load_wav(const char *filename)
303 {
304 ALLEGRO_FILE *f;
305 ALLEGRO_SAMPLE *spl;
306 ASSERT(filename);
307
308 f = al_fopen(filename, "rb");
309 if (!f) {
310 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
311 return NULL;
312 }
313
314 spl = _al_load_wav_f(f);
315
316 al_fclose(f);
317
318 return spl;
319 }
320
321 ALLEGRO_SAMPLE *_al_load_wav_f(ALLEGRO_FILE *fp)
322 {
323 WAVFILE *wavfile = wav_open(fp);
324 ALLEGRO_SAMPLE *spl = NULL;
325
326 if (wavfile) {
327 size_t n = (wavfile->bits / 8) * wavfile->channels * wavfile->samples;
328 char *data = al_malloc(n);
329
330 if (data) {
331 spl = al_create_sample(data, wavfile->samples, wavfile->freq,
332 _al_word_size_to_depth_conf(wavfile->bits / 8),
333 _al_count_to_channel_conf(wavfile->channels), true);
334
335 if (spl) {
336 memset(data, 0, n);
337 wav_read(wavfile, data, wavfile->samples);
338 }
339 else {
340 al_free(data);
341 }
342 }
343 wav_close(wavfile);
344 }
345
346 return spl;
347 }
348
349
350 /* _al_load_wav_audio_stream:
351 */
352 ALLEGRO_AUDIO_STREAM *_al_load_wav_audio_stream(const char *filename,
353 size_t buffer_count, unsigned int samples)
354 {
355 ALLEGRO_FILE *f;
356 ALLEGRO_AUDIO_STREAM *stream;
357 ASSERT(filename);
358
359 f = al_fopen(filename, "rb");
360 if (!f) {
361 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
362 return NULL;
363 }
364
365 stream = _al_load_wav_audio_stream_f(f, buffer_count, samples);
366 if (!stream) {
367 ALLEGRO_ERROR("Failed to load wav stream.\n");
368 al_fclose(f);
369 }
370
371 return stream;
372 }
373
374 /* _al_load_wav_audio_stream_f:
375 */
376 ALLEGRO_AUDIO_STREAM *_al_load_wav_audio_stream_f(ALLEGRO_FILE* f,
377 size_t buffer_count, unsigned int samples)
378 {
379 WAVFILE* wavfile;
380 ALLEGRO_AUDIO_STREAM* stream;
381
382 wavfile = wav_open(f);
383
384 if (wavfile == NULL) {
385 ALLEGRO_ERROR("Failed to load wav file.\n");
386 return NULL;
387 }
388
389 stream = al_create_audio_stream(buffer_count, samples, wavfile->freq,
390 _al_word_size_to_depth_conf(wavfile->bits / 8),
391 _al_count_to_channel_conf(wavfile->channels));
392
393 if (stream) {
394 stream->extra = wavfile;
395 wavfile->loop_start = 0.0;
396 wavfile->loop_end = wav_stream_get_length(stream);
397 stream->feeder = wav_stream_update;
398 stream->unload_feeder = wav_stream_close;
399 stream->rewind_feeder = wav_stream_rewind;
400 stream->seek_feeder = wav_stream_seek;
401 stream->get_feeder_position = wav_stream_get_position;
402 stream->get_feeder_length = wav_stream_get_length;
403 stream->set_feeder_loop = wav_stream_set_loop;
404 _al_acodec_start_feed_thread(stream);
405 }
406 else {
407 ALLEGRO_ERROR("Failed to load wav stream.\n");
408 wav_close(wavfile);
409 }
410
411 return stream;
412 }
413
414
415 /* _al_save_wav:
416 * Writes a sample into a wav ALLEGRO_FILE.
417 * Returns true on success, false on error.
418 */
419 bool _al_save_wav(const char *filename, ALLEGRO_SAMPLE *spl)
420 {
421 ALLEGRO_FILE *pf = al_fopen(filename, "wb");
422
423 if (pf) {
424 bool rvsave = _al_save_wav_f(pf, spl);
425 bool rvclose = al_fclose(pf);
426 return rvsave && rvclose;
427 }
428 else {
429 ALLEGRO_ERROR("Unable to open %s for writing.\n", filename);
430 }
431
432 return false;
433 }
434
435
436 /* _al_save_wav_f:
437 * Writes a sample into a wav packfile.
438 * Returns true on success, false on error.
439 */
440 bool _al_save_wav_f(ALLEGRO_FILE *pf, ALLEGRO_SAMPLE *spl)
441 {
442 size_t channels, bits;
443 size_t data_size;
444 size_t samples;
445 size_t i, n;
446
447 ASSERT(spl);
448 ASSERT(pf);
449
450 /* XXX: makes use of ALLEGRO_SAMPLE internals */
451
452 channels = (spl->chan_conf >> 4) + (spl->chan_conf & 0xF);
453 bits = (spl->depth == ALLEGRO_AUDIO_DEPTH_INT8 ||
454 spl->depth == ALLEGRO_AUDIO_DEPTH_UINT8) ? 8 : 16;
455
456 if (channels < 1 || channels > 2) {
457 ALLEGRO_ERROR("Can only save samples with 1 or 2 channels as WAV.\n");
458 return false;
459 }
460
461 samples = spl->len;
462 data_size = samples * channels * bits / 8;
463 n = samples * channels;
464
465 al_fputs(pf, "RIFF");
466 al_fwrite32le(pf, 36 + data_size);
467 al_fputs(pf, "WAVE");
468
469 al_fputs(pf, "fmt ");
470 al_fwrite32le(pf, 16);
471 al_fwrite16le(pf, 1);
472 al_fwrite16le(pf, (int16_t)channels);
473 al_fwrite32le(pf, spl->frequency);
474 al_fwrite32le(pf, spl->frequency * channels * bits / 8);
475 al_fwrite16le(pf, (int16_t)(channels * bits / 8));
476 al_fwrite16le(pf, (int16_t)bits);
477
478 al_fputs(pf, "data");
479 al_fwrite32le(pf, data_size);
480
481
482 if (spl->depth == ALLEGRO_AUDIO_DEPTH_UINT8) {
483 al_fwrite(pf, spl->buffer.u8, samples * channels);
484 }
485 else if (spl->depth == ALLEGRO_AUDIO_DEPTH_INT16) {
486 al_fwrite(pf, spl->buffer.s16, samples * channels * 2);
487 }
488 else if (spl->depth == ALLEGRO_AUDIO_DEPTH_INT8) {
489 int8_t *data = spl->buffer.s8;
490 for (i = 0; i < samples; ++i) {
491 al_fputc(pf, *data++ + 0x80);
492 }
493 }
494 else if (spl->depth == ALLEGRO_AUDIO_DEPTH_UINT16) {
495 uint16_t *data = spl->buffer.u16;
496 for (i = 0; i < n; ++i) {
497 al_fwrite16le(pf, *data++ - 0x8000);
498 }
499 }
500 else if (spl->depth == ALLEGRO_AUDIO_DEPTH_INT24) {
501 int32_t *data = spl->buffer.s24;
502 for (i = 0; i < n; ++i) {
503 const int v = ((float)(*data++ + 0x800000) / 0x7FFFFF) * 0x7FFF - 0x8000;
504 al_fwrite16le(pf, v);
505 }
506 }
507 else if (spl->depth == ALLEGRO_AUDIO_DEPTH_UINT24) {
508 uint32_t *data = spl->buffer.u24;
509 for (i = 0; i < n; ++i) {
510 const int v = ((float)(*data++) / 0x7FFFFF) * 0x7FFF - 0x8000;
511 al_fwrite16le(pf, v);
512 }
513 }
514 else if (spl->depth == ALLEGRO_AUDIO_DEPTH_FLOAT32) {
515 float *data = spl->buffer.f32;
516 for (i = 0; i < n; ++i) {
517 al_fwrite16le(pf, *data * 0x7FFF);
518 data++;
519 }
520 }
521 else {
522 ALLEGRO_ERROR("Unknown audio depth (%d) when saving wav ALLEGRO_FILE.\n",
523 spl->depth);
524 return false;
525 }
526
527 return true;
528 }
529
530
531 bool _al_identify_wav(ALLEGRO_FILE *f)
532 {
533 uint8_t x[4];
534 if (al_fread(f, x, 4) < 4)
535 return false;
536 if (memcmp(x, "RIFF", 4) != 0)
537 return false;
538 if (!al_fseek(f, 4, ALLEGRO_SEEK_CUR))
539 return false;
540 if (al_fread(f, x, 4) < 4)
541 return false;
542 if (memcmp(x, "WAVE", 4) == 0)
543 return true;
544 return false;
545 }
546
547 /* vim: set sts=3 sw=3 et: */