hsndfile 0.5.0 → 0.5.1
raw patch · 7 files changed
+44/−277 lines, 7 filesdep ~basesetup-changed
Dependency ranges changed: base
Files
- C2HS.hs +0/−220
- README.md +1/−1
- Setup.hs +2/−0
- Sound/File/Sndfile.hs +0/−1
- Sound/File/Sndfile/Buffer.hs +10/−35
- Sound/File/Sndfile/Interface.chs +28/−16
- hsndfile.cabal +3/−4
− C2HS.hs
@@ -1,220 +0,0 @@--- C->Haskell Compiler: Marshalling library------ Copyright (c) [1999...2005] Manuel M T Chakravarty------ Redistribution and use in source and binary forms, with or without--- modification, are permitted provided that the following conditions are met:--- --- 1. Redistributions of source code must retain the above copyright notice,--- this list of conditions and the following disclaimer. --- 2. Redistributions in binary form must reproduce the above copyright--- notice, this list of conditions and the following disclaimer in the--- documentation and/or other materials provided with the distribution. --- 3. The name of the author may not be used to endorse or promote products--- derived from this software without specific prior written permission. ------ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR--- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES--- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN--- NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED--- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR--- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF--- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING--- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS--- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.------- Description --------------------------------------------------------------------- Language: Haskell 98------ This module provides the marshaling routines for Haskell files produced by --- C->Haskell for binding to C library interfaces. It exports all of the--- low-level FFI (language-independent plus the C-specific parts) together--- with the C->HS-specific higher-level marshalling routines.-----module C2HS (-- -- * Re-export the language-independent component of the FFI - module Foreign,-- -- * Re-export the C language component of the FFI- module CForeign,-- -- * Composite marshalling functions- withCStringLenIntConv, peekCStringLenIntConv, withIntConv, withFloatConv,- peekIntConv, peekFloatConv, withBool, peekBool, withEnum, peekEnum,-- -- * Conditional results using 'Maybe'- nothingIf, nothingIfNull,-- -- * Bit masks- combineBitMasks, containsBitMask, extractBitMasks,-- -- * Conversion between C and Haskell types- cIntConv, cFloatConv, cToBool, cFromBool, cToEnum, cFromEnum-) where ---import Foreign- hiding (Word)- -- Should also hide the Foreign.Marshal.Pool exports in- -- compilers that export them-import CForeign--import Monad (when, liftM)----- Composite marshalling functions--- ----------------------------------- Strings with explicit length----withCStringLenIntConv s f = withCStringLen s $ \(p, n) -> f (p, cIntConv n)-peekCStringLenIntConv (s, n) = peekCStringLen (s, cIntConv n)---- Marshalling of numerals-----withIntConv :: (Storable b, Integral a, Integral b) - => a -> (Ptr b -> IO c) -> IO c-withIntConv = with . cIntConv--withFloatConv :: (Storable b, RealFloat a, RealFloat b) - => a -> (Ptr b -> IO c) -> IO c-withFloatConv = with . cFloatConv--peekIntConv :: (Storable a, Integral a, Integral b) - => Ptr a -> IO b-peekIntConv = liftM cIntConv . peek--peekFloatConv :: (Storable a, RealFloat a, RealFloat b) - => Ptr a -> IO b-peekFloatConv = liftM cFloatConv . peek---- Passing Booleans by reference-----withBool :: (Integral a, Storable a) => Bool -> (Ptr a -> IO b) -> IO b-withBool = with . fromBool--peekBool :: (Integral a, Storable a) => Ptr a -> IO Bool-peekBool = liftM toBool . peek----- Passing enums by reference-----withEnum :: (Enum a, Integral b, Storable b) => a -> (Ptr b -> IO c) -> IO c-withEnum = with . cFromEnum--peekEnum :: (Enum a, Integral b, Storable b) => Ptr b -> IO a-peekEnum = liftM cToEnum . peek----- Storing of 'Maybe' values--- ---------------------------instance Storable a => Storable (Maybe a) where- sizeOf _ = sizeOf (undefined :: Ptr ())- alignment _ = alignment (undefined :: Ptr ())-- peek p = do- ptr <- peek (castPtr p)- if ptr == nullPtr- then return Nothing- else liftM Just $ peek ptr-- poke p v = do- ptr <- case v of- Nothing -> return nullPtr- Just v' -> new v'- poke (castPtr p) ptr----- Conditional results using 'Maybe'--- ------------------------------------- Wrap the result into a 'Maybe' type.------ * the predicate determines when the result is considered to be non-existing,--- ie, it is represented by `Nothing'------ * the second argument allows to map a result wrapped into `Just' to some--- other domain----nothingIf :: (a -> Bool) -> (a -> b) -> a -> Maybe b-nothingIf p f x = if p x then Nothing else Just $ f x---- |Instance for special casing null pointers.----nothingIfNull :: (Ptr a -> b) -> Ptr a -> Maybe b-nothingIfNull = nothingIf (== nullPtr)----- Support for bit masks--- ------------------------- Given a list of enumeration values that represent bit masks, combine these--- masks using bitwise disjunction.----combineBitMasks :: (Enum a, Bits b) => [a] -> b-combineBitMasks = foldl (.|.) 0 . map (fromIntegral . fromEnum)---- Tests whether the given bit mask is contained in the given bit pattern--- (i.e., all bits set in the mask are also set in the pattern).----containsBitMask :: (Bits a, Enum b) => a -> b -> Bool-bits `containsBitMask` bm = let bm' = fromIntegral . fromEnum $ bm- in- bm' .&. bits == bm'---- |Given a bit pattern, yield all bit masks that it contains.------ * This does *not* attempt to compute a minimal set of bit masks that when--- combined yield the bit pattern, instead all contained bit masks are--- produced.----extractBitMasks :: (Bits a, Enum b, Bounded b) => a -> [b]-extractBitMasks bits = - [bm | bm <- [minBound..maxBound], bits `containsBitMask` bm]----- Conversion routines--- ----------------------- |Integral conversion----cIntConv :: (Integral a, Integral b) => a -> b-cIntConv = fromIntegral---- |Floating conversion----cFloatConv :: (RealFloat a, RealFloat b) => a -> b-cFloatConv = realToFrac--- As this conversion by default goes via `Rational', it can be very slow...-{-# RULES - "cFloatConv/Float->Float" forall (x::Float). cFloatConv x = x;- "cFloatConv/Double->Double" forall (x::Double). cFloatConv x = x- #-}---- |Obtain C value from Haskell 'Bool'.----cFromBool :: Num a => Bool -> a-cFromBool = fromBool---- |Obtain Haskell 'Bool' from C value.----cToBool :: Num a => a -> Bool-cToBool = toBool---- |Convert a C enumeration to Haskell.----cToEnum :: (Integral i, Enum e) => i -> e-cToEnum = toEnum . cIntConv---- |Convert a Haskell enumeration to C.----cFromEnum :: (Enum e, Integral i) => e -> i-cFromEnum = cIntConv . fromEnum
README.md view
@@ -1,4 +1,4 @@-**hsndfile** is a Haskell interface to Eric de Castro Lopo's [libsndfile][]. For more detailed information please visit [**hsndfile**'s homepage][hsndfile].+**hsndfile** is a Haskell interface to Erik de Castro Lopo's [libsndfile][]. For more detailed information please visit [**hsndfile**'s homepage][hsndfile]. [libsndfile]: http://www.mega-nerd.com/libsndfile/ [hsndfile]: http://haskell.org/haskellwiki/Hsndfile
Setup.hs view
@@ -1,4 +1,6 @@ #!/usr/bin/env runhaskell import Distribution.Simple++main :: IO () main = defaultMain
Sound/File/Sndfile.hs view
@@ -21,7 +21,6 @@ , Buffer(..) , hGetBuffer , hGetContents, readFile- -- , hGetContentChunks, readFileChunks , hPutBuffer, writeFile -- *Exception handling , Exception(..), catch
Sound/File/Sndfile/Buffer.hs view
@@ -6,19 +6,17 @@ , hGetBuffer , hGetContents , readFile- -- , hGetContentChunks- -- , readFileChunks , hPutBuffer , writeFile ) where import Control.Exception (bracket)+import Control.Monad import Foreign import Prelude hiding (readFile, writeFile) import Sound.File.Sndfile.Exception (throw) import Sound.File.Sndfile.Interface import Sound.File.Sndfile.Buffer.Sample (Sample(..))-import System.IO.Unsafe (unsafeInterleaveIO) -- | Buffer class for I\/O on soundfile handles. class Buffer a e where@@ -28,38 +26,23 @@ toForeignPtr :: a e -> IO (ForeignPtr e, Int, Int) -- | Return an buffer with the requested number of frames of data.+-- -- The resulting buffer size is equal to the product of the number of frames `n' and the number of channels in the soundfile. hGetBuffer :: forall a e . (Sample e, Storable e, Buffer a e) => Handle -> Count -> IO (Maybe (a e)) hGetBuffer h n = do- p <- mallocBytes (sizeOf (undefined :: e) * numChannels * n)- n' <- hGetBuf h p n+ fp <- mallocForeignPtrArray (nc * n)+ n' <- withForeignPtr fp $ flip (hGetBuf h) n if n' == 0 then return Nothing- else do- fp <- newForeignPtr finalizerFree p- Just `fmap` fromForeignPtr fp 0 (n * numChannels)+ else liftM Just $ fromForeignPtr fp 0 (nc * n') where- numChannels = (channels.hInfo) h+ nc = channels (hInfo h) -- | Return the contents of a handle open for reading in a single buffer. hGetContents :: (Sample e, Buffer a e) => Handle -> IO (Info, Maybe (a e)) hGetContents h = (,) info `fmap` hGetBuffer h (frames info) where info = hInfo h --- | Return the contents of a handle open for reading as a lazy list of buffers.-hGetContentChunks :: (Sample e, Buffer a e) => Count -> Handle -> IO (Info, [a e])-hGetContentChunks n h = (,) (hInfo h) `fmap` lazyread- where- {-# NOINLINE lazyread #-}- lazyread = unsafeInterleaveIO loop- loop = do- r <- hGetBuffer h n- case r of- Just b -> do- bs <- lazyread- return (b:bs)- Nothing -> return []- -- | Return the contents of a file in a single buffer. readFile :: (Sample e, Buffer a e) => FilePath -> IO (Info, Maybe (a e)) readFile path = do@@ -68,25 +51,17 @@ (hClose) (hGetContents) --- | Return the contents of a file as a lazy list of buffers.-readFileChunks :: (Sample e, Buffer a e) => Count -> FilePath -> IO (Info, [a e])-readFileChunks n path = do- bracket- (openFile path ReadMode defaultInfo)- (hClose)- (hGetContentChunks n)- -- | Write the contents of a buffer to a handle open for writing.+-- -- Return the number of frames written. 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 throw 0 "hPutBuffer: invalid buffer size (not a multiple of channel count)"- else do- withForeignPtr fp $ \ptr -> do- let p = plusPtr ptr (sizeOf (undefined :: e) * i) :: Ptr e- hPutBuf h p (n `div` numChannels)+ else+ withForeignPtr fp $ \ptr ->+ hPutBuf h (ptr `advancePtr` i) (n `div` numChannels) where numChannels = channels $ hInfo h
Sound/File/Sndfile/Interface.chs view
@@ -1,17 +1,29 @@ {-# LANGUAGE ForeignFunctionInterface #-} module Sound.File.Sndfile.Interface where -import C2HS-import Control.Monad (liftM, when)-import Data.Bits ((.|.), (.&.))+import Control.Monad (liftM, when)+import Foreign+import Foreign.C+ import qualified Sound.File.Sndfile.Exception as E-import System.IO.Unsafe (unsafePerformIO) +#include <stdint.h> #include <sndfile.h> {#context lib="libsndfile" prefix="sf"#} -- ====================================================================+-- Utilities++-- | Convert a C enumeration to Haskell.+cToEnum :: (Integral i, Enum e) => i -> e+cToEnum = toEnum . fromIntegral++-- | Convert a Haskell enumeration to C.+cFromEnum :: (Enum e, Integral i) => e -> i+cFromEnum = fromIntegral . fromEnum++-- ==================================================================== -- Basic types -- | Type for expressing sample counts.@@ -65,29 +77,29 @@ SAMPLE_FORMAT_PCM_16 = SF_FORMAT_PCM_16, SAMPLE_FORMAT_PCM_24 = SF_FORMAT_PCM_24, SAMPLE_FORMAT_PCM_32 = SF_FORMAT_PCM_32,- + SAMPLE_FORMAT_PCM_U8 = SF_FORMAT_PCM_U8,- + SAMPLE_FORMAT_FLOAT = SF_FORMAT_FLOAT, SAMPLE_FORMAT_DOUBLE = SF_FORMAT_DOUBLE,- + SAMPLE_FORMAT_ULAW = SF_FORMAT_ULAW, SAMPLE_FORMAT_ALAW = SF_FORMAT_ALAW, SAMPLE_FORMAT_IMA_ADPCM = SF_FORMAT_IMA_ADPCM, SAMPLE_FORMAT_MS_ADPCM = SF_FORMAT_MS_ADPCM,- + SAMPLE_FORMAT_GSM610 = SF_FORMAT_GSM610, SAMPLE_FORMAT_VOX_ADPCM = SF_FORMAT_VOX_ADPCM,- + SAMPLE_FORMAT_G721_32 = SF_FORMAT_G721_32, SAMPLE_FORMAT_G723_24 = SF_FORMAT_G723_24, SAMPLE_FORMAT_G723_40 = SF_FORMAT_G723_40,- + SAMPLE_FORMAT_DWVW_12 = SF_FORMAT_DWVW_12, SAMPLE_FORMAT_DWVW_16 = SF_FORMAT_DWVW_16, SAMPLE_FORMAT_DWVW_24 = SF_FORMAT_DWVW_24, SAMPLE_FORMAT_DWVW_N = SF_FORMAT_DWVW_N,- + SAMPLE_FORMAT_FORMAT_DPCM_8 = SF_FORMAT_DPCM_8, SAMPLE_FORMAT_FORMAT_DPCM_16 = SF_FORMAT_DPCM_16 };@@ -174,12 +186,12 @@ {-# NOINLINE checkFormat #-} checkFormat :: Info -> Bool checkFormat info =- unsafePerformIO (with info (liftM cToBool . {#call unsafe sf_format_check#} . castPtr))+ unsafePerformIO (with info (liftM toBool . {#call unsafe sf_format_check#} . castPtr)) -- Storable instance for Info instance Storable (Info) where- alignment _ = alignment (undefined :: CInt) -- hmm sizeOf _ = {#sizeof INFO#}+ alignment _ = {#alignof INFO#} -- Unmarshall Info from C representation peek ptr = do frames <- liftM fromIntegral $ {#get SF_INFO.frames#} ptr@@ -295,7 +307,7 @@ n <- liftM fromIntegral $ {#call unsafe sf_seek#} handle- (cIntConv frames)+ (fromIntegral frames) ((cFromEnum seekMode) .|. (case ioMode of Nothing -> 0 Just m -> cFromEnum m))@@ -313,9 +325,9 @@ -- * 'SeekFromEnd' - The offset is set to the end of the data plus offset (multichannel) frames. -- -- Internally, libsndfile keeps track of the read and write locations using separate read and write pointers. If a file has been opened with a mode of 'ReadWriteMode', calling either 'hSeekRead' or 'hSeekWrite' allows the read and write pointers to be modified separately. 'hSeek' modifies both the read and the write pointer.--- +-- -- Note that the frames offset can be negative and in fact should be when SeekFromEnd is used for the whence parameter.--- +-- -- 'hSeek' will return the offset in (multichannel) frames from the start of the audio data, or signal an error when an attempt is made to seek beyond the start or end of the file. hSeek :: Handle -> SeekMode -> Count -> IO Count
hsndfile.cabal view
@@ -1,5 +1,5 @@ Name: hsndfile-Version: 0.5.0+Version: 0.5.1 Category: Data, Sound License: GPL License-File: COPYING@@ -25,11 +25,10 @@ Library Build-Depends: base >= 4 && < 5, haskell98- Build-Tools: c2hs >= 0.15+ Build-Tools: c2hs >= 0.16.3 Exposed-Modules: Sound.File.Sndfile Sound.File.Sndfile.Buffer- Other-Modules: C2HS- Sound.File.Sndfile.Buffer.Internal+ Other-Modules: Sound.File.Sndfile.Buffer.Internal Sound.File.Sndfile.Buffer.Sample Sound.File.Sndfile.Exception Sound.File.Sndfile.Interface