]> gitweb @ CieloNegro.org - EsounD.git/blobdiff - Sound/EsounD/Internals.hs
Give up using type families for stream muxing
[EsounD.git] / Sound / EsounD / Internals.hs
index 987dd1e285262891ba79e69a6e985b0c0e7d0bb0..ef8c659ddd104d0646b9ca770800b6fcf2ab3f48 100644 (file)
@@ -1,27 +1,76 @@
+{-# LANGUAGE
+    EmptyDataDecls
+  , FlexibleInstances
+  , MultiParamTypeClasses
+  , UnicodeSyntax
+  #-}
 module Sound.EsounD.Internals
-    ( wrapSocket
+    ( Frame(..)
+
+    , Channels(..)
+    , Mono
+    , Stereo
+    , interleave
+
+    , wrapSocket
     , closeSocket
     , withCStrOrNull
     )
     where
-
 import Bindings.EsounD
+import Data.Int
+import Data.StorableVector.Lazy as L
 import Foreign.C.String
 import Foreign.C.Types
 import Foreign.Ptr
+import Foreign.Storable
 import System.IO
 import System.Posix.IO
 import System.Posix.Types
 
-wrapSocket :: String -> CInt → IO Handle
+class Storable fr ⇒ Frame fr where
+    frameFmt ∷ fr → C'esd_format_t
+
+instance Frame Int8 where
+    frameFmt _ = c'ESD_BITS8
+
+instance Frame Int16 where
+    frameFmt _ = c'ESD_BITS16
+
+class Channels ch where
+    channelFmt ∷ ch → C'esd_format_t
+
+-- Mono
+data Mono
+
+instance Channels Mono where
+    channelFmt _ = c'ESD_MONO
+
+-- Stereo
+data Stereo
+
+instance Channels Stereo where
+    channelFmt _ = c'ESD_STEREO
+
+interleave ∷ Frame fr ⇒ L.Vector fr → L.Vector fr → L.Vector fr
+interleave l r
+    -- THINKME: consider using storablevector-streamfusion
+    = let Just (lFr, l') = L.viewL l
+          Just (rFr, r') = L.viewL r
+          lr' = interleave l' r'
+      in
+        L.cons lFr (L.cons rFr lr')
+
+-- Utility functions
+wrapSocket ∷ String → CInt → IO Handle
 wrapSocket e (-1) = fail e
 wrapSocket _ fd   = fdToHandle (Fd fd)
 
-closeSocket :: Handle → IO ()
+closeSocket  Handle → IO ()
 closeSocket h = do (Fd fd) ← handleToFd h
                    _       ← c'esd_close (fromIntegral fd)
                    return ()
 
-withCStrOrNull :: Maybe String → (CString → IO a) → IO a
+withCStrOrNull  Maybe String → (CString → IO a) → IO a
 withCStrOrNull Nothing  f = f nullPtr
 withCStrOrNull (Just s) f = withCString s f