Last active
June 28, 2017 02:28
-
-
Save relrod/9ba78414a5c2520dd1f329677ff2c293 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env runhaskell | |
module Main where | |
import Data.Time.Clock.POSIX | |
import System.IO | |
avgNum :: Int | |
avgNum = 4 | |
main :: IO () | |
main = do | |
putStrLn "Tap to the beat of a song to determine its BPM." | |
hSetEcho stdin False | |
hSetBuffering stdin NoBuffering | |
loop [1] | |
average :: (Foldable t, Integral a, Show a) => t a -> a | |
average n = sum n `div` fromIntegral (length n) | |
takeLast :: Int -> [t] -> [t] | |
takeLast n [] = [] | |
takeLast n l = drop (length l - n) l | |
diff :: Num t => [t] -> [t] | |
diff [] = [] | |
diff ls = zipWith (-) (tail ls) ls | |
loop :: [Integer] -> IO a | |
loop lst = do | |
getChar | |
t <- getPOSIXTime | |
let lastFew = takeLast avgNum lst ++ [round (t * 1000)] | |
let avg = average (diff lastFew) | |
print (60000 `div` if avg > 0 then avg else 1) | |
loop lastFew |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment