-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMixer.hs
50 lines (42 loc) · 983 Bytes
/
Mixer.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
module Mixer (
initMixer,
playSE,
playBGM,
stopBGM
) where
import Graphics.UI.SDL.Mixer
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import System.IO.Unsafe (unsafePerformIO)
freq, channel, samples :: Int
format :: AudioFormat
freq = 22050
format = AudioS16Sys
channel = 2
samples = 4096
initMixer :: IO ()
initMixer = do
openAudio freq format channel samples
playSE :: Maybe Chunk -> IO ()
playSE Nothing = return ()
playSE (Just ad) = flip catch (\_ -> return ()) $ do
playChannel (-1) ad 0
return ()
playingBGM :: IORef (Maybe Music)
playingBGM = unsafePerformIO $ newIORef Nothing
stopBGM :: IO ()
stopBGM = do
m <- readIORef playingBGM
case m of
Nothing -> return ()
Just mus -> do
freeMusic mus
writeIORef playingBGM Nothing
playBGM :: String -> IO ()
playBGM bgmfn = do
stopBGM
maybeMusic <- tryLoadMUS bgmfn
case maybeMusic of
Just music -> do
playMusic music (-1)
writeIORef playingBGM $ Just music
Nothing -> return ()