module Sound.File.Sndfile.Buffer (
module Sound.File.Sndfile.Buffer.Sample
, Buffer(..)
, hGetBuffer
, hGetContents
, readFile
, hPutBuffer
, writeFile
) where
import Control.Exception (bracket)
import Control.Monad
import Foreign
import Prelude hiding (readFile, writeFile)
import Sound.File.Sndfile.Interface
import Sound.File.Sndfile.Buffer.Sample (Sample(..))
class Buffer a e where
fromForeignPtr :: ForeignPtr e -> Int -> Int -> IO (a e)
toForeignPtr :: a e -> IO (ForeignPtr e, Int, Int)
hGetBuffer :: forall a e . (Sample e, Storable e, Buffer a e) => Handle -> Count -> IO (Maybe (a e))
hGetBuffer h n = do
fp <- mallocForeignPtrArray (nc * n)
n' <- withForeignPtr fp $ flip (hGetBuf h) n
if n' == 0
then return Nothing
else liftM Just $ fromForeignPtr fp 0 (nc * n')
where
nc = channels (hInfo h)
hGetContents :: (Sample e, Buffer a e) => Handle -> IO (Info, Maybe (a e))
hGetContents h = (,) info `fmap` hGetBuffer h (frames info)
where info = hInfo h
readFile :: (Sample e, Buffer a e) => FilePath -> IO (Info, Maybe (a e))
readFile path = do
bracket
(openFile path ReadMode defaultInfo)
(hClose)
(hGetContents)
hPutBuffer :: forall a e . (Sample e, Storable e, Buffer a e) => Handle -> a e -> IO Count
hPutBuffer h buffer = do
(fp, i, n) <- toForeignPtr buffer
if n `mod` numChannels /= 0
then error "hPutBuffer: invalid buffer size (not a multiple of channel count)"
else withForeignPtr fp $ \ptr ->
hPutBuf h (ptr `advancePtr` i) (n `div` numChannels)
where
numChannels = channels $ hInfo h
writeFile :: (Sample e, Buffer a e) => Info -> FilePath -> a e -> IO Count
writeFile info path buffer = do
bracket
(openFile path WriteMode info)
(hClose)
(flip hPutBuffer buffer)