4 , MultiParamTypeClasses
7 module Sound.EsounD.Internals
21 import Bindings.EsounD
23 import Data.StorableVector.Lazy as L
24 import Foreign.C.String
25 import Foreign.C.Types
27 import Foreign.Storable
29 import System.Posix.IO
30 import System.Posix.Types
32 class Storable fr ⇒ Frame fr where
33 frameFmt ∷ fr → C'esd_format_t
35 instance Frame Int8 where
36 frameFmt _ = c'ESD_BITS8
38 instance Frame Int16 where
39 frameFmt _ = c'ESD_BITS16
41 class Channels ch where
42 channelFmt ∷ ch → C'esd_format_t
47 instance Channels Mono where
48 channelFmt _ = c'ESD_MONO
53 instance Channels Stereo where
54 channelFmt _ = c'ESD_STEREO
56 {-# INLINE interleave #-}
57 interleave ∷ Storable α ⇒ L.Vector α → L.Vector α → L.Vector α
59 -- THINKME: consider using storablevector-streamfusion
60 = let Just (lFr, l') = L.viewL l
61 Just (rFr, r') = L.viewL r
62 lr' = interleave l' r'
64 L.cons lFr (L.cons rFr lr')
66 {-# INLINE deinterleave #-}
67 deinterleave ∷ Storable α ⇒ L.Vector α → (L.Vector α, L.Vector α)
69 -- THINKME: consider using storablevector-streamfusion
70 = let (lr, v') = L.splitAt 2 v
75 let Just (lFr, r) = L.viewL lr
76 Just (rFr, _) = L.viewL r
77 (l', r') = deinterleave v'
79 (L.cons lFr l', L.cons rFr r')
82 wrapSocket ∷ String → CInt → IO Handle
83 wrapSocket e (-1) = fail e
84 wrapSocket _ fd = fdToHandle (Fd fd)
86 closeSocket ∷ Handle → IO ()
87 closeSocket h = do (Fd fd) ← handleToFd h
88 _ ← c'esd_close (fromIntegral fd)
91 withCStrOrNull ∷ Maybe String → (CString → IO a) → IO a
92 withCStrOrNull Nothing f = f nullPtr
93 withCStrOrNull (Just s) f = withCString s f