// Copyright 2006, Thomas Scott Stillwell
// All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are permitted 
//provided that the following conditions are met:
//
//Redistributions of source code must retain the above copyright notice, this list of conditions 
//and the following disclaimer. 
//
//Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
//and the following disclaimer in the documentation and/or other materials provided with the distribution. 
//
//The name of Thomas Scott Stillwell may not be used to endorse or 
//promote products derived from this software without specific prior written permission. 
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
//IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
//BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
//STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
//THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

desc:RBJ 7-Band Graphic EQ
desc:RBJ 7-Band Graphic EQ [Stillwell]
//tags: equalizer filter
//author: Stillwell

slider1:10<10,120.0.1>HPF Freq
slider2:0<-15,15,0.1>100 Hz
slider3:0<-15,15,0.1>200 Hz
slider4:0<-15,15,0.1>400 Hz
slider5:0<-15,15,0.1>800 Hz
slider6:0<-15,15,0.1>2.5 kHz
slider7:0<-15,15,0.1>6 kHz
slider8:0<-15,15,0.1>12 kHz

in_pin:left input
in_pin:right input
out_pin:left output
out_pin:right output

@init
  gain0 = 0;
  freq0 = 20;
  q0 = 1;
  a0 = 1;
  w00 = 2 * $pi * freq0/srate;
  cosw00 = cos(w00);
  sinw00 = sin(w00);
  alpha0 = sinw00 / (2 * q0);

  b00 = (1 + cosw00) / 2;
  b10 = -(1 + cosw00);
  b20 = (1 + cosw00) / 2;
  a00 = 1 + alpha0;
  a10 = -2 * cosw00;
  a20 = 1 - alpha0;
  b00 /= a00;
  b10 /= a00;
  b20 /= a00;
  a10 /= a00;
  a20 /= a00;

  gain1 = 0;
  freq1 = 100;
  q1 = 0.8;
  a1 = 10^(gain1/40);
  w01 = 2 * $pi * freq1/srate;
  cosw01 = cos(w01);
  sinw01 = sin(w01);
  alpha1 = sinw01 / (2 * q1);

  b01 = 1 + alpha1 * a1;
  b11 = -2 * cosw01;
  b21 = 1 - alpha1 * a1;
  a01 = 1 + alpha1 / a1;
  a11 = -2 * cosw01;
  a21 = 1 - alpha1 / a1;
  b01 /= a01;
  b11 /= a01;
  b21 /= a01;
  a11 /= a01;
  a21 /= a01;

  gain2 = 0;
  freq2 = 200;
  q2 = 0.8;
  a2 = 10^(gain2/40);
  w02 = 2 * $pi * freq2/srate;
  cosw02 = cos(w02);
  sinw02 = sin(w02);
  alpha2 = sinw02 / (2 * q2);

  b02 = 1 + alpha2 * a2;
  b12 = -2 * cosw02;
  b22 = 1 - alpha2 * a2;
  a02 = 1 + alpha2 / a2;
  a12 = -2 * cosw02;
  a22 = 1 - alpha2 / a2;
  b02 /= a02;
  b12 /= a02;
  b22 /= a02;
  a12 /= a02;
  a22 /= a02;

  gain3 = 0;
  freq3 = 400;
  q3 = 0.8;
  a3 = 10^(gain3/40);
  w03 = 2 * $pi * freq3/srate;
  cosw03 = cos(w03);
  sinw03 = sin(w03);
  alpha3 = sinw03 / (2 * q3);

  b03 = 1 + alpha3 * a3;
  b13 = -2 * cosw03;
  b23 = 1 - alpha3 * a3;
  a03 = 1 + alpha3 / a3;
  a13 = -2 * cosw03;
  a23 = 1 - alpha3 / a3;
  b03 /= a03;
  b13 /= a03;
  b23 /= a03;
  a13 /= a03;
  a23 /= a03;

  gain4 = 0;
  freq4 = 800;
  q4 = 0.8;
  a4 = 10^(gain4/40);
  w04 = 2 * $pi * freq4/srate;
  cosw04 = cos(w04);
  sinw04 = sin(w04);
  alpha4 = sinw04 / (2 * q4);

  b04 = 1 + alpha4 * a4;
  b14 = -2 * cosw04;
  b24 = 1 - alpha4 * a4;
  a04 = 1 + alpha4 / a4;
  a14 = -2 * cosw04;
  a24 = 1 - alpha4 / a4;
  b04 /= a04;
  b14 /= a04;
  b24 /= a04;
  a14 /= a04;
  a24 /= a04;

  gain5 = 0;
  freq5 = 2500;
  q5 = 0.8;
  a5 = 10^(gain5/40);
  w05 = 2 * $pi * freq5/srate;
  cosw05 = cos(w05);
  sinw05 = sin(w05);
  alpha5 = sinw05 / (2 * q5);

  b05 = 1 + alpha5 * a5;
  b15 = -2 * cosw05;
  b25 = 1 - alpha5 * a5;
  a05 = 1 + alpha5 / a5;
  a15 = -2 * cosw05;
  a25 = 1 - alpha5 / a5;
  b05 /= a05;
  b15 /= a05;
  b25 /= a05;
  a15 /= a05;
  a25 /= a05;

  gain6 = 0;
  freq6 = 6000;
  q6 = 0.8;
  a6 = 10^(gain6/40);
  w06 = 2 * $pi * freq6/srate;
  cosw06 = cos(w06);
  sinw06 = sin(w06);
  alpha6 = sinw06 / (2 * q6);

  b06 = 1 + alpha6 * a6;
  b16 = -2 * cosw06;
  b26 = 1 - alpha6 * a6;
  a06 = 1 + alpha6 / a6;
  a16 = -2 * cosw06;
  a26 = 1 - alpha6 / a6;
  b06 /= a06;
  b16 /= a06;
  b26 /= a06;
  a16 /= a06;
  a26 /= a06;

  gain7 = 0;
  freq7 = 12000;
  q7 = 0.8;
  a7 = 10^(gain7/40);
  w07 = 2 * $pi * freq7/srate;
  cosw07 = cos(w07);
  sinw07 = sin(w07);
  alpha7 = sinw07 / (2 * q7);

  b07 = 1 + alpha7 * a7;
  b17 = -2 * cosw07;
  b27 = 1 - alpha7 * a7;
  a07 = 1 + alpha7 / a7;
  a17 = -2 * cosw07;
  a27 = 1 - alpha7 / a7;
  b07 /= a07;
  b17 /= a07;
  b27 /= a07;
  a17 /= a07;
  a27 /= a07;

