]> gitweb @ CieloNegro.org - EsounD.git/blobdiff - Sound/EsounD/Internals.hs
save my changes from being lost
[EsounD.git] / Sound / EsounD / Internals.hs
index f75f99a0dc1770777e9aa196505058a1854539f3..4c450361571e15fd54e7c14ce562f056c0d84b08 100644 (file)
@@ -1,3 +1,10 @@
+{-# LANGUAGE
+    EmptyDataDecls
+  , FlexibleInstances
+  , MultiParamTypeClasses
+  , TypeFamilies
+  , UnicodeSyntax
+  #-}
 module Sound.EsounD.Internals
     ( Frame(..)
 
@@ -5,22 +12,25 @@ module Sound.EsounD.Internals
     , Mono
     , Stereo
 
+    , Mux(..)
+
     , 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
 
-class Frame fr where
+class Storable fr ⇒ Frame fr where
     frameFmt ∷ fr → C'esd_format_t
 
 instance Frame Int8 where
@@ -32,24 +42,48 @@ instance Frame Int16 where
 class Channels ch where
     channelFmt ∷ ch → C'esd_format_t
 
+class (Frame fr, Channels ch) ⇒ Mux (vec ∷ ★ → ★) fr ch where
+    type DemuxedVec vec fr ch
+    mux ∷ DemuxedVec vec fr ch → vec fr
+
+-- Mono
 data Mono
+
 instance Channels Mono where
     channelFmt _ = c'ESD_MONO
 
+instance Frame fr ⇒ Mux vec fr Mono where
+    type DemuxedVec vec fr Mono = vec fr
+    mux = id
+
+-- Stereo
 data Stereo
+
 instance Channels Stereo where
     channelFmt _ = c'ESD_STEREO
 
+instance Frame fr ⇒ Mux L.Vector fr Stereo where
+    type DemuxedVec L.Vector fr Stereo = (L.Vector fr, L.Vector fr)
+    mux (left, right) = loop left right
+        where
+          -- THINKME: consider using storablevector-streamfusion
+          loop l r
+              = let Just (lFr, l') = L.viewL l
+                    Just (rFr, r') = L.viewL r
+                    lr' = loop l' r'
+                in
+                  L.cons lFr (L.cons rFr lr')
 
-wrapSocket :: String -> CInt → IO Handle
+-- 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