2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
12 /* sound output via OSS */
14 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/soundcard.h>
21 #include "sndout_oss.h"
23 int sndout_oss_frag_frames = 1;
24 int sndout_oss_can_restart = 1;
26 static int sounddev = -1, mixerdev = -1;
27 static int can_write_safe;
31 int sndout_oss_init(void)
33 if (mixerdev >= 0) close(mixerdev);
34 mixerdev = open("/dev/mixer", O_RDWR);
37 perror("open(\"/dev/mixer\")");
43 void sndout_oss_stop(void)
45 /* restarting audio on GP2X causes trouble,
46 * not restarting on Caanoo causes trouble */
47 if (!sndout_oss_can_restart)
53 // sync causes trouble on Caanoo..
54 //ioctl(sounddev, SNDCTL_DSP_SYNC, 0);
60 int sndout_oss_start(int rate, int stereo)
62 static int s_oldrate = 0, s_oldstereo = 0;
63 int frag, bsize, bits, ret;
65 // GP2X: if no settings change, we don't need to do anything,
66 // since audio is never stopped there
67 if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo)
71 sounddev = open("/dev/dsp", O_WRONLY);
74 perror("open(\"/dev/dsp\")");
75 sounddev = open("/dev/dsp1", O_WRONLY);
77 perror("open(\"/dev/dsp1\")");
82 // try to fit sndout_oss_frag_frames (video) frames
83 // worth of sound data in OSS fragment
84 // ignore mono because it's unlikely to be used and
85 // both GP2X and Wiz mixes mono to stereo anyway.
86 bsize = (sndout_oss_frag_frames * rate / 50) * 4;
88 for (frag = 0; bsize; bsize >>= 1, frag++)
91 frag |= FRAG_COUNT << 16; // fragment count
92 ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
94 perror("SNDCTL_DSP_SETFRAGMENT failed");
97 ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
99 ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
101 ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
103 perror("failed to set audio format");
106 // not sure if this is still needed (avoiding driver bugs?)
110 printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n",
111 rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff));
113 s_oldrate = rate; s_oldstereo = stereo;
118 int sndout_oss_write(const void *buff, int len)
120 return write(sounddev, buff, len);
125 /* not really non-blocking, just detects if blocking occurs
126 * and starts skipping writes in case it does. */
127 int sndout_oss_write_nb(const void *buff, int len)
129 static int lag_counter, skip_counter;
133 if (lag_counter > 2) {
134 // skip writes if audio starts blocking
136 skip_counter = FRAG_COUNT;
139 if (skip_counter > 0) {
144 t = plat_get_ticks_ms();
145 ret = sndout_oss_write(buff, len);
146 t = plat_get_ticks_ms() - t;
148 // this shouldn't really happen, most likely audio is out of sync
151 printf("audio lag %u\n", t);
159 int sndout_oss_can_write(int bytes)
165 // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason
166 // if called too early, so we work around here
167 if (can_write_safe++ < 8)
170 ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi);
174 // have enough bytes to write + 1 frag
175 return bi.bytes - bi.fragsize >= bytes ? 1 : 0;
178 void sndout_oss_wait(void)
181 ioctl(sounddev, SNDCTL_DSP_SYNC, 0);
184 void sndout_oss_setvol(int l, int r)
186 if (mixerdev < 0) return;
188 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
190 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
193 void sndout_oss_exit(void)
195 if (sounddev >= 0) close(sounddev); sounddev = -1;
196 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;