X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Sound%2FEsounD%2FInternals.hs;h=735ce6324762dd14672a688df383b65515e6e727;hb=080d77f8aece1d14f71f6fd337f0cda9cdf7d740;hp=ef8c659ddd104d0646b9ca770800b6fcf2ab3f48;hpb=bb3dbc2442352e09bafe4415d3784f7f61230b17;p=EsounD.git diff --git a/Sound/EsounD/Internals.hs b/Sound/EsounD/Internals.hs index ef8c659..735ce63 100644 --- a/Sound/EsounD/Internals.hs +++ b/Sound/EsounD/Internals.hs @@ -11,14 +11,18 @@ module Sound.EsounD.Internals , Mono , Stereo , 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 @@ -52,7 +56,8 @@ data Stereo instance Channels Stereo where channelFmt _ = c'ESD_STEREO -interleave ∷ Frame fr ⇒ L.Vector fr → L.Vector fr → L.Vector fr +{-# INLINE interleave #-} +interleave ∷ Storable α ⇒ L.Vector α → L.Vector α → L.Vector α interleave l r -- THINKME: consider using storablevector-streamfusion = let Just (lFr, l') = L.viewL l @@ -61,7 +66,25 @@ interleave l r in L.cons lFr (L.cons rFr lr') +{-# INLINE deinterleave #-} +deinterleave ∷ Storable α ⇒ L.Vector α → (L.Vector α, L.Vector α) +deinterleave v + -- THINKME: consider using storablevector-streamfusion + = let (lr, v') = L.splitAt 2 v + in + if L.null lr then + (L.empty, L.empty) + else + let Just (lFr, r) = L.viewL lr + Just (rFr, _) = L.viewL r + (l', r') = deinterleave v' + in + (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) @@ -74,3 +97,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 → (CInt → Bool) → CInt → m CInt +failOnError msg isErr rv + | isErr rv = fail msg + | otherwise = return rv