#include "tuner.h"

#ifdef VOLUME_SGI

#include <audio.h>

setvolume(value)
    int value;
{
    long par[4];

    par[0] = AL_LEFT_SPEAKER_GAIN;
    par[1] = value;
    par[2] = AL_RIGHT_SPEAKER_GAIN;
    par[3] = value;
    ALsetparams(AL_DEFAULT_DEVICE, par, 4);
}

getvolume()
{
    long par[4];
    int value;

    par[0] = AL_LEFT_SPEAKER_GAIN;
    par[2] = AL_RIGHT_SPEAKER_GAIN;
    ALgetparams(AL_DEFAULT_DEVICE, par, 4);
    return (par[1]+par[3]) / 2;
}

#endif /* VOLUME_SGI */

#ifdef VOLUME_SUN

/*
 * Shamelessly ripped out of /usr/demo/SOUND/gaintool.c
 * (with some modifications) -- stolcke 4/16/93
 */

#include <sys/ioctl.h>
#include <sys/file.h>
#include <multimedia/libaudio.h>
#include <multimedia/audio_device.h>
#include <math.h>

#define AUDIO_CTLDEV	"/dev/audioctl"
#define MAX_GAIN	255

static char		*Audio_ctldev = AUDIO_CTLDEV;
static int		Audio_fd = -1;

static void
audio_open()
{
	if ((Audio_fd = open(Audio_ctldev, O_RDWR)) < 0) {
		perror(Audio_ctldev);
		exit (1);
	}
}

/* Convert local gain into device parameters */
static unsigned
scale_gain(g)
	unsigned        g;
{
	return (AUDIO_MIN_GAIN + (unsigned)
	    irint(((double) (AUDIO_MAX_GAIN - AUDIO_MIN_GAIN)) *
	    ((double)g / (double)MAX_GAIN)));
}

/* Convert device gain into the local scaling factor */
unsigned
unscale_gain(g)
	unsigned        g;
{
	return ((unsigned) irint((double)MAX_GAIN *
	    (((double)(g - AUDIO_MIN_GAIN)) /
	    (double)(AUDIO_MAX_GAIN - AUDIO_MIN_GAIN))));
}

void
setvolume(value)
    int value;
{
	Audio_info	aif;

	if (Audio_fd < 0)
		audio_open();

	AUDIO_INITINFO(&aif);
	aif.play.gain = scale_gain(value);

	if (ioctl(Audio_fd, AUDIO_SETINFO, &aif) < 0) {
		perror(Audio_ctldev);
		exit (1);
	}
	
}

getvolume ()
{
	Audio_info	aif;

	if (Audio_fd < 0)
		audio_open();

	if (ioctl(Audio_fd, AUDIO_GETINFO, &aif) < 0) {
		perror(Audio_ctldev);
		exit(1);
	}

	return unscale_gain(aif.play.gain);
}

#endif /* VOLUME_SUN */

#ifdef VOLUME_AUDIOFILE

#include <AF/AFlib.h>
#include <math.h>
#include <stdio.h>

int device;

AFAudioConn *aud;
AC audio_context;
AFSetACAttributes attributes;

int FindDefaultDevice(aud)             
AFAudioConn *aud; 
{
        AFDeviceDescriptor *aDev;
        int     i;

        for(i=0; i<ANumberOfAudioDevices(aud); i++) {
                aDev = AAudioDeviceDescriptor(aud, i);
                if(aDev->inputsFromPhone == 0 && 
		   aDev->outputsToPhone == 0 &&
		   aDev->playSampleFreq == 8000 &&
		   aDev->playBufType == MU255)
		    return i;
        }
        return -1;
}

void
setvolume(value)
    int value;
{
    int outcur, outmin, outmax, steps;
    int volume;

    extern int device;

    extern AFAudioConn *aud;
    extern AC audio_context;
    extern AFSetACAttributes attributes;

    if((aud = AFOpenAudioConn("")) == NULL) 
    {
	fprintf(stderr, "tuner: can't open connection.\n");
	exit(1);
    }
    
    /* set up audio context, find sample size and sample rate */
    
    device = FindDefaultDevice(aud);

    if(device < 0)
    {
	fprintf(stderr, "tuner: unable find a suitable device.\n");
	exit(1);
    }
    
    audio_context = AFCreateAC(aud, device, 0, 0);

    outcur = AFQueryOutputGain(audio_context, &outmin, &outmax);
    
    volume = value;
    if(volume == 0)
	AFSetOutputGain(audio_context, outmin);
    else
	if(volume > 0)
	{
	    steps = outmax - outmin;
	    AFSetOutputGain(audio_context, outmin + (steps * volume /100));
	} 
	else
	    AFSetOutputGain(audio_context, 0);

    AFFreeAC(audio_context);
    AFCloseAudioConn(aud);
}

getvolume ()
{
    int outcur, outmin, outmax, steps;
    int volume;

    extern int device;

    extern AFAudioConn *aud;
    extern AC audio_context;
    extern AFSetACAttributes attributes;

    if((aud = AFOpenAudioConn("")) == NULL) 
    {
	fprintf(stderr, "tuner: can't open connection.\n");
	exit(1);
    }
    
    /* set up audio context, find sample size and sample rate */
    
    device = FindDefaultDevice(aud);

    if(device < 0)
    {
	fprintf(stderr, "tuner: unable find a suitable device.\n");
	exit(1);
    }
    
    audio_context = AFCreateAC(aud, device, 0, 0);

    outcur = AFQueryOutputGain(audio_context, &outmin, &outmax);
    
    volume =  (outcur - outmin) / steps * 100;
    AFFreeAC(audio_context);
    AFCloseAudioConn(aud);
    return volume;
}

#endif /* VOLUME_AUDIOFILE */
