module Csound.Typed.Control.Mix(
Mix,
sco, eff, mix, mixBy,
sco_, mix_, mixBy_,
CsdSco(..), CsdEventList(..), CsdEvent
) where
import Control.Monad.IO.Class
import Data.Traversable
import System.Mem.StableName
import Csound.Dynamic hiding (Instr)
import qualified Csound.Typed.GlobalState.Elements as C
import Csound.Typed.Types
import Csound.Typed.Types.MixSco
import Csound.Typed.GlobalState hiding (notes)
import Csound.Typed.Control.Instr
newtype Mix a = Mix { unMix :: GE M }
wrapSco :: (CsdSco f) => f a -> (CsdEventList a -> GE M) -> f (Mix b)
wrapSco notes getContent = singleCsdEvent (0, csdEventListDur evts, Mix $ getContent evts)
where evts = toCsdEventList notes
sco :: (CsdSco f, Arg a, Sigs b) => (a -> SE b) -> f a -> f (Mix b)
sco instr notes = wrapSco notes $ \events -> do
events' <- traverse toNote events
cacheName <- liftIO $ C.makeCacheName instr
instrId <- saveSourceInstrCached cacheName (funArity instr) (insExp instr)
return $ Snd instrId events'
sco_ :: (CsdSco f, Arg a) => (a -> SE ()) -> f a -> f (Mix Unit)
sco_ instr notes = wrapSco notes $ \events -> do
events' <- traverse toNote events
cacheName <- liftIO $ C.makeCacheName instr
instrId <- saveSourceInstrCached_ cacheName (unitExp $ fmap (const unit) $ instr toArg)
return $ Snd instrId events'
eff :: (CsdSco f, Sigs a, Sigs b) => (a -> SE b) -> f (Mix a) -> f (Mix b)
eff ef sigs = wrapSco sigs $ \events -> do
notes <- traverse unMix events
instrId <- saveEffectInstr (funArity ef) (effExp ef)
return $ Eff instrId notes (arityIns $ funArity ef)
mix :: (Sigs a, CsdSco f) => f (Mix a) -> a
mix a = flip apInstr unit $ do
key <- mixKey a
withCache (NumDur $ csdEventListDur a') getMixKey saveMixKey key $
saveMixInstr (mixArity a) =<< toEventList a'
where a' = toCsdEventList a
mixBy :: (Arg a, Sigs b, CsdSco f) => (a -> f (Mix b)) -> (a -> b)
mixBy evts args = flip apInstr args $ do
key <- mixKey evts
withCache (NumDur $ csdEventListDur evts') getMixKey saveMixKey key $
saveMixInstr (mixArityFun evts) =<< (toEventList evts')
where evts' = toCsdEventList $ evts toArg
mix_ :: (CsdSco f) => f (Mix Unit) -> SE ()
mix_ a = fromDep_ $ hideGEinDep $ do
key <- mixKey a
withCache (NumDur $ csdEventListDur a') getMixProcKey saveMixProcKey key $
saveMixInstr_ =<< toEventList a'
where a' = toCsdEventList a
mixBy_ :: (Arg a, CsdSco f) => (a -> f (Mix Unit)) -> (a -> SE ())
mixBy_ evts args = mix_ $ evts args
mixKey :: a -> GE MixKey
mixKey = liftIO . fmap (MixKey . hashStableName) . makeStableName
toEventList :: (CsdSco f) => f (Mix a) -> GE (CsdEventList M)
toEventList evts = fmap delayAndRescaleCsdEventListM $ traverse unMix $ toCsdEventList $ evts
mixArity :: Sigs b => f (Mix b) -> Int
mixArity = tupleArity . proxy
where
proxy :: f (Mix b) -> b
proxy = const undefined
mixArityFun :: Sigs b => (a -> f (Mix b)) -> Int
mixArityFun = tupleArity . proxy
where
proxy :: (a -> f (Mix b)) -> b
proxy = const undefined