{-# LANGUAGE NoImplicitPrelude #-}
{- |
Copyright   :  (c) Henning Thielemann 2008-2009
License     :  GPL

Maintainer  :  synthesizer@henning-thielemann.de
Stability   :  provisional
Portability :  requires multi-parameter type classes
-}
module Synthesizer.Dimensional.Rate.Cut (
   splitAt, take, drop,
   concat, append, ) where

import qualified Synthesizer.Dimensional.Process as Proc
import qualified Synthesizer.Dimensional.Rate as Rate
import qualified Synthesizer.Dimensional.Amplitude as Amp

import qualified Synthesizer.Dimensional.Signal.Private as SigA
import qualified Synthesizer.Generic.Cut as CutG

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

-- import qualified Number.NonNegative     as NonNeg

import qualified Algebra.RealRing      as RealRing
-- import qualified Algebra.Field          as Field

import Data.Monoid (Monoid, mappend, mconcat, )


-- import NumericPrelude.Numeric hiding (negate)
-- import NumericPrelude.Base as P
import Prelude hiding (splitAt, take, drop, concat, )


type Signal s amp sig =
   SigA.T (Rate.Phantom s) amp sig

{- |
To avoid recomputation,
don't use this directly on State signals
but only after buffering.
-}
{-# INLINE splitAt #-}
splitAt :: (CutG.Transform sig, RealRing.C t, Dim.C u) =>
   DN.T u t ->
   Proc.T s u t
      (Signal s amp sig ->
       (Signal s amp sig, Signal s amp sig))
splitAt :: forall sig t u s amp.
(Transform sig, C t, C u) =>
T u t
-> T s
     u
     t
     (Signal s amp sig -> (Signal s amp sig, Signal s amp sig))
splitAt T u t
t' =
   forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall t u s. (C t, C u) => T u t -> T s u t t
Proc.toTimeScalar T u t
t') forall a b. (a -> b) -> a -> b
$
   \t
t Signal s amp sig
x ->
      let (sig
y,sig
z) = forall sig. Transform sig => Int -> sig -> (sig, sig)
CutG.splitAt (forall a b. (C a, C b) => a -> b
RealRing.round t
t) forall a b. (a -> b) -> a -> b
$ forall rate amplitude body. T rate amplitude body -> body
SigA.body Signal s amp sig
x
      in  (forall sig1 rate amp sig0.
sig1 -> T rate amp sig0 -> T rate amp sig1
SigA.replaceBody sig
y Signal s amp sig
x,
           forall sig1 rate amp sig0.
sig1 -> T rate amp sig0 -> T rate amp sig1
SigA.replaceBody sig
z Signal s amp sig
x)

{-# INLINE take #-}
take :: (CutG.Transform sig, RealRing.C t, Dim.C u) =>
   DN.T u t ->
   Proc.T s u t
      (Signal s amp sig ->
       Signal s amp sig)
take :: forall sig t u s amp.
(Transform sig, C t, C u) =>
T u t -> T s u t (Signal s amp sig -> Signal s amp sig)
take T u t
t' =
   forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall t u s. (C t, C u) => T u t -> T s u t t
Proc.toTimeScalar T u t
t') forall a b. (a -> b) -> a -> b
$
   \t
t -> forall sig0 sig1 rate amp.
(sig0 -> sig1) -> T rate amp sig0 -> T rate amp sig1
SigA.processBody (forall sig. Transform sig => Int -> sig -> sig
CutG.take (forall a b. (C a, C b) => a -> b
RealRing.round t
t))

{-# INLINE drop #-}
drop :: (CutG.Transform sig, RealRing.C t, Dim.C u) =>
   DN.T u t ->
   Proc.T s u t
      (Signal s amp sig ->
       Signal s amp sig)
drop :: forall sig t u s amp.
(Transform sig, C t, C u) =>
T u t -> T s u t (Signal s amp sig -> Signal s amp sig)
drop T u t
t' =
   forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall t u s. (C t, C u) => T u t -> T s u t t
Proc.toTimeScalar T u t
t') forall a b. (a -> b) -> a -> b
$
   \t
t -> forall sig0 sig1 rate amp.
(sig0 -> sig1) -> T rate amp sig0 -> T rate amp sig1
SigA.processBody (forall sig. Transform sig => Int -> sig -> sig
CutG.drop (forall a b. (C a, C b) => a -> b
RealRing.round t
t))


{-# INLINE concat #-}
concat ::
   (Amp.Primitive amp, Monoid sig, Dim.C u) =>
   Proc.T s u t (
      [Signal s amp sig] ->
      Signal s amp sig)
concat :: forall amp sig u s t.
(Primitive amp, Monoid sig, C u) =>
T s u t ([Signal s amp sig] -> Signal s amp sig)
concat =
   forall a s u t. a -> T s u t a
Proc.pure forall a b. (a -> b) -> a -> b
$
   forall rate amplitude body.
rate -> amplitude -> body -> T rate amplitude body
SigA.Cons forall s. Phantom s
Rate.Phantom forall amp. Primitive amp => amp
Amp.primitive forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall rate amplitude body. T rate amplitude body -> body
SigA.body

{-# INLINE append #-}
append ::
   (Amp.Primitive amp, Monoid sig, Dim.C u) =>
   Proc.T s u t (
      Signal s amp sig ->
      Signal s amp sig ->
      Signal s amp sig)
append :: forall amp sig u s t.
(Primitive amp, Monoid sig, C u) =>
T s u t (Signal s amp sig -> Signal s amp sig -> Signal s amp sig)
append =
   forall a s u t. a -> T s u t a
Proc.pure forall a b. (a -> b) -> a -> b
$
   \Signal s amp sig
x -> forall sig0 sig1 rate amp.
(sig0 -> sig1) -> T rate amp sig0 -> T rate amp sig1
SigA.processBody (forall a. Monoid a => a -> a -> a
mappend (forall rate amplitude body. T rate amplitude body -> body
SigA.body Signal s amp sig
x))