/Doc/library/sunau.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 261 lines · 144 code · 117 blank · 0 comment · 0 complexity · ae3a3f6decf16382d0e7564722f69445 MD5 · raw file

  1. :mod:`sunau` --- Read and write Sun AU files
  2. ============================================
  3. .. module:: sunau
  4. :synopsis: Provide an interface to the Sun AU sound format.
  5. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  6. The :mod:`sunau` module provides a convenient interface to the Sun AU sound
  7. format. Note that this module is interface-compatible with the modules
  8. :mod:`aifc` and :mod:`wave`.
  9. An audio file consists of a header followed by the data. The fields of the
  10. header are:
  11. +---------------+-----------------------------------------------+
  12. | Field | Contents |
  13. +===============+===============================================+
  14. | magic word | The four bytes ``.snd``. |
  15. +---------------+-----------------------------------------------+
  16. | header size | Size of the header, including info, in bytes. |
  17. +---------------+-----------------------------------------------+
  18. | data size | Physical size of the data, in bytes. |
  19. +---------------+-----------------------------------------------+
  20. | encoding | Indicates how the audio samples are encoded. |
  21. +---------------+-----------------------------------------------+
  22. | sample rate | The sampling rate. |
  23. +---------------+-----------------------------------------------+
  24. | # of channels | The number of channels in the samples. |
  25. +---------------+-----------------------------------------------+
  26. | info | ASCII string giving a description of the |
  27. | | audio file (padded with null bytes). |
  28. +---------------+-----------------------------------------------+
  29. Apart from the info field, all header fields are 4 bytes in size. They are all
  30. 32-bit unsigned integers encoded in big-endian byte order.
  31. The :mod:`sunau` module defines the following functions:
  32. .. function:: open(file, mode)
  33. If *file* is a string, open the file by that name, otherwise treat it as a
  34. seekable file-like object. *mode* can be any of
  35. ``'r'``
  36. Read only mode.
  37. ``'w'``
  38. Write only mode.
  39. Note that it does not allow read/write files.
  40. A *mode* of ``'r'`` returns a :class:`AU_read` object, while a *mode* of ``'w'``
  41. or ``'wb'`` returns a :class:`AU_write` object.
  42. .. function:: openfp(file, mode)
  43. A synonym for :func:`open`, maintained for backwards compatibility.
  44. The :mod:`sunau` module defines the following exception:
  45. .. exception:: Error
  46. An error raised when something is impossible because of Sun AU specs or
  47. implementation deficiency.
  48. The :mod:`sunau` module defines the following data items:
  49. .. data:: AUDIO_FILE_MAGIC
  50. An integer every valid Sun AU file begins with, stored in big-endian form. This
  51. is the string ``.snd`` interpreted as an integer.
  52. .. data:: AUDIO_FILE_ENCODING_MULAW_8
  53. AUDIO_FILE_ENCODING_LINEAR_8
  54. AUDIO_FILE_ENCODING_LINEAR_16
  55. AUDIO_FILE_ENCODING_LINEAR_24
  56. AUDIO_FILE_ENCODING_LINEAR_32
  57. AUDIO_FILE_ENCODING_ALAW_8
  58. Values of the encoding field from the AU header which are supported by this
  59. module.
  60. .. data:: AUDIO_FILE_ENCODING_FLOAT
  61. AUDIO_FILE_ENCODING_DOUBLE
  62. AUDIO_FILE_ENCODING_ADPCM_G721
  63. AUDIO_FILE_ENCODING_ADPCM_G722
  64. AUDIO_FILE_ENCODING_ADPCM_G723_3
  65. AUDIO_FILE_ENCODING_ADPCM_G723_5
  66. Additional known values of the encoding field from the AU header, but which are
  67. not supported by this module.
  68. .. _au-read-objects:
  69. AU_read Objects
  70. ---------------
  71. AU_read objects, as returned by :func:`open` above, have the following methods:
  72. .. method:: AU_read.close()
  73. Close the stream, and make the instance unusable. (This is called automatically
  74. on deletion.)
  75. .. method:: AU_read.getnchannels()
  76. Returns number of audio channels (1 for mone, 2 for stereo).
  77. .. method:: AU_read.getsampwidth()
  78. Returns sample width in bytes.
  79. .. method:: AU_read.getframerate()
  80. Returns sampling frequency.
  81. .. method:: AU_read.getnframes()
  82. Returns number of audio frames.
  83. .. method:: AU_read.getcomptype()
  84. Returns compression type. Supported compression types are ``'ULAW'``, ``'ALAW'``
  85. and ``'NONE'``.
  86. .. method:: AU_read.getcompname()
  87. Human-readable version of :meth:`getcomptype`. The supported types have the
  88. respective names ``'CCITT G.711 u-law'``, ``'CCITT G.711 A-law'`` and ``'not
  89. compressed'``.
  90. .. method:: AU_read.getparams()
  91. Returns a tuple ``(nchannels, sampwidth, framerate, nframes, comptype,
  92. compname)``, equivalent to output of the :meth:`get\*` methods.
  93. .. method:: AU_read.readframes(n)
  94. Reads and returns at most *n* frames of audio, as a string of bytes. The data
  95. will be returned in linear format. If the original data is in u-LAW format, it
  96. will be converted.
  97. .. method:: AU_read.rewind()
  98. Rewind the file pointer to the beginning of the audio stream.
  99. The following two methods define a term "position" which is compatible between
  100. them, and is otherwise implementation dependent.
  101. .. method:: AU_read.setpos(pos)
  102. Set the file pointer to the specified position. Only values returned from
  103. :meth:`tell` should be used for *pos*.
  104. .. method:: AU_read.tell()
  105. Return current file pointer position. Note that the returned value has nothing
  106. to do with the actual position in the file.
  107. The following two functions are defined for compatibility with the :mod:`aifc`,
  108. and don't do anything interesting.
  109. .. method:: AU_read.getmarkers()
  110. Returns ``None``.
  111. .. method:: AU_read.getmark(id)
  112. Raise an error.
  113. .. _au-write-objects:
  114. AU_write Objects
  115. ----------------
  116. AU_write objects, as returned by :func:`open` above, have the following methods:
  117. .. method:: AU_write.setnchannels(n)
  118. Set the number of channels.
  119. .. method:: AU_write.setsampwidth(n)
  120. Set the sample width (in bytes.)
  121. .. method:: AU_write.setframerate(n)
  122. Set the frame rate.
  123. .. method:: AU_write.setnframes(n)
  124. Set the number of frames. This can be later changed, when and if more frames
  125. are written.
  126. .. method:: AU_write.setcomptype(type, name)
  127. Set the compression type and description. Only ``'NONE'`` and ``'ULAW'`` are
  128. supported on output.
  129. .. method:: AU_write.setparams(tuple)
  130. The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
  131. compname)``, with values valid for the :meth:`set\*` methods. Set all
  132. parameters.
  133. .. method:: AU_write.tell()
  134. Return current position in the file, with the same disclaimer for the
  135. :meth:`AU_read.tell` and :meth:`AU_read.setpos` methods.
  136. .. method:: AU_write.writeframesraw(data)
  137. Write audio frames, without correcting *nframes*.
  138. .. method:: AU_write.writeframes(data)
  139. Write audio frames and make sure *nframes* is correct.
  140. .. method:: AU_write.close()
  141. Make sure *nframes* is correct, and close the file.
  142. This method is called upon deletion.
  143. Note that it is invalid to set any parameters after calling :meth:`writeframes`
  144. or :meth:`writeframesraw`.