-- | Envelope / UGen.
module Sound.SC3.UGen.Envelope where

import Sound.SC3.Common.Enum
import Sound.SC3.Common.Envelope
import Sound.SC3.Common.Math.Operator
import Sound.SC3.Common.Rate

import Sound.SC3.UGen.Bindings
import Sound.SC3.UGen.Type
import Sound.SC3.UGen.UGen

{- | Trapezoidal envelope generator.

> import Sound.SC3.Plot
> plotEnvelope [envTrapezoid 0.99 0.5 1 1,envTrapezoid 0.5 0.75 0.65 0.35]

-}
envTrapezoid :: OrdE t => t -> t -> t -> t -> Envelope t
envTrapezoid :: t -> t -> t -> t -> Envelope t
envTrapezoid = (t -> t -> t, t -> t -> t) -> t -> t -> t -> t -> Envelope t
forall t.
Num t =>
(t -> t -> t, t -> t -> t) -> t -> t -> t -> t -> Envelope t
envTrapezoid_f (t -> t -> t
forall a. OrdE a => a -> a -> a
less_than_or_equal_to,t -> t -> t
forall a. OrdE a => a -> a -> a
greater_than_or_equal_to)

-- | 'latch' 1 of 'impulse' 0.
first_zero_then_one :: Rate -> UGen
first_zero_then_one :: Rate -> UGen
first_zero_then_one Rate
rt = UGen -> UGen -> UGen
latch UGen
1 (Rate -> UGen -> UGen -> UGen
impulse Rate
rt UGen
0 UGen
0)

-- | 'env_circle_z' of k-rate 'first_zero_thereafter_one'.
env_circle_u :: UGen -> Envelope_Curve UGen -> Envelope UGen -> Envelope UGen
env_circle_u :: UGen -> Envelope_Curve UGen -> Envelope UGen -> Envelope UGen
env_circle_u = UGen
-> UGen -> Envelope_Curve UGen -> Envelope UGen -> Envelope UGen
forall a.
Fractional a =>
a -> a -> Envelope_Curve a -> Envelope a -> Envelope a
env_circle_z (Rate -> UGen
first_zero_then_one Rate
KR)

-- | Singleton fade envelope.
envGate :: UGen -> UGen -> UGen -> DoneAction UGen -> Envelope_Curve UGen -> UGen
envGate :: UGen
-> UGen -> UGen -> DoneAction UGen -> Envelope_Curve UGen -> UGen
envGate UGen
level UGen
gate_ UGen
fadeTime DoneAction UGen
doneAction Envelope_Curve UGen
curve =
    let startVal :: UGen
startVal = UGen
fadeTime UGen -> UGen -> UGen
forall a. OrdE a => a -> a -> a
`less_than_or_equal_to` UGen
0
        e :: Envelope UGen
e = [UGen]
-> [UGen]
-> [Envelope_Curve UGen]
-> Maybe Int
-> Maybe Int
-> UGen
-> Envelope UGen
forall a.
[a]
-> [a]
-> [Envelope_Curve a]
-> Maybe Int
-> Maybe Int
-> a
-> Envelope a
Envelope [UGen
startVal,UGen
1,UGen
0] [UGen
1,UGen
1] [Envelope_Curve UGen
curve] (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1) Maybe Int
forall a. Maybe a
Nothing UGen
0
    in Rate
-> UGen
-> UGen
-> UGen
-> UGen
-> DoneAction UGen
-> Envelope UGen
-> UGen
envGen Rate
KR UGen
gate_ UGen
level UGen
0 UGen
fadeTime DoneAction UGen
doneAction Envelope UGen
e

-- | Variant with default values for all inputs.  @gate@ and
-- @fadeTime@ are 'control's, @doneAction@ is 'RemoveSynth', @curve@
-- is 'EnvSin'.
envGate_def :: UGen
envGate_def :: UGen
envGate_def =
    let level :: UGen
level = UGen
1
        gate_ :: UGen
gate_ = Rate -> String -> Double -> Control_Meta_T3 Double -> UGen
control_m Rate
KR String
"gate" Double
1 (Double
0,Double
1,String
"gate")
        fadeTime :: UGen
fadeTime = Rate -> String -> Double -> Control_Meta_T3 Double -> UGen
control_m Rate
KR String
"fadeTime" Double
0.02 (Double
0,Double
10,String
"lin")
        doneAction :: DoneAction t
doneAction = DoneAction t
forall t. DoneAction t
RemoveSynth
        curve :: Envelope_Curve a
curve = Envelope_Curve a
forall a. Envelope_Curve a
EnvSin
    in UGen
-> UGen -> UGen -> DoneAction UGen -> Envelope_Curve UGen -> UGen
envGate UGen
level UGen
gate_ UGen
fadeTime DoneAction UGen
forall t. DoneAction t
doneAction Envelope_Curve UGen
forall a. Envelope_Curve a
curve