Debian Codegraph @

addons/audio/sdl_audio.c

raw
1 #define ALLEGRO_INTERNAL_UNSTABLE
2
3 #include "allegro5/allegro.h"
4 #include "allegro5/internal/aintern_audio.h"
5 #include "allegro5/platform/allegro_internal_sdl.h"
6
7 ALLEGRO_DEBUG_CHANNEL("SDL")
8
9 typedef struct SDL_VOICE
10 {
11 SDL_AudioDeviceID device;
12 SDL_AudioSpec spec;
13 ALLEGRO_VOICE *voice;
14 bool is_playing;
15 } SDL_VOICE;
16
17 typedef struct SDL_RECORDER
18 {
19 SDL_AudioDeviceID device;
20 SDL_AudioSpec spec;
21 unsigned int fragment;
22 } SDL_RECORDER;
23
24 static _AL_LIST* output_device_list;
25
26 static void audio_callback(void *userdata, Uint8 *stream, int len)
27 {
28 // TODO: Allegro has those mysterious "non-streaming" samples, but I
29 // can't figure out what their purpose is and how would I play them...
30
31 SDL_VOICE *sv = userdata;
32 ALLEGRO_SAMPLE_INSTANCE *instance = sv->voice->attached_stream;
33 ALLEGRO_SAMPLE *sample = &instance->spl_data;
34
35 unsigned int frames = sv->spec.samples;
36 const void *data = _al_voice_update(sv->voice, sv->voice->mutex, &frames);
37 if (data) {
38 // FIXME: What is frames for?
39 memcpy(stream, sample->buffer.ptr, len);
40 }
41 }
42
43 static int sdl_open(void)
44 {
45 return 0;
46 }
47
48 static void sdl_close(void)
49 {
50 }
51
52 static SDL_AudioFormat allegro_format_to_sdl(ALLEGRO_AUDIO_DEPTH d)
53 {
54 if (d == ALLEGRO_AUDIO_DEPTH_INT8) return AUDIO_S8;
55 if (d == ALLEGRO_AUDIO_DEPTH_UINT8) return AUDIO_U8;
56 if (d == ALLEGRO_AUDIO_DEPTH_INT16) return AUDIO_S16;
57 if (d == ALLEGRO_AUDIO_DEPTH_UINT16) return AUDIO_U16;
58 return AUDIO_F32;
59 }
60
61 static ALLEGRO_AUDIO_DEPTH sdl_format_to_allegro(SDL_AudioFormat d)
62 {
63 if (d == AUDIO_S8) return ALLEGRO_AUDIO_DEPTH_INT8;
64 if (d == AUDIO_U8) return ALLEGRO_AUDIO_DEPTH_UINT8;
65 if (d == AUDIO_S16) return ALLEGRO_AUDIO_DEPTH_INT16;
66 if (d == AUDIO_U16) return ALLEGRO_AUDIO_DEPTH_UINT16;
67 return ALLEGRO_AUDIO_DEPTH_FLOAT32;
68 }
69
70 static int sdl_allocate_voice(ALLEGRO_VOICE *voice)
71 {
72 SDL_VOICE *sv = al_malloc(sizeof *sv);
73 SDL_AudioSpec want;
74 memset(&want, 0, sizeof want);
75
76 want.freq = voice->frequency;
77 want.format = allegro_format_to_sdl(voice->depth);
78 want.channels = al_get_channel_count(voice->chan_conf);
79 // TODO: Should make this configurable somehow
80 want.samples = 4096;
81 want.callback = audio_callback;
82 want.userdata = sv;
83
84 sv->device = SDL_OpenAudioDevice(NULL, 0, &want, &sv->spec,
85 SDL_AUDIO_ALLOW_FORMAT_CHANGE);
86
87 voice->extra = sv;
88 sv->voice = voice;
89 // we allow format change above so need to update here
90 voice->depth = sdl_format_to_allegro(sv->spec.format);
91
92 return 0;
93 }
94
95 static void sdl_deallocate_voice(ALLEGRO_VOICE *voice)
96 {
97 SDL_VOICE *sv = voice->extra;
98 _al_list_destroy(output_device_list);
99 SDL_CloseAudioDevice(sv->device);
100 al_free(sv);
101 }
102
103 static int sdl_load_voice(ALLEGRO_VOICE *voice, const void *data)
104 {
105 (void)data;
106 voice->attached_stream->pos = 0;
107 return 0;
108 }
109
110 static void sdl_unload_voice(ALLEGRO_VOICE *voice)
111 {
112 (void) voice;
113 }
114
115 static int sdl_start_voice(ALLEGRO_VOICE *voice)
116 {
117 SDL_VOICE *sv = voice->extra;
118 sv->is_playing = true;
119 SDL_PauseAudioDevice(sv->device, 0);
120 return 0;
121 }
122
123 static int sdl_stop_voice(ALLEGRO_VOICE *voice)
124 {
125 SDL_VOICE *sv = voice->extra;
126 sv->is_playing = false;
127 SDL_PauseAudioDevice(sv->device, 1);
128 return 0;
129 }
130
131 static bool sdl_voice_is_playing(const ALLEGRO_VOICE *voice)
132 {
133 SDL_VOICE *sv = voice->extra;
134 return sv->is_playing;
135 }
136
137 static unsigned int sdl_get_voice_position(const ALLEGRO_VOICE *voice)
138 {
139 return voice->attached_stream->pos;
140 }
141
142 static int sdl_set_voice_position(ALLEGRO_VOICE *voice, unsigned int pos)
143 {
144 voice->attached_stream->pos = pos;
145 return 0;
146 }
147
148 static void recorder_callback(void *userdata, Uint8 *stream, int len)
149 {
150 ALLEGRO_AUDIO_RECORDER *r = (ALLEGRO_AUDIO_RECORDER *) userdata;
151 SDL_RECORDER *sdl = (SDL_RECORDER *) r->extra;
152
153 al_lock_mutex(r->mutex);
154 if (!r->is_recording) {
155 al_unlock_mutex(r->mutex);
156 return;
157 }
158
159 while (len > 0) {
160 int count = SDL_min(len, r->samples * r->sample_size);
161 memcpy(r->fragments[sdl->fragment], stream, count);
162
163 ALLEGRO_EVENT user_event;
164 ALLEGRO_AUDIO_RECORDER_EVENT *e;
165 user_event.user.type = ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT;
166 e = al_get_audio_recorder_event(&user_event);
167 e->buffer = r->fragments[sdl->fragment];
168 e->samples = count / r->sample_size;
169 al_emit_user_event(&r->source, &user_event, NULL);
170
171 sdl->fragment++;
172 if (sdl->fragment == r->fragment_count) {
173 sdl->fragment = 0;
174 }
175 len -= count;
176 }
177
178 al_unlock_mutex(r->mutex);
179 }
180
181 static int sdl_allocate_recorder(ALLEGRO_AUDIO_RECORDER *r)
182 {
183 SDL_RECORDER *sdl;
184
185 sdl = al_calloc(1, sizeof(*sdl));
186 if (!sdl) {
187 ALLEGRO_ERROR("Unable to allocate memory for SDL_RECORDER.\n");
188 return 1;
189 }
190
191 SDL_AudioSpec want;
192 memset(&want, 0, sizeof want);
193
194 want.freq = r->frequency;
195 want.format = allegro_format_to_sdl(r->depth);
196 want.channels = al_get_channel_count(r->chan_conf);
197 want.samples = r->samples;
198 want.callback = recorder_callback;
199 want.userdata = r;
200
201 sdl->device = SDL_OpenAudioDevice(NULL, 1, &want, &sdl->spec, 0);
202 sdl->fragment = 0;
203 r->extra = sdl;
204
205 SDL_PauseAudioDevice(sdl->device, 0);
206
207 return 0;
208 }
209
210 static void sdl_deallocate_recorder(ALLEGRO_AUDIO_RECORDER *r)
211 {
212 SDL_RECORDER *sdl = (SDL_RECORDER *) r->extra;
213 SDL_CloseAudioDevice(sdl->device);
214 al_free(r->extra);
215 }
216
217 static void _output_device_list_dtor(void* value, void* userdata)
218 {
219 (void)userdata;
220
221 ALLEGRO_AUDIO_DEVICE* device = (ALLEGRO_AUDIO_DEVICE*)value;
222 al_free(device->name);
223 al_free(device);
224 }
225
226 static _AL_LIST* sdl_get_output_devices(void)
227 {
228 if (!output_device_list) {
229 output_device_list = _al_list_create();
230
231 int i, count = SDL_GetNumAudioDevices(0);
232 for (i = 0; i < count; ++i) {
233 int len = strlen(SDL_GetAudioDeviceName(i, 0)) + 1;
234
235 ALLEGRO_AUDIO_DEVICE* device = (ALLEGRO_AUDIO_DEVICE*)al_malloc(sizeof(ALLEGRO_AUDIO_DEVICE));
236 device->name = (char*)al_malloc(len);
237 device->identifier = device->name; // Name returned by SDL2 is used to identify devices.
238 strcpy(device->name, SDL_GetAudioDeviceName(i, 0));
239
240 _al_list_push_back_ex(output_device_list, device, _output_device_list_dtor);
241 }
242 }
243
244 return output_device_list;
245 }
246
247 ALLEGRO_AUDIO_DRIVER _al_kcm_sdl_driver =
248 {
249 "SDL",
250
251 sdl_open,
252 sdl_close,
253
254 sdl_allocate_voice,
255 sdl_deallocate_voice,
256
257 sdl_load_voice,
258 sdl_unload_voice,
259
260 sdl_start_voice,
261 sdl_stop_voice,
262
263 sdl_voice_is_playing,
264
265 sdl_get_voice_position,
266 sdl_set_voice_position,
267
268 sdl_allocate_recorder,
269 sdl_deallocate_recorder,
270
271 sdl_get_output_devices,
272 };