Introduction
Librosa is the standard Python library for music and audio analysis. It provides the building blocks for music information retrieval (MIR) research and audio machine learning, from loading files and computing spectrograms to extracting high-level features like tempo, key, and beat positions.
What Librosa Does
- Loads audio files in various formats and resamples to target sample rates
- Computes spectral representations: STFT, mel spectrograms, constant-Q transform, chromagrams
- Extracts audio features: MFCCs, spectral centroid, zero-crossing rate, tonnetz
- Performs beat tracking, onset detection, and tempo estimation
- Provides time-stretching, pitch-shifting, and harmonic-percussive source separation
Architecture Overview
Librosa is built on NumPy and SciPy, using array operations for efficient signal processing. Audio loading delegates to soundfile (libsndfile) with optional audioread fallback for MP3/AAC. The feature extraction pipeline follows a modular design where each function takes raw audio or spectrograms and returns NumPy arrays. Caching via joblib avoids redundant computation in interactive analysis. The library maintains a functional API style without stateful objects.
Self-Hosting & Configuration
- Install via pip with optional dependencies for MP3 support (
pip install librosa[ffmpeg]) - No configuration files; all parameters are function arguments with sensible defaults
- Default sample rate is 22050 Hz (sufficient for most MIR tasks); override with
srparameter - Use
librosa.displaywith matplotlib for spectrogram visualization - Works in Jupyter notebooks for interactive exploration
Key Features
- Comprehensive feature extraction covering spectral, rhythmic, and tonal descriptors
- Time-frequency representations with customizable hop length and window functions
- Beat-synchronous feature aggregation for music structure analysis
- Harmonic-percussive separation for isolating tonal and rhythmic components
- Extensive documentation with tutorial notebooks and API reference
Comparison with Similar Tools
- torchaudio — GPU-accelerated, PyTorch integration; librosa is simpler for research and prototyping
- Essentia — C++ based with more algorithms; librosa is pure Python and easier to modify
- madmom — focused on beat/onset detection; librosa covers broader feature extraction
- pyAudioAnalysis — higher-level classification focus; librosa provides lower-level building blocks
FAQ
Q: Is librosa suitable for real-time audio processing? A: No, it is designed for offline analysis. For real-time, use libraries like pyaudio or sounddevice.
Q: Can librosa handle very long audio files?
A: Yes, use the offset and duration parameters in librosa.load() to process segments, or stream with soundfile.
Q: What audio formats does librosa support? A: WAV and FLAC natively via soundfile. MP3, AAC, and others with ffmpeg or audioread installed.
Q: How do I visualize spectrograms?
A: Use librosa.display.specshow() which integrates with matplotlib for publication-quality plots.