{-# Language FlexibleInstances, ScopedTypeVariables #-}
module Csound.Typed.Types.Array(
    Arr(..),
    newLocalArr, newGlobalArr, newLocalCtrlArr, newGlobalCtrlArr,
    fillLocalArr, fillGlobalArr, fillLocalCtrlArr, fillGlobalCtrlArr,
    readArr, writeArr, writeInitArr, modifyArr, mixArr,
    -- * Misc functions to help the type inverence
    Arr1, DArr1, Arr2, DArr2, Arr3, DArr3,
    arr1, darr1, arr2, darr2, arr3, darr3,

    -- * Array opcodes
    maparrayNew, lenarray, copyf2array, copya2ftab, minarray, maxarray, sumarray,
    scalearray, slicearrayNew,

    maparrayCopy, slicearrayCopy,
    divArrayCopy, subArrayCopy,
    addArrayCopy, mulArrayCopy,
    addArrayNew, mulArrayNew, divArrayNew, subArrayNew,

    -- * Spectral opcodes
    SpecArr,

    fftNew, fftinvNew, rfftNew, rifftNew, pvs2tab, tab2pvs, cmplxprodNew,
    rect2polNew, pol2rectNew, pol2rect2New, windowArrayNew,
    r2cNew, c2rNew, magsArrayNew, phsArrayNew,

    fftCopy, fftinvCopy, rfftCopy, rifftCopy, cmplxprodCopy,
    rect2polCopy, pol2rectCopy, pol2rect2Copy, windowArrayCopy,
    r2cCopy, c2rCopy, magsArrayCopy, phsArrayCopy
) where


import Control.Monad
import Data.Proxy
import Data.Text (Text)

import Csound.Dynamic hiding (writeArr, writeInitArr, readArr, newLocalArrVar, newTmpArrVar, int)
import qualified Csound.Dynamic as D

import Csound.Typed.Types.Prim
import Csound.Typed.Types.Tuple
import Csound.Typed.GlobalState.SE
import Csound.Typed.GlobalState.GE

-- | An array with single signal index.
type Arr1 a  = Arr Sig a

-- | An array with single constant index.
type DArr1 a = Arr D   a


-- | A matrix (2D array) with signal index.
type Arr2 a  = Arr (Sig, Sig) a

-- | A matrix (2D array) with constant index.
type DArr2 a = Arr (D, D) a

-- | A 3D array with signal index.
type Arr3 a  = Arr (Sig, Sig, Sig) a

-- | A 3D array with constant index.
type DArr3 a = Arr (D, D, D) a

-- | Function to help the type inference.
arr1  :: SE (Arr Sig a) -> SE (Arr Sig a)
arr1 :: forall a. SE (Arr Sig a) -> SE (Arr Sig a)
arr1 = SE (Arr Sig a) -> SE (Arr Sig a)
forall a. a -> a
id

-- | Function to help the type inference.
darr1 :: SE (Arr D   a) -> SE (Arr D   a)
darr1 :: forall a. SE (Arr D a) -> SE (Arr D a)
darr1 = SE (Arr D a) -> SE (Arr D a)
forall a. a -> a
id

-- | Function to help the type inference.
arr2  :: SE (Arr (Sig,Sig) a) -> SE (Arr (Sig,Sig) a)
arr2 :: forall a. SE (Arr (Sig, Sig) a) -> SE (Arr (Sig, Sig) a)
arr2 = SE (Arr (Sig, Sig) a) -> SE (Arr (Sig, Sig) a)
forall a. a -> a
id

-- | Function to help the type inference.
darr2 :: SE (Arr (D,D)   a)   -> SE (Arr (D,D)     a)
darr2 :: forall a. SE (Arr (D, D) a) -> SE (Arr (D, D) a)
darr2 = SE (Arr (D, D) a) -> SE (Arr (D, D) a)
forall a. a -> a
id

-- | Function to help the type inference.
arr3  :: SE (Arr (Sig,Sig,Sig) a) -> SE (Arr (Sig,Sig,Sig) a)
arr3 :: forall a. SE (Arr (Sig, Sig, Sig) a) -> SE (Arr (Sig, Sig, Sig) a)
arr3 = SE (Arr (Sig, Sig, Sig) a) -> SE (Arr (Sig, Sig, Sig) a)
forall a. a -> a
id

-- | Function to help the type inference.
darr3 :: SE (Arr (D,D,D)   a)     -> SE (Arr (D,D,D)     a)
darr3 :: forall a. SE (Arr (D, D, D) a) -> SE (Arr (D, D, D) a)
darr3 = SE (Arr (D, D, D) a) -> SE (Arr (D, D, D) a)
forall a. a -> a
id

-- | Arrays. The array data type is parametrized with type of the index and the type of the value.
-- Note that the data tpyes for indices and values can be tuples.
newtype Arr ix a = Arr { forall ix a. Arr ix a -> [Var]
unArr :: [Var] }

newArrBy :: forall ix a . (Tuple a, Tuple ix) => (Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
newArrBy :: forall ix a.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
newArrBy Rate -> GE [E] -> SE Var
mkVar [D]
sizes =
    ([Var] -> Arr ix a) -> SE [Var] -> SE (Arr ix a)
forall a b. (a -> b) -> SE a -> SE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Var] -> Arr ix a
forall ix a. [Var] -> Arr ix a
Arr (SE [Var] -> SE (Arr ix a)) -> SE [Var] -> SE (Arr ix a)
forall a b. (a -> b) -> a -> b
$ (Rate -> SE Var) -> [Rate] -> SE [Var]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\Rate
x -> Rate -> GE [E] -> SE Var
mkVar Rate
x ((D -> GE E) -> [D] -> GE [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM D -> GE E
forall a. Val a => a -> GE E
toGE [D]
sizes)) (Proxy a -> [Rate]
forall a. Tuple a => Proxy a -> [Rate]
tupleRates (Proxy a -> [Rate]) -> Proxy a -> [Rate]
forall a b. (a -> b) -> a -> b
$ (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a))

getIndices :: Tuple ix => [Int] -> [ix]
getIndices :: forall ix. Tuple ix => [Int] -> [ix]
getIndices [Int]
xs = ([Int] -> ix) -> [[Int]] -> [ix]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GE [E] -> ix
forall a. Tuple a => GE [E] -> a
toTuple (GE [E] -> ix) -> ([Int] -> GE [E]) -> [Int] -> ix
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [E] -> GE [E]
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return ([E] -> GE [E]) -> ([Int] -> [E]) -> [Int] -> GE [E]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> E) -> [Int] -> [E]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> E
D.int) ([[Int]] -> [ix]) -> [[Int]] -> [ix]
forall a b. (a -> b) -> a -> b
$ [Int] -> [[Int]]
getIntIndices [Int]
xs

getIntIndices :: [Int] -> [[Int]]
getIntIndices :: [Int] -> [[Int]]
getIntIndices = ([Int] -> [Int]) -> [[Int]] -> [[Int]]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Int] -> [Int]
forall a. [a] -> [a]
reverse ([[Int]] -> [[Int]]) -> ([Int] -> [[Int]]) -> [Int] -> [[Int]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([[Int]] -> Int -> [[Int]]) -> [[Int]] -> [Int] -> [[Int]]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl [[Int]] -> Int -> [[Int]]
go []
    where
        go :: [[Int]] -> Int -> [[Int]]
        go :: [[Int]] -> Int -> [[Int]]
go [[Int]]
res Int
n = case [[Int]]
res of
            [] -> (Int -> [Int]) -> [Int] -> [[Int]]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Int
x -> [Int
x]) [Int]
ix
            [[Int]]
