module Csound.Typed.Opcode.MathematicalOperations (
    
    
    -- * Comparators and Accumulators.
    clear, vincr,
    
    -- * Amplitude Functions.
    ampdb, ampdbfs, dbamp, dbfsamp,
    
    -- * Random Functions.
    birnd, rnd,
    
    -- * Opcode Equivalents of Functions.
    divz, mac, maca, polynomial, pow, product', sum', taninv2) where

import Control.Monad.Trans.Class
import Csound.Dynamic
import Csound.Typed

-- Comparators and Accumulators.

-- | 
-- Zeroes a list of audio signals.
--
-- clear zeroes a list of audio signals.
--
-- >  clear  avar1 [, avar2] [, avar3] [...]
--
-- csound doc: <http://csound.com/docs/manual/clear.html>
clear ::  [Sig] -> SE ()
clear :: [Sig] -> SE ()
clear [Sig]
b1 = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ (E -> Dep ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> Dep ()) -> DepT GE E -> Dep ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (DepT GE E -> Dep ()) -> DepT GE E -> Dep ()
forall a b. (a -> b) -> a -> b
$ GE E -> DepT GE E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (GE E -> DepT GE E) -> GE E -> DepT GE E
forall a b. (a -> b) -> a -> b
$ [E] -> E
f ([E] -> E) -> GE [E] -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Sig -> GE E) -> [Sig] -> GE [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Sig -> GE E
unSig [Sig]
b1
    where f :: [E] -> E
f [E]
a1 = Name -> Spec1 -> [E] -> E
opcs Name
"clear" [(Rate
Xr,(Rate -> [Rate]
forall a. a -> [a]
repeat Rate
Ar))] [E]
a1

-- | 
-- Accumulates audio signals.
--
-- vincr increments one audio variable with another signal, i.e. it accumulates output.
--
-- >  vincr  accum, aincr
--
-- csound doc: <http://csound.com/docs/manual/vincr.html>
vincr ::  Sig -> Sig -> SE ()
vincr :: Sig -> Sig -> SE ()
vincr Sig
b1 Sig
b2 = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ (E -> Dep ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> Dep ()) -> DepT GE E -> Dep ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (DepT GE E -> Dep ()) -> DepT GE E -> Dep ()
forall a b. (a -> b) -> a -> b
$ GE E -> DepT GE E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (GE E -> DepT GE E) -> GE E -> DepT GE E
forall a b. (a -> b) -> a -> b
$ E -> E -> E
f (E -> E -> E) -> GE E -> GE (E -> E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sig -> GE E
unSig Sig
b1 GE (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
unSig Sig
b2
    where f :: E -> E -> E
f E
a1 E
a2 = Name -> Spec1 -> [E] -> E
opcs Name
"vincr" [(Rate
Xr,[Rate
Ar,Rate
Ar])] [E
a1,E
a2]

-- Amplitude Functions.

-- | 
-- Returns the amplitude equivalent of the decibel value x.
--
-- Returns the amplitude equivalent of the decibel value x. Thus:
--
-- >  ampdb (x)  (no rate restriction)
--
-- csound doc: <http://csound.com/docs/manual/ampdb.html>
ampdb :: SigOrD a => a -> a
ampdb :: a -> a
ampdb a
b1 = GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> GE E -> a
forall a b. (a -> b) -> a -> b
$ E -> E
f (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1
    where f :: E -> E
f E
a1 = Name -> E -> E
opr1 Name
"ampdb" E
a1

-- | 
-- Returns the amplitude equivalent (in 16-bit signed integer scale) of the full scale decibel (dB FS) value x.
--
-- Returns the amplitude equivalent of the full scale decibel (dB FS) value x. The logarithmic full scale decibel values will be converted to linear 16-bit signed integer values from −32,768 to +32,767.
--
-- >  ampdbfs (x)  (no rate restriction)
--
-- csound doc: <http://csound.com/docs/manual/ampdbfs.html>
ampdbfs :: SigOrD a => a -> a
ampdbfs :: a -> a
ampdbfs a
b1 = GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> GE E -> a
forall a b. (a -> b) -> a -> b
$ E -> E
f (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1
    where f :: E -> E
f E
a1 = Name -> E -> E
opr1 Name
"ampdbfs" E
a1

-- | 
-- Returns the decibel equivalent of the raw amplitude x.
--
-- >  dbamp (x)  (init-rate or control-rate args only)
--
-- csound doc: <http://csound.com/docs/manual/dbamp.html>
dbamp :: SigOrD a => a -> a
dbamp :: a -> a
dbamp a
b1 = GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> GE E -> a
forall a b. (a -> b) -> a -> b
$ E -> E
f (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1
    where f :: E -> E
f E
a1 = Name -> E -> E
opr1k Name
"dbamp" E
a1

-- | 
-- Returns the decibel equivalent of the raw amplitude x, relative to full scale amplitude.
--
-- Returns the decibel equivalent of the raw amplitude x, relative to full scale amplitude. Full scale is assumed to be 16 bit. New is Csound version 4.10.
--
-- >  dbfsamp (x)  (init-rate or control-rate args only)
--
-- csound doc: <http://csound.com/docs/manual/dbfsamp.html>
dbfsamp :: SigOrD a => a -> a
dbfsamp :: a -> a
dbfsamp a
b1 = GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> GE E -> a
forall a b. (a -> b) -> a -> b
$ E -> E
f (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1
    where f :: E -> E
f E
a1 = Name -> E -> E
opr1k Name
"dbfsamp" E
a1

-- Random Functions.

-- | 
-- Returns a random number in a bi-polar range.
--
-- >  birnd (x) (init- or control-rate only)
--
-- csound doc: <http://csound.com/docs/manual/birnd.html>
birnd :: SigOrD a => a -> SE a
birnd :: a -> SE a
birnd a
b1 = (E -> a) -> SE E -> SE a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ( GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> (E -> GE E) -> E -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. E -> GE E
forall (m :: * -> *) a. Monad m => a -> m a
return) (SE E -> SE a) -> SE E -> SE a
forall a b. (a -> b) -> a -> b
$ DepT GE E -> SE E
forall a. Dep a -> SE a
SE (DepT GE E -> SE E) -> DepT GE E -> SE E
forall a b. (a -> b) -> a -> b
$ (E -> DepT GE E
forall (m :: * -> *). Monad m => E -> DepT m E
depT (E -> DepT GE E) -> DepT GE E -> DepT GE E
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (DepT GE E -> DepT GE E) -> DepT GE E -> DepT GE E
forall a b. (a -> b) -> a -> b
$ GE E -> DepT GE E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (GE E -> DepT GE E) -> GE E -> DepT GE E
forall a b. (a -> b) -> a -> b
$ E -> E
f (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1
    where f :: E -> E
f E
a1 = Name -> E -> E
opr1k Name
"birnd" E
a1

-- | 
-- Returns a random number in a unipolar range at the rate given by the input argument.
--
-- >  rnd (x) (init- or control-rate only)
--
-- csound doc: <http://csound.com/docs/manual/rnd.html>
rnd :: SigOrD a => a -> SE a
rnd :: a -> SE a
rnd a
b1 = (E -> a) -> SE E -> SE a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ( GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> (E -> GE E) -> E -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. E -> GE E
forall (m :: * -> *) a. Monad m => a -> m a
return) (SE E -> SE a) -> SE E -> SE a
forall a b. (a -> b) -> a -> b
$ DepT GE E -> SE E
forall a. Dep a -> SE a
SE (DepT GE E -> SE E) -> DepT GE E -> SE E
forall a b. (a -> b) -> a -> b
$ (E -> DepT GE E
forall (m :: * -> *). Monad m => E -> DepT m E
depT (E -> DepT GE E) -> DepT GE E -> DepT GE E
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (DepT GE E -> DepT GE E) -> DepT GE E -> DepT GE E
forall a b. (a -> b) -> a -> b
$ GE E -> DepT GE E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (GE E -> DepT GE E) -> GE E -> DepT GE E
forall a b. (a -> b) -> a -> b
$ E -> E
f (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1
    where f :: E -> E
f E
a1 = Name -> E -> E
opr1k Name
"rnd" E
a1

-- Opcode Equivalents of Functions.

-- | 
-- Safely divides two numbers.
--
-- > ares  divz  xa, xb, ksubst
-- > ires  divz  ia, ib, isubst
-- > kres  divz  ka, kb, ksubst
-- > ... divz (ka, kb, ksubst)... (no rate restriction)
--
-- csound doc: <http://csound.com/docs/manual/divz.html>
divz :: SigOrD a => a -> a -> a
divz :: a -> a -> a
divz a
b1 a
b2 = GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> GE E -> a
forall a b. (a -> b) -> a -> b
$ E -> E -> E
f (E -> E -> E) -> GE E -> GE (E -> E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1 GE (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> GE E
forall a. Val a => a -> GE E
toGE a
b2
    where f :: E -> E -> E
f E
a1 E
a2 = Name -> Spec1 -> [E] -> E
opcs Name
"divz" [(Rate
Ar,[Rate
Xr,Rate
Xr]),(Rate
Kr,[Rate
Kr,Rate
Kr]),(Rate
Ir,[Rate
Ir,Rate
Ir])] [E
a1,E
a2]

-- | 
-- Multiplies and accumulates a- and k-rate signals.
--
-- > ares  mac  ksig1, asig1 [, ksig2] [, asig2] [, ksig3] [, asig3] [...]
--
-- csound doc: <http://csound.com/docs/manual/mac.html>
mac ::  [Sig] -> Sig
mac :: [Sig] -> Sig
mac [Sig]
b1 = GE E -> Sig
Sig (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ [E] -> E
f ([E] -> E) -> GE [E] -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Sig -> GE E) -> [Sig] -> GE [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Sig -> GE E
unSig [Sig]
b1
    where f :: [E] -> E
f [E]
a1 = Name -> Spec1 -> [E] -> E
opcs Name
"mac" [(Rate
Ar,[Rate
Kr,Rate
Ar,Rate
Kr,Rate
Ar,Rate
Kr] [Rate] -> [Rate] -> [Rate]
forall a. [a] -> [a] -> [a]
++ (Rate -> [Rate]
forall a. a -> [a]
repeat Rate
Ar))] [E]
a1

-- | 
-- Multiply and accumulate a-rate signals only.
--
-- > ares  maca  asig1 , asig2 [, asig3] [, asig4] [, asig5] [...]
--
-- csound doc: <http://csound.com/docs/manual/maca.html>
maca ::  [Sig] -> Sig
maca :: [Sig] -> Sig
maca [Sig]
b1 = GE E -> Sig
Sig (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ [E] -> E
f ([E] -> E) -> GE [E] -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Sig -> GE E) -> [Sig] -> GE [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Sig -> GE E
unSig [Sig]
b1
    where f :: [E] -> E
f [E]
a1 = Name -> Spec1 -> [E] -> E
opcs Name
"maca" [(Rate
Ar,(Rate -> [Rate]
forall a. a -> [a]
repeat Rate
Ar))] [E]
a1

-- | 
-- Efficiently evaluates a polynomial of arbitrary order.
--
-- The polynomial opcode calculates a polynomial with a single a-rate input variable.  The polynomial is a sum of any number of terms in the form kn*x^n where kn is the nth coefficient of the expression.  These coefficients are k-rate values.
--
-- > aout  polynomial  ain, k0 [, k1 [, k2 [...]]]
--
-- csound doc: <http://csound.com/docs/manual/polynomial.html>
polynomial ::  Sig -> [Sig] -> Sig
polynomial :: Sig -> [Sig] -> Sig
polynomial Sig
b1 [Sig]
b2 = GE E -> Sig
Sig (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ E -> [E] -> E
f (E -> [E] -> E) -> GE E -> GE ([E] -> E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sig -> GE E
unSig Sig
b1 GE ([E] -> E) -> GE [E] -> GE E
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Sig -> GE E) -> [Sig] -> GE [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Sig -> GE E
unSig [Sig]
b2
    where f :: E -> [E] -> E
f E
a1 [E]
a2 = Name -> Spec1 -> [E] -> E
opcs Name
"polynomial" [(Rate
Ar,[Rate
Ar] [Rate] -> [Rate] -> [Rate]
forall a. [a] -> [a] -> [a]
++ (Rate -> [Rate]
forall a. a -> [a]
repeat Rate
Kr))] ([E
a1] [E] -> [E] -> [E]
forall a. [a] -> [a] -> [a]
++ [E]
a2)

-- | 
-- Computes one argument to the power of another argument.
--
-- Computes xarg to the power of kpow (or ipow) and scales the result by inorm.
--
-- > ares  pow  aarg, kpow [, inorm]
-- > ires  pow  iarg, ipow [, inorm]
-- > kres  pow  karg, kpow [,
-- >         inorm]
-- > ires[]  pow  iarg[], ipow[] 
-- > kres[]  pow  karg[], kpow[]
-- > ires[]  pow  iarg[], ipow 
-- > kres[]  pow  karg[], kpow 
--
-- csound doc: <http://csound.com/docs/manual/pow.html>
pow ::  Sig -> Sig -> Sig
pow :: Sig -> Sig -> Sig
pow Sig
b1 Sig
b2 = GE E -> Sig
Sig (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ E -> E -> E
f (E -> E -> E) -> GE E -> GE (E -> E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sig -> GE E
unSig Sig
b1 GE (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Sig -> GE E
unSig Sig
b2
    where f :: E -> E -> E
f E
a1 E
a2 = Name -> Spec1 -> [E] -> E
opcs Name
"pow" [(Rate
Ar,[Rate
Ar,Rate
Kr,Rate
Ir])
                               ,(Rate
Ir,[Rate
Ir,Rate
Ir,Rate
Ir])
                               ,(Rate
Kr,[Rate
Kr,Rate
Kr,Rate
Ir])
                               ,(Rate
Ir,[Rate
Ir,Rate
Ir])
                               ,(Rate
Kr,[Rate
Kr,Rate
Kr])
                               ,(Rate
Ir,[Rate
Ir,Rate
Ir])
                               ,(Rate
Kr,[Rate
Kr,Rate
Kr])] [E
a1,E
a2]

-- | 
-- Multiplies any number of a-rate signals.
--
-- > ares  product  asig1, asig2 [, asig3] [...]
--
-- csound doc: <http://csound.com/docs/manual/product.html>
product' ::  [Sig] -> Sig
product' :: [Sig] -> Sig
product' [Sig]
b1 = GE E -> Sig
Sig (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ [E] -> E
f ([E] -> E) -> GE [E] -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Sig -> GE E) -> [Sig] -> GE [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Sig -> GE E
unSig [Sig]
b1
    where f :: [E] -> E
f [E]
a1 = Name -> Spec1 -> [E] -> E
opcs Name
"product" [(Rate
Ar,(Rate -> [Rate]
forall a. a -> [a]
repeat Rate
Ar))] [E]
a1

-- | 
-- Sums any number of a-rate signals, or array elements.
--
-- > ares  sum  asig1 [, asig2] [, asig3] [...]
-- > kres  sum  karr
-- > ires  sum  iarr
--
-- csound doc: <http://csound.com/docs/manual/sum.html>
sum' ::  [Sig] -> Sig
sum' :: [Sig] -> Sig
sum' [Sig]
b1 = GE E -> Sig
Sig (GE E -> Sig) -> GE E -> Sig
forall a b. (a -> b) -> a -> b
$ [E] -> E
f ([E] -> E) -> GE [E] -> GE E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Sig -> GE E) -> [Sig] -> GE [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Sig -> GE E
unSig [Sig]
b1
    where f :: [E] -> E
f [E]
a1 = Name -> Spec1 -> [E] -> E
opcs Name
"sum" [(Rate
Ar,(Rate -> [Rate]
forall a. a -> [a]
repeat Rate
Ar)),(Rate
Kr,[Rate
Kr]),(Rate
Ir,[Rate
Ir])] [E]
a1

-- | 
-- Returns an arctangent.
--
-- Returns the arctangent of iy/ix, ky/kx, or ay/ax.
--
-- > ares  taninv2  ay, ax
-- > ires  taninv2  iy, ix
-- > kres  taninv2  ky, kx
-- > ... taninv2 (ky, kx)... (no rate restriction)
--
-- csound doc: <http://csound.com/docs/manual/taninv2.html>
taninv2 :: SigOrD a => a -> a -> a
taninv2 :: a -> a -> a
taninv2 a
b1 a
b2 = GE E -> a
forall a. Val a => GE E -> a
fromGE (GE E -> a) -> GE E -> a
forall a b. (a -> b) -> a -> b
$ E -> E -> E
f (E -> E -> E) -> GE E -> GE (E -> E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> GE E
forall a. Val a => a -> GE E
toGE a
b1 GE (E -> E) -> GE E -> GE E
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> GE E
forall a. Val a => a -> GE E
toGE a
b2
    where f :: E -> E -> E
f E
a1 E
a2 = Name -> Spec1 -> [E] -> E
opcs Name
"taninv2" [(Rate
Ar,[Rate
Ar,Rate
Ar]),(Rate
Kr,[Rate
Kr,Rate
Kr]),(Rate
Ir,[Rate
Ir,Rate
Ir])] [E
a1,E
a2]