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 static int sounddev = -1, mixerdev = -1;
24 static int can_write_safe;
28 int sndout_oss_init(void)
30 if (mixerdev >= 0) close(mixerdev);
31 mixerdev = open("/dev/mixer", O_RDWR);
34 perror("open(\"/dev/mixer\")");
40 void sndout_oss_stop(void)
45 ioctl(sounddev, SOUND_PCM_SYNC, 0);
50 int sndout_oss_start(int rate, int stereo, int frames_in_frag)
52 static int s_oldrate = 0, s_oldstereo = 0;
53 int frag, bsize, bits, ret;
55 // GP2X: if no settings change, we don't need to do anything,
56 // since audio is never stopped there
57 if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo)
61 sounddev = open("/dev/dsp", O_WRONLY);
64 perror("open(\"/dev/dsp\")");
65 sounddev = open("/dev/dsp1", O_WRONLY);
67 perror("open(\"/dev/dsp1\")");
72 // try to fit frames_in_frag frames worth of data in fragment
73 // ignore mono because it's unlikely to be used and
74 // both GP2X and Wiz mixes mono to stereo anyway.
75 bsize = (frames_in_frag * rate / 50) * 4;
77 for (frag = 0; bsize; bsize >>= 1, frag++)
80 frag |= FRAG_COUNT << 16; // fragment count
81 ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
83 perror("SNDCTL_DSP_SETFRAGMENT failed");
86 ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
88 ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
90 ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
92 perror("failed to set audio format");
95 // not sure if this is still needed (avoiding driver bugs?)
99 printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n",
100 rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff));
102 s_oldrate = rate; s_oldstereo = stereo;
107 int sndout_oss_write(const void *buff, int len)
109 return write(sounddev, buff, len);
114 /* not really non-blocking, just detects if blocking occurs
115 * and starts skipping writes in case it does. */
116 int sndout_oss_write_nb(const void *buff, int len)
118 static int lag_counter, skip_counter;
122 if (lag_counter > 2) {
123 // skip writes if audio starts blocking
125 skip_counter = FRAG_COUNT;
128 if (skip_counter > 0) {
133 t = plat_get_ticks_ms();
134 ret = sndout_oss_write(buff, len);
135 t = plat_get_ticks_ms() - t;
137 // this shouldn't really happen, most likely audio is out of sync
140 printf("audio lag %u\n", t);
148 int sndout_oss_can_write(int bytes)
154 // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason
155 // if called too early, so we work around here
156 if (can_write_safe++ < 8)
159 ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi);
163 // have enough bytes to write + 1 frag
164 return bi.bytes - bi.fragsize >= bytes ? 1 : 0;
167 void sndout_oss_sync(void)
169 ioctl(sounddev, SOUND_PCM_SYNC, 0);
172 void sndout_oss_setvol(int l, int r)
174 if (mixerdev < 0) return;
176 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
178 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
181 void sndout_oss_exit(void)
183 if (sounddev >= 0) close(sounddev); sounddev = -1;
184 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;