chunk — Read IFF chunked data¶
Source code:Lib/chunk.py
Deprecated since version 3.11, will be removed in version 3.13:Thechunk module is deprecated(seePEP 594 for details).
This module provides an interface for reading files that use EA IFF 85 chunks.[1] This format is used in at least the Audio Interchange File Format(AIFF/AIFF-C) and the Real Media File Format (RMFF). The WAVE audio file formatis closely related and can also be read using this module.
A chunk has the following structure:
Offset | Length | Contents |
|---|---|---|
0 | 4 | Chunk ID |
4 | 4 | Size of chunk in big-endianbyte order, not including theheader |
8 | n | Data bytes, wheren is thesize given in the precedingfield |
8 +n | 0 or 1 | Pad byte needed ifn is oddand chunk alignment is used |
The ID is a 4-byte string which identifies the type of chunk.
The size field (a 32-bit value, encoded using big-endian byte order) gives thesize of the chunk data, not including the 8-byte header.
Usually an IFF-type file consists of one or more chunks. The proposed usage oftheChunk class defined here is to instantiate an instance at the startof each chunk and read from the instance until it reaches the end, after which anew instance can be instantiated. At the end of the file, creating a newinstance will fail with anEOFError exception.
- classchunk.Chunk(file,align=True,bigendian=True,inclheader=False)¶
Class which represents a chunk. Thefile argument is expected to be afile-like object. An instance of this class is specifically allowed. Theonly method that is needed is
read(). If the methodsseek()andtell()are present and don’traise an exception, they are also used.If these methods are present and raise an exception, they are expected to nothave altered the object. If the optional argumentalign is true, chunksare assumed to be aligned on 2-byte boundaries. Ifalign is false, noalignment is assumed. The default value is true. If the optional argumentbigendian is false, the chunk size is assumed to be in little-endian order.This is needed for WAVE audio files. The default value is true. If theoptional argumentinclheader is true, the size given in the chunk headerincludes the size of the header. The default value is false.A
Chunkobject supports the following methods:- getname()¶
Returns the name (ID) of the chunk. This is the first 4 bytes of thechunk.
- getsize()¶
Returns the size of the chunk.
- close()¶
Close and skip to the end of the chunk. This does not close theunderlying file.
The remaining methods will raise
OSErrorif called after theclose()method has been called. Before Python 3.3, they used toraiseIOError, now an alias ofOSError.- isatty()¶
Returns
False.
- seek(pos,whence=0)¶
Set the chunk’s current position. Thewhence argument is optional anddefaults to
0(absolute file positioning); other values are1(seek relative to the current position) and2(seek relative to thefile’s end). There is no return value. If the underlying file does notallow seek, only forward seeks are allowed.
- tell()¶
Return the current position into the chunk.
- read(size=-1)¶
Read at mostsize bytes from the chunk (less if the read hits the end ofthe chunk before obtainingsize bytes). If thesize argument isnegative or omitted, read all data until the end of the chunk. An emptybytes object is returned when the end of the chunk is encounteredimmediately.
Footnotes
[1]“EA IFF 85” Standard for Interchange Format Files, Jerry Morrison, ElectronicArts, January 1985.