| 1 | \section{\module{chunk} ---
|
|---|
| 2 | Read IFF chunked data}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{chunk}
|
|---|
| 5 | \modulesynopsis{Module to read IFF chunks.}
|
|---|
| 6 | \moduleauthor{Sjoerd Mullender}{[email protected]}
|
|---|
| 7 | \sectionauthor{Sjoerd Mullender}{[email protected]}
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | This module provides an interface for reading files that use EA IFF 85
|
|---|
| 12 | chunks.\footnote{``EA IFF 85'' Standard for Interchange Format Files,
|
|---|
| 13 | Jerry Morrison, Electronic Arts, January 1985.} This format is used
|
|---|
| 14 | in at least the Audio\index{Audio Interchange File
|
|---|
| 15 | Format}\index{AIFF}\index{AIFF-C} Interchange File Format
|
|---|
| 16 | (AIFF/AIFF-C) and the Real\index{Real Media File Format} Media File
|
|---|
| 17 | Format\index{RMFF} (RMFF). The WAVE audio file format is closely
|
|---|
| 18 | related and can also be read using this module.
|
|---|
| 19 |
|
|---|
| 20 | A chunk has the following structure:
|
|---|
| 21 |
|
|---|
| 22 | \begin{tableiii}{c|c|l}{textrm}{Offset}{Length}{Contents}
|
|---|
| 23 | \lineiii{0}{4}{Chunk ID}
|
|---|
| 24 | \lineiii{4}{4}{Size of chunk in big-endian byte order, not including the
|
|---|
| 25 | header}
|
|---|
| 26 | \lineiii{8}{\var{n}}{Data bytes, where \var{n} is the size given in
|
|---|
| 27 | the preceding field}
|
|---|
| 28 | \lineiii{8 + \var{n}}{0 or 1}{Pad byte needed if \var{n} is odd and
|
|---|
| 29 | chunk alignment is used}
|
|---|
| 30 | \end{tableiii}
|
|---|
| 31 |
|
|---|
| 32 | The ID is a 4-byte string which identifies the type of chunk.
|
|---|
| 33 |
|
|---|
| 34 | The size field (a 32-bit value, encoded using big-endian byte order)
|
|---|
| 35 | gives the size of the chunk data, not including the 8-byte header.
|
|---|
| 36 |
|
|---|
| 37 | Usually an IFF-type file consists of one or more chunks. The proposed
|
|---|
| 38 | usage of the \class{Chunk} class defined here is to instantiate an
|
|---|
| 39 | instance at the start of each chunk and read from the instance until
|
|---|
| 40 | it reaches the end, after which a new instance can be instantiated.
|
|---|
| 41 | At the end of the file, creating a new instance will fail with a
|
|---|
| 42 | \exception{EOFError} exception.
|
|---|
| 43 |
|
|---|
| 44 | \begin{classdesc}{Chunk}{file\optional{, align, bigendian, inclheader}}
|
|---|
| 45 | Class which represents a chunk. The \var{file} argument is expected
|
|---|
| 46 | to be a file-like object. An instance of this class is specifically
|
|---|
| 47 | allowed. The only method that is needed is \method{read()}. If the
|
|---|
| 48 | methods \method{seek()} and \method{tell()} are present and don't
|
|---|
| 49 | raise an exception, they are also used. If these methods are present
|
|---|
| 50 | and raise an exception, they are expected to not have altered the
|
|---|
| 51 | object. If the optional argument \var{align} is true, chunks are
|
|---|
| 52 | assumed to be aligned on 2-byte boundaries. If \var{align} is
|
|---|
| 53 | false, no alignment is assumed. The default value is true. If the
|
|---|
| 54 | optional argument \var{bigendian} is false, the chunk size is assumed
|
|---|
| 55 | to be in little-endian order. This is needed for WAVE audio files.
|
|---|
| 56 | The default value is true. If the optional argument \var{inclheader}
|
|---|
| 57 | is true, the size given in the chunk header includes the size of the
|
|---|
| 58 | header. The default value is false.
|
|---|
| 59 | \end{classdesc}
|
|---|
| 60 |
|
|---|
| 61 | A \class{Chunk} object supports the following methods:
|
|---|
| 62 |
|
|---|
| 63 | \begin{methoddesc}{getname}{}
|
|---|
| 64 | Returns the name (ID) of the chunk. This is the first 4 bytes of the
|
|---|
| 65 | chunk.
|
|---|
| 66 | \end{methoddesc}
|
|---|
| 67 |
|
|---|
| 68 | \begin{methoddesc}{getsize}{}
|
|---|
| 69 | Returns the size of the chunk.
|
|---|
| 70 | \end{methoddesc}
|
|---|
| 71 |
|
|---|
| 72 | \begin{methoddesc}{close}{}
|
|---|
| 73 | Close and skip to the end of the chunk. This does not close the
|
|---|
| 74 | underlying file.
|
|---|
| 75 | \end{methoddesc}
|
|---|
| 76 |
|
|---|
| 77 | The remaining methods will raise \exception{IOError} if called after
|
|---|
| 78 | the \method{close()} method has been called.
|
|---|
| 79 |
|
|---|
| 80 | \begin{methoddesc}{isatty}{}
|
|---|
| 81 | Returns \code{False}.
|
|---|
| 82 | \end{methoddesc}
|
|---|
| 83 |
|
|---|
| 84 | \begin{methoddesc}{seek}{pos\optional{, whence}}
|
|---|
| 85 | Set the chunk's current position. The \var{whence} argument is
|
|---|
| 86 | optional and defaults to \code{0} (absolute file positioning); other
|
|---|
| 87 | values are \code{1} (seek relative to the current position) and
|
|---|
| 88 | \code{2} (seek relative to the file's end). There is no return value.
|
|---|
| 89 | If the underlying file does not allow seek, only forward seeks are
|
|---|
| 90 | allowed.
|
|---|
| 91 | \end{methoddesc}
|
|---|
| 92 |
|
|---|
| 93 | \begin{methoddesc}{tell}{}
|
|---|
| 94 | Return the current position into the chunk.
|
|---|
| 95 | \end{methoddesc}
|
|---|
| 96 |
|
|---|
| 97 | \begin{methoddesc}{read}{\optional{size}}
|
|---|
| 98 | Read at most \var{size} bytes from the chunk (less if the read hits
|
|---|
| 99 | the end of the chunk before obtaining \var{size} bytes). If the
|
|---|
| 100 | \var{size} argument is negative or omitted, read all data until the
|
|---|
| 101 | end of the chunk. The bytes are returned as a string object. An
|
|---|
| 102 | empty string is returned when the end of the chunk is encountered
|
|---|
| 103 | immediately.
|
|---|
| 104 | \end{methoddesc}
|
|---|
| 105 |
|
|---|
| 106 | \begin{methoddesc}{skip}{}
|
|---|
| 107 | Skip to the end of the chunk. All further calls to \method{read()}
|
|---|
| 108 | for the chunk will return \code{''}. If you are not interested in the
|
|---|
| 109 | contents of the chunk, this method should be called so that the file
|
|---|
| 110 | points to the start of the next chunk.
|
|---|
| 111 | \end{methoddesc}
|
|---|