Debian Codegraph @

addons/audio/audio_io.c

raw
1 /*
2 * Allegro audio codec table.
3 */
4
5 #include "allegro5/allegro.h"
6 #include "allegro5/allegro_audio.h"
7 #include "allegro5/internal/aintern.h"
8 #include "allegro5/internal/aintern_audio.h"
9 #include "allegro5/internal/aintern_exitfunc.h"
10 #include "allegro5/internal/aintern_vector.h"
11
12 ALLEGRO_DEBUG_CHANNEL("audio")
13
14
15 #define MAX_EXTENSION_LENGTH (32)
16
17 typedef struct ACODEC_TABLE ACODEC_TABLE;
18 struct ACODEC_TABLE
19 {
20 char ext[MAX_EXTENSION_LENGTH];
21 ALLEGRO_SAMPLE * (*loader)(const char *filename);
22 bool (*saver)(const char *filename, ALLEGRO_SAMPLE *spl);
23 ALLEGRO_AUDIO_STREAM *(*stream_loader)(const char *filename,
24 size_t buffer_count, unsigned int samples);
25
26 ALLEGRO_SAMPLE * (*fs_loader)(ALLEGRO_FILE *fp);
27 bool (*fs_saver)(ALLEGRO_FILE *fp, ALLEGRO_SAMPLE *spl);
28 ALLEGRO_AUDIO_STREAM *(*fs_stream_loader)(ALLEGRO_FILE *fp,
29 size_t buffer_count, unsigned int samples);
30
31 bool (*identifier)(ALLEGRO_FILE *fp);
32 };
33
34
35 /* globals */
36 static bool acodec_inited = false;
37 static _AL_VECTOR acodec_table = _AL_VECTOR_INITIALIZER(ACODEC_TABLE);
38 static ALLEGRO_AUDIO_STREAM *global_stream;
39
40
41 static void acodec_shutdown(void)
42 {
43 if (acodec_inited) {
44 _al_vector_free(&acodec_table);
45 acodec_inited = false;
46 }
47 al_destroy_audio_stream(global_stream);
48 }
49
50
51 static ACODEC_TABLE *find_acodec_table_entry(const char *ext)
52 {
53 ACODEC_TABLE *ent;
54 unsigned i;
55
56 for (i = 0; i < _al_vector_size(&acodec_table); i++) {
57 ent = _al_vector_ref(&acodec_table, i);
58 if (0 == _al_stricmp(ent->ext, ext)) {
59 return ent;
60 }
61 }
62
63 return NULL;
64 }
65
66
67 static ACODEC_TABLE *find_acodec_table_entry_for_file(ALLEGRO_FILE *f)
68 {
69 ACODEC_TABLE *ent;
70 unsigned i;
71
72 for (i = 0; i < _al_vector_size(&acodec_table); i++) {
73 ent = _al_vector_ref(&acodec_table, i);
74 if (ent->identifier) {
75 int64_t pos = al_ftell(f);
76 bool identified = ent->identifier(f);
77 al_fseek(f, pos, ALLEGRO_SEEK_SET);
78 if (identified)
79 return ent;
80 }
81 }
82 return NULL;
83 }
84
85
86 static ACODEC_TABLE *add_acodec_table_entry(const char *ext)
87 {
88 ACODEC_TABLE *ent;
89
90 ASSERT(ext);
91
92 if (!acodec_inited) {
93 acodec_inited = true;
94 _al_add_exit_func(acodec_shutdown, "acodec_shutdown");
95 }
96
97 ent = _al_vector_alloc_back(&acodec_table);
98 strcpy(ent->ext, ext);
99 ent->loader = NULL;
100 ent->saver = NULL;
101 ent->stream_loader = NULL;
102
103 ent->fs_loader = NULL;
104 ent->fs_saver = NULL;
105 ent->fs_stream_loader = NULL;
106
107 ent->identifier = NULL;
108
109 return ent;
110 }
111
112
113 /* Function: al_register_sample_loader
114 */
115 bool al_register_sample_loader(const char *ext,
116 ALLEGRO_SAMPLE *(*loader)(const char *filename))
117 {
118 ACODEC_TABLE *ent;
119
120 if (strlen(ext) + 1 >= MAX_EXTENSION_LENGTH) {
121 return false;
122 }
123
124 ent = find_acodec_table_entry(ext);
125 if (!loader) {
126 if (!ent || !ent->loader) {
127 return false; /* Nothing to remove. */
128 }
129 }
130 else if (!ent) {
131 ent = add_acodec_table_entry(ext);
132 }
133
134 ent->loader = loader;
135
136 return true;
137 }
138
139
140 /* Function: al_register_sample_loader_f
141 */
142 bool al_register_sample_loader_f(const char *ext,
143 ALLEGRO_SAMPLE *(*loader)(ALLEGRO_FILE* fp))
144 {
145 ACODEC_TABLE *ent;
146
147 if (strlen(ext) + 1 >= MAX_EXTENSION_LENGTH) {
148 return false;
149 }
150
151 ent = find_acodec_table_entry(ext);
152 if (!loader) {
153 if (!ent || !ent->fs_loader) {
154 return false; /* Nothing to remove. */
155 }
156 }
157 else if (!ent) {
158 ent = add_acodec_table_entry(ext);
159 }
160
161 ent->fs_loader = loader;
162
163 return true;
164 }
165
166
167 /* Function: al_register_sample_saver
168 */
169 bool al_register_sample_saver(const char *ext,
170 bool (*saver)(const char *filename, ALLEGRO_SAMPLE *spl))
171 {
172 ACODEC_TABLE *ent;
173
174 if (strlen(ext) + 1 >= MAX_EXTENSION_LENGTH) {
175 return false;
176 }
177
178 ent = find_acodec_table_entry(ext);
179 if (!saver) {
180 if (!ent || !ent->saver) {
181 return false; /* Nothing to remove. */
182 }
183 }
184 else if (!ent) {
185 ent = add_acodec_table_entry(ext);
186 }
187
188 ent->saver = saver;
189
190 return true;
191 }
192
193
194 /* Function: al_register_sample_saver_f
195 */
196 bool al_register_sample_saver_f(const char *ext,
197 bool (*saver)(ALLEGRO_FILE* fp, ALLEGRO_SAMPLE *spl))
198 {
199 ACODEC_TABLE *ent;
200
201 if (strlen(ext) + 1 >= MAX_EXTENSION_LENGTH) {
202 return false;
203 }
204
205 ent = find_acodec_table_entry(ext);
206 if (!saver) {
207 if (!ent || !ent->fs_saver) {
208 return false; /* Nothing to remove. */
209 }
210 }
211 else if (!ent) {
212 ent = add_acodec_table_entry(ext);
213 }
214
215 ent->fs_saver = saver;
216
217 return true;
218 }
219
220
221 /* Function: al_register_audio_stream_loader
222 */
223 bool al_register_audio_stream_loader(const char *ext,
224 ALLEGRO_AUDIO_STREAM *(*stream_loader)(const char *filename,
225 size_t buffer_count, unsigned int samples))
226 {
227 ACODEC_TABLE *ent;
228
229 if (strlen(ext) + 1 >= MAX_EXTENSION_LENGTH) {
230 return false;
231 }
232
233 ent = find_acodec_table_entry(ext);
234 if (!stream_loader) {
235 if (!ent || !ent->stream_loader) {
236 return false; /* Nothing to remove. */
237 }
238 }
239 else if (!ent) {
240 ent = add_acodec_table_entry(ext);
241 }
242
243 ent->stream_loader = stream_loader;
244
245 return true;
246 }
247
248
249 /* Function: al_register_audio_stream_loader_f
250 */
251 bool al_register_audio_stream_loader_f(const char *ext,
252 ALLEGRO_AUDIO_STREAM *(*stream_loader)(ALLEGRO_FILE* fp,
253 size_t buffer_count, unsigned int samples))
254 {
255 ACODEC_TABLE *ent;
256
257 if (strlen(ext) + 1 >= MAX_EXTENSION_LENGTH) {
258 return false;
259 }
260
261 ent = find_acodec_table_entry(ext);
262 if (!stream_loader) {
263 if (!ent || !ent->fs_stream_loader) {
264 return false; /* Nothing to remove. */
265 }
266 }
267 else if (!ent) {
268 ent = add_acodec_table_entry(ext);
269 }
270
271 ent->fs_stream_loader = stream_loader;
272
273 return true;
274 }
275
276
277 /* Function: al_register_sample_identifier
278 */
279 bool al_register_sample_identifier(const char *ext,
280 bool (*identifier)(ALLEGRO_FILE* fp))
281 {
282 ACODEC_TABLE *ent;
283
284 if (strlen(ext) + 1 >= MAX_EXTENSION_LENGTH) {
285 return false;
286 }
287
288 ent = find_acodec_table_entry(ext);
289 if (!identifier) {
290 if (!ent || !ent->identifier) {
291 return false; /* Nothing to remove. */
292 }
293 }
294 else if (!ent) {
295 ent = add_acodec_table_entry(ext);
296 }
297
298 ent->identifier = identifier;
299
300 return true;
301 }
302
303
304 /* Function: al_load_sample
305 */
306 ALLEGRO_SAMPLE *al_load_sample(const char *filename)
307 {
308 const char *ext;
309 ACODEC_TABLE *ent;
310
311 ASSERT(filename);
312 ext = al_identify_sample(filename);
313 if (!ext) {
314 ext = strrchr(filename, '.');
315 if (ext == NULL) {
316 ALLEGRO_ERROR("Unable to determine extension for %s.\n", filename);
317 return NULL;
318 }
319 }
320
321 ent = find_acodec_table_entry(ext);
322 if (ent && ent->loader) {
323 return (ent->loader)(filename);
324 }
325 else {
326 ALLEGRO_ERROR("No handler for audio file extension %s - "
327 "therefore not trying to load %s.\n", ext, filename);
328 }
329
330 return NULL;
331 }
332
333
334 /* Function: al_load_sample_f
335 */
336 ALLEGRO_SAMPLE *al_load_sample_f(ALLEGRO_FILE* fp, const char *ident)
337 {
338 ACODEC_TABLE *ent;
339
340 ASSERT(fp);
341 ASSERT(ident);
342
343 ent = find_acodec_table_entry(ident);
344 if (ent && ent->fs_loader) {
345 return (ent->fs_loader)(fp);
346 }
347 else {
348 ALLEGRO_ERROR("No handler for audio file extension %s.\n", ident);
349 }
350
351 return NULL;
352 }
353
354
355 /* Function: al_load_audio_stream
356 */
357 ALLEGRO_AUDIO_STREAM *al_load_audio_stream(const char *filename,
358 size_t buffer_count, unsigned int samples)
359 {
360 const char *ext;
361 ACODEC_TABLE *ent;
362
363 ASSERT(filename);
364 ext = al_identify_sample(filename);
365 if (!ext) {
366 ext = strrchr(filename, '.');
367 if (ext == NULL) {
368 ALLEGRO_ERROR("Unable to determine extension for %s.\n", filename);
369 return NULL;
370 }
371 }
372
373 ent = find_acodec_table_entry(ext);
374 if (ent && ent->stream_loader) {
375 return (ent->stream_loader)(filename, buffer_count, samples);
376 }
377 else {
378 ALLEGRO_ERROR("No handler for audio file extension %s - "
379 "therefore not trying to load %s.\n", ext, filename);
380 }
381
382 return NULL;
383 }
384
385
386 /* Function: al_load_audio_stream_f
387 */
388 ALLEGRO_AUDIO_STREAM *al_load_audio_stream_f(ALLEGRO_FILE* fp, const char *ident,
389 size_t buffer_count, unsigned int samples)
390 {
391 ACODEC_TABLE *ent;
392
393 ASSERT(fp);
394 ASSERT(ident);
395
396 ent = find_acodec_table_entry(ident);
397 if (ent && ent->fs_stream_loader) {
398 return (ent->fs_stream_loader)(fp, buffer_count, samples);
399 }
400 else {
401 ALLEGRO_ERROR("No handler for audio file extension %s.\n", ident);
402 }
403
404 return NULL;
405 }
406
407
408 /* Function: al_play_audio_stream
409 */
410 ALLEGRO_AUDIO_STREAM *al_play_audio_stream(const char *filename)
411 {
412 ASSERT(filename);
413 if (!al_get_default_mixer()) {
414 ALLEGRO_ERROR("No default mixer\n!");
415 return NULL;
416 }
417 al_destroy_audio_stream(global_stream);
418 global_stream = al_load_audio_stream(filename, 4, 2048);
419 if (!global_stream) {
420 ALLEGRO_ERROR("Could not play audio stream: %s.\n", filename);
421 return NULL;
422 }
423 if (!al_attach_audio_stream_to_mixer(global_stream, al_get_default_mixer())) {
424 ALLEGRO_ERROR("Could not attach stream to mixer\n");
425 return NULL;
426 }
427 return global_stream;
428 }
429
430
431 /* Function: al_play_audio_stream_f
432 */
433 ALLEGRO_AUDIO_STREAM *al_play_audio_stream_f(ALLEGRO_FILE *fp, const char *ident)
434 {
435 ASSERT(fp);
436 ASSERT(ident);
437 if (!al_get_default_mixer()) {
438 ALLEGRO_ERROR("No default mixer\n!");
439 return NULL;
440 }
441 al_destroy_audio_stream(global_stream);
442 global_stream = al_load_audio_stream_f(fp, ident, 4, 2048);
443 if (!global_stream) {
444 ALLEGRO_ERROR("Could not play audio stream.\n");
445 return NULL;
446 }
447 if (!al_attach_audio_stream_to_mixer(global_stream, al_get_default_mixer())) {
448 ALLEGRO_ERROR("Could not attach stream to mixer\n");
449 return NULL;
450 }
451 return global_stream;
452 }
453
454
455 /* Function: al_save_sample
456 */
457 bool al_save_sample(const char *filename, ALLEGRO_SAMPLE *spl)
458 {
459 const char *ext;
460 ACODEC_TABLE *ent;
461
462 ASSERT(filename);
463 ext = strrchr(filename, '.');
464 if (ext == NULL) {
465 ALLEGRO_ERROR("Unable to determine extension for %s.\n", filename);
466 return false;
467 }
468
469 ent = find_acodec_table_entry(ext);
470 if (ent && ent->saver) {
471 return (ent->saver)(filename, spl);
472 }
473 else {
474 ALLEGRO_ERROR("No handler for audio file extension %s - "
475 "therefore not trying to load %s.\n", ext, filename);
476 }
477
478 return false;
479 }
480
481
482 /* Function: al_save_sample_f
483 */
484 bool al_save_sample_f(ALLEGRO_FILE *fp, const char *ident, ALLEGRO_SAMPLE *spl)
485 {
486 ACODEC_TABLE *ent;
487
488 ASSERT(fp);
489 ASSERT(ident);
490
491 ent = find_acodec_table_entry(ident);
492 if (ent && ent->fs_saver) {
493 return (ent->fs_saver)(fp, spl);
494 }
495 else {
496 ALLEGRO_ERROR("No handler for audio file extension %s.\n", ident);
497 }
498
499 return false;
500 }
501
502
503 /* Function: al_identify_sample_f
504 */
505 char const *al_identify_sample_f(ALLEGRO_FILE *fp)
506 {
507 ACODEC_TABLE *h = find_acodec_table_entry_for_file(fp);
508 if (!h)
509 return NULL;
510 return h->ext;
511 }
512
513
514 /* Function: al_identify_sample
515 */
516 char const *al_identify_sample(char const *filename)
517 {
518 char const *ext;
519 ALLEGRO_FILE *fp = al_fopen(filename, "rb");
520 if (!fp)
521 return NULL;
522 ext = al_identify_sample_f(fp);
523 al_fclose(fp);
524 return ext;
525 }
526
527 /* vim: set sts=3 sw=3 et: */