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
52 #ifdef HAVE_SYS_AUDIOIO_H
53 static struct audio_device device; /* audio device ident */
54 #endif /* HAVE_SYS_AUDIOIO_H */
55 #ifdef PCM_STYLE_SOUND
56 # define INIT_FILE "/etc/ntp.audio"
57 int agc = SOUND_MIXER_WRITE_RECLEV; /* or IGAIN or LINE */
58 int monitor = SOUND_MIXER_WRITE_VOLUME; /* or OGAIN */
61 char cf_c_dev[100], cf_i_dev[100], cf_agc[100], cf_monitor[100];
63 const char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
64 #else /* not PCM_STYLE_SOUND */
65 static struct audio_info info; /* audio device info */
66 #endif /* not PCM_STYLE_SOUND */
67 static int ctl_fd; /* audio control file descriptor */
69 #ifdef PCM_STYLE_SOUND
70 static void audio_config_read (int, const char **, const char **);
71 static int mixer_name (const char *, int);
82 for (i = 0; i < SOUND_MIXER_NRDEVICES; ++i)
83 if (((1 << i) & m_mask)
84 && !strcmp(m_names[i], m_name))
87 return (SOUND_MIXER_NRDEVICES == i)
97 * /etc/ntp.audio# where # is the unit number
98 * /etc/ntp.audio.# where # is the unit number
101 * for contents of the form:
103 * idev /dev/input_device
104 * cdev /dev/control_device
105 * agc pcm_input_device {igain,line,line1,...}
106 * monitor pcm_monitor_device {ogain,...}
108 * The device names for the "agc" and "monitor" keywords
109 * can be found by running either the "mixer" program or the
110 * util/audio-pcm program.
112 * Great hunks of this subroutine were swiped from refclock_oncore.c
117 const char **c_dev, /* Control device */
118 const char **i_dev /* input device */
122 char device[20], line[100], ab[100];
124 snprintf(device, sizeof(device), "%s%d", INIT_FILE, unit);
125 if ((fd = fopen(device, "r")) == NULL) {
126 printf("audio_config_read: <%s> NO\n", device);
127 snprintf(device, sizeof(device), "%s.%d", INIT_FILE,
129 if ((fd = fopen(device, "r")) == NULL) {
130 printf("audio_config_read: <%s> NO\n", device);
131 snprintf(device, sizeof(device), "%s",
133 if ((fd = fopen(device, "r")) == NULL) {
134 printf("audio_config_read: <%s> NO\n",
140 printf("audio_config_read: reading <%s>\n", device);
141 while (fgets(line, sizeof line, fd)) {
145 /* Remove comments */
146 if ((cp = strchr(line, '#')))
149 /* Remove any trailing spaces */
150 for (i = strlen(line);
151 i > 0 && isascii((unsigned char)line[i - 1]) && isspace((unsigned char)line[i - 1]);
155 /* Remove leading space */
156 for (cc = line; *cc && isascii((unsigned char)*cc) && isspace((unsigned char)*cc); cc++)
159 /* Stop if nothing left */
163 /* Uppercase the command and find the arg */
164 for (ca = cc; *ca; ca++) {
165 if (isascii((unsigned char)*ca)) {
166 if (islower((unsigned char)*ca)) {
167 *ca = toupper((unsigned char)*ca);
168 } else if (isspace((unsigned char)*ca) || (*ca == '='))
173 /* Remove space (and possible =) leading the arg */
174 for (; *ca && isascii((unsigned char)*ca) && (isspace((unsigned char)*ca) || (*ca == '=')); ca++)
177 if (!strncmp(cc, "IDEV", 4) &&
178 1 == sscanf(ca, "%99s", ab)) {
179 strlcpy(cf_i_dev, ab, sizeof(cf_i_dev));
180 printf("idev <%s>\n", ab);
181 } else if (!strncmp(cc, "CDEV", 4) &&
182 1 == sscanf(ca, "%99s", ab)) {
183 strlcpy(cf_c_dev, ab, sizeof(cf_c_dev));
184 printf("cdev <%s>\n", ab);
185 } else if (!strncmp(cc, "AGC", 3) &&
186 1 == sscanf(ca, "%99s", ab)) {
187 strlcpy(cf_agc, ab, sizeof(cf_agc));
188 printf("agc <%s> %d\n", ab, i);
189 } else if (!strncmp(cc, "MONITOR", 7) &&
190 1 == sscanf(ca, "%99s", ab)) {
191 strlcpy(cf_monitor, ab, sizeof(cf_monitor));
192 printf("monitor <%s> %d\n", ab, mixer_name(ab, -1));
198 #endif /* PCM_STYLE_SOUND */
201 * audio_init - open and initialize audio device
203 * This code works with SunOS 4.x, Solaris 2.x, and PCM; however, it is
204 * believed generic and applicable to other systems with a minor twid
205 * or two. All it does is open the device, set the buffer size (Solaris
206 * only), preset the gain and set the input port. It assumes that the
207 * codec sample rate (8000 Hz), precision (8 bits), number of channels
208 * (1) and encoding (ITU-T G.711 mu-law companded) have been set by
213 const char *dname, /* device name */
214 int bufsiz, /* buffer size */
215 int unit /* device unit (0-3) */
218 #ifdef PCM_STYLE_SOUND
219 # define ACTL_DEV "/dev/mixer%d"
221 # ifdef HAVE_STRUCT_SND_SIZE
222 struct snd_size s_size;
225 snd_chan_param s_c_p;
231 #ifdef PCM_STYLE_SOUND
238 #ifdef PCM_STYLE_SOUND
239 snprintf(actl_dev, sizeof(actl_dev), ACTL_DEV, unit);
241 audio_config_read(unit, &actl, &dname);
242 /* If we have values for cf_c_dev or cf_i_dev, use them. */
252 fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
254 msyslog(LOG_ERR, "audio_init: %s %m", dname);
259 * Open audio control device.
261 ctl_fd = open(actl, O_RDWR);
263 msyslog(LOG_ERR, "audio_init: invalid control device <%s>",
270 * Set audio device parameters.
272 #ifdef PCM_STYLE_SOUND
273 printf("audio_init: <%s> bufsiz %d\n", dname, bufsiz);
276 # ifdef HAVE_STRUCT_SND_SIZE
277 if (ioctl(fd, AIOGSIZE, &s_size) == -1)
278 printf("audio_init: AIOGSIZE: %s\n", strerror(errno));
280 printf("audio_init: orig: play_size %d, rec_size %d\n",
281 s_size.play_size, s_size.rec_size);
283 s_size.play_size = s_size.rec_size = bufsiz;
284 printf("audio_init: want: play_size %d, rec_size %d\n",
285 s_size.play_size, s_size.rec_size);
287 if (ioctl(fd, AIOSSIZE, &s_size) == -1)
288 printf("audio_init: AIOSSIZE: %s\n", strerror(errno));
290 printf("audio_init: set: play_size %d, rec_size %d\n",
291 s_size.play_size, s_size.rec_size);
292 # endif /* HAVE_STRUCT_SND_SIZE */
294 # ifdef SNDCTL_DSP_SETFRAGMENT
296 int tmp = (16 << 16) + 6; /* 16 fragments, each 2^6 bytes */
297 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
298 printf("audio_init: SNDCTL_DSP_SETFRAGMENT: %s\n",
301 # endif /* SNDCTL_DSP_SETFRAGMENT */
304 if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
305 printf("audio_init: AIOGFMT: %s\n", strerror(errno));
307 printf("audio_init: play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
308 s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
311 /* Grab the device and record masks */
313 if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
314 printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
315 if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
316 printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));
318 /* validate and set any specified config file stuff */
319 if (cf_agc[0] != '\0') {
323 i = mixer_name(cf_agc, recmask);
325 agc = MIXER_WRITE(i);
327 printf("input %s not in recmask %#x\n",
331 if (cf_monitor[0] != '\0') {
335 i = mixer_name(cf_monitor, devmask);
337 monitor = MIXER_WRITE(i);
339 printf("monitor %s not in devmask %#x\n",
340 cf_monitor, devmask);
343 #else /* not PCM_STYLE_SOUND */
344 AUDIO_INITINFO(&info);
345 info.play.gain = AUDIO_MAX_GAIN;
346 info.play.port = AUDIO_SPEAKER;
347 # ifdef HAVE_SYS_AUDIOIO_H
348 info.record.buffer_size = bufsiz;
349 # endif /* HAVE_SYS_AUDIOIO_H */
350 rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
352 msyslog(LOG_ERR, "audio: invalid control device parameters");
358 #endif /* not PCM_STYLE_SOUND */
364 * audio_gain - adjust codec gains and port
368 int gain, /* volume level (gain) 0-255 */
369 int mongain, /* input to output mix (monitor gain) 0-255 */
370 int port /* selected I/O port: 1 mic/2 line in */
374 static int o_mongain = -1;
375 static int o_port = -1;
377 #ifdef PCM_STYLE_SOUND
382 r = l = 100 * gain / 255; /* Normalize to 0-100 */
385 printf("audio_gain: gain %d/%d\n", gain, l);
387 #if 0 /* not a good idea to do this; connector wiring dependency */
388 /* figure out what channel(s) to use. just nuke right for now. */
389 r = 0 ; /* setting to zero nicely mutes the channel */
392 if (cf_agc[0] != '\0')
393 rval = ioctl(ctl_fd, agc, &l);
396 rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_LINE, &l);
398 rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_MIC, &l);
400 printf("audio_gain: agc write: %s\n", strerror(errno));
404 if (o_mongain != mongain) {
405 r = l = 100 * mongain / 255; /* Normalize to 0-100 */
408 printf("audio_gain: mongain %d/%d\n", mongain, l);
411 if (cf_monitor[0] != '\0')
412 rval = ioctl(ctl_fd, monitor, &l );
414 rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_VOLUME,
417 printf("audio_gain: mongain write: %s\n",
424 if (o_port != port) {
427 printf("audio_gain: port %d\n", port);
429 l = (1 << ((port == 2) ? SOUND_MIXER_LINE : SOUND_MIXER_MIC));
430 rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_RECSRC, &l);
432 printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
438 if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &l) == -1)
439 printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
442 printf("audio_gain: recsrc is %d\n", l);
447 #else /* not PCM_STYLE_SOUND */
448 ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
449 info.record.encoding = AUDIO_ENCODING_ULAW;
450 info.record.error = 0;
451 info.record.gain = gain;
452 if (o_mongain != mongain)
453 o_mongain = info.monitor_gain = mongain;
455 o_port = info.record.port = port;
456 rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
458 msyslog(LOG_ERR, "audio_gain: %m");
461 rval = info.record.error;
462 #endif /* not PCM_STYLE_SOUND */
468 * audio_show - display audio parameters
470 * This code doesn't really do anything, except satisfy curiousity and
471 * verify the ioctl's work.
476 #ifdef PCM_STYLE_SOUND
479 printf("audio_show: ctl_fd %d\n", ctl_fd);
480 if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
481 printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));
483 #else /* not PCM_STYLE_SOUND */
484 # ifdef HAVE_SYS_AUDIOIO_H
485 ioctl(ctl_fd, (int)AUDIO_GETDEV, &device);
486 printf("audio: name %s, version %s, config %s\n",
487 device.name, device.version, device.config);
488 # endif /* HAVE_SYS_AUDIOIO_H */
489 ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
491 "audio: rate %d, chan %d, prec %d, code %d, gain %d, mon %d, port %d\n",
492 info.record.sample_rate, info.record.channels,
493 info.record.precision, info.record.encoding,
494 info.record.gain, info.monitor_gain, info.record.port);
496 "audio: samples %d, eof %d, pause %d, error %d, waiting %d, balance %d\n",
497 info.record.samples, info.record.eof,
498 info.record.pause, info.record.error,
499 info.record.waiting, info.record.balance);
500 #endif /* not PCM_STYLE_SOUND */
504 #endif /* HAVE_{SYS_AUDIOIO,SUN_AUDIOIO,MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */