{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# 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 LLVM.Extra.Representation as Rep
import qualified LLVM.Extra.Vector as Vector
import qualified Synthesizer.LLVM.Parameter as Param

import qualified Synthesizer.LLVM.Simple.Value as Value

import qualified LLVM.Core as LLVM
import LLVM.Core
   (Value, Vector, IsArithmetic, IsFloating, IsConst, IsSized, IsFirstClass, )

import qualified Synthesizer.LLVM.Frame.Stereo as Stereo

import qualified Data.TypeLevel.Num      as TypeNum
import qualified Data.TypeLevel.Num.Sets as TypeSet

import Foreign.Storable (Storable, )

import qualified Algebra.Field as Field
import qualified Algebra.Module as Module
import qualified Algebra.Ring as Ring


{- |
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 C parameter a b | parameter a -> b, parameter b -> a where
   process :: CausalP.T p (parameter, a) b


processCtrlRate ::
   (C parameter a b,
    Rep.Memory parameter struct,
    IsSized struct ss,
    Ring.C r, IsFloating r, Storable r, IsConst r,
    LLVM.MakeValueTuple r (Value r),
    LLVM.CmpRet r Bool,
    IsSized r rs) =>
   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
   (Ring.C a, Module.C (Value.T a) (Value.T v),
    IsFirstClass a, IsSized a as, IsConst a, IsArithmetic a,
    IsFirstClass v, IsSized v vs, IsConst v, IsArithmetic v) =>
      C (Filt1.Parameter (Value a))
        (Value v) (Filt1.Result (Value v)) where
   process = Filt1.causalP

instance
   (Ring.C a, Module.C (Value.T a) (Value.T v),
    IsFirstClass a, IsSized a as, IsConst a, IsArithmetic a,
    IsFirstClass v, IsSized v vs, IsConst v, IsArithmetic v) =>
      C (Filt2.Parameter (Value a))
        (Value v) (Value v) where
   process = Filt2.causalP

instance
   (Field.C a, IsConst a, Vector.Arithmetic a,
    IsSized (Vector TypeNum.D4 a) as) =>
      C (Filt2P.Parameter a)
        (Value a) (Value a) where
   process = Filt2P.causalP

instance
   (Ring.C a, Module.C (Value.T a) (Value.T v),
    IsFirstClass a, IsSized a as, IsConst a, IsArithmetic a,
    IsFirstClass v, IsSized v vs, IsConst v, IsArithmetic v,
    TypeSet.Nat n,
    TypeNum.Mul n LLVM.UnknownSize paramSize, TypeSet.Pos paramSize) =>
      C (Cascade.ParameterValue n a)
        (Value v) (Value v) where
   process = Cascade.causalP


instance
   (Field.C a, Module.C (Value.T a) (Value.T v),
    IsFirstClass a, IsSized a as, IsConst a, IsArithmetic a,
    IsFirstClass v, IsSized v vs, IsConst v, IsArithmetic v) =>
      C (Allpass.Parameter (Value a))
        (Value v) (Value v) where
   process = Allpass.causalP

instance
   (Field.C a, Module.C (Value.T a) (Value.T v),
    IsFirstClass a, IsSized a as, IsConst a, IsArithmetic a,
    IsFirstClass v, IsSized v vs, IsConst v, IsArithmetic v,
    TypeNum.Nat n) =>
      C (Allpass.CascadeParameter n (Value a))
        (Value v) (Value v) where
   process = Allpass.cascadeP


instance
   (Module.C a v, Module.C (Value.T a) (Value.T v),
    IsFirstClass a, IsSized a as, IsConst a, IsArithmetic a,
    IsFirstClass v, IsSized v vs, IsConst v, IsArithmetic v,
    LLVM.MakeValueTuple a (Value a),
    LLVM.MakeValueTuple v (Value v),
    Storable v,
    TypeSet.Nat n) =>
      C (Moog.Parameter n (Value a))
        (Value v) (Value v) where
   process = Moog.causalP


instance
   (Field.C a, Module.C (Value.T a) (Value.T v),
    IsFirstClass a, IsSized a as, IsConst a, IsArithmetic a,
    IsFirstClass v, IsSized v vs, IsConst v, IsArithmetic v) =>
      C (UniFilter.Parameter (Value a))
        (Value v) (UniFilter.Result (Value v)) where
   process = UniFilter.causalP

instance
   (IsFirstClass a, IsSized a sa, IsConst a, IsFloating a) =>
      C (ComplexFilt.Parameter (Value a))
        (Stereo.T (Value a)) (Stereo.T (Value a)) where
   process = ComplexFilt.causalP

instance
   (IsConst a, Vector.Arithmetic a,
    IsSized (Vector TypeNum.D4 a) as) =>
      C (ComplexFiltPack.Parameter a)
        (Stereo.T (Value a)) (Stereo.T (Value a)) where
   process = ComplexFiltPack.causalP