Debian Codegraph @

rplayd/audio/audio_hpux.c

raw symbols SCIP
1 /*
2 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
3 *
4 * This file is part of rplay.
5 *
6 * rplay is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * rplay is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with rplay; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22
23
24 /*
25 * The _hpux version was cobbled together from the _generic version by Hendrik
26 * ([email protected]).
27 */
28
29 #include "rplayd.h"
30
31 /*
32 * System audio include files:
33 */
34 #include <sys/types.h>
35 #include <sys/file.h>
36 #include <sys/ioctl.h>
37 #include <sys/fcntl.h>
38 #include <sys/errno.h>
39 #include <sys/audio.h>
40 #include <errno.h>
41
42 /*
43 * External variables:
44 */
45 extern char *rplay_audio_device;
46 extern int rplay_audio_sample_rate;
47 extern int rplay_audio_channels;
48 extern int rplay_audio_precision;
49 extern int rplay_audio_format;
50 extern int rplay_audio_port;
51 extern int optional_sample_rate;
52 extern int optional_precision;
53 extern int optional_channels;
54 extern int optional_format;
55 extern int optional_port;
56
57 /*
58 * Internal variables:
59 */
60 static int rplay_audio_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 * rplay_audio_port
70 *
71 * and may use the following optional parameters:
72 * optional_sample_rate
73 * optional_precision
74 * optional_channels
75 * optional_format
76 * optional_port
77 *
78 * optional_* variables with values of zero should be ignored.
79 *
80 * Return 0 on success and -1 on error.
81 */
82 int
83 rplay_audio_init()
84 {
85 struct audio_describe ad;
86 struct audio_status as;
87 int af; /* audio_format */
88 int ar; /* audio_rate */
89 int ao; /* audio_output */
90 int ac; /* audio_channels */
91
92 /* Open the audio device */
93 if (rplay_audio_fd == -1)
94 {
95 rplay_audio_open();
96 if (rplay_audio_fd == -1)
97 {
98 report(REPORT_ERROR, "rplay_audio_init: cannot open %s\n",
99 rplay_audio_device);
100 return -1;
101 }
102 }
103
104 /* Reset the audio device */
105 if (ioctl(rplay_audio_fd, AUDIO_RESET, RESET_RX_BUF | RESET_TX_BUF | RESET_RX_OVF | RESET_TX_UNF) < 0)
106 {
107 report(REPORT_ERROR, "rplay_audio_init: AUDIO_RESET: %s\n", sys_err_str(errno));
108 return -1;
109 }
110
111 /* Interrogate the audio device */
112 if (ioctl(rplay_audio_fd, AUDIO_DESCRIBE, &ad) < 0)
113 {
114 report(REPORT_ERROR, "rplay_audio_init: AUDIO_DESCRIBE: %s\n", sys_err_str(errno));
115 return -1;
116 }
117
118 if (ioctl(rplay_audio_fd, AUDIO_GET_STATUS, &as) < 0)
119 {
120 report(REPORT_ERROR, "rplay_audio_init: AUDIO_GET_STATUS: %s\n", sys_err_str(errno));
121 return -1;
122 }
123 if (ioctl(rplay_audio_fd, AUDIO_GET_SAMPLE_RATE, &ar) < 0)
124 {
125 report(REPORT_ERROR, "rplay_audio_init: AUDIO_GET_SAMPLE_RATE: %s\n", sys_err_str(errno));
126 return -1;
127 }
128 if (ioctl(rplay_audio_fd, AUDIO_GET_DATA_FORMAT, &af) < 0)
129 {
130 report(REPORT_ERROR, "rplay_audio_init: AUDIO_GET_DATA_FORMAT: %s\n", sys_err_str(errno));
131 return -1;
132 }
133 if (ioctl(rplay_audio_fd, AUDIO_GET_OUTPUT, &ao) < 0)
134 {
135 report(REPORT_ERROR, "rplay_audio_init: AUDIO_GET_OUTPUT: %s\n", sys_err_str(errno));
136 return -1;
137 }
138
139 #ifdef DEBUG
140 printf("*** AUDIO_DESCRIBE DATA ***\n");
141 printf("audio_id:\t\t%d\tnrates:\t\t\t%d\tflags:\t%X\n",
142 ad.audio_id, ad.nrates, ad.flags);
143 printf("max_bits_per_sample:\t%d\tnchannels:\t\t%d\n",
144 ad.max_bits_per_sample, ad.nchannels);
145 printf("min_receive_gain:\t%d\tmin_transmit_gain:\t%d\tmin_monitor_gain:\t%d\n",
146 ad.min_receive_gain, ad.min_transmit_gain, ad.min_monitor_gain);
147 printf("max_receive_gain:\t%d\tmax_transmit_gain:\t%d\tmax_monitor_gain:\t%d\n",
148 ad.max_receive_gain, ad.max_transmit_gain, ad.max_monitor_gain);
149 printf("*** AUDIO_STATUS DATA ***\n");
150 printf("receive_status: %x\treceive_buffer_count: %d\treceive_overflow_count: %d\n",
151 as.receive_status, as.receive_buffer_count, as.receive_overflow_count);
152 printf("transmit_status: %x\ttransmit_buffer_count: %d\ttransmit_underflow_count: %d\n",
153 as.transmit_status, as.transmit_buffer_count, as.transmit_underflow_count);
154 printf("*** END DATA ***\n");
155 #endif
156
157 /* Set internal configuration */
158
159 /* Having tried sample rates from 0 upto 500000 in steps of 1, the device will
160 only accept a rate of 8000Hz. Dunno about any other models of HP though. */
161
162 rplay_audio_sample_rate = ar = optional_sample_rate ? optional_sample_rate : 8000;
163 rplay_audio_channels = ac = optional_channels ? optional_channels : ad.nchannels;
164
165 /* Set the channel mask according to the number of channels allowed */
166 ac = (ac == 2) ? (AUDIO_CHANNEL_LEFT | AUDIO_CHANNEL_RIGHT) :
167 ((ac == 1) ? (AUDIO_CHANNEL_LEFT) : 0);
168
169 /* Only Ulaw, Alaw and linear 16-bit formats are allowed - anyone know what Alaw is ? */
170 rplay_audio_format = optional_format ?
171 ((optional_format == RPLAY_FORMAT_ULAW) ? RPLAY_FORMAT_ULAW :
172 RPLAY_FORMAT_LINEAR_16) : RPLAY_FORMAT_LINEAR_16;
173 af = (rplay_audio_format == RPLAY_FORMAT_ULAW) ? AUDIO_FORMAT_ULAW :
174 AUDIO_FORMAT_LINEAR16BIT;
175
176 /* Precision cannot be set seperatly to sound format (I think) */
177 rplay_audio_precision = (af == AUDIO_FORMAT_LINEAR16BIT) ? 16 : 8;
178
179 /* Set the output device - how should this be specified ? */
180 rplay_audio_port = optional_port ? optional_port :
181 RPLAY_AUDIO_PORT_SPEAKER | RPLAY_AUDIO_PORT_HEADPHONE; /* default ports */
182 ao = 0;
183 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT))
184 {
185 #ifdef AUDIO_LINE_OUT
186 SET_BIT(ao, AUDIO_LINE_OUT);
187 #endif
188 #ifdef AUDIO_OUT_LINE
189 SET_BIT(ao, AUDIO_OUT_LINE);
190 #endif
191 }
192 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE))
193 {
194 SET_BIT(ao, AUDIO_OUT_HEADPHONE);
195 }
196 if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER))
197 {
198 SET_BIT(ao, AUDIO_OUT_SPEAKER);
199 }
200
201 /* Program the audio device */
202 if (ioctl(rplay_audio_fd, AUDIO_SET_SAMPLE_RATE, ar) < 0)
203 {
204 report(REPORT_ERROR, "rplay_audio_init: AUDIO_SET_SAMPLE_RATE: %s\n", sys_err_str(errno));
205 return -1;
206 }
207 if (ioctl(rplay_audio_fd, AUDIO_SET_CHANNELS, ac) < 0)
208 {
209 report(REPORT_ERROR, "rplay_audio_init: AUDIO_SET_CHANNELS: %s\n", sys_err_str(errno));
210 return -1;
211 }
212 if (ioctl(rplay_audio_fd, AUDIO_SET_DATA_FORMAT, af) < 0)
213 {
214 report(REPORT_ERROR, "rplay_audio_init: AUDIO_SET_DATA_FORMAT: %s\n", sys_err_str(errno));
215 return -1;
216 }
217 if (ioctl(rplay_audio_fd, AUDIO_SET_OUTPUT, ao) < 0)
218 {
219 report(REPORT_ERROR, "rplay_audio_init: AUDIO_SET_OUTPUTT: %s\n", sys_err_str(errno));
220 return -1;
221 }
222
223 return 0;
224 }
225
226 /*
227 * Open the audio device.
228 *
229 * Return 0 on success and -1 on error.
230 */
231 int
232 rplay_audio_open()
233 {
234 int flags;
235
236 report(REPORT_DEBUG, "opening device >%s<\n", rplay_audio_device);
237
238 rplay_audio_fd = open(rplay_audio_device, O_WRONLY | O_NDELAY, 0);
239 if (rplay_audio_fd < 0)
240 {
241 return -1;
242 }
243
244 if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0)
245 {
246 report(REPORT_ERROR,
247 "rplay_audio_open: close-on-exec %d\n",
248 sys_err_str(errno));
249 /* return -1; */
250 }
251
252 if (rplay_audio_init() < 0)
253 {
254 return -1;
255 }
256
257 /*
258 * Make sure the audio device writes are non-blocking.
259 */
260 flags = fcntl(rplay_audio_fd, F_GETFL, 0);
261 if (flags < 0)
262 {
263 return -1;
264 }
265 flags |= FNDELAY;
266 if (fcntl(rplay_audio_fd, F_SETFL, flags) < 0)
267 {
268 return -1;
269 }
270
271 return 0;
272 }
273
274 /*
275 * Is the audio device open?
276 *
277 * Return 1 for true and 0 for false.
278 */
279 int
280 rplay_audio_isopen()
281 {
282 return (rplay_audio_fd != -1);
283 }
284
285 /*
286 * Flush the audio device.
287 *
288 * Return 0 on success and -1 on error.
289 */
290 int
291 rplay_audio_flush()
292 {
293 if (rplay_audio_fd != -1)
294 {
295 if (ioctl(rplay_audio_fd, AUDIO_DRAIN, 0) < 0)
296 {
297 report(REPORT_ERROR, "rplay_audio_init: AUDIO_DRAIN: %s\n", sys_err_str(errno));
298 }
299 }
300
301 return 0;
302 }
303
304 /*
305 * Write nbytes from buf to the audio device.
306 *
307 * Return the number of bytes written on success and -1 on error.
308 */
309 #ifdef __STDC__
310 int
311 rplay_audio_write(char *buf, int nbytes)
312 #else
313 int
314 rplay_audio_write(buf, nbytes)
315 char *buf;
316 int nbytes;
317 #endif
318 {
319 int n, nleft, nwritten;
320 char *p;
321
322 nleft = nbytes;
323 nwritten = 0;
324
325 for (p = buf; nleft > 0; nleft -= n, p += n)
326 {
327 if ((n = write(rplay_audio_fd, p, nleft)) < 0)
328 {
329 /* report(RPLAY_DEBUG, "rplay_audio_write: %s\n", sys_err_str(errno)); */
330
331 if (errno == EWOULDBLOCK)
332 {
333 return nwritten;
334 }
335 else if (errno != EINTR)
336 {
337 report(REPORT_ERROR, "rplay_audio_write: %s\n", sys_err_str(errno));
338 return -1;
339 }
340 n = 0;
341 }
342 else
343 {
344 nwritten += n;
345 }
346 }
347
348 return nwritten;
349 }
350
351 /*
352 * Close the audio device.
353 *
354 * Return 0 on success and -1 on error.
355 */
356 int
357 rplay_audio_close()
358 {
359 if (rplay_audio_fd != -1)
360 {
361 close(rplay_audio_fd);
362 }
363
364 rplay_audio_fd = -1;
365
366 return 0;
367 }
368
369 /*
370 * Return the volume of the audio device.
371 *
372 * Return 0-255 or -1 on error.
373 */
374 int
375 rplay_audio_get_volume()
376 {
377 #ifdef FAKE_VOLUME
378 return rplay_audio_volume;
379 #else /* not FAKE_VOLUME */
380 struct audio_gain ag;
381 struct audio_describe ad;
382
383 if (rplay_audio_fd < 0)
384 {
385 rplay_audio_open();
386 }
387 if (rplay_audio_fd < 0)
388 {
389 return -1;
390 }
391 if (ioctl(rplay_audio_fd, AUDIO_GET_GAINS, &ag) < 0)
392 {
393 report(REPORT_ERROR, "rplay_audio_get_volume: AUDIO_GET_GAINS: %s\n", sys_err_str(errno));
394 return -1;
395 }
396 if (ioctl(rplay_audio_fd, AUDIO_DESCRIBE, &ad) < 0)
397 {
398 report(REPORT_ERROR, "rplay_audio_get_volume: AUDIO_DESCRIBE: %s\n", sys_err_str(errno));
399 return -1;
400 }
401
402 #ifdef DEBUG
403 printf("audio.c: ag.cgain[1].receive_gain: %d\tag.cgain[1].transmit_gain: %d\tag.cgain[1].monitor_gain: %d\n",
404 ag.cgain[1].receive_gain, ag.cgain[1].transmit_gain, ag.cgain[1].monitor_gain);
405 printf("audio.c: ag.cgain[2].receive_gain: %d\tag.cgain[2].transmit_gain: %d\tag.cgain[2].monitor_gain: %d\n",
406 ag.cgain[2].receive_gain, ag.cgain[2].transmit_gain, ag.cgain[2].monitor_gain);
407 printf("audio.c: channel_mask: %d\n", ag.channel_mask);
408 #endif
409
410 return ((unsigned char) (
411 ((double) ag.cgain[1].transmit_gain - ad.min_transmit_gain)
412 / (ad.max_transmit_gain - ad.min_transmit_gain)
413 * 256));
414 #endif /* not FAKE_VOLUME */
415 }
416
417 /*
418 * Set the volume of the audio device.
419 * Input should be 0-255.
420 *
421 * Return the volume of the audio device 0-255 or -1.
422 */
423 #ifdef __STDC__
424 int
425 rplay_audio_set_volume(int volume)
426 #else
427 int
428 rplay_audio_set_volume(volume)
429 int volume;
430 #endif
431 {
432 #ifdef FAKE_VOLUME
433 if (volume < RPLAY_MIN_VOLUME)
434 {
435 volume = RPLAY_MIN_VOLUME;
436 }
437 else if (volume > RPLAY_MAX_VOLUME)
438 {
439 volume = RPLAY_MAX_VOLUME;
440 }
441 rplay_audio_volume = volume;
442
443 return rplay_audio_volume;
444 #else /* not FAKE_VOLUME */
445 struct audio_gain ag;
446 struct audio_describe ad;
447
448 rplay_audio_volume = 0;
449
450 if (rplay_audio_fd < 0)
451 {
452 rplay_audio_open();
453 }
454 if (rplay_audio_fd < 0)
455 {
456 return -1;
457 }
458 if (ioctl(rplay_audio_fd, AUDIO_GET_GAINS, &ag) < 0)
459 {
460 report(REPORT_ERROR, "rplay_audio_get_volume: AUDIO_GET_GAINS: %s\n", sys_err_str(errno));
461 return -1;
462 }
463 if (ioctl(rplay_audio_fd, AUDIO_DESCRIBE, &ad) < 0)
464 {
465 report(REPORT_ERROR, "rplay_audio_get_volume: AUDIO_DESCRIBE: %s\n", sys_err_str(errno));
466 return -1;
467 }
468
469 #ifdef DEBUG
470 printf("ag.cgain[1].receive_gain: %d\tag.cgain[1].transmit_gain: %d\tag.cgain[1].monitor_gain: %d\n",
471 ag.cgain[1].receive_gain, ag.cgain[1].transmit_gain, ag.cgain[1].monitor_gain);
472 printf("ag.cgain[2].receive_gain: %d\tag.cgain[2].transmit_gain: %d\tag.cgain[2].monitor_gain: %d\n",
473 ag.cgain[2].receive_gain, ag.cgain[2].transmit_gain, ag.cgain[2].monitor_gain);
474 printf("channel_mask: %d\n", ag.channel_mask);
475 #endif
476
477 ag.cgain[1].transmit_gain = ((int) (
478 (((double) volume)
479 * (ad.max_transmit_gain - ad.min_transmit_gain)
480 / 256)
481 + ad.min_transmit_gain));
482
483 #ifdef DEBUG
484 printf("ag.cgain[1].receive_gain: %d\tag.cgain[1].transmit_gain: %d\tag.cgain[1].monitor_gain: %d\n",
485 ag.cgain[1].receive_gain, ag.cgain[1].transmit_gain, ag.cgain[1].monitor_gain);
486 printf("ag.cgain[2].receive_gain: %d\tag.cgain[2].transmit_gain: %d\tag.cgain[2].monitor_gain: %d\n",
487 ag.cgain[2].receive_gain, ag.cgain[2].transmit_gain, ag.cgain[2].monitor_gain);
488 printf("channel_mask: %d\n", ag.channel_mask);
489 #endif
490
491 if (ioctl(rplay_audio_fd, AUDIO_SET_GAINS, ag) < 0)
492 {
493 report(REPORT_ERROR, "rplay_audio_get_volume: AUDIO_SET_GAINS: %s\n", sys_err_str(errno));
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 }