LEXverb.c 2.49 KB
/***************************************************************
 *
 *	LEXverb.c
 *	 a reverb program implementing a Lexicon 244
 *
 *	12/20/94		bfs initial version
 *
 */

#include <stdlib.h>
#include "LEXverb.h"
#include "allpass.h"
#include "delay.h"

struct ap_info ap[NUM_OF_AP_SECTS];
struct dl_info dl[NUM_OF_DELAY_SECTS];

void
init_proc_blocks(void)
{
	int i,j;
	
	for (i=0; i<NUM_OF_AP_SECTS; i++){
		ap[i].length = LEX_REVERB_PARMS[i].length;
		ap[i].coef = LEX_REVERB_PARMS[i].coef;
		
		/* allocate storage for delay line */
		if(( ap[i].base = (float *)calloc(sizeof(float),ap[i].length)) == NULL ){
			fprintf(stderr,"Can't allocate ap delay line memory!\n");
			exit(1);
		}
		ap[i].out_ptr = ap[i].base + 1;
		ap[i].out_data = ap[0].in_data = 0.0;
	}		
	for (j=0; j<NUM_OF_DELAY_SECTS; j++){
		dl[j].length = LEX_REVERB_PARMS[i+j].length;
		dl[j].coef = LEX_REVERB_PARMS[i+j].coef;
		
		/* allocate storage for delay line */
		if(( dl[j].base = (float *)calloc(sizeof(float),dl[j].length)) == NULL ){
			fprintf(stderr,"Can't allocate delay line memory!\n");
			exit(1);
		}
		
		dl[j].out_ptr = dl[j].base + 1;
		dl[i].out_data = dl[0].in_data = 0.0;
	}		
}		

int do_reverb(FILE *in_file, FILE *out_file, float *wet, float *dry, int stereo)
{
	short	in;
	int	i, count=0;
	float	inLeft, inRight, trim=0.7;
	long 	temp;

	init_proc_blocks();
		
	while(!feof(in_file)){
	    fread(&in, sizeof(short), 1, in_file);
	    inLeft = (float)in;
	    ap[0].in_data = (inLeft*trim);
	    ap[1].in_data = all_pass_filter(&ap[0]);
	    dl[1].in_data = ap[9].out_data;
	    ap[2].in_data = all_pass_filter(&ap[1]) + delay(&dl[1]) * dl[1].coef;
	    ap[3].in_data = all_pass_filter(&ap[2]);
	    ap[4].in_data = all_pass_filter(&ap[3]);
	    temp = (all_pass_filter(&ap[4])*(*wet) + in * (*dry));
	    wrout(temp, out_file);

	    if (stereo) {
		fread(&in, sizeof(short), 1, in_file);
		inRight = (float)in;
		ap[5].in_data = (inRight*trim);
		ap[6].in_data = all_pass_filter(&ap[5]);
		dl[0].in_data = ap[4].out_data;
		ap[7].in_data = all_pass_filter(&ap[6]) + delay(&dl[0]) * dl[0].coef;
		ap[8].in_data = all_pass_filter(&ap[7]);
		ap[9].in_data = all_pass_filter(&ap[8]);
		temp = (all_pass_filter(&ap[9])*(*wet) + in * (*dry));
		wrout(temp, out_file);
	    }

	    count++;
	}

	/* de-allocate storage for each all-pass delay line */
	for (i=0; i<NUM_OF_AP_SECTS; i++){
		free(ap[i].base);
	}	
	/* de-allocate storage for each all-pass delay line */
	for (i=0; i<NUM_OF_DELAY_SECTS; i++){
		free(dl[i].base);
	}	
	return count;	
		
}