Skip to main content

PyAudio Examples

PyAudio How To Examples in Python

PyAudio is a cross-platform audio I/O library that provides access to the audio hardware on your computer. It allows you to record and play audio streams in real-time. In this tutorial, we will explore various examples of using the PyAudio library in Python.

Example 1: Recording Audio

To record audio using PyAudio, you need to create an instance of the pyaudio.PyAudio class and open a stream with the desired audio parameters. Here's an example:

import pyaudio
import wave

def record_audio(filename, duration):
chunk = 1024
format = pyaudio.paInt16
channels = 1
rate = 44100

p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)

print("Recording started...")
frames = []

for i in range(int(rate / chunk * duration)):
data = stream.read(chunk)
frames.append(data)

print("Recording finished.")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format))
wf.setframerate(rate)
wf.writeframes(b''.join(frames))
wf.close()

record_audio("output.wav", 5)

This example records audio for a duration of 5 seconds and saves it to a WAV file named "output.wav". The record_audio function opens an audio stream, reads audio data in chunks, and appends it to a list of frames. Finally, it writes the frames to a WAV file.

Example 2: Playing Audio

To play audio using PyAudio, you need to create an instance of the pyaudio.PyAudio class and open a stream with the desired audio parameters. Here's an example:

import pyaudio
import wave

def play_audio(filename):
chunk = 1024

wf = wave.open(filename, 'rb')

p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)

print("Playing audio...")

data = wf.readframes(chunk)
while data:
stream.write(data)
data = wf.readframes(chunk)

print("Audio playback finished.")

stream.stop_stream()
stream.close()
p.terminate()

play_audio("output.wav")

This example reads audio data from a WAV file named "output.wav" and plays it in real-time. The play_audio function opens the audio file, creates an audio stream, and continuously writes audio data to the stream until the end of the file is reached.

Example 3: Changing Audio Parameters

You can modify the audio format, channels, and sample rate to suit your requirements. Here's an example that demonstrates changing the audio parameters for recording and playing audio:

import pyaudio
import wave

def record_audio(filename, duration, format, channels, rate):
chunk = 1024

p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)

print("Recording started...")
frames = []

for i in range(int(rate / chunk * duration)):
data = stream.read(chunk)
frames.append(data)

print("Recording finished.")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format))
wf.setframerate(rate)
wf.writeframes(b''.join(frames))
wf.close()

def play_audio(filename, format, channels, rate):
chunk = 1024

wf = wave.open(filename, 'rb')

p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
output=True)

print("Playing audio...")

data = wf.readframes(chunk)
while data:
stream.write(data)
data = wf.readframes(chunk)

print("Audio playback finished.")

stream.stop_stream()
stream.close()
p.terminate()

record_audio("output.wav", 5, pyaudio.paInt16, 2, 44100)
play_audio("output.wav", pyaudio.paInt16, 2, 44100)

In this example, the record_audio and play_audio functions accept additional parameters for audio format, channels, and sample rate. You can modify these values to meet your specific requirements.

Example 4: Stream Callbacks

PyAudio allows you to use stream callbacks for real-time audio processing. Here's an example that demonstrates how to use a stream callback to visualize audio data:

import pyaudio
import numpy as np
import matplotlib.pyplot as plt

def visualize_audio(data, frame_count, time_info, status):
samples = np.frombuffer(data, dtype=np.int16)
plt.plot(samples)
plt.show(block=False)
plt.pause(0.1)
plt.clf()

return None, pyaudio.paContinue

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024,
stream_callback=visualize_audio)

stream.start_stream()

while stream.is_active():
pass

stream.stop_stream()
stream.close()
p.terminate()

In this example, the visualize_audio function is called as a stream callback. It receives audio data as a byte buffer and converts it to a NumPy array for visualization using Matplotlib. The plot is updated every 0.1 seconds to display real-time audio data.

Example 5: Audio Device Selection

You can select specific audio input and output devices using PyAudio. Here's an example that lists available audio devices and allows you to choose the desired ones:

import pyaudio

def list_audio_devices():
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
num_devices = info.get('deviceCount')

print("Available audio devices:")
for i in range(num_devices):
device_info = p.get_device_info_by_host_api_device_index(0, i)
print(f"{i + 1}. {device_info['name']}")

p.terminate()

def select_audio_devices(input_device_index, output_device_index):
chunk = 1024
format = pyaudio.paInt16
channels = 1
rate = 44100

p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
output=True,
input_device_index=input_device_index,
output_device_index=output_device_index,
frames_per_buffer=chunk)

print("Audio stream opened.")

stream.start_stream()

while stream.is_active():
pass

stream.stop_stream()
stream.close()
p.terminate()

list_audio_devices()
select_audio_devices(0, 1)

In this example, the list_audio_devices function lists all available audio input and output devices. You can select the desired devices by providing their indices to the select_audio_devices function. It opens an audio stream using the specified input and output devices.