xs -> [ Int
first Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
rest | Int
first <- [Int]
ix, [Int]
rest <- [[Int]]
xs ]
            where ix :: [Int]
ix = [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]

fillArrBy :: (Tuple a, Tuple ix) => (Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
fillArrBy :: forall a ix.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
fillArrBy Rate -> GE [E] -> SE Var
mkVar [Int]
sizes [a]
inits = do
    Arr ix a
arr <- (Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
forall ix a.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
newArrBy Rate -> GE [E] -> SE Var
mkVar ((Int -> D) -> [Int] -> [D]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> D
int [Int]
sizes)
    (ix -> a -> SE ()) -> [ix] -> [a] -> SE ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_  (Arr ix a -> ix -> a -> SE ()
forall ix a. (Tuple ix, Tuple a) => Arr ix a -> ix -> a -> SE ()
writeInitArr Arr ix a
arr) ([Int] -> [ix]
forall ix. Tuple ix => [Int] -> [ix]
getIndices [Int]
sizes) [a]
inits
    Arr ix a -> SE (Arr ix a)
forall a. a -> SE a
forall (m :: * -> *) a. Monad m => a -> m a
return Arr ix a
arr

-- | Creates an array that is local to the body of Csound instrument where it's defined.
-- The array contains audio signals.
--
-- > newLocalArr sizes
newLocalArr :: (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newLocalArr :: forall a ix. (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newLocalArr = (Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
forall ix a.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
newArrBy Rate -> GE [E] -> SE Var
newLocalArrVar

-- | Creates a global array. The array contains audio signals.
--
-- > newGlobalArr sizes
newGlobalArr :: (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newGlobalArr :: forall a ix. (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newGlobalArr = (Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
forall ix a.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
newArrBy Rate -> GE [E] -> SE Var
newGlobalArrVar

-- | Creates an array that is local to the body of Csound instrument where it's defined.
-- The array contains control signals.
--
-- > newLocalCtrlArr sizes
newLocalCtrlArr :: (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newLocalCtrlArr :: forall a ix. (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newLocalCtrlArr = (Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
forall ix a.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
newArrBy Rate -> GE [E] -> SE Var
newLocalCtrlArrVar

-- | Creates a global array. The array contains control signals.
--
-- > newGlobalCtrlArr sizes
newGlobalCtrlArr :: (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newGlobalCtrlArr :: forall a ix. (Tuple a, Tuple ix) => [D] -> SE (Arr ix a)
newGlobalCtrlArr = (Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
forall ix a.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [D] -> SE (Arr ix a)
newArrBy Rate -> GE [E] -> SE Var
newGlobalCtrlArrVar

-- | Creates an array that is local to the body of Csound instrument where it's defined.
-- The array contains audio signals. It fills the array from the list of values (the last argument).
--
-- > fillLocalArr sizes initValues = ...
fillLocalArr :: (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillLocalArr :: forall a ix. (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillLocalArr = (Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
forall a ix.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
fillArrBy Rate -> GE [E] -> SE Var
newLocalArrVar

-- | Creates a global array. The array contains audio signals. It fills the array from the list of values (the last argument).
--
-- > fillGlobalArr sizes initValues = ...
fillGlobalArr :: (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillGlobalArr :: forall a ix. (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillGlobalArr = (Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
forall a ix.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
fillArrBy Rate -> GE [E] -> SE Var
newGlobalArrVar

-- | Creates an array that is local to the body of Csound instrument where it's defined.
-- The array contains control signals. It fills the array from the list of values (the last argument).
--
-- > fillLocalCtrlArr sizes initValues = ...
fillLocalCtrlArr :: (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillLocalCtrlArr :: forall a ix. (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillLocalCtrlArr = (Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
forall a ix.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
fillArrBy Rate -> GE [E] -> SE Var
newLocalCtrlArrVar

-- | Creates a global array. The array contains control signals. It fills the array from the list of values (the last argument).
--
-- > fillGlobalCtrlArr sizes initValues = ...
fillGlobalCtrlArr :: (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillGlobalCtrlArr :: forall a ix. (Tuple a, Tuple ix) => [Int] -> [a] -> SE (Arr ix a)
fillGlobalCtrlArr = (Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
forall a ix.
(Tuple a, Tuple ix) =>
(Rate -> GE [E] -> SE Var) -> [Int] -> [a] -> SE (Arr ix a)
fillArrBy Rate -> GE [E] -> SE Var
newGlobalCtrlArrVar

newLocalCtrlArrVar, newGlobalCtrlArrVar :: Rate -> GE [E] -> SE Var

newLocalCtrlArrVar :: Rate -> GE [E] -> SE Var
newLocalCtrlArrVar  = Rate -> GE [E] -> SE Var
newLocalArrVar  (Rate -> GE [E] -> SE Var)
-> (Rate -> Rate) -> Rate -> GE [E] -> SE Var
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rate -> Rate
toCtrlRate
newGlobalCtrlArrVar :: Rate -> GE [E] -> SE Var
newGlobalCtrlArrVar = Rate -> GE [E] -> SE Var
newGlobalArrVar (Rate -> GE [E] -> SE Var)
-> (Rate -> Rate) -> Rate -> GE [E] -> SE Var
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rate -> Rate
toCtrlRate

toCtrlRate :: Rate -> Rate
toCtrlRate :: Rate -> Rate
toCtrlRate Rate
x = case Rate
x of
    Rate
Ar -> Rate
Kr
    Rate
Kr -> Rate
Ir
    Rate
_  -> Rate
x

-- | Reads data from the array.
readArr :: (Tuple a, Tuple ix) => Arr ix a -> ix -> SE a
readArr :: forall a ix. (Tuple a, Tuple ix) => Arr ix a -> ix -> SE a
readArr (Arr [Var]
vars) ix
ixs = ([E] -> a) -> SE [E] -> SE a
forall a b. (a -> b) -> SE a -> SE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GE [E] -> a
forall a. Tuple a => GE [E] -> a
toTuple (GE [E] -> a) -> ([E] -> GE [E]) -> [E] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [E] -> GE [E]
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return) (SE [E] -> SE a) -> SE [E] -> SE a
forall a b. (a -> b) -> a -> b
$ Dep [E] -> SE [E]
forall a. Dep a -> SE a
SE (Dep [E] -> SE [E]) -> Dep [E] -> SE [E]
forall a b. (a -> b) -> a -> b
$ GE (Dep [E]) -> Dep [E]
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep [E]) -> Dep [E]) -> GE (Dep [E]) -> Dep [E]
forall a b. (a -> b) -> a -> b
$ do
    [E]
ixsExp <- ix -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple ix
ixs
    Dep [E] -> GE (Dep [E])
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep [E] -> GE (Dep [E])) -> Dep [E] -> GE (Dep [E])
forall a b. (a -> b) -> a -> b
$ (Var -> DepT GE E) -> [Var] -> Dep [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\Var
v -> Var -> [E] -> DepT GE E
read' Var
v [E]
ixsExp) [Var]
vars
    where
        read' ::  Var -> [E] -> Dep E
        read' :: Var -> [E] -> DepT GE E
read' = Var -> [E] -> DepT GE E
forall (m :: * -> *). Monad m => Var -> [E] -> DepT m E
D.readArr

-- | Writes data to the array.
writeArr :: (Tuple ix, Tuple a) => Arr ix a -> ix -> a -> SE ()
writeArr :: forall ix a. (Tuple ix, Tuple a) => Arr ix a -> ix -> a -> SE ()
writeArr (Arr [Var]
vars) ix
ixs a
b = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ GE (Dep ()) -> Dep ()
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep ()) -> Dep ()) -> GE (Dep ()) -> Dep ()
forall a b. (a -> b) -> a -> b
$ do
    [E]
ixsExp <- ix -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple ix
ixs
    [E]
bsExp <- a -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple a
b
    Dep () -> GE (Dep ())
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep () -> GE (Dep ())) -> Dep () -> GE (Dep ())
forall a b. (a -> b) -> a -> b
$ (Var -> E -> Dep ()) -> [Var] -> [E] -> Dep ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ (\Var
var E
value -> Var -> [E] -> E -> Dep ()
write Var
var [E]
ixsExp E
value) [Var]
vars [E]
bsExp
    where
        write ::  Var -> [E] -> E -> Dep ()
        write :: Var -> [E] -> E -> Dep ()
write = Var -> [E] -> E -> Dep ()
forall (m :: * -> *). Monad m => Var -> [E] -> E -> DepT m ()
D.writeArr

-- | Writes data to the array.
writeInitArr :: (Tuple ix, Tuple a) => Arr ix a -> ix -> a -> SE ()
writeInitArr :: forall ix a. (Tuple ix, Tuple a) => Arr ix a -> ix -> a -> SE ()
writeInitArr (Arr [Var]
vars) ix
ixs a
b = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ GE (Dep ()) -> Dep ()
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep ()) -> Dep ()) -> GE (Dep ()) -> Dep ()
forall a b. (a -> b) -> a -> b
$ do
    [E]
ixsExp <- ix -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple ix
ixs
    [E]
bsExp <- a -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple a
b
    Dep () -> GE (Dep ())
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep () -> GE (Dep ())) -> Dep () -> GE (Dep ())
forall a b. (a -> b) -> a -> b
$ (Var -> E -> Dep ()) -> [Var] -> [E] -> Dep ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ (\Var
var E
value -> Var -> [E] -> E -> Dep ()
write Var
var [E]
ixsExp E
value) [Var]
vars [E]
bsExp
    where
        write ::  Var -> [E] -> E -> Dep ()
        write :: Var -> [E] -> E -> Dep ()
write = Var -> [E] -> E -> Dep ()
forall (m :: * -> *). Monad m => Var -> [E] -> E -> DepT m ()
D.writeInitArr

-- | Updates the value of the array with pure function.
modifyArr :: (Tuple a, Tuple ix) => Arr ix a -> ix -> (a -> a) -> SE ()
modifyArr :: forall a ix.
(Tuple a, Tuple ix) =>
Arr ix a -> ix -> (a -> a) -> SE ()
modifyArr Arr ix a
ref ix
ixs a -> a
f = do
    a
value <- Arr ix a -> ix -> SE a
forall a ix. (Tuple a, Tuple ix) => Arr ix a -> ix -> SE a
readArr Arr ix a
ref ix
ixs
    Arr ix a -> ix -> a -> SE ()
forall ix a. (Tuple ix, Tuple a) => Arr ix a -> ix -> a -> SE ()
writeArr Arr ix a
ref ix
ixs (a -> a
f a
value)

mixArr :: (Tuple ix, Tuple a, Num a) => Arr ix a -> ix -> a -> SE ()
mixArr :: forall ix a.
(Tuple ix, Tuple a, Num a) =>
Arr ix a -> ix -> a -> SE ()
mixArr Arr ix a
ref ix
ixs a
a = Arr ix a -> ix -> (a -> a) -> SE ()
forall a ix.
(Tuple a, Tuple ix) =>
Arr ix a -> ix -> (a -> a) -> SE ()
modifyArr Arr ix a
ref ix
ixs (a -> a -> a
forall a. Num a => a -> a -> a
+ a
a)

-----------------------------------------------------
-- opcodes with array allocation

-- | Multiplies two numeric arrays and save the result in the third array.
mulArrayNew :: (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
mulArrayNew :: forall b a. (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
mulArrayNew = Text -> Arr a b -> Arr a b -> SE (Arr a b)
forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
binOp Text
"*"

-- | Summs two numeric arrays and save the result in the third array.
addArrayNew :: (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
addArrayNew :: forall b a. (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
addArrayNew = Text -> Arr a b -> Arr a b -> SE (Arr a b)
forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
binOp Text
"+"

-- | Substraction of two numeric arrays and save the result in the third array.
subArrayNew :: (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
subArrayNew :: forall b a. (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
subArrayNew = Text -> Arr a b -> Arr a b -> SE (Arr a b)
forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
binOp Text
"-"

-- | Division of two numeric arrays and save the result in the third array.
divArrayNew :: (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
divArrayNew :: forall b a. (Tuple b, Num b) => Arr a b -> Arr a b -> SE (Arr a b)
divArrayNew = Text -> Arr a b -> Arr a b -> SE (Arr a b)
forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
binOp Text
"/"

lenarray :: SigOrD c => Arr a b -> c
lenarray :: forall c a b. SigOrD c => Arr a b -> c
lenarray (Arr [Var]
vs) = GE E -> c
forall a. Val a => GE E -> a
fromGE (GE E -> c) -> GE E -> c
forall a b. (a -> b) -> a -> b
$ E -> GE E
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (E -> GE E) -> E -> GE E
forall a b. (a -> b) -> a -> b
$ E -> E
f (Var -> E
inlineVar (Var -> E) -> Var -> E
forall a b. (a -> b) -> a -> b
$ [Var] -> Var
forall a. HasCallStack => [a] -> a
head [Var]
vs)
    where f :: E -> E
f E
a = Text -> Spec1 -> [E] -> E
opcs Text
"lenarray" [(Rate
Kr, [Rate
Xr, Rate
Ir]), (Rate
Ir, [Rate
Xr, Rate
Ir])] [E
a]

-- | Copies table to array.
copyf2array :: Arr Sig Sig -> Tab -> SE ()
copyf2array :: Arr Sig Sig -> Tab -> SE ()
copyf2array (Arr [Var]
vs) Tab
t = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ GE (Dep ()) -> Dep ()
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep ()) -> Dep ()) -> GE (Dep ()) -> Dep ()
forall a b. (a -> b) -> a -> b
$ do
    E
tabExp <- Tab -> GE E
forall a. Val a => a -> GE E
toGE Tab
t
    Dep () -> GE (Dep ())
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep () -> GE (Dep ())) -> Dep () -> GE (Dep ())
forall a b. (a -> b) -> a -> b
$ E -> Dep ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> Dep ()) -> E -> Dep ()
forall a b. (a -> b) -> a -> b
$ Text -> Spec1 -> [E] -> E
opcs Text
"copyf2array" [(Rate
Xr, [Var -> Rate
varRate (Var -> Rate) -> Var -> Rate
forall a b. (a -> b) -> a -> b
$ [Var] -> Var
forall a. HasCallStack => [a] -> a
head [Var]
vs, Rate
Ir])] [Var -> E
inlineVar (Var -> E) -> Var -> E
forall a b. (a -> b) -> a -> b
$ [Var] -> Var
forall a. HasCallStack => [a] -> a
head [Var]
vs, E
tabExp]

-- | Copies array to table.
copya2ftab :: Arr Sig Sig -> Tab -> SE ()
copya2ftab :: Arr Sig Sig -> Tab -> SE ()
copya2ftab (Arr [Var]
vs) Tab
t = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ GE (Dep ()) -> Dep ()
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep ()) -> Dep ()) -> GE (Dep ()) -> Dep ()
forall a b. (a -> b) -> a -> b
$ do
    E
tabExp <- Tab -> GE E
forall a. Val a => a -> GE E
toGE Tab
t
    Dep () -> GE (Dep ())
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep () -> GE (Dep ())) -> Dep () -> GE (Dep ())
forall a b. (a -> b) -> a -> b
$ E -> Dep ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> Dep ()) -> E -> Dep ()
forall a b. (a -> b) -> a -> b
$ Text -> Spec1 -> [E] -> E
opcs Text
"copya2ftab" [(Rate
Xr, [Var -> Rate
varRate (Var -> Rate) -> Var -> Rate
forall a b. (a -> b) -> a -> b
$ [Var] -> Var
forall a. HasCallStack => [a] -> a
head [Var]
vs, Rate
Ir])] [Var -> E
inlineVar (Var -> E) -> Var -> E
forall a b. (a -> b) -> a -> b
$ [Var] -> Var
forall a. HasCallStack => [a] -> a
head [Var]
vs, E
tabExp]

-- | Mapps all values in the array with the function.
--
-- Csound docs: <http://csound.github.io/docs/manual/maparray.html>
maparrayNew :: Arr a b -> Str -> SE (Arr a b)
maparrayNew :: forall a b. Arr a b -> Str -> SE (Arr a b)
maparrayNew (Arr [Var]
vs) Str
s = Dep (Arr a b) -> SE (Arr a b)
forall a. Dep a -> SE a
SE (Dep (Arr a b) -> SE (Arr a b)) -> Dep (Arr a b) -> SE (Arr a b)
forall a b. (a -> b) -> a -> b
$ ([Var] -> Arr a b) -> DepT GE [Var] -> Dep (Arr a b)
forall a b. (a -> b) -> DepT GE a -> DepT GE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Var] -> Arr a b
forall ix a. [Var] -> Arr ix a
Arr (DepT GE [Var] -> Dep (Arr a b)) -> DepT GE [Var] -> Dep (Arr a b)
forall a b. (a -> b) -> a -> b
$ GE (DepT GE [Var]) -> DepT GE [Var]
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (DepT GE [Var]) -> DepT GE [Var])
-> GE (DepT GE [Var]) -> DepT GE [Var]
forall a b. (a -> b) -> a -> b
$ do
    E
strExp <- Str -> GE E
forall a. Val a => a -> GE E
toGE Str
s
    DepT GE [Var] -> GE (DepT GE [Var])
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (DepT GE [Var] -> GE (DepT GE [Var]))
-> DepT GE [Var] -> GE (DepT GE [Var])
forall a b. (a -> b) -> a -> b
$ (Var -> DepT GE Var) -> [Var] -> DepT GE [Var]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\Var
var -> Var -> E -> DepT GE Var
go Var
var E
strExp) [Var]
vs
    where
        go :: Var -> E -> DepT GE Var
go Var
var E
strExp = do
            Var
outVar <- SE Var -> DepT GE Var
forall a. SE a -> Dep a
unSE (SE Var -> DepT GE Var) -> SE Var -> DepT GE Var
forall a b. (a -> b) -> a -> b
$ Rate -> SE Var
newTmpArrVar (Var -> Rate
varRate Var
var)
            IsArrInit -> Var -> Text -> Spec1 -> [E] -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
isArrayInit Var
outVar Text
"slicearray" Spec1
idRate [Var -> E
inlineVar Var
var, E
strExp]
            Var -> DepT GE Var
forall a. a -> DepT GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Var -> DepT GE Var) -> Var -> DepT GE Var
forall a b. (a -> b) -> a -> b
$ Var
outVar

        idRate :: Spec1
idRate = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
rate -> (Rate
rate, [Rate
rate, Rate
Ir, Rate
Ir])) [Rate
Ir, Rate
Kr, Rate
Ar]

-- | Finds a minimum value of the array.
minarray :: (Tuple b, Num b) => Arr a b -> SE b
minarray :: forall b a. (Tuple b, Num b) => Arr a b -> SE b
minarray = Text -> Arr a b -> SE b
forall b a. Tuple b => Text -> Arr a b -> SE b
extractArray Text
"minarray"

-- | Finds a maximum value of the array.
maxarray :: (Tuple b, Num b) => Arr a b -> SE b
maxarray :: forall b a. (Tuple b, Num b) => Arr a b -> SE b
maxarray = Text -> Arr a b -> SE b
forall b a. Tuple b => Text -> Arr a b -> SE b
extractArray Text
"maxarray"

-- | Summs all elements in the array.
sumarray :: (Tuple b, Num b) => Arr a b -> SE b
sumarray :: forall b a. (Tuple b, Num b) => Arr a b -> SE b
sumarray = Text -> Arr a b -> SE b
forall b a. Tuple b => Text -> Arr a b -> SE b
extractArray Text
"sumarray"

-- | Scales all elements in the array.
scalearray :: (Tuple b, Num b) => Arr a b -> (b, b) -> SE ()
scalearray :: forall b a. (Tuple b, Num b) => Arr a b -> (b, b) -> SE ()
scalearray (Arr [Var]
vs) (b
a, b
b) = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ GE (Dep ()) -> Dep ()
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep ()) -> Dep ()) -> GE (Dep ()) -> Dep ()
forall a b. (a -> b) -> a -> b
$ do
    [E]
aExps <- b -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple b
a
    [E]
bExps <- b -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple b
b
    Dep () -> GE (Dep ())
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep () -> GE (Dep ())) -> Dep () -> GE (Dep ())
forall a b. (a -> b) -> a -> b
$ (Var -> (E, E) -> Dep ()) -> [Var] -> [(E, E)] -> Dep ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ (\Var
var (E
aExp, E
bExp) -> Var -> (E, E) -> Dep ()
forall {m :: * -> *}. Monad m => Var -> (E, E) -> DepT m ()
go Var
var (E
aExp, E
bExp)) [Var]
vs ([E] -> [E] -> [(E, E)]
forall a b. [a] -> [b] -> [(a, b)]
zip [E]
aExps [E]
bExps)
    where
        go :: Var -> (E, E) -> DepT m ()
go Var
v (E
aExp, E
bExp) =
            E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Text -> Spec1 -> [E] -> E
opcs Text
"copyf2array" [(Rate
Xr, [Var -> Rate
varRate (Var -> Rate) -> Var -> Rate
forall a b. (a -> b) -> a -> b
$ [Var] -> Var
forall a. HasCallStack => [a] -> a
head [Var]
vs, Rate
Ir])] [Var -> E
inlineVar Var
v, E
aExp, E
bExp]

-- | Creates a copy of some part of the given array
slicearrayNew :: Arr D a -> (D, D) -> SE (Arr D a)
slicearrayNew :: forall a. Arr D a -> (D, D) -> SE (Arr D a)
slicearrayNew (Arr [Var]
vs) (D
from, D
to) = Dep (Arr D a) -> SE (Arr D a)
forall a. Dep a -> SE a
SE (Dep (Arr D a) -> SE (Arr D a)) -> Dep (Arr D a) -> SE (Arr D a)
forall a b. (a -> b) -> a -> b
$ ([Var] -> Arr D a) -> DepT GE [Var] -> Dep (Arr D a)
forall a b. (a -> b) -> DepT GE a -> DepT GE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Var] -> Arr D a
forall ix a. [Var] -> Arr ix a
Arr (DepT GE [Var] -> Dep (Arr D a)) -> DepT GE [Var] -> Dep (Arr D a)
forall a b. (a -> b) -> a -> b
$ GE (DepT GE [Var]) -> DepT GE [Var]
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (DepT GE [Var]) -> DepT GE [Var])
-> GE (DepT GE [Var]) -> DepT GE [Var]
forall a b. (a -> b) -> a -> b
$ do
    E
fromExpr <- D -> GE E
forall a. Val a => a -> GE E
toGE D
from
    E
toExpr   <- D -> GE E
forall a. Val a => a -> GE E
toGE D
to
    DepT GE [Var] -> GE (DepT GE [Var])
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (DepT GE [Var] -> GE (DepT GE [Var]))
-> DepT GE [Var] -> GE (DepT GE [Var])
forall a b. (a -> b) -> a -> b
$ (Var -> DepT GE Var) -> [Var] -> DepT GE [Var]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\Var
var -> Var -> (E, E) -> DepT GE Var
go Var
var (E
fromExpr, E
toExpr)) [Var]
vs
    where
        go :: Var -> (E, E) -> DepT GE Var
go Var
var (E
fromExpr, E
toExpr) = do
            Var
outVar <- SE Var -> DepT GE Var
forall a. SE a -> Dep a
unSE (SE Var -> DepT GE Var) -> SE Var -> DepT GE Var
forall a b. (a -> b) -> a -> b
$ Rate -> SE Var
newTmpArrVar (Var -> Rate
varRate Var
var)
            IsArrInit -> Var -> Text -> Spec1 -> [E] -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
isArrayInit Var
outVar Text
"slicearray" Spec1
idRate [Var -> E
inlineVar Var
var, E
fromExpr, E
toExpr]
            Var -> DepT GE Var
forall a. a -> DepT GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Var -> DepT GE Var) -> Var -> DepT GE Var
forall a b. (a -> b) -> a -> b
$ Var
outVar

        idRate :: Spec1
idRate = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
rate -> (Rate
rate, [Rate
rate, Rate
Ir, Rate
Ir])) [Rate
Ir, Rate
Kr, Rate
Ar]

