-
Notifications
You must be signed in to change notification settings - Fork 279
/
ex_stream_file.c
139 lines (115 loc) · 3.62 KB
/
ex_stream_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* An example program that plays a file from the disk using Allegro5
* streaming API. The file is being read in small chunks and played on the
* sound device instead of being loaded at once.
*
* usage: ./ex_stream_file file.[wav,ogg...] ...
*
* by Milan Mimica
*/
#include <stdio.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_audio.h"
#include "allegro5/allegro_acodec.h"
#include "common.c"
/* Attaches the stream directly to a voice. Streamed file's and voice's sample
* rate, channels and depth must match.
*/
//#define BYPASS_MIXER
char *default_files[] = {NULL, "../demos/skater/data/menu/skate2.ogg"};
int main(int argc, char **argv)
{
int i;
ALLEGRO_VOICE* voice;
ALLEGRO_MIXER* mixer;
bool loop = false;
int arg_start = 1;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
open_log();
if (argc < 2) {
log_printf("This example can be run from the command line.\n");
log_printf("Usage: %s [--loop] {audio_files}\n", argv[0]);
argv = default_files;
argc = 2;
}
if (strcmp(argv[1], "--loop") == 0) {
loop = true;
arg_start = 2;
}
al_init_acodec_addon();
if (!al_install_audio()) {
abort_example("Could not init sound!\n");
}
voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16,
ALLEGRO_CHANNEL_CONF_2);
if (!voice) {
abort_example("Could not create ALLEGRO_VOICE.\n");
}
log_printf("Voice created.\n");
#ifndef BYPASS_MIXER
mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
ALLEGRO_CHANNEL_CONF_2);
if (!mixer) {
abort_example("Could not create ALLEGRO_MIXER.\n");
}
log_printf("Mixer created.\n");
if (!al_attach_mixer_to_voice(mixer, voice)) {
abort_example("al_attach_mixer_to_voice failed.\n");
}
#endif
for (i = arg_start; i < argc; ++i)
{
ALLEGRO_AUDIO_STREAM* stream;
const char* filename = argv[i];
bool playing = true;
ALLEGRO_EVENT event;
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
stream = al_load_audio_stream(filename, 4, 2048);
if (!stream) {
/* If it is not packed, e.g. on Android or iOS. */
if (!strcmp(filename, default_files[1])) {
stream = al_load_audio_stream("data/welcome.wav", 4, 2048);
}
}
if (!stream) {
log_printf("Could not create an ALLEGRO_AUDIO_STREAM from '%s'!\n",
filename);
continue;
}
log_printf("Stream created from '%s'.\n", filename);
if (loop) {
al_set_audio_stream_playmode(stream, loop ? ALLEGRO_PLAYMODE_LOOP : ALLEGRO_PLAYMODE_ONCE);
}
al_register_event_source(queue, al_get_audio_stream_event_source(stream));
#ifndef BYPASS_MIXER
if (!al_attach_audio_stream_to_mixer(stream, mixer)) {
log_printf("al_attach_audio_stream_to_mixer failed.\n");
continue;
}
#else
if (!al_attach_audio_stream_to_voice(stream, voice)) {
abort_example("al_attach_audio_stream_to_voice failed.\n");
}
#endif
log_printf("Playing %s ... Waiting for stream to finish ", filename);
do {
al_wait_for_event(queue, &event);
if(event.type == ALLEGRO_EVENT_AUDIO_STREAM_FINISHED)
playing = false;
} while (playing);
log_printf("\n");
al_destroy_event_queue(queue);
al_destroy_audio_stream(stream);
}
log_printf("Done\n");
#ifndef BYPASS_MIXER
al_destroy_mixer(mixer);
#endif
al_destroy_voice(voice);
al_uninstall_audio();
close_log(true);
return 0;
}
/* vim: set sts=3 sw=3 et: */