1 /* sound output via OSS */
7 #include <sys/soundcard.h>
10 #include "sndout_oss.h"
12 static int sounddev = -1, mixerdev = -1;
14 int sndout_oss_init(void)
16 if (mixerdev >= 0) close(mixerdev);
17 mixerdev = open("/dev/mixer", O_RDWR);
20 perror("open(\"/dev/mixer\")");
27 int sndout_oss_start(int rate, int bits, int stereo)
29 static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
30 int frag = 0, bsize, buffers, ret;
32 // if no settings change, we don't need to do anything,
33 // since audio is never stopped
34 if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo)
37 if (sounddev >= 0) close(sounddev);
38 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
41 perror("open(\"/dev/dsp\")");
45 // calculate buffer size
46 // this is tuned for GP2X
49 if (rate > 22050) { bsize*=4; buffers*=2; }
50 while ((bsize>>=1)) frag++;
51 frag |= buffers<<16; // 16 buffers
52 ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
53 if (ret) perror("SNDCTL_DSP_SETFRAGMENT failed");
55 ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
56 ret |= ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
57 ret |= ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
58 if (ret) printf("failed to set audio format\n");
61 printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
62 rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
64 s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
69 int sndout_oss_write(const void *buff, int len)
71 return write(sounddev, buff, len);
75 void sndout_oss_sync(void)
77 ioctl(sounddev, SOUND_PCM_SYNC, 0);
81 void sndout_oss_setvol(int l, int r)
83 if (mixerdev < 0) return;
85 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
87 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
91 void sndout_oss_exit(void)
93 if (sounddev >= 0) close(sounddev); sounddev = -1;
94 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;