1 module RMCA.Global.Clock where
3 import Control.Concurrent
6 import Data.ReactiveValue
11 metronome :: SF Tempo (Event Beat)
12 metronome = repeatedlyS () <<^ tempoToQNoteIvl
14 -- Tempo is the number of quarter notes per minute.
15 tempoToQNoteIvl :: Tempo -> DTime
16 tempoToQNoteIvl = (15/) . fromIntegral
18 type TickingClock = (CBMVar (), ThreadId)
20 -- Make a clock that will execute any IO when it updates.
21 mkClockGeneric :: IO () -> DTime -> IO TickingClock
22 mkClockGeneric io d = do
24 tid <- forkIO $ forever $ do
29 where dInt = floor $ d * 1000
31 -- Ticking clock in the IO monad, sending callbacks every t milliseconds.
32 mkClock :: DTime -> IO TickingClock
33 mkClock = mkClockGeneric (return ())
35 -- For debugging purposes.
36 mkClockDebug :: DTime -> IO TickingClock
37 mkClockDebug = mkClockGeneric (putStrLn "Ping !")
39 clockRV :: TickingClock -> ReactiveFieldRead IO ThreadId
40 clockRV (mvar, tid) = ReactiveFieldRead (return tid)
41 (installCallbackCBMVar mvar)
43 mkClockRV :: DTime -> IO (ReactiveFieldRead IO ThreadId)
44 mkClockRV d = clockRV <$> mkClock d
46 stopClock :: TickingClock -> IO ()
47 stopClock (_,t) = killThread t