/usr/share/doc/python-pyaudio-doc/docs/examples.html is in python-pyaudio-doc 0.2.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example: Blocking Mode Audio I/O — PyAudio 0.2.11 documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.2.11',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="top" title="PyAudio 0.2.11 documentation" href="index.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">PyAudio 0.2.11 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<div class="section" id="example-blocking-mode-audio-i-o">
<h1>Example: Blocking Mode Audio I/O<a class="headerlink" href="#example-blocking-mode-audio-i-o" title="Permalink to this headline">¶</a></h1>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="sd">"""PyAudio Example: Play a wave file."""</span>
<span class="kn">import</span> <span class="nn">pyaudio</span>
<span class="kn">import</span> <span class="nn">wave</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">CHUNK</span> <span class="o">=</span> <span class="mi">1024</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o"><</span> <span class="mi">2</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Plays a wave file.</span><span class="se">\n\n</span><span class="s2">Usage: </span><span class="si">%s</span><span class="s2"> filename.wav"</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="n">wf</span> <span class="o">=</span> <span class="n">wave</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s1">'rb'</span><span class="p">)</span>
<span class="c1"># instantiate PyAudio (1)</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">PyAudio</span><span class="p">()</span>
<span class="c1"># open stream (2)</span>
<span class="n">stream</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="n">p</span><span class="o">.</span><span class="n">get_format_from_width</span><span class="p">(</span><span class="n">wf</span><span class="o">.</span><span class="n">getsampwidth</span><span class="p">()),</span>
<span class="n">channels</span><span class="o">=</span><span class="n">wf</span><span class="o">.</span><span class="n">getnchannels</span><span class="p">(),</span>
<span class="n">rate</span><span class="o">=</span><span class="n">wf</span><span class="o">.</span><span class="n">getframerate</span><span class="p">(),</span>
<span class="n">output</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># read data</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">wf</span><span class="o">.</span><span class="n">readframes</span><span class="p">(</span><span class="n">CHUNK</span><span class="p">)</span>
<span class="c1"># play stream (3)</span>
<span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="n">stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">wf</span><span class="o">.</span><span class="n">readframes</span><span class="p">(</span><span class="n">CHUNK</span><span class="p">)</span>
<span class="c1"># stop stream (4)</span>
<span class="n">stream</span><span class="o">.</span><span class="n">stop_stream</span><span class="p">()</span>
<span class="n">stream</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="c1"># close PyAudio (5)</span>
<span class="n">p</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
</pre></div>
</div>
<p>To use PyAudio, first instantiate PyAudio using
<a class="reference internal" href="index.html#pyaudio.PyAudio" title="pyaudio.PyAudio"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.PyAudio()</span></code></a> (1), which sets up the portaudio system.</p>
<p>To record or play audio, open a stream on the desired device with the
desired audio parameters using <a class="reference internal" href="index.html#pyaudio.PyAudio.open" title="pyaudio.PyAudio.open"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.PyAudio.open()</span></code></a>
(2). This sets up a <a class="reference internal" href="index.html#pyaudio.Stream" title="pyaudio.Stream"><code class="xref py py-class docutils literal"><span class="pre">pyaudio.Stream</span></code></a> to play or record
audio.</p>
<p>Play audio by writing audio data to the stream using
<a class="reference internal" href="index.html#pyaudio.Stream.write" title="pyaudio.Stream.write"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.Stream.write()</span></code></a>, or read audio data from the stream
using <a class="reference internal" href="index.html#pyaudio.Stream.read" title="pyaudio.Stream.read"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.Stream.read()</span></code></a>. (3)</p>
<p>Note that in “blocking mode”, each <a class="reference internal" href="index.html#pyaudio.Stream.write" title="pyaudio.Stream.write"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.Stream.write()</span></code></a> or
<a class="reference internal" href="index.html#pyaudio.Stream.read" title="pyaudio.Stream.read"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.Stream.read()</span></code></a> blocks until all the given/requested
frames have been played/recorded. Alternatively, to generate audio
data on the fly or immediately process recorded audio data, use the
“callback mode” outlined below.</p>
<p>Use <a class="reference internal" href="index.html#pyaudio.Stream.stop_stream" title="pyaudio.Stream.stop_stream"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.Stream.stop_stream()</span></code></a> to pause playing/recording,
and <a class="reference internal" href="index.html#pyaudio.Stream.close" title="pyaudio.Stream.close"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.Stream.close()</span></code></a> to terminate the stream. (4)</p>
<p>Finally, terminate the portaudio session using
<a class="reference internal" href="index.html#pyaudio.PyAudio.terminate" title="pyaudio.PyAudio.terminate"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.PyAudio.terminate()</span></code></a> (5)</p>
</div>
<div class="section" id="example-callback-mode-audio-i-o">
<h1>Example: Callback Mode Audio I/O<a class="headerlink" href="#example-callback-mode-audio-i-o" title="Permalink to this headline">¶</a></h1>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="sd">"""PyAudio Example: Play a wave file (callback version)."""</span>
<span class="kn">import</span> <span class="nn">pyaudio</span>
<span class="kn">import</span> <span class="nn">wave</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o"><</span> <span class="mi">2</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Plays a wave file.</span><span class="se">\n\n</span><span class="s2">Usage: </span><span class="si">%s</span><span class="s2"> filename.wav"</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="n">wf</span> <span class="o">=</span> <span class="n">wave</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s1">'rb'</span><span class="p">)</span>
<span class="c1"># instantiate PyAudio (1)</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">PyAudio</span><span class="p">()</span>
<span class="c1"># define callback (2)</span>
<span class="k">def</span> <span class="nf">callback</span><span class="p">(</span><span class="n">in_data</span><span class="p">,</span> <span class="n">frame_count</span><span class="p">,</span> <span class="n">time_info</span><span class="p">,</span> <span class="n">status</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">wf</span><span class="o">.</span><span class="n">readframes</span><span class="p">(</span><span class="n">frame_count</span><span class="p">)</span>
<span class="k">return</span> <span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">paContinue</span><span class="p">)</span>
<span class="c1"># open stream using callback (3)</span>
<span class="n">stream</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="n">p</span><span class="o">.</span><span class="n">get_format_from_width</span><span class="p">(</span><span class="n">wf</span><span class="o">.</span><span class="n">getsampwidth</span><span class="p">()),</span>
<span class="n">channels</span><span class="o">=</span><span class="n">wf</span><span class="o">.</span><span class="n">getnchannels</span><span class="p">(),</span>
<span class="n">rate</span><span class="o">=</span><span class="n">wf</span><span class="o">.</span><span class="n">getframerate</span><span class="p">(),</span>
<span class="n">output</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">stream_callback</span><span class="o">=</span><span class="n">callback</span><span class="p">)</span>
<span class="c1"># start the stream (4)</span>
<span class="n">stream</span><span class="o">.</span><span class="n">start_stream</span><span class="p">()</span>
<span class="c1"># wait for stream to finish (5)</span>
<span class="k">while</span> <span class="n">stream</span><span class="o">.</span><span class="n">is_active</span><span class="p">():</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="c1"># stop stream (6)</span>
<span class="n">stream</span><span class="o">.</span><span class="n">stop_stream</span><span class="p">()</span>
<span class="n">stream</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">wf</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="c1"># close PyAudio (7)</span>
<span class="n">p</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
</pre></div>
</div>
<p>In callback mode, PyAudio will call a specified callback function (2)
whenever it needs new audio data (to play) and/or when there is new
(recorded) audio data available. Note that PyAudio calls the callback
function in a separate thread. The function has the following
signature <code class="docutils literal"><span class="pre">callback(<input_data>,</span> <span class="pre"><frame_count>,</span> <span class="pre"><time_info>,</span>
<span class="pre"><status_flag>)</span></code> and must return a tuple containing <code class="docutils literal"><span class="pre">frame_count</span></code>
frames of audio data and a flag signifying whether there are more
frames to play/record.</p>
<p>Start processing the audio stream using
<a class="reference internal" href="index.html#pyaudio.Stream.start_stream" title="pyaudio.Stream.start_stream"><code class="xref py py-func docutils literal"><span class="pre">pyaudio.Stream.start_stream()</span></code></a> (4), which will call the
callback function repeatedly until that function returns
<a class="reference internal" href="index.html#pyaudio.paComplete" title="pyaudio.paComplete"><code class="xref py py-data docutils literal"><span class="pre">pyaudio.paComplete</span></code></a>.</p>
<p>To keep the stream active, the main thread must not terminate, e.g.,
by sleeping (5).</p>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">PyAudio 0.2.11 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2017, Hubert Pham.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.9.
</div>
</body>
</html>
|