audio.c 12.7 KB
Newer Older
Harlan Stenn's avatar
Harlan Stenn committed
1 2
/*
 * audio.c - audio interface for reference clock audio drivers
3 4 5 6 7 8
 *
 * Assumes the Sun Audio Driver API (SADA) interface used by SunOS,
 * Solaris, and OpenSolaris, supported in the Linux OSS sound layer,
 * and still used by the *BSD operating systems. In particular it
 * should work for FreeBSD from version 4.1 on with compatible sound
 * card.
Harlan Stenn's avatar
Harlan Stenn committed
9
 */
Hal Murray's avatar
Hal Murray committed
10
#include <config.h>
Harlan Stenn's avatar
Harlan Stenn committed
11

12 13
#include <string.h>

14 15 16
#if defined(HAVE_SYS_AUDIOIO_H) || \
    defined(HAVE_SYS_SOUNDCARD_H) || \
    defined(HAVE_MACHINE_SOUNDCARD_H)
Dave Mills's avatar
Dave Mills committed
17

Harlan Stenn's avatar
Harlan Stenn committed
18
#include "audio.h"
Marc Brett's avatar
Marc Brett committed
19
#include "ntp_stdlib.h"
Dave Mills's avatar
Dave Mills committed
20
#include "ntp_syslog.h"
21
#include <unistd.h>
Harlan Stenn's avatar
Harlan Stenn committed
22 23 24
#include <stdio.h>

#ifdef HAVE_SYS_AUDIOIO_H
25
# include <sys/audioio.h>
Harlan Stenn's avatar
Harlan Stenn committed
26
#endif /* HAVE_SYS_AUDIOIO_H */
Dave Mills's avatar
Dave Mills committed
27

28
#include <sys/ioctl.h>
Harlan Stenn's avatar
Harlan Stenn committed
29 30 31

#include <fcntl.h>

32 33
#ifdef HAVE_MACHINE_SOUNDCARD_H
# include <machine/soundcard.h>
34
# define USE_PCM_STYLE_SOUND
35 36 37
#else
# ifdef HAVE_SYS_SOUNDCARD_H
#  include <sys/soundcard.h>
38
#  define USE_PCM_STYLE_SOUND
39 40 41
# endif
#endif

42
#ifdef USE_PCM_STYLE_SOUND
43 44 45
# include <ctype.h>
#endif

Harlan Stenn's avatar
Harlan Stenn committed
46 47 48 49 50
/*
 * Global variables
 */
#ifdef HAVE_SYS_AUDIOIO_H
static struct audio_device device; /* audio device ident */
Dave Mills's avatar
Dave Mills committed
51
#endif /* HAVE_SYS_AUDIOIO_H */
52
#ifdef USE_PCM_STYLE_SOUND
53 54 55
# define INIT_FILE "/etc/ntp.audio"
int agc =	SOUND_MIXER_WRITE_RECLEV; /* or IGAIN or LINE */
int monitor =	SOUND_MIXER_WRITE_VOLUME; /* or OGAIN */
56 57 58 59 60
int devmask = 0;
int recmask = 0;
char cf_c_dev[100], cf_i_dev[100], cf_agc[100], cf_monitor[100];

const char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
61
#else /* not USE_PCM_STYLE_SOUND */
Harlan Stenn's avatar
Harlan Stenn committed
62
static struct audio_info info;	/* audio device info */
63
#endif /* not USE_PCM_STYLE_SOUND */
Harlan Stenn's avatar
Harlan Stenn committed
64 65
static int ctl_fd;		/* audio control file descriptor */

66
#ifdef USE_PCM_STYLE_SOUND
67
static void audio_config_read (int, const char **, const char **);
68
static int  mixer_name (const char *, int);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89


