Python extension packages for windows pyaudio

Cross-platform audio I/O with PortAudio

Project description

PyAudio provides Python bindings for PortAudio 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.

  • Homepage
  • API Documentation
  • PyPi

Installation

See the INSTALLATION file in the source distribution for details. In summary, install PyAudio using pip on most platforms.

Windows

python -m pip install pyaudio

This installs the precompiled PyAudio library with PortAudio v19 19.7.0 included. The library is compiled with support for Windows MME API, DirectSound, WASAPI, and WDM-KS. It does not include support for ASIO. If you require support for APIs not included, you will need to compile PortAudio and PyAudio.

macOS

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

brew install portaudio
pip install pyaudio

GNU/Linux

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

sudo apt install python3-pyaudio

Alternatively, if the latest version of PyAudio is not available, install it using pip. Be sure to first install development libraries for portaudio19 and python3.

Building from source

See the INSTALLATION file.

Documentation & Usage Examples

  • Read the API Documentation, or generate it from the source using sphinx.

  • Usage examples are in the examples directory of the source distribution, or see the project homepage.

License

PyAudio is distributed under the MIT License. See LICENSE.txt.

Download files

Download the file for your platform. If you’re not sure which to choose, learn more about installing packages.

Source Distribution

Built Distributions

Precompiled & Extended | PyAudio with PortAudio for Windows

Used versions:
· PyAudio 0.2.11 | co 7090e25bcba41413bd7ce89aa73bc0efb1ae1ca1
· PortAudio V19 | co 1bdcb9e41357ec76d8cf73f9ef278202a3ea1e3b

Extensions:
· Support of Windows sound loopback: Record the output of your soundcard


This project is a fork of two open source projects. If you’d like, give them some love:

  • http://www.portaudio.com/
  • https://people.csail.mit.edu/hubert/pyaudio/

Usage

See the example.

Exactly like the official PyAudio but with the extra option «as_loopback» which expects a boolean.

import pyaudio
p = pyaudio.PyAudio()
stream = p.open([...], as_loopback = True)

How to install?

You can find the precompiled PyAudio build, static linked with PortAudio, as well as only the static linked PortAudio in the release.

I will try to rebuild the project on each update from one of the used projects.

How to build?

Cygwin

You will need a working cygwin installation with basic developer tools and python.

Step 0

You might have to change all files to use LF line endings with

find . -type f -exec sed -i 's/x0d//g' {} +

Step 1

Change to /pyaudio/portaudio-v19 and type

./configure --with-winapi=wasapi --enable-static=yes --enable-shared=no
make loopback

To rebuild type

Step 2

Change to /pyaudio and type

python setup.py install --static-link

Microsoft Visual Studio (2017)

You will need to include the python executable in PATH.

Step 1

  • Open the portaudio project located in pyaudioportaudio-v19buildmsvcportaudio.sln.

  • Open the project configuration and make sure that the configuration type is set to static library.

  • Select the build type Release and x64. Then build the project.

  • Make sure the build was succesful and the file pyaudioportaudio-v19buildmsvcx64Releaseportaudio.lib does exist.

Step 2

Open the PowerShell as administrator and change the directory. Then you can build and install pyaudio with portaudio:

cd <Location of the repository>pyaudio_portaudiopyaudio
python.exe .setup.py install --static-link

Microsoft Visual Studio (2017) — 32 bit

For 32-bit support see instructions above and this issue: #8.

The code from that issue is merged now, but as I do not have a 32-bit version I can’t verify it. Comment in the issue for further help.

Help!!

If you get errors, let me know. Thank you ♥

I am currently trying to install PyAudio on Python 3.7 but when I use the command «pip install pyaudio«, an error message appears:

  "command 'cl.exe' failed: No such file or directory".

I have already tried to modify the PATH but it doesn’t work. I thought maybe someone could help me?

cigien's user avatar

cigien

56.7k11 gold badges69 silver badges107 bronze badges

asked Mar 5, 2019 at 8:05

Cyrielle Albert's user avatar

Cyrielle AlbertCyrielle Albert

1291 gold badge2 silver badges7 bronze badges

Download the wheel on this site https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio.

Choose PyAudio‑0.2.11‑cp37‑cp37m‑win32.whl if you use 32 bit, or PyAudio‑0.2.11‑cp37‑cp37m‑win_amd64.whl for 64 bit.
Then go to your download folder:

