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