17db96d56Sopenharmony_ci:mod:`chunk` --- Read IFF chunked data
27db96d56Sopenharmony_ci======================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: chunk
57db96d56Sopenharmony_ci   :synopsis: Module to read IFF chunks.
67db96d56Sopenharmony_ci   :deprecated:
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci.. moduleauthor:: Sjoerd Mullender <sjoerd@acm.org>
97db96d56Sopenharmony_ci.. sectionauthor:: Sjoerd Mullender <sjoerd@acm.org>
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci**Source code:** :source:`Lib/chunk.py`
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ci.. index::
147db96d56Sopenharmony_ci   single: Audio Interchange File Format
157db96d56Sopenharmony_ci   single: AIFF
167db96d56Sopenharmony_ci   single: AIFF-C
177db96d56Sopenharmony_ci   single: Real Media File Format
187db96d56Sopenharmony_ci   single: RMFF
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci.. deprecated-removed:: 3.11 3.13
217db96d56Sopenharmony_ci   The :mod:`chunk` module is deprecated
227db96d56Sopenharmony_ci   (see :pep:`PEP 594 <594#chunk>` for details).
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ci--------------
257db96d56Sopenharmony_ci
267db96d56Sopenharmony_ciThis module provides an interface for reading files that use EA IFF 85 chunks.
277db96d56Sopenharmony_ci[#]_  This format is used in at least the Audio Interchange File Format
287db96d56Sopenharmony_ci(AIFF/AIFF-C) and the Real Media File Format (RMFF).  The WAVE audio file format
297db96d56Sopenharmony_ciis closely related and can also be read using this module.
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ciA chunk has the following structure:
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci+---------+--------+-------------------------------+
347db96d56Sopenharmony_ci| Offset  | Length | Contents                      |
357db96d56Sopenharmony_ci+=========+========+===============================+
367db96d56Sopenharmony_ci| 0       | 4      | Chunk ID                      |
377db96d56Sopenharmony_ci+---------+--------+-------------------------------+
387db96d56Sopenharmony_ci| 4       | 4      | Size of chunk in big-endian   |
397db96d56Sopenharmony_ci|         |        | byte order, not including the |
407db96d56Sopenharmony_ci|         |        | header                        |
417db96d56Sopenharmony_ci+---------+--------+-------------------------------+
427db96d56Sopenharmony_ci| 8       | *n*    | Data bytes, where *n* is the  |
437db96d56Sopenharmony_ci|         |        | size given in the preceding   |
447db96d56Sopenharmony_ci|         |        | field                         |
457db96d56Sopenharmony_ci+---------+--------+-------------------------------+
467db96d56Sopenharmony_ci| 8 + *n* | 0 or 1 | Pad byte needed if *n* is odd |
477db96d56Sopenharmony_ci|         |        | and chunk alignment is used   |
487db96d56Sopenharmony_ci+---------+--------+-------------------------------+
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ciThe ID is a 4-byte string which identifies the type of chunk.
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ciThe size field (a 32-bit value, encoded using big-endian byte order) gives the
537db96d56Sopenharmony_cisize of the chunk data, not including the 8-byte header.
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ciUsually an IFF-type file consists of one or more chunks.  The proposed usage of
567db96d56Sopenharmony_cithe :class:`Chunk` class defined here is to instantiate an instance at the start
577db96d56Sopenharmony_ciof each chunk and read from the instance until it reaches the end, after which a
587db96d56Sopenharmony_cinew instance can be instantiated. At the end of the file, creating a new
597db96d56Sopenharmony_ciinstance will fail with an :exc:`EOFError` exception.
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci.. class:: Chunk(file, align=True, bigendian=True, inclheader=False)
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci   Class which represents a chunk.  The *file* argument is expected to be a
657db96d56Sopenharmony_ci   file-like object.  An instance of this class is specifically allowed.  The
667db96d56Sopenharmony_ci   only method that is needed is :meth:`~io.IOBase.read`.  If the methods
677db96d56Sopenharmony_ci   :meth:`~io.IOBase.seek` and :meth:`~io.IOBase.tell` are present and don't
687db96d56Sopenharmony_ci   raise an exception, they are also used.
697db96d56Sopenharmony_ci   If these methods are present and raise an exception, they are expected to not
707db96d56Sopenharmony_ci   have altered the object.  If the optional argument *align* is true, chunks
717db96d56Sopenharmony_ci   are assumed to be aligned on 2-byte boundaries.  If *align* is false, no
727db96d56Sopenharmony_ci   alignment is assumed.  The default value is true.  If the optional argument
737db96d56Sopenharmony_ci   *bigendian* is false, the chunk size is assumed to be in little-endian order.
747db96d56Sopenharmony_ci   This is needed for WAVE audio files. The default value is true.  If the
757db96d56Sopenharmony_ci   optional argument *inclheader* is true, the size given in the chunk header
767db96d56Sopenharmony_ci   includes the size of the header.  The default value is false.
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   A :class:`Chunk` object supports the following methods:
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci
817db96d56Sopenharmony_ci   .. method:: getname()
827db96d56Sopenharmony_ci
837db96d56Sopenharmony_ci      Returns the name (ID) of the chunk.  This is the first 4 bytes of the
847db96d56Sopenharmony_ci      chunk.
857db96d56Sopenharmony_ci
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ci   .. method:: getsize()
887db96d56Sopenharmony_ci
897db96d56Sopenharmony_ci      Returns the size of the chunk.
907db96d56Sopenharmony_ci
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci   .. method:: close()
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci      Close and skip to the end of the chunk.  This does not close the
957db96d56Sopenharmony_ci      underlying file.
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci   The remaining methods will raise :exc:`OSError` if called after the
987db96d56Sopenharmony_ci   :meth:`close` method has been called.  Before Python 3.3, they used to
997db96d56Sopenharmony_ci   raise :exc:`IOError`, now an alias of :exc:`OSError`.
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ci
1027db96d56Sopenharmony_ci   .. method:: isatty()
1037db96d56Sopenharmony_ci
1047db96d56Sopenharmony_ci      Returns ``False``.
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ci   .. method:: seek(pos, whence=0)
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ci      Set the chunk's current position.  The *whence* argument is optional and
1107db96d56Sopenharmony_ci      defaults to ``0`` (absolute file positioning); other values are ``1``
1117db96d56Sopenharmony_ci      (seek relative to the current position) and ``2`` (seek relative to the
1127db96d56Sopenharmony_ci      file's end).  There is no return value. If the underlying file does not
1137db96d56Sopenharmony_ci      allow seek, only forward seeks are allowed.
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ci
1167db96d56Sopenharmony_ci   .. method:: tell()
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ci      Return the current position into the chunk.
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci   .. method:: read(size=-1)
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ci      Read at most *size* bytes from the chunk (less if the read hits the end of
1247db96d56Sopenharmony_ci      the chunk before obtaining *size* bytes).  If the *size* argument is
1257db96d56Sopenharmony_ci      negative or omitted, read all data until the end of the chunk.  An empty
1267db96d56Sopenharmony_ci      bytes object is returned when the end of the chunk is encountered
1277db96d56Sopenharmony_ci      immediately.
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ci
1307db96d56Sopenharmony_ci   .. method:: skip()
1317db96d56Sopenharmony_ci
1327db96d56Sopenharmony_ci      Skip to the end of the chunk.  All further calls to :meth:`read` for the
1337db96d56Sopenharmony_ci      chunk will return ``b''``.  If you are not interested in the contents of
1347db96d56Sopenharmony_ci      the chunk, this method should be called so that the file points to the
1357db96d56Sopenharmony_ci      start of the next chunk.
1367db96d56Sopenharmony_ci
1377db96d56Sopenharmony_ci
1387db96d56Sopenharmony_ci.. rubric:: Footnotes
1397db96d56Sopenharmony_ci
1407db96d56Sopenharmony_ci.. [#] "EA IFF 85" Standard for Interchange Format Files, Jerry Morrison, Electronic
1417db96d56Sopenharmony_ci   Arts, January 1985.
1427db96d56Sopenharmony_ci
143