]> gitweb @ CieloNegro.org - EsounD.git/blob - examples/EsdPlayerExample.hs
updating things to prepare EsounD-0.2
[EsounD.git] / examples / EsdPlayerExample.hs
1 {-# LANGUAGE
2     UnicodeSyntax
3   #-}
4 module Main where
5 import Control.Monad.IO.Class
6 import Control.Monad.Trans.Region
7 import Data.Int
8 import qualified Data.StorableVector.Lazy as L
9 import Prelude.Unicode
10 import Sound.EsounD
11
12 main ∷ IO ()
13 main = runRegionT $
14        do pl ← openPlayer 44100 Nothing Nothing
15           -- Let's play an 'A' note for 1 sec.
16           playMono16Sine pl 44100 1 440
17
18 playMono16Sine ∷ ( AncestorRegion pr cr
19                   , MonadIO cr
20                   )
21                ⇒ Player Int16 Mono pr
22                → Int
23                → Double
24                → Double
25                → cr ()
26 playMono16Sine pl sampleFreq sec noteFreq
27     = writeFrames pl buffer
28     where
29       buffer ∷ L.Vector Int16
30       buffer = L.pack L.defaultChunkSize frames
31
32       frames ∷ [Int16]
33       frames = let nFrames = round $ sec ⋅ realToFrac sampleFreq
34                in
35                  map calcFrame [0 .. nFrames - 1]
36
37       calcFrame ∷ Int → Int16
38       calcFrame n = let frame = calcFrame' n
39                     in
40                       -- -1.0 ≤ frame ≤ 1.0
41                       floor $ fromIntegral ((maxBound ∷ Int16) - 1) ⋅ frame
42
43       calcFrame' ∷ Int → Double
44       calcFrame' n
45           = sin $
46             2
47             ⋅ π
48             ⋅ noteFreq
49             ⋅ (realToFrac n ÷ realToFrac sampleFreq)