View difference between Paste ID: Dyz7C2VY and RmUCkwQu
SHOW: | | - or go back to the newest paste.
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-
-- From an answer by David Milani at:
9+
10-
-- https://stackoverflow.com/questions/5658391/generating-wav-sound-data-in-haskell
10+
11-
sound :: Double  -- | Frequency
11+
12-
      -> Int     -- | Samples per second
12+
13-
      -> Double  -- | Length of sound in seconds
13+
14-
      -> Int32   -- | Volume, (maxBound :: Int32) for highest, 0 for lowest
14+
15-
      -> [Int32]
15+
16
            -> [WAVESample]
17-
sound freq samples len volume = 
17+
18-
   take (round $ len * (fromIntegral samples)) $ 
18+
19-
         map (round . (* fromIntegral volume)) $
19+
20-
         map sin [0.0, (freq * 2 * pi / (fromIntegral samples))..]
20+
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 ()