cd <your_donwload_path>

Then, for 64 bit:

pip install PyAudio‑0.2.11‑cp37‑cp37m‑win_amd64.whl

or for 32 bit:

pip install PyAudio‑0.2.11‑cp37‑cp37m‑win32.whl

answered Mar 5, 2019 at 9:38

Hiadore's user avatar

7

WINDOWS

  1. pip install pipwin
  2. pipwin install pyaudio

This will install pyaudio

answered Jan 7, 2020 at 7:24

Joljas Kerketta's user avatar

It is supported in Python 3.7. Even though not officially on standalone 3.7, Anaconda 2019.10 which uses 3.7 has it.
So you have 2 options:

  1. You can conda install -c anaconda pyaudio, if you use Anaconda
  2. Or for standalone Python 3.7 you can:

2a)
After installing the .whl, build portaudio.dll just following the instructions at http://portaudio.com/docs/v19-doxydocs/compile_windows.html. I used VS2019 and i worked.
Note if you choose ASIO option you will need ASIO SDK from https://www.steinberg.net/en/company/developers.html

After that just put portaudio.dll anywhere in your system path or local directory and you can import pyaudio.

2b) If you don’t want to build, simply steal portaudio.dll from Anaconda. It’s in <Anaconda_root>/Library/bin

Community's user avatar

answered Nov 13, 2019 at 1:07

David's user avatar

DavidDavid

6045 silver badges4 bronze badges

It’s not compatible with 3.7 and as far as I have been able to find thus far there doesn’t appear to be any work being put into making it compatible and I haven’t found any sort of replacement, so unless you want to build the package yourself and make it compatible with 3.7 then it can’t be used without downgrading your python install to 3.6, I did try Hiadore’s answer and that does not work either with 3.7.

answered Mar 7, 2019 at 1:02

Zenshu's user avatar

Simply Download wheel file from https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio , get the file for your Python version and bitness. Change the directory location to the download file location or copy downloaded file to the working location.

Install it with pip install filename.whl

sanyassh's user avatar

sanyassh

7,87213 gold badges32 silver badges63 bronze badges

answered May 25, 2019 at 22:57

Influencer_ Subodh Padgilwar's user avatar

Extract to https://people.csail.mit.edu/hubert/pyaudio/

In Windows:

Install using pip:

python -m pip install pyaudio 

Currently, there are wheels compatible with the official distributions of Python 2.7, 3.4, 3.5, and 3.6.

Apparently there is no version of that library for Python 3.7, so I’d try downgrading the Python version.

If you continue to have problems with the error of .exe, in this answer https://stackoverflow.com/a/41724634/7290770 they solve it.

answered Mar 5, 2019 at 8:14

mollywind's user avatar

mollywindmollywind

3801 gold badge3 silver badges14 bronze badges

pyaudio is not supported with python 3.7.

Downgrade your version to python 3.6.8.

then try installing pyaudio using the command: pip install pyaudio

answered Oct 7, 2019 at 6:18

A_jain2310's user avatar

A_jain2310A_jain2310

391 silver badge5 bronze badges

  1. Install python 3.6
  2. Install PyCharm
  3. While in PyCharm click File
  4. Click on settings
  5. Click Project Interpreter
  6. Click on the Gear icon ,You should see Add,Show All
    Click on Add
  7. Next Click System Interpreter
  8. Click the dropdown if you installed python 3.6 you should see it there
  9. Click it Hit ok and install PyAudio this should fix it..

patrick.elmquist's user avatar

answered Nov 23, 2019 at 20:23

Lifeofkaz's user avatar

Use this link to download .whl file according to the python version it supports.
For Python -3.7 use PyAudio‑0.2.11‑cp37‑cp37m‑win32.whl

Go and run this command on the Windows command prompt:

pip install PyAudio-0.2.11-cp37-cp37m-win32.whl

WhipStreak23's user avatar

answered Apr 9, 2020 at 17:49

Shristi Bisht's user avatar

1

To get started with playback and recording audio on Windows, Linux, and MacOS in a Python environment you should consider using the PyAudio library. PyAudio is a set of Python bindings for PortAudio, a cross-platform C++ library interfacing with audio drivers.

