rm unused file
[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;
b08ccf71 13static int can_write_safe;
14
e5ab6faf 15
16int sndout_oss_init(void)
17{
18 if (mixerdev >= 0) close(mixerdev);
19 mixerdev = open("/dev/mixer", O_RDWR);
20 if (mixerdev == -1)
21 {
22 perror("open(\"/dev/mixer\")");
23 }
24
25 return 0;
26}
27
b08ccf71 28int sndout_oss_start(int rate, int frame_samples, int stereo)
e5ab6faf 29{
b08ccf71 30 static int s_oldrate = 0, s_old_fsamples = 0, s_oldstereo = 0;
31 int frag, bsize, bits, ret;
e5ab6faf 32
33 // if no settings change, we don't need to do anything,
34 // since audio is never stopped
b08ccf71 35 if (rate == s_oldrate && s_old_fsamples == frame_samples && s_oldstereo == stereo)
e5ab6faf 36 return 0;
37
b08ccf71 38 if (sounddev >= 0) {
39 ioctl(sounddev, SOUND_PCM_SYNC, 0);
40 close(sounddev);
41 }
42
e5ab6faf 43 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
44 if (sounddev == -1)
45 {
46 perror("open(\"/dev/dsp\")");
47 return -1;
48 }
49
b08ccf71 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;
53
54 for (frag = 0; bsize; bsize >>= 1, frag++)
55 ;
e5ab6faf 56
b08ccf71 57 frag |= 16 << 16; // fragment count
58 ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
59 if (ret < 0)
60 perror("SNDCTL_DSP_SETFRAGMENT failed");
61
62 bits = 16;
63 ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
64 if (ret == 0)
65 ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
66 if (ret == 0)
67 ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
68 if (ret < 0)
69 perror("failed to set audio format");
70
71 // not sure if this is still needed (avoiding driver bugs?)
e5ab6faf 72 usleep(192*1024);
73
b08ccf71 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));
e5ab6faf 76
b08ccf71 77 s_oldrate = rate; s_old_fsamples = frame_samples; s_oldstereo = stereo;
78 can_write_safe = 0;
e5ab6faf 79 return 0;
80}
81
e5ab6faf 82int sndout_oss_write(const void *buff, int len)
83{
84 return write(sounddev, buff, len);
85}
86
b08ccf71 87int sndout_oss_can_write(int bytes)
88{
89 audio_buf_info bi;
90 int ret;
91
92#ifdef __GP2X__
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)
96 return 1;
97#endif
98 ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi);
99 if (ret < 0)
100 return 1;
101
102 // have enough bytes to write + 4 extra frags
103 return bi.bytes - bi.fragsize * 4 >= bytes ? 1 : 0;
104}
e5ab6faf 105
106void sndout_oss_sync(void)
107{
108 ioctl(sounddev, SOUND_PCM_SYNC, 0);
109}
110
e5ab6faf 111void sndout_oss_setvol(int l, int r)
112{
113 if (mixerdev < 0) return;
114
115 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
116 l<<=8; l|=r;
117 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
118}
119
e5ab6faf 120void sndout_oss_exit(void)
121{
122 if (sounddev >= 0) close(sounddev); sounddev = -1;
123 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;
124}
125