@slider
  freq0 = slider1;
  gain1 = slider2;
  gain2 = slider3;
  gain3 = slider4;
  gain4 = slider5;
  gain5 = slider6;
  gain6 = slider7;
  gain7 = slider8;

  gain0 = 0;
  q0 = 1;
  a0 = 1;
  w00 = 2 * $pi * freq0/srate;
  cosw00 = cos(w00);
  sinw00 = sin(w00);
  alpha0 = sinw00 / (2 * q0);

  b00 = (1 + cosw00) / 2;
  b10 = -(1 + cosw00);
  b20 = (1 + cosw00) / 2;
  a00 = 1 + alpha0;
  a10 = -2 * cosw00;
  a20 = 1 - alpha0;
  b00 /= a00;
  b10 /= a00;
  b20 /= a00;
  a10 /= a00;
  a20 /= a00;

  freq1 = 100;
  q1 = 0.8;
  a1 = 10^(gain1/40);
  w01 = 2 * $pi * freq1/srate;
  cosw01 = cos(w01);
  sinw01 = sin(w01);
  alpha1 = sinw01 / (2 * q1);

  b01 = 1 + alpha1 * a1;
  b11 = -2 * cosw01;
  b21 = 1 - alpha1 * a1;
  a01 = 1 + alpha1 / a1;
  a11 = -2 * cosw01;
  a21 = 1 - alpha1 / a1;
  b01 /= a01;
  b11 /= a01;
  b21 /= a01;
  a11 /= a01;
  a21 /= a01;

  freq2 = 200;
  q2 = 0.8;
  a2 = 10^(gain2/40);
  w02 = 2 * $pi * freq2/srate;
  cosw02 = cos(w02);
  sinw02 = sin(w02);
  alpha2 = sinw02 / (2 * q2);

  b02 = 1 + alpha2 * a2;
  b12 = -2 * cosw02;
  b22 = 1 - alpha2 * a2;
  a02 = 1 + alpha2 / a2;
  a12 = -2 * cosw02;
  a22 = 1 - alpha2 / a2;
  b02 /= a02;
  b12 /= a02;
  b22 /= a02;
  a12 /= a02;
  a22 /= a02;

  freq3 = 400;
  q3 = 0.8;
  a3 = 10^(gain3/40);
  w03 = 2 * $pi * freq3/srate;
  cosw03 = cos(w03);
  sinw03 = sin(w03);
  alpha3 = sinw03 / (2 * q3);

  b03 = 1 + alpha3 * a3;
  b13 = -2 * cosw03;
  b23 = 1 - alpha3 * a3;
  a03 = 1 + alpha3 / a3;
  a13 = -2 * cosw03;
  a23 = 1 - alpha3 / a3;
  b03 /= a03;
  b13 /= a03;
  b23 /= a03;
  a13 /= a03;
  a23 /= a03;

  freq4 = 800;
  q4 = 0.8;
  a4 = 10^(gain4/40);
  w04 = 2 * $pi * freq4/srate;
  cosw04 = cos(w04);
  sinw04 = sin(w04);
  alpha4 = sinw04 / (2 * q4);

  b04 = 1 + alpha4 * a4;
  b14 = -2 * cosw04;
  b24 = 1 - alpha4 * a4;
  a04 = 1 + alpha4 / a4;
  a14 = -2 * cosw04;
  a24 = 1 - alpha4 / a4;
  b04 /= a04;
  b14 /= a04;
  b24 /= a04;
  a14 /= a04;
  a24 /= a04;

  freq5 = 2500;
  q5 = 0.8;
  a5 = 10^(gain5/40);
  w05 = 2 * $pi * freq5/srate;
  cosw05 = cos(w05);
  sinw05 = sin(w05);
  alpha5 = sinw05 / (2 * q5);

  b05 = 1 + alpha5 * a5;
  b15 = -2 * cosw05;
  b25 = 1 - alpha5 * a5;
  a05 = 1 + alpha5 / a5;
  a15 = -2 * cosw05;
  a25 = 1 - alpha5 / a5;
  b05 /= a05;
  b15 /= a05;
  b25 /= a05;
  a15 /= a05;
  a25 /= a05;

  freq6 = 6000;
  q6 = 0.8;
  a6 = 10^(gain6/40);
  w06 = 2 * $pi * freq6/srate;
  cosw06 = cos(w06);
  sinw06 = sin(w06);
  alpha6 = sinw06 / (2 * q6);

  b06 = 1 + alpha6 * a6;
  b16 = -2 * cosw06;
  b26 = 1 - alpha6 * a6;
  a06 = 1 + alpha6 / a6;
  a16 = -2 * cosw06;
  a26 = 1 - alpha6 / a6;
  b06 /= a06;
  b16 /= a06;
  b26 /= a06;
  a16 /= a06;
  a26 /= a06;

  freq7 = 12000;
  q7 = 0.8;
  a7 = 10^(gain7/40);
  w07 = 2 * $pi * freq7/srate;
  cosw07 = cos(w07);
  sinw07 = sin(w07);
  alpha7 = sinw07 / (2 * q7);

  b07 = 1 + alpha7 * a7;
  b17 = -2 * cosw07;
  b27 = 1 - alpha7 * a7;
  a07 = 1 + alpha7 / a7;
  a17 = -2 * cosw07;
  a27 = 1 - alpha7 / a7;
  b07 /= a07;
  b17 /= a07;
  b27 /= a07;
  a17 /= a07;
  a27 /= a07;

