Home | History | Annotate | Line # | Download | only in libntp
audio.c revision 1.5.4.2.2.1
      1 /*	$NetBSD: audio.c,v 1.5.4.2.2.1 2015/11/08 01:55:28 riz 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, const char **, const 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 	const char **c_dev,	/* Control device */
    120 	const char **i_dev	/* input device */
    121 	)
    122 {
    123 	FILE *fd;
    124 	char device[20], line[100], ab[100];
    125 
    126 	snprintf(device, sizeof(device), "%s%d", INIT_FILE, unit);
    127 	if ((fd = fopen(device, "r")) == NULL) {
    128 		printf("audio_config_read: <%s> NO\n", device);
    129 		snprintf(device, sizeof(device), "%s.%d", INIT_FILE,
    130 			 unit);
    131 		if ((fd = fopen(device, "r")) == NULL) {
    132 			printf("audio_config_read: <%s> NO\n", device);
    133 			snprintf(device, sizeof(device), "%s",
    134 				 INIT_FILE);
    135 			if ((fd = fopen(device, "r")) == NULL) {
    136 				printf("audio_config_read: <%s> NO\n",
    137 				       device);
    138 				return;
    139 			}
    140 		}
    141 	}
    142 	printf("audio_config_read: reading <%s>\n", device);
    143 	while (fgets(line, sizeof line, fd)) {
    144 		char *cp, *cc, *ca;
    145 		int i;
    146 
    147 		/* Remove comments */
    148 		if ((cp = strchr(line, '#')))
    149 			*cp = '\0';
    150 
    151 		/* Remove any trailing spaces */
    152 		for (i = strlen(line);
    153 		     i > 0 && isascii((unsigned char)line[i - 1]) && isspace((unsigned char)line[i - 1]);
    154 			)
    155 			line[--i] = '\0';
    156 
    157 		/* Remove leading space */
    158 		for (cc = line; *cc && isascii((unsigned char)*cc) && isspace((unsigned char)*cc); cc++)
    159 			continue;
    160 
    161 		/* Stop if nothing left */
    162 		if (!*cc)
    163 			continue;
    164 
    165 		/* Uppercase the command and find the arg */
    166 		for (ca = cc; *ca; ca++) {
    167 			if (isascii((unsigned char)*ca)) {
    168 				if (islower((unsigned char)*ca)) {
    169 					*ca = toupper((unsigned char)*ca);
    170 				} else if (isspace((unsigned char)*ca) || (*ca == '='))
    171 					break;
    172 			}
    173 		}
    174 
    175 		/* Remove space (and possible =) leading the arg */
    176 		for (; *ca && isascii((unsigned char)*ca) && (isspace((unsigned char)*ca) || (*ca == '=')); ca++)
    177 			continue;
    178 
    179 		if (!strncmp(cc, "IDEV", 4) &&
    180 		    1 == sscanf(ca, "%99s", ab)) {
    181 			strlcpy(cf_i_dev, ab, sizeof(cf_i_dev));
    182 			printf("idev <%s>\n", ab);
    183 		} else if (!strncmp(cc, "CDEV", 4) &&
    184 			   1 == sscanf(ca, "%99s", ab)) {
    185 			strlcpy(cf_c_dev, ab, sizeof(cf_c_dev));
    186 			printf("cdev <%s>\n", ab);
    187 		} else if (!strncmp(cc, "AGC", 3) &&
    188 			   1 == sscanf(ca, "%99s", ab)) {
    189 			strlcpy(cf_agc, ab, sizeof(cf_agc));
    190 			printf("agc <%s> %d\n", ab, i);
    191 		} else if (!strncmp(cc, "MONITOR", 7) &&
    192 			   1 == sscanf(ca, "%99s", ab)) {
    193 			strlcpy(cf_monitor, ab, sizeof(cf_monitor));
    194 			printf("monitor <%s> %d\n", ab, mixer_name(ab, -1));
    195 		}
    196 	}
    197 	fclose(fd);
    198 	return;
    199 }
    200 #endif /* PCM_STYLE_SOUND */
    201 
    202 /*
    203  * audio_init - open and initialize audio device
    204  *
    205  * This code works with SunOS 4.x, Solaris 2.x, and PCM; however, it is
    206  * believed generic and applicable to other systems with a minor twid
    207  * or two. All it does is open the device, set the buffer size (Solaris
    208  * only), preset the gain and set the input port. It assumes that the
    209  * codec sample rate (8000 Hz), precision (8 bits), number of channels
    210  * (1) and encoding (ITU-T G.711 mu-law companded) have been set by
    211  * default.
    212  */
    213 int
    214 audio_init(
    215 	const char *dname,	/* device name */
    216 	int	bufsiz,		/* buffer size */
    217 	int	unit		/* device unit (0-3) */
    218 	)
    219 {
    220 #ifdef PCM_STYLE_SOUND
    221 # define ACTL_DEV	"/dev/mixer%d"
    222 	char actl_dev[30];
    223 # ifdef HAVE_STRUCT_SND_SIZE
    224 	struct snd_size s_size;
    225 # endif
    226 # ifdef AIOGFMT
    227 	snd_chan_param s_c_p;
    228 # endif
    229 #endif
    230 	int fd;
    231 	int rval;
    232 	const char *actl =
    233 #ifdef PCM_STYLE_SOUND
    234 		actl_dev
    235 #else
    236 		"/dev/audioctl"
    237 #endif
    238 		;
    239 
    240 #ifdef PCM_STYLE_SOUND
    241 	snprintf(actl_dev, sizeof(actl_dev), ACTL_DEV, unit);
    242 
    243 	audio_config_read(unit, &actl, &dname);
    244 	/* If we have values for cf_c_dev or cf_i_dev, use them. */
    245 	if (*cf_c_dev)
    246 		actl = cf_c_dev;
    247 	if (*cf_i_dev)
    248 		dname = cf_i_dev;
    249 #endif
    250 
    251 	/*
    252 	 * Open audio device
    253 	 */
    254 	fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
    255 	if (fd < 0) {
    256 		msyslog(LOG_ERR, "audio_init: %s %m", dname);
    257 		return (fd);
    258 	}
    259 
    260 	/*
    261 	 * Open audio control device.
    262 	 */
    263 	ctl_fd = open(actl, O_RDWR);
    264 	if (ctl_fd < 0) {
    265 		msyslog(LOG_ERR, "audio_init: invalid control device <%s>",
    266 		    actl);
    267 		close(fd);
    268 		return(ctl_fd);
    269 	}
    270 
    271 	/*
    272 	 * Set audio device parameters.
    273 	 */
    274 #ifdef PCM_STYLE_SOUND
    275 	printf("audio_init: <%s> bufsiz %d\n", dname, bufsiz);
    276 	rval = fd;
    277 
    278 # ifdef HAVE_STRUCT_SND_SIZE
    279 	if (ioctl(fd, AIOGSIZE, &s_size) == -1)
    280 	    printf("audio_init: AIOGSIZE: %s\n", strerror(errno));
    281 	else
    282 	    printf("audio_init: orig: play_size %d, rec_size %d\n",
    283 		s_size.play_size, s_size.rec_size);
    284 
    285 	s_size.play_size = s_size.rec_size = bufsiz;
    286 	printf("audio_init: want: play_size %d, rec_size %d\n",
    287 	       s_size.play_size, s_size.rec_size);
    288 
    289 	if (ioctl(fd, AIOSSIZE, &s_size) == -1)
    290 	    printf("audio_init: AIOSSIZE: %s\n", strerror(errno));
    291 	else
    292 	    printf("audio_init: set:  play_size %d, rec_size %d\n",
    293 		s_size.play_size, s_size.rec_size);
    294 # endif /* HAVE_STRUCT_SND_SIZE */
    295 
    296 # ifdef SNDCTL_DSP_SETFRAGMENT
    297 	{
    298 		int tmp = (16 << 16) + 6; /* 16 fragments, each 2^6 bytes */
    299 		if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
    300 		    printf("audio_init: SNDCTL_DSP_SETFRAGMENT: %s\n",
    301 			   strerror(errno));
    302 	}
    303 # endif /* SNDCTL_DSP_SETFRAGMENT */
    304 
    305 # ifdef AIOGFMT
    306 	if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
    307 	    printf("audio_init: AIOGFMT: %s\n", strerror(errno));
    308 	else
    309 	    printf("audio_init: play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
    310 		s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
    311 # endif
    312 
    313 	/* Grab the device and record masks */
    314 
    315 	if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
    316 	    printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
    317 	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
    318 	    printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));
    319 
    320 	/* validate and set any specified config file stuff */
    321 	if (cf_agc[0] != '\0') {
    322 		int i;
    323 
    324 		/* recmask */
    325 		i = mixer_name(cf_agc, recmask);
    326 		if (i >= 0)
    327 			agc = MIXER_WRITE(i);
    328 		else
    329 			printf("input %s not in recmask %#x\n",
    330 			       cf_agc, recmask);
    331 	}
    332 
    333 	if (cf_monitor[0] != '\0') {
    334 		int i;
    335 
    336 		/* devmask */
    337 		i = mixer_name(cf_monitor, devmask);
    338 		if (i >= 0)
    339 			monitor = MIXER_WRITE(i);
    340 		else
    341 			printf("monitor %s not in devmask %#x\n",
    342 			       cf_monitor, devmask);
    343 	}
    344 
    345 #else /* not PCM_STYLE_SOUND */
    346 	AUDIO_INITINFO(&info);
    347 	info.play.gain = AUDIO_MAX_GAIN;
    348 	info.play.port = AUDIO_SPEAKER;
    349 # ifdef HAVE_SYS_AUDIOIO_H
    350 	info.record.buffer_size = bufsiz;
    351 # endif /* HAVE_SYS_AUDIOIO_H */
    352 	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
    353 	if (rval < 0) {
    354 		msyslog(LOG_ERR, "audio: invalid control device parameters");
    355 		close(ctl_fd);
    356 		close(fd);
    357 		return(rval);
    358 	}
    359 	rval = fd;
    360 #endif /* not PCM_STYLE_SOUND */
    361 	return (rval);
    362 }
    363 
    364 
    365 /*
    366  * audio_gain - adjust codec gains and port
    367  */
    368 int
    369 audio_gain(
    370 	int gain,		/* volume level (gain) 0-255 */
    371 	int mongain,		/* input to output mix (monitor gain) 0-255 */
    372 	int port		/* selected I/O port: 1 mic/2 line in */
    373 	)
    374 {
    375 	int rval;
    376 	static int o_mongain = -1;
    377 	static int o_port = -1;
    378 
    379 #ifdef PCM_STYLE_SOUND
    380 	int l, r;
    381 
    382 # ifdef GCC
    383 	rval = 0;		/* GCC thinks rval is used uninitialized */
    384 # endif
    385 
    386 	r = l = 100 * gain / 255;	/* Normalize to 0-100 */
    387 # ifdef DEBUG
    388 	if (debug > 1)
    389 		printf("audio_gain: gain %d/%d\n", gain, l);
    390 # endif
    391 #if 0	/* not a good idea to do this; connector wiring dependency */
    392 	/* figure out what channel(s) to use. just nuke right for now. */
    393 	r = 0 ; /* setting to zero nicely mutes the channel */
    394 #endif
    395 	l |= r << 8;
    396 	if (cf_agc[0] != '\0')
    397 		rval = ioctl(ctl_fd, agc, &l);
    398 	else
    399 		rval = ioctl(ctl_fd
    400 			    , (2 == port)
    401 				? SOUND_MIXER_WRITE_LINE
    402 				: SOUND_MIXER_WRITE_MIC
    403 			    , &l);
    404 	if (-1 == rval) {
    405 		printf("audio_gain: agc write: %s\n", strerror(errno));
    406 		return rval;
    407 	}
    408 
    409 	if (o_mongain != mongain) {
    410 		r = l = 100 * mongain / 255;    /* Normalize to 0-100 */
    411 # ifdef DEBUG
    412 		if (debug > 1)
    413 			printf("audio_gain: mongain %d/%d\n", mongain, l);
    414 # endif
    415 		l |= r << 8;
    416 		if (cf_monitor[0] != '\0')
    417 			rval = ioctl(ctl_fd, monitor, &l );
    418 		else
    419 			rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_VOLUME,
    420 				     &l);
    421 		if (-1 == rval) {
    422 			printf("audio_gain: mongain write: %s\n",
    423 			       strerror(errno));
    424 			return (rval);
    425 		}
    426 		o_mongain = mongain;
    427 	}
    428 
    429 	if (o_port != port) {
    430 # ifdef DEBUG
    431 		if (debug > 1)
    432 			printf("audio_gain: port %d\n", port);
    433 # endif
    434 		l = (1 << ((port == 2) ? SOUND_MIXER_LINE : SOUND_MIXER_MIC));
    435 		rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_RECSRC, &l);
    436 		if (rval == -1) {
    437 			printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
    438 			       strerror(errno));
    439 			return (rval);
    440 		}
    441 # ifdef DEBUG
    442 		if (debug > 1) {
    443 			if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &l) == -1)
    444 				printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
    445 				       strerror(errno));
    446 			else
    447 				printf("audio_gain: recsrc is %d\n", l);
    448 		}
    449 # endif
    450 		o_port = port;
    451 	}
    452 #else /* not PCM_STYLE_SOUND */
    453 	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
    454 	info.record.encoding = AUDIO_ENCODING_ULAW;
    455 	info.record.error = 0;
    456 	info.record.gain = gain;
    457 	if (o_mongain != mongain)
    458 		o_mongain = info.monitor_gain = mongain;
    459 	if (o_port != port)
    460 		o_port = info.record.port = port;
    461 	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
    462 	if (rval < 0) {
    463 		msyslog(LOG_ERR, "audio_gain: %m");
    464 		return (rval);
    465 	}
    466 	rval = info.record.error;
    467 #endif /* not PCM_STYLE_SOUND */
    468 	return (rval);
    469 }
    470 
    471 
    472 /*
    473  * audio_show - display audio parameters
    474  *
    475  * This code doesn't really do anything, except satisfy curiousity and
    476  * verify the ioctl's work.
    477  */
    478 void
    479 audio_show(void)
    480 {
    481 #ifdef PCM_STYLE_SOUND
    482 	int recsrc = 0;
    483 
    484 	printf("audio_show: ctl_fd %d\n", ctl_fd);
    485 	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
    486 	    printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));
    487 
    488 #else /* not PCM_STYLE_SOUND */
    489 # ifdef HAVE_SYS_AUDIOIO_H
    490 	ioctl(ctl_fd, (int)AUDIO_GETDEV, &device);
    491 	printf("audio: name %s, version %s, config %s\n",
    492 	    device.name, device.version, device.config);
    493 # endif /* HAVE_SYS_AUDIOIO_H */
    494 	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
    495 	printf(
    496 	    "audio: rate %d, chan %d, prec %d, code %d, gain %d, mon %d, port %d\n",
    497 	    info.record.sample_rate, info.record.channels,
    498 	    info.record.precision, info.record.encoding,
    499 	    info.record.gain, info.monitor_gain, info.record.port);
    500 	printf(
    501 	    "audio: samples %d, eof %d, pause %d, error %d, waiting %d, balance %d\n",
    502 	    info.record.samples, info.record.eof,
    503 	    info.record.pause, info.record.error,
    504 	    info.record.waiting, info.record.balance);
    505 #endif /* not PCM_STYLE_SOUND */
    506 }
    507 #else
    508 int audio_bs;
    509 #endif /* HAVE_{SYS_AUDIOIO,SUN_AUDIOIO,MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */
    510