Debian Codegraph @

src/libout123/modules/oss.c

raw
1 /*
2 oss: audio output via Open Sound System
3
4 copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Michael Hipp
7 */
8
9 #include "../out123_int.h"
10
11 #include <sys/ioctl.h>
12 #include <fcntl.h>
13
14 #ifdef HAVE_LINUX_SOUNDCARD_H
15 #include <linux/soundcard.h>
16 #endif
17
18 #ifdef HAVE_SYS_SOUNDCARD_H
19 #include <sys/soundcard.h>
20 #endif
21
22 #ifdef HAVE_MACHINE_SOUNDCARD_H
23 #include <machine/soundcard.h>
24 #endif
25
26 #ifndef AFMT_S16_NE
27 # ifdef OSS_BIG_ENDIAN
28 # define AFMT_S16_NE AFMT_S16_BE
29 # else
30 # define AFMT_S16_NE AFMT_S16_LE
31 # endif
32 #endif
33
34 #ifndef AFMT_U16_NE
35 # ifdef OSS_BIG_ENDIAN
36 # define AFMT_U16_NE AFMT_U16_BE
37 # else
38 # define AFMT_U16_NE AFMT_U16_LE
39 # endif
40 #endif
41
42 #include "../../common/debug.h"
43
44 struct oss_stuff
45 {
46 int fragment; /* size of one fragment */
47 int nfrag; /* number of fragments */
48 };
49
50 static int rate_best_match_oss(out123_handle *ao)
51 {
52 int ret,dsp_rate;
53
54 if(!ao || ao->fn < 0 || ao->rate < 0) return -1;
55 dsp_rate = ao->rate;
56
57 ret = ioctl(ao->fn, SNDCTL_DSP_SPEED,&dsp_rate);
58 if(ret < 0) return ret;
59 ao->rate = dsp_rate;
60 return 0;
61 }
62
63 static int set_rate_oss(out123_handle *ao)
64 {
65 int dsp_rate;
66 int ret = 0;
67
68 if(ao->rate >= 0) {
69 dsp_rate = ao->rate;
70 ret = ioctl(ao->fn, SNDCTL_DSP_SPEED,&dsp_rate);
71 }
72 return ret;
73 }
74
75 static int set_channels_oss(out123_handle *ao)
76 {
77 int chan = ao->channels - 1;
78 int ret;
79
80 if(ao->channels < 0) return 0;
81
82 ret = ioctl(ao->fn, SNDCTL_DSP_STEREO, &chan);
83 if(chan != (ao->channels-1)) return -1;
84
85 return ret;
86 }
87
88 static int set_format_oss(out123_handle *ao)
89 {
90 int fmts;
91 int sf,ret;
92
93 if(ao->format == -1) return 0;
94
95 switch(ao->format) {
96 case MPG123_ENC_SIGNED_16:
97 default:
98 fmts = AFMT_S16_NE;
99 break;
100 case MPG123_ENC_UNSIGNED_8:
101 fmts = AFMT_U8;
102 break;
103 case MPG123_ENC_SIGNED_8:
104 fmts = AFMT_S8;
105 break;
106 case MPG123_ENC_ULAW_8:
107 fmts = AFMT_MU_LAW;
108 break;
109 case MPG123_ENC_ALAW_8:
110 fmts = AFMT_A_LAW;
111 break;
112 case MPG123_ENC_UNSIGNED_16:
113 fmts = AFMT_U16_NE;
114 break;
115 }
116
117 sf = fmts;
118 ret = ioctl(ao->fn, SNDCTL_DSP_SETFMT, &fmts);
119 if(sf != fmts) return -1;
120
121 return ret;
122 }
123
124
125 static int reset_parameters_oss(out123_handle *ao)
126 {
127 int ret;
128 ret = ioctl(ao->fn, SNDCTL_DSP_RESET, NULL);
129 if(ret < 0 && !AOQUIET) error("Can't reset audio!");
130 ret = set_format_oss(ao);
131 if (ret == -1) goto err;
132 ret = set_channels_oss(ao);
133 if (ret == -1) goto err;
134 ret = set_rate_oss(ao);
135 if (ret == -1) goto err;
136
137 /* Careful here. As per OSS v1.1, the next ioctl() commits the format
138 * set above, so we must issue SNDCTL_DSP_RESET before we're allowed to
139 * change it again. [dk]
140 */
141
142 /* FIXME: this needs re-enabled (but not using global variables this time):
143 if (ioctl(ao->fn, SNDCTL_DSP_GETBLKSIZE, &outburst) == -1 ||
144 outburst > MAXOUTBURST)
145 outburst = MAXOUTBURST;
146 */
147
148 err:
149 return ret;
150 }
151
152
153 static int open_oss(out123_handle *ao)
154 {
155 char usingdefdev = 0;
156 const char *dev;
157
158 if(!ao) return -1;
159
160 dev = ao->device;
161 if(!dev) {
162 dev = "/dev/dsp";
163 usingdefdev = 1;
164 }
165
166 ao->fn = open(dev,O_WRONLY);
167
168 if(ao->fn < 0)
169 {
170 if(usingdefdev) {
171 dev = "/dev/sound/dsp";
172 ao->fn = open(dev,O_WRONLY);
173 if(ao->fn < 0) {
174 if(!AOQUIET) error("Can't open default sound device!");
175 return -1;
176 }
177 } else {
178 if(!AOQUIET) error1("Can't open %s!",dev);
179 return -1;
180 }
181 }
182
183 if(reset_parameters_oss(ao) < 0) {
184 close(ao->fn);
185 return -1;
186 }
187
188 if(ao->gain >= 0) {
189 int e,mask;
190 e = ioctl(ao->fn , SOUND_MIXER_READ_DEVMASK ,&mask);
191 if(e < 0) {
192 if(!AOQUIET) error("audio/gain: Can't get audio device features list.");
193 }
194 else if(mask & SOUND_MASK_PCM) {
195 int gain = (ao->gain<<8)|(ao->gain);
196 e = ioctl(ao->fn, SOUND_MIXER_WRITE_PCM , &gain);
197 }
198 else if(!(mask & SOUND_MASK_VOLUME)) {
199 if(!AOQUIET) error1("audio/gain: setable Volume/PCM-Level not supported by your audio device: %#04x",mask);
200 }
201 else {
202 int gain = (ao->gain<<8)|(ao->gain);
203 e = ioctl(ao->fn, SOUND_MIXER_WRITE_VOLUME , &gain);
204 }
205 }
206
207 return ao->fn;
208 }
209
210
211
212 /*
213 * get formats for specific channel/rate parameters
214 */
215 static int get_formats_oss(out123_handle *ao)
216 {
217 int fmt = 0;
218 int r = ao->rate;
219 int c = ao->channels;
220 int i;
221
222 static int fmts[] = {
223 MPG123_ENC_ULAW_8 , MPG123_ENC_SIGNED_16 ,
224 MPG123_ENC_UNSIGNED_8 , MPG123_ENC_SIGNED_8 ,
225 MPG123_ENC_UNSIGNED_16 , MPG123_ENC_ALAW_8
226 };
227
228 /* Reset is required before we're allowed to set the new formats. [dk] */
229 ioctl(ao->fn, SNDCTL_DSP_RESET, NULL);
230
231 for(i=0;i<6;i++) {
232 ao->format = fmts[i];
233 if(set_format_oss(ao) < 0) {
234 continue;
235 }
236 ao->channels = c;
237 if(set_channels_oss(ao) < 0) {
238 continue;
239 }
240 ao->rate = r;
241 if(rate_best_match_oss(ao) < 0) {
242 continue;
243 }
244 if( (ao->rate*100 > r*(100-AUDIO_RATE_TOLERANCE)) && (ao->rate*100 < r*(100+AUDIO_RATE_TOLERANCE)) ) {
245 fmt |= fmts[i];
246 }
247 }
248
249
250 #if 0
251 if(ioctl(ao->fn,SNDCTL_DSP_GETFMTS,&fmts) < 0) {
252 if(!AOQUIET) error("Failed to get SNDCTL_DSP_GETFMTS");
253 return -1;
254 }
255
256 if(fmts & AFMT_MU_LAW)
257 ret |= MPG123_ENC_ULAW_8;
258 if(fmts & AFMT_S16_NE)
259 ret |= MPG123_ENC_SIGNED_16;
260 if(fmts & AFMT_U8)
261 ret |= MPG123_ENC_UNSIGNED_8;
262 if(fmts & AFMT_S8)
263 ret |= MPG123_ENC_SIGNED_8;
264 if(fmts & AFMT_U16_NE)
265 ret |= MPG123_ENC_UNSIGNED_16;
266 if(fmts & AFMT_A_LAW)
267 ret |= MPG123_ENC_ALAW_8;
268 #endif
269
270 return fmt;
271 }
272
273 static int write_oss(out123_handle *ao,unsigned char *buf,int len)
274 {
275 return write(ao->fn,buf,len);
276 }
277
278 static int close_oss(out123_handle *ao)
279 {
280 close(ao->fn);
281 return 0;
282 }
283
284 static void flush_oss(out123_handle *ao)
285 {
286 }
287
288
289
290
291 static int init_oss(out123_handle* ao)
292 {
293 if (ao==NULL) return -1;
294
295 /* Set callbacks */
296 ao->open = open_oss;
297 ao->flush = flush_oss;
298 ao->write = write_oss;
299 ao->get_formats = get_formats_oss;
300 ao->close = close_oss;
301
302 /* Success */
303 return 0;
304 }
305
306
307
308 /*
309 Module information data structure
310 */
311 mpg123_module_t mpg123_output_module_info = {
312 /* api_version */ MPG123_MODULE_API_VERSION,
313 /* name */ "oss",
314 /* description */ "Output audio using OSS",
315 /* revision */ "$Rev: 5386 $",
316 /* handle */ NULL,
317
318 /* init_output */ init_oss,
319 };
320
321