Movatterモバイル変換


[0]ホーム

URL:


Snakey: Headphones wearing Python

PyAudio

PyAudio providesPython bindings forPortAudio v19, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms, such as GNU/Linux, Microsoft Windows, and Apple macOS.

PyAudio is distributed under the MIT License.

This library was originally inspired by:

What's new

November 23, 2024

PyAudio 0.2.14 pre-compiled wheels for Microsoft Windows (32-bit and 64-bit) are now available forPython 3.13 onPyPI. Install using pip, as describedbelow.

November 6, 2023

PyAudio 0.2.14 is a new release that fixes a build issue. Thanks to Bhanu Victor DiCara for the help.

Also, PyAudio 0.2.14 pre-compiled wheels for Microsoft Windows (32-bit and 64-bit) are now available forPython 3.12 onPyPI. Install using pip, as describedbelow.

December 26, 2022

PyAudio 0.2.13 is a new release focused on refactoring, maintenance, and cleanup. See the CHANGELOG in thesource distribution for details. Notable changes include:

October 25, 2022

PyAudio 0.2.12 pre-compiled wheels for Microsoft Windows (32-bit and 64-bit) are now available forPython 3.11 onPyPI. Install using pip, as describedbelow.

July 18, 2022

PyAudio 0.2.12 is a new release with many updates. See the CHANGELOG in thesource distribution for details. Notably, the PyAudio build process for Microsoft Windows is streamlined, using the native toolchain. Special thanks to:

March 18, 2017

PyAudio 0.2.11 is a new release with a bug fix related to memory management.

Many thanks to both Blaise Potard and Matthias Schaff for discovering the issue and for their patches! Thanks as well to Timothy Port for helping to correct a docstring.

January 16, 2017

PyAudio 0.2.10 is a new release with bug fixes related to the Python GIL. It also introduces a few automated unit tests.

Great thanks to Michael Graczyk for discovering the GIL-related issues and for submitting a patch!

October 19, 2015

PyAudioinstallation is better streamlined. Install PyAudio on most platforms using pip.

October 18, 2015

PyAudio 0.2.9 is a new release with bug fixes related to overflow error handling and IOError exception arguments.

Many thanks to Tony Jacobson for discovering and helping with the overflow error. Thanks also to Sami Liedes for reporting the IOError exception issue!

April 12, 2014

PyAudio 0.2.8 is a new release with bug fixes related to the Python GIL and device name encoding.

Many thanks to Jason Roehm for discovering and patching the threading-related issue!

January 30, 2014

Many thanks to Sebastian Audet, who wroteinstructions for building PyAudio using Microsoft Visual Studio!

October 27, 2012

PyAudio 0.2.7 is a new release with:

Thanks again to Bastian Bechtold for his help converting the documentation for use with Sphinx! In addition, thanks to John K. Luebs for the callback fix.

October 14, 2012

PyAudio 0.2.6 is a new release with:

Many thanks to Bastian Bechtold and Bob Jamison for their contributions! Without their patches and Bastian's careful review, this release would still be far away. Also, great thanks to Danilo J. S. Bellini for reporting bugs.

Note: As of this update, PyAudio is compatible with Python 2.6, Python 2.7, and Python 3.2. For Python installations older than 2.6, usePyAudio 0.2.4.

August 22, 2012

This web page has beentranslated to French—thanks to the efforts of Cyril Danilevski!

April 20, 2012

This web page has beentranslated to Russian—thanks to the efforts of Oleg Meister!

March 7, 2012

This web page has beentranslated to German—thanks to the efforts of Olga Babenko!

December 7, 2010

This web page has beentranslated to Belorussian—thanks to the efforts of Jason Fragoso!

November 2, 2010

PyAudio 0.2.4 has been uploaded to Debian (sid). A huge thanks to Felipe Sateler for sponsoring the package!

October 12, 2010

PyAudio 0.2.4 is a maintenance release—there are no new features or bug fixes. The binary packages now include PortAudio-v19 (r1535). The source for PyAudio is now ingit (previously subversion).

November 2, 2008

PyAudio 0.2.3 fixes several outstanding bugs (thanks to all who have sent patches); see theCHANGELOG for details. PyAudio 0.2.3 provides bindings for PortAudio-v19 (r1395) and now includes binary distributions for Python 2.4, 2.5, and 2.6.

March 5, 2008

PyAudio 0.2.0 now works with both Python 2.4 and Python 2.5. Additionally, PyAudio features support for PortAudio's Mac OS X Host API Specific Stream Info extensions (e.g., forchannel mapping)—see examples below. The new binary installers include an updated version of PortAudio-v19 (r1368).

July 6, 2006

PyAudio 0.1.0 is released.

Older news...

Installation

The current version isPyAudio v0.2.14 and supports Python version 3.8+. Install PyAudio using pip on most platforms. For PyAudio versions prior to v0.2.9, PyAudio distributed installation binaries, which arearchived here.

Microsoft Windows

Install usingpip:

python -m pip install pyaudio

Notes:


Apple macOS

UseHomebrew to install the prerequisite portaudio library, then install PyAudio using pip:

brew install portaudio
pip install pyaudio

Notes:


GNU/Linux

Use the package manager to install PyAudio. For example, on Debian-based systems:

sudo apt-get install python3-pyaudio

If the latest version of PyAudio is not available, install it using pip:

pip install pyaudio

Notes:


PyAudio Source

Source is available for download at thePython Package Index (PyPI).

Or clone the git repository:

git clone https://people.csail.mit.edu/hubert/git/pyaudio.git

To build PyAudio from source, you will also need to buildPortAudio v19. See the INSTALLATION file in the PyAudio source bundle for build instructions.

