/*
* 4.4BSD-Lite switched to an unsigned long ioctl arg. Detect common
* derivatives here, and apply that type. To make the following code
* less verbose we make a proper typedef.
* The joy of IOCTL programming...
*/
# if defined(__FreeBSD__) || defined(__APPLE__) || defined(__NetBSD__) || defined __OpenBSD__
typedef unsigned long ioctl_arg_T;
#else
typedef int ioctl_arg_T;
#endif
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
;
}
/*
* 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,
const char **c_dev, /* Control device */
const char **i_dev /* input device */
)
{
FILE *fd;
char device[20], line[100], ab[100];
/* Remove any trailing spaces */
for (i = strlen(line);
i > 0 && isascii((unsigned char)line[i - 1]) && isspace((unsigned char)line[i - 1]);
)
line[--i] = '\0';
/* Remove leading space */
for (cc = line; *cc && isascii((unsigned char)*cc) && isspace((unsigned char)*cc); cc++)
continue;
/* Stop if nothing left */
if (!*cc)
continue;
/* Uppercase the command and find the arg */
for (ca = cc; *ca; ca++) {
if (isascii((unsigned char)*ca)) {
if (islower((unsigned char)*ca)) {
*ca = toupper((unsigned char)*ca);
} else if (isspace((unsigned char)*ca) || (*ca == '='))
break;
}
}
/* Remove space (and possible =) leading the arg */
for (; *ca && isascii((unsigned char)*ca) && (isspace((unsigned char)*ca) || (*ca == '=')); ca++)
continue;
/*
* audio_init - open and initialize audio device
*
* This code works with SunOS 4.x, Solaris 2.x, and PCM; however, it is
* 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
audio_init(
const char *dname, /* device name */
int bufsiz, /* buffer size */
int unit /* device unit (0-3) */
)
{
#ifdef PCM_STYLE_SOUND
# define ACTL_DEV "/dev/mixer%d"
char actl_dev[30];
# ifdef HAVE_STRUCT_SND_SIZE
struct snd_size s_size;
# endif
# ifdef AIOGFMT
snd_chan_param s_c_p;
# endif
#endif
int fd;
int rval;
const char *actl =
#ifdef PCM_STYLE_SOUND
actl_dev
#else
"/dev/audioctl"
#endif
;
audio_config_read(unit, &actl, &dname);
/* If we have values for cf_c_dev or cf_i_dev, use them. */
if (*cf_c_dev)
actl = cf_c_dev;
if (*cf_i_dev)
dname = cf_i_dev;
#endif
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 */
if (cf_agc[0] != '\0') {
int i;
/* recmask */
i = mixer_name(cf_agc, recmask);
if (i >= 0)
agc = MIXER_WRITE(i);
else
printf("input %s not in recmask %#x\n",
cf_agc, recmask);
}
if (cf_monitor[0] != '\0') {
int i;
/* devmask */
i = mixer_name(cf_monitor, devmask);
if (i >= 0)
audiomonitor = MIXER_WRITE(i);
else
printf("monitor %s not in devmask %#x\n",
cf_monitor, devmask);
}
/*
* audio_gain - adjust codec gains and port
*/
int
audio_gain(
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 */
)
{
int rval;
static int o_mongain = -1;
static int o_port = -1;
#ifdef PCM_STYLE_SOUND
int l, r;
# ifdef GCC
rval = 0; /* GCC thinks rval is used uninitialized */
# endif
r = l = 100 * gain / 255; /* Normalize to 0-100 */
# ifdef DEBUG
if (debug > 1)
printf("audio_gain: gain %d/%d\n", gain, l);
# endif
#if 0 /* not a good idea to do this; connector wiring dependency */
/* figure out what channel(s) to use. just nuke right for now. */
r = 0 ; /* setting to zero nicely mutes the channel */
#endif
l |= r << 8;
if (cf_agc[0] != '\0')
rval = ioctl(ctl_fd, agc, &l);
else
rval = ioctl(ctl_fd
, (2 == port)
? SOUND_MIXER_WRITE_LINE
: SOUND_MIXER_WRITE_MIC
, &l);
if (-1 == rval) {
printf("audio_gain: agc write: %s\n", strerror(errno));
return rval;
}
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;
if (cf_monitor[0] != '\0')
rval = ioctl(ctl_fd, audiomonitor, &l );
else
rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_VOLUME,
&l);
if (-1 == rval) {
printf("audio_gain: mongain write: %s\n",
strerror(errno));
return (rval);
}
o_mongain = mongain;
}