synthesizer-llvm-0.7: src/Synthesizer/LLVM/CausalParameterized/Controlled.hs
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# 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.CausalParameterized.Controlled 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.CausalParameterized.Process as CausalP
import qualified Synthesizer.LLVM.Parameterized.Signal as SigP
import qualified Synthesizer.LLVM.Parameter as Param
import qualified LLVM.Extra.Memory as Memory
import qualified LLVM.Extra.ScalarOrVector as SoV
import qualified LLVM.Extra.Vector as Vector
import qualified LLVM.Extra.Arithmetic as A
import LLVM.Extra.Class (MakeValueTuple, ValueTuple, )
import qualified LLVM.Core as LLVM
import LLVM.Core (Value, IsFloating, IsConst, IsSized, )
import qualified Synthesizer.LLVM.Frame.Stereo as Stereo
import qualified Type.Data.Num.Decimal as TypeNum
import Type.Data.Num.Decimal.Number ((:*:), )
import Foreign.Storable (Storable, )
{- |
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 :: CausalP.T p (parameter, a) b
processCtrlRate ::
(C parameter a b,
Memory.C parameter,
Memory.FirstClass r, IsSized (Memory.Stored r),
IsFloating r, Storable r, SoV.IntegerConstant r,
MakeValueTuple r, ValueTuple r ~ (Value r),
LLVM.CmpRet r, LLVM.CmpResult r ~ Bool) =>
Param.T p r ->
(Param.T p r -> SigP.T p parameter) ->
CausalP.T p a b
processCtrlRate reduct ctrlGen =
CausalP.applyFst process
(SigP.interpolateConstant reduct (ctrlGen reduct))
{-
Instances for the particular filters shall are defined here
in order to avoid orphan instances.
-}
instance
(a ~ A.Scalar v, A.PseudoModule v, A.IntegerConstant a,
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
(Vector.Arithmetic a, SoV.RationalConstant a,
Memory.C (Value (Filt2P.State a)) {-,
Memory.FirstClass a am, IsSized (Vector TypeNum.D4 a) as,
IsSized am ams, LLVM.IsPrimitive am -}) =>
C (Filt2P.Parameter a)
(Value a) (Value a) where
type Input (Filt2P.Parameter a) (Value a) = Value a
type Output (Filt2P.Parameter a) (Value a) = Value a
process = Filt2P.causal
instance
(a ~ SoV.Scalar v, SoV.PseudoModule v, SoV.IntegerConstant a,
Memory.FirstClass a, IsSized a, IsSized (Memory.Stored a),
Memory.FirstClass v, IsSized v, IsSized (Memory.Stored v),
TypeNum.Natural n,
TypeNum.Positive (n :*: LLVM.UnknownSize)) =>
C (Cascade.ParameterValue n a)
(Value v) (Value v) where
type Input (Cascade.ParameterValue n a) (Value v) = Value v
type Output (Cascade.ParameterValue n a) (Value v) = Value v
process = Cascade.causal
instance
(a ~ A.Scalar v, A.PseudoModule v, A.RationalConstant a,
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
(a ~ A.Scalar v, A.PseudoModule v, A.RationalConstant a,
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
(A.PseudoModule v, A.Scalar v ~ a, A.IntegerConstant a,
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 ~ A.Scalar v, A.PseudoModule v, 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
(Vector.Arithmetic a, IsConst a,
Memory.C (Value (Filt2P.State a))) =>
{-
(Memory.FirstClass a am, Vector.Arithmetic a, LLVM.IsPrimitive am,
IsSized am ams,
IsSized (Vector TypeNum.D4 a) as) =>
-}
C (ComplexFiltPack.Parameter a)
(Stereo.T (Value a)) (Stereo.T (Value a)) where
type Input (ComplexFiltPack.Parameter a) (Stereo.T (Value a)) = Stereo.T (Value a)
type Output (ComplexFiltPack.Parameter a) (Stereo.T (Value a)) = Stereo.T (Value a)
process = ComplexFiltPack.causal