/*----------------------------------------------------------------------
filter an audio file
----------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>

int bfilt (double *s, int l, double fsamp,
           double fpass, double fstop, double hpass, double hstop);
int rmono (char *ifil, double *s, int l);
int wmono (char *ofil, double *s, int l);

#define LARR 441000

int main (void)
{
/* Local variables */
	int ierr, j, l;
	double fpass, fsamp, fstop, hpass, hstop, s[LARR], sfilt[LARR];
	char ifil[32] = "input.raw";
	FILE *iou;
/* Check input file */
	iou = fopen(ifil, "r");
	if (NULL == iou) {
		printf("couldn't open file:  %s\n", ifil);
		exit(EXIT_FAILURE);
	}
	j = fseek(iou, -1L, SEEK_END);
	if (0 != j) {
		printf("couldn't seek to end of file!\n");
		exit(EXIT_FAILURE);
	}
	l = ftell(iou) + 1;
	if (l <= 0) {
		printf("file length is %i!\n", l);
		exit(EXIT_FAILURE);
	}
	l /= 2;
/* Read input */
	ierr = rmono (ifil, s, l);
	if (0 != ierr) {
		printf("trouble reading input file\n");
		exit(EXIT_FAILURE);
	}
/* Low-pass filter */
	for (j = 0; j < l; ++j) sfilt[j] = s[j];
	fsamp = 44100.;
	fpass = 1000.;
	fstop = 1100.;
	hpass = 0.95;
	hstop = 0.05;
	ierr = bfilt (sfilt, l, fsamp, fpass, fstop, hpass, hstop);
	printf("lo pass, bfilt returns #%i\n", ierr);
	ierr = wmono ("lowpass.raw", sfilt, l);
	printf("wmono returns #%i\n", ierr);
/* High-pass filter */
	for (j = 0; j < l; ++j) sfilt[j] = s[j];
	fstop = 900.;
	hpass = 0.9;
	hstop = 0.1;
	ierr = bfilt (sfilt, l, fsamp, fpass, fstop, hpass, hstop);
	printf("hi pass, bfilt returns #%i\n", ierr);
	ierr = wmono ("highpass.raw", sfilt, l);
	printf("wmono returns #%i\n", ierr);
/* Bandpass filter */
	for (j = 0; j < l; ++j) sfilt[j] = s[j];
	fpass = 1050.;
	fstop = 1100.;
	hpass = 0.99;
	hstop = 0.1;
	ierr = bfilt (sfilt, l, fsamp, fpass, fstop, hpass, hstop);
	printf("bandpass lo, bfilt returns #%i\n", ierr);
	fpass = 950.;
	fstop = 900.;
	ierr = bfilt (sfilt, l, fsamp, fpass, fstop, hpass, hstop);
	printf("bandpass hi, bfilt returns #%i\n", ierr);
	ierr = wmono ("bandpass.raw", sfilt, l);
	printf("wmono returns #%i\n", ierr);
	printf("program complete\n");
	exit(EXIT_SUCCESS);
} /* end of main */