Installing PyAudio

Since PyAudio depends on PortAudio you will need to have it installed in order for installation to be successful.

You can find the official installation instructions for the latest version of PyAudio (0.2.11) on the project website but unfortunately there a few common problems new users run into.

For Python 3.7.x and later you can’t simply pip install pyaudio because the version-specific precompiled binaries are currently not available. Depending on your operating system you may need to take a few other extra steps as well.

On Windows

To help with installation on Windows, there is a repository of pre-compiled .whl options. You’ll need to pick the one that matches your system and Python build.

For example, for Python 3.7 choose:

pip install PyAudio‑0.2.11‑cp37‑cp37m‑win_amd64.whl

If you are using any of older versions of Python (specifically 2.7, 3.4, 3.5 or 3.6) you can simply go with:

pip install pyaudio

The above examples assume that you run pip within your Python virtual environment. If not use ‘python -m pip’ command instead.

On OSX

You need to have the Xcode command line tools and Homebrew installed in order to install PortAudio first.

brew install portaudio

and then PyAudio will install with pip install pyaudio.

If you get an error like the following:

src/_portaudiomodule.c:29:10: fatal error: 'portaudio.h' file not found
#include "portaudio.h"
               ^~~~~
1 error generated.
error: command 'clang' failed with exit status 1 

Then run something similar to the following command to help identify the location of PortAudio:

pip install --global-option="build_ext" --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib" pyaudio

On Linux

You can install PyAudio with your package manager, such as:

sudo apt-get install python3-pyaudio

Importing PyAudio

Let’s start with importing PyAudio:

import pyaudio

In order to verify that it was installed correctly, try getting the PortAudio version:

pyaudio.get_portaudio_version()

Exploring Audio Input and Output Devices

This schema illustrates the basic concept behind PortAudio as it interacts with various native audio APIs.

PortAudio Architecture

https://portaudio.com/docs/v19-doxydocs/api_overview.html

Start by creating a PyAudio instance and getting some information on the default Host API (ie. audio driver):

pa = pyaudio.PyAudio()
pa.get_default_host_api_info()

The results are system specific and will likely look different on your own machine.

For example, here are the results on a Windows PC:

{'index': 0,
'structVersion': 1,
'type': 2,
'name': 'MME',
'deviceCount': 6,
'defaultInputDevice': 1,
'defaultOutputDevice': 4}

and on a Mac:

{'index': 0,
 'structVersion': 1,
 'type': 5,
 'name': 'Core Audio',
 'deviceCount': 5,
 'defaultInputDevice': 2,
 'defaultOutputDevice': 1}

Let’s see if there are any other audio interfaces and check their details:

for id in range(pa.get_host_api_count()):
    print(pa.get_host_api_info_by_index(id))

As you saw in the diagram above, there are multiple native audio APIs present in the Windows system: MME, DirectSound, ASIO, Windows Wasapi, and WDM-KS.

Let’s check the details of a default audio output device:

pa.get_default_output_device_info()

Here are the results on Windows with built-in speakers:

{'index': 4,
 'structVersion': 2,
 'name': 'Speaker/HP (Realtek High Defini',
 'hostApi': 0,
 'maxInputChannels': 0,
 'maxOutputChannels': 2,
 'defaultLowInputLatency': 0.09,
 'defaultLowOutputLatency': 0.09,
 'defaultHighInputLatency': 0.18,
 'defaultHighOutputLatency': 0.18,
 'defaultSampleRate': 44100.0}

and on a Mac using a pair of headphones:

{'index': 1,
 'structVersion': 2,
 'name': 'Jabra EVOLVE 65',
 'hostApi': 0,
 'maxInputChannels': 0,
 'maxOutputChannels': 2,
 'defaultLowInputLatency': 0.01,
 'defaultLowOutputLatency': 0.004354166666666667,
 'defaultHighInputLatency': 0.1,
 'defaultHighOutputLatency': 0.0136875,
 'defaultSampleRate': 48000.0}

To get the full information on all available I/O interfaces, execute the code below:

for id in range(pa.get_device_count()):
  dev_dict = pa.get_device_info_by_index(id)
  for key, value in dev_dict.items():
      print(key, value)