-- spectral opcodes

-- | Spectral array.
type SpecArr = Arr Sig Sig

-- |  Complex-to-complex Fast Fourier Transform.
--
-- csound docs: <http://csound.github.io/docs/manual/fft.html>
fftNew :: SpecArr -> SE SpecArr
fftNew :: Arr Sig Sig -> SE (Arr Sig Sig)
fftNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"fft"

-- | Complex-to-complex Inverse Fast Fourier Transform.
--
-- csound docs: <http://csound.github.io/docs/manual/fftinv.html>
fftinvNew :: SpecArr -> SE SpecArr
fftinvNew :: Arr Sig Sig -> SE (Arr Sig Sig)
fftinvNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"fftinvi"

-- | Fast Fourier Transform of a real-value array.
--
-- csound docs: <http://csound.github.io/docs/manual/rfft.html>
rfftNew :: SpecArr -> SE SpecArr
rfftNew :: Arr Sig Sig -> SE (Arr Sig Sig)
rfftNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"rfft"

-- | Complex-to-real Inverse Fast Fourier Transform.
--
-- csound docs: <http://csound.github.io/docs/manual/rifft.html>
rifftNew :: SpecArr -> SE SpecArr
rifftNew :: Arr Sig Sig -> SE (Arr Sig Sig)
rifftNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"rifft"

