katkitten1

Piecewise Continuous Wav

May 17th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Pattern where
  2. import Data.Int
  3. import Data.WAVE
  4. import System.Process
  5.  
  6. sampleRate = 44100
  7. bitrate = 32
  8.  
  9. genWaveFormCycle p fs = samples
  10.     where
  11.         doubleList = fs <*> map (* (2 * p * pi / (fromIntegral sampleRate))) [0.0 .. (fromIntegral sampleRate ) / p]
  12.         samples = map doubleToSample doubleList
  13.  
  14. genWaveForm :: [WAVESample]  -- | One cycle of the wave
  15.             -> Int           -- | duration in seconds
  16.             -> [WAVESample]
  17.  
  18. genWaveForm wavecycle duration = concat $ replicate n wavecycle
  19.     where
  20.         n = duration * sampleRate `div` (length wavecycle)
  21.  
  22. genWaveFile xs = WAVE header xs
  23.     where header = WAVEHeader 1 sampleRate bitrate (Just $ length $ concat xs)
  24.  
  25. wobbleFuncs = map (\x y -> 0.5 * sin (x * y) ) [1.0, 1.1 .. 3]
  26. genWobbleWave p d = genWaveForm (genWaveFormCycle p (wobbleFuncs ++ (reverse wobbleFuncs))) d
  27. wobbleWave = genWaveFile $ concat  $ replicate 4  [ genWobbleWave 55 3 , genWobbleWave 97.99886 1]
  28.  
  29. main :: IO ()
  30. main =
  31.     do
  32.         putWAVEFile "out.wav" wobbleWave
  33.         pid <- runCommand "aplay -q out.wav"
  34.         -- waitForProcess pid
  35.         return ()
Advertisement
Add Comment
Please, Sign In to add comment