From 0eb9c9ae700704332a088c394159889ecedac6e0 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Thu, 3 Dec 2015 20:37:33 -0500 Subject: use real clock time (adjusted from the monotonic clock) --- midi-dump.hs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'midi-dump.hs') diff --git a/midi-dump.hs b/midi-dump.hs index c7cd5e1..69f8480 100644 --- a/midi-dump.hs +++ b/midi-dump.hs @@ -30,6 +30,12 @@ data LoopState = LoopState { lastTick :: TimeSpec } +getAbsTime = do + (startTime, startTimeReal, _, _, _, _, _, _) <- ask + now <- liftIO $ getTime Monotonic + return $ now - startTime + startTimeReal + + emptyLoopState = LoopState Set.empty [] main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do @@ -37,12 +43,13 @@ main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do putStrLn "Rock on!" startTime <- getTime Monotonic + startTimeReal <- getTime Realtime - (_, ()) <- execRWST loop (startTime, h, public, private, q, publicAddr, privateAddr) (emptyLoopState startTime) + (_, ()) <- execRWST loop (startTime, startTimeReal, h, public, private, q, publicAddr, privateAddr) (emptyLoopState startTime) return () loop = do - (startTime, h, _, _, q, publicAddr, _) <- ask + (startTime, startTimeReal, h, _, _, q, publicAddr, _) <- ask oldKeys <- gets keysDown let forwardNOW = forwardNoteEvent h q publicAddr @@ -51,15 +58,15 @@ loop = do if (oldKeys == newKeys) then liftIO $ threadDelay 15000 -- 15ms. Seems like a lot, but it sounds OK. Cuts CPU down to 2%. else do - now <- liftIO $ getTime Monotonic - let delta = now - startTime + now <- getAbsTime let newEvents = map (MidiEvent now) events liftIO $ printChordLn newKeys modify $ \s -> s { keysDown = newKeys, inputHistory = newEvents ++ inputHistory s } when (Set.null newKeys) $ do - hist <- gets $ takeWhile (not . isSilence) . inputHistory +-- hist <- gets $ takeWhile (not . isSilence) . inputHistory + hist <- gets $ filter (not . isSilence) . inputHistory liftIO $ print $ map (`div` 1000000) $ tsDeltas $ map getTS $ reverse hist liftIO $ print $ mapMaybe getMidiDesc $ reverse hist modify $ \s -> s { inputHistory = Silence now:inputHistory s } -- cgit v1.2.3