module Sound.Sc3.Server.Scsynth where
import Data.IORef
import qualified Sound.Osc.Packet as Osc
import Sound.Sc3.Ugen.Ugen
import Sound.Sc3.Ugen.Util
import Sound.Sc3.Server.Transport.Monad
data Scsynth = Scsynth {Scsynth -> IORef [Message]
scsynthResetMessages :: IORef [Osc.Message]}
newScsynth :: IO Scsynth
newScsynth :: IO Scsynth
newScsynth = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap IORef [Message] -> Scsynth
Scsynth (forall a. a -> IO (IORef a)
newIORef [])
scsynthPrint :: Scsynth -> IO ()
scsynthPrint :: Scsynth -> IO ()
scsynthPrint (Scsynth IORef [Message]
mRef) = do
[Message]
m <- forall a. IORef a -> IO a
readIORef IORef [Message]
mRef
forall a. Show a => a -> IO ()
print [Message]
m
scsynthOnReset :: Scsynth -> [Osc.Message] -> IO ()
scsynthOnReset :: Scsynth -> [Message] -> IO ()
scsynthOnReset (Scsynth IORef [Message]
mRef) [Message]
messages =
if Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Message]
messages)
then forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef [Message]
mRef (forall a. [a] -> [a] -> [a]
++ [Message]
messages)
else forall (m :: * -> *) a. Monad m => a -> m a
return ()
scsynthReset :: Scsynth -> IO ()
scsynthReset :: Scsynth -> IO ()
scsynthReset (Scsynth IORef [Message]
mRef) = do
[Message]
onResetMessages <- forall a. IORef a -> IO a
readIORef IORef [Message]
mRef
forall a. IORef a -> a -> IO ()
writeIORef IORef [Message]
mRef []
forall a. Connection Udp a -> IO a
withSc3 (forall (m :: * -> *). SendOsc m => m ()
reset forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall (m :: * -> *). DuplexOsc m => Message -> m ()
maybe_async [Message]
onResetMessages)
scsynthPlayAt :: Scsynth -> Play_Opt -> Ugen -> IO ()
scsynthPlayAt :: Scsynth -> Play_Opt -> Ugen -> IO ()
scsynthPlayAt Scsynth
scsynth Play_Opt
opt Ugen
ugen = do
let ([Message]
pre, [Message]
post) = Ugen -> ([Message], [Message])
ugenCollectBrackets Ugen
ugen
if Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Message]
post) then Scsynth -> [Message] -> IO ()
scsynthOnReset Scsynth
scsynth [Message]
post else forall (m :: * -> *) a. Monad m => a -> m a
return ()
forall a. Connection Udp a -> IO a
withSc3 (forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall (m :: * -> *). DuplexOsc m => Message -> m ()
maybe_async [Message]
pre forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall e (m :: * -> *).
(Audible e, Transport m) =>
Play_Opt -> e -> m ()
playAt Play_Opt
opt Ugen
ugen)
scsynthPlay :: Scsynth -> Ugen -> IO ()
scsynthPlay :: Scsynth -> Ugen -> IO ()
scsynthPlay Scsynth
scsynth = Scsynth -> Play_Opt -> Ugen -> IO ()
scsynthPlayAt Scsynth
scsynth Play_Opt
def_play_opt