/*	Copyright (c) 1987, 1988 AT&T and Entropic Speech, Inc.	*/
/*	  All Rights Reserved	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	AND ENTROPIC SPEECH, INC.				*/
/*	The copyright notice above does not evidence any	*/
/*	actual or intended publication of such source code.	*/

/* record_data.c */
/*	record subroutine for DSP32/VME board
 *
 *	uses buffering in PGA DSP32 ( 8 Buffers with 1024 samples )
 *
 *	adc(file,buffer,size,freq)
 *
 *	file:	open file descriptor or zero (no file)
 *	buffer:	pointer to sample buffer (shorts) or zero
 *	size:	number of samples to record
 *	*freq:	sampling freq; converted to integer in setrate() to access
 *              '(freq)to' and 'to(freq)' sample rate converter programs.
 *
 *	keyboard intr signal will quit recording
 *
 *
 */
#ifndef lint
static char *sccs_id = "@(#)record_data.	1.3 12/20/90	ESI ATT";
#endif

#include <stdio.h>
#include <signal.h>
#include <dsplock.h>
#include <dsp32.h>

/* default path for dsp32 programs */
#ifdef DSP32_PATH
#define	path	DSP32_PATH /* default path for dsp32 programs */
#else
#define	path	"/usr/esps/32bin"
#endif

#define	DSP_PCR	DMA_MODE
#define	DSP_RUN	(EMR_DEF<<16|DSP_PCR)

#define	DSPBUF	0x2000	/* dsp output buffer hardwired in dsp prog. */
#define	NBUF	1024	/* transfer size to dpr ... ditto */
#define	NBYTES	(2*NBUF)

static	short*	dpr;	/* pointer to dual ported ram */
static	int	ifd;
static	short*	iptr;
static	int	icnt;	/* input count */
static	int	qcnt;	/* buffers in queue */
static	int	iwait;	/* void input buffers */
static	int	atime;	/* time for alarm */
static	int	smax, smin;	/* sample max per block */
static	int	lastpir;/* error from dsp */
static	int	vox_irqs;/*calls to irq to timeout in VOX */

static	short	ibuf[NBUF];
extern int debug_level;
int 	    dsp32_wait = 0;
static int  locked = 0;		/* lock file set? */
extern int vox_do, vox_sec, vox_thr;

adc_32(inpfd, inpbuf, size, freq, amax, amin, do_echo)
int inpfd, size, *amax, *amin, do_echo;
short *inpbuf;
double *freq;
{
    int i, j;
    int timeout(), irq();
    void (*sigsav[3])();

    iptr = inpbuf;
    icnt = size;
/*
    if ( (ifd=inpfd)<=0 ) {
	    ifd=0;
	    if ( !ifd || ( !iptr || ((long)iptr&1) ) ) return -1;
    }
*/
    if(debug_level) fprintf(stderr,"iptr ok\n");
    atime = 1;
    iwait = 2;
    lastpir = 0;
    smax = smin = 0;
    vox_irqs = (*freq / NBUF) * vox_sec;

    switch(DSP_LOCK(dsp32_wait))
    {
    case LCKFIL_OK:
	break;
    case LCKFIL_INUSE:
	printf("%s: dsp board in use.\n", "adc");
	return -1;
	break;
    case LCKFIL_ERR:
	printf("%s: error trying to secure exclusive access to dsp board.\n",
	       "adc");
	return -1;
	break;
    }

    locked = 1;

    dsprg(0,C_CSR,CSR_RESET);	/* reset all (resets sio transfer) */
    dsprg(0,C_CSR,CSR_DEFAULT);
    dsprg(0,C_STOP);		/* now we must stop them all */
    dsprg(1,C_STOP);
    dsprg(2,C_STOP);
    if ( setrate(path,freq,NULL,(do_echo? 2 : 10)) < 0 )
    {
	(void) DSP_UNLOCK;
	locked = 0;
	return -1;
    }

    if(debug_level) fprintf(stderr,"back from setrate ok\n");
    signal( SIGDSP, irq);		/* catch dsp irq's */
    sigsav[0] = (void*)signal( SIGALRM, timeout);
    sigsav[1] = (void*)signal( SIGINT, timeout);
    sigsav[2] = (void*)signal( SIGQUIT, timeout);
    if ( !dpr ) dpr = (short*)dspmap();	/* get pointer to dual ported ram */
    dsprg(1,C_RUN,DSP_RUN);		/* start DIP dsp32's */
    dsprg(2,C_RUN,DSP_RUN);
    irq(0);				/* start irq routine */
    dsprg(0,C_RUN,DSP_RUN|PCR_ENI);	/* start PGA dsp, enable irq */
    while( atime ) {
	    pause();
	    if ( debug_level )
		    fprintf(stderr, " smax %d smin %d icnt %d qcnt %d\n",
			smax, smin, size-icnt, qcnt);
    }
    dsprg(0,C_STOP);		/* halt dsp#0 */
    dsprg(0,C_CLRSIG);		/* clear pending signals */
    signal( SIGALRM, sigsav[0]);	/* restore signal handlers */
    signal( SIGINT, sigsav[1]);
    signal( SIGQUIT, sigsav[2]);
    *amax = smax;
    *amin = smin;
    if (locked)
    {
	DSP_UNLOCK;
	locked = 0;
    }
    return(size - icnt);
}

static 
irq(signal)
{
	register int n;

	if ( iwait )
		iwait--;		/* wait for first real input */
	else {
		if ( (n=icnt) > 0 ) {	/* read input data from dpr */
			if ( n > NBUF ) n = NBUF;
			if ( ifd )  {
				cpy(dpr,ibuf,NBUF);
				write(ifd,ibuf,NBYTES);
			} else {
				cpy(dpr, iptr, n);
				iptr += n;
			}
			icnt -= n;
		}
	}
	if ( (n=(short)dsprg(0,C_PIR)) <= 0 )	/* check buffer queue */
		if ( signal ) {
			lastpir = n;
			return;		/* wait for next timeout */
		}
	qcnt = n;
	if ( icnt <= 0 ) {
abort:
		alarm(0);
		atime=0;
		return;
	}
	alarm(atime);
	dsprg(0,C_SIG);
}
static 
timeout(signal)
{
	if ( (signal == SIGINT) || (signal == SIGQUIT) )
		fprintf(stderr,"\nINTR\n");
	else if (signal == -1)
		fprintf(stderr, "\nVOX\n");
	else {
		fprintf(stderr,"\nTIMEOUT");
		if ( lastpir )
			fprintf(stderr," PIR=0x%x\n", lastpir&0xffff);
		else
			fprintf(stderr,"\n");
	}
	atime = 0;
}
static 
cpy(from,to,size)		/* copy and zero padding */
register short *from;
register short *to;
{
	register int lmin = 0, lmax = 0;
	register int cnt = size;
	register int max = 0;
	register short c;
	static int vox_cnt = 0;

	if ( cnt )
		do {
			*to++ = c = *from++;
			if ( c > smax )
				smax = c;
			else if ( c < smin )
				smin = c;
			if (c > lmax)
				lmax = c;
			else if (c < lmin)
				lmin = c;
		} while ( --cnt );
	if (vox_do)
		if (abs(lmax) < vox_thr && abs(lmin) < vox_thr) {
			if (++vox_cnt == vox_irqs)
				timeout(-1);
		}
		else
			vox_cnt = 0;
}
