Real-Time Audio and Music Analysis via Free Python Library
Aubio is an Open Source Python Audio Processing Library Used by Software Developers for Pitching Detection, Beat tracking, MFCC Extraction, and Real-Time Audio Processing.
Modern audio applications—such as music production tools, audio analysis platforms, and real-time sound processing systems—require efficient ways to analyze sound signals. One popular open-source solution for this purpose is Aubio, a lightweight yet powerful library designed to extract meaningful information from audio signals. Aubio is an open-source library designed to extract annotations and musical features from audio signals. It analyzes audio streams and identifies events such as note onsets, pitch, beats, and tempo. It is written primarily in C for high performance and works across Linux, macOS, and Windows.
The library is very easy to handle and supports real-time audio processing. The library can detect musical events like when a drum hit occurs, the frequency of a musical note and the tempo or rhythm of a track. It can operate both offline (audio files) and in real-time audio streams, making it suitable for audio editors, digital instruments, and sound analysis tools. With support for pitch detection, beat tracking, onset detection, MFCC extraction, and real-time signal processing, it provides developers with the tools needed to build advanced audio applications.
Getting Started with Aubio
The easiest way to install Aubio is using the Python Package Index (PyPI). Please use the following command for complete installation.
Install using Aubio
pip install aubio Install Aubio from GitHub
go get -u github.com/aubio/aubio.git Onset Detection via Python
One of the most useful features of Aubio library is onset detection, which identifies when a musical note or sound event begins. Onset detection finds the precise moment a new sound or musical note begins. In music, an "onset" is the attack of a note — the instant a drum is struck, a guitar is plucked, or a voice begins a syllable. Aubio includes multiple onset detection algorithms for different use cases. The following example demonstrates, how to detect Onsets inside an audio file using Python code.
How to Detect Onsets in an Audio File via Python Library?
import aubio
import numpy as np
# Configuration
filename = "audio_sample.wav"
win_s = 512 # FFT window size
hop_s = 256 # Hop size (frames between analysis windows)
# Open the audio source
src = aubio.source(filename, hop_size=hop_s)
samplerate = src.samplerate
# Create the onset detector (using 'complex' method)
onset = aubio.onset("complex", win_s, hop_s, samplerate)
# Set detection threshold (0.0 to 1.0 — lower = more sensitive)
onset.set_threshold(0.3)
onsets = []
# Process audio frame by frame
while True:
samples, read = src() # Read one hop of audio
if onset(samples): # Returns True when onset detected
print(f"Onset at: {onset.get_last_s():.3f} seconds")
onsets.append(onset.get_last_s())
if read < hop_s: # End of file
break
print(f"\nTotal onsets detected: {len(onsets)}")
Pitch Detection Support
Pitch detection is another important capability of open source Aubio library. Pitch detection identifies the fundamental frequency (F0) of a sound — essentially, "what musical note is this?" It is the core of any tuner, melody extractor, or vocal analysis tool. Aubio supports multiple pitch detection algorithms such as yin, yinfft, mcomb, fcomb, and so on. Here is a simple example that demonstrates how to perform real-time Pitch detection inside Python applications.
How to Perform Real-Time Pitch Detection via Python?
import aubio
import numpy as np
samplerate = 44100
win_s = 1024
hop_s = 512
pitch_o = aubio.pitch("yin", win_s, hop_s, samplerate)
pitch_o.set_unit("Hz")
pitch_o.set_silence(-40)
samples = np.random.rand(hop_s).astype(np.float32)
pitch = pitch_o(samples)
print("Detected pitch:", pitch)
Tempo Tracking and Beat Detection via Python
The open source Aubio library can analyze rhythmic patterns and detect beats and tempo in music inside Python applications. Aubio can listen to audio and tap along — detecting the tempo in BPM (beats per minute) and pinpointing individual beat timestamps. This is invaluable for anything from DJ software to music synchronization. The library determines beat locations by analyzing spectral changes and rhythmic patterns in the audio signal.
Sound File Reading and Writing via Python
The python library Aubio wraps audio file I/O through its source and sink objects, supporting multiple formats (WAV, FLAC, OGG, MP3 via libav or ffmpeg) and handling sample rate conversion automatically. Here is a simple example that shows how software developers can perform audio file reading, processing, and writing using Python commands.
How to Read, Process, and Write Audio Files via Python?
import aubio
import numpy as np
input_file = "input.wav"
output_file = "output.wav"
target_samplerate = 22050 # Downsample to 22.05 kHz
hop_s = 512
# Open source — resamples automatically to target samplerate
src = aubio.source(input_file, samplerate=target_samplerate, hop_size=hop_s)
actual_sr = src.samplerate
num_channels = src.channels
# Open sink for writing
sink = aubio.sink(output_file, actual_sr, channels=1)
while True:
samples, read = src()
# Write mono (sink expects 1D array for mono output)
if num_channels > 1:
mono = samples.reshape(num_channels, -1).mean(axis=0)
else:
mono = samples
sink(mono[:read], read)
if read < hop_s:
break
del sink # Flushes and closes file
print(f"Written {output_file} at {actual_sr} Hz")