support multiple sound drivers, add sdl
[libpicofe.git] / linux / sndout_oss.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  *  - MAME license.
9  * See the COPYING file in the top-level directory.
10  */
11
12 /* sound output via OSS */
13 #include <stdio.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <sys/ioctl.h>
18 #include <sys/soundcard.h>
19 #include <unistd.h>
20
21 #include "sndout_oss.h"
22
23 int sndout_oss_frag_frames = 1;
24
25 static int sounddev = -1, mixerdev = -1;
26 static int can_write_safe;
27
28 #define FRAG_COUNT 4
29
30 int sndout_oss_init(void)
31 {
32         if (mixerdev >= 0) close(mixerdev);
33         mixerdev = open("/dev/mixer", O_RDWR);
34         if (mixerdev == -1)
35         {
36                 perror("open(\"/dev/mixer\")");
37         }
38
39         return 0;
40 }
41
42 void sndout_oss_stop(void)
43 {
44         if (sounddev < 0)
45                 return;
46
47         ioctl(sounddev, SOUND_PCM_SYNC, 0);
48         close(sounddev);
49         sounddev = -1;
50 }
51
52 int sndout_oss_start(int rate, int stereo)
53 {
54         static int s_oldrate = 0, s_oldstereo = 0;
55         int frag, bsize, bits, ret;
56
57         // GP2X: if no settings change, we don't need to do anything,
58         // since audio is never stopped there
59         if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo)
60                 return 0;
61
62         sndout_oss_stop();
63         sounddev = open("/dev/dsp", O_WRONLY);
64         if (sounddev == -1)
65         {
66                 perror("open(\"/dev/dsp\")");
67                 sounddev = open("/dev/dsp1", O_WRONLY);
68                 if (sounddev == -1) {
69                         perror("open(\"/dev/dsp1\")");
70                         return -1;
71                 }
72         }
73
74         // try to fit sndout_oss_frag_frames (video) frames
75         // worth of sound data in OSS fragment
76         // ignore mono because it's unlikely to be used and
77         // both GP2X and Wiz mixes mono to stereo anyway.
78         bsize = (sndout_oss_frag_frames * rate / 50) * 4;
79
80         for (frag = 0; bsize; bsize >>= 1, frag++)
81                 ;
82
83         frag |= FRAG_COUNT << 16;       // fragment count
84         ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
85         if (ret < 0)
86                 perror("SNDCTL_DSP_SETFRAGMENT failed");
87
88         bits = 16;
89         ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
90         if (ret == 0)
91                 ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
92         if (ret == 0)
93                 ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
94         if (ret < 0)
95                 perror("failed to set audio format");
96
97 #ifdef __GP2X__
98         // not sure if this is still needed (avoiding driver bugs?)
99         usleep(192*1024);
100 #endif
101
102         printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n",
103                 rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff));
104
105         s_oldrate = rate; s_oldstereo = stereo;
106         can_write_safe = 0;
107         return 0;
108 }
109
110 int sndout_oss_write(const void *buff, int len)
111 {
112         return write(sounddev, buff, len);
113 }
114
115 #include "../plat.h"
116
117 /* not really non-blocking, just detects if blocking occurs
118  * and starts skipping writes in case it does. */
119 int sndout_oss_write_nb(const void *buff, int len)
120 {
121         static int lag_counter, skip_counter;
122         unsigned int t;
123         int ret;
124
125         if (lag_counter > 2) {
126                 // skip writes if audio starts blocking
127                 lag_counter = 0;
128                 skip_counter = FRAG_COUNT;
129         }
130
131         if (skip_counter > 0) {
132                 skip_counter--;
133                 return len;
134         }
135
136         t = plat_get_ticks_ms();
137         ret = sndout_oss_write(buff, len);
138         t = plat_get_ticks_ms() - t;
139         if (t > 1) {
140                 // this shouldn't really happen, most likely audio is out of sync
141                 lag_counter++;
142                 if (lag_counter > 2)
143                         printf("audio lag %u\n", t);
144         }
145         else
146                 lag_counter = 0;
147
148         return ret;
149 }
150
151 int sndout_oss_can_write(int bytes)
152 {
153         audio_buf_info bi;
154         int ret;
155
156 #ifdef __GP2X__
157         // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason
158         // if called too early, so we work around here
159         if (can_write_safe++ < 8)
160                 return 1;
161 #endif
162         ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi);
163         if (ret < 0)
164                 return 1;
165
166         // have enough bytes to write + 1 frag
167         return bi.bytes - bi.fragsize >= bytes ? 1 : 0;
168 }
169
170 void sndout_oss_wait(void)
171 {
172         // FIXME?
173         ioctl(sounddev, SOUND_PCM_SYNC, 0);
174 }
175
176 void sndout_oss_setvol(int l, int r)
177 {
178         if (mixerdev < 0) return;
179
180         l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
181         l<<=8; l|=r;
182         ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
183 }
184
185 void sndout_oss_exit(void)
186 {
187         if (sounddev >= 0) close(sounddev); sounddev = -1;
188         if (mixerdev >= 0) close(mixerdev); mixerdev = -1;
189 }
190