Skip to content

Instantly share code, notes, and snippets.

@lucamolteni
Last active October 30, 2015 08:31
Show Gist options
  • Select an option

  • Save lucamolteni/44f6083584ff3f4d6d76 to your computer and use it in GitHub Desktop.

Select an option

Save lucamolteni/44f6083584ff3f4d6d76 to your computer and use it in GitHub Desktop.
module Main where
import Data.WAVE
import Control.Monad
import Data.List
main :: IO ()
main = do
f <- getWAVEFile "example.wav"
let samples = waveSamples f
let flatten = flatSamples samples
let toConsole = map (toString . consoleAmplitude) flatten
forM_ toConsole putStrLn
return ()
numChannel :: WAVE -> Int
numChannel f = waveNumChannels (waveHeader f)
flatSamples :: WAVESamples -> [Double]
flatSamples f = map average $ splitEvery 10 (map avgSample f)
average xs = realToFrac (sum xs) / genericLength xs
avgSample :: [WAVESample] -> Double
avgSample = average
splitEvery :: Int -> [a] -> [[a]]
splitEvery _ [] = []
splitEvery n xs = as : splitEvery n bs
where (as,bs) = splitAt n xs
toString :: Int -> String
toString n = map (const 'X') [1..n]
consoleAmplitude :: Double -> Int
consoleAmplitude x = floor x `mod` 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment