This module creates a background thread that continuously prints timing/cycle information to the terminal.
streamGetnowMods- Gets the current cycle position and calculates where you are within multiple time subdivisions (mod, mod×2, mod×4, mod×8, mod×16, mod×32):
streamGetnowMods :: Double -> Stream -> IO [Double]
streamGetnowMods mod s = do
let mods = take 6 $ iterate (*2) mod
result <- streamGetnow s
return $! map (mod' result) modsstreamGetnowModsString- Formats this into a readable string likec:35 | 1/8 3/16 7/32...:
streamGetnowModsString :: Double -> Stream -> IO String
streamGetnowModsString mod s = do
let mods = take 6 $ iterate (*2) mod
results <- streamGetnowMods mod s
cyclePos <- streamGetnow s
let formattedResults = printf "c:%.0f |" (cyclePos+1) : zipWith (\x y -> printf "%d/%.0f" (floor x + 1) y) results mods
return $ unwords formattedResultsprintEverySecond- The main loop that prints BPM + cycle position. Despite the name, it actually delays based on CPS (cycles per second), so it updates roughly once per cycle:
printEverySecond :: Double -> Stream -> IO ()
printEverySecond mod s = CM.forever $ do
result <- streamGetnowModsString mod s
bpm <- getbpm s
let bpmString = "t:" ++ printf "%.2f" bpm
cps <- streamGetcps s
let delay = floor (1 / cps * 1000000) -- convert cps to microseconds
threadDelay delay
putStrLn (bpmString ++ " | " ++ result)startPrinting spawns a background thread using forkIO and returns an MVar (a thread-safe variable) for control:
startPrinting :: Double -> Stream -> IO (MVar ())
startPrinting mod s = do
stopVar <- newEmptyMVar
_ <- forkIO $ do
let loop = do
stopped <- tryTakeMVar stopVar
CM.when (stopped == Nothing) $ do
printEverySecond mod s
loop
loop
return stopVarstopPrinting signals the thread to stop by putting a value in the MVar:
stopPrinting :: MVar () -> IO ()
stopPrinting stopVar = do
putMVar stopVar ()
return ()To use it in your boot/session:
-- Start monitoring (8 = base cycle subdivision)
monitor <- startPrinting 8 stream
-- Later, to stop:
stopPrinting monitorThe output looks like: t:120.00 | c:35 | 1/8 3/16 7/32 15/64 31/128 63/256
This tells you the BPM, current cycle number, and your position within increasingly longer cycle periods (useful for tracking where you are in longer structural patterns like ur patterns).