int
mixer_name(
	const char *m_name,
	int m_mask
	)
{
	int i;

	for (i = 0; i < SOUND_MIXER_NRDEVICES; ++i)
		if (((1 << i) & m_mask)
		    && !strcmp(m_names[i], m_name))
			break;

	return (SOUND_MIXER_NRDEVICES == i)
	    ? -1
	    : i
	    ;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

/*
 * Check:
 *
 * /etc/ntp.audio#	where # is the unit number
 * /etc/ntp.audio.#	where # is the unit number
 * /etc/ntp.audio
 *
 * for contents of the form:
 *
 * idev /dev/input_device
 * cdev /dev/control_device
 * agc pcm_input_device {igain,line,line1,...}
 * monitor pcm_monitor_device {ogain,...}
 *
 * The device names for the "agc" and "monitor" keywords
 * can be found by running either the "mixer" program or the
 * util/audio-pcm program.
 *
 * Great hunks of this subroutine were swiped from refclock_oncore.c
 */
static void
audio_config_read(
	int unit,
114 115
	const char **c_dev,	/* Control device */
	const char **i_dev	/* input device */
116 117 118 119 120
	)
{
	FILE *fd;
	char device[20], line[100], ab[100];

121
	snprintf(device, sizeof(device), "%s%d", INIT_FILE, unit);
122 123
	if ((fd = fopen(device, "r")) == NULL) {
		printf("audio_config_read: <%s> NO\n", device);
124 125
		snprintf(device, sizeof(device), "%s.%d", INIT_FILE,
			 unit);
126 127
		if ((fd = fopen(device, "r")) == NULL) {
			printf("audio_config_read: <%s> NO\n", device);
128 129
			snprintf(device, sizeof(device), "%s",
				 INIT_FILE);
130
			if ((fd = fopen(device, "r")) == NULL) {
131 132
				printf("audio_config_read: <%s> NO\n",
				       device);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
				return;
			}
		}
	}
	printf("audio_config_read: reading <%s>\n", device);
	while (fgets(line, sizeof line, fd)) {
		char *cp, *cc, *ca;
		int i;

		/* Remove comments */
		if ((cp = strchr(line, '#')))
			*cp = '\0';

		/* Remove any trailing spaces */
		for (i = strlen(line);
148
		     i > 0 && isascii((unsigned char)line[i - 1]) && isspace((unsigned char)line[i - 1]);
149 150 151 152
			)
			line[--i] = '\0';

		/* Remove leading space */
153
		for (cc = line; *cc && isascii((unsigned char)*cc) && isspace((unsigned char)*cc); cc++)
154 155 156 157 158 159 160 161
			continue;

		/* Stop if nothing left */
		if (!*cc)
			continue;

		/* Uppercase the command and find the arg */
		for (ca = cc; *ca; ca++) {
162 163 164 165
			if (isascii((unsigned char)*ca)) {
				if (islower((unsigned char)*ca)) {
					*ca = toupper((unsigned char)*ca);
				} else if (isspace((unsigned char)*ca) || (*ca == '='))
166 167 168 169 170
					break;
			}
		}

		/* Remove space (and possible =) leading the arg */
171
		for (; *ca && isascii((unsigned char)*ca) && (isspace((unsigned char)*ca) || (*ca == '=')); ca++)
172 173
			continue;

174 175
		if (!strncmp(cc, "IDEV", 4) &&
		    1 == sscanf(ca, "%99s", ab)) {
176
			strlcpy(cf_i_dev, ab, sizeof(cf_i_dev));
177
			printf("idev <%s>\n", ab);
178 179
		} else if (!strncmp(cc, "CDEV", 4) &&
			   1 == sscanf(ca, "%99s", ab)) {
180
			strlcpy(cf_c_dev, ab, sizeof(cf_c_dev));
181
			printf("cdev <%s>\n", ab);
182 183
		} else if (!strncmp(cc, "AGC", 3) &&
			   1 == sscanf(ca, "%99s", ab)) {
184
			strlcpy(cf_agc, ab, sizeof(cf_agc));
185
			printf("agc <%s> %d\n", ab, i);
186 187
		} else if (!strncmp(cc, "MONITOR", 7) &&
			   1 == sscanf(ca, "%99s", ab)) {
188
			strlcpy(cf_monitor, ab, sizeof(cf_monitor));
189
			printf("monitor <%s> %d\n", ab, mixer_name(ab, -1));
190 191 192 193 194
		}
	}
	fclose(fd);
	return;
}
195
#endif /* USE_PCM_STYLE_SOUND */
Harlan Stenn's avatar
Harlan Stenn committed
196 197 198 199

/*
 * audio_init - open and initialize audio device
 *
200
 * This code works with SunOS 4.x, Solaris 2.x, and PCM; however, it is
Harlan Stenn's avatar
Harlan Stenn committed
201 202 203 204 205 206 207 208
 * believed generic and applicable to other systems with a minor twid
 * or two. All it does is open the device, set the buffer size (Solaris
 * only), preset the gain and set the input port. It assumes that the
 * codec sample rate (8000 Hz), precision (8 bits), number of channels
 * (1) and encoding (ITU-T G.711 mu-law companded) have been set by
 * default.
 */
int
Dave Mills's avatar
Dave Mills committed
209
audio_init(
210
	const char *dname,	/* device name */
211
	int	bufsiz,		/* buffer size */
212
	int	unit		/* device unit (0-3) */
Dave Mills's avatar
Dave Mills committed
213
	)
Harlan Stenn's avatar
Harlan Stenn committed
214
{
215
#ifdef USE_PCM_STYLE_SOUND
216 217
# define ACTL_DEV	"/dev/mixer%d"
	char actl_dev[30];
218
# ifdef HAVE_STRUCT_SND_SIZE
219
	struct snd_size s_size;
220
# endif
221
# ifdef AIOGFMT
222
	snd_chan_param s_c_p;
223
# endif
224
#endif
Harlan Stenn's avatar
Harlan Stenn committed
225 226
	int fd;
	int rval;
227
	const char *actl =
228
#ifdef USE_PCM_STYLE_SOUND
229
		actl_dev
230 231 232 233
#else
		"/dev/audioctl"
#endif
		;
234

235
#ifdef USE_PCM_STYLE_SOUND
236
	snprintf(actl_dev, sizeof(actl_dev), ACTL_DEV, unit);
237 238

	audio_config_read(unit, &actl, &dname);
239 240
	/* If we have values for cf_c_dev or cf_i_dev, use them. */
	if (*cf_c_dev)
241
		actl = cf_c_dev;
242
	if (*cf_i_dev)
243
		dname = cf_i_dev;
244
#endif
Harlan Stenn's avatar
Harlan Stenn committed
245 246

	/*
247
	 * Open audio device
Harlan Stenn's avatar
Harlan Stenn committed
248
	 */
249
	fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
250
	if (fd < 0) {
251
		msyslog(LOG_ERR, "audio_init: %s %m", dname);
Harlan Stenn's avatar
Harlan Stenn committed
252
		return (fd);
253
	}
Harlan Stenn's avatar
Harlan Stenn committed
254 255

	/*
Dave Mills's avatar
Dave Mills committed
256
	 * Open audio control device.
Harlan Stenn's avatar
Harlan Stenn committed
257
	 */
258
	ctl_fd = open(actl, O_RDWR);
Harlan Stenn's avatar
Harlan Stenn committed
259
	if (ctl_fd < 0) {
260
		msyslog(LOG_ERR, "audio_init: invalid control device <%s>",
261
		    actl);
Harlan Stenn's avatar
Harlan Stenn committed
262 263 264 265 266 267 268
		close(fd);
		return(ctl_fd);
	}

	/*
	 * Set audio device parameters.
	 */
269
#ifdef USE_PCM_STYLE_SOUND
270 271 272
	printf("audio_init: <%s> bufsiz %d\n", dname, bufsiz);
	rval = fd;

273
# ifdef HAVE_STRUCT_SND_SIZE
274
	if (ioctl(fd, AIOGSIZE, &s_size) == -1)
275
	    printf("audio_init: AIOGSIZE: %s\n", strerror(errno));
276
	else
277 278 279 280 281 282 283 284 285 286 287
	    printf("audio_init: orig: play_size %d, rec_size %d\n",
		s_size.play_size, s_size.rec_size);

	s_size.play_size = s_size.rec_size = bufsiz;
	printf("audio_init: want: play_size %d, rec_size %d\n",
	       s_size.play_size, s_size.rec_size);

	if (ioctl(fd, AIOSSIZE, &s_size) == -1)
	    printf("audio_init: AIOSSIZE: %s\n", strerror(errno));
	else
	    printf("audio_init: set:  play_size %d, rec_size %d\n",
288
		s_size.play_size, s_size.rec_size);
289
# endif /* HAVE_STRUCT_SND_SIZE */
290

291 292 293 294 295 296 297 298 299
# ifdef SNDCTL_DSP_SETFRAGMENT
	{
		int tmp = (16 << 16) + 6; /* 16 fragments, each 2^6 bytes */
		if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
		    printf("audio_init: SNDCTL_DSP_SETFRAGMENT: %s\n",
			   strerror(errno));
	}
# endif /* SNDCTL_DSP_SETFRAGMENT */

300
# ifdef AIOGFMT
301
	if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
302
	    printf("audio_init: AIOGFMT: %s\n", strerror(errno));
303
	else
304
	    printf("audio_init: play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
305
		s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
306
# endif
307 308 309 310 311 312 313 314 315

	/* Grab the device and record masks */

	if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
	    printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
	    printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));

	/* validate and set any specified config file stuff */
316
	if (cf_agc[0] != '\0') {
317 318
		int i;

319 320
		/* recmask */
		i = mixer_name(cf_agc, recmask);
321 322 323 324 325 326 327
		if (i >= 0)
			agc = MIXER_WRITE(i);
		else
			printf("input %s not in recmask %#x\n",
			       cf_agc, recmask);
	}

328
	if (cf_monitor[0] != '\0') {
329 330 331 332 333 334 335 336 337 338 339
		int i;

		/* devmask */
		i = mixer_name(cf_monitor, devmask);
		if (i >= 0)
			monitor = MIXER_WRITE(i);
		else
			printf("monitor %s not in devmask %#x\n",
			       cf_monitor, devmask);
	}

340
#else /* not USE_PCM_STYLE_SOUND */
341
	AUDIO_INITINFO(&info);
342 343
	info.play.gain = AUDIO_MAX_GAIN;
	info.play.port = AUDIO_SPEAKER;
344
# ifdef HAVE_SYS_AUDIOIO_H
345
	info.record.buffer_size = bufsiz;
346
# endif /* HAVE_SYS_AUDIOIO_H */
John Hay's avatar
John Hay committed
347
	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
Harlan Stenn's avatar
Harlan Stenn committed
348
	if (rval < 0) {
349
		msyslog(LOG_ERR, "audio: invalid control device parameters");
Harlan Stenn's avatar
Harlan Stenn committed
350 351 352 353
		close(ctl_fd);
		close(fd);
		return(rval);
	}
354
	rval = fd;
355
#endif /* not USE_PCM_STYLE_SOUND */
356
	return (rval);
Harlan Stenn's avatar
Harlan Stenn committed
357 358 359 360
}


