module Csound.Typed.Plugins.Korg35(
    linKorg_lp, linKorg_hp, korg_lp, korg_hp
) where

import Csound.Dynamic

import Csound.Typed.Types
import Csound.Typed.GlobalState
import qualified Csound.Typed.GlobalState.Elements as E(korg35Plugin)

-- | Linear korg 35 low pass filter (12 dB).
--
-- > linDiode centerFrequency resonance asig
--
-- resonance ranges in the interval [0, 1] and higher.
-- self-resonance occurs at 1.
linKorg_lp :: Sig -> Sig -> Sig -> Sig
linKorg_lp :: Sig -> Sig -> Sig -> Sig
linKorg_lp Sig
cfq Sig
res Sig
ain = Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_lpf Sig
ain Sig
cfq (Sig -> Sig
normReson Sig
res) Sig
0 Sig
1

-- | Linear korg 35 high pass filter (6 dB).
--
-- > linDiode centerFrequency resonance asig
--
-- resonance ranges in the interval [0, 1] and higher.
-- self-resonance occurs at 1.
linKorg_hp :: Sig -> Sig -> Sig -> Sig
linKorg_hp :: Sig -> Sig -> Sig -> Sig
linKorg_hp Sig
cfq Sig
res Sig
ain = Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_hpf Sig
ain Sig
cfq (Sig -> Sig
normReson Sig
res) Sig
0 Sig
1


-- | Korg 35 low pass filter (12 dB).
--
-- > diode saturation centerFrequency resonance asig
--
-- resonance ranges in the interval [0, 1] and higher.
-- self-resonance occurs at 1.
--
-- saturation ranges from 1 and higher (typical value: 4)
korg_lp :: Sig -> Sig -> Sig -> Sig -> Sig
korg_lp :: Sig -> Sig -> Sig -> Sig -> Sig
korg_lp Sig
ksaturation Sig
cfq Sig
res Sig
ain = Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_lpf Sig
ain Sig
cfq (Sig -> Sig
normReson Sig
res) Sig
1 Sig
ksaturation

-- | Korg 35 high pass filter (6 dB).
--
-- > diode saturation centerFrequency resonance asig
--
-- resonance ranges in the interval [0, 1] and higher.
-- self-resonance occurs at 1.
--
-- saturation ranges from 1 and higher (typical value: 4)
korg_hp :: Sig -> Sig -> Sig -> Sig -> Sig
korg_hp :: Sig -> Sig -> Sig -> Sig -> Sig
korg_hp Sig
ksaturation Sig
cfq Sig
res Sig
ain = Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_hpf Sig
ain Sig
cfq (Sig -> Sig
normReson Sig
res) Sig
1 Sig
ksaturation

normReson :: Sig -> Sig
normReson :: Sig -> Sig
normReson Sig
res = Sig
res Sig -> Sig -> Sig
forall a. Num a => a -> a -> a
* Sig
10

-------------------------------------------------------------------------------

-- 12db/oct low-pass filter based on Korg 35 module
-- (found in MS-10 and MS-20).
--
-- Based on code by Will Pirkle, presented in:
--
-- http://www.willpirkle.com/Downloads/AN-5Korg35_V3.pdf
--
-- [ARGS]
--
-- ain - audio input
-- acutoff - frequency of cutoff
-- kQ - filter Q [1, 10.0] (k35-lpf will clamp to boundaries)
-- knonlinear - use non-linear processing
-- ksaturation - saturation for tanh distortion
k35_lpf :: Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_lpf :: Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_lpf Sig
ain Sig
acutoff Sig
kQ Sig
knonlinear Sig
ksaturation = GE E -> Sig
forall a. Val a => GE E -> a
fromGE (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ do
    UdoPlugin -> GE ()
addUdoPlugin UdoPlugin
E.korg35Plugin
    E -> E -> E -> E -> E -> E
f (E -> E -> E -> E -> E -> E) -> GE E -> GE (E -> E -> E -> E -> E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
ain GE (E -> E -> E -> E -> E) -> GE E -> GE (E -> E -> E -> E)
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
acutoff GE (E -> E -> E -> E) -> GE E -> GE (E -> E -> E)
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
kQ GE (E -> E -> E) -> GE E -> GE (E -> E)
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
knonlinear GE (E -> E) -> GE E -> GE E
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
ksaturation
    where f :: E -> E -> E -> E -> E -> E
f E
ain' E
acutoff' E
kQ' E
knonlinear' E
ksaturation' = Name -> Spec1 -> [E] -> E
opcs Name
"k35_lpf" [(Rate
Ar, [Rate
Ar, Rate
Xr, Rate
Kr, Rate
Kr, Rate
Kr])] [E
ain', E
acutoff', E
kQ', E
knonlinear', E
ksaturation']


-- 6db/oct high-pass filter based on Korg 35 module
-- (found in MS-10 and MS-20).
--
-- Based on code by Will Pirkle, presented in:
--
-- http://www.willpirkle.com/Downloads/AN-7Korg35HPF_V2.pdf
--
-- [ARGS]
--
-- ain - audio input
-- acutoff - frequency of cutoff
-- kQ - filter Q [1, 10.0] (k35_hpf will clamp to boundaries)
-- knonlinear - use non-linear processing
-- ksaturation - saturation for tanh distortion
k35_hpf :: Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_hpf :: Sig -> Sig -> Sig -> Sig -> Sig -> Sig
k35_hpf Sig
ain Sig
acutoff Sig
kQ Sig
knonlinear Sig
ksaturation = GE E -> Sig
forall a. Val a => GE E -> a
fromGE (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ do
    UdoPlugin -> GE ()
addUdoPlugin UdoPlugin
E.korg35Plugin
    E -> E -> E -> E -> E -> E
f (E -> E -> E -> E -> E -> E) -> GE E -> GE (E -> E -> E -> E -> E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
ain GE (E -> E -> E -> E -> E) -> GE E -> GE (E -> E -> E -> E)
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
acutoff GE (E -> E -> E -> E) -> GE E -> GE (E -> E -> E)
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
kQ GE (E -> E -> E) -> GE E -> GE (E -> E)
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
knonlinear GE (E -> E) -> GE E -> GE E
forall a b. GE (a -> b) -> GE a -> GE b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
forall a. Val a => a -> GE E
toGE Sig
ksaturation
    where f :: E -> E -> E -> E -> E -> E
f E
ain' E
acutoff' E
kQ' E
knonlinear' E
ksaturation' = Name -> Spec1 -> [E] -> E
opcs Name
"k35_hpf" [(Rate
Ar, [Rate
Ar, Rate
Xr, Rate
Kr, Rate
Kr, Rate
Kr])] [E
ain', E
acutoff', E
kQ', E
knonlinear', E
ksaturation']