-- | Copies spectral data to k-rate arrays (or t-variables). Also known as pvs2array.
--
-- csound docs: <http://csound.github.io/docs/manual/pvs2tab.html>
pvs2tab :: SpecArr -> Spec -> SE Sig
pvs2tab :: Arr Sig Sig -> Spec -> SE Sig
pvs2tab = Text -> (Rate, [Rate]) -> Arr Sig Sig -> Spec -> SE Sig
forall b c d a.
(Tuple b, Tuple c, Tuple d) =>
Text -> (Rate, [Rate]) -> Arr a b -> c -> SE d
extractWith Text
"pvs2tab" (Rate
Kr, [Rate
Xr, Rate
Fr])

-- | Copies spectral data from k-rate arrays (or t-variables.). Also known as pvsfromarray.
--
-- csound docs: <http://csound.github.io/docs/manual/tab2pvs.html>
tab2pvs :: SpecArr -> SE Spec
tab2pvs :: Arr Sig Sig -> SE Spec
tab2pvs = Rate -> Text -> Arr Sig Sig -> SE Spec
forall b c a. (Tuple b, Tuple c) => Rate -> Text -> Arr a b -> SE c
extract1 Rate
Fr Text
"tab2pvs"

-- | Complex product of two arrays.
--
-- > kout[] cmplxprod kin1[], kin2[]
--
-- csound docs: <http://csound.github.io/docs/manual/cmplxprod.html>
cmplxprodNew :: SpecArr -> SpecArr -> SE SpecArr
cmplxprodNew :: Arr Sig Sig -> Arr Sig Sig -> SE (Arr Sig Sig)
cmplxprodNew = Text -> Arr Sig Sig -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
convert2 Text
"cmplxprod"

