| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`chunk` --- Read IFF chunked data | 
 | 2 | ====================================== | 
 | 3 |  | 
 | 4 | .. module:: chunk | 
 | 5 |    :synopsis: Module to read IFF chunks. | 
 | 6 | .. moduleauthor:: Sjoerd Mullender <sjoerd@acm.org> | 
 | 7 | .. sectionauthor:: Sjoerd Mullender <sjoerd@acm.org> | 
 | 8 |  | 
 | 9 |  | 
 | 10 | .. index:: | 
 | 11 |    single: Audio Interchange File Format | 
 | 12 |    single: AIFF | 
 | 13 |    single: AIFF-C | 
 | 14 |    single: Real Media File Format | 
 | 15 |    single: RMFF | 
 | 16 |  | 
 | 17 | This module provides an interface for reading files that use EA IFF 85 chunks. | 
 | 18 | [#]_  This format is used in at least the Audio Interchange File Format | 
 | 19 | (AIFF/AIFF-C) and the Real Media File Format (RMFF).  The WAVE audio file format | 
 | 20 | is closely related and can also be read using this module. | 
 | 21 |  | 
 | 22 | A chunk has the following structure: | 
 | 23 |  | 
 | 24 | +---------+--------+-------------------------------+ | 
 | 25 | | Offset  | Length | Contents                      | | 
 | 26 | +=========+========+===============================+ | 
 | 27 | | 0       | 4      | Chunk ID                      | | 
 | 28 | +---------+--------+-------------------------------+ | 
 | 29 | | 4       | 4      | Size of chunk in big-endian   | | 
 | 30 | |         |        | byte order, not including the | | 
 | 31 | |         |        | header                        | | 
 | 32 | +---------+--------+-------------------------------+ | 
 | 33 | | 8       | *n*    | Data bytes, where *n* is the  | | 
 | 34 | |         |        | size given in the preceding   | | 
 | 35 | |         |        | field                         | | 
 | 36 | +---------+--------+-------------------------------+ | 
 | 37 | | 8 + *n* | 0 or 1 | Pad byte needed if *n* is odd | | 
 | 38 | |         |        | and chunk alignment is used   | | 
 | 39 | +---------+--------+-------------------------------+ | 
 | 40 |  | 
 | 41 | The ID is a 4-byte string which identifies the type of chunk. | 
 | 42 |  | 
 | 43 | The size field (a 32-bit value, encoded using big-endian byte order) gives the | 
 | 44 | size of the chunk data, not including the 8-byte header. | 
 | 45 |  | 
 | 46 | Usually an IFF-type file consists of one or more chunks.  The proposed usage of | 
 | 47 | the :class:`Chunk` class defined here is to instantiate an instance at the start | 
 | 48 | of each chunk and read from the instance until it reaches the end, after which a | 
 | 49 | new instance can be instantiated. At the end of the file, creating a new | 
 | 50 | instance will fail with a :exc:`EOFError` exception. | 
 | 51 |  | 
 | 52 |  | 
| Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 53 | .. class:: Chunk(file, align=True, bigendian=True, inclheader=False) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 |  | 
 | 55 |    Class which represents a chunk.  The *file* argument is expected to be a | 
 | 56 |    file-like object.  An instance of this class is specifically allowed.  The | 
| Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 57 |    only method that is needed is :meth:`~io.IOBase.read`.  If the methods | 
 | 58 |    :meth:`~io.IOBase.seek` and :meth:`~io.IOBase.tell` are present and don't | 
 | 59 |    raise an exception, they are also used. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 69 |    A :class:`Chunk` object supports the following methods: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 |  | 
 | 71 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 72 |    .. method:: getname() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 74 |       Returns the name (ID) of the chunk.  This is the first 4 bytes of the | 
 | 75 |       chunk. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 |  | 
 | 77 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 78 |    .. method:: getsize() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 80 |       Returns the size of the chunk. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 |  | 
 | 82 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 83 |    .. method:: close() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 85 |       Close and skip to the end of the chunk.  This does not close the | 
 | 86 |       underlying file. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 |  | 
| Antoine Pitrou | 4272d6a | 2011-10-12 19:10:10 +0200 | [diff] [blame] | 88 |    The remaining methods will raise :exc:`OSError` if called after the | 
 | 89 |    :meth:`close` method has been called.  Before Python 3.3, they used to | 
 | 90 |    raise :exc:`IOError`, now an alias of :exc:`OSError`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 |  | 
 | 92 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 93 |    .. method:: isatty() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 95 |       Returns ``False``. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 |  | 
 | 97 |  | 
| Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 98 |    .. method:: seek(pos, whence=0) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 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. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 |  | 
 | 106 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 107 |    .. method:: tell() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 109 |       Return the current position into the chunk. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 |  | 
 | 111 |  | 
| Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 112 |    .. method:: read(size=-1) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 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 | 
| Serhiy Storchaka | 5e028ae | 2014-02-06 21:10:41 +0200 | [diff] [blame] | 116 |       negative or omitted, read all data until the end of the chunk.  An empty | 
 | 117 |       bytes object is returned when the end of the chunk is encountered | 
 | 118 |       immediately. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 |  | 
 | 120 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 121 |    .. method:: skip() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 123 |       Skip to the end of the chunk.  All further calls to :meth:`read` for the | 
| Serhiy Storchaka | 5e028ae | 2014-02-06 21:10:41 +0200 | [diff] [blame] | 124 |       chunk will return ``b''``.  If you are not interested in the contents of | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 125 |       the chunk, this method should be called so that the file points to the | 
 | 126 |       start of the next chunk. | 
 | 127 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 |  | 
 | 129 | .. rubric:: Footnotes | 
 | 130 |  | 
 | 131 | .. [#] "EA IFF 85" Standard for Interchange Format Files, Jerry Morrison, Electronic | 
 | 132 |    Arts, January 1985. | 
 | 133 |  |