blob: 8e2a4947f4d1844ba5be97e1cb3e28857a49e526 [file] [log] [blame]
Fred Drake97793ab1999-06-22 18:49:20 +00001\section{\module{chunk} ---
Fred Drake60b66e11999-06-25 17:52:17 +00002 Read IFF chunked data}
Fred Drake97793ab1999-06-22 18:49:20 +00003
4\declaremodule{standard}{chunk}
Fred Drake60b66e11999-06-25 17:52:17 +00005\modulesynopsis{Module to read IFF chunks.}
6\moduleauthor{Sjoerd Mullender}{sjoerd@acm.org}
7\sectionauthor{Sjoerd Mullender}{sjoerd@acm.org}
Fred Drake97793ab1999-06-22 18:49:20 +00008
Fred Drake97793ab1999-06-22 18:49:20 +00009
Fred Drake97793ab1999-06-22 18:49:20 +000010
Fred Drake60b66e11999-06-25 17:52:17 +000011This module provides an interface for reading files that use EA IFF 85
12chunks.\footnote{``EA IFF 85'' Standard for Interchange Format Files,
13Jerry Morrison, Electronic Arts, January 1985.} This format is used
14in at least the Audio\index{Audio Interchange File
15Format}\index{AIFF}\index{AIFF-C} Interchange File Format
Guido van Rossume7f19201999-08-26 15:57:44 +000016(AIFF/AIFF-C) and the Real\index{Real Media File Format} Media File
17Format\index{RMFF} (RMFF). The WAVE audio file format is closely
18related and can also be read using this module.
Fred Drake97793ab1999-06-22 18:49:20 +000019
Fred Drake60b66e11999-06-25 17:52:17 +000020A chunk has the following structure:
Fred Drake97793ab1999-06-22 18:49:20 +000021
22\begin{tableiii}{c|c|l}{textrm}{Offset}{Length}{Contents}
23 \lineiii{0}{4}{Chunk ID}
Guido van Rossume7f19201999-08-26 15:57:44 +000024 \lineiii{4}{4}{Size of chunk in big-endian byte order, not including the
Fred Drake97793ab1999-06-22 18:49:20 +000025 header}
Fred Drake60b66e11999-06-25 17:52:17 +000026 \lineiii{8}{\var{n}}{Data bytes, where \var{n} is the size given in
Thomas Woutersf8316632000-07-16 19:01:10 +000027 the preceding field}
Fred Drake60b66e11999-06-25 17:52:17 +000028 \lineiii{8 + \var{n}}{0 or 1}{Pad byte needed if \var{n} is odd and
29 chunk alignment is used}
Fred Drake97793ab1999-06-22 18:49:20 +000030\end{tableiii}
31
Fred Drake60b66e11999-06-25 17:52:17 +000032The ID is a 4-byte string which identifies the type of chunk.
Fred Drake97793ab1999-06-22 18:49:20 +000033
Fred Drake60b66e11999-06-25 17:52:17 +000034The size field (a 32-bit value, encoded using big-endian byte order)
Guido van Rossume7f19201999-08-26 15:57:44 +000035gives the size of the chunk data, not including the 8-byte header.
Fred Drake97793ab1999-06-22 18:49:20 +000036
Fred Drake60b66e11999-06-25 17:52:17 +000037Usually an IFF-type file consists of one or more chunks. The proposed
38usage of the \class{Chunk} class defined here is to instantiate an
39instance at the start of each chunk and read from the instance until
40it reaches the end, after which a new instance can be instantiated.
41At the end of the file, creating a new instance will fail with a
42\exception{EOFError} exception.
43
Guido van Rossume7f19201999-08-26 15:57:44 +000044\begin{classdesc}{Chunk}{file\optional{, align, bigendian, inclheader}}
Fred Drake60b66e11999-06-25 17:52:17 +000045Class which represents a chunk. The \var{file} argument is expected
46to be a file-like object. An instance of this class is specifically
47allowed. The only method that is needed is \method{read()}. If the
48methods \method{seek()} and \method{tell()} are present and don't
49raise an exception, they are also used. If these methods are present
50and raise an exception, they are expected to not have altered the
51object. If the optional argument \var{align} is true, chunks are
52assumed to be aligned on 2-byte boundaries. If \var{align} is
Guido van Rossume7f19201999-08-26 15:57:44 +000053false, no alignment is assumed. The default value is true. If the
54optional argument \var{bigendian} is false, the chunk size is assumed
55to be in little-endian order. This is needed for WAVE audio files.
56The default value is true. If the optional argument \var{inclheader}
57is true, the size given in the chunk header includes the size of the
58header. The default value is false.
Fred Drake60b66e11999-06-25 17:52:17 +000059\end{classdesc}
60
61A \class{Chunk} object supports the following methods:
Fred Drake97793ab1999-06-22 18:49:20 +000062
63\begin{methoddesc}{getname}{}
Fred Drake60b66e11999-06-25 17:52:17 +000064Returns the name (ID) of the chunk. This is the first 4 bytes of the
65chunk.
Fred Drake97793ab1999-06-22 18:49:20 +000066\end{methoddesc}
67
Guido van Rossume7f19201999-08-26 15:57:44 +000068\begin{methoddesc}{getsize}{}
69Returns the size of the chunk.
70\end{methoddesc}
71
Fred Drake97793ab1999-06-22 18:49:20 +000072\begin{methoddesc}{close}{}
Fred Drake60b66e11999-06-25 17:52:17 +000073Close and skip to the end of the chunk. This does not close the
74underlying file.
Fred Drake97793ab1999-06-22 18:49:20 +000075\end{methoddesc}
76
Fred Drake60b66e11999-06-25 17:52:17 +000077The remaining methods will raise \exception{IOError} if called after
78the \method{close()} method has been called.
79
Fred Drake97793ab1999-06-22 18:49:20 +000080\begin{methoddesc}{isatty}{}
Neal Norwitz6b353702002-04-09 18:15:00 +000081Returns \code{False}.
Fred Drake97793ab1999-06-22 18:49:20 +000082\end{methoddesc}
83
Fred Drake60b66e11999-06-25 17:52:17 +000084\begin{methoddesc}{seek}{pos\optional{, whence}}
85Set the chunk's current position. The \var{whence} argument is
86optional and defaults to \code{0} (absolute file positioning); other
87values 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.
89If the underlying file does not allow seek, only forward seeks are
90allowed.
Fred Drake97793ab1999-06-22 18:49:20 +000091\end{methoddesc}
92
93\begin{methoddesc}{tell}{}
Fred Drake60b66e11999-06-25 17:52:17 +000094Return the current position into the chunk.
Fred Drake97793ab1999-06-22 18:49:20 +000095\end{methoddesc}
96
Fred Drake60b66e11999-06-25 17:52:17 +000097\begin{methoddesc}{read}{\optional{size}}
98Read at most \var{size} bytes from the chunk (less if the read hits
99the end of the chunk before obtaining \var{size} bytes). If the
100\var{size} argument is negative or omitted, read all data until the
101end of the chunk. The bytes are returned as a string object. An
102empty string is returned when the end of the chunk is encountered
103immediately.
Fred Drake97793ab1999-06-22 18:49:20 +0000104\end{methoddesc}
105
106\begin{methoddesc}{skip}{}
107Skip to the end of the chunk. All further calls to \method{read()}
108for the chunk will return \code{''}. If you are not interested in the
109contents of the chunk, this method should be called so that the file
110points to the start of the next chunk.
111\end{methoddesc}