hsndfile-0.8.0: Haskell bindings for libsndfile

Safe HaskellNone



This module provides the Buffer type class that abstracts the array type that is being used for I/O. For concrete instances see for example the hsndfile-vector package http://hackage.haskell.org/package/hsndfile-vector.



class Storable e => Sample e where Source

The class Sample is used for polymorphic I/O on a Handle, and is parameterized with the element type that is to be read from a file.

It is important to note that the data type used by the calling program and the data format of the file do not need to be the same. For instance, it is possible to open a 16 bit PCM encoded WAV file and read the data in floating point format. The library seamlessly converts between the two formats on-the-fly; the Haskell interface currently supports reading and writing Double or Float floating point values, as well as Int16 and Int32 integer values.

When converting between integer data and floating point data, the following rules apply: The default behaviour when reading floating point data from a file with integer data is normalisation. Regardless of whether data in the file is 8, 16, 24 or 32 bit wide, the data will be read as floating point data in the range [-1.0, 1.0]. Similarly, data in the range [-1.0, 1.0] will be written to an integer PCM file so that a data value of 1.0 will be the largest allowable integer for the given bit width. This normalisation can be turned on or off using the command interface (implementation missing in Haskell).

hGetSamples and hGetFrames return the number of items read. Unless the end of the file was reached during the read, the return value should equal the number of items requested. Attempts to read beyond the end of the file will not result in an error but will cause the read functions to return less than the number of items requested or 0 if already at the end of the file.


hGetBuf :: Handle -> Ptr e -> Count -> IO Count Source

Read a buffer of frames.

hPutBuf :: Handle -> Ptr e -> Count -> IO Count Source

Write a buffer of frames.

class Buffer a e where Source

Buffer class for I/O on soundfile handles.


fromForeignPtr :: ForeignPtr e -> Int -> Int -> IO (a e) Source

Construct a buffer from a ForeignPtr, a start index and the element count.

toForeignPtr :: a e -> IO (ForeignPtr e, Int, Int) Source

Retrieve from a buffer a ForeignPtr pointing to its data, a start index and an element count.

hGetBuffer :: forall a e. (Sample e, Storable e, Buffer a e) => Handle -> Count -> IO (Maybe (a e)) Source

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.

hGetContents :: (Sample e, Buffer a e) => Handle -> IO (Info, Maybe (a e)) Source

Return the contents of a handle open for reading in a single buffer.

readFile :: (Sample e, Buffer a e) => FilePath -> IO (Info, Maybe (a e)) Source

Return the contents of a file in a single buffer.

hPutBuffer :: forall a e. (Sample e, Storable e, Buffer a e) => Handle -> a e -> IO Count Source

Write the contents of a buffer to a handle open for writing.

Return the number of frames written.

writeFile :: (Sample e, Buffer a e) => Info -> FilePath -> a e -> IO Count Source

Write the contents of a buffer to a file. Return the number of frames written.