-- |  Rectangular to polar format conversion.
--
-- > kout[] rect2pol kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/rect2pol.html>
rect2polNew :: SpecArr -> SE SpecArr
rect2polNew :: Arr Sig Sig -> SE (Arr Sig Sig)
rect2polNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"rect2pol"

-- | Polar to rectangular format conversion.
--
-- > kout[] pol2rect kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/pol2rect.html>
pol2rectNew :: SpecArr -> SE SpecArr
pol2rectNew :: Arr Sig Sig -> SE (Arr Sig Sig)
pol2rectNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"pol2rect"


-- | Polar to rectangular format conversion.
--
-- > kout[] pol2rect kmags[], kphs[]
--
-- csound docs: <http://csound.github.io/docs/manual/pol2rect.html>
pol2rect2New :: SpecArr -> SpecArr -> SE SpecArr
pol2rect2New :: Arr Sig Sig -> Arr Sig Sig -> SE (Arr Sig Sig)
pol2rect2New = Text -> Arr Sig Sig -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
convert2 Text
"pol2rect"

-- | Applies a window to an array.
--
-- > kout[] window kin[][, koff, itype]
--
-- csound docs: <http://csound.github.io/docs/manual/window.html>
windowArrayNew :: SpecArr -> SE SpecArr
windowArrayNew :: Arr Sig Sig -> SE (Arr Sig Sig)
windowArrayNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"window"

-- | Real to complex format conversion.
--
-- > kout[] r2c kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/r2c.html>
r2cNew :: SpecArr -> SE SpecArr
r2cNew :: Arr Sig Sig -> SE (Arr Sig Sig)
r2cNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"r2c"

-- | Complex to real format conversion.
--
-- > kout[] c2r kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/c2r.html>
c2rNew :: SpecArr -> SE SpecArr
c2rNew :: Arr Sig Sig -> SE (Arr Sig Sig)
c2rNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"c2r"

-- | Obtains the magnitudes of a complex-number array
--
-- > kout[] mags kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/mags.html>
magsArrayNew :: SpecArr -> SE SpecArr
magsArrayNew :: Arr Sig Sig -> SE (Arr Sig Sig)
magsArrayNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"mags"

-- | Obtains the phases of a complex-number array
--
-- kout[] phs kin[]
--
-- > csound docs: <http://csound.github.io/docs/manual/phs.html>
phsArrayNew :: SpecArr -> SE SpecArr
phsArrayNew :: Arr Sig Sig -> SE (Arr Sig Sig)
phsArrayNew = Text -> Arr Sig Sig -> SE (Arr Sig Sig)
forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
"phs"

-----------------------------

isArrayInit, noArrayInit :: Bool

isArrayInit :: IsArrInit
isArrayInit = IsArrInit
True
noArrayInit :: IsArrInit
noArrayInit = IsArrInit
False

