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 = (IORef [Message] -> Scsynth) -> IO (IORef [Message]) -> IO Scsynth
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap IORef [Message] -> Scsynth
Scsynth ([Message] -> IO (IORef [Message])
forall a. a -> IO (IORef a)
newIORef [])
scsynthPrint :: Scsynth -> IO ()
scsynthPrint :: Scsynth -> IO ()
scsynthPrint (Scsynth IORef [Message]
mRef) = do
[Message]
m <- IORef [Message] -> IO [Message]
forall a. IORef a -> IO a
readIORef IORef [Message]
mRef
[Message] -> IO ()
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 ([Message] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Message]
messages)
then IORef [Message] -> ([Message] -> [Message]) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef [Message]
mRef ([Message] -> [Message] -> [Message]
forall a. [a] -> [a] -> [a]
++ [Message]
messages)
else () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
scsynthReset :: Scsynth -> IO ()
scsynthReset :: Scsynth -> IO ()
scsynthReset (Scsynth IORef [Message]
mRef) = do
[Message]
onResetMessages <- IORef [Message] -> IO [Message]
forall a. IORef a -> IO a
readIORef IORef [Message]
mRef
IORef [Message] -> [Message] -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef [Message]
mRef []
Connection OscSocket () -> IO ()
forall a. Connection OscSocket a -> IO a
withSc3 (Connection OscSocket ()
forall (m :: * -> *). SendOsc m => m ()
reset Connection OscSocket ()
-> Connection OscSocket () -> Connection OscSocket ()
forall a b.
ReaderT OscSocket IO a
-> ReaderT OscSocket IO b -> ReaderT OscSocket IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Message -> Connection OscSocket ())
-> [Message] -> Connection OscSocket ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Message -> Connection OscSocket ()
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 ([Message] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Message]
post) then Scsynth -> [Message] -> IO ()
scsynthOnReset Scsynth
scsynth [Message]
post else () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Connection OscSocket () -> IO ()
forall a. Connection OscSocket a -> IO a
withSc3 ((Message -> Connection OscSocket ())
-> [Message] -> Connection OscSocket ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Message -> Connection OscSocket ()
forall (m :: * -> *). DuplexOsc m => Message -> m ()
maybe_async [Message]
pre Connection OscSocket ()
-> Connection OscSocket () -> Connection OscSocket ()
forall a b.
ReaderT OscSocket IO a
-> ReaderT OscSocket IO b -> ReaderT OscSocket IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Play_Opt -> Ugen -> Connection OscSocket ()
forall e (m :: * -> *).
(Audible e, Transport m) =>
Play_Opt -> e -> m ()
forall (m :: * -> *). Transport m => Play_Opt -> Ugen -> 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