blob: 04c7e27a77486891619cbfbaf5f8de8dfaf7f338 [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
Serhiy Storchakab33336f2013-10-13 23:09:00 +030058 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +000061 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
Benjamin Petersonc7b05922008-04-25 01:29:10 +000070 A :class:`Chunk` object supports the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000071
72
Benjamin Petersonc7b05922008-04-25 01:29:10 +000073 .. method:: getname()
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
Benjamin Petersonc7b05922008-04-25 01:29:10 +000075 Returns the name (ID) of the chunk. This is the first 4 bytes of the
76 chunk.
Georg Brandl8ec7f652007-08-15 14:28:01 +000077
78
Benjamin Petersonc7b05922008-04-25 01:29:10 +000079 .. method:: getsize()
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 Returns the size of the chunk.
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83
Benjamin Petersonc7b05922008-04-25 01:29:10 +000084 .. method:: close()
Georg Brandl8ec7f652007-08-15 14:28:01 +000085
Benjamin Petersonc7b05922008-04-25 01:29:10 +000086 Close and skip to the end of the chunk. This does not close the
87 underlying file.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
Benjamin Petersonc7b05922008-04-25 01:29:10 +000089 The remaining methods will raise :exc:`IOError` if called after the
90 :meth:`close` method has been called.
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
92
Benjamin Petersonc7b05922008-04-25 01:29:10 +000093 .. method:: isatty()
Georg Brandl8ec7f652007-08-15 14:28:01 +000094
Benjamin Petersonc7b05922008-04-25 01:29:10 +000095 Returns ``False``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
97
Benjamin Petersonc7b05922008-04-25 01:29:10 +000098 .. method:: seek(pos[, whence])
Georg Brandl8ec7f652007-08-15 14:28:01 +000099
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000100 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
106
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000107 .. method:: tell()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000109 Return the current position into the chunk.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
111
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000112 .. method:: read([size])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000113
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000114 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000119
120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 .. method:: skip()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000123 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
Georg Brandl8ec7f652007-08-15 14:28:01 +0000128
129.. rubric:: Footnotes
130
131.. [#] "EA IFF 85" Standard for Interchange Format Files, Jerry Morrison, Electronic
132 Arts, January 1985.
133