Documentation & Examples

Browse thePyAudio API documentation. PyAudio roughly mirrors thePortAudio v19 API 2.0.

The PyAudiosource distribution contains a set of demos. Here's a selection from that set:

"""PyAudio Example: Play a wave file."""importwaveimportsysimportpyaudioCHUNK=1024iflen(sys.argv)<2:print(f'Plays a wave file. Usage:{sys.argv[0]} filename.wav')sys.exit(-1)withwave.open(sys.argv[1],'rb')aswf:# Instantiate PyAudio and initialize PortAudio system resources (1)p=pyaudio.PyAudio()# Open stream (2)stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True)# Play samples from the wave file (3)whilelen(data:=wf.readframes(CHUNK)):# Requires Python 3.8+ for :=stream.write(data)# Close stream (4)stream.close()# Release PortAudio system resources (5)p.terminate()
"""PyAudio Example: Record a few seconds of audio and save to a wave file."""importwaveimportsysimportpyaudioCHUNK=1024FORMAT=pyaudio.paInt16CHANNELS=1ifsys.platform=='darwin'else2RATE=44100RECORD_SECONDS=5withwave.open('output.wav','wb')aswf:p=pyaudio.PyAudio()wf.setnchannels(CHANNELS)wf.setsampwidth(p.get_sample_size(FORMAT))wf.setframerate(RATE)stream=p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True)print('Recording...')for_inrange(0,RATE//CHUNK*RECORD_SECONDS):wf.writeframes(stream.read(CHUNK))print('Done')stream.close()p.terminate()
"""PyAudio Example: full-duplex wire between input and output."""importsysimportpyaudioRECORD_SECONDS=5CHUNK=1024RATE=44100p=pyaudio.PyAudio()stream=p.open(format=p.get_format_from_width(2),channels=1ifsys.platform=='darwin'else2,rate=RATE,input=True,output=True,frames_per_buffer=CHUNK)print('* recording')foriinrange(0,int(RATE/CHUNK*RECORD_SECONDS)):stream.write(stream.read(CHUNK))print('* done')stream.close()p.terminate()
"""PyAudio Example: Play a wave file (callback version)."""importwaveimporttimeimportsysimportpyaudioiflen(sys.argv)<2:print(f'Plays a wave file. Usage:{sys.argv[0]} filename.wav')sys.exit(-1)withwave.open(sys.argv[1],'rb')aswf:# Define callback for playback (1)defcallback(in_data,frame_count,time_info,status):data=wf.readframes(frame_count)# If len(data) is less than requested frame_count, PyAudio automatically# assumes the stream is finished, and the stream stops.return(data,pyaudio.paContinue)# Instantiate PyAudio and initialize PortAudio system resources (2)p=pyaudio.PyAudio()# Open stream using callback (3)stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True,stream_callback=callback)# Wait for stream to finish (4)whilestream.is_active():time.sleep(0.1)# Close the stream (5)stream.close()# Release PortAudio system resources (6)p.terminate()
"""PyAudio Example: Audio wire between input and output. Callback version."""importtimeimportsysimportpyaudioDURATION=5# secondsdefcallback(in_data,frame_count,time_info,status):return(in_data,pyaudio.paContinue)p=pyaudio.PyAudio()stream=p.open(format=p.get_format_from_width(2),channels=1ifsys.platform=='darwin'else2,rate=44100,input=True,output=True,stream_callback=callback)start=time.time()whilestream.is_active()and(time.time()-start)<DURATION:time.sleep(0.1)stream.close()p.terminate()
"""PyAudio Example: macOS-only: Play a wave file with channel maps."""importwaveimportsysimportpyaudioCHUNK=1024iflen(sys.argv)<2:print(f'Plays a wave file. Usage:{sys.argv[0]} filename.wav')sys.exit(-1)# 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 speakerchannel_map=(1,-1)# etc...try:stream_info=pyaudio.PaMacCoreStreamInfo(flags=pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice,channel_map=channel_map)exceptAttributeError:print('Could not find PaMacCoreStreamInfo. Ensure you are running on macOS.')sys.exit(-1)print('Stream Info Flags:',stream_info.flags)print('Stream Info Channel Map:',stream_info.channel_map)withwave.open(sys.argv[1],'rb')aswf:p=pyaudio.PyAudio()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)# Play streamwhilelen(data:=wf.readframes(CHUNK)):# Requires Python 3.8+ for :=stream.write(data)stream.close()p.terminate()

License

PyAudio is distributed under theMIT License:

Copyright (c) 2006Hubert 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.

Acknowledgements

Many thanks to Andrew Baldwin, Alex ThreeD, Timothée Lecomte, Frank Samuelson, Matthieu Brucher, Chris Stawarz, Barry Walker, Bob Jamison, Danilo J. S. Bellini, Bastian Bechtold, Christoph Gohlke, Sebastian Audet, Jason Roehm, Tony Jacobson, Sami Liedes, Michael Graczyk, Blaise Potard, Matthias Schaff, Timothy Port, Sean Zimmermann, Jason Hihn, Eiichi Takamori, Matěj Cepl, Vasily Zakharov, Artur Janowiec, and Bhanu Victor DiCara for their much appreciated suggestions and patches—as well as to others who have written to say hello!

Special thanks to Felipe Sateler and Justin Mazzola Paluska for Debian/Ubuntu packaging help.

The development of PyAudio was funded in part by the Cambridge-MIT Institute and T-Party.

Contact

For help with PyAudio, your best bet for a timely response is to consult or post toStackOverflow.

Comments, suggestions, and patches welcomed. Send mail to my first name atmit.edu.


[8]ページ先頭

©2009-2025 Movatter.jp