-
Notifications
You must be signed in to change notification settings - Fork 11
/
Basic.hs
49 lines (37 loc) · 864 Bytes
/
Basic.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
import qualified SDL.Mixer as Mix
import qualified SDL
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.initialize [Mix.InitMP3]
-- open device
Mix.openAudio Mix.defaultSpec 256
-- open file
sound <- Mix.load fileName
-- play file
channel <- Mix.play sound
-- wait until finished
whileTrueM $ Mix.playing channel
-- free resources
Mix.freeChunk sound
-- close device
Mix.closeAudio
-- quit
Mix.quit
SDL.quit
whileTrueM :: Monad m => m Bool -> m ()
whileTrueM cond = do
loop <- cond
if loop then whileTrueM cond
else return ()