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.
pip install pipwin
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:
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.