Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 1 | \section{\module{chunk} --- |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 2 | Read IFF chunked data} |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{standard}{chunk} |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 5 | \modulesynopsis{Module to read IFF chunks.} |
| 6 | \moduleauthor{Sjoerd Mullender}{sjoerd@acm.org} |
| 7 | \sectionauthor{Sjoerd Mullender}{sjoerd@acm.org} |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 8 | |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 9 | |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 10 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e7f1920 | 1999-08-26 15:57:44 +0000 | [diff] [blame] | 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. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 19 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 20 | A chunk has the following structure: |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 21 | |
| 22 | \begin{tableiii}{c|c|l}{textrm}{Offset}{Length}{Contents} |
| 23 | \lineiii{0}{4}{Chunk ID} |
Guido van Rossum | e7f1920 | 1999-08-26 15:57:44 +0000 | [diff] [blame] | 24 | \lineiii{4}{4}{Size of chunk in big-endian byte order, not including the |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 25 | header} |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 26 | \lineiii{8}{\var{n}}{Data bytes, where \var{n} is the size given in |
| 27 | the preceeding field} |
| 28 | \lineiii{8 + \var{n}}{0 or 1}{Pad byte needed if \var{n} is odd and |
| 29 | chunk alignment is used} |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 30 | \end{tableiii} |
| 31 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 32 | The ID is a 4-byte string which identifies the type of chunk. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 33 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 34 | The size field (a 32-bit value, encoded using big-endian byte order) |
Guido van Rossum | e7f1920 | 1999-08-26 15:57:44 +0000 | [diff] [blame] | 35 | gives the size of the chunk data, not including the 8-byte header. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 36 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | e7f1920 | 1999-08-26 15:57:44 +0000 | [diff] [blame] | 44 | \begin{classdesc}{Chunk}{file\optional{, align, bigendian, inclheader}} |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e7f1920 | 1999-08-26 15:57:44 +0000 | [diff] [blame] | 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. |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 59 | \end{classdesc} |
| 60 | |
| 61 | A \class{Chunk} object supports the following methods: |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 62 | |
| 63 | \begin{methoddesc}{getname}{} |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 64 | Returns the name (ID) of the chunk. This is the first 4 bytes of the |
| 65 | chunk. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 66 | \end{methoddesc} |
| 67 | |
Guido van Rossum | e7f1920 | 1999-08-26 15:57:44 +0000 | [diff] [blame] | 68 | \begin{methoddesc}{getsize}{} |
| 69 | Returns the size of the chunk. |
| 70 | \end{methoddesc} |
| 71 | |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 72 | \begin{methoddesc}{close}{} |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 73 | Close and skip to the end of the chunk. This does not close the |
| 74 | underlying file. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 75 | \end{methoddesc} |
| 76 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 77 | The remaining methods will raise \exception{IOError} if called after |
| 78 | the \method{close()} method has been called. |
| 79 | |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 80 | \begin{methoddesc}{isatty}{} |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 81 | Returns \code{0}. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 82 | \end{methoddesc} |
| 83 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 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. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 91 | \end{methoddesc} |
| 92 | |
| 93 | \begin{methoddesc}{tell}{} |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 94 | Return the current position into the chunk. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 95 | \end{methoddesc} |
| 96 | |
Fred Drake | 60b66e1 | 1999-06-25 17:52:17 +0000 | [diff] [blame] | 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. |
Fred Drake | 97793ab | 1999-06-22 18:49:20 +0000 | [diff] [blame] | 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} |