]> gitweb @ CieloNegro.org - EsounD.git/blob - Sound/EsounD/Internals.hs
Sound.EsounD.Player
[EsounD.git] / Sound / EsounD / Internals.hs
1 module Sound.EsounD.Internals
2     ( Frame(..)
3
4     , Channels(..)
5     , Mono
6     , Stereo
7
8     , wrapSocket
9     , closeSocket
10     , withCStrOrNull
11     )
12     where
13
14 import Bindings.EsounD
15 import Data.Int
16 import Foreign.C.String
17 import Foreign.C.Types
18 import Foreign.Ptr
19 import System.IO
20 import System.Posix.IO
21 import System.Posix.Types
22
23 class Frame fr where
24     frameFmt ∷ fr → C'esd_format_t
25
26 instance Frame Int8 where
27     frameFmt _ = c'ESD_BITS8
28
29 instance Frame Int16 where
30     frameFmt _ = c'ESD_BITS16
31
32 class Channels ch where
33     channelFmt ∷ ch → C'esd_format_t
34
35 data Mono
36 instance Channels Mono where
37     channelFmt _ = c'ESD_MONO
38
39 data Stereo
40 instance Channels Stereo where
41     channelFmt _ = c'ESD_STEREO
42
43
44 wrapSocket :: String -> CInt → IO Handle
45 wrapSocket e (-1) = fail e
46 wrapSocket _ fd   = fdToHandle (Fd fd)
47
48 closeSocket :: Handle → IO ()
49 closeSocket h = do (Fd fd) ← handleToFd h
50                    _       ← c'esd_close (fromIntegral fd)
51                    return ()
52
53 withCStrOrNull :: Maybe String → (CString → IO a) → IO a
54 withCStrOrNull Nothing  f = f nullPtr
55 withCStrOrNull (Just s) f = withCString s f