binOp :: Text -> Arr a b -> Arr a b -> SE (Arr a b)
binOp :: forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
binOp Text
name (Arr [Var]
xs) (Arr [Var]
ys) = ([Var] -> Arr a b) -> SE [Var] -> SE (Arr a b)
forall a b. (a -> b) -> SE a -> SE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Var] -> Arr a b
forall ix a. [Var] -> Arr ix a
Arr (SE [Var] -> SE (Arr a b)) -> SE [Var] -> SE (Arr a b)
forall a b. (a -> b) -> a -> b
$ (Var -> Var -> SE Var) -> [Var] -> [Var] -> SE [Var]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Var -> Var -> SE Var
go [Var]
xs [Var]
ys
    where
        go :: Var -> Var -> SE Var
go Var
x Var
y = DepT GE Var -> SE Var
forall a. Dep a -> SE a
SE (DepT GE Var -> SE Var) -> DepT GE Var -> SE Var
forall a b. (a -> b) -> a -> b
$ do
            Var
outVar <- SE Var -> DepT GE Var
forall a. SE a -> Dep a
unSE (SE Var -> DepT GE Var) -> SE Var -> DepT GE Var
forall a b. (a -> b) -> a -> b
$ Rate -> SE Var
newTmpArrVar (Var -> Rate
varRate Var
x)
            IsArrInit -> Var -> Text -> E -> E -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> E -> E -> DepT m ()
infOprArr IsArrInit
isArrayInit Var
outVar Text
name (Var -> E
inlineVar Var
x) (Var -> E
inlineVar Var
y)
            Var -> DepT GE Var
forall a. a -> DepT GE a
forall (m :: * -> *) a. Monad m => a -> m a
return Var
outVar

convert :: Text -> Arr a b -> SE (Arr a b)
convert :: forall a b. Text -> Arr a b -> SE (Arr a b)
convert Text
name (Arr [Var]
vars) = ([Var] -> Arr a b) -> SE [Var] -> SE (Arr a b)
forall a b. (a -> b) -> SE a -> SE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Var] -> Arr a b
forall ix a. [Var] -> Arr ix a
Arr (SE [Var] -> SE (Arr a b)) -> SE [Var] -> SE (Arr a b)
forall a b. (a -> b) -> a -> b
$ (Var -> SE Var) -> [Var] -> SE [Var]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Var -> SE Var
go [Var]
vars
    where
        go :: Var -> SE Var
go Var
v = DepT GE Var -> SE Var
forall a. Dep a -> SE a
SE (DepT GE Var -> SE Var) -> DepT GE Var -> SE Var
forall a b. (a -> b) -> a -> b
$ do
            Var
outVar <- SE Var -> DepT GE Var
forall a. SE a -> Dep a
unSE (SE Var -> DepT GE Var) -> SE Var -> DepT GE Var
forall a b. (a -> b) -> a -> b
$ Rate -> SE Var
newTmpArrVar (Var -> Rate
varRate Var
v)
            IsArrInit -> Var -> Text -> Spec1 -> [E] -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
isArrayInit Var
outVar Text
name Spec1
idRate1 [Var -> E
inlineVar Var
v]
            Var -> DepT GE Var
forall a. a -> DepT GE a
forall (m :: * -> *) a. Monad m => a -> m a
return Var
outVar

        idRate1 :: Spec1
idRate1 = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
r -> (Rate
r, [Rate
r])) [Rate
Kr, Rate
Ar, Rate
Ir, Rate
Sr, Rate
Fr]

convert2 :: Text -> Arr a b -> Arr a b -> SE (Arr a b)
convert2 :: forall a b. Text -> Arr a b -> Arr a b -> SE (Arr a b)
convert2 Text
name (Arr [Var]
xs) (Arr [Var]
ys) = ([Var] -> Arr a b) -> SE [Var] -> SE (Arr a b)
forall a b. (a -> b) -> SE a -> SE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Var] -> Arr a b
forall ix a. [Var] -> Arr ix a
Arr (SE [Var] -> SE (Arr a b)) -> SE [Var] -> SE (Arr a b)
forall a b. (a -> b) -> a -> b
$ (Var -> Var -> SE Var) -> [Var] -> [Var] -> SE [Var]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Var -> Var -> SE Var
go [Var]
xs [Var]
ys
    where
        go :: Var -> Var -> SE Var
go Var
x Var
y = DepT GE Var -> SE Var
forall a. Dep a -> SE a
SE (DepT GE Var -> SE Var) -> DepT GE Var -> SE Var
forall a b. (a -> b) -> a -> b
$ do
            Var
outVar <- SE Var -> DepT GE Var
forall a. SE a -> Dep a
unSE (SE Var -> DepT GE Var) -> SE Var -> DepT GE Var
forall a b. (a -> b) -> a -> b
$ Rate -> SE Var
newTmpArrVar (Var -> Rate
varRate Var
x)
            IsArrInit -> Var -> Text -> Spec1 -> [E] -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
isArrayInit Var
outVar Text
name Spec1
idRate2 [Var -> E
inlineVar Var
x, Var -> E
inlineVar Var
y]
            Var -> DepT GE Var
forall a. a -> DepT GE a
forall (m :: * -> *) a. Monad m => a -> m a
return Var
outVar

        idRate2 :: Spec1
idRate2 = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
r -> (Rate
r, [Rate
r, Rate
r])) [Rate
Kr, Rate
Ar, Rate
Ir, Rate
Sr, Rate
Fr]

extractArray :: (Tuple b) => Text -> Arr a b -> SE b
extractArray :: forall b a. Tuple b => Text -> Arr a b -> SE b
extractArray Text
name (Arr [Var]
vs) = Dep b -> SE b
forall a. Dep a -> SE a
SE (Dep b -> SE b) -> Dep b -> SE b
forall a b. (a -> b) -> a -> b
$ ([E] -> b) -> Dep [E] -> Dep b
forall a b. (a -> b) -> DepT GE a -> DepT GE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GE [E] -> b
forall a. Tuple a => GE [E] -> a
toTuple (GE [E] -> b) -> ([E] -> GE [E]) -> [E] -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [E] -> GE [E]
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return) (Dep [E] -> Dep b) -> Dep [E] -> Dep b
forall a b. (a -> b) -> a -> b
$ (Var -> DepT GE E) -> [Var] -> Dep [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (E -> DepT GE E
forall {m :: * -> *}. Monad m => E -> DepT m E
f (E -> DepT GE E) -> (Var -> E) -> Var -> DepT GE E
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> E
inlineVar) [Var]
vs
    where f :: E -> DepT m E
f E
a = E -> DepT m E
forall {m :: * -> *}. Monad m => E -> DepT m E
depT (E -> DepT m E) -> E -> DepT m E
forall a b. (a -> b) -> a -> b
$ Text -> Spec1 -> [E] -> E
opcs Text
name [(Rate
Xr, [Rate
Xr])] [E
a]

extract1 :: (Tuple b, Tuple c) => Rate -> Text -> Arr a b -> SE c
extract1 :: forall b c a. (Tuple b, Tuple c) => Rate -> Text -> Arr a b -> SE c
extract1 Rate
rate Text
name (Arr [Var]
vs) = Dep c -> SE c
forall a. Dep a -> SE a
SE (Dep c -> SE c) -> Dep c -> SE c
forall a b. (a -> b) -> a -> b
$ ([E] -> c) -> Dep [E] -> Dep c
forall a b. (a -> b) -> DepT GE a -> DepT GE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GE [E] -> c
forall a. Tuple a => GE [E] -> a
toTuple (GE [E] -> c) -> ([E] -> GE [E]) -> [E] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [E] -> GE [E]
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return) (Dep [E] -> Dep c) -> Dep [E] -> Dep c
forall a b. (a -> b) -> a -> b
$ (Var -> DepT GE E) -> [Var] -> Dep [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (E -> DepT GE E
forall {m :: * -> *}. Monad m => E -> DepT m E
f (E -> DepT GE E) -> (Var -> E) -> Var -> DepT GE E
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> E
inlineVar) [Var]
vs
    where f :: E -> DepT m E
f E
a = E -> DepT m E
forall {m :: * -> *}. Monad m => E -> DepT m E
depT (E -> DepT m E) -> E -> DepT m E
forall a b. (a -> b) -> a -> b
$ Text -> Spec1 -> [E] -> E
opcs Text
name [(Rate
rate, [Rate
Xr])] [E
a]

extractWith :: (Tuple b, Tuple c, Tuple d) => Text -> (Rate, [Rate]) -> Arr a b -> c -> SE d
extractWith :: forall b c d a.
(Tuple b, Tuple c, Tuple d) =>
Text -> (Rate, [Rate]) -> Arr a b -> c -> SE d
extractWith Text
name (Rate, [Rate])
rates (Arr [Var]
vs) c
argument = Dep d -> SE d
forall a. Dep a -> SE a
SE (Dep d -> SE d) -> Dep d -> SE d
forall a b. (a -> b) -> a -> b
$ ([E] -> d) -> Dep [E] -> Dep d
forall a b. (a -> b) -> DepT GE a -> DepT GE b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GE [E] -> d
forall a. Tuple a => GE [E] -> a
toTuple (GE [E] -> d) -> ([E] -> GE [E]) -> [E] -> d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [E] -> GE [E]
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return) (Dep [E] -> Dep d) -> Dep [E] -> Dep d
forall a b. (a -> b) -> a -> b
$ GE (Dep [E]) -> Dep [E]
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep [E]) -> Dep [E]) -> GE (Dep [E]) -> Dep [E]
forall a b. (a -> b) -> a -> b
$ do
        [E]
