4 , MultiParamTypeClasses
7 module Sound.EsounD.Internals
23 import Bindings.EsounD
25 import Data.StorableVector as S
26 import Data.StorableVector.Lazy as L
27 import Foreign.C.String
28 import Foreign.C.Types
30 import Foreign.Storable
32 import System.Posix.IO
33 import System.Posix.Types
35 class Storable fr ⇒ Frame fr where
36 frameFmt ∷ fr → C'esd_format_t
38 instance Frame Int8 where
39 frameFmt _ = c'ESD_BITS8
41 instance Frame Int16 where
42 frameFmt _ = c'ESD_BITS16
44 class Channels ch where
45 channelFmt ∷ ch → C'esd_format_t
50 instance Channels Mono where
51 channelFmt _ = c'ESD_MONO
56 instance Channels Stereo where
57 channelFmt _ = c'ESD_STEREO
59 {-# INLINE interleave #-}
60 interleave ∷ Storable α ⇒ L.Vector α → L.Vector α → L.Vector α
62 -- THINKME: consider using storablevector-streamfusion
63 = let Just (lFr, l') = L.viewL l
64 Just (rFr, r') = L.viewL r
65 lr' = interleave l' r'
67 L.cons lFr (L.cons rFr lr')
69 {-# INLINE deinterleave #-}
70 deinterleave ∷ Storable α ⇒ L.Vector α → (L.Vector α, L.Vector α)
72 -- THINKME: consider using storablevector-streamfusion
73 = let (lr, v') = L.splitAt 2 v
78 let Just (lFr, r) = L.viewL lr
79 Just (rFr, _) = L.viewL r
80 (l', r') = deinterleave v'
82 (L.cons lFr l', L.cons rFr r')
85 toLSV ∷ Storable α ⇒ S.Vector α → L.Vector α
86 toLSV v = L.fromChunks [v]
88 wrapSocket ∷ String → CInt → IO Handle
89 wrapSocket e (-1) = fail e
90 wrapSocket _ fd = fdToHandle (Fd fd)
92 closeSocket ∷ Handle → IO ()
93 closeSocket h = do (Fd fd) ← handleToFd h
94 _ ← c'esd_close (fromIntegral fd)
97 withCStrOrNull ∷ Maybe String → (CString → IO a) → IO a
98 withCStrOrNull Nothing f = f nullPtr
99 withCStrOrNull (Just s) f = withCString s f