@sample

  freq0 > 10 ? (
  ospl0 = spl0;
  spl0 = b00 * spl0 + b10 * xl10 + b20 * xl20 - a10 * yl10 - a20 * yl20;
  xl20 = xl10;
  xl10 = ospl0;
  yl20 = yl10;
  yl10 = spl0 ;

  ospl1 = spl1;
  spl1 = b00 * spl1 + b10 * xr10 + b20 * xr20 - a10 * yr10 - a20 * yr20;
  xr20 = xr10;
  xr10 = ospl1;
  yr20 = yr10;
  yr10 = spl1 ;
  );

  gain1 != 0 ? (
  ospl0 = spl0;
  spl0 = b01 * spl0 + b11 * xl11 + b21 * xl21 - a11 * yl11 - a21 * yl21;
  xl21 = xl11;
  xl11 = ospl0;
  yl21 = yl11;
  yl11 = spl0;

  ospl1 = spl1;
  spl1 = b01 * spl1 + b11 * xr11 + b21 * xr21 - a11 * yr11 - a21 * yr21;
  xr21 = xr11;
  xr11 = ospl1;
  yr21 = yr11;
  yr11 = spl1;
  );

  gain2 != 0 ? (
  ospl0 = spl0;
  spl0 = b02 * spl0 + b12 * xl12 + b22 * xl22 - a12 * yl12 - a22 * yl22;
  xl22 = xl12;
  xl12 = ospl0;
  yl22 = yl12;
  yl12 = spl0;

  ospl1 = spl1;
  spl1 = b02 * spl1 + b12 * xr12 + b22 * xr22 - a12 * yr12 - a22 * yr22;
  xr22 = xr12;
  xr12 = ospl1;
  yr22 = yr12;
  yr12 = spl1;
  );

  gain3 != 0 ? (
  ospl0 = spl0;
  spl0 = b03 * spl0 + b13 * xl13 + b23 * xl23 - a13 * yl13 - a23 * yl23;
  xl23 = xl13;
  xl13 = ospl0;
  yl23 = yl13;
  yl13 = spl0;

  ospl1 = spl1;
  spl1 = b03 * spl1 + b13 * xr13 + b23 * xr23 - a13 * yr13 - a23 * yr23;
  xr23 = xr13;
  xr13 = ospl1;
  yr23 = yr13;
  yr13 = spl1;
  );

  gain4 != 0 ? (
  ospl0 = spl0;
  spl0 = b04 * spl0 + b14 * xl14 + b24 * xl24 - a14 * yl14 - a24 * yl24;
  xl24 = xl14;
  xl14 = ospl0;
  yl24 = yl14;
  yl14 = spl0;

  ospl1 = spl1;
  spl1 = b04 * spl1 + b14 * xr14 + b24 * xr24 - a14 * yr14 - a24 * yr24;
  xr24 = xr14;
  xr14 = ospl1;
  yr24 = yr14;
  yr14 = spl1;
  );

  gain5 != 0 ? (
  ospl0 = spl0;
  spl0 = b05 * spl0 + b15 * xl15 + b25 * xl25 - a15 * yl15 - a25 * yl25;
  xl25 = xl15;
  xl15 = ospl0;
  yl25 = yl15;
  yl15 = spl0;

  ospl1 = spl1;
  spl1 = b05 * spl1 + b15 * xr15 + b25 * xr25 - a15 * yr15 - a25 * yr25;
  xr25 = xr15;
  xr15 = ospl1;
  yr25 = yr15;
  yr15 = spl1;
  );

  gain6 != 0 ? (
  ospl0 = spl0;
  spl0 = b06 * spl0 + b16 * xl16 + b26 * xl26 - a16 * yl16 - a26 * yl26;
  xl26 = xl16;
  xl16 = ospl0;
  yl26 = yl16;
  yl16 = spl0;

  ospl1 = spl1;
  spl1 = b06 * spl1 + b16 * xr16 + b26 * xr26 - a16 * yr16 - a26 * yr26;
  xr26 = xr16;
  xr16 = ospl1;
  yr26 = yr16;
  yr16 = spl1;
  );

  gain7 != 0 ? (
  ospl0 = spl0;
  spl0 = b07 * spl0 + b17 * xl17 + b27 * xl27 - a17 * yl17 - a27 * yl27;
  xl27 = xl17;
  xl17 = ospl0;
  yl27 = yl17;
  yl17 = spl0;

  ospl1 = spl1;
  spl1 = b07 * spl1 + b17 * xr17 + b27 * xr27 - a17 * yr17 - a27 * yr27;
  xr27 = xr17;
  xr17 = ospl1;
  yr27 = yr17;
  yr17 = spl1;
  );
