Debian Codegraph @

rplayd/audio/audio_oss.c

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