1 /* sound output via OSS */
7 #include <sys/soundcard.h>
10 #include "sndout_oss.h"
12 static int sounddev = -1, mixerdev = -1;
13 static int can_write_safe;
16 int sndout_oss_init(void)
18 if (mixerdev >= 0) close(mixerdev);
19 mixerdev = open("/dev/mixer", O_RDWR);
22 perror("open(\"/dev/mixer\")");
28 int sndout_oss_start(int rate, int frame_samples, int stereo)
30 static int s_oldrate = 0, s_old_fsamples = 0, s_oldstereo = 0;
31 int frag, bsize, bits, ret;
33 // if no settings change, we don't need to do anything,
34 // since audio is never stopped
35 if (rate == s_oldrate && s_old_fsamples == frame_samples && s_oldstereo == stereo)
39 ioctl(sounddev, SOUND_PCM_SYNC, 0);
43 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
46 perror("open(\"/dev/dsp\")");
50 // calculate buffer size. We one to fit 1 frame worth of sound data.
51 // Also ignore mono because both GP2X and Wiz mixes mono to stereo anyway.
52 bsize = frame_samples << 2;
54 for (frag = 0; bsize; bsize >>= 1, frag++)
57 frag |= 16 << 16; // fragment count
58 ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
60 perror("SNDCTL_DSP_SETFRAGMENT failed");
63 ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
65 ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
67 ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
69 perror("failed to set audio format");
71 // not sure if this is still needed (avoiding driver bugs?)
74 printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n",
75 rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff));
77 s_oldrate = rate; s_old_fsamples = frame_samples; s_oldstereo = stereo;
82 int sndout_oss_write(const void *buff, int len)
84 return write(sounddev, buff, len);
87 int sndout_oss_can_write(int bytes)
93 // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason
94 // if called too early, so we work around here
95 if (can_write_safe++ < 8)
98 ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi);
102 // have enough bytes to write + 4 extra frags
103 return bi.bytes - bi.fragsize * 4 >= bytes ? 1 : 0;
106 void sndout_oss_sync(void)
108 ioctl(sounddev, SOUND_PCM_SYNC, 0);
111 void sndout_oss_setvol(int l, int r)
113 if (mixerdev < 0) return;
115 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
117 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
120 void sndout_oss_exit(void)
122 if (sounddev >= 0) close(sounddev); sounddev = -1;
123 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;