Skip to content

Instantly share code, notes, and snippets.

@relrod
Created March 6, 2020 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relrod/7559fea2a01435fc13569af24db96ed6 to your computer and use it in GitHub Desktop.
Save relrod/7559fea2a01435fc13569af24db96ed6 to your computer and use it in GitHub Desktop.
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