/*
361
 * audio_gain - adjust codec gains and port
Harlan Stenn's avatar
Harlan Stenn committed
362 363 364
 */
int
audio_gain(
365 366 367
	int gain,		/* volume level (gain) 0-255 */
	int mongain,		/* input to output mix (monitor gain) 0-255 */
	int port		/* selected I/O port: 1 mic/2 line in */
Harlan Stenn's avatar
Harlan Stenn committed
368 369 370
	)
{
	int rval;
371 372
	static int o_mongain = -1;
	static int o_port = -1;
373

374
#ifdef USE_PCM_STYLE_SOUND
375 376 377
	int l, r;

	rval = 0;
Harlan Stenn's avatar
Harlan Stenn committed
378

379 380 381
	r = l = 100 * gain / 255;	/* Normalize to 0-100 */
# ifdef DEBUG
	if (debug > 1)
382
		printf("audio_gain: gain %d/%d\n", gain, l);
383
# endif
384
#if 0	/* not a good idea to do this; connector wiring dependency */
385 386
	/* figure out what channel(s) to use. just nuke right for now. */
	r = 0 ; /* setting to zero nicely mutes the channel */
387
#endif
388
	l |= r << 8;
389 390 391 392 393 394 395 396
	if (cf_agc[0] != '\0')
		rval = ioctl(ctl_fd, agc, &l);
	else
		if (2 == port)
			rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_LINE, &l);
		else
			rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_MIC, &l);
	if (-1 == rval) {
397
		printf("audio_gain: agc write: %s\n", strerror(errno));
398
		return rval;
399 400 401 402 403 404 405 406 407
	}

	if (o_mongain != mongain) {
		r = l = 100 * mongain / 255;    /* Normalize to 0-100 */
# ifdef DEBUG
		if (debug > 1)
			printf("audio_gain: mongain %d/%d\n", mongain, l);
# endif
		l |= r << 8;
408 409 410 411 412 413
		if (cf_monitor[0] != '\0')
			rval = ioctl(ctl_fd, monitor, &l );
		else 
			rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_VOLUME,
				     &l);
		if (-1 == rval) {
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
			printf("audio_gain: mongain write: %s\n",
			       strerror(errno));
			return (rval);
		}
		o_mongain = mongain;
	}

	if (o_port != port) {
# ifdef DEBUG
		if (debug > 1)
			printf("audio_gain: port %d\n", port);
# endif
		l = (1 << ((port == 2) ? SOUND_MIXER_LINE : SOUND_MIXER_MIC));
		rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_RECSRC, &l);
		if (rval == -1) {
			printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
			       strerror(errno));
			return (rval);
		}
