-- | BEQ filter coefficient calculations, results are (a0,a1,a2,b0,b1).
module Sound.SC3.Common.Math.Filter.BEQ where

-- | Calculate coefficients for bi-quad low pass filter.
bLowPassCoef :: Floating a => a -> a -> a -> (a,a,a,a,a)
bLowPassCoef :: a -> a -> a -> (a, a, a, a, a)
bLowPassCoef a
sr a
freq a
rq =
    let w0 :: a
w0 = a
forall a. Floating a => a
pi a -> a -> a
forall a. Num a => a -> a -> a
* a
2 a -> a -> a
forall a. Num a => a -> a -> a
* a
freq a -> a -> a
forall a. Num a => a -> a -> a
* (a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
sr)
        cos_w0 :: a
cos_w0 = a -> a
forall a. Floating a => a -> a
cos a
w0
        i :: a
i = a
1 a -> a -> a
forall a. Num a => a -> a -> a
- a
cos_w0
        alpha :: a
alpha = a -> a
forall a. Floating a => a -> a
sin a
w0 a -> a -> a
forall a. Num a => a -> a -> a
* a
0.5 a -> a -> a
forall a. Num a => a -> a -> a
* a
rq
        b0rz :: a
b0rz = a -> a
forall a. Fractional a => a -> a
recip (a
1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
alpha)
        a0 :: a
a0 = a
i a -> a -> a
forall a. Num a => a -> a -> a
* a
0.5 a -> a -> a
forall a. Num a => a -> a -> a
* a
b0rz
        a1 :: a
a1 = a
i a -> a -> a
forall a. Num a => a -> a -> a
* a
b0rz
        b1 :: a
b1 = a
cos_w0 a -> a -> a
forall a. Num a => a -> a -> a
* a
2 a -> a -> a
forall a. Num a => a -> a -> a
* a
b0rz
        b2 :: a
b2 = (a
1 a -> a -> a
forall a. Num a => a -> a -> a
- a
alpha) a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Num a => a -> a
negate a
b0rz
    in (a
a0,a
a1,a
a0,a
b1,a
b2)

-- | Calculate coefficients for bi-quad high pass filter.
bHiPassCoef :: Floating t => t -> t -> t -> (t, t, t, t, t)
bHiPassCoef :: t -> t -> t -> (t, t, t, t, t)
bHiPassCoef t
sr t
freq t
rq =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      cos_w0 :: t
cos_w0 = t -> t
forall a. Floating a => a -> a
cos t
w0
      i :: t
i = t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
cos_w0
      alpha :: t
alpha = t -> t
forall a. Floating a => a -> a
sin t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
rq
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      a0 :: t
a0 = t
i t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a1 :: t
a1 = t -> t
forall a. Num a => a -> a
negate t
i t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
cos_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
a1, t
a0, t
b1, t
b2)

-- | Calculate coefficients for bi-quad all pass filter.
bAllPassCoef :: Floating t => t -> t -> t -> (t, t, t, t, t)
bAllPassCoef :: t -> t -> t -> (t, t, t, t, t)
bAllPassCoef t
sr t
freq t
rq =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      alpha :: t
alpha = t -> t
forall a. Floating a => a -> a
sin t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
rq
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      a0 :: t
a0 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
  in (t
a0,t -> t
forall a. Num a => a -> a
negate t
b1, t
1.0, t
b1,t -> t
forall a. Num a => a -> a
negate t
a0)

-- | Calculate coefficients for bi-quad band pass filter.
bBandPassCoef :: Floating t => t -> t -> t -> (t, t, t, t, t)
bBandPassCoef :: t -> t -> t -> (t, t, t, t, t)
bBandPassCoef t
sr t
freq t
bw =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sinh (t
0.34657359027997 t -> t -> t
forall a. Num a => a -> a -> a
* t
bw t -> t -> t
forall a. Num a => a -> a -> a
* t
w0 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sin_w0)
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      a0 :: t
a0 = t
alpha t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
0.0, t -> t
forall a. Num a => a -> a
negate t
a0, t
b1, t
b2)

