/* FilterParametricEqualizer.c */
/*****************************************************************************/
/*                                                                           */
/*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
/*    Copyright (C) 1994  Thomas R. Lawrence                                 */
/*                                                                           */
/*    This program is free software; you can redistribute it and/or modify   */
/*    it under the terms of the GNU General Public License as published by   */
/*    the Free Software Foundation; either version 2 of the License, or      */
/*    (at your option) any later version.                                    */
/*                                                                           */
/*    This program is distributed in the hope that it will be useful,        */
/*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
/*    GNU General Public License for more details.                           */
/*                                                                           */
/*    You should have received a copy of the GNU General Public License      */
/*    along with this program; if not, write to the Free Software            */
/*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
/*                                                                           */
/*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
/*                                                                           */
/*****************************************************************************/

/* Originally from the article by P. A. Regalia and S. K. Mitra, */
/* "Tunable Digital Frequency Response Equalization Filters", */
/* IEEE Trans. on ASSP Vol. ASSP-35, 1. */
/* Implementation borrowed from the Music 4C program at the */
/* University of Illinois, Urbana-Champaign's */
/* Computer Music Project */

#include "MiscInfo.h"
#include "Audit.h"
#include "Debug.h"
#include "Definitions.h"

#include "FilterParametricEqualizer.h"
#include "Memory.h"
#include "FloatingPoint.h"


struct ParametricEqualizerRec
	{
		/* link */
		ParametricEqualizerRec*	Next;

		/* state variables */
		float										State1;
		float										State0;

		/* coefficients */
		float										A;
		float										B;
		float										K;
	};


static ParametricEqualizerRec*		FreeList = NIL;


/* flush free list */
void											FlushCachedParametricEqualizerStuff(void)
	{
		while (FreeList != NIL)
			{
				ParametricEqualizerRec*		Temp;

				Temp = FreeList;
				FreeList = FreeList->Next;
				ReleasePtr((char*)Temp);
			}
	}


/* create a new filter record */
ParametricEqualizerRec*		NewParametricEqualizer(void)
	{
		ParametricEqualizerRec*	Filter;

		if (FreeList != NIL)
			{
				Filter = FreeList;
				FreeList = FreeList->Next;
			}
		 else
			{
				Filter = (ParametricEqualizerRec*)AllocPtrCanFail(sizeof(ParametricEqualizerRec),
					"ParametricEqualizerRec");
				if (Filter == NIL)
					{
						return NIL;
					}
			}
		Filter->State1 = 0;
		Filter->State0 = 0;
		return Filter;
	}


/* dispose filter record */
void											DisposeParametricEqualizer(ParametricEqualizerRec* Filter)
	{
		CheckPtrExistence(Filter);
		Filter->Next = FreeList;
		FreeList = Filter;
	}


/* adjust filter coefficients */
void											SetParametricEqualizerCoefficients(ParametricEqualizerRec* Filter,
														float Cutoff, float Bandwidth, float Gain, long SamplingRate)
	{
		float										X;

		CheckPtrExistence(Filter);
		X = DTAN(6.28318530717958648 * Bandwidth / SamplingRate);
		Filter->A = (1 - X) / (1 + X);
		Filter->B = - DCOS(6.28318530717958648 * Cutoff / SamplingRate);
		Filter->K = Gain;
	}


/* apply filter to a sample value */
float											ApplyParametricEqualizer(ParametricEqualizerRec* Filter, float Xin)
	{
		float										Allpass;
		float										Outval;
		float										Temp;

		Allpass = Filter->A * (Xin - Filter->A * Filter->State1) + Filter->State1;
		Outval = 0.5 * ((1 + Filter->K) * Xin + (1 - Filter->K) * Allpass);
		Temp = Xin - Filter->A * Filter->State1 - Filter->B * Filter->State0;
		Filter->State1 = Filter->B * Temp + Filter->State0;
		Filter->State0 = Temp;
		return Outval;
	}
