GP2X updates
[libpicofe.git] / linux / sndout_oss.c
CommitLineData
f89d8471 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
b3972d82 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
26d3ca0d
GI
23int sndout_oss_frag_frames = 1;
24
b3972d82 25static int sounddev = -1, mixerdev = -1;
dd59a74b 26static int can_write_safe;
27
f679add7 28#define FRAG_COUNT 4
b3972d82 29
30int 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
b188c2b6 42void sndout_oss_stop(void)
43{
7ceadd99 44#ifdef __GP2X__
45 /* restarting audio on GP2X causes trouble */
46 return;
47#endif
48
b188c2b6 49 if (sounddev < 0)
50 return;
51
52 ioctl(sounddev, SOUND_PCM_SYNC, 0);
53 close(sounddev);
54 sounddev = -1;
55}
56
26d3ca0d 57int sndout_oss_start(int rate, int stereo)
b3972d82 58{
f679add7 59 static int s_oldrate = 0, s_oldstereo = 0;
dd59a74b 60 int frag, bsize, bits, ret;
b3972d82 61
b188c2b6 62 // GP2X: if no settings change, we don't need to do anything,
63 // since audio is never stopped there
f679add7 64 if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo)
b3972d82 65 return 0;
66
b188c2b6 67 sndout_oss_stop();
d747e9d6 68 sounddev = open("/dev/dsp", O_WRONLY);
b3972d82 69 if (sounddev == -1)
70 {
71 perror("open(\"/dev/dsp\")");
d747e9d6 72 sounddev = open("/dev/dsp1", O_WRONLY);
5f7b5155 73 if (sounddev == -1) {
74 perror("open(\"/dev/dsp1\")");
75 return -1;
76 }
b3972d82 77 }
78
26d3ca0d
GI
79 // try to fit sndout_oss_frag_frames (video) frames
80 // worth of sound data in OSS fragment
f679add7 81 // ignore mono because it's unlikely to be used and
82 // both GP2X and Wiz mixes mono to stereo anyway.
26d3ca0d 83 bsize = (sndout_oss_frag_frames * rate / 50) * 4;
dd59a74b 84
85 for (frag = 0; bsize; bsize >>= 1, frag++)
86 ;
b3972d82 87
f679add7 88 frag |= FRAG_COUNT << 16; // fragment count
dd59a74b 89 ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
90 if (ret < 0)
91 perror("SNDCTL_DSP_SETFRAGMENT failed");
92
93 bits = 16;
94 ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
95 if (ret == 0)
96 ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
97 if (ret == 0)
98 ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
99 if (ret < 0)
100 perror("failed to set audio format");
101
b188c2b6 102#ifdef __GP2X__
dd59a74b 103 // not sure if this is still needed (avoiding driver bugs?)
b3972d82 104 usleep(192*1024);
b188c2b6 105#endif
b3972d82 106
dd59a74b 107 printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n",
108 rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff));
b3972d82 109
f679add7 110 s_oldrate = rate; s_oldstereo = stereo;
dd59a74b 111 can_write_safe = 0;
b3972d82 112 return 0;
113}
114
b3972d82 115int sndout_oss_write(const void *buff, int len)
116{
117 return write(sounddev, buff, len);
118}
119
a86e9a3e 120#include "../plat.h"
a33164ff 121
122/* not really non-blocking, just detects if blocking occurs
123 * and starts skipping writes in case it does. */
124int sndout_oss_write_nb(const void *buff, int len)
125{
126 static int lag_counter, skip_counter;
127 unsigned int t;
128 int ret;
129
130 if (lag_counter > 2) {
131 // skip writes if audio starts blocking
132 lag_counter = 0;
133 skip_counter = FRAG_COUNT;
134 }
135
136 if (skip_counter > 0) {
137 skip_counter--;
138 return len;
139 }
140
141 t = plat_get_ticks_ms();
142 ret = sndout_oss_write(buff, len);
143 t = plat_get_ticks_ms() - t;
144 if (t > 1) {
145 // this shouldn't really happen, most likely audio is out of sync
146 lag_counter++;
147 if (lag_counter > 2)
148 printf("audio lag %u\n", t);
149 }
150 else
151 lag_counter = 0;
152
153 return ret;
154}
155
dd59a74b 156int sndout_oss_can_write(int bytes)
157{
158 audio_buf_info bi;
159 int ret;
160
161#ifdef __GP2X__
162 // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason
163 // if called too early, so we work around here
164 if (can_write_safe++ < 8)
165 return 1;
166#endif
167 ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi);
168 if (ret < 0)
169 return 1;
170
f679add7 171 // have enough bytes to write + 1 frag
172 return bi.bytes - bi.fragsize >= bytes ? 1 : 0;
dd59a74b 173}
b3972d82 174
26d3ca0d 175void sndout_oss_wait(void)
b3972d82 176{
26d3ca0d 177 // FIXME?
b3972d82 178 ioctl(sounddev, SOUND_PCM_SYNC, 0);
179}
180
b3972d82 181void sndout_oss_setvol(int l, int r)
182{
183 if (mixerdev < 0) return;
184
185 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
186 l<<=8; l|=r;
187 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
188}
189
b3972d82 190void sndout_oss_exit(void)
191{
192 if (sounddev >= 0) close(sounddev); sounddev = -1;
193 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;
194}
195