Commit | Line | Data |
---|---|---|
f89d8471 | 1 | /* |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2009-2010 | |
3 | * | |
4 | * This work is licensed under the terms of any of these licenses | |
5 | * (at your option): | |
6 | * - GNU GPL, version 2 or later. | |
7 | * - GNU LGPL, version 2.1 or later. | |
8 | * - MAME license. | |
9 | * See the COPYING file in the top-level directory. | |
10 | */ | |
11 | ||
b3972d82 | 12 | /* sound output via OSS */ |
13 | #include <stdio.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/stat.h> | |
16 | #include <fcntl.h> | |
17 | #include <sys/ioctl.h> | |
18 | #include <sys/soundcard.h> | |
19 | #include <unistd.h> | |
20 | ||
21 | #include "sndout_oss.h" | |
22 | ||
26d3ca0d | 23 | int sndout_oss_frag_frames = 1; |
39014486 | 24 | int sndout_oss_can_restart = 1; |
26d3ca0d | 25 | |
b3972d82 | 26 | static int sounddev = -1, mixerdev = -1; |
dd59a74b | 27 | static int can_write_safe; |
28 | ||
f679add7 | 29 | #define FRAG_COUNT 4 |
b3972d82 | 30 | |
31 | int sndout_oss_init(void) | |
32 | { | |
33 | if (mixerdev >= 0) close(mixerdev); | |
34 | mixerdev = open("/dev/mixer", O_RDWR); | |
35 | if (mixerdev == -1) | |
36 | { | |
37 | perror("open(\"/dev/mixer\")"); | |
38 | } | |
39 | ||
40 | return 0; | |
41 | } | |
42 | ||
b188c2b6 | 43 | void sndout_oss_stop(void) |
44 | { | |
39014486 | 45 | /* restarting audio on GP2X causes trouble, |
46 | * not restarting on Caanoo causes trouble */ | |
47 | if (!sndout_oss_can_restart) | |
48 | return; | |
7ceadd99 | 49 | |
b188c2b6 | 50 | if (sounddev < 0) |
51 | return; | |
52 | ||
39014486 | 53 | // sync causes trouble on Caanoo.. |
0415ebf1 | 54 | //ioctl(sounddev, SNDCTL_DSP_SYNC, 0); |
39014486 | 55 | |
b188c2b6 | 56 | close(sounddev); |
57 | sounddev = -1; | |
58 | } | |
59 | ||
26d3ca0d | 60 | int sndout_oss_start(int rate, int stereo) |
b3972d82 | 61 | { |
f679add7 | 62 | static int s_oldrate = 0, s_oldstereo = 0; |
dd59a74b | 63 | int frag, bsize, bits, ret; |
b3972d82 | 64 | |
b188c2b6 | 65 | // GP2X: if no settings change, we don't need to do anything, |
66 | // since audio is never stopped there | |
f679add7 | 67 | if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo) |
b3972d82 | 68 | return 0; |
69 | ||
b188c2b6 | 70 | sndout_oss_stop(); |
d747e9d6 | 71 | sounddev = open("/dev/dsp", O_WRONLY); |
b3972d82 | 72 | if (sounddev == -1) |
73 | { | |
74 | perror("open(\"/dev/dsp\")"); | |
d747e9d6 | 75 | sounddev = open("/dev/dsp1", O_WRONLY); |
5f7b5155 | 76 | if (sounddev == -1) { |
77 | perror("open(\"/dev/dsp1\")"); | |
78 | return -1; | |
79 | } | |
b3972d82 | 80 | } |
81 | ||
26d3ca0d GI |
82 | // try to fit sndout_oss_frag_frames (video) frames |
83 | // worth of sound data in OSS fragment | |
f679add7 | 84 | // ignore mono because it's unlikely to be used and |
85 | // both GP2X and Wiz mixes mono to stereo anyway. | |
26d3ca0d | 86 | bsize = (sndout_oss_frag_frames * rate / 50) * 4; |
dd59a74b | 87 | |
88 | for (frag = 0; bsize; bsize >>= 1, frag++) | |
89 | ; | |
b3972d82 | 90 | |
f679add7 | 91 | frag |= FRAG_COUNT << 16; // fragment count |
dd59a74b | 92 | ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag); |
93 | if (ret < 0) | |
94 | perror("SNDCTL_DSP_SETFRAGMENT failed"); | |
95 | ||
96 | bits = 16; | |
97 | ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo); | |
98 | if (ret == 0) | |
99 | ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits); | |
100 | if (ret == 0) | |
101 | ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate); | |
102 | if (ret < 0) | |
103 | perror("failed to set audio format"); | |
104 | ||
b188c2b6 | 105 | #ifdef __GP2X__ |
dd59a74b | 106 | // not sure if this is still needed (avoiding driver bugs?) |
b3972d82 | 107 | usleep(192*1024); |
b188c2b6 | 108 | #endif |
b3972d82 | 109 | |
dd59a74b | 110 | printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n", |
111 | rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff)); | |
b3972d82 | 112 | |
f679add7 | 113 | s_oldrate = rate; s_oldstereo = stereo; |
dd59a74b | 114 | can_write_safe = 0; |
b3972d82 | 115 | return 0; |
116 | } | |
117 | ||
b3972d82 | 118 | int sndout_oss_write(const void *buff, int len) |
119 | { | |
120 | return write(sounddev, buff, len); | |
121 | } | |
122 | ||
a86e9a3e | 123 | #include "../plat.h" |
a33164ff | 124 | |
125 | /* not really non-blocking, just detects if blocking occurs | |
126 | * and starts skipping writes in case it does. */ | |
127 | int sndout_oss_write_nb(const void *buff, int len) | |
128 | { | |
129 | static int lag_counter, skip_counter; | |
130 | unsigned int t; | |
131 | int ret; | |
132 | ||
133 | if (lag_counter > 2) { | |
134 | // skip writes if audio starts blocking | |
135 | lag_counter = 0; | |
136 | skip_counter = FRAG_COUNT; | |
137 | } | |
138 | ||
139 | if (skip_counter > 0) { | |
140 | skip_counter--; | |
141 | return len; | |
142 | } | |
143 | ||
144 | t = plat_get_ticks_ms(); | |
145 | ret = sndout_oss_write(buff, len); | |
146 | t = plat_get_ticks_ms() - t; | |
147 | if (t > 1) { | |
148 | // this shouldn't really happen, most likely audio is out of sync | |
149 | lag_counter++; | |
150 | if (lag_counter > 2) | |
151 | printf("audio lag %u\n", t); | |
152 | } | |
153 | else | |
154 | lag_counter = 0; | |
155 | ||
156 | return ret; | |
157 | } | |
158 | ||
dd59a74b | 159 | int sndout_oss_can_write(int bytes) |
160 | { | |
161 | audio_buf_info bi; | |
162 | int ret; | |
163 | ||
164 | #ifdef __GP2X__ | |
165 | // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason | |
166 | // if called too early, so we work around here | |
167 | if (can_write_safe++ < 8) | |
168 | return 1; | |
169 | #endif | |
170 | ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi); | |
171 | if (ret < 0) | |
172 | return 1; | |
173 | ||
f679add7 | 174 | // have enough bytes to write + 1 frag |
175 | return bi.bytes - bi.fragsize >= bytes ? 1 : 0; | |
dd59a74b | 176 | } |
b3972d82 | 177 | |
26d3ca0d | 178 | void sndout_oss_wait(void) |
b3972d82 | 179 | { |
26d3ca0d | 180 | // FIXME? |
0415ebf1 | 181 | ioctl(sounddev, SNDCTL_DSP_SYNC, 0); |
b3972d82 | 182 | } |
183 | ||
b3972d82 | 184 | void sndout_oss_setvol(int l, int r) |
185 | { | |
186 | if (mixerdev < 0) return; | |
187 | ||
188 | l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r; | |
189 | l<<=8; l|=r; | |
190 | ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/ | |
191 | } | |
192 | ||
b3972d82 | 193 | void sndout_oss_exit(void) |
194 | { | |
795b71c5 | 195 | if (sounddev >= 0) |
196 | close(sounddev); | |
197 | if (mixerdev >= 0) | |
198 | close(mixerdev); | |
199 | sounddev = mixerdev = -1; | |
b3972d82 | 200 | } |
201 |