-- | Calculate coefficients for bi-quad stop band filter.
bBandStopCoef :: Floating t => t -> t -> t -> (t, t, t, t, t)
bBandStopCoef :: t -> t -> t -> (t, t, t, t, t)
bBandStopCoef t
sr t
freq t
bw =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sinh (t
0.34657359027997 t -> t -> t
forall a. Num a => a -> a -> a
* t
bw t -> t -> t
forall a. Num a => a -> a -> a
* t
w0 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sin_w0)
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
b0rz, t -> t
forall a. Num a => a -> a
negate t
b1, t
b0rz, t
b1, t
b2)

-- | Calculate coefficients for bi-quad peaking EQ filter.
bPeakEQCoef :: Floating t => t -> t -> t -> t -> (t, t, t, t, t)
bPeakEQCoef :: t -> t -> t -> t -> (t, t, t, t, t)
bPeakEQCoef t
sr t
freq t
rq t
db =
  let a :: t
a = t
10 t -> t -> t
forall a. Floating a => a -> a -> a
** (t
db t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
40)
      w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      alpha :: t
alpha = t -> t
forall a. Floating a => a -> a
sin t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
rq
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ (t
alpha t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
a))
      a0 :: t
a0 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ (t
alpha t -> t -> t
forall a. Num a => a -> a -> a
* t
a)) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a2 :: t
a2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- (t
alpha t -> t -> t
forall a. Num a => a -> a -> a
* t
a)) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- (t
alpha t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
a)) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t -> t
forall a. Num a => a -> a
negate t
b1, t
a2, t
b1, t
b2)

-- | Calculate coefficients for bi-quad low shelf filter.
bLowShelfCoef :: Floating t => t -> t -> t -> t -> (t, t, t, t, t)
bLowShelfCoef :: t -> t -> t -> t -> (t, t, t, t, t)
bLowShelfCoef t
sr t
freq t
rs t
db =
  let a :: t
a = t
10 t -> t -> t
forall a. Floating a => a -> a -> a
** (t
db t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
40)
      w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      cos_w0 :: t
cos_w0 = t -> t
forall a. Floating a => a -> a
cos t
w0
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t -> t
forall a. Fractional a => a -> a
recip t
a) t -> t -> t
forall a. Num a => a -> a -> a
* (t
rs t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
2.0)
      i :: t
i = (t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      j :: t
j = (t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      k :: t
k = t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt t
a t -> t -> t
forall a. Num a => a -> a -> a
* t
alpha
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k)
      a0 :: t
a0 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a1 :: t
a1 = t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a2 :: t
a2 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
a1, t
a2, t
b1, t
b2)

-- | Calculate coefficients for bi-quad high shelf filter.
bHiShelfCoef :: Floating t => t -> t -> t -> t -> (t, t, t, t, t)
bHiShelfCoef :: t -> t -> t -> t -> (t, t, t, t, t)
bHiShelfCoef t
sr t
freq t
rs t
db =
  let a :: t
a = t
10 t -> t -> t
forall a. Floating a => a -> a -> a
** (t
db t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
40)
      w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      cos_w0 :: t
cos_w0 = t -> t
forall a. Floating a => a -> a
cos t
w0
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t -> t
forall a. Fractional a => a -> a
recip t
a) t -> t -> t
forall a. Num a => a -> a -> a
* (t
rs t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
2.0)
      i :: t
i = (t
at -> t -> t
forall a. Num a => a -> a -> a
+t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      j :: t
j = (t
at -> t -> t
forall a. Num a => a -> a -> a
-t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      k :: t
k = t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt t
a t -> t -> t
forall a. Num a => a -> a -> a
* t
alpha
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k)
      a0 :: t
a0 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a1 :: t
a1 = -t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a2 :: t
a2 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = -t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
a1, t
a2, t
b1, t
b2)