module Sound.Osc.Time.Thread where
import Control.Concurrent
import Control.Monad
import qualified Sound.Osc.Time as Time
pauseThreadLimit :: Fractional n => n
pauseThreadLimit :: forall n. Fractional n => n
pauseThreadLimit = Int -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
forall a. Bounded a => a
maxBound :: Int) n -> n -> n
forall a. Fractional a => a -> a -> a
/ n
1e6
pauseThreadFor :: RealFrac n => n -> IO ()
pauseThreadFor :: forall n. RealFrac n => n -> IO ()
pauseThreadFor n
n = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (n
n n -> n -> Bool
forall a. Ord a => a -> a -> Bool
> n
0) (Int -> IO ()
threadDelay (n -> Int
forall b. Integral b => n -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (n
n n -> n -> n
forall a. Num a => a -> a -> a
* n
1e6)))
pauseThreadUntilTime :: RealFrac n => n -> IO ()
pauseThreadUntilTime :: forall n. RealFrac n => n -> IO ()
pauseThreadUntilTime n
t = n -> IO ()
forall n. RealFrac n => n -> IO ()
pauseThreadFor (n -> IO ()) -> (NtpReal -> n) -> NtpReal -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (n
t n -> n -> n
forall a. Num a => a -> a -> a
-) (n -> n) -> (NtpReal -> n) -> NtpReal -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NtpReal -> n
forall a b. (Real a, Fractional b) => a -> b
realToFrac (NtpReal -> IO ()) -> IO NtpReal -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO NtpReal
Time.currentTime
sleepThreadFor :: RealFrac n => n -> IO ()
sleepThreadFor :: forall n. RealFrac n => n -> IO ()
sleepThreadFor n
n =
if n
n n -> n -> Bool
forall a. Ord a => a -> a -> Bool
>= n
forall n. Fractional n => n
pauseThreadLimit
then
let n' :: n
n' = n
forall n. Fractional n => n
pauseThreadLimit n -> n -> n
forall a. Num a => a -> a -> a
- n
1
in n -> IO ()
forall n. RealFrac n => n -> IO ()
pauseThreadFor n
n' IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> n -> IO ()
forall n. RealFrac n => n -> IO ()
sleepThreadFor (n
n n -> n -> n
forall a. Num a => a -> a -> a
- n
n')
else n -> IO ()
forall n. RealFrac n => n -> IO ()
pauseThreadFor n
n
sleepThreadUntilTime :: RealFrac n => n -> IO ()
sleepThreadUntilTime :: forall n. RealFrac n => n -> IO ()
sleepThreadUntilTime n
t = n -> IO ()
forall n. RealFrac n => n -> IO ()
sleepThreadFor (n -> IO ()) -> (NtpReal -> n) -> NtpReal -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (n
t n -> n -> n
forall a. Num a => a -> a -> a
-) (n -> n) -> (NtpReal -> n) -> NtpReal -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NtpReal -> n
forall a b. (Real a, Fractional b) => a -> b
realToFrac (NtpReal -> IO ()) -> IO NtpReal -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO NtpReal
Time.currentTime