PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms.
PyAudio is designed to work with the PortAudio v19 API 2.0. Note that PyAudio currently only supports blocking-mode audio I/O.
PyAudio is inspired by:
| Download: |
PyAudio for Python 2.6
md5sum: be0645039ca6e01f39bb9a05ef801228 |
|
|
PyAudio for Python 2.5.2
md5sum: 652c393f5cf12fad5cd0f5e8576f46dd |
||
|
PyAudio for Python 2.4.4
md5sum: a69e26002886b4f76759eed039ff8922 |
||
| Notes: | If you are using Python 2.6, please be sure to have installed Python for all users (rather than just for the current user). The PyAudio installer will not run otherwise. To install Python for all users in Windows Vista, jaux.net offers some hints. |
| Download: |
PyAudio for Mac OS X (Universal)
md5sum: 4a5c0eb77ae9b4017b64e1c6e64c1341 |
% apt-get install libportaudio2 python-support
% dpkg -i python-pyaudio_0.2.3_i386.deb
# (or python-pyaudio_0.2.3_amd64.deb)
| Download: | PyAudio deb package (i386)
md5sum: 9f6a0d6260ce576b5bbd5deac954df7b |
|
| PyAudio deb package (amd64)
md5sum: 8c4bce4e772ccbfd9286b714460c0efd |
| Download: |
PyAudio tarball
md5sum: 227919af592c39f93c561275cb99c2dc |
Subversion:
% svn co https://svn.csail.mit.edu/pyaudio/trunk/pyaudio/ \
pyaudio
Additionally, you can browse the PyAudio API documentation. PyAudio roughly mirrors the PortAudio v19 API 2.0.
If you're building PyAudio from source, here are compilation hints for building PyAudio on GNU/Linux, Windows (cygwin or Win32), and Mac OS X.
""" Play a WAVE file. """
import pyaudio
import wave
import sys
chunk = 1024
if len(sys.argv) < 2:
print "Plays a wave file.\n\n" +\
"Usage: %s filename.wav" % sys.argv[0]
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
p = pyaudio.PyAudio()
# open stream
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data
data = wf.readframes(chunk)
# play stream
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
""" Record a few seconds of audio and save to a WAVE file. """
import pyaudio
import wave
import sys
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)
print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
print "* done recording"
stream.close()
p.terminate()
# write data to WAVE file
data = ''.join(all)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
""" A wire between input and output. """
import pyaudio
import sys
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
output = True,
frames_per_buffer = chunk)
print "* recording"
for i in range(0, 44100 / chunk * RECORD_SECONDS):
data = stream.read(chunk)
stream.write(data, chunk)
print "* done"
stream.stop_stream()
stream.close()
p.terminate()
""" Mac OS X only: specifying channel maps. """
import pyaudio
import wave
import sys
chunk = 1024
PyAudio = pyaudio.PyAudio
if len(sys.argv) < 2:
print "Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0]
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
p = PyAudio()
# standard L-R stereo
# channel_map = (0, 1)
# reverse: R-L stereo
# channel_map = (1, 0)
# no audio
# channel_map = (-1, -1)
# left channel audio --> left speaker; no right channel
# channel_map = (0, -1)
# right channel audio --> right speaker; no left channel
# channel_map = (-1, 1)
# left channel audio --> right speaker
# channel_map = (-1, 0)
# right channel audio --> left speaker
channel_map = (1, -1)
# etc...
try:
stream_info = pyaudio.PaMacCoreStreamInfo(
# default:
flags = pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice,
channel_map = channel_map)
except AttributeError:
print "Sorry, couldn't find PaMacCoreStreamInfo. Make sure that" \
" you're running on Mac OS X."
import sys
sys.exit(-1)
print "Stream Info Flags:", stream_info.get_flags()
print "Stream Info Channel Map:", stream_info.get_channel_map()
# open stream
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True,
output_host_api_specific_stream_info = stream_info)
# read data
data = wf.readframes(chunk)
# play stream
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.stop_stream()
stream.close()
p.terminate()
Copyright (c) 2006-2008 Hubert Pham
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The development of PyAudio was funded in part by the Cambridge-MIT Institute and T-Party.
Many thanks to Andrew Baldwin, Alex ThreeD, Timothée Lecomte, Frank Samuelson, Matthieu Brucher, and Chris Stawarz for their much appreciated suggestions and patches—as well as to others who have written to say hello!
Also special thanks to Justin Mazzola Paluska for packaging PyAudio for Debian/Ubuntu.