argExps <- c -> GE [E]
forall a. Tuple a => a -> GE [E]
fromTuple c
argument
        Dep [E] -> GE (Dep [E])
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep [E] -> GE (Dep [E])) -> Dep [E] -> GE (Dep [E])
forall a b. (a -> b) -> a -> b
$ (Var -> E -> DepT GE E) -> [Var] -> [E] -> Dep [E]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (\Var
var E
x -> E -> E -> DepT GE E
forall {m :: * -> *}. Monad m => E -> E -> DepT m E
f (Var -> E
inlineVar Var
var) E
x) [Var]
vs [E]
argExps
    where f :: E -> E -> DepT m E
f E
a E
b = E -> DepT m E
forall {m :: * -> *}. Monad m => E -> DepT m E
depT (E -> DepT m E) -> E -> DepT m E
forall a b. (a -> b) -> a -> b
$ Text -> Spec1 -> [E] -> E
opcs Text
name [(Rate, [Rate])
rates] [E
a, E
b]

---------------------------------------------------
-- opcodes with copy

-- | Transforms the dta of the array and copies it to the second array.
maparrayCopy :: Arr a b -> Str -> Arr a b -> SE ()
maparrayCopy :: forall a b. Arr a b -> Str -> Arr a b -> SE ()
maparrayCopy (Arr [Var]
vs) Str
s (Arr [Var]
outs) = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ GE (Dep ()) -> Dep ()
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep ()) -> Dep ()) -> GE (Dep ()) -> Dep ()
forall a b. (a -> b) -> a -> b
$ do
    E
strExp <- Str -> GE E
forall a. Val a => a -> GE E
toGE Str
s
    Dep () -> GE (Dep ())
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep () -> GE (Dep ())) -> Dep () -> GE (Dep ())
forall a b. (a -> b) -> a -> b
$ (Var -> Var -> Dep ()) -> [Var] -> [Var] -> Dep ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ (\Var
var Var
outVar -> Var -> E -> Var -> Dep ()
forall {m :: * -> *}. Monad m => Var -> E -> Var -> DepT m ()
go Var
var E
strExp Var
outVar) [Var]
vs [Var]
outs
    where
        go :: Var -> E -> Var -> DepT m ()
go Var
var E
strExp Var
outVar = IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
noArrayInit Var
outVar Text
"slicearray" Spec1
idRate [Var -> E
inlineVar Var
var, E
strExp]
        idRate :: Spec1
idRate = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
rate -> (Rate
rate, [Rate
rate, Rate
Ir, Rate
Ir])) [Rate
Ir, Rate
Kr, Rate
Ar]

-- | Copies a part of array to another array.
slicearrayCopy :: Arr D a -> (D, D) -> Arr D a -> SE ()
slicearrayCopy :: forall a. Arr D a -> (D, D) -> Arr D a -> SE ()
slicearrayCopy (Arr [Var]
vs) (D
from, D
to) (Arr [Var]
outs) = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ GE (Dep ()) -> Dep ()
forall a. GE (Dep a) -> Dep a
hideGEinDep (GE (Dep ()) -> Dep ()) -> GE (Dep ()) -> Dep ()
forall a b. (a -> b) -> a -> b
$ do
    E
fromExpr <- D -> GE E
forall a. Val a => a -> GE E
toGE D
from
    E
toExpr   <- D -> GE E
forall a. Val a => a -> GE E
toGE D
to
    Dep () -> GE (Dep ())
forall a. a -> GE a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dep () -> GE (Dep ())) -> Dep () -> GE (Dep ())
forall a b. (a -> b) -> a -> b
$ (Var -> Var -> Dep ()) -> [Var] -> [Var] -> Dep ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ (\Var
var Var
outVar -> Var -> (E, E) -> Var -> Dep ()
forall {m :: * -> *}. Monad m => Var -> (E, E) -> Var -> DepT m ()
go Var
var (E
fromExpr, E
toExpr) Var
outVar) [Var]
vs [Var]
outs
    where
        go :: Var -> (E, E) -> Var -> DepT m ()
go Var
var (E
fromExpr, E
toExpr) Var
outVar = IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
noArrayInit Var
outVar Text
"slicearray" Spec1
idRate [Var -> E
inlineVar Var
var, E
fromExpr, E
toExpr]
        idRate :: Spec1
idRate = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
rate -> (Rate
rate, [Rate
rate, Rate
Ir, Rate
Ir])) [Rate
Ir, Rate
Kr, Rate
Ar]

-- | Multiplies two arrays and copies the result into third array.
mulArrayCopy :: (Tuple b, Num b) => Arr a b -> Arr a b -> Arr a b -> SE ()
mulArrayCopy :: forall b a.
(Tuple b, Num b) =>
Arr a b -> Arr a b -> Arr a b -> SE ()
mulArrayCopy = Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
binOpCopy Text
"*"

-- | Summs two arrays and copies the result into third array.
addArrayCopy :: (Tuple b, Num b) => Arr a b -> Arr a b -> Arr a b -> SE ()
addArrayCopy :: forall b a.
(Tuple b, Num b) =>
Arr a b -> Arr a b -> Arr a b -> SE ()
addArrayCopy = Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
binOpCopy Text
"+"

-- | Substracts two arrays and copies the result into third array.
subArrayCopy :: (Tuple b, Num b) => Arr a b -> Arr a b -> Arr a b -> SE ()
subArrayCopy :: forall b a.
(Tuple b, Num b) =>
Arr a b -> Arr a b -> Arr a b -> SE ()
subArrayCopy = Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
binOpCopy Text
"-"

