Skip to content

Instantly share code, notes, and snippets.

@jsoffer
Created August 23, 2010 14:48
Show Gist options
  • Save jsoffer/545626 to your computer and use it in GitHub Desktop.
Save jsoffer/545626 to your computer and use it in GitHub Desktop.
-- medidor de beat
-- compilar: 'ghc --make beat.hs'
-- cuenta distancias en milisegundos entre clicks de teclado
-- salir con 'q'
import qualified Graphics.UI.SDL as SDL
import Graphics.UI.SDL.Keysym as K
import Graphics.UI.SDL.Time as T
import Data.Word(Word32)
avg :: [Word32] -> Word32
avg xs = round (suma / longitud) where
suma = fromIntegral $ sum xs
longitud = fromIntegral $ length xs
avg_integral :: [Word32] -> Word32
avg_integral xs = snd par where
margen = 50
promedio = avg xs
rango = [(promedio - margen)..(promedio + margen)]
f x = abs $ sum $ zipWith (-) xs (repeat x)
par = minimum $ zip (map f rango) rango
main :: IO [Word32]
main = do
SDL.init []
SDL.setVideoMode 640 480 24 []
xs <- eventLoop []
SDL.quit
let ys = zipWith (-) (tail xs) (drop 2 xs)
print ys
print $ avg_integral ys
return xs
where
eventLoop :: [Word32] -> IO [Word32]
eventLoop ys = SDL.waitEventBlocking >>= checkEvent ys
checkEvent :: [Word32] -> SDL.Event -> IO [Word32]
checkEvent ys (SDL.KeyDown (Keysym {symKey = K.SDLK_q})) = return ys
checkEvent ys (SDL.KeyDown _) = T.getTicks >>= (\k -> eventLoop (k:ys))
checkEvent ys _ = eventLoop ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment