gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

588 lines
38KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="wave — Read and write WAV files" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/wave.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/wave.py The wave module provides a convenient interface to the Waveform Audio “WAVE” (or “WAV”) file format. Only uncompressed PCM encoded wave files are supported. The wave module..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/wave.py The wave module provides a convenient interface to the Waveform Audio “WAVE” (or “WAV”) file format. Only uncompressed PCM encoded wave files are supported. The wave module..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>wave — Read and write WAV files &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="colorsys — Conversions between color systems" href="colorsys.html" />
  33. <link rel="prev" title="Multimedia Services" href="mm.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/wave.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code> — Read and write WAV files</a><ul>
  86. <li><a class="reference internal" href="#wave-read-objects">Wave_read Objects</a></li>
  87. <li><a class="reference internal" href="#wave-write-objects">Wave_write Objects</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="mm.html"
  95. title="previous chapter">Multimedia Services</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="colorsys.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">colorsys</span></code> — Conversions between color systems</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/wave.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="colorsys.html" title="colorsys — Conversions between color systems"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="mm.html" title="Multimedia Services"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="mm.html" accesskey="U">Multimedia Services</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code> — Read and write WAV files</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-wave">
  170. <span id="wave-read-and-write-wav-files"></span><h1><a class="reference internal" href="#module-wave" title="wave: Provide an interface to the WAV sound format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code></a> — Read and write WAV files<a class="headerlink" href="#module-wave" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/wave.py">Lib/wave.py</a></p>
  172. <hr class="docutils" />
  173. <p>The <a class="reference internal" href="#module-wave" title="wave: Provide an interface to the WAV sound format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code></a> module provides a convenient interface to the Waveform Audio
  174. “WAVE” (or “WAV”) file format. Only uncompressed PCM encoded wave files are
  175. supported.</p>
  176. <div class="versionchanged">
  177. <p><span class="versionmodified changed">Changed in version 3.12: </span>Support for <code class="docutils literal notranslate"><span class="pre">WAVE_FORMAT_EXTENSIBLE</span></code> headers was added, provided that the
  178. extended format is <code class="docutils literal notranslate"><span class="pre">KSDATAFORMAT_SUBTYPE_PCM</span></code>.</p>
  179. </div>
  180. <p>The <a class="reference internal" href="#module-wave" title="wave: Provide an interface to the WAV sound format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code></a> module defines the following function and exception:</p>
  181. <dl class="py function">
  182. <dt class="sig sig-object py" id="wave.open">
  183. <span class="sig-prename descclassname"><span class="pre">wave.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.open" title="Link to this definition">¶</a></dt>
  184. <dd><p>If <em>file</em> is a string, open the file by that name, otherwise treat it as a
  185. file-like object. <em>mode</em> can be:</p>
  186. <dl class="simple">
  187. <dt><code class="docutils literal notranslate"><span class="pre">'rb'</span></code></dt><dd><p>Read only mode.</p>
  188. </dd>
  189. <dt><code class="docutils literal notranslate"><span class="pre">'wb'</span></code></dt><dd><p>Write only mode.</p>
  190. </dd>
  191. </dl>
  192. <p>Note that it does not allow read/write WAV files.</p>
  193. <p>A <em>mode</em> of <code class="docutils literal notranslate"><span class="pre">'rb'</span></code> returns a <a class="reference internal" href="#wave.Wave_read" title="wave.Wave_read"><code class="xref py py-class docutils literal notranslate"><span class="pre">Wave_read</span></code></a> object, while a <em>mode</em> of
  194. <code class="docutils literal notranslate"><span class="pre">'wb'</span></code> returns a <a class="reference internal" href="#wave.Wave_write" title="wave.Wave_write"><code class="xref py py-class docutils literal notranslate"><span class="pre">Wave_write</span></code></a> object. If <em>mode</em> is omitted and a
  195. file-like object is passed as <em>file</em>, <code class="docutils literal notranslate"><span class="pre">file.mode</span></code> is used as the default
  196. value for <em>mode</em>.</p>
  197. <p>If you pass in a file-like object, the wave object will not close it when its
  198. <code class="docutils literal notranslate"><span class="pre">close()</span></code> method is called; it is the caller’s responsibility to close
  199. the file object.</p>
  200. <p>The <a class="reference internal" href="#wave.open" title="wave.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function may be used in a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement. When
  201. the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> block completes, the <a class="reference internal" href="#wave.Wave_read.close" title="wave.Wave_read.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Wave_read.close()</span></code></a> or
  202. <a class="reference internal" href="#wave.Wave_write.close" title="wave.Wave_write.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Wave_write.close()</span></code></a> method is called.</p>
  203. <div class="versionchanged">
  204. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for unseekable files.</p>
  205. </div>
  206. </dd></dl>
  207. <dl class="py exception">
  208. <dt class="sig sig-object py" id="wave.Error">
  209. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">wave.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#wave.Error" title="Link to this definition">¶</a></dt>
  210. <dd><p>An error raised when something is impossible because it violates the WAV
  211. specification or hits an implementation deficiency.</p>
  212. </dd></dl>
  213. <section id="wave-read-objects">
  214. <span id="id1"></span><h2>Wave_read Objects<a class="headerlink" href="#wave-read-objects" title="Link to this heading">¶</a></h2>
  215. <dl class="py class">
  216. <dt class="sig sig-object py" id="wave.Wave_read">
  217. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">wave.</span></span><span class="sig-name descname"><span class="pre">Wave_read</span></span><a class="headerlink" href="#wave.Wave_read" title="Link to this definition">¶</a></dt>
  218. <dd><p>Read a WAV file.</p>
  219. <p>Wave_read objects, as returned by <a class="reference internal" href="#wave.open" title="wave.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, have the following methods:</p>
  220. <dl class="py method">
  221. <dt class="sig sig-object py" id="wave.Wave_read.close">
  222. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.close" title="Link to this definition">¶</a></dt>
  223. <dd><p>Close the stream if it was opened by <a class="reference internal" href="#module-wave" title="wave: Provide an interface to the WAV sound format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code></a>, and make the instance
  224. unusable. This is called automatically on object collection.</p>
  225. </dd></dl>
  226. <dl class="py method">
  227. <dt class="sig sig-object py" id="wave.Wave_read.getnchannels">
  228. <span class="sig-name descname"><span class="pre">getnchannels</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getnchannels" title="Link to this definition">¶</a></dt>
  229. <dd><p>Returns number of audio channels (<code class="docutils literal notranslate"><span class="pre">1</span></code> for mono, <code class="docutils literal notranslate"><span class="pre">2</span></code> for stereo).</p>
  230. </dd></dl>
  231. <dl class="py method">
  232. <dt class="sig sig-object py" id="wave.Wave_read.getsampwidth">
  233. <span class="sig-name descname"><span class="pre">getsampwidth</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getsampwidth" title="Link to this definition">¶</a></dt>
  234. <dd><p>Returns sample width in bytes.</p>
  235. </dd></dl>
  236. <dl class="py method">
  237. <dt class="sig sig-object py" id="wave.Wave_read.getframerate">
  238. <span class="sig-name descname"><span class="pre">getframerate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getframerate" title="Link to this definition">¶</a></dt>
  239. <dd><p>Returns sampling frequency.</p>
  240. </dd></dl>
  241. <dl class="py method">
  242. <dt class="sig sig-object py" id="wave.Wave_read.getnframes">
  243. <span class="sig-name descname"><span class="pre">getnframes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getnframes" title="Link to this definition">¶</a></dt>
  244. <dd><p>Returns number of audio frames.</p>
  245. </dd></dl>
  246. <dl class="py method">
  247. <dt class="sig sig-object py" id="wave.Wave_read.getcomptype">
  248. <span class="sig-name descname"><span class="pre">getcomptype</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getcomptype" title="Link to this definition">¶</a></dt>
  249. <dd><p>Returns compression type (<code class="docutils literal notranslate"><span class="pre">'NONE'</span></code> is the only supported type).</p>
  250. </dd></dl>
  251. <dl class="py method">
  252. <dt class="sig sig-object py" id="wave.Wave_read.getcompname">
  253. <span class="sig-name descname"><span class="pre">getcompname</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getcompname" title="Link to this definition">¶</a></dt>
  254. <dd><p>Human-readable version of <a class="reference internal" href="#wave.Wave_read.getcomptype" title="wave.Wave_read.getcomptype"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getcomptype()</span></code></a>. Usually <code class="docutils literal notranslate"><span class="pre">'not</span> <span class="pre">compressed'</span></code>
  255. parallels <code class="docutils literal notranslate"><span class="pre">'NONE'</span></code>.</p>
  256. </dd></dl>
  257. <dl class="py method">
  258. <dt class="sig sig-object py" id="wave.Wave_read.getparams">
  259. <span class="sig-name descname"><span class="pre">getparams</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getparams" title="Link to this definition">¶</a></dt>
  260. <dd><p>Returns a <a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a> <code class="docutils literal notranslate"><span class="pre">(nchannels,</span> <span class="pre">sampwidth,</span>
  261. <span class="pre">framerate,</span> <span class="pre">nframes,</span> <span class="pre">comptype,</span> <span class="pre">compname)</span></code>, equivalent to output of the
  262. <code class="docutils literal notranslate"><span class="pre">get*()</span></code> methods.</p>
  263. </dd></dl>
  264. <dl class="py method">
  265. <dt class="sig sig-object py" id="wave.Wave_read.readframes">
  266. <span class="sig-name descname"><span class="pre">readframes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.readframes" title="Link to this definition">¶</a></dt>
  267. <dd><p>Reads and returns at most <em>n</em> frames of audio, as a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.</p>
  268. </dd></dl>
  269. <dl class="py method">
  270. <dt class="sig sig-object py" id="wave.Wave_read.rewind">
  271. <span class="sig-name descname"><span class="pre">rewind</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.rewind" title="Link to this definition">¶</a></dt>
  272. <dd><p>Rewind the file pointer to the beginning of the audio stream.</p>
  273. </dd></dl>
  274. <p>The following two methods are defined for compatibility with the <a class="reference internal" href="aifc.html#module-aifc" title="aifc: Read and write audio files in AIFF or AIFC format. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">aifc</span></code></a>
  275. module, and don’t do anything interesting.</p>
  276. <dl class="py method">
  277. <dt class="sig sig-object py" id="wave.Wave_read.getmarkers">
  278. <span class="sig-name descname"><span class="pre">getmarkers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getmarkers" title="Link to this definition">¶</a></dt>
  279. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  280. </dd></dl>
  281. <dl class="py method">
  282. <dt class="sig sig-object py" id="wave.Wave_read.getmark">
  283. <span class="sig-name descname"><span class="pre">getmark</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">id</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.getmark" title="Link to this definition">¶</a></dt>
  284. <dd><p>Raise an error.</p>
  285. </dd></dl>
  286. <p>The following two methods define a term “position” which is compatible between
  287. them, and is otherwise implementation dependent.</p>
  288. <dl class="py method">
  289. <dt class="sig sig-object py" id="wave.Wave_read.setpos">
  290. <span class="sig-name descname"><span class="pre">setpos</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pos</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.setpos" title="Link to this definition">¶</a></dt>
  291. <dd><p>Set the file pointer to the specified position.</p>
  292. </dd></dl>
  293. <dl class="py method">
  294. <dt class="sig sig-object py" id="wave.Wave_read.tell">
  295. <span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_read.tell" title="Link to this definition">¶</a></dt>
  296. <dd><p>Return current file pointer position.</p>
  297. </dd></dl>
  298. </dd></dl>
  299. </section>
  300. <section id="wave-write-objects">
  301. <span id="id2"></span><h2>Wave_write Objects<a class="headerlink" href="#wave-write-objects" title="Link to this heading">¶</a></h2>
  302. <dl class="py class">
  303. <dt class="sig sig-object py" id="wave.Wave_write">
  304. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">wave.</span></span><span class="sig-name descname"><span class="pre">Wave_write</span></span><a class="headerlink" href="#wave.Wave_write" title="Link to this definition">¶</a></dt>
  305. <dd><p>Write a WAV file.</p>
  306. <p>Wave_write objects, as returned by <a class="reference internal" href="#wave.open" title="wave.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>.</p>
  307. <p>For seekable output streams, the <code class="docutils literal notranslate"><span class="pre">wave</span></code> header will automatically be updated
  308. to reflect the number of frames actually written. For unseekable streams, the
  309. <em>nframes</em> value must be accurate when the first frame data is written. An
  310. accurate <em>nframes</em> value can be achieved either by calling
  311. <a class="reference internal" href="#wave.Wave_write.setnframes" title="wave.Wave_write.setnframes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setnframes()</span></code></a> or <a class="reference internal" href="#wave.Wave_write.setparams" title="wave.Wave_write.setparams"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setparams()</span></code></a> with the number
  312. of frames that will be written before <a class="reference internal" href="#wave.Wave_write.close" title="wave.Wave_write.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> is called and
  313. then using <a class="reference internal" href="#wave.Wave_write.writeframesraw" title="wave.Wave_write.writeframesraw"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writeframesraw()</span></code></a> to write the frame data, or by
  314. calling <a class="reference internal" href="#wave.Wave_write.writeframes" title="wave.Wave_write.writeframes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writeframes()</span></code></a> with all of the frame data to be
  315. written. In the latter case <a class="reference internal" href="#wave.Wave_write.writeframes" title="wave.Wave_write.writeframes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writeframes()</span></code></a> will calculate
  316. the number of frames in the data and set <em>nframes</em> accordingly before writing
  317. the frame data.</p>
  318. <div class="versionchanged">
  319. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for unseekable files.</p>
  320. </div>
  321. <p>Wave_write objects have the following methods:</p>
  322. <dl class="py method">
  323. <dt class="sig sig-object py" id="wave.Wave_write.close">
  324. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.close" title="Link to this definition">¶</a></dt>
  325. <dd><p>Make sure <em>nframes</em> is correct, and close the file if it was opened by
  326. <a class="reference internal" href="#module-wave" title="wave: Provide an interface to the WAV sound format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code></a>. This method is called upon object collection. It will raise
  327. an exception if the output stream is not seekable and <em>nframes</em> does not
  328. match the number of frames actually written.</p>
  329. </dd></dl>
  330. <dl class="py method">
  331. <dt class="sig sig-object py" id="wave.Wave_write.setnchannels">
  332. <span class="sig-name descname"><span class="pre">setnchannels</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.setnchannels" title="Link to this definition">¶</a></dt>
  333. <dd><p>Set the number of channels.</p>
  334. </dd></dl>
  335. <dl class="py method">
  336. <dt class="sig sig-object py" id="wave.Wave_write.setsampwidth">
  337. <span class="sig-name descname"><span class="pre">setsampwidth</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.setsampwidth" title="Link to this definition">¶</a></dt>
  338. <dd><p>Set the sample width to <em>n</em> bytes.</p>
  339. </dd></dl>
  340. <dl class="py method">
  341. <dt class="sig sig-object py" id="wave.Wave_write.setframerate">
  342. <span class="sig-name descname"><span class="pre">setframerate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.setframerate" title="Link to this definition">¶</a></dt>
  343. <dd><p>Set the frame rate to <em>n</em>.</p>
  344. <div class="versionchanged">
  345. <p><span class="versionmodified changed">Changed in version 3.2: </span>A non-integral input to this method is rounded to the nearest
  346. integer.</p>
  347. </div>
  348. </dd></dl>
  349. <dl class="py method">
  350. <dt class="sig sig-object py" id="wave.Wave_write.setnframes">
  351. <span class="sig-name descname"><span class="pre">setnframes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.setnframes" title="Link to this definition">¶</a></dt>
  352. <dd><p>Set the number of frames to <em>n</em>. This will be changed later if the number
  353. of frames actually written is different (this update attempt will
  354. raise an error if the output stream is not seekable).</p>
  355. </dd></dl>
  356. <dl class="py method">
  357. <dt class="sig sig-object py" id="wave.Wave_write.setcomptype">
  358. <span class="sig-name descname"><span class="pre">setcomptype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.setcomptype" title="Link to this definition">¶</a></dt>
  359. <dd><p>Set the compression type and description. At the moment, only compression type
  360. <code class="docutils literal notranslate"><span class="pre">NONE</span></code> is supported, meaning no compression.</p>
  361. </dd></dl>
  362. <dl class="py method">
  363. <dt class="sig sig-object py" id="wave.Wave_write.setparams">
  364. <span class="sig-name descname"><span class="pre">setparams</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tuple</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.setparams" title="Link to this definition">¶</a></dt>
  365. <dd><p>The <em>tuple</em> should be <code class="docutils literal notranslate"><span class="pre">(nchannels,</span> <span class="pre">sampwidth,</span> <span class="pre">framerate,</span> <span class="pre">nframes,</span> <span class="pre">comptype,</span>
  366. <span class="pre">compname)</span></code>, with values valid for the <code class="docutils literal notranslate"><span class="pre">set*()</span></code> methods. Sets all
  367. parameters.</p>
  368. </dd></dl>
  369. <dl class="py method">
  370. <dt class="sig sig-object py" id="wave.Wave_write.tell">
  371. <span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.tell" title="Link to this definition">¶</a></dt>
  372. <dd><p>Return current position in the file, with the same disclaimer for the
  373. <a class="reference internal" href="#wave.Wave_read.tell" title="wave.Wave_read.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Wave_read.tell()</span></code></a> and <a class="reference internal" href="#wave.Wave_read.setpos" title="wave.Wave_read.setpos"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Wave_read.setpos()</span></code></a> methods.</p>
  374. </dd></dl>
  375. <dl class="py method">
  376. <dt class="sig sig-object py" id="wave.Wave_write.writeframesraw">
  377. <span class="sig-name descname"><span class="pre">writeframesraw</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.writeframesraw" title="Link to this definition">¶</a></dt>
  378. <dd><p>Write audio frames, without correcting <em>nframes</em>.</p>
  379. <div class="versionchanged">
  380. <p><span class="versionmodified changed">Changed in version 3.4: </span>Any <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> is now accepted.</p>
  381. </div>
  382. </dd></dl>
  383. <dl class="py method">
  384. <dt class="sig sig-object py" id="wave.Wave_write.writeframes">
  385. <span class="sig-name descname"><span class="pre">writeframes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#wave.Wave_write.writeframes" title="Link to this definition">¶</a></dt>
  386. <dd><p>Write audio frames and make sure <em>nframes</em> is correct. It will raise an
  387. error if the output stream is not seekable and the total number of frames
  388. that have been written after <em>data</em> has been written does not match the
  389. previously set value for <em>nframes</em>.</p>
  390. <div class="versionchanged">
  391. <p><span class="versionmodified changed">Changed in version 3.4: </span>Any <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> is now accepted.</p>
  392. </div>
  393. <p>Note that it is invalid to set any parameters after calling <a class="reference internal" href="#wave.Wave_write.writeframes" title="wave.Wave_write.writeframes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writeframes()</span></code></a>
  394. or <a class="reference internal" href="#wave.Wave_write.writeframesraw" title="wave.Wave_write.writeframesraw"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writeframesraw()</span></code></a>, and any attempt to do so will raise
  395. <a class="reference internal" href="#wave.Error" title="wave.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">wave.Error</span></code></a>.</p>
  396. </dd></dl>
  397. </dd></dl>
  398. </section>
  399. </section>
  400. <div class="clearer"></div>
  401. </div>
  402. </div>
  403. </div>
  404. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  405. <div class="sphinxsidebarwrapper">
  406. <div>
  407. <h3><a href="../contents.html">Table of Contents</a></h3>
  408. <ul>
  409. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code> — Read and write WAV files</a><ul>
  410. <li><a class="reference internal" href="#wave-read-objects">Wave_read Objects</a></li>
  411. <li><a class="reference internal" href="#wave-write-objects">Wave_write Objects</a></li>
  412. </ul>
  413. </li>
  414. </ul>
  415. </div>
  416. <div>
  417. <h4>Previous topic</h4>
  418. <p class="topless"><a href="mm.html"
  419. title="previous chapter">Multimedia Services</a></p>
  420. </div>
  421. <div>
  422. <h4>Next topic</h4>
  423. <p class="topless"><a href="colorsys.html"
  424. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">colorsys</span></code> — Conversions between color systems</a></p>
  425. </div>
  426. <div role="note" aria-label="source link">
  427. <h3>This Page</h3>
  428. <ul class="this-page-menu">
  429. <li><a href="../bugs.html">Report a Bug</a></li>
  430. <li>
  431. <a href="https://github.com/python/cpython/blob/main/Doc/library/wave.rst"
  432. rel="nofollow">Show Source
  433. </a>
  434. </li>
  435. </ul>
  436. </div>
  437. </div>
  438. <div id="sidebarbutton" title="Collapse sidebar">
  439. <span>«</span>
  440. </div>
  441. </div>
  442. <div class="clearer"></div>
  443. </div>
  444. <div class="related" role="navigation" aria-label="related navigation">
  445. <h3>Navigation</h3>
  446. <ul>
  447. <li class="right" style="margin-right: 10px">
  448. <a href="../genindex.html" title="General Index"
  449. >index</a></li>
  450. <li class="right" >
  451. <a href="../py-modindex.html" title="Python Module Index"
  452. >modules</a> |</li>
  453. <li class="right" >
  454. <a href="colorsys.html" title="colorsys — Conversions between color systems"
  455. >next</a> |</li>
  456. <li class="right" >
  457. <a href="mm.html" title="Multimedia Services"
  458. >previous</a> |</li>
  459. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  460. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  461. <li class="switchers">
  462. <div class="language_switcher_placeholder"></div>
  463. <div class="version_switcher_placeholder"></div>
  464. </li>
  465. <li>
  466. </li>
  467. <li id="cpython-language-and-version">
  468. <a href="../index.html">3.12.3 Documentation</a> &#187;
  469. </li>
  470. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  471. <li class="nav-item nav-item-2"><a href="mm.html" >Multimedia Services</a> &#187;</li>
  472. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code> — Read and write WAV files</a></li>
  473. <li class="right">
  474. <div class="inline-search" role="search">
  475. <form class="inline-search" action="../search.html" method="get">
  476. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  477. <input type="submit" value="Go" />
  478. </form>
  479. </div>
  480. |
  481. </li>
  482. <li class="right">
  483. <label class="theme-selector-label">
  484. Theme
  485. <select class="theme-selector" oninput="activateTheme(this.value)">
  486. <option value="auto" selected>Auto</option>
  487. <option value="light">Light</option>
  488. <option value="dark">Dark</option>
  489. </select>
  490. </label> |</li>
  491. </ul>
  492. </div>
  493. <div class="footer">
  494. &copy;
  495. <a href="../copyright.html">
  496. Copyright
  497. </a>
  498. 2001-2024, Python Software Foundation.
  499. <br />
  500. This page is licensed under the Python Software Foundation License Version 2.
  501. <br />
  502. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  503. <br />
  504. See <a href="/license.html">History and License</a> for more information.<br />
  505. <br />
  506. The Python Software Foundation is a non-profit corporation.
  507. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  508. <br />
  509. <br />
  510. Last updated on Apr 09, 2024 (13:47 UTC).
  511. <a href="/bugs.html">Found a bug</a>?
  512. <br />
  513. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  514. </div>
  515. </body>
  516. </html>
上海开阖软件有限公司 沪ICP备12045867号-1