Debian Codegraph @

rplayd/audio/audio_oss.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: audio_oss.c,v 1.5 1999/03/10 07:58:10 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #include "rplayd.h"
27
28 /*
29 * System audio include files:
30 */
31 #include <sys/types.h>
32 #include <sys/file.h>
33 #include <sys/ioctl.h>
34 #include <sys/fcntl.h>
35 #include <sys/errno.h>
36 #include <errno.h>
37 #include <sys/soundcard.h>
38
39 /*
40 * External variables:
41 */
42 extern char *rplay_audio_device;
43 extern int rplay_audio_sample_rate;
44 extern int rplay_audio_channels;
45 extern int rplay_audio_precision;
46 extern int rplay_audio_format;
47 extern int rplay_audio_port;
48 extern int rplay_audio_fragsize;
49 extern int optional_sample_rate;
50 extern int optional_precision;
51 extern int optional_channels;
52 extern int optional_format;
53 extern int optional_port;
54 extern int optional_fragsize;
55
56 /*
57 * Internal variables:
58 */
59 static int rplay_audio_fd = -1;
60 static int rplay_audio_mixer_fd = -1;
61
62 /*
63 * Initialize the audio device.
64 * This routine must set the following external variables:
65 * rplay_audio_sample_rate
66 * rplay_audio_precision
67 * rplay_audio_channels
68 * rplay_audio_format
69 *
70 * and may use the following optional parameters:
71 * optional_sample_rate
72 * optional_precision
73 * optional_channels
74 * optional_format
75 *
76 * optional_* variables with values of zero should be ignored.
77 *
78 * Return 0 on success and -1 on error.
79 */
80 int
81 rplay_audio_init()
82 {
83 int n;
84
85 if (rplay_audio_fd == -1)
86 {
87 rplay_audio_open();
88 if (rplay_audio_fd == -1)
89 {
90 report(REPORT_ERROR, "rplay_audio_init: cannot open %s\n", rplay_audio_device);
91 return -1;
92 }
93 }
94
95 /* /dev/audio */
96 if (strcmp(rplay_audio_device, "/dev/audio") == 0)
97 {
98 rplay_audio_sample_rate = 8000;
99 rplay_audio_precision = 8;
100 rplay_audio_channels = 1;
101 rplay_audio_format = RPLAY_FORMAT_ULAW;
102 rplay_audio_set_port();
103 }
104 /* /dev/dsp */
105 else if (strcmp(rplay_audio_device, "/dev/dsp") == 0)
106 {
107 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 44100;
108 rplay_audio_precision = optional_precision ? optional_precision : 16;
109 rplay_audio_channels = optional_channels ? optional_channels : 2;
110 rplay_audio_set_port();
111
112 /* Precision */
113 n = rplay_audio_precision;
114 if (ioctl(rplay_audio_fd, SNDCTL_DSP_SETFMT, &n) == -1)
115 {
116 report(REPORT_ERROR, "rplay_audio_init: can't set audio precision to %d (%d)\n",
117 rplay_audio_precision, n);
118 return -1;
119 }
120 if (n != rplay_audio_precision)
121 {
122 report(REPORT_NOTICE, "rplay_audio_init: audio precision changed from %d to %d\n",
123 rplay_audio_precision, n);
124 rplay_audio_precision = n;
125 }
126
127 /* Channels */
128 n = rplay_audio_channels;
129 if (ioctl(rplay_audio_fd, SNDCTL_DSP_CHANNELS, &n) == -1)
130 {
131 report(REPORT_ERROR, "rplay_audio_init: can't set audio channels to %d (%d)\n",
132 rplay_audio_channels, n);
133 return -1;
134 }
135 if (n != rplay_audio_channels)
136 {
137 report(REPORT_NOTICE, "rplay_audio_init: audio channels changed from %d to %d\n",
138 rplay_audio_channels, n);
139 rplay_audio_channels = n;
140 }
141
142 /* Sample rate */
143 n = rplay_audio_sample_rate;
144 ioctl(rplay_audio_fd, SNDCTL_DSP_SYNC, NULL);
145 if (ioctl(rplay_audio_fd, SNDCTL_DSP_SPEED, &n) == -1)
146 {
147 report(REPORT_ERROR, "rplay_audio_init: can't set audio sample rate to %d (%d)\n",
148 rplay_audio_sample_rate, n);
149 return -1;
150 }
151 if (n != rplay_audio_sample_rate)
152 {
153 report(REPORT_NOTICE, "rplay_audio_init: audio sample rate changed from %d to %d\n",
154 rplay_audio_sample_rate, n);
155 rplay_audio_sample_rate = n;
156 }
157
158 /* Format */
159 rplay_audio_format = optional_format ? optional_format :
160 rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_ULINEAR_8;
161 }
162 else
163 {
164 report(REPORT_ERROR, "rplay_audio_init: `%s' unknown audio device\n",
165 rplay_audio_device);
166 return -1;
167 }
168
169 return 0;
170 }
171
172 /*
173 * Open the audio device.
174 *
175 * Return 0 on success and -1 on error.
176 */
177 int
178 rplay_audio_open()
179 {
180 int flags;
181 audio_buf_info info;
182 int n, i;
183
184 rplay_audio_fd = open(rplay_audio_device, O_WRONLY, 0);
185 if (rplay_audio_fd < 0)
186 {
187 return -1;
188 }
189
190 if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0)
191 {
192 report(REPORT_ERROR,
193 "rplay_audio_open: close-on-exec %d\n",
194 sys_err_str(errno));
195 /* return -1; */
196 }
197
198 /*
199 From the OSS driver docs:
200
201 Argument of this call is an integer encoded as 0xMMMMSSSS (in hex).
202
203 The 16 least significant bits determine the fragment size. The size is
204 2SSSS. For example SSSS=0008 gives fragment size of 256 bytes (28).
205 The minimum is 16 bytes (SSSS=4) and the maximum is
206 total_buffer_size/2. Some devices or processor architectures may
207 require larger fragments in this case the requested fragment size is
208 automatically increased.
209
210 The 16 most significant bits (MMMM) determine maximum number of
211 fragments. By default the deriver computes this based on available
212 buffer space. The minimum value is 2 and the maximum depends on the
213 situation. Set MMMM=0x7fff if you don't want to limit the number of
214 fragments.
215 */
216
217 /* Set the audio buffer fragment size. Default to zero which
218 lets the driver pick. */
219 rplay_audio_fragsize = optional_fragsize ? optional_fragsize : 0;
220 if (rplay_audio_fragsize)
221 {
222 n = rplay_audio_fragsize;
223 }
224 else
225 {
226 n = 4096;
227 }
228
229 for (i = 0; n > 1; i++)
230 {
231 n >>= 1;
232 }
233
234 rplay_audio_setfragsize((0x7fff << 16) | i);
235
236 ioctl(rplay_audio_fd, SNDCTL_DSP_GETOSPACE, &info);
237 report(REPORT_DEBUG, "OSS info: fragments=%d totalfrags=%d fragsize=%d DSP=%d\n",
238 info.fragments, info.fragstotal, info.fragsize, info.fragsize * info.fragstotal);
239
240 rplay_audio_fragsize = info.fragsize;
241
242 if (rplay_audio_init() < 0)
243 {
244 return -1;
245 }
246
247 return 0;
248 }
249
250 /*
251 * Is the audio device open?
252 *
253 * Return 1 for true and 0 for false.
254 */
255 int
256 rplay_audio_isopen()
257 {
258 return rplay_audio_fd != -1;
259 }
260
261 /*
262 * Flush the audio device.
263 *
264 * Return 0 on success and -1 on error.
265 */
266 int
267 rplay_audio_flush()
268 {
269 if (rplay_audio_fd != -1)
270 {
271 ioctl(rplay_audio_fd, SNDCTL_DSP_SYNC, 0);
272 }
273
274 return 0;
275 }
276
277 /*
278 * Write nbytes from buf to the audio device.
279 *
280 * Return the number of bytes written on success and -1 on error.
281 */
282 #ifdef __STDC__
283 int
284 rplay_audio_write(char *buf, int nbytes)
285 #else
286 int
287 rplay_audio_write(buf, nbytes)
288 char *buf;
289 int nbytes;
290 #endif
291 {
292 return write(rplay_audio_fd, buf, nbytes);
293 }
294
295 /*
296 * Close the audio device.
297 *
298 * Return 0 on success and -1 on error.
299 */
300 int
301 rplay_audio_close()
302 {
303 if (rplay_audio_fd != -1)
304 {
305 close(rplay_audio_fd);
306 }
307
308 rplay_audio_fd = -1;
309
310 return 0;
311 }
312
313 int
314 rplay_audio_set_port()
315 {
316 int mask = 0;
317 int vol = 0;
318
319 if (rplay_audio_mixer_fd == -1)
320 {
321 if (rplay_audio_mixer_open() < 0)
322 {
323 return -1;
324 }
325 }
326
327 if (ioctl(rplay_audio_mixer_fd, SOUND_MIXER_READ_DEVMASK, &mask) < 0)
328 {
329 return -1;
330 }
331
332 rplay_audio_port = optional_port ? optional_port :
333 RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER | RPLAY_AUDIO_PORT_HEADPHONE;
334
335 /* Clear any ports that aren't available. */
336 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_HEADPHONE))
337 {
338 if (!(mask & SOUND_MASK_VOLUME))
339 {
340 CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_HEADPHONE);
341 }
342 }
343 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER))
344 {
345 if (!(mask & SOUND_MASK_SPEAKER))
346 {
347 CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER);
348 }
349 }
350
351 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_HEADPHONE))
352 {
353 rplay_audio_port |= RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_HEADPHONE;
354
355 /* Make sure the volume isn't zero. */
356 vol = 0;
357 ioctl(rplay_audio_mixer_fd, SOUND_MIXER_READ_VOLUME, &vol);
358 if (vol == 0)
359 {
360 vol = 50;
361 vol |= ((vol & 0x00ff) << 8);
362 ioctl(rplay_audio_mixer_fd, SOUND_MIXER_WRITE_VOLUME, &vol);
363 }
364 }
365 else
366 {
367 /* Zero the volume. */
368 vol = 0;
369 ioctl(rplay_audio_mixer_fd, SOUND_MIXER_WRITE_VOLUME, &vol);
370 }
371
372 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER))
373 {
374 vol = 0;
375 ioctl(rplay_audio_mixer_fd, SOUND_MIXER_READ_SPEAKER, &vol);
376 /* Make sure the volume isn't zero. */
377 if (vol == 0)
378 {
379 vol = 50;
380 vol |= ((vol & 0x00ff) << 8);
381 ioctl(rplay_audio_mixer_fd, SOUND_MIXER_WRITE_SPEAKER, &vol);
382 }
383 }
384 else
385 {
386 /* Zero the volume. */
387 vol = 0;
388 ioctl(rplay_audio_mixer_fd, SOUND_MIXER_WRITE_SPEAKER, &vol);
389 }
390 }
391
392 /*
393 * Return the volume of the audio device.
394 *
395 * Return 0-255 or -1 on error.
396 */
397 int
398 rplay_audio_get_volume()
399 {
400 #ifdef FAKE_VOLUME
401 return rplay_audio_volume;
402 #else /* not FAKE_VOLUME */
403 int mask;
404 int left_vol;
405 int right_vol;
406 int vol;
407
408 vol = left_vol = right_vol = 0;
409
410 if (rplay_audio_mixer_fd == -1)
411 {
412 if (rplay_audio_mixer_open() < 0)
413 {
414 return -1;
415 }
416 }
417
418 if (ioctl(rplay_audio_mixer_fd, SOUND_MIXER_READ_DEVMASK, &mask) < 0)
419 {
420 report(REPORT_ERROR, "rplay_audio_get_volume: unable to get mixer device mask\n");
421 return -1;
422 }
423
424 if (!(mask & SOUND_MASK_PCM))
425 {
426 report(REPORT_ERROR, "rplay_audio_get_volume: pcm mixer device not installed\n");
427 return -1;
428 }
429
430 if (ioctl(rplay_audio_mixer_fd, SOUND_MIXER_READ_PCM, &vol) < 0)
431 {
432 report(REPORT_ERROR, "rplay_audio_get_volume: unable to get mixer volume\n");
433 return -1;
434 }
435 else
436 {
437 left_vol = (int) ((vol & 0x00ff) * 2.55);
438 right_vol = (int) (((vol & 0xff00) >> 8) * 2.55);
439 vol = (int) ((left_vol + right_vol) / 2);
440 }
441
442 return vol;
443 #endif /* not FAKE_VOLUME */
444 }
445
446 /*
447 * Set the volume of the audio device.
448 * Input should be 0-255.
449 *
450 * Return the volume of the audio device 0-255 or -1.
451 */
452 #ifdef __STDC__
453 int
454 rplay_audio_set_volume(int volume)
455 #else
456 int
457 rplay_audio_set_volume(volume)
458 int volume;
459 #endif
460 {
461 #ifdef FAKE_VOLUME
462 if (volume < RPLAY_MIN_VOLUME)
463 {
464 volume = RPLAY_MIN_VOLUME;
465 }
466 else if (volume > RPLAY_MAX_VOLUME)
467 {
468 volume = RPLAY_MAX_VOLUME;
469 }
470 rplay_audio_volume = volume;
471
472 return rplay_audio_volume;
473 #else /* not FAKE_VOLUME */
474 int mask;
475 int left_vol;
476 int right_vol;
477 int vol = (int) (volume / 2.55);
478
479 vol |= ((vol & 0x00ff) << 8);
480
481 if (rplay_audio_mixer_fd == -1)
482 {
483 if (rplay_audio_mixer_open() < 0)
484 {
485 return -1;
486 }
487 }
488
489 if (ioctl(rplay_audio_mixer_fd, SOUND_MIXER_READ_DEVMASK, &mask) < 0)
490 {
491 report(REPORT_ERROR, "rplay_audio_set_volume: unable to get mixer device mask\n");
492 return -1;
493 }
494
495 if (!(mask & SOUND_MASK_PCM))
496 {
497 report(REPORT_ERROR, "rplay_audio_set_volume: pcm mixer device not installed\n");
498 return -1;
499 }
500
501 if (ioctl(rplay_audio_mixer_fd, SOUND_MIXER_WRITE_PCM, &vol) < 0)
502 {
503 report(REPORT_ERROR, "rplay_audio_set_volume: unable to set mixer volume\n");
504 return -1;
505 }
506 else
507 {
508 left_vol = (int) ((vol & 0x00ff) * 2.55);
509 right_vol = (int) (((vol & 0xff00) >> 8) * 2.55);
510 vol = (int) ((left_vol + right_vol) / 2);
511 }
512
513 rplay_audio_volume = vol;
514
515 return rplay_audio_volume;
516 #endif /* not FAKE_VOLUME */
517 }
518
519 int
520 rplay_audio_getblksize()
521 {
522 int blksize;
523
524 if (rplay_audio_fd == -1)
525 {
526 return -1;
527 }
528
529 if (ioctl(rplay_audio_fd, SNDCTL_DSP_GETBLKSIZE, &blksize) < 0)
530 {
531 report(REPORT_NOTICE, "DSP_GETBLKSIZE failed\n");
532 return -1;
533 }
534 else
535 {
536 return blksize;
537 }
538 }
539
540 #ifdef __STDC__
541 int
542 rplay_audio_setfragsize(int frag)
543 #else
544 int
545 rplay_audio_setfragsize(frag)
546 int frag;
547 #endif
548 {
549 int n;
550
551 if (rplay_audio_fd == -1)
552 {
553 return -1;
554 }
555
556 n = ioctl(rplay_audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag);
557 if (n < 0)
558 {
559 report(REPORT_NOTICE, "DSP_SETFRAGMENT failed n = %d\n", n);
560 return -1;
561 }
562
563 return 0;
564 }
565
566 int
567 rplay_audio_getospace_bytes()
568 {
569 audio_buf_info info;
570
571 if (rplay_audio_fd < 0)
572 {
573 return 0;
574 }
575 ioctl(rplay_audio_fd, SNDCTL_DSP_GETOSPACE, &info);
576 /* printf ("frags=%d fragtotal=%d fragsize=%d bytes=%d\n", */
577 /* info.fragments, info.fragstotal, info.fragsize, info.bytes); */
578 return info.bytes;
579 }
580
581 int
582 rplay_audio_getospace_fragsize()
583 {
584 audio_buf_info info;
585
586 if (rplay_audio_fd < 0)
587 {
588 return 0;
589 }
590 ioctl(rplay_audio_fd, SNDCTL_DSP_GETOSPACE, &info);
591 return info.fragsize;
592 }
593
594 int
595 rplay_audio_getfd()
596 {
597 return rplay_audio_fd;
598 }
599
600 int
601 rplay_audio_mixer_open()
602 {
603 rplay_audio_mixer_fd = open(RPLAY_AUDIO_MIXER_DEVICE, O_RDONLY);
604 if (rplay_audio_mixer_fd < 0)
605 {
606 report(REPORT_ERROR, "rplay_audio_mixer_open: unable to open mixer device\n");
607 return -1;
608 }
609
610 return 0;
611 }
612
613 int
614 rplay_audio_mixer_close()
615 {
616 if (rplay_audio_mixer_fd != -1)
617 {
618 close(rplay_audio_mixer_fd);
619 rplay_audio_mixer_fd = -1;
620 }
621 }
622
623 int
624 rplay_audio_mixer_isopen()
625 {
626 return rplay_audio_mixer_fd != -1;
627 }