• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
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 an :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:`~file.read`.  If the methods
59   :meth:`~file.seek` and :meth:`~file.tell` are present and don't
60   raise an exception, they are also used.
61   If these methods are present and raise an exception, they are expected to not
62   have altered the object.  If the optional argument *align* is true, chunks
63   are assumed to be aligned on 2-byte boundaries.  If *align* is false, no
64   alignment is assumed.  The default value is true.  If the optional argument
65   *bigendian* is false, the chunk size is assumed to be in little-endian order.
66   This is needed for WAVE audio files. The default value is true.  If the
67   optional argument *inclheader* is true, the size given in the chunk header
68   includes the size of the header.  The default value is false.
69
70   A :class:`Chunk` object supports the following methods:
71
72
73   .. method:: getname()
74
75      Returns the name (ID) of the chunk.  This is the first 4 bytes of the
76      chunk.
77
78
79   .. method:: getsize()
80
81      Returns the size of the chunk.
82
83
84   .. method:: close()
85
86      Close and skip to the end of the chunk.  This does not close the
87      underlying file.
88
89   The remaining methods will raise :exc:`IOError` if called after the
90   :meth:`close` method has been called.
91
92
93   .. method:: isatty()
94
95      Returns ``False``.
96
97
98   .. method:: seek(pos[, whence])
99
100      Set the chunk's current position.  The *whence* argument is optional and
101      defaults to ``0`` (absolute file positioning); other values are ``1``
102      (seek relative to the current position) and ``2`` (seek relative to the
103      file's end).  There is no return value. If the underlying file does not
104      allow seek, only forward seeks are allowed.
105
106
107   .. method:: tell()
108
109      Return the current position into the chunk.
110
111
112   .. method:: read([size])
113
114      Read at most *size* bytes from the chunk (less if the read hits the end of
115      the chunk before obtaining *size* bytes).  If the *size* argument is
116      negative or omitted, read all data until the end of the chunk.  The bytes
117      are returned as a string object.  An empty string is returned when the end
118      of the chunk is encountered immediately.
119
120
121   .. method:: skip()
122
123      Skip to the end of the chunk.  All further calls to :meth:`read` for the
124      chunk will return ``''``.  If you are not interested in the contents of
125      the chunk, this method should be called so that the file points to the
126      start of the next chunk.
127
128
129.. rubric:: Footnotes
130
131.. [#] "EA IFF 85" Standard for Interchange Format Files, Jerry Morrison, Electronic
132   Arts, January 1985.
133
134