# ifdef DEBUG
		if (debug > 1) {
			if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &l) == -1)
				printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
				       strerror(errno));
			else
				printf("audio_gain: recsrc is %d\n", l);
		}
# endif
		o_port = port;
443
	}
444
#else /* not USE_PCM_STYLE_SOUND */
John Hay's avatar
John Hay committed
445
	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
446
	info.record.encoding = AUDIO_ENCODING_ULAW;
Harlan Stenn's avatar
Harlan Stenn committed
447
	info.record.error = 0;
448
	info.record.gain = gain;
449 450 451 452
	if (o_mongain != mongain)
		o_mongain = info.monitor_gain = mongain;
	if (o_port != port)
		o_port = info.record.port = port;
John Hay's avatar
John Hay committed
453
	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
Harlan Stenn's avatar
Harlan Stenn committed
454
	if (rval < 0) {
Marc Brett's avatar
Marc Brett committed
455
		msyslog(LOG_ERR, "audio_gain: %m");
Harlan Stenn's avatar
Harlan Stenn committed
456 457
		return (rval);
	}
458
	rval = info.record.error;
459
#endif /* not USE_PCM_STYLE_SOUND */
460
	return (rval);
Harlan Stenn's avatar
Harlan Stenn committed
461 462 463 464 465 466 467 468 469 470 471 472
}


/*
 * audio_show - display audio parameters
 *
 * This code doesn't really do anything, except satisfy curiousity and
 * verify the ioctl's work.
 */
void
audio_show(void)
{
473
#ifdef USE_PCM_STYLE_SOUND
474
	int recsrc = 0;
475 476 477 478 479

	printf("audio_show: ctl_fd %d\n", ctl_fd);
	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
	    printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));

480
#else /* not USE_PCM_STYLE_SOUND */
481
# ifdef HAVE_SYS_AUDIOIO_H
Harlan Stenn's avatar
Harlan Stenn committed
482 483 484
	ioctl(ctl_fd, (int)AUDIO_GETDEV, &device);
	printf("audio: name %s, version %s, config %s\n",
	    device.name, device.version, device.config);
485
# endif /* HAVE_SYS_AUDIOIO_H */
John Hay's avatar
John Hay committed
486
	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
Harlan Stenn's avatar
Harlan Stenn committed
487
	printf(
488 489 490 491
	    "audio: rate %d, chan %d, prec %d, code %d, gain %d, mon %d, port %d\n",
	    info.record.sample_rate, info.record.channels,
	    info.record.precision, info.record.encoding,
	    info.record.gain, info.monitor_gain, info.record.port);
Harlan Stenn's avatar
Harlan Stenn committed
492 493 494 495 496
	printf(
	    "audio: samples %d, eof %d, pause %d, error %d, waiting %d, balance %d\n",
	    info.record.samples, info.record.eof,
	    info.record.pause, info.record.error,
	    info.record.waiting, info.record.balance);
497
#endif /* not USE_PCM_STYLE_SOUND */
Harlan Stenn's avatar
Harlan Stenn committed
498
}
Marc Brett's avatar
Marc Brett committed
499 500
#else
int audio_bs;
501
#endif /* HAVE_{SYS_AUDIOIO,SUN_AUDIOIO,MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */