Debian Codegraph @

addons/audio/oss.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Open Sound System sound driver.
12 *
13 * By Milan Mimica.
14 *
15 * See readme.txt for copyright information.
16 */
17
18 #include "allegro5/allegro.h"
19 #include "allegro5/internal/aintern_audio.h"
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <sys/ioctl.h>
25 #include <string.h>
26 #include <poll.h>
27
28 ALLEGRO_DEBUG_CHANNEL("oss")
29
30 #if defined ALLEGRO_HAVE_SOUNDCARD_H
31 #include <soundcard.h>
32 #elif defined ALLEGRO_HAVE_SYS_SOUNDCARD_H
33 #include <sys/soundcard.h>
34 #elif defined ALLEGRO_HAVE_LINUX_SOUNDCARD_H
35 #include <linux/soundcard.h>
36 #elif defined ALLEGRO_HAVE_MACHINE_SOUNDCARD_H
37 #include <machine/soundcard.h>
38 #endif
39
40 #if OSS_VERSION >= 0x040000
41 #define OSS_VER_4
42 #else
43 #define OSS_VER_3
44 #endif
45
46 #ifndef AFMT_S16_NE
47 #ifdef ALLEGRO_BIG_ENDIAN
48 #define AFMT_S16_NE AFMT_S16_BE
49 #else
50 #define AFMT_S16_NE AFMT_S16_LE
51 #endif
52 #endif
53 #ifndef AFMT_U16_NE
54 #ifdef ALLEGRO_BIG_ENDIAN
55 #define AFMT_U16_NE AFMT_U16_BE
56 #else
57 #define AFMT_U16_NE AFMT_U16_LE
58 #endif
59 #endif
60
61
62 /* Audio device used by OSS3.
63 * Make this configurable. */
64 static const char* oss_audio_device_ver3 = "/dev/dsp";
65
66 /* Audio device is dynamically retrived in OSS4. */
67 static char oss_audio_device[512];
68
69 /* timing policy (between 0 and 10), used by OSS4
70 * Make this configurable? */
71 #ifdef OSS_VER_4
72 static const int oss_timing_policy = 5;
73 #endif
74
75 /* Fragment size, used by OSS3
76 * Make this configurable? */
77 static int oss_fragsize = (8 << 16) | (10);
78
79 /* Auxiliary buffer used to store silence. */
80 #define SIL_BUF_SIZE 1024
81
82 static bool using_ver_4;
83
84
85 typedef struct OSS_VOICE {
86 int fd;
87 int volume;
88
89 /* Copied from the parent ALLEGRO_VOICE. Used for convenince. */
90 unsigned int len; /* in frames */
91 unsigned int frame_size; /* in bytes */
92
93 volatile bool stopped;
94 volatile bool stop;
95
96 ALLEGRO_THREAD *poll_thread;
97 } OSS_VOICE;
98
99
100 #ifdef OSS_VER_4
101 static int oss_open_ver4()
102 {
103 int mixer_fd, i;
104 oss_sysinfo sysinfo;
105
106 if ((mixer_fd = open("/dev/mixer", O_RDWR, 0)) == -1) {
107 switch (errno) {
108 case ENXIO:
109 case ENODEV:
110 ALLEGRO_ERROR("Open Sound System is not running in your system.\n");
111 break;
112
113 case ENOENT:
114 ALLEGRO_ERROR("No /dev/mixer device available in your system.\n");
115 ALLEGRO_ERROR("Perhaps Open Sound System is not installed or "
116 "running.\n");
117 break;
118
119 default:
120 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
121 }
122
123 return 1;
124 }
125
126 if (ioctl(mixer_fd, SNDCTL_SYSINFO, &sysinfo) == -1) {
127 if (errno == ENXIO) {
128 ALLEGRO_ERROR("OSS has not detected any supported sound hardware in "
129 "your system.\n");
130 }
131 else if (errno == EINVAL) {
132 ALLEGRO_INFO("The version of OSS installed on the system is not "
133 "compatible with OSS4.\n");
134 }
135 else
136 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
137
138 close(mixer_fd);
139 return 1;
140 }
141
142 /* Some OSS implementations (ALSA emulation) don't fail on SNDCTL_SYSINFO even
143 * though they don't support OSS4. They *seem* to set numcards to 0. */
144 if (sysinfo.numcards < 1) {
145 ALLEGRO_WARN("The version of OSS installed on the system is not "
146 "compatible with OSS4.\n");
147 return 1;
148 }
149
150 ALLEGRO_INFO("OSS Version: %s\n", sysinfo.version);
151 ALLEGRO_INFO("Found %i sound cards.\n", sysinfo.numcards);
152
153 for (i = 0; i < sysinfo.numcards; i++) {
154 oss_audioinfo audioinfo;
155 memset(&audioinfo, 0, sizeof(oss_audioinfo));
156 audioinfo.dev = i;
157
158 ALLEGRO_INFO("Trying sound card no. %i ...\n", audioinfo.dev);
159
160 ioctl(mixer_fd, SNDCTL_AUDIOINFO, &audioinfo);
161
162 if (audioinfo.enabled) {
163 if (strlen(audioinfo.devnode)) {
164 strncpy(oss_audio_device, audioinfo.devnode, 511);
165 }
166 else if (audioinfo.legacy_device != -1) {
167 sprintf(oss_audio_device, "/dev/dsp%i", audioinfo.legacy_device);
168 }
169 else {
170 ALLEGRO_ERROR("Cannot find device name.\n");
171 }
172
173 ALLEGRO_INFO("Using device: %s\n", oss_audio_device);
174
175 break;
176 }
177 else {
178 ALLEGRO_INFO("Device disabled.\n");
179 }
180 }
181
182 if (i == sysinfo.numcards) {
183 ALLEGRO_ERROR("Couldn't find a suitable device.\n");
184 close(mixer_fd);
185 return 1;
186 }
187
188 close(mixer_fd);
189
190 using_ver_4 = true;
191
192 return 0;
193 }
194 #endif
195
196 static int oss_open_ver3(void)
197 {
198 const char *config_device;
199 config_device = al_get_config_value(al_get_system_config(),
200 "oss", "device");
201 if (config_device && config_device[0] != '\0')
202 oss_audio_device_ver3 = config_device;
203
204 int fd = open(oss_audio_device_ver3, O_WRONLY);
205 if (fd == -1) {
206 switch (errno) {
207 case ENXIO:
208 case ENODEV:
209 ALLEGRO_ERROR("Open Sound System is not running in your "
210 "system.\n");
211 break;
212
213 case ENOENT:
214 ALLEGRO_ERROR("No '%s' device available in your system.\n",
215 oss_audio_device_ver3);
216 ALLEGRO_ERROR("Perhaps Open Sound System is not installed "
217 "or running.\n");
218 break;
219
220 default:
221 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
222 }
223
224 return 1;
225 }
226
227 close(fd);
228 strncpy(oss_audio_device, oss_audio_device_ver3, 511);
229 ALLEGRO_INFO("Using device: %s\n", oss_audio_device);
230
231 using_ver_4 = false;
232
233 return 0;
234 }
235
236
237 static int oss_open(void)
238 {
239 bool force_oss3 = false;
240 const char *force_oss3_cfg;
241 force_oss3_cfg = al_get_config_value(al_get_system_config(), "oss",
242 "force_ver3");
243 if (force_oss3_cfg && force_oss3_cfg[0] != '\0')
244 force_oss3 = strcmp(force_oss3_cfg, "yes") ? false : true;
245
246 if (force_oss3) {
247 ALLEGRO_WARN("Skipping OSS4 probe.\n");
248 }
249
250 #ifdef OSS_VER_4
251 bool inited = false;
252 if (!force_oss3) {
253 if (oss_open_ver4())
254 ALLEGRO_WARN("OSS ver. 4 init failed, trying ver. 3...\n");
255 else
256 inited = true;
257 }
258 if (!inited && oss_open_ver3()) {
259 ALLEGRO_ERROR("Failed to init OSS.\n");
260 return 1;
261 }
262 #else
263 ALLEGRO_INFO("OSS4 support not compiled in. Skipping OSS4 probe.\n");
264 if (oss_open_ver3()) {
265 ALLEGRO_ERROR("Failed to init OSS.\n");
266 return 1;
267 }
268 #endif
269
270 return 0;
271 }
272
273
274 static void oss_close(void)
275 {
276 }
277
278
279 static void oss_deallocate_voice(ALLEGRO_VOICE *voice)
280 {
281 OSS_VOICE *oss_voice = voice->extra;
282
283 /* We do NOT hold the voice mutex here, so this does NOT result in a
284 * deadlock when oss_update calls _al_voice_update (which tries to
285 * acquire the voice->mutex).
286 */
287 al_join_thread(oss_voice->poll_thread, NULL);
288 al_destroy_thread(oss_voice->poll_thread);
289
290 close(oss_voice->fd);
291 al_free(voice->extra);
292 voice->extra = NULL;
293 }
294
295
296 static int oss_start_voice(ALLEGRO_VOICE *voice)
297 {
298 OSS_VOICE *ex_data = voice->extra;
299 ex_data->stop = false;
300 return 0;
301 }
302
303
304 static int oss_stop_voice(ALLEGRO_VOICE *voice)
305 {
306 OSS_VOICE *ex_data = voice->extra;
307
308 ex_data->stop = true;
309 if (!voice->is_streaming) {
310 voice->attached_stream->pos = 0;
311 }
312
313 while (!ex_data->stopped)
314 al_rest(0.001);
315
316 return 0;
317 }
318
319
320 static int oss_load_voice(ALLEGRO_VOICE *voice, const void *data)
321 {
322 OSS_VOICE *ex_data = voice->extra;
323
324 /*
325 * One way to support backward playing would be to do like alsa driver does:
326 * mmap(2) the FD and write reversed samples into that. To much trouble for
327 * an optional feature IMO. -- milan
328 */
329 if (voice->attached_stream->loop == ALLEGRO_PLAYMODE_BIDIR) {
330 ALLEGRO_INFO("Backwards playing not supported by the driver.\n");
331 return -1;
332 }
333
334 voice->attached_stream->pos = 0;
335 ex_data->len = voice->attached_stream->spl_data.len;
336
337 return 0;
338 (void)data;
339 }
340
341
342 static void oss_unload_voice(ALLEGRO_VOICE *voice)
343 {
344 (void)voice;
345 }
346
347
348 static bool oss_voice_is_playing(const ALLEGRO_VOICE *voice)
349 {
350 OSS_VOICE *ex_data = voice->extra;
351 return !ex_data->stopped;
352 }
353
354
355 static unsigned int oss_get_voice_position(const ALLEGRO_VOICE *voice)
356 {
357 return voice->attached_stream->pos;
358 }
359
360
361 static int oss_set_voice_position(ALLEGRO_VOICE *voice, unsigned int val)
362 {
363 voice->attached_stream->pos = val;
364 return 0;
365 }
366
367
368
369 /*
370 * Updates the supplied non-streaming voice.
371 * buf - Returns a pointer to the buffer containing sample data.
372 * bytes - The requested size of the sample data buffer. Returns the actual
373 * size of returned the buffer.
374 * Updates 'stop', 'pos' and 'reversed' fields of the supplied voice to the
375 * future position.
376 */
377 static int oss_update_nonstream_voice(ALLEGRO_VOICE *voice, void **buf, int *bytes)
378 {
379 OSS_VOICE *oss_voice = voice->extra;
380 int bpos = voice->attached_stream->pos * oss_voice->frame_size;
381 int blen = oss_voice->len * oss_voice->frame_size;
382
383 *buf = (char *)voice->attached_stream->spl_data.buffer.ptr + bpos;
384
385 if (bpos + *bytes > blen) {
386 *bytes = blen - bpos;
387 if (voice->attached_stream->loop == ALLEGRO_PLAYMODE_ONCE) {
388 oss_voice->stop = true;
389 voice->attached_stream->pos = 0;
390 }
391 if (voice->attached_stream->loop == ALLEGRO_PLAYMODE_LOOP) {
392 voice->attached_stream->pos = 0;
393 }
394 /*else if (voice->attached_stream->loop == ALLEGRO_PLAYMODE_BIDIR) {
395 oss_voice->reversed = true;
396 voice->attached_stream->pos = oss_voice->len;
397 }*/
398 return 1;
399 }
400 else
401 voice->attached_stream->pos += *bytes / oss_voice->frame_size;
402
403 return 0;
404 }
405
406
407 static void oss_update_silence(ALLEGRO_VOICE *voice, OSS_VOICE *oss_voice)
408 {
409 char sil_buf[SIL_BUF_SIZE];
410 unsigned int silent_samples;
411
412 silent_samples = SIL_BUF_SIZE /
413 (al_get_audio_depth_size(voice->depth) *
414 al_get_channel_count(voice->chan_conf));
415 al_fill_silence(sil_buf, silent_samples, voice->depth, voice->chan_conf);
416 if (write(oss_voice->fd, sil_buf, SIL_BUF_SIZE) == -1) {
417 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
418 }
419 }
420
421
422 static void* oss_update(ALLEGRO_THREAD *self, void *arg)
423 {
424 ALLEGRO_VOICE *voice = arg;
425 OSS_VOICE *oss_voice = voice->extra;
426 (void)self;
427
428 while (!al_get_thread_should_stop(self)) {
429 /*
430 For possible eventual non-blocking mode:
431
432 audio_buf_info bi;
433
434 if (ioctl(oss_voice->fd, SNDCTL_DSP_GETOSPACE, &bi) == -1) {
435 ALLEGRO_ERROR("Error SNDCTL_DSP_GETOSPACE, errno=%i (%s)\n",
436 errno, strerror(errno));
437 return NULL;
438 }
439
440 len = bi.bytes;
441 */
442
443 /* How many bytes are we supposed to try to write at once? */
444 unsigned int frames = 1024;
445
446 if (oss_voice->stop && !oss_voice->stopped) {
447 oss_voice->stopped = true;
448 }
449
450 if (!oss_voice->stop && oss_voice->stopped) {
451 oss_voice->stopped = false;
452 }
453
454 if (!voice->is_streaming && !oss_voice->stopped) {
455 void *buf;
456 int bytes = frames * oss_voice->frame_size;
457
458 oss_update_nonstream_voice(voice, &buf, &bytes);
459 frames = bytes / oss_voice->frame_size;
460 if (write(oss_voice->fd, buf, bytes) == -1) {
461 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
462 if (errno != EINTR)
463 return NULL;
464 }
465 }
466 else if (voice->is_streaming && !oss_voice->stopped) {
467 const void *data = _al_voice_update(voice, voice->mutex, &frames);
468 if (data == NULL) {
469 oss_update_silence(voice, oss_voice);
470 continue;
471 }
472 if (write(oss_voice->fd, data, frames * oss_voice->frame_size) == -1) {
473 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
474 if (errno != EINTR)
475 return NULL;
476 }
477 }
478 else {
479 /* If stopped just fill with silence. */
480 oss_update_silence(voice, oss_voice);
481 }
482 }
483
484 return NULL;
485 }
486
487
488 static int oss_allocate_voice(ALLEGRO_VOICE *voice)
489 {
490 int format;
491 int chan_count;
492
493 OSS_VOICE *ex_data = al_calloc(1, sizeof(OSS_VOICE));
494 if (!ex_data)
495 return 1;
496
497 ex_data->fd = open(oss_audio_device, O_WRONLY/*, O_NONBLOCK*/);
498 if (ex_data->fd == -1) {
499 ALLEGRO_ERROR("Failed to open audio device '%s'.\n",
500 oss_audio_device);
501 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
502 al_free(ex_data);
503 return 1;
504 }
505
506 chan_count = al_get_channel_count(voice->chan_conf);
507
508 ex_data->frame_size = chan_count * al_get_audio_depth_size(voice->depth);
509 if (!ex_data->frame_size)
510 goto Error;
511
512 ex_data->stop = true;
513 ex_data->stopped = true;
514
515 if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT8)
516 format = AFMT_S8;
517 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_UINT8)
518 format = AFMT_U8;
519 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT16)
520 format = AFMT_S16_NE;
521 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_UINT16)
522 format = AFMT_U16_NE;
523 #ifdef OSS_VER_4
524 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT24)
525 format = AFMT_S24_NE;
526 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_FLOAT32)
527 format = AFMT_FLOAT;
528 #endif
529 else {
530 ALLEGRO_ERROR("Unsupported OSS sound format.\n");
531 goto Error;
532 }
533
534 int tmp_format = format;
535 int tmp_chan_count = chan_count;
536 unsigned int tmp_freq = voice->frequency;
537 int tmp_oss_fragsize = oss_fragsize;
538
539 if (using_ver_4) {
540 #ifdef OSS_VER_4
541 int tmp_oss_timing_policy = oss_timing_policy;
542 if (ioctl(ex_data->fd, SNDCTL_DSP_POLICY, &tmp_oss_timing_policy) == -1) {
543 ALLEGRO_ERROR("Failed to set_timig policity to '%i'.\n",
544 tmp_oss_timing_policy);
545 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
546 goto Error;
547 }
548 ALLEGRO_INFO("Accepted timing policy value: %i\n", tmp_oss_timing_policy);
549 #endif
550 }
551 else {
552 if (ioctl(ex_data->fd, SNDCTL_DSP_SETFRAGMENT, &tmp_oss_fragsize) == -1) {
553 ALLEGRO_ERROR("Failed to set fragment size.\n");
554 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
555 goto Error;
556 }
557 }
558
559 if (ioctl(ex_data->fd, SNDCTL_DSP_SETFMT, &tmp_format) == -1) {
560 ALLEGRO_ERROR("Failed to set sample format.\n");
561 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
562 goto Error;
563 }
564 if (tmp_format != format) {
565 ALLEGRO_ERROR("Sample format not supported by the driver.\n");
566 goto Error;
567 }
568
569 if (ioctl(ex_data->fd, SNDCTL_DSP_CHANNELS, &tmp_chan_count)) {
570 ALLEGRO_ERROR("Failed to set channel count.\n");
571 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
572 goto Error;
573 }
574 if (tmp_chan_count != chan_count) {
575 ALLEGRO_ERROR("Requested sample channe count %i, got %i.\n",
576 tmp_chan_count, chan_count);
577 }
578
579 if (ioctl(ex_data->fd, SNDCTL_DSP_SPEED, &tmp_freq) == -1) {
580 ALLEGRO_ERROR("Failed to set sample rate.\n");
581 ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
582 goto Error;
583 }
584 if (voice->frequency != tmp_freq) {
585 ALLEGRO_ERROR("Requested sample rate %u, got %iu.\n", voice->frequency,
586 tmp_freq);
587 }
588
589 voice->extra = ex_data;
590 ex_data->poll_thread = al_create_thread(oss_update, (void*)voice);
591 al_start_thread(ex_data->poll_thread);
592
593 return 0;
594
595 Error:
596 close(ex_data->fd);
597 al_free(ex_data);
598 return 1;
599 }
600
601
602 ALLEGRO_AUDIO_DRIVER _al_kcm_oss_driver =
603 {
604 "OSS",
605
606 oss_open,
607 oss_close,
608
609 oss_allocate_voice,
610 oss_deallocate_voice,
611
612 oss_load_voice,
613 oss_unload_voice,
614
615 oss_start_voice,
616 oss_stop_voice,
617
618 oss_voice_is_playing,
619
620 oss_get_voice_position,
621 oss_set_voice_position,
622
623 NULL,
624 NULL,
625
626 NULL
627 };
628
629 /* vim: set sts=3 sw=3 et: */