Created
March 6, 2020 09:28
-
-
Save relrod/7559fea2a01435fc13569af24db96ed6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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) | |
diff :: Num t => [t] -> [t] | |
diff [] = [] | |
diff ls = zipWith (flip (-)) (tail ls) ls | |
loop :: [Integer] -> IO a | |
loop lst = do | |
getChar | |
t <- getPOSIXTime | |
let lastFew = round (t * 1000) : take avgNum lst | |
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