-
Notifications
You must be signed in to change notification settings - Fork 11
/
Mixer.hs
222 lines (193 loc) · 5.79 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module SDL.Mixer (
initialize,
quit,
openAudio,
querySpec,
load,
play,
playChannel,
playing,
playingCount,
freeChunk,
closeAudio,
defaultSpec,
AudioSpec(..),
Output(..),
Format(..),
InitFlag(..),
ChannelChoice(..),
Loops(..),
Chunk,
Channel,
) where
import Prelude hiding (foldl)
import Control.Applicative
import Control.Monad.IO.Class
import Data.Bits
import Data.Foldable
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
import SDL.Exception
import qualified SDL.Raw.Mixer as Raw
foldFlags :: (Bits b, Foldable f, Num b) => (flag -> b) -> f flag -> b
foldFlags f = foldl (\a b -> a .|. f b) 0
class RawConversion a where
type R a :: *
toRaw :: a -> R a
fromRaw :: R a -> a
data InitFlag
= InitFLAC
| InitMOD
| InitMP3
| InitOGG
deriving (Bounded, Eq, Read, Show)
instance RawConversion InitFlag where
type R InitFlag = Raw.InitFlag
toRaw InitFLAC = Raw.MIX_INIT_FLAC
toRaw InitMOD = Raw.MIX_INIT_MOD
toRaw InitMP3 = Raw.MIX_INIT_MP3
toRaw InitOGG = Raw.MIX_INIT_OGG
fromRaw r = case r of
r' | r' == Raw.MIX_INIT_FLAC -> InitFLAC
| r' == Raw.MIX_INIT_MOD -> InitMOD
| r' == Raw.MIX_INIT_MP3 -> InitMP3
| r' == Raw.MIX_INIT_OGG -> InitOGG
| otherwise -> error "Raw InitFlag not recognized"
initialize :: (Foldable f, Functor m, MonadIO m) => f InitFlag -> m ()
initialize flags =
throwIf_ ((/= rawFlags) . (.&. rawFlags)) "SDL.Mixer.initialize" "Mix_Init" $
Raw.init rawFlags
where
rawFlags = foldFlags toRaw flags
quit :: MonadIO m => m ()
quit = Raw.quit
data Format
= FormatU8
| FormatS8
| FormatU16_LSB
| FormatS16_LSB
| FormatU16_MSB
| FormatS16_MSB
| FormatU16
| FormatS16
| FormatU16_Sys
| FormatS16_Sys
deriving (Bounded, Eq, Read, Show)
instance RawConversion Format where
type R Format = Raw.Format
toRaw FormatU8 = Raw.AUDIO_U8
toRaw FormatS8 = Raw.AUDIO_S8
toRaw FormatU16_LSB = Raw.AUDIO_U16LSB
toRaw FormatS16_LSB = Raw.AUDIO_S16LSB
toRaw FormatU16_MSB = Raw.AUDIO_U16MSB
toRaw FormatS16_MSB = Raw.AUDIO_S16MSB
toRaw FormatU16 = Raw.AUDIO_U16
toRaw FormatS16 = Raw.AUDIO_S16
toRaw FormatU16_Sys = Raw.AUDIO_U16SYS
toRaw FormatS16_Sys = Raw.AUDIO_S16SYS
fromRaw r = case r of
r' | r' == Raw.AUDIO_U8 -> FormatU8
| r' == Raw.AUDIO_S8 -> FormatS8
| r' == Raw.AUDIO_U16LSB -> FormatU16_LSB
| r' == Raw.AUDIO_S16LSB -> FormatS16_LSB
| r' == Raw.AUDIO_U16MSB -> FormatU16_MSB
| r' == Raw.AUDIO_S16MSB -> FormatS16_MSB
| r' == Raw.AUDIO_U16 -> FormatU16
| r' == Raw.AUDIO_S16 -> FormatS16
| r' == Raw.AUDIO_U16SYS -> FormatU16_Sys
| r' == Raw.AUDIO_S16SYS -> FormatS16_Sys
| otherwise -> error "Raw Format not recognized"
defaultSpec :: AudioSpec
defaultSpec = AudioSpec
{ audioFrequency = 44100
, audioFormat = FormatS16_Sys
, audioOutput = Stereo
}
data Output
= Stereo
| Mono
deriving (Bounded, Eq, Read, Show)
instance RawConversion Output where
type R Output = CInt
toRaw Stereo = 2
toRaw Mono = 1
fromRaw 2 = Stereo
fromRaw 1 = Mono
fromRaw _ = error "Raw Output not recognized"
data AudioSpec = AudioSpec
{ audioFrequency :: Int
, audioFormat :: Format
, audioOutput :: Output
} deriving (Eq, Read, Show)
openAudio :: (Functor m, MonadIO m) => AudioSpec -> Int -> m ()
openAudio config chunkSize_ =
throwIfNeg_ "SDL.Mixer.openAudio" "Mix_OpenAudio" $
Raw.openAudio frequency format output chunkSize
where
frequency = fromIntegral (audioFrequency config)
format = toRaw (audioFormat config)
output = toRaw (audioOutput config)
chunkSize = fromIntegral chunkSize_
querySpec :: (MonadIO m) => m AudioSpec
querySpec = liftIO $
alloca $ \pa ->
alloca $ \pb ->
alloca $ \pc -> do
_ <- throwIf0 "SDL.Mixer.querySpec" "Mix_QuerySpec" $ Raw.querySpec pa pb pc
frequency <- fromIntegral <$> peek pa
format <- fromRaw <$> peek pb
output <- fromRaw <$> peek pc
return $ AudioSpec frequency format output
newtype Chunk = Chunk (Ptr Raw.Chunk)
load :: (Functor m, MonadIO m) => FilePath -> m Chunk
load filePath =
fmap Chunk $
throwIfNull "SDL.Mixer.load" "Mix_LoadWAV" $
liftIO $ withCString filePath $ \cstr ->
Raw.loadWav cstr
newtype Channel = Channel CInt
data ChannelChoice
= AnyChannel
| SpecificChannel Channel
data Loops
= Infinite
| Once
| Repeat Int
play :: (Functor m, MonadIO m) => Chunk -> m Channel
play chunk = playChannel AnyChannel chunk Once
playChannel :: (Functor m, MonadIO m) => ChannelChoice -> Chunk -> Loops -> m Channel
playChannel channel chunk loops =
fmap Channel $
throwIfNeg "SDL.Mixer.playChannel" "Mix_PlayChannel" $
Raw.playChannel channel' chunk' loops'
where
Chunk chunk' = chunk
channel' = case channel of
AnyChannel -> -1
SpecificChannel (Channel n) -> n
loops' = case loops of
Infinite -> (-1)
Once -> 0
Repeat n | n > 1 -> fromIntegral (n - 1)
| otherwise -> error "Invalid Repeat value"
playing :: (Functor m, MonadIO m) => Channel -> m Bool
playing channel =
fmap (> 0) $
Raw.playing channel'
where
Channel channel' = channel
playingCount :: (Functor m, MonadIO m) => m Int
playingCount =
fmap fromIntegral $
Raw.playing (-1)
freeChunk :: MonadIO m => Chunk -> m ()
freeChunk chunk = Raw.freeChunk chunk'
where
Chunk chunk' = chunk
closeAudio :: MonadIO m => m ()
closeAudio = Raw.closeAudio