blob: 64ce4e23d2bd9011777666d7b5c01cd5ca479404 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`chunk` --- Read IFF chunked data
3======================================
4
5.. module:: chunk
6 :synopsis: Module to read IFF chunks.
7.. moduleauthor:: Sjoerd Mullender <sjoerd@acm.org>
8.. sectionauthor:: Sjoerd Mullender <sjoerd@acm.org>
9
10
11.. index::
12 single: Audio Interchange File Format
13 single: AIFF
14 single: AIFF-C
15 single: Real Media File Format
16 single: RMFF
17
18This module provides an interface for reading files that use EA IFF 85 chunks.
19[#]_ This format is used in at least the Audio Interchange File Format
20(AIFF/AIFF-C) and the Real Media File Format (RMFF). The WAVE audio file format
21is closely related and can also be read using this module.
22
23A chunk has the following structure:
24
25+---------+--------+-------------------------------+
26| Offset | Length | Contents |
27+=========+========+===============================+
28| 0 | 4 | Chunk ID |
29+---------+--------+-------------------------------+
30| 4 | 4 | Size of chunk in big-endian |
31| | | byte order, not including the |
32| | | header |
33+---------+--------+-------------------------------+
34| 8 | *n* | Data bytes, where *n* is the |
35| | | size given in the preceding |
36| | | field |
37+---------+--------+-------------------------------+
38| 8 + *n* | 0 or 1 | Pad byte needed if *n* is odd |
39| | | and chunk alignment is used |
40+---------+--------+-------------------------------+
41
42The ID is a 4-byte string which identifies the type of chunk.
43
44The size field (a 32-bit value, encoded using big-endian byte order) gives the
45size of the chunk data, not including the 8-byte header.
46
47Usually an IFF-type file consists of one or more chunks. The proposed usage of
48the :class:`Chunk` class defined here is to instantiate an instance at the start
49of each chunk and read from the instance until it reaches the end, after which a
50new instance can be instantiated. At the end of the file, creating a new
51instance will fail with a :exc:`EOFError` exception.
52
53
54.. class:: Chunk(file[, align, bigendian, inclheader])
55
56 Class which represents a chunk. The *file* argument is expected to be a
57 file-like object. An instance of this class is specifically allowed. The
58 only method that is needed is :meth:`read`. If the methods :meth:`seek` and
59 :meth:`tell` are present and don't raise an exception, they are also used.
60 If these methods are present and raise an exception, they are expected to not
61 have altered the object. If the optional argument *align* is true, chunks
62 are assumed to be aligned on 2-byte boundaries. If *align* is false, no
63 alignment is assumed. The default value is true. If the optional argument
64 *bigendian* is false, the chunk size is assumed to be in little-endian order.
65 This is needed for WAVE audio files. The default value is true. If the
66 optional argument *inclheader* is true, the size given in the chunk header
67 includes the size of the header. The default value is false.
68
Benjamin Petersonc7b05922008-04-25 01:29:10 +000069 A :class:`Chunk` object supports the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
71
Benjamin Petersonc7b05922008-04-25 01:29:10 +000072 .. method:: getname()
Georg Brandl8ec7f652007-08-15 14:28:01 +000073
Benjamin Petersonc7b05922008-04-25 01:29:10 +000074 Returns the name (ID) of the chunk. This is the first 4 bytes of the
75 chunk.
Georg Brandl8ec7f652007-08-15 14:28:01 +000076
77
Benjamin Petersonc7b05922008-04-25 01:29:10 +000078 .. method:: getsize()
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
Benjamin Petersonc7b05922008-04-25 01:29:10 +000080 Returns the size of the chunk.
Georg Brandl8ec7f652007-08-15 14:28:01 +000081
82
Benjamin Petersonc7b05922008-04-25 01:29:10 +000083 .. method:: close()
Georg Brandl8ec7f652007-08-15 14:28:01 +000084
Benjamin Petersonc7b05922008-04-25 01:29:10 +000085 Close and skip to the end of the chunk. This does not close the
86 underlying file.
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
Benjamin Petersonc7b05922008-04-25 01:29:10 +000088 The remaining methods will raise :exc:`IOError` if called after the
89 :meth:`close` method has been called.
Georg Brandl8ec7f652007-08-15 14:28:01 +000090
91
Benjamin Petersonc7b05922008-04-25 01:29:10 +000092 .. method:: isatty()
Georg Brandl8ec7f652007-08-15 14:28:01 +000093
Benjamin Petersonc7b05922008-04-25 01:29:10 +000094 Returns ``False``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000095
96
Benjamin Petersonc7b05922008-04-25 01:29:10 +000097 .. method:: seek(pos[, whence])
Georg Brandl8ec7f652007-08-15 14:28:01 +000098
Benjamin Petersonc7b05922008-04-25 01:29:10 +000099 Set the chunk's current position. The *whence* argument is optional and
100 defaults to ``0`` (absolute file positioning); other values are ``1``
101 (seek relative to the current position) and ``2`` (seek relative to the
102 file's end). There is no return value. If the underlying file does not
103 allow seek, only forward seeks are allowed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
105
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000106 .. method:: tell()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000108 Return the current position into the chunk.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000109
110
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000111 .. method:: read([size])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000113 Read at most *size* bytes from the chunk (less if the read hits the end of
114 the chunk before obtaining *size* bytes). If the *size* argument is
115 negative or omitted, read all data until the end of the chunk. The bytes
116 are returned as a string object. An empty string is returned when the end
117 of the chunk is encountered immediately.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000118
119
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000120 .. method:: skip()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000122 Skip to the end of the chunk. All further calls to :meth:`read` for the
123 chunk will return ``''``. If you are not interested in the contents of
124 the chunk, this method should be called so that the file points to the
125 start of the next chunk.
126
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
128.. rubric:: Footnotes
129
130.. [#] "EA IFF 85" Standard for Interchange Format Files, Jerry Morrison, Electronic
131 Arts, January 1985.
132