Keep in mind that input and output sections of a single audio device will appear as separate items here. It also applies to different audio interfaces of a physical unit (e.g. analog/digital, line/headphone, optical/coaxial, etc.)

Audio Playback

In order to play to an output device you first need to open a wav file:

import wave
wav_file = wave.open('audio-clip.wav')

Then create an output stream (note that sample rate, channels and format of the stream have to match the wav parameters):

stream_out = pa.open(
    rate=wav_file.getframerate(),     # sampling rate
    channels=wav_file.getnchannels(), # number of output channels
    format=pa.get_format_from_width(wav_file.getsampwidth()),  # sample format and length
    output=True,             # output stream flag
    output_device_index=4,   # output device index
    frames_per_buffer=1024,  # buffer length
)

Finally fill the output stream with samples read from the file:

output_audio = wav_file.readframes(5 * wav_file.getframerate())
stream_out.write(output_audio)

You should hear a few seconds of your audio clip.

Audio Recording

Similarly, you can read audio samples from the input. Create an input stream with parameters corresponding to your input device configuration (check audio system settings to explore that):

stream_in = pa.open(
    rate=48000,
    channels=2,
    format=pyaudio.paInt16,
    input=True,                   # input stream flag
    input_device_index=1,         # input device index
    frames_per_buffer=1024
)

# read 5 seconds of the input stream
input_audio = stream_in.read(5 * 48000)

Finally, save the result to a wav output file:

output_filename = 'audio-recording.wav'
wav_file = wave.open(output_filename, 'wb')

# define audio stream properties
wav_file.setnchannels(2)        # number of channels
wav_file.setsampwidth(2)        # sample width in bytes
wav_file.setframerate(48000)    # sampling rate in Hz

# write samples to the file
wav_file.writeframes(input_audio)

Improving Quality

Quality depends highly on your microphone and local environment, as you may have picked up some noise or other quality issues in the captured audio.  You can use the Media Processing Enhance API as a quick & convenient way to improve the audio without need of doing manual editing or buying expensive hardware.

You can find a workflow demonstration in python to upload the wav file you captured, process it, and download the result.  Take a look at the sample code on GitHub for an example: https://github.com/DolbyIO/media-api-samples/blob/master/python/workflows/media_enhance.py

Summary

This article should give you a good start with getting up and running with PyAudio. There is a lot more you can do with this library beyond playing back or recording audio so keep an eye out for more sample workflows in future posts.

anaconda

/

packages

/

pyaudio

0.2.11

  • 9

Bindings for PortAudio v19, the cross-platform audio stream library

  • Conda

  • Files
  • Labels
  • Badges

  • License: MIT

  • Home: http://people.csail.mit.edu/hubert/pyaudio/

  • Development: http://people.csail.mit.edu/hubert/git/?p=pyaudio.git

  • Documentation: http://people.csail.mit.edu/hubert/pyaudio/docs/

  • 27435 total downloads

  • Last upload: 9 months and 1 day ago

Installers

  • linux-ppc64le
    v0.2.11

  • linux-64
    v0.2.11

  • win-32
    v0.2.11

  • linux-aarch64
    v0.2.11

  • osx-64
    v0.2.11

  • linux-32
    v0.2.11

  • win-64
    v0.2.11

conda install

To install this package run one of the following:
conda install -c anaconda pyaudio


Description

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 Mac OS X.

This article covers the audio library, Pyaudio in Python.

Pyaudio is a Python binding for PortAudio, a cross platform library for input and output of audio. This basically means that we can use Pyaudio to record and play sound across all platforms and Operating systems such as windows, Mac and Linux.

Be sure to download and install the pyaudio library before trying any of the commands and functions that we’ll be teaching you.

Problems with installing Pyaudio

If you’re on windows you’ll probably end up running into some errors while attempting to install Pyaudio. In order to fix this and the “Pyaudio module not found error”, you need to do the following 2 steps in the command prompt.

  1. pip install pipwin
  2. pipwin install pyaudio

This is the simplest way to get it working.


Playing Audio with PyAudio

You’ve probably already noticed, but we’ll be needing more than just the pyaudio module. We’ll need some supporting modules such as wave. If you installed Pyaudio the way we instructed, these modules will install themselves automatically.

The code below is pretty self explanatory, especially with the help of the comments.

import pyaudio
import wave

filename = 'background.wav'

