/* FilterButterworthBandpass.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.             */
/*                                                                           */
/*****************************************************************************/

/* Based on material from pages 184-190 of */
/* Dodge, Charles and Jerse, Thomas A. */
/* Computer Music:  Synthesis, Composition, and Performance */
/* Schirmer Books, New York, 1985 */

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

#include "FilterButterworthBandpass.h"
#include "Memory.h"
#include "FloatingPoint.h"


struct ButterworthBandpassRec
	{
		/* link */
		ButterworthBandpassRec*	Next;

		/* state variables */
		float										Ym1;
		float										Ym2;
		float										Xm1;
		float										Xm2;

		/* coefficients */
		float										A0;
		float										A1;
		float										A2;
		float										B1;
		float										B2;
	};


static ButterworthBandpassRec*		FreeList = NIL;


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

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


/* create a new filter record */
ButterworthBandpassRec*		NewButterworthBandpass(void)
	{
		ButterworthBandpassRec*	Filter;

		if (FreeList != NIL)
			{
				Filter = FreeList;
				FreeList = FreeList->Next;
			}
		 else
			{
				Filter = (ButterworthBandpassRec*)AllocPtrCanFail(sizeof(ButterworthBandpassRec),
					"ButterworthBandpassRec");
				if (Filter == NIL)
					{
						return NIL;
					}
			}
		Filter->Ym1 = 0;
		Filter->Ym2 = 0;
		Filter->Xm1 = 0;
		Filter->Xm2 = 0;
		return Filter;
	}


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


/* adjust filter coefficients */
void											SetButterworthBandpassCoefficients(ButterworthBandpassRec* Filter,
														float Cutoff, float Bandwidth, long SamplingRate)
	{
		float										C;
		float										D;

		CheckPtrExistence(Filter);
		C = 1 / DTAN(3.14159265358979324 * Bandwidth / SamplingRate);
		D = 2 * DCOS(6.28318530717958648 * Cutoff / SamplingRate);
		Filter->A0 = 1 / (1 + C);
		Filter->A1 = 0;
		Filter->A2 = - Filter->A0;
		Filter->B1 = - C * D * Filter->A0;
		Filter->B2 = (C - 1) * Filter->A0;
	}


/* apply filter to a sample value */
float											ApplyButterworthBandpass(ButterworthBandpassRec* Filter, float Xin)
	{
		float										Y;

		CheckPtrExistence(Filter);
		Y = Filter->A0 * Xin + Filter->A1 * Filter->Xm1 + Filter->A2 * Filter->Xm2
			- Filter->B1 * Filter->Ym1 - Filter->B2 * Filter->Ym2;
		Filter->Xm2 = Filter->Xm1;
		Filter->Xm1 = Xin;
		Filter->Ym2 = Filter->Ym1;
		Filter->Ym1 = Y;
		return Y;
	}
