Skip to content

Instantly share code, notes, and snippets.

@relrod
Created September 28, 2016 05:39
Show Gist options
  • Save relrod/00f9429a4381eaa5020bb4983f829f16 to your computer and use it in GitHub Desktop.
Save relrod/00f9429a4381eaa5020bb4983f829f16 to your computer and use it in GitHub Desktop.
module Main where
import Data.Time.Clock.POSIX
import System.IO
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) => 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 4 lst ++ [round (t * 1000)]
print (60000 `div` average (diff lastFew))
loop lastFew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment