Debian Codegraph @

rplayd/audio/audio_solaris.c

raw symbols SCIP
1 /* $Id: audio_solaris.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 <sys/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 audio_info_t a;
119 audio_device_t d;
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 if (ioctl(rplay_audio_fd, AUDIO_GETDEV, &d) < 0)
133 {
134 report(REPORT_ERROR, "rplay_audio_init: AUDIO_GETDEV: %s\n",
135 sys_err_str(errno));
136 return -1;
137 }
138
139 if (strcmp(d.name, "SUNW,dbri") == 0)
140 {
141 report(REPORT_DEBUG, "%s device detected\n", d.name);
142 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025;
143 rplay_audio_precision = optional_precision ? optional_precision : 16;
144 rplay_audio_channels = optional_channels ? optional_channels : 1;
145 rplay_audio_format = optional_format ? optional_format :
146 rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
147 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
148 rplay_audio_table = dbri_table;
149 }
150 else if (strcmp(d.name, "SUNW,CS4231") == 0)
151 {
152 report(REPORT_DEBUG, "%s device detected\n", d.name);
153 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025;
154 rplay_audio_precision = optional_precision ? optional_precision : 16;
155 rplay_audio_channels = optional_channels ? optional_channels : 1;
156 rplay_audio_format = optional_format ? optional_format :
157 rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
158 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
159 rplay_audio_table = dbri_table; /* use the dbri table */
160 }
161 else if (strcmp(d.name, "SUNW,am79c30") == 0)
162 {
163 report(REPORT_DEBUG, "%s device detected\n", d.name);
164 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 8000;
165 rplay_audio_precision = optional_precision ? optional_precision : 8;
166 rplay_audio_channels = optional_channels ? optional_channels : 1;
167 rplay_audio_format = optional_format ? optional_format : RPLAY_FORMAT_ULAW;
168 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_SPEAKER;
169 rplay_audio_table = amd_table;
170 }
171 else if (strcmp(d.name, "SUNW,sb16") == 0)
172 {
173 report(REPORT_DEBUG, "%s device detected\n", d.name);
174 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 44100;
175 rplay_audio_precision = optional_precision ? optional_precision : 16;
176 rplay_audio_channels = optional_channels ? optional_channels : 2;
177 rplay_audio_format = optional_format ? optional_format :
178 rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
179 rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
180 rplay_audio_table = dbri_table; /* use the dbri table */
181 }
182 else
183 {
184 report(REPORT_ERROR, "`%s' unknown audio device detected\n", d.name);
185 return -1;
186 }
187
188 /* Verify the precision and format. */
189 switch (rplay_audio_precision)
190 {
191 case 8:
192 if (rplay_audio_format != RPLAY_FORMAT_ULAW
193 && rplay_audio_format != RPLAY_FORMAT_LINEAR_8)
194 {
195 report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n",
196 rplay_audio_precision, rplay_audio_format);
197 return -1;
198 }
199 break;
200
201 case 16:
202 if (rplay_audio_format != RPLAY_FORMAT_LINEAR_16)
203 {
204 report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n",
205 rplay_audio_precision, rplay_audio_format);
206 return -1;
207 }
208 break;
209
210 default:
211 report(REPORT_ERROR, "rplay_audio_init: `%d' unsupported audio precision\n",
212 rplay_audio_precision);
213 return -1;
214 }
215
216 AUDIO_INITINFO(&a);
217
218 switch (rplay_audio_format)
219 {
220 case RPLAY_FORMAT_ULAW:
221 a.play.encoding = AUDIO_ENCODING_ULAW;
222 break;
223
224 case RPLAY_FORMAT_LINEAR_8:
225 case RPLAY_FORMAT_LINEAR_16:
226 a.play.encoding = AUDIO_ENCODING_LINEAR;
227 break;
228
229 default:
230 report(REPORT_ERROR, "rplay_audio_init: unsupported audio format `%d'\n",
231 rplay_audio_format);
232 return -1;
233 }
234
235 /* Audio port. */
236 if (rplay_audio_port == RPLAY_AUDIO_PORT_NONE)
237 {
238 a.play.port = ~0; /* see AUDIO_INITINFO in /usr/include/sys/audioio.h. */
239 }
240 else
241 {
242 a.play.port = 0;
243 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT))
244 {
245 #ifdef AUDIO_LINE_OUT
246 SET_BIT(a.play.port, AUDIO_LINE_OUT);
247 #else
248 CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT);
249 #endif
250 }
251 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE))
252 {
253 #ifdef AUDIO_HEADPHONE
254 SET_BIT(a.play.port, AUDIO_HEADPHONE);
255 #else
256 CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE);
257 #endif
258 }
259 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER))
260 {
261 #ifdef AUDIO_SPEAKER
262 SET_BIT(a.play.port, AUDIO_SPEAKER);
263 #endif
264 /* Assume speaker is okay. */
265 }
266 }
267
268 a.play.sample_rate = rplay_audio_sample_rate;
269 a.play.precision = rplay_audio_precision;
270 a.play.channels = rplay_audio_channels;
271
272 if (ioctl(rplay_audio_fd, AUDIO_SETINFO, &a) < 0)
273 {
274 report(REPORT_ERROR, "rplay_audio_init: AUDIO_SETINFO: %s\n", sys_err_str(errno));
275 return -1;
276 }
277
278 return 0;
279 }
280
281 /*
282 * Open the audio device.
283 *
284 * Return 0 on success and -1 on error.
285 */
286 int
287 rplay_audio_open()
288 {
289 int flags;
290
291 rplay_audio_fd = open(rplay_audio_device, O_WRONLY | O_NDELAY, 0);
292 if (rplay_audio_fd < 0)
293 {
294 return -1;
295 }
296
297 if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0)
298 {
299 report(REPORT_ERROR,
300 "rplay_audio_open: close-on-exec %d\n",
301 sys_err_str(errno));
302 /* return -1; */
303 }
304
305 if (rplay_audio_init() < 0)
306 {
307 return -1;
308 }
309
310 /*
311 * Make sure the audio device writes are non-blocking.
312 */
313 flags = fcntl(rplay_audio_fd, F_GETFL, 0);
314 if (flags < 0)
315 {
316 return -1;
317 }
318 flags |= FNDELAY;
319 if (fcntl(rplay_audio_fd, F_SETFL, flags) < 0)
320 {
321 return -1;
322 }
323
324 return 0;
325 }
326
327 /*
328 * Is the audio device open?
329 *
330 * Return 1 for true and 0 for false.
331 */
332 int
333 rplay_audio_isopen()
334 {
335 return rplay_audio_fd != -1;
336 }
337
338 /*
339 * Flush the audio device.
340 *
341 * Return 0 on success and -1 on error.
342 */
343 int
344 rplay_audio_flush()
345 {
346 if (rplay_audio_fd != -1)
347 {
348 ioctl(rplay_audio_fd, AUDIO_DRAIN, 0);
349 }
350
351 return 0;
352 }
353
354 /*
355 * Write nbytes from buf to the audio device.
356 *
357 * Return the number of bytes written on success and -1 on error.
358 */
359 #ifdef __STDC__
360 int
361 rplay_audio_write(char *buf, int nbytes)
362 #else
363 int
364 rplay_audio_write(buf, nbytes)
365 char *buf;
366 int nbytes;
367 #endif
368 {
369 int n, nleft, nwritten;
370 char *p;
371
372 nleft = nbytes;
373 nwritten = 0;
374
375 for (p = buf; nleft > 0; nleft -= n, p += n)
376 {
377 n = write(rplay_audio_fd, p, nleft);
378 if (n < 0)
379 {
380 if (errno == EWOULDBLOCK)
381 {
382 return nwritten;
383 }
384 else if (errno != EINTR)
385 {
386 return -1;
387 }
388 n = 0;
389 }
390 else
391 {
392 nwritten += n;
393 }
394 }
395
396 return nwritten;
397 }
398
399 /*
400 * Close the audio device.
401 *
402 * Return 0 on success and -1 on error.
403 */
404 int
405 rplay_audio_close()
406 {
407 if (rplay_audio_fd != -1)
408 {
409 close(rplay_audio_fd);
410 }
411
412 rplay_audio_fd = -1;
413
414 return 0;
415 }
416
417 /*
418 * Return the volume of the audio device.
419 *
420 * Return 0-255 or -1 on error.
421 */
422 int
423 rplay_audio_get_volume()
424 {
425 #ifdef FAKE_VOLUME
426 return rplay_audio_volume;
427 #else /* not FAKE_VOLUME */
428 audio_info_t a;
429
430 if (rplay_audio_fd < 0)
431 {
432 rplay_audio_open();
433 }
434 if (rplay_audio_fd < 0)
435 {
436 return -1;
437 }
438 if (ioctl(rplay_audio_fd, AUDIO_GETINFO, &a) < 0)
439 {
440 return -1;
441 }
442 else
443 {
444 return a.play.gain;
445 }
446 #endif /* not FAKE_VOLUME */
447 }
448
449 /*
450 * Set the volume of the audio device.
451 * Input should be 0-255.
452 *
453 * Return the volume of the audio device 0-255 or -1.
454 */
455 #ifdef __STDC__
456 int
457 rplay_audio_set_volume(int volume)
458 #else
459 int
460 rplay_audio_set_volume(volume)
461 int volume;
462 #endif
463 {
464 #ifdef FAKE_VOLUME
465 if (volume < RPLAY_MIN_VOLUME)
466 {
467 volume = RPLAY_MIN_VOLUME;
468 }
469 else if (volume > RPLAY_MAX_VOLUME)
470 {
471 volume = RPLAY_MAX_VOLUME;
472 }
473 rplay_audio_volume = volume;
474
475 return rplay_audio_volume;
476 #else /* not FAKE_VOLUME */
477 audio_info_t a;
478
479 rplay_audio_volume = 0;
480
481 if (rplay_audio_fd < 0)
482 {
483 rplay_audio_open();
484 }
485 if (rplay_audio_fd < 0)
486 {
487 return -1;
488 }
489
490 AUDIO_INITINFO(&a);
491 a.play.gain = volume;
492 if (ioctl(rplay_audio_fd, AUDIO_SETINFO, &a) < 0)
493 {
494 return -1;
495 }
496
497 rplay_audio_volume = rplay_audio_get_volume();
498
499 return rplay_audio_volume;
500 #endif /* not FAKE_VOLUME */
501 }