fix Wiz timer drift and button name
[picodrive.git] / platform / linux / sndout_oss.c
CommitLineData
e5ab6faf 1/* sound output via OSS */
2#include <stdio.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <sys/ioctl.h>
7#include <sys/soundcard.h>
8#include <unistd.h>
9
10#include "sndout_oss.h"
11
12static int sounddev = -1, mixerdev = -1;
13
14int sndout_oss_init(void)
15{
16 if (mixerdev >= 0) close(mixerdev);
17 mixerdev = open("/dev/mixer", O_RDWR);
18 if (mixerdev == -1)
19 {
20 perror("open(\"/dev/mixer\")");
21 }
22
23 return 0;
24}
25
26
27int sndout_oss_start(int rate, int bits, int stereo)
28{
29 static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
30 int frag = 0, bsize, buffers, ret;
31
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)
35 return 0;
36
37 if (sounddev >= 0) close(sounddev);
38 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
39 if (sounddev == -1)
40 {
41 perror("open(\"/dev/dsp\")");
42 return -1;
43 }
44
45 // calculate buffer size
46 // this is tuned for GP2X
47 buffers = 16;
48 bsize = rate / 32;
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");
54
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");
59 usleep(192*1024);
60
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));
63
64 s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
65 return 0;
66}
67
68
69int sndout_oss_write(const void *buff, int len)
70{
71 return write(sounddev, buff, len);
72}
73
74
75void sndout_oss_sync(void)
76{
77 ioctl(sounddev, SOUND_PCM_SYNC, 0);
78}
79
80
81void sndout_oss_setvol(int l, int r)
82{
83 if (mixerdev < 0) return;
84
85 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
86 l<<=8; l|=r;
87 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
88}
89
90
91void sndout_oss_exit(void)
92{
93 if (sounddev >= 0) close(sounddev); sounddev = -1;
94 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;
95}
96