Debian Codegraph @

addons/audio/audio.c

raw
1 /**
2 * Originally digi.c from allegro wiki
3 * Original authors: KC/Milan
4 *
5 * Converted to allegro5 by Ryan Dickie
6 */
7
8
9 #include <math.h>
10 #include <stdio.h>
11
12 #include "allegro5/allegro.h"
13 #include "allegro5/allegro_audio.h"
14 #include "allegro5/internal/aintern.h"
15 #include "allegro5/internal/aintern_exitfunc.h"
16 #include "allegro5/internal/aintern_audio.h"
17 #include "allegro5/internal/aintern_audio_cfg.h"
18
19 ALLEGRO_DEBUG_CHANNEL("audio")
20
21 void _al_set_error(int error, char* string)
22 {
23 ALLEGRO_ERROR("%s (error code: %d)\n", string, error);
24 }
25
26 ALLEGRO_AUDIO_DRIVER *_al_kcm_driver = NULL;
27
28 #if defined(ALLEGRO_CFG_KCM_OPENAL)
29 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_openal_driver;
30 #endif
31 #if defined(ALLEGRO_CFG_KCM_OPENSL)
32 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_opensl_driver;
33 #endif
34 #if defined(ALLEGRO_CFG_KCM_ALSA)
35 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_alsa_driver;
36 #endif
37 #if defined(ALLEGRO_CFG_KCM_OSS)
38 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_oss_driver;
39 #endif
40 #if defined(ALLEGRO_CFG_KCM_DSOUND)
41 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_dsound_driver;
42 #endif
43 #if defined(ALLEGRO_CFG_KCM_AQUEUE)
44 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_aqueue_driver;
45 #endif
46 #if defined(ALLEGRO_CFG_KCM_PULSEAUDIO)
47 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_pulseaudio_driver;
48 #endif
49 #if defined(ALLEGRO_SDL)
50 extern struct ALLEGRO_AUDIO_DRIVER _al_kcm_sdl_driver;
51 #endif
52
53 /* Channel configuration helpers */
54
55 /* Function: al_get_channel_count
56 */
57 size_t al_get_channel_count(ALLEGRO_CHANNEL_CONF conf)
58 {
59 return (conf>>4)+(conf&0xF);
60 }
61
62 /* Depth configuration helpers */
63 /* Function: al_get_audio_depth_size
64 */
65 size_t al_get_audio_depth_size(ALLEGRO_AUDIO_DEPTH depth)
66 {
67 switch (depth) {
68 case ALLEGRO_AUDIO_DEPTH_INT8:
69 case ALLEGRO_AUDIO_DEPTH_UINT8:
70 return sizeof(int8_t);
71 case ALLEGRO_AUDIO_DEPTH_INT16:
72 case ALLEGRO_AUDIO_DEPTH_UINT16:
73 return sizeof(int16_t);
74 case ALLEGRO_AUDIO_DEPTH_INT24:
75 case ALLEGRO_AUDIO_DEPTH_UINT24:
76 return sizeof(int32_t);
77 case ALLEGRO_AUDIO_DEPTH_FLOAT32:
78 return sizeof(float);
79 default:
80 ASSERT(false);
81 return 0;
82 }
83 }
84
85 /* FIXME: use the allegro provided helpers */
86 ALLEGRO_CHANNEL_CONF _al_count_to_channel_conf(int num_channels)
87 {
88 switch (num_channels) {
89 case 1:
90 return ALLEGRO_CHANNEL_CONF_1;
91 case 2:
92 return ALLEGRO_CHANNEL_CONF_2;
93 case 3:
94 return ALLEGRO_CHANNEL_CONF_3;
95 case 4:
96 return ALLEGRO_CHANNEL_CONF_4;
97 case 6:
98 return ALLEGRO_CHANNEL_CONF_5_1;
99 case 7:
100 return ALLEGRO_CHANNEL_CONF_6_1;
101 case 8:
102 return ALLEGRO_CHANNEL_CONF_7_1;
103 default:
104 return 0;
105 }
106 }
107
108 /* FIXME: assumes 8-bit is unsigned, and all others are signed. */
109 ALLEGRO_AUDIO_DEPTH _al_word_size_to_depth_conf(int word_size)
110 {
111 switch (word_size) {
112 case 1:
113 return ALLEGRO_AUDIO_DEPTH_UINT8;
114 case 2:
115 return ALLEGRO_AUDIO_DEPTH_INT16;
116 case 3:
117 return ALLEGRO_AUDIO_DEPTH_INT24;
118 case 4:
119 return ALLEGRO_AUDIO_DEPTH_FLOAT32;
120 default:
121 return 0;
122 }
123 }
124
125 /* Function: al_fill_silence
126 */
127 void al_fill_silence(void *buf, unsigned int samples,
128 ALLEGRO_AUDIO_DEPTH depth, ALLEGRO_CHANNEL_CONF chan_conf)
129 {
130 size_t bytes = samples * al_get_audio_depth_size(depth) *
131 al_get_channel_count(chan_conf);
132
133 switch (depth) {
134 case ALLEGRO_AUDIO_DEPTH_INT8:
135 case ALLEGRO_AUDIO_DEPTH_INT16:
136 case ALLEGRO_AUDIO_DEPTH_INT24:
137 case ALLEGRO_AUDIO_DEPTH_FLOAT32:
138 memset(buf, 0, bytes);
139 break;
140 case ALLEGRO_AUDIO_DEPTH_UINT8:
141 memset(buf, 0x80, bytes);
142 break;
143 case ALLEGRO_AUDIO_DEPTH_UINT16:
144 {
145 uint16_t *buffer = buf;
146 size_t n = bytes / sizeof(uint16_t);
147 size_t i;
148
149 for (i = 0; i < n; i++)
150 buffer[i] = 0x8000;
151 }
152 break;
153 case ALLEGRO_AUDIO_DEPTH_UINT24:
154 {
155 uint32_t *buffer = buf;
156 size_t n = bytes / sizeof(uint32_t);
157 size_t i;
158
159 for (i = 0; i < n; i++)
160 buffer[i] = 0x800000;
161 }
162 break;
163 default:
164 ASSERT(false);
165 break;
166 }
167 }
168
169 static ALLEGRO_AUDIO_DRIVER_ENUM get_config_audio_driver(void)
170 {
171 ALLEGRO_CONFIG *config = al_get_system_config();
172 const char *value;
173
174 value = al_get_config_value(config, "audio", "driver");
175 if (!value || value[0] == '\0')
176 return ALLEGRO_AUDIO_DRIVER_AUTODETECT;
177
178 if (0 == _al_stricmp(value, "ALSA"))
179 return ALLEGRO_AUDIO_DRIVER_ALSA;
180
181 if (0 == _al_stricmp(value, "OPENAL"))
182 return ALLEGRO_AUDIO_DRIVER_OPENAL;
183
184 if (0 == _al_stricmp(value, "OPENSL"))
185 return ALLEGRO_AUDIO_DRIVER_OPENSL;
186
187 if (0 == _al_stricmp(value, "OSS"))
188 return ALLEGRO_AUDIO_DRIVER_OSS;
189
190 if (0 == _al_stricmp(value, "PULSEAUDIO"))
191 return ALLEGRO_AUDIO_DRIVER_PULSEAUDIO;
192
193 if (0 == _al_stricmp(value, "DSOUND") || 0 == _al_stricmp(value, "DIRECTSOUND"))
194 return ALLEGRO_AUDIO_DRIVER_DSOUND;
195
196 return ALLEGRO_AUDIO_DRIVER_AUTODETECT;
197 }
198
199 /* Function: al_get_num_audio_output_devices
200 */
201 int al_get_num_audio_output_devices()
202 {
203 if (_al_kcm_driver) {
204 if (_al_kcm_driver->get_output_devices) {
205 _AL_LIST* audio_devices = _al_kcm_driver->get_output_devices();
206 return _al_list_size(audio_devices);
207 }
208 else {
209 return -1;
210 }
211 }
212
213 return 0;
214 }
215
216 /* Function: al_get_audio_output_device
217 */
218 const ALLEGRO_AUDIO_DEVICE* al_get_audio_output_device(int index)
219 {
220 if (_al_kcm_driver) {
221 if (_al_kcm_driver->get_output_devices) {
222 _AL_LIST* audio_devices = _al_kcm_driver->get_output_devices();
223
224 if (index >= 0 && index < (int)_al_list_size(audio_devices)) {
225 return _al_list_item_data(_al_list_at(audio_devices, index));
226 }
227 }
228 else {
229 return NULL;
230 }
231 }
232
233 return NULL;
234 }
235
236 /* Function: al_get_audio_device_name
237 */
238 const char* al_get_audio_device_name(const ALLEGRO_AUDIO_DEVICE * device)
239 {
240 return device ? device->name : NULL;
241 }
242
243 static bool do_install_audio(ALLEGRO_AUDIO_DRIVER_ENUM mode)
244 {
245 bool retVal;
246
247 /* check to see if a driver is already installed and running */
248 if (_al_kcm_driver) {
249 _al_set_error(ALLEGRO_GENERIC_ERROR, "A driver already running");
250 return false;
251 }
252
253 if (mode == ALLEGRO_AUDIO_DRIVER_AUTODETECT) {
254 mode = get_config_audio_driver();
255 }
256
257 switch (mode) {
258 case ALLEGRO_AUDIO_DRIVER_AUTODETECT:
259 #if defined(ALLEGRO_SDL)
260 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_SDL);
261 if (retVal)
262 return retVal;
263 #endif
264 #if defined(ALLEGRO_CFG_KCM_AQUEUE)
265 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_AQUEUE);
266 if (retVal)
267 return retVal;
268 #endif
269 /* If a PA server is running, we should use it by default as it will
270 * hijack ALSA and OSS and using those then just means extra lag.
271 *
272 * FIXME: Detect if no PA server is running and in that case prefer
273 * ALSA and OSS first.
274 */
275 #if defined(ALLEGRO_CFG_KCM_PULSEAUDIO)
276 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_PULSEAUDIO);
277 if (retVal)
278 return retVal;
279 #endif
280 #if defined(ALLEGRO_CFG_KCM_ALSA)
281 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_ALSA);
282 if (retVal)
283 return retVal;
284 #endif
285 #if defined(ALLEGRO_CFG_KCM_DSOUND)
286 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_DSOUND);
287 if (retVal)
288 return retVal;
289 #endif
290 #if defined(ALLEGRO_CFG_KCM_OSS)
291 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_OSS);
292 if (retVal)
293 return retVal;
294 #endif
295 #if defined(ALLEGRO_CFG_KCM_OPENAL)
296 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_OPENAL);
297 if (retVal)
298 return retVal;
299 #endif
300 #if defined(ALLEGRO_CFG_KCM_OPENSL)
301 retVal = do_install_audio(ALLEGRO_AUDIO_DRIVER_OPENSL);
302 if (retVal)
303 return retVal;
304 #endif
305
306 _al_set_error(ALLEGRO_INVALID_PARAM, "No audio driver can be used.");
307 _al_kcm_driver = NULL;
308 return false;
309
310 case ALLEGRO_AUDIO_DRIVER_AQUEUE:
311 #if defined(ALLEGRO_CFG_KCM_AQUEUE)
312 if (_al_kcm_aqueue_driver.open() == 0) {
313 ALLEGRO_INFO("Using Apple Audio Queue driver\n");
314 _al_kcm_driver = &_al_kcm_aqueue_driver;
315 return true;
316 }
317 return false;
318 #else
319 _al_set_error(ALLEGRO_INVALID_PARAM, "Audio Queue driver not available on this platform");
320 return false;
321 #endif
322
323 case ALLEGRO_AUDIO_DRIVER_OPENAL:
324 #if defined(ALLEGRO_CFG_KCM_OPENAL)
325 if (_al_kcm_openal_driver.open() == 0) {
326 ALLEGRO_INFO("Using OpenAL driver\n");
327 _al_kcm_driver = &_al_kcm_openal_driver;
328 return true;
329 }
330 return false;
331 #else
332 _al_set_error(ALLEGRO_INVALID_PARAM, "OpenAL not available on this platform");
333 return false;
334 #endif
335
336 case ALLEGRO_AUDIO_DRIVER_OPENSL:
337 #if defined(ALLEGRO_CFG_KCM_OPENSL)
338 if (_al_kcm_opensl_driver.open() == 0) {
339 ALLEGRO_INFO("Using OpenSL driver\n");
340 _al_kcm_driver = &_al_kcm_opensl_driver;
341 return true;
342 }
343 return false;
344 #else
345 _al_set_error(ALLEGRO_INVALID_PARAM, "OpenSL not available on this platform");
346 return false;
347 #endif
348
349 case ALLEGRO_AUDIO_DRIVER_ALSA:
350 #if defined(ALLEGRO_CFG_KCM_ALSA)
351 if (_al_kcm_alsa_driver.open() == 0) {
352 ALLEGRO_INFO("Using ALSA driver\n");
353 _al_kcm_driver = &_al_kcm_alsa_driver;
354 return true;
355 }
356 return false;
357 #else
358 _al_set_error(ALLEGRO_INVALID_PARAM, "ALSA not available on this platform");
359 return false;
360 #endif
361
362 case ALLEGRO_AUDIO_DRIVER_OSS:
363 #if defined(ALLEGRO_CFG_KCM_OSS)
364 if (_al_kcm_oss_driver.open() == 0) {
365 ALLEGRO_INFO("Using OSS driver\n");
366 _al_kcm_driver = &_al_kcm_oss_driver;
367 return true;
368 }
369 return false;
370 #else
371 _al_set_error(ALLEGRO_INVALID_PARAM, "OSS not available on this platform");
372 return false;
373 #endif
374
375 case ALLEGRO_AUDIO_DRIVER_PULSEAUDIO:
376 #if defined(ALLEGRO_CFG_KCM_PULSEAUDIO)
377 if (_al_kcm_pulseaudio_driver.open() == 0) {
378 ALLEGRO_INFO("Using PulseAudio driver\n");
379 _al_kcm_driver = &_al_kcm_pulseaudio_driver;
380 return true;
381 }
382 return false;
383 #else
384 _al_set_error(ALLEGRO_INVALID_PARAM, "PulseAudio not available on this platform");
385 return false;
386 #endif
387
388 case ALLEGRO_AUDIO_DRIVER_DSOUND:
389 #if defined(ALLEGRO_CFG_KCM_DSOUND)
390 if (_al_kcm_dsound_driver.open() == 0) {
391 ALLEGRO_INFO("Using DirectSound driver\n");
392 _al_kcm_driver = &_al_kcm_dsound_driver;
393 return true;
394 }
395 return false;
396 #else
397 _al_set_error(ALLEGRO_INVALID_PARAM, "DirectSound not available on this platform");
398 return false;
399 #endif
400
401 case ALLEGRO_AUDIO_DRIVER_SDL:
402 #if defined(ALLEGRO_SDL)
403 if (_al_kcm_sdl_driver.open() == 0) {
404 ALLEGRO_INFO("Using SDL driver\n");
405 _al_kcm_driver = &_al_kcm_sdl_driver;
406 return true;
407 }
408 return false;
409 #else
410 _al_set_error(ALLEGRO_INVALID_PARAM, "SDL not available on this platform");
411 return false;
412 #endif
413
414 default:
415 _al_set_error(ALLEGRO_INVALID_PARAM, "Invalid audio driver");
416 return false;
417 }
418 }
419
420 /* Function: al_install_audio
421 */
422 bool al_install_audio(void)
423 {
424 bool ret;
425
426 if (_al_kcm_driver)
427 return true;
428
429 /* The destructors are initialised even if the audio driver fails to install
430 * because the user may still create samples.
431 */
432 _al_kcm_init_destructors();
433 _al_add_exit_func(al_uninstall_audio, "al_uninstall_audio");
434
435 ret = do_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT);
436 return ret;
437 }
438
439 /* Function: al_uninstall_audio
440 */
441 void al_uninstall_audio(void)
442 {
443 if (_al_kcm_driver) {
444 _al_kcm_shutdown_default_mixer();
445 _al_kcm_shutdown_destructors();
446 _al_kcm_driver->close();
447 _al_kcm_driver = NULL;
448 }
449 else {
450 _al_kcm_shutdown_destructors();
451 }
452 }
453
454 /* Function: al_is_audio_installed
455 */
456 bool al_is_audio_installed(void)
457 {
458 return _al_kcm_driver ? true : false;
459 }
460
461 /* Function: al_get_allegro_audio_version
462 */
463 uint32_t al_get_allegro_audio_version(void)
464 {
465 return ALLEGRO_VERSION_INT;
466 }
467
468 /* vim: set sts=3 sw=3 et: */