Librosa
Librosa is a Python module for audio and music processing, providing a wide array of tools for analysis, feature extraction, and manipulation of audio signals. The current version is 0.11.0, with regular updates that typically include bug fixes, performance improvements, and minor feature enhancements, aiming for stable releases every few months.
Warnings
- gotcha For Python 3.13 environments, `librosa` 0.11.0 may require manual installation of `standard-aifc` and `standard-sunau` packages (`pip install standard-aifc standard-sunau`) for full compatibility. This is not needed for Python 3.12 or earlier.
- gotcha Windows users on Python 3.13 may encounter problems with the optional `samplerate` backend package for sample rate conversion. This may lead to `librosa.load` or `librosa.resample` issues if `samplerate` is used.
- breaking Librosa 0.11.0 bumps the minimum required `matplotlib` version to 3.5.0. Using an older version of `matplotlib` can lead to import errors or unexpected display behavior.
- gotcha The `norm` argument in `librosa.feature.mfcc` had a name collision issue in earlier versions. This was fixed in 0.11.0, meaning the behavior of `norm='ortho'` or `norm='none'` might differ from previous versions.
- gotcha By default, `librosa.load` resamples audio to `sr=22050` Hz. If you need to preserve the original sample rate, always specify `sr=None`. Not doing so is a common source of unexpected behavior or data loss.
Install
-
pip install librosa
Imports
- librosa
import librosa
- librosa.display
import librosa.display
- librosa.feature
import librosa.feature
- librosa.effects
import librosa.effects
- librosa.util
import librosa.util
Quickstart
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
# Generate a dummy audio signal for demonstration (5 seconds of random noise)
sr = 22050 # sample rate
duration = 5 # seconds
y = np.random.randn(int(sr * duration))
# In a real application, you'd load an audio file:
# y, sr = librosa.load("path/to/your/audio.wav", sr=None) # Use sr=None to preserve original sample rate
# Compute Mel-frequency cepstral coefficients (MFCCs)
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
# Display the MFCCs
plt.figure(figsize=(10, 4))
librosa.display.specshow(mfccs, x_axis='time', sr=sr, cmap='viridis')
plt.colorbar()
plt.title('MFCC (Mel-frequency cepstral coefficients)')
plt.tight_layout()
# plt.show() # Uncomment to display the plot
print(f"MFCCs shape: {mfccs.shape}")