# Defines a chunk size of 1024 samples per data frame.
chunk = 1024  

# Open sound file  in read binary form.
file = wave.open(filename, 'rb')

# Initialize PyAudio
p = pyaudio.PyAudio()

# Creates a Stream to which the wav file is written to.
# Setting output to "True" makes the sound be "played" rather than recorded
stream = p.open(format = p.get_format_from_width(file.getsampwidth()),
                channels = file.getnchannels(),
                rate = file.getframerate(),
                output = True)

# Read data in chunks
data = file.readframes(chunk)

# Play the sound by writing the audio data to the stream
while data != '':
    stream.write(data)
    data = file.readframes(chunk)

# Stop, Close and terminate the stream
stream.stop_stream()
stream.close()
p.terminate()

To create a stream, you need to define certain things such as the number of channels, the sampling rate etc. Using commands like file.getnchannels() we extract the relevant data from the file and use it to create the appropriate audio stream.

The stop_stream() function isn’t actually needed here as by the time the code arrives at it’s location, the stream has already finished. It’s a useful function to know if you want to stop the stream half way through or something.


Recording sound with PyAudio

If we reverse what we did in the previous section, we can “record” audio instead.

import pyaudio
import wave

chunk = 1024      # Each chunk will consist of 1024 samples
sample_format = pyaudio.paInt16      # 16 bits per sample
channels = 2      # Number of audio channels
fs = 44100        # Record at 44100 samples per second
time_in_seconds = 3
filename = "soundsample.wav"

p = pyaudio.PyAudio()  # Create an interface to PortAudio

print('-----Now Recording-----')

#Open a Stream with the values we just defined
stream = p.open(format=sample_format,
                channels = channels,
                rate = fs,
                frames_per_buffer = chunk,
                input = True)

frames = []  # Initialize array to store frames

# Store data in chunks for 3 seconds
for i in range(0, int(fs / chunk * time_in_seconds)):
    data = stream.read(chunk)
    frames.append(data)

# Stop and close the Stream and PyAudio
stream.stop_stream()
stream.close()
p.terminate()

print('-----Finished Recording-----')

# Open and Set the data of the WAV file
file = wave.open(filename, 'wb')
file.setnchannels(channels)
file.setsampwidth(p.get_sample_size(sample_format))
file.setframerate(fs)

#Write and Close the File
file.writeframes(b''.join(frames))
file.close()

As you can see, this time we defined our own channels, sample and format (start of the code) in the start, which we later use while opening and creating the WAV file (end of the code). In the middle is part where the audio was being recorded for the duration that we set (3 seconds).

Now try running the code for yourself. When the “recording” prompt appears, start speaking. After three seconds a wav file will appear in the same location as your script which contains your voice. Congratulations, you just learnt how to make a sound recorder.

Other sound libraries:

  • Python Playsound
  • Pygame audio mixer
  • Python simpleaudio

This marks the end of the Python Pyaudio article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

PyAudio

PyAudio provides Python bindings for PortAudio 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.

  • Homepage
  • API Documentation
  • PyPi

Installation

See the INSTALLATION file in the source distribution for details. In summary, install PyAudio using pip on most platforms.

Windows

python -m pip install pyaudio

This installs the precompiled PyAudio library with PortAudio v19 19.7.0 included. The library is compiled with support for Windows MME API, DirectSound, WASAPI, and WDM-KS. It does not include support for ASIO. If you require support for APIs not included, you will need to compile PortAudio and PyAudio.

macOS

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

brew install portaudio
pip install pyaudio

GNU/Linux

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

sudo apt install python3-pyaudio

Alternatively, if the latest version of PyAudio is not available, install it using pip. Be sure to first install development libraries for portaudio19 and python3.

Building from source

See the INSTALLATION file.

Documentation & Usage Examples

  • Read the API Documentation, or generate it from the source using sphinx.

  • Usage examples are in the examples directory of the source distribution, or see the project homepage.

License

PyAudio is distributed under the MIT License. See LICENSE.txt.

Понравилась статья? Поделить с друзьями:
  • Python extension packages for windows christoph gohlke
  • Python download for windows 10 64 bit
  • Python download 32 bit windows 7 free
  • Python decode windows 1251 to utf 8
  • Python 3 скачать для windows 10 на русском 64 бит