X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=EsounD.git;a=blobdiff_plain;f=Sound%2FEsounD%2FPlayer.hs;h=56781da08f2753efc24178d2bcac28f9c0379af2;hp=cfb4e31edb9b9f3f70d5c1dcf04f071860b90426;hb=080d77f8aece1d14f71f6fd337f0cda9cdf7d740;hpb=bb3dbc2442352e09bafe4415d3784f7f61230b17 diff --git a/Sound/EsounD/Player.hs b/Sound/EsounD/Player.hs index cfb4e31..56781da 100644 --- a/Sound/EsounD/Player.hs +++ b/Sound/EsounD/Player.hs @@ -13,20 +13,21 @@ module Sound.EsounD.Player ) where import Bindings.EsounD +import Control.Exception.Peel import Control.Monad.IO.Class +import Control.Monad.IO.Peel import Control.Monad.Trans.Region import Control.Monad.Trans.Region.OnExit import Control.Monad.Unicode import Data.Bits import Data.StorableVector.Lazy as L -import Foreign.C.String import Network import Prelude.Unicode import Sound.EsounD.Streams import Sound.EsounD.Internals import System.IO import System.IO.SaferFileHandles.Unsafe - +import Text.Printf -- ^ An opaque ESD handle for playing a stream. data Player fr ch (r ∷ ★ → ★) @@ -43,27 +44,37 @@ instance Dup (Player fr ch) where dup pl = do ch' ← dup (plCloseH pl) return pl { plCloseH = ch' } -instance Frame fr ⇒ Writable (Player fr Mono) (L.Vector fr) where - write pl v - = liftIO $ sanitizeIOError $ L.hPut (plHandle pl) v +instance Stream (Player fr ch) where + streamSampleRate = plRate + +instance Frame fr ⇒ WritableStream (Player fr Mono) (L.Vector fr) where + writeFrames pl v + = liftIO $ + sanitizeIOError $ + do L.hPut (plHandle pl) v + hFlush (plHandle pl) -instance Frame fr ⇒ Writable (Player fr Stereo) (L.Vector fr, L.Vector fr) where - write pl (l, r) - = liftIO $ sanitizeIOError $ L.hPut (plHandle pl) (interleave l r) +instance Frame fr ⇒ WritableStream (Player fr Stereo) (L.Vector fr, L.Vector fr) where + writeFrames pl (l, r) + = liftIO $ + sanitizeIOError $ + do L.hPut (plHandle pl) (interleave l r) + hFlush (plHandle pl) -- | Open an ESD handle for playing a stream. openPlayer ∷ ∀fr ch s pr. - ( Frame fr - , Channels ch - , MonadIO pr - ) - ⇒ Int -- ^ sample rate for the stream. - → HostName -- ^ host to connect to. - → Maybe String -- ^ name used to identify this stream to - -- ESD (if any). + ( Frame fr + , Channels ch + , MonadPeelIO pr + ) + ⇒ Int -- ^ sample rate for the stream. + → Maybe HostName -- ^ host to connect to. + → Maybe String -- ^ name used to identify this stream to + -- ESD (if any). → RegionT s pr (Player fr ch (RegionT s pr)) openPlayer rate host name - = do h ← liftIO openSocket + = block $ + do h ← liftIO openSocket ch ← onExit $ sanitizeIOError $ closeSocket h return Player { plRate = rate @@ -72,17 +83,23 @@ openPlayer rate host name } where fmt :: C'esd_format_t - fmt = frameFmt ((⊥) ∷ fr) .&. - channelFmt ((⊥) ∷ ch) .&. - c'ESD_STREAM .&. + fmt = frameFmt ((⊥) ∷ fr) .|. + channelFmt ((⊥) ∷ ch) .|. + c'ESD_STREAM .|. c'ESD_PLAY openSocket :: IO Handle - openSocket = withCString host $ \hostPtr → + openSocket = withCStrOrNull host $ \hostPtr → withCStrOrNull name $ \namePtr → c'esd_play_stream fmt (fromIntegral rate) hostPtr namePtr - ≫= wrapSocket "esd_play_stream() returned an error" + ≫= wrapSocket + ( printf "esd_play_stream(%s, %s, %s, %s) returned an error" + (show fmt ) + (show rate) + (show host) + (show name) + )