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