-- | Divides two arrays and copies the result into third array.
divArrayCopy :: (Tuple b, Num b) => Arr a b -> Arr a b -> Arr a b -> SE ()
divArrayCopy :: forall b a.
(Tuple b, Num b) =>
Arr a b -> Arr a b -> Arr a b -> SE ()
divArrayCopy = Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
binOpCopy Text
"/"

-- spectral opcodes


-- |  Complex-to-complex Fast Fourier Transform.
--
-- csound docs: <http://csound.github.io/docs/manual/fft.html>
fftCopy :: SpecArr -> SpecArr -> SE ()
fftCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
fftCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"fft"

-- | Complex-to-complex Inverse Fast Fourier Transform.
--
-- csound docs: <http://csound.github.io/docs/manual/fftinv.html>
fftinvCopy :: SpecArr -> SpecArr -> SE ()
fftinvCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
fftinvCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"fftinvi"

-- | Fast Fourier Transform of a real-value array.
--
-- csound docs: <http://csound.github.io/docs/manual/rfft.html>
rfftCopy :: SpecArr -> SpecArr -> SE ()
rfftCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
rfftCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"rfft"

-- | Complex-to-real Inverse Fast Fourier Transform.
--
-- csound docs: <http://csound.github.io/docs/manual/rifft.html>
rifftCopy :: SpecArr -> SpecArr -> SE ()
rifftCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
rifftCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"rifft"


-- | Complex product of two arrays.
--
-- > kout[] cmplxprod kin1[], kin2[]
--
-- csound docs: <http://csound.github.io/docs/manual/cmplxprod.html>
cmplxprodCopy :: SpecArr -> SpecArr -> SpecArr -> SE ()
cmplxprodCopy :: Arr Sig Sig -> Arr Sig Sig -> Arr Sig Sig -> SE ()
cmplxprodCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
convert2Copy Text
"cmplxprod"

-- |  Rectangular to polar format conversion.
--
-- > kout[] rect2pol kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/rect2pol.html>
rect2polCopy :: SpecArr -> SpecArr -> SE ()
rect2polCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
rect2polCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"rect2pol"

-- | Polar to rectangular format conversion.
--
-- > kout[] pol2rect kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/pol2rect.html>
pol2rectCopy :: SpecArr -> SpecArr -> SE ()
pol2rectCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
pol2rectCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"pol2rect"

-- | Polar to rectangular format conversion.
--
-- > kout[] pol2rect kmags[], kphs[]
--
-- csound docs: <http://csound.github.io/docs/manual/pol2rect.html>
pol2rect2Copy :: SpecArr -> SpecArr -> SpecArr -> SE ()
pol2rect2Copy :: Arr Sig Sig -> Arr Sig Sig -> Arr Sig Sig -> SE ()
pol2rect2Copy = Text -> Arr Sig Sig -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
convert2Copy Text
"pol2rect2"

-- | Applies a window to an array.
--
-- > kout[] window kin[][, koff, itype]
--
-- csound docs: <http://csound.github.io/docs/manual/window.html>
windowArrayCopy :: SpecArr -> SpecArr -> SE ()
windowArrayCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
windowArrayCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"window"

-- | Real to complex format conversion.
--
-- > kout[] r2c kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/r2c.html>
r2cCopy :: SpecArr -> SpecArr -> SE ()
r2cCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
r2cCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"r2c"

-- | Complex to real format conversion.
--
-- > kout[] c2r kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/c2r.html>
c2rCopy :: SpecArr -> SpecArr -> SE ()
c2rCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
c2rCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"c2r"


-- | Obtains the magnitudes of a complex-number array
--
-- > kout[] mags kin[]
--
-- csound docs: <http://csound.github.io/docs/manual/mags.html>
magsArrayCopy :: SpecArr -> SpecArr -> SE ()
magsArrayCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
magsArrayCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"mags"

-- | Obtains the phases of a complex-number array
--
-- kout[] phs kin[]
--
-- > csound docs: <http://csound.github.io/docs/manual/phs.html>
phsArrayCopy :: SpecArr -> SpecArr -> SE ()
phsArrayCopy :: Arr Sig Sig -> Arr Sig Sig -> SE ()
phsArrayCopy = Text -> Arr Sig Sig -> Arr Sig Sig -> SE ()
forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
"phs"

---------------------------------------------------------------

binOpCopy :: Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
binOpCopy :: forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
binOpCopy Text
name (Arr [Var]
xs) (Arr [Var]
ys) (Arr [Var]
outs) = ((Var, Var, Var) -> SE ()) -> [(Var, Var, Var)] -> SE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Var, Var, Var) -> SE ()
go ([(Var, Var, Var)] -> SE ()) -> [(Var, Var, Var)] -> SE ()
forall a b. (a -> b) -> a -> b
$ [Var] -> [Var] -> [Var] -> [(Var, Var, Var)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Var]
xs [Var]
ys [Var]
outs
    where
        go :: (Var, Var, Var) -> SE ()
go (Var
x, Var
y, Var
outVar) = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ IsArrInit -> Var -> Text -> E -> E -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> E -> E -> DepT m ()
infOprArr IsArrInit
noArrayInit Var
outVar Text
name (Var -> E
inlineVar Var
x) (Var -> E
inlineVar Var
y)

convertCopy :: Text -> Arr a b -> Arr a b -> SE ()
convertCopy :: forall a b. Text -> Arr a b -> Arr a b -> SE ()
convertCopy Text
name (Arr [Var]
vars) (Arr [Var]
outs) = (Var -> Var -> SE ()) -> [Var] -> [Var] -> SE ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ Var -> Var -> SE ()
go [Var]
vars [Var]
outs
    where
        go :: Var -> Var -> SE ()
go Var
v Var
outVar = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ IsArrInit -> Var -> Text -> Spec1 -> [E] -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
noArrayInit Var
outVar Text
name Spec1
idRate1 [Var -> E
inlineVar Var
v]
        idRate1 :: Spec1
idRate1 = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
r -> (Rate
r, [Rate
r])) [Rate
Kr, Rate
Ar, Rate
Ir, Rate
Sr, Rate
Fr]

convert2Copy :: Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
convert2Copy :: forall a b. Text -> Arr a b -> Arr a b -> Arr a b -> SE ()
convert2Copy Text
name (Arr [Var]
xs) (Arr [Var]
ys) (Arr [Var]
outs) = ((Var, Var, Var) -> SE ()) -> [(Var, Var, Var)] -> SE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Var, Var, Var) -> SE ()
go ([(Var, Var, Var)] -> SE ()) -> [(Var, Var, Var)] -> SE ()
forall a b. (a -> b) -> a -> b
$ [Var] -> [Var] -> [Var] -> [(Var, Var, Var)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Var]
xs [Var]
ys [Var]
outs
    where
        go :: (Var, Var, Var) -> SE ()
go (Var
x, Var
y, Var
outVar) = Dep () -> SE ()
forall a. Dep a -> SE a
SE (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ IsArrInit -> Var -> Text -> Spec1 -> [E] -> Dep ()
forall (m :: * -> *).
Monad m =>
IsArrInit -> Var -> Text -> Spec1 -> [E] -> DepT m ()
opcsArr IsArrInit
noArrayInit Var
outVar Text
name Spec1
idRate2 [Var -> E
inlineVar Var
x, Var -> E
inlineVar Var
y]
        idRate2 :: Spec1
idRate2 = (Rate -> (Rate, [Rate])) -> [Rate] -> Spec1
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Rate
r -> (Rate
r, [Rate
r, Rate
r])) [Rate
Kr, Rate
Ar, Rate
Ir, Rate
Sr, Rate
Fr]