/*----------------------------------------------------------------------------1//2// 3 Band EQ :)3//4// EQ.C - Main Source file for 3 band EQ5//6// (c) Neil C / Etanza Systems / 2K67//8// Shouts / Loves / Moans = etanza at lycos dot co dot uk9//10// This work is hereby placed in the public domain for all purposes, including11// use in commercial applications.12//13// The author assumes NO RESPONSIBILITY for any problems caused by the use of14// this software.15//16//----------------------------------------------------------------------------*/1718/* NOTES :19//20// - Original filter code by Paul Kellet (musicdsp.pdf)21//22// - Uses 4 first order filters in series, should give 24dB per octave23//24// - Now with P4 Denormal fix :)252627//----------------------------------------------------------------------------*/2829/* ----------30//| Includes |31// ----------*/32#include <stdio.h>33#include <string.h>34#include <math.h>35#include "eq.h"36#include "macros.h"373839/* -----------40//| Constants |41// -----------*/4243static double vsa = (1.0 / 4294967295.0); /* Very small amount (Denormal Fix) */444546/* ---------------47//| Initialise EQ |48// ---------------*/4950/* Recommended frequencies are ...51//52// lowfreq = 880 Hz53// highfreq = 5000 Hz54//55// Set mixfreq to whatever rate your system is using (eg 48Khz)*/5657void init_3band_state(EQSTATE * es, int lowfreq, int highfreq, int mixfreq)58{59/* Clear state */6061memset(es, 0, sizeof(EQSTATE));6263/* Set Low/Mid/High gains to unity */6465es->lg = 1.0;66es->mg = 1.0;67es->hg = 1.0;6869/* Calculate filter cutoff frequencies */7071es->lf = 2 * sin(M_PI * ((double) lowfreq / (double) mixfreq));72es->hf = 2 * sin(M_PI * ((double) highfreq / (double) mixfreq));73}747576/* ---------------77//| EQ one sample |78// ---------------*/7980/* - sample can be any range you like :)81//82// Note that the output will depend on the gain settings for each band83// (especially the bass) so may require clipping before output, but you84// knew that anyway :)*/8586double do_3band(EQSTATE * es, int sample)87{88/* Locals */8990double l, m, h; /* Low / Mid / High - Sample Values */9192/* Filter #1 (lowpass) */9394es->f1p0 += (es->lf * ((double) sample - es->f1p0)) + vsa;95es->f1p1 += (es->lf * (es->f1p0 - es->f1p1));96es->f1p2 += (es->lf * (es->f1p1 - es->f1p2));97es->f1p3 += (es->lf * (es->f1p2 - es->f1p3));9899l = es->f1p3;100101/* Filter #2 (highpass) */102103es->f2p0 += (es->hf * ((double) sample - es->f2p0)) + vsa;104es->f2p1 += (es->hf * (es->f2p0 - es->f2p1));105es->f2p2 += (es->hf * (es->f2p1 - es->f2p2));106es->f2p3 += (es->hf * (es->f2p2 - es->f2p3));107108h = es->sdm3 - es->f2p3;109110/* Calculate midrange (signal - (low + high)) */111112/* m = es->sdm3 - (h + l); */113/* fix from http://www.musicdsp.org/showArchiveComment.php?ArchiveID=236 ? */114m = sample - (h + l);115116/* Scale, Combine and store */117118l *= es->lg;119m *= es->mg;120h *= es->hg;121122/* Shuffle history buffer */123124es->sdm3 = es->sdm2;125es->sdm2 = es->sdm1;126es->sdm1 = sample;127128/* Return result */129130return (int) (l + m + h);131}132133134