Example 6: Audio Input Validation

You can validate the audio input by checking for silence or specific audio patterns. Here's an example that checks for silence during audio recording:

import pyaudio
import numpy as np

def validate_audio_input(duration, threshold):
chunk = 1024
format = pyaudio.paInt16
channels = 1
rate = 44100

p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)

print("Recording started...")

is_silent = True
frames = []

for i in range(int(rate / chunk * duration)):
data = stream.read(chunk)
frames.append(data)

samples = np.frombuffer(data, dtype=np.int16)
if np.max(np.abs(samples)) > threshold:
is_silent = False

print("Recording finished.")

stream.stop_stream()
stream.close()
p.terminate()

if is_silent:
print("Audio input is silent.")
else:
print("Audio input is not silent.")

validate_audio_input(5, 100)

In this example, the validate_audio_input function records audio for a specified duration and checks if the maximum amplitude of the recorded samples exceeds a certain threshold. If the audio input is silent (below the threshold), it prints a corresponding message.

Example 7: Audio Output Volume Control

You can control the volume of audio output by adjusting the amplitude of the audio samples. Here's an example that demonstrates volume control during audio playback:

import pyaudio
import wave
import numpy as np

def adjust_volume(filename, volume_scale):
chunk = 1024

wf = wave.open(filename, 'rb')

p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)

print("Playing audio...")

data = wf.readframes(chunk)
while data:
samples = np.frombuffer(data, dtype=np.int16)
scaled_samples = np.multiply(samples, volume_scale).astype(np.int16)
stream.write(scaled_samples.tobytes())

data = wf.readframes(chunk)

print("Audio playback finished.")

stream.stop_stream()
stream.close()
p.terminate()

adjust_volume("output.wav", 0.5)

In this example, the adjust_volume function reads audio data from a WAV file and adjusts the volume by scaling the amplitude of the audio samples. The volume_scale parameter determines the volume level, where 1.0 represents the original volume, 0.5 represents half the volume, and so on.

Example 8: Audio Loopback

PyAudio allows you to create loopback streams to capture audio from the output and redirect it to the input. Here's an example that demonstrates audio loopback:

import pyaudio

def loopback_audio(duration):
chunk = 1024
format = pyaudio.paInt16
channels = 2
rate = 44100

p = pyaudio.PyAudio()
input_stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)

output_stream = p.open(format=format,
channels=channels,
rate=rate,
output=True)

print("Recording and playing audio simultaneously...")

frames = []

for _ in range(int(rate / chunk * duration)):
data = input_stream.read(chunk)
frames.append(data)
output_stream.write(data)

print("Audio loopback finished.")

input_stream.stop_stream()
input_stream.close()
output_stream.stop_stream()
output_stream.close()
p.terminate()

loopback_audio(5)

In this example, the loopback_audio function creates an input stream and an output stream. It simultaneously records audio from the input and plays it back through the output, creating an audio loopback. The audio data is stored in frames and written back to the output stream.

Example 9: Audio Streaming over Network

PyAudio can be used to stream audio over a network. Here's an example that demonstrates audio streaming using sockets:

import pyaudio
import socket

def stream_audio(host, port, duration):
chunk = 1024
format = pyaudio.paInt16
channels = 1
rate = 44100

p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)

print("Streaming audio...")

frames = []

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))

for _ in range(int(rate / chunk * duration)):
data = stream.read(chunk)
frames.append(data)
s.sendall(data)

print("Audio streaming finished.")

stream.stop_stream()
stream.close()
p.terminate()

stream_audio("localhost", 5000, 5)

In this example, the stream_audio function opens an audio stream and streams audio data over a network using a TCP socket. The audio data is read from the stream, stored in frames, and sent to the specified host and port using the socket's sendall method.

Example 10: Audio Analysis

PyAudio can be combined with other libraries to perform audio analysis tasks. Here's an example that uses the Librosa library to extract audio features:

import pyaudio
import librosa

def analyze_audio(filename):
chunk = 1024

wf = wave.open(filename, 'rb')

p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
input=True)

print("Analyzing audio...")

data = wf.readframes(chunk)
audio_data = []

while data:
audio_data.append(librosa.core.to_mono(librosa.core.load(data, sr=wf.getframerate())[0]))
data = wf.readframes(chunk)

# Perform audio analysis on audio_data

print("Audio analysis finished.")

stream.stop_stream()
stream.close()
p.terminate()

analyze_audio("output.wav")

In this example, the analyze_audio function reads audio data from a WAV file and uses the Librosa library to extract audio features. The audio data is converted to mono using Librosa's to_mono function and stored in a list for analysis. You can perform various audio analysis tasks on the audio_data list.

These examples cover a range of common use cases for the PyAudio library in Python. Experiment with these examples and explore the PyAudio documentation to further enhance your understanding of audio processing and manipulation using PyAudio.