blob: 5e24df923ed21024f986d8597921f1ac6938b344 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`chunk` --- Read IFF chunked data
2======================================
3
4.. module:: chunk
5 :synopsis: Module to read IFF chunks.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Sjoerd Mullender <sjoerd@acm.org>
8.. sectionauthor:: Sjoerd Mullender <sjoerd@acm.org>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/chunk.py`
Georg Brandl116aa622007-08-15 14:28:22 +000011
12.. index::
13 single: Audio Interchange File Format
14 single: AIFF
15 single: AIFF-C
16 single: Real Media File Format
17 single: RMFF
18
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040019--------------
20
Georg Brandl116aa622007-08-15 14:28:22 +000021This module provides an interface for reading files that use EA IFF 85 chunks.
22[#]_ This format is used in at least the Audio Interchange File Format
23(AIFF/AIFF-C) and the Real Media File Format (RMFF). The WAVE audio file format
24is closely related and can also be read using this module.
25
26A chunk has the following structure:
27
28+---------+--------+-------------------------------+
29| Offset | Length | Contents |
30+=========+========+===============================+
31| 0 | 4 | Chunk ID |
32+---------+--------+-------------------------------+
33| 4 | 4 | Size of chunk in big-endian |
34| | | byte order, not including the |
35| | | header |
36+---------+--------+-------------------------------+
37| 8 | *n* | Data bytes, where *n* is the |
38| | | size given in the preceding |
39| | | field |
40+---------+--------+-------------------------------+
41| 8 + *n* | 0 or 1 | Pad byte needed if *n* is odd |
42| | | and chunk alignment is used |
43+---------+--------+-------------------------------+
44
45The ID is a 4-byte string which identifies the type of chunk.
46
47The size field (a 32-bit value, encoded using big-endian byte order) gives the
48size of the chunk data, not including the 8-byte header.
49
50Usually an IFF-type file consists of one or more chunks. The proposed usage of
51the :class:`Chunk` class defined here is to instantiate an instance at the start
52of each chunk and read from the instance until it reaches the end, after which a
53new instance can be instantiated. At the end of the file, creating a new
Martin Panter7462b6492015-11-02 03:37:02 +000054instance will fail with an :exc:`EOFError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +000055
56
Georg Brandl0d8f0732009-04-05 22:20:44 +000057.. class:: Chunk(file, align=True, bigendian=True, inclheader=False)
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 Class which represents a chunk. The *file* argument is expected to be a
60 file-like object. An instance of this class is specifically allowed. The
Serhiy Storchakabfdcd432013-10-13 23:09:14 +030061 only method that is needed is :meth:`~io.IOBase.read`. If the methods
62 :meth:`~io.IOBase.seek` and :meth:`~io.IOBase.tell` are present and don't
63 raise an exception, they are also used.
Georg Brandl116aa622007-08-15 14:28:22 +000064 If these methods are present and raise an exception, they are expected to not
65 have altered the object. If the optional argument *align* is true, chunks
66 are assumed to be aligned on 2-byte boundaries. If *align* is false, no
67 alignment is assumed. The default value is true. If the optional argument
68 *bigendian* is false, the chunk size is assumed to be in little-endian order.
69 This is needed for WAVE audio files. The default value is true. If the
70 optional argument *inclheader* is true, the size given in the chunk header
71 includes the size of the header. The default value is false.
72
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 A :class:`Chunk` object supports the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000074
75
Benjamin Petersone41251e2008-04-25 01:59:09 +000076 .. method:: getname()
Georg Brandl116aa622007-08-15 14:28:22 +000077
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 Returns the name (ID) of the chunk. This is the first 4 bytes of the
79 chunk.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 .. method:: getsize()
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 Returns the size of the chunk.
Georg Brandl116aa622007-08-15 14:28:22 +000085
86
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +000088
Benjamin Petersone41251e2008-04-25 01:59:09 +000089 Close and skip to the end of the chunk. This does not close the
90 underlying file.
Georg Brandl116aa622007-08-15 14:28:22 +000091
Antoine Pitrou4272d6a2011-10-12 19:10:10 +020092 The remaining methods will raise :exc:`OSError` if called after the
93 :meth:`close` method has been called. Before Python 3.3, they used to
94 raise :exc:`IOError`, now an alias of :exc:`OSError`.
Georg Brandl116aa622007-08-15 14:28:22 +000095
96
Benjamin Petersone41251e2008-04-25 01:59:09 +000097 .. method:: isatty()
Georg Brandl116aa622007-08-15 14:28:22 +000098
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 Returns ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101
Georg Brandl0d8f0732009-04-05 22:20:44 +0000102 .. method:: seek(pos, whence=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 Set the chunk's current position. The *whence* argument is optional and
105 defaults to ``0`` (absolute file positioning); other values are ``1``
106 (seek relative to the current position) and ``2`` (seek relative to the
107 file's end). There is no return value. If the underlying file does not
108 allow seek, only forward seeks are allowed.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 .. method:: tell()
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 Return the current position into the chunk.
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115
Georg Brandl0d8f0732009-04-05 22:20:44 +0000116 .. method:: read(size=-1)
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 Read at most *size* bytes from the chunk (less if the read hits the end of
119 the chunk before obtaining *size* bytes). If the *size* argument is
Serhiy Storchaka5e028ae2014-02-06 21:10:41 +0200120 negative or omitted, read all data until the end of the chunk. An empty
121 bytes object is returned when the end of the chunk is encountered
122 immediately.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 .. method:: skip()
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Benjamin Petersone41251e2008-04-25 01:59:09 +0000127 Skip to the end of the chunk. All further calls to :meth:`read` for the
Serhiy Storchaka5e028ae2014-02-06 21:10:41 +0200128 chunk will return ``b''``. If you are not interested in the contents of
Benjamin Petersone41251e2008-04-25 01:59:09 +0000129 the chunk, this method should be called so that the file points to the
130 start of the next chunk.
131
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133.. rubric:: Footnotes
134
135.. [#] "EA IFF 85" Standard for Interchange Format Files, Jerry Morrison, Electronic
136 Arts, January 1985.
137