module Sound.OpenSoundControl.Time where
import Control.Concurrent
import Control.Monad
import qualified Data.Time as T
data Time = UTCr Double | NTPr Double | NTPi Integer
deriving (Eq, Show)
as_ntpi :: Time -> Integer
as_ntpi (UTCr t) = utcr_ntpi t
as_ntpi (NTPr t) = ntpr_ntpi t
as_ntpi (NTPi t) = t
instance Ord Time where
compare (UTCr p) (UTCr q) = compare p q
compare (NTPr p) (NTPr q) = compare p q
compare (NTPi p) (NTPi q) = compare p q
compare p q = compare (as_ntpi p) (as_ntpi q)
ntpr_ntpi :: Double -> Integer
ntpr_ntpi t = round (t * 2^(32::Int))
utcr_ntpi :: Double -> Integer
utcr_ntpi t = ntpr_ntpi (t + secdif)
where secdif = (70 * 365 + 17) * 24 * 60 * 60
utc_base :: T.UTCTime
utc_base = T.UTCTime d s
where d = T.fromGregorian 1970 1 1
s = T.secondsToDiffTime 0
utcr :: IO Double
utcr = do t <- T.getCurrentTime
return (realToFrac (T.diffUTCTime t utc_base))
ntpi :: IO Integer
ntpi = liftM utcr_ntpi utcr
pauseThread :: Double -> IO ()
pauseThread n = when (n > 1e-4) (threadDelay (floor (n * 1e6)))
pauseThreadUntil :: Double -> IO ()
pauseThreadUntil t = pauseThread . (t ) =<< utcr