2 * audio.c - audio interface for reference clock audio drivers
8 #if defined(HAVE_SYS_AUDIOIO_H) || defined(HAVE_SUN_AUDIOIO_H) || \
9 defined(HAVE_SYS_SOUNDCARD_H) || defined(HAVE_MACHINE_SOUNDCARD_H)
12 #include "ntp_stdlib.h"
13 #include "ntp_syslog.h"
18 #include "ntp_string.h"
20 #ifdef HAVE_SYS_AUDIOIO_H
21 # include <sys/audioio.h>
22 #endif /* HAVE_SYS_AUDIOIO_H */
24 #ifdef HAVE_SUN_AUDIOIO_H
25 # include <sys/ioccom.h>
26 # include <sun/audioio.h>
27 #endif /* HAVE_SUN_AUDIOIO_H */
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
31 #endif /* HAVE_SYS_IOCTL_H */
35 #ifdef HAVE_MACHINE_SOUNDCARD_H
36 # include <machine/soundcard.h>
37 # define PCM_STYLE_SOUND
39 # ifdef HAVE_SYS_SOUNDCARD_H
40 # include <sys/soundcard.h>
41 # define PCM_STYLE_SOUND
45 #ifdef PCM_STYLE_SOUND
51 * 4.4BSD-Lite switched to an unsigned long ioctl arg. Detect common
52 * derivatives here, and apply that type. To make the following code
53 * less verbose we make a proper typedef.
54 * The joy of IOCTL programming...
56 # if defined(__FreeBSD__) || defined(__APPLE__) || defined(__NetBSD__) || defined __OpenBSD__
57 typedef unsigned long ioctl_arg_T;
59 typedef int ioctl_arg_T;
65 #ifdef HAVE_SYS_AUDIOIO_H
66 static struct audio_device device; /* audio device ident */
67 #endif /* HAVE_SYS_AUDIOIO_H */
68 #ifdef PCM_STYLE_SOUND
69 # define INIT_FILE "/etc/ntp.audio"
71 static ioctl_arg_T agc = SOUND_MIXER_WRITE_RECLEV; /* or IGAIN or LINE */
72 static ioctl_arg_T audiomonitor = SOUND_MIXER_WRITE_VOLUME; /* or OGAIN */
73 static int devmask = 0;
74 static int recmask = 0;
75 static char cf_c_dev[100], cf_i_dev[100], cf_agc[100], cf_monitor[100];
77 static const char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
78 #else /* not PCM_STYLE_SOUND */
79 static struct audio_info info; /* audio device info */
80 #endif /* not PCM_STYLE_SOUND */
81 static int ctl_fd; /* audio control file descriptor */
83 #ifdef PCM_STYLE_SOUND
84 static void audio_config_read (int, const char **, const char **);
85 static int mixer_name (const char *, int);
96 for (i = 0; i < SOUND_MIXER_NRDEVICES; ++i)
97 if (((1 << i) & m_mask)
98 && !strcmp(m_names[i], m_name))
101 return (SOUND_MIXER_NRDEVICES == i)
111 * /etc/ntp.audio# where # is the unit number
112 * /etc/ntp.audio.# where # is the unit number
115 * for contents of the form:
117 * idev /dev/input_device
118 * cdev /dev/control_device
119 * agc pcm_input_device {igain,line,line1,...}
120 * monitor pcm_monitor_device {ogain,...}
122 * The device names for the "agc" and "monitor" keywords
123 * can be found by running either the "mixer" program or the
124 * util/audio-pcm program.
126 * Great hunks of this subroutine were swiped from refclock_oncore.c
131 const char **c_dev, /* Control device */
132 const char **i_dev /* input device */
136 char device[20], line[100], ab[100];
138 snprintf(device, sizeof(device), "%s%d", INIT_FILE, unit);
139 if ((fd = fopen(device, "r")) == NULL) {
140 printf("audio_config_read: <%s> NO\n", device);
141 snprintf(device, sizeof(device), "%s.%d", INIT_FILE,
143 if ((fd = fopen(device, "r")) == NULL) {
144 printf("audio_config_read: <%s> NO\n", device);
145 snprintf(device, sizeof(device), "%s",
147 if ((fd = fopen(device, "r")) == NULL) {
148 printf("audio_config_read: <%s> NO\n",
154 printf("audio_config_read: reading <%s>\n", device);
155 while (fgets(line, sizeof line, fd)) {
159 /* Remove comments */
160 if ((cp = strchr(line, '#')))
163 /* Remove any trailing spaces */
164 for (i = strlen(line);
165 i > 0 && isascii((unsigned char)line[i - 1]) && isspace((unsigned char)line[i - 1]);
169 /* Remove leading space */
170 for (cc = line; *cc && isascii((unsigned char)*cc) && isspace((unsigned char)*cc); cc++)
173 /* Stop if nothing left */
177 /* Uppercase the command and find the arg */
178 for (ca = cc; *ca; ca++) {
179 if (isascii((unsigned char)*ca)) {
180 if (islower((unsigned char)*ca)) {
181 *ca = toupper((unsigned char)*ca);
182 } else if (isspace((unsigned char)*ca) || (*ca == '='))
187 /* Remove space (and possible =) leading the arg */
188 for (; *ca && isascii((unsigned char)*ca) && (isspace((unsigned char)*ca) || (*ca == '=')); ca++)
191 if (!strncmp(cc, "IDEV", 4) &&
192 1 == sscanf(ca, "%99s", ab)) {
193 strlcpy(cf_i_dev, ab, sizeof(cf_i_dev));
194 printf("idev <%s>\n", ab);
195 } else if (!strncmp(cc, "CDEV", 4) &&
196 1 == sscanf(ca, "%99s", ab)) {
197 strlcpy(cf_c_dev, ab, sizeof(cf_c_dev));
198 printf("cdev <%s>\n", ab);
199 } else if (!strncmp(cc, "AGC", 3) &&
200 1 == sscanf(ca, "%99s", ab)) {
201 strlcpy(cf_agc, ab, sizeof(cf_agc));
202 printf("agc <%s> %d\n", ab, i);
203 } else if (!strncmp(cc, "MONITOR", 7) &&
204 1 == sscanf(ca, "%99s", ab)) {
205 strlcpy(cf_monitor, ab, sizeof(cf_monitor));
206 printf("monitor <%s> %d\n", ab, mixer_name(ab, -1));
212 #endif /* PCM_STYLE_SOUND */
215 * audio_init - open and initialize audio device
217 * This code works with SunOS 4.x, Solaris 2.x, and PCM; however, it is
218 * believed generic and applicable to other systems with a minor twid
219 * or two. All it does is open the device, set the buffer size (Solaris
220 * only), preset the gain and set the input port. It assumes that the
221 * codec sample rate (8000 Hz), precision (8 bits), number of channels
222 * (1) and encoding (ITU-T G.711 mu-law companded) have been set by
227 const char *dname, /* device name */
228 int bufsiz, /* buffer size */
229 int unit /* device unit (0-3) */
232 #ifdef PCM_STYLE_SOUND
233 # define ACTL_DEV "/dev/mixer%d"
235 # ifdef HAVE_STRUCT_SND_SIZE
236 struct snd_size s_size;
239 snd_chan_param s_c_p;
245 #ifdef PCM_STYLE_SOUND
252 #ifdef PCM_STYLE_SOUND
253 snprintf(actl_dev, sizeof(actl_dev), ACTL_DEV, unit);
255 audio_config_read(unit, &actl, &dname);
256 /* If we have values for cf_c_dev or cf_i_dev, use them. */
266 fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
268 msyslog(LOG_ERR, "audio_init: %s %m", dname);
273 * Open audio control device.
275 ctl_fd = open(actl, O_RDWR);
277 msyslog(LOG_ERR, "audio_init: invalid control device <%s>",
284 * Set audio device parameters.
286 #ifdef PCM_STYLE_SOUND
287 printf("audio_init: <%s> bufsiz %d\n", dname, bufsiz);
290 # ifdef HAVE_STRUCT_SND_SIZE
291 if (ioctl(fd, AIOGSIZE, &s_size) == -1)
292 printf("audio_init: AIOGSIZE: %s\n", strerror(errno));
294 printf("audio_init: orig: play_size %d, rec_size %d\n",
295 s_size.play_size, s_size.rec_size);
297 s_size.play_size = s_size.rec_size = bufsiz;
298 printf("audio_init: want: play_size %d, rec_size %d\n",
299 s_size.play_size, s_size.rec_size);
301 if (ioctl(fd, AIOSSIZE, &s_size) == -1)
302 printf("audio_init: AIOSSIZE: %s\n", strerror(errno));
304 printf("audio_init: set: play_size %d, rec_size %d\n",
305 s_size.play_size, s_size.rec_size);
306 # endif /* HAVE_STRUCT_SND_SIZE */
308 # ifdef SNDCTL_DSP_SETFRAGMENT
310 int tmp = (16 << 16) + 6; /* 16 fragments, each 2^6 bytes */
311 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
312 printf("audio_init: SNDCTL_DSP_SETFRAGMENT: %s\n",
315 # endif /* SNDCTL_DSP_SETFRAGMENT */
318 if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
319 printf("audio_init: AIOGFMT: %s\n", strerror(errno));
321 printf("audio_init: play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
322 s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
325 /* Grab the device and record masks */
327 if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
328 printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
329 if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
330 printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));
332 /* validate and set any specified config file stuff */
333 if (cf_agc[0] != '\0') {
337 i = mixer_name(cf_agc, recmask);
339 agc = MIXER_WRITE(i);
341 printf("input %s not in recmask %#x\n",
345 if (cf_monitor[0] != '\0') {
349 i = mixer_name(cf_monitor, devmask);
351 audiomonitor = MIXER_WRITE(i);
353 printf("monitor %s not in devmask %#x\n",
354 cf_monitor, devmask);
357 #else /* not PCM_STYLE_SOUND */
358 AUDIO_INITINFO(&info);
359 info.play.gain = AUDIO_MAX_GAIN;
360 info.play.port = AUDIO_SPEAKER;
361 # ifdef HAVE_SYS_AUDIOIO_H
362 info.record.buffer_size = bufsiz;
363 # endif /* HAVE_SYS_AUDIOIO_H */
364 rval = ioctl(ctl_fd, AUDIO_SETINFO, (char *)&info);
366 msyslog(LOG_ERR, "audio: invalid control device parameters");
372 #endif /* not PCM_STYLE_SOUND */
378 * audio_gain - adjust codec gains and port
382 int gain, /* volume level (gain) 0-255 */
383 int mongain, /* input to output mix (monitor gain) 0-255 */
384 int port /* selected I/O port: 1 mic/2 line in */
388 static int o_mongain = -1;
389 static int o_port = -1;
391 #ifdef PCM_STYLE_SOUND
395 rval = 0; /* GCC thinks rval is used uninitialized */
398 r = l = 100 * gain / 255; /* Normalize to 0-100 */
401 printf("audio_gain: gain %d/%d\n", gain, l);
403 #if 0 /* not a good idea to do this; connector wiring dependency */
404 /* figure out what channel(s) to use. just nuke right for now. */
405 r = 0 ; /* setting to zero nicely mutes the channel */
408 if (cf_agc[0] != '\0')
409 rval = ioctl(ctl_fd, agc, &l);
413 ? SOUND_MIXER_WRITE_LINE
414 : SOUND_MIXER_WRITE_MIC
417 printf("audio_gain: agc write: %s\n", strerror(errno));
421 if (o_mongain != mongain) {
422 r = l = 100 * mongain / 255; /* Normalize to 0-100 */
425 printf("audio_gain: mongain %d/%d\n", mongain, l);
428 if (cf_monitor[0] != '\0')
429 rval = ioctl(ctl_fd, audiomonitor, &l );
431 rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_VOLUME,
434 printf("audio_gain: mongain write: %s\n",
441 if (o_port != port) {
444 printf("audio_gain: port %d\n", port);
446 l = (1 << ((port == 2) ? SOUND_MIXER_LINE : SOUND_MIXER_MIC));
447 rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_RECSRC, &l);
449 printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
455 if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &l) == -1)
456 printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
459 printf("audio_gain: recsrc is %d\n", l);
464 #else /* not PCM_STYLE_SOUND */
465 ioctl(ctl_fd, AUDIO_GETINFO, (char *)&info);
466 info.record.encoding = AUDIO_ENCODING_ULAW;
467 info.record.error = 0;
468 info.record.gain = gain;
469 if (o_mongain != mongain)
470 o_mongain = info.monitor_gain = mongain;
472 o_port = info.record.port = port;
473 rval = ioctl(ctl_fd, AUDIO_SETINFO, (char *)&info);
475 msyslog(LOG_ERR, "audio_gain: %m");
478 rval = info.record.error;
479 #endif /* not PCM_STYLE_SOUND */
485 * audio_show - display audio parameters
487 * This code doesn't really do anything, except satisfy curiousity and
488 * verify the ioctl's work.
493 #ifdef PCM_STYLE_SOUND
496 printf("audio_show: ctl_fd %d\n", ctl_fd);
497 if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
498 printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));
500 #else /* not PCM_STYLE_SOUND */
501 # ifdef HAVE_SYS_AUDIOIO_H
502 ioctl(ctl_fd, AUDIO_GETDEV, &device);
503 printf("audio: name %s, version %s, config %s\n",
504 device.name, device.version, device.config);
505 # endif /* HAVE_SYS_AUDIOIO_H */
506 ioctl(ctl_fd, AUDIO_GETINFO, (char *)&info);
508 "audio: rate %d, chan %d, prec %d, code %d, gain %d, mon %d, port %d\n",
509 info.record.sample_rate, info.record.channels,
510 info.record.precision, info.record.encoding,
511 info.record.gain, info.monitor_gain, info.record.port);
513 "audio: samples %d, eof %d, pause %d, error %d, waiting %d, balance %d\n",
514 info.record.samples, info.record.eof,
515 info.record.pause, info.record.error,
516 info.record.waiting, info.record.balance);
517 #endif /* not PCM_STYLE_SOUND */
521 #endif /* HAVE_{SYS_AUDIOIO,SUN_AUDIOIO,MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */