]> gitweb @ CieloNegro.org - EsounD.git/blobdiff - Sound/EsounD/Internals.hs
updating things to prepare EsounD-0.2
[EsounD.git] / Sound / EsounD / Internals.hs
index fcaff1e5e88d84d5c887bf0a45e3d34f93b1e865..997899a6291e0ae05a100110a5300564e45d0b58 100644 (file)
@@ -13,13 +13,16 @@ module Sound.EsounD.Internals
     , interleave
     , deinterleave
 
+    , toLSV
     , wrapSocket
     , closeSocket
     , withCStrOrNull
+    , failOnError
     )
     where
 import Bindings.EsounD
 import Data.Int
+import Data.StorableVector      as S
 import Data.StorableVector.Lazy as L
 import Foreign.C.String
 import Foreign.C.Types
@@ -30,28 +33,34 @@ import System.Posix.IO
 import System.Posix.Types
 
 class Storable fr ⇒ Frame fr where
-    frameFmt ∷ fr → C'esd_format_t
+    frameFmt  ∷ fr → C'esd_format_t
+    frameSize ∷ fr → Int
 
 instance Frame Int8 where
-    frameFmt _ = c'ESD_BITS8
+    frameFmt  _ = c'ESD_BITS8
+    frameSize _ = 1
 
 instance Frame Int16 where
-    frameFmt _ = c'ESD_BITS16
+    frameFmt  _ = c'ESD_BITS16
+    frameSize _ = 2
 
 class Channels ch where
-    channelFmt ∷ ch → C'esd_format_t
+    channelFmt  ∷ ch → C'esd_format_t
+    numChannels ∷ ch → Int
 
 -- Mono
 data Mono
 
 instance Channels Mono where
-    channelFmt _ = c'ESD_MONO
+    channelFmt  _ = c'ESD_MONO
+    numChannels _ = 1
 
 -- Stereo
 data Stereo
 
 instance Channels Stereo where
-    channelFmt _ = c'ESD_STEREO
+    channelFmt  _ = c'ESD_STEREO
+    numChannels _ = 2
 
 {-# INLINE interleave #-}
 interleave ∷ Storable α ⇒ L.Vector α → L.Vector α → L.Vector α
@@ -79,6 +88,9 @@ deinterleave v
               (L.cons lFr l', L.cons rFr r')
 
 -- Utility functions
+toLSV ∷ Storable α ⇒ S.Vector α → L.Vector α
+toLSV v = L.fromChunks [v]
+
 wrapSocket ∷ String → CInt → IO Handle
 wrapSocket e (-1) = fail e
 wrapSocket _ fd   = fdToHandle (Fd fd)
@@ -91,3 +103,8 @@ closeSocket h = do (Fd fd) ← handleToFd h
 withCStrOrNull ∷ Maybe String → (CString → IO a) → IO a
 withCStrOrNull Nothing  f = f nullPtr
 withCStrOrNull (Just s) f = withCString s f
+
+failOnError ∷ Monad m ⇒ String → (α → Bool) → α → m α
+failOnError msg isErr rv
+    | isErr rv  = fail msg
+    | otherwise = return rv