module Csound.Typed.Control.SERef where
import Control.DeepSeq(deepseq)
import Control.Monad
import Control.Monad.Trans.Class
import Csound.Dynamic hiding (newLocalVars)
import Csound.Typed.Types.Tuple
import Csound.Typed.GlobalState
newtype SERef a = SERef [Var]
writeSERef :: Tuple a => SERef a -> a -> SE ()
writeSERef (SERef vars) a = fromDep_ $ hideGEinDep $ do
vals <- fromTuple a
return $ zipWithM_ writeVar vars vals
readSERef :: Tuple a => SERef a -> SE a
readSERef (SERef vars) = SE $ fmap (toTuple . return) $ mapM readVar vars
newSERef :: Tuple a => a -> SE (SERef a)
newSERef t = fmap SERef $ newLocalVars (tupleRates t) (fromTuple t)
mixSERef :: (Num a, Tuple a) => SERef a -> a -> SE ()
mixSERef ref asig = modifySERef ref (+ asig)
modifySERef :: Tuple a => SERef a -> (a -> a) -> SE ()
modifySERef ref f = do
v <- readSERef ref
writeSERef ref (f v)
sensorsSE :: Tuple a => a -> SE (SE a, a -> SE ())
sensorsSE a = do
ref <- newSERef a
return $ (readSERef ref, writeSERef ref)
newGlobalSERef :: Tuple a => a -> SE (SERef a)
newGlobalSERef t = fmap SERef $ newGlobalVars (tupleRates t) (fromTuple t)
globalSensorsSE :: Tuple a => a -> SE (SE a, a -> SE ())
globalSensorsSE a = do
ref <- newSERef a
return $ (readSERef ref, writeSERef ref)