1 /* $NetBSD: audio.c,v 1.3 2003/12/04 16:23:36 drochner Exp $ */
4 * audio.c - audio interface for reference clock audio drivers
10 #if defined(HAVE_SYS_AUDIOIO_H) || defined(HAVE_SUN_AUDIOIO_H) || \
11 defined(HAVE_SYS_SOUNDCARD_H) || defined(HAVE_MACHINE_SOUNDCARD_H)
14 #include "ntp_stdlib.h"
15 #include "ntp_syslog.h"
20 #include "ntp_string.h"
22 #ifdef HAVE_SYS_AUDIOIO_H
23 # include <sys/audioio.h>
24 #endif /* HAVE_SYS_AUDIOIO_H */
26 #ifdef HAVE_SUN_AUDIOIO_H
27 # include <sys/ioccom.h>
28 # include <sun/audioio.h>
29 #endif /* HAVE_SUN_AUDIOIO_H */
31 #ifdef HAVE_SYS_IOCTL_H
32 # include <sys/ioctl.h>
33 #endif /* HAVE_SYS_IOCTL_H */
37 #ifdef HAVE_MACHINE_SOUNDCARD_H
38 # include <machine/soundcard.h>
39 # define PCM_STYLE_SOUND
41 # ifdef HAVE_SYS_SOUNDCARD_H
42 # include <sys/soundcard.h>
43 # define PCM_STYLE_SOUND
47 #ifdef PCM_STYLE_SOUND
54 #ifdef HAVE_SYS_AUDIOIO_H
55 static struct audio_device device
; /* audio device ident */
56 #endif /* HAVE_SYS_AUDIOIO_H */
57 #ifdef PCM_STYLE_SOUND
58 # define INIT_FILE "/etc/ntp.audio"
59 int agc
= SOUND_MIXER_WRITE_RECLEV
; /* or IGAIN or LINE */
60 int monitor
= SOUND_MIXER_WRITE_VOLUME
; /* or OGAIN */
63 char cf_c_dev
[100], cf_i_dev
[100], cf_agc
[100], cf_monitor
[100];
65 const char *m_names
[SOUND_MIXER_NRDEVICES
] = SOUND_DEVICE_NAMES
;
66 #else /* not PCM_STYLE_SOUND */
67 static struct audio_info info
; /* audio device info */
68 #endif /* not PCM_STYLE_SOUND */
69 static int ctl_fd
; /* audio control file descriptor */
71 #ifdef PCM_STYLE_SOUND
72 static void audio_config_read
P((int, char **, char **));
73 static int mixer_name
P((const char *, int));
84 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; ++i
)
85 if (((1 << i
) & m_mask
)
86 && !strcmp(m_names
[i
], m_name
))
89 return (SOUND_MIXER_NRDEVICES
== i
)
99 * /etc/ntp.audio# where # is the unit number
100 * /etc/ntp.audio.# where # is the unit number
103 * for contents of the form:
105 * idev /dev/input_device
106 * cdev /dev/control_device
107 * agc pcm_input_device {igain,line,line1,...}
108 * monitor pcm_monitor_device {ogain,...}
110 * The device names for the "agc" and "monitor" keywords
111 * can be found by running either the "mixer" program or the
112 * util/audio-pcm program.
114 * Great hunks of this subroutine were swiped from refclock_oncore.c
119 char **c_dev
, /* Control device */
120 char **i_dev
/* input device */
124 char device
[20], line
[100], ab
[100];
126 sprintf(device
, "%s%d", INIT_FILE
, unit
);
127 if ((fd
= fopen(device
, "r")) == NULL
) {
128 printf("audio_config_read: <%s> NO\n", device
);
129 sprintf(device
, "%s.%d", INIT_FILE
, unit
);
130 if ((fd
= fopen(device
, "r")) == NULL
) {
131 printf("audio_config_read: <%s> NO\n", device
);
132 sprintf(device
, "%s.%d", INIT_FILE
, unit
);
133 if ((fd
= fopen(device
, "r")) == NULL
) {
134 printf("audio_config_read: <%s> NO\n", device
);
139 printf("audio_config_read: reading <%s>\n", device
);
140 while (fgets(line
, sizeof line
, fd
)) {
144 /* Remove comments */
145 if ((cp
= strchr(line
, '#')))
148 /* Remove any trailing spaces */
149 for (i
= strlen(line
);
150 i
> 0 && isascii((int)line
[i
- 1]) && isspace((int)line
[i
- 1]);
154 /* Remove leading space */
155 for (cc
= line
; *cc
&& isascii((int)*cc
) && isspace((int)*cc
); cc
++)
158 /* Stop if nothing left */
162 /* Uppercase the command and find the arg */
163 for (ca
= cc
; *ca
; ca
++) {
164 if (isascii((int)*ca
)) {
165 if (islower((int)*ca
)) {
167 } else if (isspace((int)*ca
) || (*ca
== '='))
172 /* Remove space (and possible =) leading the arg */
173 for (; *ca
&& isascii((int)*ca
) && (isspace((int)*ca
) || (*ca
== '=')); ca
++)
176 if (!strncmp(cc
, "IDEV", (size_t) 4)) {
177 sscanf(ca
, "%s", ab
);
178 strcpy(cf_i_dev
, ab
);
179 printf("idev <%s>\n", ab
);
180 } else if (!strncmp(cc
, "CDEV", (size_t) 4)) {
181 sscanf(ca
, "%s", ab
);
182 strcpy(cf_c_dev
, ab
);
183 printf("cdev <%s>\n", ab
);
184 } else if (!strncmp(cc
, "AGC", (size_t) 3)) {
185 sscanf(ca
, "%s", ab
);
187 printf("agc <%s> %d\n", ab
, i
);
188 } else if (!strncmp(cc
, "MONITOR", (size_t) 7)) {
189 sscanf(ca
, "%s", ab
);
190 strcpy(cf_monitor
, ab
);
191 printf("monitor <%s> %d\n", ab
, mixer_name(ab
, -1));
197 #endif /* PCM_STYLE_SOUND */
200 * audio_init - open and initialize audio device
202 * This code works with SunOS 4.x, Solaris 2.x, and PCM; however, it is
203 * believed generic and applicable to other systems with a minor twid
204 * or two. All it does is open the device, set the buffer size (Solaris
205 * only), preset the gain and set the input port. It assumes that the
206 * codec sample rate (8000 Hz), precision (8 bits), number of channels
207 * (1) and encoding (ITU-T G.711 mu-law companded) have been set by
212 char *dname
, /* device name */
213 int bufsiz
, /* buffer size */
214 int unit
/* device unit (0-3) */
217 #ifdef PCM_STYLE_SOUND
218 # define ACTL_DEV "/dev/mixer%d"
220 # ifdef HAVE_STRUCT_SND_SIZE
221 struct snd_size s_size
;
224 snd_chan_param s_c_p
;
230 #ifdef PCM_STYLE_SOUND
237 #ifdef PCM_STYLE_SOUND
238 (void)sprintf(actl_dev
, ACTL_DEV
, unit
);
240 audio_config_read(unit
, &actl
, &dname
);
241 /* If we have values for cf_c_dev or cf_i_dev, use them. */
249 * Open audio device. Do not complain if not there.
251 fd
= open(dname
, O_RDWR
| O_NONBLOCK
, 0777);
256 * Open audio control device.
258 ctl_fd
= open(actl
, O_RDWR
);
260 msyslog(LOG_ERR
, "audio_init: invalid control device <%s>\n", actl
);
266 * Set audio device parameters.
268 #ifdef PCM_STYLE_SOUND
269 printf("audio_init: <%s> bufsiz %d\n", dname
, bufsiz
);
272 # ifdef HAVE_STRUCT_SND_SIZE
273 if (ioctl(fd
, AIOGSIZE
, &s_size
) == -1)
274 printf("audio_init: AIOGSIZE: %s\n", strerror(errno
));
276 printf("audio_init: orig: play_size %d, rec_size %d\n",
277 s_size
.play_size
, s_size
.rec_size
);
279 s_size
.play_size
= s_size
.rec_size
= bufsiz
;
280 printf("audio_init: want: play_size %d, rec_size %d\n",
281 s_size
.play_size
, s_size
.rec_size
);
283 if (ioctl(fd
, AIOSSIZE
, &s_size
) == -1)
284 printf("audio_init: AIOSSIZE: %s\n", strerror(errno
));
286 printf("audio_init: set: play_size %d, rec_size %d\n",
287 s_size
.play_size
, s_size
.rec_size
);
288 # endif /* HAVE_STRUCT_SND_SIZE */
290 # ifdef SNDCTL_DSP_SETFRAGMENT
292 int tmp
= (16 << 16) + 6; /* 16 fragments, each 2^6 bytes */
293 if (ioctl(fd
, SNDCTL_DSP_SETFRAGMENT
, &tmp
) == -1)
294 printf("audio_init: SNDCTL_DSP_SETFRAGMENT: %s\n",
297 # endif /* SNDCTL_DSP_SETFRAGMENT */
300 if (ioctl(fd
, AIOGFMT
, &s_c_p
) == -1)
301 printf("audio_init: AIOGFMT: %s\n", strerror(errno
));
303 printf("audio_init: play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
304 s_c_p
.play_rate
, s_c_p
.rec_rate
, s_c_p
.play_format
, s_c_p
.rec_format
);
307 /* Grab the device and record masks */
309 if (ioctl(ctl_fd
, SOUND_MIXER_READ_DEVMASK
, &devmask
) == -1)
310 printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno
));
311 if (ioctl(ctl_fd
, SOUND_MIXER_READ_RECMASK
, &recmask
) == -1)
312 printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno
));
314 /* validate and set any specified config file stuff */
318 i
= mixer_name(cf_agc
, devmask
);
320 agc
= MIXER_WRITE(i
);
322 printf("input %s not in recmask %#x\n",
330 i
= mixer_name(cf_monitor
, devmask
);
332 monitor
= MIXER_WRITE(i
);
334 printf("monitor %s not in devmask %#x\n",
335 cf_monitor
, devmask
);
338 #else /* not PCM_STYLE_SOUND */
339 AUDIO_INITINFO(&info
);
340 info
.play
.gain
= AUDIO_MAX_GAIN
;
341 info
.play
.port
= AUDIO_SPEAKER
;
342 # ifdef HAVE_SYS_AUDIOIO_H
343 info
.record
.buffer_size
= bufsiz
;
344 # endif /* HAVE_SYS_AUDIOIO_H */
345 rval
= ioctl(ctl_fd
, (int)AUDIO_SETINFO
, (char *)&info
);
347 msyslog(LOG_ERR
, "audio: invalid control device parameters\n");
353 #endif /* not PCM_STYLE_SOUND */
359 * audio_gain - adjust codec gains and port
363 int gain
, /* volume level (gain) 0-255 */
364 int mongain
, /* input to output mix (monitor gain) 0-255 */
365 int port
/* selected I/O port: 1 mic/2 line in */
369 static int o_mongain
= -1;
370 static int o_port
= -1;
372 #ifdef PCM_STYLE_SOUND
377 r
= l
= 100 * gain
/ 255; /* Normalize to 0-100 */
380 printf("audio_gain: gain %d/%d\n", gain
, l
);
382 /* figure out what channel(s) to use. just nuke right for now. */
383 r
= 0 ; /* setting to zero nicely mutes the channel */
387 rval
= ioctl(ctl_fd
, agc
, &l
);
390 rval
= ioctl(ctl_fd
, SOUND_MIXER_WRITE_LINE
, &l
);
392 rval
= ioctl(ctl_fd
, SOUND_MIXER_WRITE_MIC
, &l
);
395 printf("audio_gain: agc write: %s\n", strerror(errno
));
399 if (o_mongain
!= mongain
) {
400 r
= l
= 100 * mongain
/ 255; /* Normalize to 0-100 */
403 printf("audio_gain: mongain %d/%d\n", mongain
, l
);
407 rval
= ioctl(ctl_fd
, monitor
, &l
);
409 rval
= ioctl(ctl_fd
, SOUND_MIXER_WRITE_VOLUME
, &l
);
411 printf("audio_gain: mongain write: %s\n",
418 if (o_port
!= port
) {
421 printf("audio_gain: port %d\n", port
);
423 l
= (1 << ((port
== 2) ? SOUND_MIXER_LINE
: SOUND_MIXER_MIC
));
424 rval
= ioctl(ctl_fd
, SOUND_MIXER_WRITE_RECSRC
, &l
);
426 printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
432 if (ioctl(ctl_fd
, SOUND_MIXER_READ_RECSRC
, &l
) == -1)
433 printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
436 printf("audio_gain: recsrc is %d\n", l
);
441 #else /* not PCM_STYLE_SOUND */
442 ioctl(ctl_fd
, (int)AUDIO_GETINFO
, (char *)&info
);
443 info
.record
.encoding
= AUDIO_ENCODING_ULAW
;
444 info
.record
.error
= 0;
445 info
.record
.gain
= gain
;
446 if (o_mongain
!= mongain
)
447 o_mongain
= info
.monitor_gain
= mongain
;
449 o_port
= info
.record
.port
= port
;
450 rval
= ioctl(ctl_fd
, (int)AUDIO_SETINFO
, (char *)&info
);
452 msyslog(LOG_ERR
, "audio_gain: %m");
455 rval
= info
.record
.error
;
456 #endif /* not PCM_STYLE_SOUND */
462 * audio_show - display audio parameters
464 * This code doesn't really do anything, except satisfy curiousity and
465 * verify the ioctl's work.
470 #ifdef PCM_STYLE_SOUND
473 printf("audio_show: ctl_fd %d\n", ctl_fd
);
474 if (ioctl(ctl_fd
, SOUND_MIXER_READ_RECSRC
, &recsrc
) == -1)
475 printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno
));
477 #else /* not PCM_STYLE_SOUND */
478 # ifdef HAVE_SYS_AUDIOIO_H
479 ioctl(ctl_fd
, (int)AUDIO_GETDEV
, &device
);
480 printf("audio: name %s, version %s, config %s\n",
481 device
.name
, device
.version
, device
.config
);
482 # endif /* HAVE_SYS_AUDIOIO_H */
483 ioctl(ctl_fd
, (int)AUDIO_GETINFO
, (char *)&info
);
485 "audio: rate %d, chan %d, prec %d, code %d, gain %d, mon %d, port %d\n",
486 info
.record
.sample_rate
, info
.record
.channels
,
487 info
.record
.precision
, info
.record
.encoding
,
488 info
.record
.gain
, info
.monitor_gain
, info
.record
.port
);
490 "audio: samples %d, eof %d, pause %d, error %d, waiting %d, balance %d\n",
491 info
.record
.samples
, info
.record
.eof
,
492 info
.record
.pause
, info
.record
.error
,
493 info
.record
.waiting
, info
.record
.balance
);
494 #endif /* not PCM_STYLE_SOUND */
498 #endif /* HAVE_{SYS_AUDIOIO,SUN_AUDIOIO,MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */