[Haskell-cafe] library to read/write audio files

John Lato jwlato at gmail.com
Tue Dec 11 12:14:34 EST 2007


I've been working on a library to encode/decode audio files (wave,
aiff, etc.) to and from lazy bytestrings, and it's finally in a form
where I'm willing to share.  It's available at
http://mml.music.utexas.edu/jwlato/HSoundFile/, lightly cabalized and
haddock-ified.  The basic item is a Data.SoundFile datatype and a
SndFileCls class.  Each file format (wave, etc.) has a datatype with
instances of SndFileCls and Data.Binary (thanks to Don S. for
suggesting Data.Binary).  At least that's the idea, I've only
implemented the Wave format so far, but I wanted to make it easy to
add new formats.

I originally intended to use Erik de Castro Lopo's excellent
libsndfile (and there are some remnants of that in darcs), but after
realizing I wanted a more functional interface I decided to roll my
own.  It's likely possible to wrap libsndfile in a functional
interface, but that is beyond my current abilities.

This library is far from complete.  Firstly, only the wave format is
implemented (and incompletely).  There are also only minimal HUnit
tests; I plan to add QuickCheck tests when I have some more time.
Secondly there are performance issues.  For relatively short clips
it's fine, but there seems to be a space leak.  Attempting to copy
large files while just decoding and encoding the data will quickly
exhaust available memory.  Other than the space leak; performance is
good.  Profiling indicates that reading is extremely fast, and the
slowest operations during write are the multiple calls to
Data.Binary.Put.putWord16le (for 16-bit files).  Also, sound data is
currently read into a list.  I will probably change this at some point
in the future, most likely copying the lazy bytestring implementation
and using a list of CFloat arrays.

If anyone would care to offer suggestions (or patches), they would be
greatly appreciated.  This is my first real project in Haskell, so
please be kind.  I've successfully build it on ubuntu linux with GHC
6.6.1 and 6.8.1, and I don't see any reason it won't work on other

Thank you,
John Lato

PS  I'd appreciate knowing if this project is reinventing the wheel.
I didn't see anything similar in hoogle or google, but you never know.

More information about the Haskell-Cafe mailing list