-- | Named channels.
--
-- With named channels we can read and 
-- write values to the variables with dynamic names.
-- We can specify the variable with string (Str).
--
-- Csound has an C api wich is ported to many languages.
-- With named channels we can interact with csound
-- that runns a program. We can read and write to named channels
-- from another program.
module Csound.Typed.Control.Channel(
    -- * Getters
    chnGetD, chnGetSig, chnGetCtrl, chnGetStr,

    -- * Setters
    chnSetD, chnSetSig, chnSetCtrl, chnSetStr
) where

import Control.Monad.Trans.Class

import Csound.Dynamic(Rate(..))
import Csound.Typed.Types
import Csound.Typed.GlobalState

-- getters

-- | Reads a value of type double.
chnGetD :: Str -> SE D
chnGetD :: Str -> SE D
chnGetD = Rate -> Str -> SE D
forall a. Val a => Rate -> Str -> SE a
get Rate
Ir

-- | Reads a control signal. 
-- The control signals are updated at 
-- the lower rate. 
chnGetCtrl :: Str -> SE Sig
chnGetCtrl :: Str -> SE Sig
chnGetCtrl = Rate -> Str -> SE Sig
forall a. Val a => Rate -> Str -> SE a
get Rate
Kr

-- | Reads an audio signal.
chnGetSig :: Str -> SE Sig
chnGetSig :: Str -> SE Sig
chnGetSig = Rate -> Str -> SE Sig
forall a. Val a => Rate -> Str -> SE a
get Rate
Ar

-- | Reads a string.
chnGetStr :: Str -> SE Str
chnGetStr :: Str -> SE Str
chnGetStr = Rate -> Str -> SE Str
forall a. Val a => Rate -> Str -> SE a
get Rate
Sr

-- setters


-- | Writes a value of type double.
chnSetD :: D -> Str -> SE ()
chnSetD :: D -> Str -> SE ()
chnSetD = Rate -> D -> Str -> SE ()
forall a. Val a => Rate -> a -> Str -> SE ()
set Rate
Ir

-- | Writes an audio signal.
chnSetSig :: Sig -> Str -> SE ()
chnSetSig :: Sig -> Str -> SE ()
chnSetSig = Rate -> Sig -> Str -> SE ()
forall a. Val a => Rate -> a -> Str -> SE ()
set Rate
Ar

-- | Writes a control signal. 
-- The control signals are updated at 
-- the lower rate. 
chnSetCtrl :: Sig -> Str -> SE ()
chnSetCtrl :: Sig -> Str -> SE ()
chnSetCtrl = Rate -> Sig -> Str -> SE ()
forall a. Val a => Rate -> a -> Str -> SE ()
set Rate
Kr

-- | Writes a string.
chnSetStr :: Str -> Str -> SE ()
chnSetStr :: Str -> Str -> SE ()
chnSetStr = Rate -> Str -> Str -> SE ()
forall a. Val a => Rate -> a -> Str -> SE ()
set Rate
Sr

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

get :: Val a => Rate -> Str -> SE a
get :: Rate -> Str -> SE a
get Rate
rate Str
chn = (GE E -> a) -> SE (GE 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 (SE (GE E) -> SE a) -> SE (GE E) -> SE a
forall a b. (a -> b) -> a -> b
$ Dep E -> SE (GE E)
forall a. Dep a -> SE (GE a)
fromDep (Dep E -> SE (GE E)) -> Dep E -> SE (GE E)
forall a b. (a -> b) -> a -> b
$ (Rate -> E -> Dep E
forall (m :: * -> *). Monad m => Rate -> E -> DepT m E
chnGet Rate
rate) (E -> Dep E) -> Dep E -> Dep E
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (GE E -> Dep E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (GE E -> Dep E) -> GE E -> Dep E
forall a b. (a -> b) -> a -> b
$ Str -> GE E
unStr Str
chn)

set :: Val a => Rate -> a -> Str -> SE ()
set :: Rate -> a -> Str -> SE ()
set Rate
rate a
val Str
chn = Dep () -> SE ()
fromDep_ (Dep () -> SE ()) -> Dep () -> SE ()
forall a b. (a -> b) -> a -> b
$ do
    E
v <- GE E -> Dep E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (GE E -> Dep E) -> GE E -> Dep E
forall a b. (a -> b) -> a -> b
$ a -> GE E
forall a. Val a => a -> GE E
toGE a
val
    E
c <- GE E -> Dep E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (GE E -> Dep E) -> GE E -> Dep E
forall a b. (a -> b) -> a -> b
$ Str -> GE E
unStr Str
chn
    Rate -> E -> E -> Dep ()
forall (m :: * -> *). Monad m => Rate -> E -> E -> DepT m ()
chnSet Rate
rate E
v E
c