SoX - Sound eXchange. Tutorials:soundfont:sound_font_utilities.pdf (Objet application/pdf) How to print integer literals in binary or hex in haskell. Chapter 10. Code case study: parsing a binary data format. Chapter 10.
Code case study: parsing a binary data format In this chapter, we'll discuss a common task: parsing a binary file. We will use this task for two purposes. Our first is indeed to talk a little about parsing, but our main goal is to talk about program organisation, refactoring, and “boilerplate removal”. We will demonstrate how you can tidy up repetitious code, and set the stage for our discussion of monads in Chapter 14, Monads.
The file formats that we will work with come from the netpbm suite, an ancient and venerable collection of programs and file formats for working with bitmap images. The name of netpbm's greyscale file format is PGM (“portable grey map”). A file of either format starts with a header, which in turn begins with a “magic” string describing the format. After the maximum grey value comes the image data. A raw file can contain a sequence of images, one after the other, each with its own header. We'll use a straightforward data type to represent PGM images. Dealing with binary data. 1 Handling Binary Data with Haskell Many programming problems call for the use of binary formats for compactness, ease-of-use, compatibility or speed.
This page quickly covers some common libraries for handling binary data in Haskell. 1.1 Bytestrings. Wave File Specifications. File Description: WAVE or RIFF WAVE sound fileFile Extension: Commonly .wav, sometimes .waveFile Byte Order: Little-endian Prof.
Peter Kabal, MMSP Lab, ECE, McGill University: Last update: 2011-01-03 WAVE Specifications The WAVE file specifications came from Microsoft. The WAVE file format use RIFF chunks, each chunk consisting of a chunk identifier, chunk length and chunk data. Data Types The data in WAVE files can be of many different types. Internet RFC, Codec registrations, 1998-06: Local copy: rfc2361.txt Microsoft include files (part of the MSVC compiler or the DirectX SDK: from Microsoft Download Center) Local copy: MMREG.H (Version 1.46) Local copy: ksmedia.h Wave File Format Wave files have a master RIFF chunk which includes a WAVE identifier followed by sub-chunks. Format Chunk The Format chunk specifies the format of the data. The standard format codes for waveform data are given below. PCM Format The first part of the Format chunk is used to describe PCM data. Non-PCM Formats. Microsoft WAVE soundfile format. The WAVE file format is a subset of Microsoft's RIFF specification for the storage of multimedia files.
A RIFF file starts out with a file header followed by a sequence of data chunks. A WAVE file is often just a RIFF file with a single "WAVE" chunk which consists of two sub-chunks -- a "fmt " chunk specifying the data format and a "data" chunk containing the actual sample data. Call this form the "Canonical form". Who knows how it really all works. I use the standard WAVE format as created by the sox program: Offset Size Name Description The canonical WAVE format starts with the RIFF header: 0 4 ChunkID Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form). 4 4 ChunkSize 36 + SubChunk2Size, or more precisely: 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size) This is the size of the rest of the chunk following this number.
As an example, here are the opening 72 bytes of a WAVE file with bytes shown as hexadecimal numbers: Notes: General discussion of RIFF files: References: