{-# OPTIONS -fglasgow-exts #-}
{- |
Copyright   :  (c) Henning Thielemann 2008
License     :  GPL

Maintainer  :  synthesizer@henning-thielemann.de
Stability   :  provisional
Portability :  requires multi-parameter type classes

Class that allows unified handling of
@SigS.T@ and @Sig.D Dim.Scalar@
which is often used for control curves.
module Synthesizer.Dimensional.Abstraction.Flat where

import qualified Synthesizer.Dimensional.RatePhantom as RP
import qualified Synthesizer.Dimensional.Straight.Signal as SigS
import qualified Synthesizer.Dimensional.Amplitude.Signal as SigA

import qualified Synthesizer.State.Signal as Sig

import qualified Number.DimensionTerm        as DN
import qualified Algebra.DimensionTerm       as Dim

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

-- import Number.DimensionTerm ((&/&))

-- import NumericPrelude
import PreludeBase
-- import Prelude ()

toSamples :: C sig y => RP.T s sig y -> Sig.T y
toSamples = unwrappedToSamples . RP.toSignal

class C sig y where
   unwrappedToSamples :: sig y -> Sig.T y

instance C Sig.T y where
   unwrappedToSamples = id

instance C sig y => C (SigS.T sig) y where
   unwrappedToSamples = unwrappedToSamples . SigS.samples

instance (Dim.IsScalar scalar, Module.C y yv) => C (SigA.T scalar y) yv where
   toSamples =
      SigA.vectorSamples (DN.toNumber . DN.rewriteDimension Dim.toScalar)

instance (C flat y, Dim.IsScalar scalar, Ring.C y) =>
             C (SigA.T scalar y flat) y where
   unwrappedToSamples =
      SigA.scalarSamples (DN.toNumber . DN.rewriteDimension Dim.toScalar) .
      (\x ->
            (SigA.privateAmplitude x)
            (unwrappedToSamples (SigA.signal x)))