synthesizer-llvm-1.2: src/Synthesizer/LLVM/Causal/Controlled.hs
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{- |
This module provides a type class that automatically selects a filter
for a given parameter type.
We choose the dependency this way
because there may be different ways to specify the filter parameters
but there is only one implementation of the filter itself.
-}
module Synthesizer.LLVM.Causal.Controlled (
C(..),
processCtrlRate,
) where
import qualified Synthesizer.LLVM.Filter.ComplexFirstOrderPacked
as ComplexFiltPack
import qualified Synthesizer.LLVM.Filter.ComplexFirstOrder as ComplexFilt
import qualified Synthesizer.LLVM.Filter.Allpass as Allpass
import qualified Synthesizer.LLVM.Filter.FirstOrder as Filt1
import qualified Synthesizer.LLVM.Filter.SecondOrder as Filt2
import qualified Synthesizer.LLVM.Filter.SecondOrderCascade as Cascade
import qualified Synthesizer.LLVM.Filter.SecondOrderPacked as Filt2P
import qualified Synthesizer.LLVM.Filter.Moog as Moog
import qualified Synthesizer.LLVM.Filter.Universal as UniFilter
import qualified Synthesizer.LLVM.Causal.Private as Causal
import qualified Synthesizer.LLVM.Generator.Signal as Sig
import qualified Synthesizer.LLVM.Frame.Stereo as Stereo
import Synthesizer.Causal.Class (($<))
import qualified LLVM.DSL.Expression as Expr
import LLVM.DSL.Expression (Exp)
import qualified LLVM.Extra.Nice.Value.Marshal as Marshal
import qualified LLVM.Extra.Nice.Value as NiceValue
import qualified LLVM.Extra.Nice.Vector as NiceVector
import qualified LLVM.Extra.Memory as Memory
import qualified LLVM.Extra.Arithmetic as A
import qualified LLVM.Core as LLVM
import qualified Type.Data.Num.Decimal as TypeNum
import Type.Data.Num.Decimal.Number ((:*:))
import qualified Algebra.Module as Module
processCtrlRate ::
(C parameter a b, Memory.C parameter) =>
(Marshal.C r, NiceValue.IntegerConstant r,
NiceValue.Additive r, NiceValue.Comparison r) =>
Exp r -> (Exp r -> Sig.T parameter) -> Causal.T a b
processCtrlRate reduct ctrlGen =
process $< Sig.interpolateConstant reduct (ctrlGen reduct)
{- |
A filter parameter type uniquely selects a filter function.
However it does not uniquely determine the input and output type,
since the same filter can run on mono and stereo signals.
-}
class (a ~ Input parameter b, b ~ Output parameter a) => C parameter a b where
type Input parameter b
type Output parameter a
process :: Causal.T (parameter, a) b
{-
Instances for the particular filters shall be defined here
in order to avoid orphan instances.
-}
instance
(Module.C ae ve, Expr.Aggregate ae a, Expr.Aggregate ve v,
Memory.C a, Memory.C v) =>
C (Filt1.Parameter a) v (Filt1.Result v) where
type Input (Filt1.Parameter a) (Filt1.Result v) = v
type Output (Filt1.Parameter a) v = Filt1.Result v
process = Filt1.causal
instance
(a ~ A.Scalar v, A.PseudoModule v, A.RationalConstant a,
Memory.C a, Memory.C v) =>
C (Filt2.Parameter a) v v where
type Input (Filt2.Parameter a) v = v
type Output (Filt2.Parameter a) v = v
process = Filt2.causal
instance
(Marshal.C a, Marshal.Vector TypeNum.D4 a, NiceVector.PseudoRing a) =>
C (Filt2P.Parameter a) (NiceValue.T a) (NiceValue.T a) where
type Input (Filt2P.Parameter a) (NiceValue.T a) = NiceValue.T a
type Output (Filt2P.Parameter a) (NiceValue.T a) = NiceValue.T a
process = Filt2P.causal
instance
(a ~ NiceValue.Scalar v, NiceValue.PseudoModule v,
Marshal.C a, NiceValue.IntegerConstant a, Marshal.C v,
TypeNum.Natural n, TypeNum.Positive (n :*: LLVM.UnknownSize),
inp ~ NiceValue.T v, out ~ NiceValue.T v) =>
C (Cascade.ParameterValue n a) inp out where
type Input (Cascade.ParameterValue n a) out = out
type Output (Cascade.ParameterValue n a) inp = inp
process = Cascade.causal
instance
(Module.C ae ve, Expr.Aggregate ae a, Expr.Aggregate ve v,
Memory.C a, Memory.C v) =>
C (Allpass.Parameter a) v v where
type Input (Allpass.Parameter a) v = v
type Output (Allpass.Parameter a) v = v
process = Allpass.causal
instance
(Module.C ae ve, Expr.Aggregate ae a, Expr.Aggregate ve v,
Memory.C a, Memory.C v, TypeNum.Natural n) =>
C (Allpass.CascadeParameter n a) v v where
type Input (Allpass.CascadeParameter n a) v = v
type Output (Allpass.CascadeParameter n a) v = v
process = Allpass.cascade
instance
(Module.C ae ve, Expr.Aggregate ae a, Expr.Aggregate ve v,
Memory.C v, TypeNum.Natural n) =>
C (Moog.Parameter n a) v v where
type Input (Moog.Parameter n a) v = v
type Output (Moog.Parameter n a) v = v
process = Moog.causal
instance
(A.PseudoModule v, A.Scalar v ~ a, A.RationalConstant a,
Memory.C a, Memory.C v) =>
C (UniFilter.Parameter a) v (UniFilter.Result v) where
type Input (UniFilter.Parameter a) (UniFilter.Result v) = v
type Output (UniFilter.Parameter a) v = UniFilter.Result v
process = UniFilter.causal
instance
(A.PseudoRing a, A.RationalConstant a, Memory.C a) =>
C (ComplexFilt.Parameter a) (Stereo.T a) (Stereo.T a) where
type Input (ComplexFilt.Parameter a) (Stereo.T a) = Stereo.T a
type Output (ComplexFilt.Parameter a) (Stereo.T a) = Stereo.T a
process = ComplexFilt.causal
instance
(Marshal.Vector n a, n ~ TypeNum.D3, NiceVector.PseudoRing a,
inp ~ NiceValue.T a, out ~ NiceValue.T a) =>
C (ComplexFiltPack.ParameterNice a) (Stereo.T inp) (Stereo.T out) where
type Input (ComplexFiltPack.ParameterNice a) (Stereo.T out) = Stereo.T out
type Output (ComplexFiltPack.ParameterNice a) (Stereo.T inp) = Stereo.T inp
process = ComplexFiltPack.causal