add new libao audio backend
[rofl0r-gnuboy.git] / sys / ao / ao.c
blobb037e49d86e9f91f340197a99564d086c8ee533c
1 #undef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #include <string.h>
5 #include <unistd.h>
6 #include <sys/ioctl.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
13 #include <ao/ao.h>
15 #include "defs.h"
16 #include "pcm.h"
17 #include "rc.h"
19 struct pcm pcm;
21 static int stereo = 1;
22 static int samplerate = 44100;
23 static int sound = 1;
24 static ao_device *device;
25 static ao_sample_format format;
26 static int aodriver;
29 rcvar_t pcm_exports[] =
31 RCV_BOOL("sound", &sound),
32 RCV_INT("stereo", &stereo),
33 RCV_INT("samplerate", &samplerate),
34 RCV_END
38 static void no_sound(void) {
39 pcm.hz = 11025;
40 pcm.len = 4096;
41 pcm.buf = malloc(pcm.len);
42 pcm.pos = 0;
45 void pcm_init()
47 if (!sound)
49 no_sound();
50 return;
53 ao_initialize();
54 format.bits = 8;
55 format.channels = 2;
56 format.rate = samplerate;
57 format.byte_format = AO_FMT_LITTLE;
58 aodriver = ao_default_driver_id();
59 device = ao_open_live(aodriver, &format, NULL);
61 if(!device) {
62 no_sound();
63 return;
66 pcm.stereo = 1;
67 pcm.hz = samplerate;
68 pcm.len = samplerate / 60;
69 pcm.buf = malloc(pcm.len);
72 void pcm_close()
74 if (pcm.buf) free(pcm.buf);
75 memset(&pcm, 0, sizeof pcm);
76 if(device) ao_close(device);
79 int pcm_submit()
81 if (!device)
83 pcm.pos = 0;
84 return 0;
86 if (pcm.buf) ao_play(device, pcm.buf, pcm.pos);
87 pcm.pos = 0;
88 return 1;