module Sound.OpenSoundControl.Transport ( Transport(..)
, withTransport
, waitFor, wait ) where
import Control.Exception
import Sound.OpenSoundControl.OSC
class Transport t where
send :: t -> OSC -> IO ()
recv :: t -> IO OSC
close :: t -> IO ()
has_address :: String -> OSC -> Bool
has_address x (Message y _) = x == y
has_address _ _ = False
untilM :: Monad m => (a -> Maybe b) -> m a -> m b
untilM f act = recurse
where g p = let q = f p in case q of { Nothing -> recurse
; Just r -> return r }
recurse = act >>= g
waitFor :: Transport t => t -> (OSC -> Maybe a) -> IO a
waitFor t f = untilM f (recv t)
wait :: Transport t => t -> String -> IO OSC
wait t s = waitFor t (\o -> if has_address s o then Just o else Nothing)
withTransport :: Transport t => IO t -> (t -> IO a) -> IO a
withTransport u = bracket u close