Debian Codegraph @

rplayd/audio/audio_sun.c

raw symbols SCIP
1 /* $Id: audio_sun.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 <sun/audioio.h>
37 #include <errno.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 optional_sample_rate;
49 extern int optional_precision;
50 extern int optional_channels;
51 extern int optional_format;
52 extern int optional_port;
53
54 /*
55 * Internal variables:
56 */
57 static int rplay_audio_fd = -1;
58
59 static RPLAY_AUDIO_TABLE amd_table[] =
60 {
61 {8000, RPLAY_FORMAT_ULAW, 8, 1},
62 {0, 0, 0, 0}
63 };
64
65 static RPLAY_AUDIO_TABLE dbri_table[] =
66 {
67 {6615, RPLAY_FORMAT_LINEAR_16, 16, 1},
68 {6615, RPLAY_FORMAT_LINEAR_16, 16, 2},
69 {8000, RPLAY_FORMAT_ULAW, 8, 1},
70 {8000, RPLAY_FORMAT_LINEAR_16, 16, 1},
71 {8000, RPLAY_FORMAT_LINEAR_16, 16, 2},
72 {9600, RPLAY_FORMAT_LINEAR_16, 16, 1},
73 {9600, RPLAY_FORMAT_LINEAR_16, 16, 2},
74 {11025, RPLAY_FORMAT_LINEAR_16, 16, 1},
75 {11025, RPLAY_FORMAT_LINEAR_16, 16, 2},
76 {16000, RPLAY_FORMAT_LINEAR_16, 16, 1},
77 {16000, RPLAY_FORMAT_LINEAR_16, 16, 2},
78 {18900, RPLAY_FORMAT_LINEAR_16, 16, 1},
79 {18900, RPLAY_FORMAT_LINEAR_16, 16, 2},
80 {22050, RPLAY_FORMAT_LINEAR_16, 16, 1},
81 {22050, RPLAY_FORMAT_LINEAR_16, 16, 2},
82 {32000, RPLAY_FORMAT_LINEAR_16, 16, 1},
83 {32000, RPLAY_FORMAT_LINEAR_16, 16, 2},
84 {33075, RPLAY_FORMAT_LINEAR_16, 16, 1},
85 {33075, RPLAY_FORMAT_LINEAR_16, 16, 2},
86 {37800, RPLAY_FORMAT_LINEAR_16, 16, 1},
87 {37800, RPLAY_FORMAT_LINEAR_16, 16, 2},
88 {44100, RPLAY_FORMAT_LINEAR_16, 16, 1},
89 {44100, RPLAY_FORMAT_LINEAR_16, 16, 2},
90 {48000, RPLAY_FORMAT_LINEAR_16, 16, 1},
91 {48000, RPLAY_FORMAT_LINEAR_16, 16, 2},
92 {0, 0, 0, 0}
93 };
94
95 /*
96 * Initialize the audio device.
97 * This routine must set the following external variables:
98 * rplay_audio_sample_rate
99 * rplay_audio_precision
100 * rplay_audio_channels
101 * rplay_audio_format
102 * rplay_audio_port
103 *
104 * and may use the following optional parameters:
105 * optional_sample_rate
106 * optional_precision
107 * optional_channels
108 * optional_format
109 * optional_port
110 *
111 * optional_* variables with values of zero should be ignored.
112 *
113 * Return 0 on success and -1 on error.
114 */
115 int
116 rplay_audio_init()
117 {
118 int d;
119 audio_info_t a;
120
121 if (rplay_audio_fd == -1)
122 {
123 rplay_audio_open();
124 if (rplay_audio_fd == -1)
125 {
126 report(REPORT_ERROR, "rplay_audio_init: cannot open %s\n",
127 rplay_audio_device);
128 return -1;
129 }
130 }
131
132 /*
133 * Not all versions of SunOS have dbri support.
134 */
135 #ifdef AUDIO_GETDEV
136 #ifdef AUDIO_DEV_SPEAKERBOX
137 if (ioctl(rplay_audio_fd, AUDIO_GETDEV, &d) < 0)
138 {
139 report(REPORT_ERROR, "rplay_audio_init: AUDIO_GETDEV: %s\n", sys_err_str(errno));
140 return -1;
141 }
142
143 if (d == AUDIO_DEV_SPEAKERBOX || d == AUDIO_DEV_CODEC)
144 {
145 report(REPORT_DEBUG, "dbri device detected\n");
146 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025;
147 rplay_audio_precision = optional_precision ? optional_precision : 16;
148 rplay_audio_channels = optional_channels ? optional_channels : 1;
149 rplay_audio_format = optional_format ? optional_format :
150 rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
151 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
152 rplay_audio_table = dbri_table;
153 }
154 else if (d == AUDIO_DEV_AMD)
155 {
156 report(REPORT_DEBUG, "amd device detected\n");
157 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 8000;
158 rplay_audio_precision = optional_precision ? optional_precision : 8;
159 rplay_audio_channels = optional_channels ? optional_channels : 1;
160 rplay_audio_format = optional_format ? optional_format : RPLAY_FORMAT_ULAW;
161 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_SPEAKER;
162 rplay_audio_table = amd_table;
163 }
164 /* The SS5 AUDIO_GETDEV ioctl returns 5. This value is not in <sun/audioio.h>. */
165 #ifndef AUDIO_DEV_CS4231
166 #define AUDIO_DEV_CS4231 5
167 #endif
168 else if (d == AUDIO_DEV_CS4231)
169 {
170 report(REPORT_DEBUG, "cs4231 device detected\n");
171 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025;
172 rplay_audio_precision = optional_precision ? optional_precision : 16;
173 rplay_audio_channels = optional_channels ? optional_channels : 1;
174 rplay_audio_format = optional_format ? optional_format :
175 rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
176 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
177 rplay_audio_table = dbri_table;
178 }
179 else
180 #endif /* not AUDIO_DEV_SPEAKERBOX */
181 #endif /* not AUDIO_GETDEV */
182 {
183 report(REPORT_DEBUG, "assuming amd device\n");
184 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 8000;
185 rplay_audio_precision = optional_precision ? optional_precision : 8;
186 rplay_audio_channels = optional_channels ? optional_channels : 1;
187 rplay_audio_format = optional_format ? optional_format : RPLAY_FORMAT_ULAW;
188 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_SPEAKER;
189 rplay_audio_table = amd_table;
190 }
191
192 /* Verify the precision and format. */
193 switch (rplay_audio_precision)
194 {
195 case 8:
196 if (rplay_audio_format != RPLAY_FORMAT_ULAW
197 && rplay_audio_format != RPLAY_FORMAT_LINEAR_8)
198 {
199 report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n",
200 rplay_audio_precision, rplay_audio_format);
201 return -1;
202 }
203 break;
204
205 case 16:
206 if (rplay_audio_format != RPLAY_FORMAT_LINEAR_16)
207 {
208 report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n",
209 rplay_audio_precision, rplay_audio_format);
210 return -1;
211 }
212 break;
213
214 default:
215 report(REPORT_ERROR, "rplay_audio_init: `%d' unsupported audio precision\n",
216 rplay_audio_precision);
217 return -1;
218 }
219
220 AUDIO_INITINFO(&a);
221
222 switch (rplay_audio_format)
223 {
224 case RPLAY_FORMAT_ULAW:
225 a.play.encoding = AUDIO_ENCODING_ULAW;
226 break;
227
228 case RPLAY_FORMAT_LINEAR_8:
229 case RPLAY_FORMAT_LINEAR_16:
230 a.play.encoding = AUDIO_ENCODING_LINEAR;
231 break;
232
233 default:
234 report(REPORT_ERROR, "rplay_audio_init: unsupported audio format `%d'\n",
235 rplay_audio_format);
236 return -1;
237 }
238
239 /* Audio port. */
240 if (rplay_audio_port == RPLAY_AUDIO_PORT_NONE)
241 {
242 a.play.port = ~0; /* see AUDIO_INITINFO in /usr/include/sun/audioio.h. */
243 }
244 else
245 {
246 a.play.port = 0;
247 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT))
248 {
249 #ifdef AUDIO_LINE_OUT
250 SET_BIT(a.play.port, AUDIO_LINE_OUT);
251 #else
252 CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT);
253 #endif
254 }
255 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE))
256 {
257 #ifdef AUDIO_HEADPHONE
258 SET_BIT(a.play.port, AUDIO_HEADPHONE);
259 #else
260 CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE);
261 #endif
262 }
263 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER))
264 {
265 #ifdef AUDIO_SPEAKER
266 SET_BIT(a.play.port, AUDIO_SPEAKER);
267 #endif
268 /* Assume speaker is okay. */
269 }
270 }
271
272 a.play.sample_rate = rplay_audio_sample_rate;
273 a.play.precision = rplay_audio_precision;
274 a.play.channels = rplay_audio_channels;
275
276 if (ioctl(rplay_audio_fd, AUDIO_SETINFO, &a) < 0)
277 {
278 report(REPORT_ERROR, "rplay_audio_init: AUDIO_SETINFO: %s\n", sys_err_str(errno));
279 return -1;
280 }
281
282 return 0;
283 }
284
285 /*
286 * Open the audio device.
287 *
288 * Return 0 on success and -1 on error.
289 */
290 int
291 rplay_audio_open()
292 {
293 int flags;
294
295 rplay_audio_fd = open(rplay_audio_device, O_WRONLY | O_NDELAY, 0);
296 if (rplay_audio_fd < 0)
297 {
298 return -1;
299 }
300
301 if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0)
302 {
303 report(REPORT_ERROR,
304 "rplay_audio_open: close-on-exec %d\n",
305 sys_err_str(errno));
306 /* return -1; */
307 }
308
309 if (rplay_audio_init() < 0)
310 {
311 return -1;
312 }
313
314 /*
315 * Make sure the audio device writes are non-blocking.
316 */
317 flags = fcntl(rplay_audio_fd, F_GETFL, 0);
318 if (flags < 0)
319 {
320 return -1;
321 }
322 flags |= FNDELAY;
323 if (fcntl(rplay_audio_fd, F_SETFL, flags) < 0)
324 {
325 return -1;
326 }
327
328 return 0;
329 }
330
331 /*
332 * Is the audio device open?
333 *
334 * Return 1 for true and 0 for false.
335 */
336 int
337 rplay_audio_isopen()
338 {
339 return rplay_audio_fd != -1;
340 }
341
342 /*
343 * Flush the audio device.
344 *
345 * Return 0 on success and -1 on error.
346 */
347 int
348 rplay_audio_flush()
349 {
350 if (rplay_audio_fd != -1)
351 {
352 ioctl(rplay_audio_fd, AUDIO_DRAIN, 0);
353 }
354
355 return 0;
356 }
357
358 /*
359 * Write nbytes from buf to the audio device.
360 *
361 * Return the number of bytes written on success and -1 on error.
362 */
363 #ifdef __STDC__
364 int
365 rplay_audio_write(char *buf, int nbytes)
366 #else
367 int
368 rplay_audio_write(buf, nbytes)
369 char *buf;
370 int nbytes;
371 #endif
372 {
373 int n, nleft, nwritten;
374 char *p;
375
376 nleft = nbytes;
377 nwritten = 0;
378
379 for (p = buf; nleft > 0; nleft -= n, p += n)
380 {
381 n = write(rplay_audio_fd, p, nleft);
382 if (n < 0)
383 {
384 if (errno == EWOULDBLOCK)
385 {
386 return nwritten;
387 }
388 else if (errno != EINTR)
389 {
390 return -1;
391 }
392 n = 0;
393 }
394 else
395 {
396 nwritten += n;
397 }
398 }
399
400 return nwritten;
401 }
402
403 /*
404 * Close the audio device.
405 *
406 * Return 0 on success and -1 on error.
407 */
408 int
409 rplay_audio_close()
410 {
411 if (rplay_audio_fd != -1)
412 {
413 close(rplay_audio_fd);
414 }
415
416 rplay_audio_fd = -1;
417
418 return 0;
419 }
420
421 /*
422 * Return the volume of the audio device.
423 *
424 * Return 0-255 or -1 on error.
425 */
426 int
427 rplay_audio_get_volume()
428 {
429 #ifdef FAKE_VOLUME
430 return rplay_audio_volume;
431 #else /* not FAKE_VOLUME */
432 audio_info_t a;
433
434 if (rplay_audio_fd < 0)
435 {
436 rplay_audio_open();
437 }
438 if (rplay_audio_fd < 0)
439 {
440 return -1;
441 }
442 if (ioctl(rplay_audio_fd, AUDIO_GETINFO, &a) < 0)
443 {
444 return -1;
445 }
446 else
447 {
448 return a.play.gain;
449 }
450 #endif /* not FAKE_VOLUME */
451 }
452
453 /*
454 * Set the volume of the audio device.
455 * Input should be 0-255.
456 *
457 * Return the volume of the audio device 0-255 or -1.
458 */
459 #ifdef __STDC__
460 int
461 rplay_audio_set_volume(int volume)
462 #else
463 int
464 rplay_audio_set_volume(volume)
465 int volume;
466 #endif
467 {
468 #ifdef FAKE_VOLUME
469 if (volume < RPLAY_MIN_VOLUME)
470 {
471 volume = RPLAY_MIN_VOLUME;
472 }
473 else if (volume > RPLAY_MAX_VOLUME)
474 {
475 volume = RPLAY_MAX_VOLUME;
476 }
477 rplay_audio_volume = volume;
478
479 return rplay_audio_volume;
480 #else /* not FAKE_VOLUME */
481 audio_info_t a;
482
483 rplay_audio_volume = 0;
484
485 if (rplay_audio_fd < 0)
486 {
487 rplay_audio_open();
488 }
489 if (rplay_audio_fd < 0)
490 {
491 return -1;
492 }
493
494 AUDIO_INITINFO(&a);
495 a.play.gain = volume;
496 if (ioctl(rplay_audio_fd, AUDIO_SETINFO, &a) < 0)
497 {
498 return -1;
499 }
500
501 rplay_audio_volume = rplay_audio_get_volume();
502
503 return rplay_audio_volume;
504 #endif /* not FAKE_VOLUME */
505 }