xref: /netbsd/external/bsd/ntp/dist/libntp/audio.c (revision 6550d01e)
1 /*	$NetBSD: audio.c,v 1.3 2010/12/04 23:08:34 christos Exp $	*/
2 
3 /*
4  * audio.c - audio interface for reference clock audio drivers
5  */
6 #ifdef HAVE_CONFIG_H
7 # include <config.h>
8 #endif
9 
10 #if defined(HAVE_SYS_AUDIOIO_H) || defined(HAVE_SUN_AUDIOIO_H) || \
11     defined(HAVE_SYS_SOUNDCARD_H) || defined(HAVE_MACHINE_SOUNDCARD_H)
12 
13 #include "audio.h"
14 #include "ntp_stdlib.h"
15 #include "ntp_syslog.h"
16 #ifdef HAVE_UNISTD_H
17 # include <unistd.h>
18 #endif
19 #include <stdio.h>
20 #include "ntp_string.h"
21 
22 #ifdef HAVE_SYS_AUDIOIO_H
23 # include <sys/audioio.h>
24 #endif /* HAVE_SYS_AUDIOIO_H */
25 
26 #ifdef HAVE_SUN_AUDIOIO_H
27 # include <sys/ioccom.h>
28 # include <sun/audioio.h>
29 #endif /* HAVE_SUN_AUDIOIO_H */
30 
31 #ifdef HAVE_SYS_IOCTL_H
32 # include <sys/ioctl.h>
33 #endif /* HAVE_SYS_IOCTL_H */
34 
35 #include <fcntl.h>
36 
37 #ifdef HAVE_MACHINE_SOUNDCARD_H
38 # include <machine/soundcard.h>
39 # define PCM_STYLE_SOUND
40 #else
41 # ifdef HAVE_SYS_SOUNDCARD_H
42 #  include <sys/soundcard.h>
43 #  define PCM_STYLE_SOUND
44 # endif
45 #endif
46 
47 #ifdef PCM_STYLE_SOUND
48 # include <ctype.h>
49 #endif
50 
51 /*
52  * Global variables
53  */
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 */
61 int devmask = 0;
62 int recmask = 0;
63 char cf_c_dev[100], cf_i_dev[100], cf_agc[100], cf_monitor[100];
64 
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 */
70 
71 #ifdef PCM_STYLE_SOUND
72 static void audio_config_read (int, char **, char **);
73 static int  mixer_name (const char *, int);
74 
75 
76 int
77 mixer_name(
78 	const char *m_name,
79 	int m_mask
80 	)
81 {
82 	int i;
83 
84 	for (i = 0; i < SOUND_MIXER_NRDEVICES; ++i)
85 		if (((1 << i) & m_mask)
86 		    && !strcmp(m_names[i], m_name))
87 			break;
88 
89 	return (SOUND_MIXER_NRDEVICES == i)
90 	    ? -1
91 	    : i
92 	    ;
93 }
94 
95 
96 /*
97  * Check:
98  *
99  * /etc/ntp.audio#	where # is the unit number
100  * /etc/ntp.audio.#	where # is the unit number
101  * /etc/ntp.audio
102  *
103  * for contents of the form:
104  *
105  * idev /dev/input_device
106  * cdev /dev/control_device
107  * agc pcm_input_device {igain,line,line1,...}
108  * monitor pcm_monitor_device {ogain,...}
109  *
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.
113  *
114  * Great hunks of this subroutine were swiped from refclock_oncore.c
115  */
116 static void
117 audio_config_read(
118 	int unit,
119 	char **c_dev,	/* Control device */
120 	char **i_dev	/* input device */
121 	)
122 {
123 	FILE *fd;
124 	char device[20], line[100], ab[100];
125 
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);
135 				return;
136 			}
137 		}
138 	}
139 	printf("audio_config_read: reading <%s>\n", device);
140 	while (fgets(line, sizeof line, fd)) {
141 		char *cp, *cc, *ca;
142 		int i;
143 
144 		/* Remove comments */
145 		if ((cp = strchr(line, '#')))
146 			*cp = '\0';
147 
148 		/* Remove any trailing spaces */
149 		for (i = strlen(line);
150 		     i > 0 && isascii((unsigned char)line[i - 1]) && isspace((unsigned char)line[i - 1]);
151 			)
152 			line[--i] = '\0';
153 
154 		/* Remove leading space */
155 		for (cc = line; *cc && isascii((unsigned char)*cc) && isspace((unsigned char)*cc); cc++)
156 			continue;
157 
158 		/* Stop if nothing left */
159 		if (!*cc)
160 			continue;
161 
162 		/* Uppercase the command and find the arg */
163 		for (ca = cc; *ca; ca++) {
164 			if (isascii((unsigned char)*ca)) {
165 				if (islower((unsigned char)*ca)) {
166 					*ca = toupper((unsigned char)*ca);
167 				} else if (isspace((unsigned char)*ca) || (*ca == '='))
168 					break;
169 			}
170 		}
171 
172 		/* Remove space (and possible =) leading the arg */
173 		for (; *ca && isascii((unsigned char)*ca) && (isspace((unsigned char)*ca) || (*ca == '=')); ca++)
174 			continue;
175 
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);
186 			strcpy(cf_agc, 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));
192 		}
193 	}
194 	fclose(fd);
195 	return;
196 }
197 #endif /* PCM_STYLE_SOUND */
198 
199 /*
200  * audio_init - open and initialize audio device
201  *
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
208  * default.
209  */
210 int
211 audio_init(
212 	const char *dname,	/* device name */
213 	int	bufsiz,		/* buffer size */
214 	int	unit		/* device unit (0-3) */
215 	)
216 {
217 #ifdef PCM_STYLE_SOUND
218 # define ACTL_DEV	"/dev/mixer%d"
219 	char actl_dev[30];
220 # ifdef HAVE_STRUCT_SND_SIZE
221 	struct snd_size s_size;
222 # endif
223 # ifdef AIOGFMT
224 	snd_chan_param s_c_p;
225 # endif
226 #endif
227 	int fd;
228 	int rval;
229 	const char *actl =
230 #ifdef PCM_STYLE_SOUND
231 		actl_dev
232 #else
233 		"/dev/audioctl"
234 #endif
235 		;
236 
237 #ifdef PCM_STYLE_SOUND
238 	(void)sprintf(actl_dev, ACTL_DEV, unit);
239 
240 	audio_config_read(unit, &actl, &dname);
241 	/* If we have values for cf_c_dev or cf_i_dev, use them. */
242 	if (*cf_c_dev)
243 		actl = cf_c_dev;
244 	if (*cf_i_dev)
245 		dname = cf_i_dev;
246 #endif
247 
248 	/*
249 	 * Open audio device
250 	 */
251 	fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
252 	if (fd < 0) {
253 		msyslog(LOG_ERR, "audio_init: %s %m\n", dname);
254 		return (fd);
255 	}
256 
257 	/*
258 	 * Open audio control device.
259 	 */
260 	ctl_fd = open(actl, O_RDWR);
261 	if (ctl_fd < 0) {
262 		msyslog(LOG_ERR, "audio_init: invalid control device <%s>\n",
263 		    actl);
264 		close(fd);
265 		return(ctl_fd);
266 	}
267 
268 	/*
269 	 * Set audio device parameters.
270 	 */
271 #ifdef PCM_STYLE_SOUND
272 	printf("audio_init: <%s> bufsiz %d\n", dname, bufsiz);
273 	rval = fd;
274 
275 # ifdef HAVE_STRUCT_SND_SIZE
276 	if (ioctl(fd, AIOGSIZE, &s_size) == -1)
277 	    printf("audio_init: AIOGSIZE: %s\n", strerror(errno));
278 	else
279 	    printf("audio_init: orig: play_size %d, rec_size %d\n",
280 		s_size.play_size, s_size.rec_size);
281 
282 	s_size.play_size = s_size.rec_size = bufsiz;
283 	printf("audio_init: want: play_size %d, rec_size %d\n",
284 	       s_size.play_size, s_size.rec_size);
285 
286 	if (ioctl(fd, AIOSSIZE, &s_size) == -1)
287 	    printf("audio_init: AIOSSIZE: %s\n", strerror(errno));
288 	else
289 	    printf("audio_init: set:  play_size %d, rec_size %d\n",
290 		s_size.play_size, s_size.rec_size);
291 # endif /* HAVE_STRUCT_SND_SIZE */
292 
293 # ifdef SNDCTL_DSP_SETFRAGMENT
294 	{
295 		int tmp = (16 << 16) + 6; /* 16 fragments, each 2^6 bytes */
296 		if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
297 		    printf("audio_init: SNDCTL_DSP_SETFRAGMENT: %s\n",
298 			   strerror(errno));
299 	}
300 # endif /* SNDCTL_DSP_SETFRAGMENT */
301 
302 # ifdef AIOGFMT
303 	if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
304 	    printf("audio_init: AIOGFMT: %s\n", strerror(errno));
305 	else
306 	    printf("audio_init: play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
307 		s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
308 # endif
309 
310 	/* Grab the device and record masks */
311 
312 	if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
313 	    printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
314 	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
315 	    printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));
316 
317 	/* validate and set any specified config file stuff */
318 	if (*cf_agc) {
319 		int i;
320 
321 		i = mixer_name(cf_agc, devmask);
322 		if (i >= 0)
323 			agc = MIXER_WRITE(i);
324 		else
325 			printf("input %s not in recmask %#x\n",
326 			       cf_agc, recmask);
327 	}
328 
329 	if (*cf_monitor) {
330 		int i;
331 
332 		/* devmask */
333 		i = mixer_name(cf_monitor, devmask);
334 		if (i >= 0)
335 			monitor = MIXER_WRITE(i);
336 		else
337 			printf("monitor %s not in devmask %#x\n",
338 			       cf_monitor, devmask);
339 	}
340 
341 #else /* not PCM_STYLE_SOUND */
342 	AUDIO_INITINFO(&info);
343 	info.play.gain = AUDIO_MAX_GAIN;
344 	info.play.port = AUDIO_SPEAKER;
345 # ifdef HAVE_SYS_AUDIOIO_H
346 	info.record.buffer_size = bufsiz;
347 # endif /* HAVE_SYS_AUDIOIO_H */
348 	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
349 	if (rval < 0) {
350 		msyslog(LOG_ERR, "audio: invalid control device parameters\n");
351 		close(ctl_fd);
352 		close(fd);
353 		return(rval);
354 	}
355 	rval = fd;
356 #endif /* not PCM_STYLE_SOUND */
357 	return (rval);
358 }
359 
360 
361 /*
362  * audio_gain - adjust codec gains and port
363  */
364 int
365 audio_gain(
366 	int gain,		/* volume level (gain) 0-255 */
367 	int mongain,		/* input to output mix (monitor gain) 0-255 */
368 	int port		/* selected I/O port: 1 mic/2 line in */
369 	)
370 {
371 	int rval;
372 	static int o_mongain = -1;
373 	static int o_port = -1;
374 
375 #ifdef PCM_STYLE_SOUND
376 	int l, r;
377 
378 	rval = 0;
379 
380 	r = l = 100 * gain / 255;	/* Normalize to 0-100 */
381 # ifdef DEBUG
382 	if (debug > 1)
383 		printf("audio_gain: gain %d/%d\n", gain, l);
384 # endif
385 #if 0	/* not a good idea to do this; connector wiring dependency */
386 	/* figure out what channel(s) to use. just nuke right for now. */
387 	r = 0 ; /* setting to zero nicely mutes the channel */
388 #endif
389 	l |= r << 8;
390         if ( cf_agc )
391           rval = ioctl(ctl_fd, agc, &l);
392         else
393 	  if (port == 2) {
394 	    rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_LINE, &l);
395 	  } else {
396 	    rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_MIC, &l);
397 	  }
398 	if (rval == -1) {
399 		printf("audio_gain: agc write: %s\n", strerror(errno));
400 		return (rval);
401 	}
402 
403 	if (o_mongain != mongain) {
404 		r = l = 100 * mongain / 255;    /* Normalize to 0-100 */
405 # ifdef DEBUG
406 		if (debug > 1)
407 			printf("audio_gain: mongain %d/%d\n", mongain, l);
408 # endif
409 		l |= r << 8;
410                 if ( cf_monitor )
411                   rval = ioctl(ctl_fd, monitor, &l );
412                 else
413 		  rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_VOLUME, &l);
414 		if (rval == -1) {
415 			printf("audio_gain: mongain write: %s\n",
416 			       strerror(errno));
417 			return (rval);
418 		}
419 		o_mongain = mongain;
420 	}
421 
422 	if (o_port != port) {
423 # ifdef DEBUG
424 		if (debug > 1)
425 			printf("audio_gain: port %d\n", port);
426 # endif
427 		l = (1 << ((port == 2) ? SOUND_MIXER_LINE : SOUND_MIXER_MIC));
428 		rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_RECSRC, &l);
429 		if (rval == -1) {
430 			printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
431 			       strerror(errno));
432 			return (rval);
433 		}
434 # ifdef DEBUG
435 		if (debug > 1) {
436 			if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &l) == -1)
437 				printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
438 				       strerror(errno));
439 			else
440 				printf("audio_gain: recsrc is %d\n", l);
441 		}
442 # endif
443 		o_port = port;
444 	}
445 #else /* not PCM_STYLE_SOUND */
446 	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
447 	info.record.encoding = AUDIO_ENCODING_ULAW;
448 	info.record.error = 0;
449 	info.record.gain = gain;
450 	if (o_mongain != mongain)
451 		o_mongain = info.monitor_gain = mongain;
452 	if (o_port != port)
453 		o_port = info.record.port = port;
454 	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
455 	if (rval < 0) {
456 		msyslog(LOG_ERR, "audio_gain: %m");
457 		return (rval);
458 	}
459 	rval = info.record.error;
460 #endif /* not PCM_STYLE_SOUND */
461 	return (rval);
462 }
463 
464 
465 /*
466  * audio_show - display audio parameters
467  *
468  * This code doesn't really do anything, except satisfy curiousity and
469  * verify the ioctl's work.
470  */
471 void
472 audio_show(void)
473 {
474 #ifdef PCM_STYLE_SOUND
475 	int recsrc = 0;
476 
477 	printf("audio_show: ctl_fd %d\n", ctl_fd);
478 	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
479 	    printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));
480 
481 #else /* not PCM_STYLE_SOUND */
482 # ifdef HAVE_SYS_AUDIOIO_H
483 	ioctl(ctl_fd, (int)AUDIO_GETDEV, &device);
484 	printf("audio: name %s, version %s, config %s\n",
485 	    device.name, device.version, device.config);
486 # endif /* HAVE_SYS_AUDIOIO_H */
487 	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
488 	printf(
489 	    "audio: rate %d, chan %d, prec %d, code %d, gain %d, mon %d, port %d\n",
490 	    info.record.sample_rate, info.record.channels,
491 	    info.record.precision, info.record.encoding,
492 	    info.record.gain, info.monitor_gain, info.record.port);
493 	printf(
494 	    "audio: samples %d, eof %d, pause %d, error %d, waiting %d, balance %d\n",
495 	    info.record.samples, info.record.eof,
496 	    info.record.pause, info.record.error,
497 	    info.record.waiting, info.record.balance);
498 #endif /* not PCM_STYLE_SOUND */
499 }
500 #else
501 int audio_bs;
502 #endif /* HAVE_{SYS_AUDIOIO,SUN_AUDIOIO,MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */
503