-
Notifications
You must be signed in to change notification settings - Fork 11
/
BasicRaw.hs
65 lines (51 loc) · 1.26 KB
/
BasicRaw.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import qualified SDL.Raw.Mixer as Mix
import qualified SDL
import Control.Applicative
import Foreign.C.String
import Foreign.Ptr
import System.Environment
import System.Exit
main :: IO ()
main = do
-- read arguments
fileName <- do
args <- getArgs
case args of
[arg] -> return arg
_ -> do
putStrLn "Usage: <cmd> <sound filename>"
exitWith $ ExitFailure 1
-- initialize libraries
SDL.initialize [SDL.InitAudio]
_ <- Mix.init Mix.MIX_INIT_MP3
let rate = 22050
format = Mix.AUDIO_S16SYS
channels = 2
bufsize = 256
-- open device
result <- Mix.openAudio rate format channels bufsize
assert $ result == 0
-- open file
sound <- withCString fileName $ \cstr -> Mix.loadWav cstr
assert $ sound /= nullPtr
-- play file
channel <- Mix.playChannel (-1) sound 0
assert $ channel /= -1
-- wait until finished
whileTrueM $ (/= 0) <$> Mix.playing channel
-- free resources
Mix.freeChunk sound
-- close device
Mix.closeAudio
-- quit
Mix.quit
SDL.quit
assert :: Bool -> IO ()
assert x = if x
then return ()
else error "Assertion failed"
whileTrueM :: Monad m => m Bool -> m ()
whileTrueM cond = do
loop <- cond
if loop then whileTrueM cond
else return ()