Last modified: Apr 16, 2026 By Alexander Williams
Python Audio Libraries: Play, Record, Process
Sound is a powerful medium. Python makes it accessible. This guide explores the best libraries for audio tasks. You will learn to play, record, and process sound.
We cover core libraries for different needs. From simple playback to complex analysis. Each section includes clear examples. You can follow along and test the code.
Why Use Python for Audio?
Python is a versatile language. It has a rich ecosystem of libraries. Audio programming can seem complex. Python libraries simplify the process.
You can automate tasks. Build music applications. Analyze sound data. The possibilities are vast. Python's readability helps beginners start quickly.
Core Python Audio Libraries
Different libraries serve different purposes. Some handle low-level device access. Others focus on high-level processing. We will look at the most popular ones.
1. PyAudio: For Playback and Recording
PyAudio provides bindings for PortAudio. This is a cross-platform audio I/O library. It lets you play and record audio in real-time.
It works with microphones and speakers. You can stream audio data. It is perfect for building voice applications.
First, install it using pip.
pip install pyaudio
Here is a simple example to play a WAV file.
import pyaudio
import wave
# Open the WAV file
file_path = "sound.wav"
wf = wave.open(file_path, 'rb')
# Create a PyAudio interface
p = pyaudio.PyAudio()
# Open a stream
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# Read data and play it
data = wf.readframes(1024)
while data:
stream.write(data)
data = wf.readframes(1024)
# Cleanup
stream.stop_stream()
stream.close()
p.terminate()
This code opens a WAV file. It reads chunks of audio data. Then it sends them to your speakers. The stream.write() method handles playback.
2. Librosa: For Audio Analysis
Librosa is a powerhouse for music and audio analysis. It is not for playback. It is for understanding audio content.
You can extract features like tempo and pitch. It is used in machine learning for audio. Install it with pip.
pip install librosa
Let's load an audio file and get its tempo.
import librosa
# Load an audio file
audio_path = "song.mp3"
y, sr = librosa.load(audio_path)
# Estimate the tempo (beats per minute)
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
print(f"Estimated tempo: {tempo:.2f} BPM")
Estimated tempo: 128.50 BPM
The librosa.load() function reads the file. It returns the audio time series and sample rate. The beat_track() function finds the beats.
3. Pydub: For Simple Audio Manipulation
Pydub offers a simple, high-level API. It is built on top of ffmpeg. You can slice, concatenate, and change volume easily.
It is great for quick edits. You do not need deep audio knowledge. First, install pydub and ffmpeg.
pip install pydub
You also need ffmpeg installed on your system. Here is how to trim an audio file.
from pydub import AudioSegment
# Load an audio file
audio = AudioSegment.from_file("input.mp3", format="mp3")
# Trim from 10 seconds to 30 seconds
# 1000 milliseconds = 1 second
start_time = 10 * 1000
end_time = 30 * 1000
trimmed_audio = audio[start_time:end_time]
# Export the trimmed audio
trimmed_audio.export("trimmed_output.mp3", format="mp3")
print("Audio trimmed and saved.")
This code loads an MP3. It then cuts a 20-second segment. The export() method saves the new file. Pydub makes such tasks very straightforward.
Choosing the Right Library
Your project goal decides the library. Need real-time input/output? Use PyAudio. Doing music analysis or machine learning? Librosa is your friend.
Need to quickly edit or convert files? Pydub is the easiest choice. Sometimes you may combine them. For instance, use PyAudio to record and librosa to analyze.
Common Tasks and Examples
Let's look at some practical tasks. These examples combine concepts.
Task: Record Audio and Save It
This uses PyAudio to record from a microphone.
import pyaudio
import wave
# Recording parameters
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
OUTPUT_FILENAME = "my_recording.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("Recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("Finished recording.")
stream.stop_stream()
stream.close()
p.terminate()
# Save to WAV file
wf = wave.open(OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
print(f"Saved to {OUTPUT_FILENAME}")
This script records 5 seconds of audio. It saves it as a WAV file. The stream.read() method captures audio data in chunks.
Task: Change Audio File Volume
This uses Pydub to adjust volume.
from pydub import AudioSegment
audio = AudioSegment.from_file("quiet_song.mp3", format="mp3")
# Increase volume by 10 dB
louder_audio = audio + 10
# Decrease volume by 5 dB
softer_audio = audio - 5
louder_audio.export("louder_song.mp3", format="mp3")
print("Volume adjusted and file saved.")
Pydub allows volume changes with simple arithmetic. Adding decibels makes it louder. Subtracting makes it quieter.
Conclusion
Python's audio libraries are powerful and diverse. PyAudio handles real-time audio I/O. Librosa excels at analysis and feature extraction. Pydub simplifies file manipulation.
Start with a simple task. Play a sound file. Then try recording your voice. Finally, experiment with analysis. The key is to practice.
These tools open doors to many applications. You can build voice assistants, music players, or data analysis pipelines. Python makes audio programming accessible to all developers.