πŸ”¬ librosa Quick-Start Cheat Sheet

πŸ”¬ librosa Quick-Start Cheat Sheet #

An epically overpowered, complete, precise, condensed, over-emojified, logically structured, clean, genius, practical, enticing, and easy-to-follow markdown quick-start cheat sheet for librosa.


πŸš€ Quick Start #

πŸ“¦ Installation #

Install librosa using your preferred package manager:

_# Using pip
_pip install librosa

_# Using conda
_conda install -c conda-forge librosa

▢️ Basic Usage: Beat Tracking #

librosa excels at analyzing audio. Here’s a quick taste:

_# Import the library
_import librosa

_# 1. Load an example audio file
_y, sr = librosa.load(librosa.ex('nutcracker'))

_# 2. Run the beat tracker
_tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)

print(f'Estimated tempo: {tempo:.2f} beats per minute')

_# 3. Convert frame numbers to timestamps
_beat_times = librosa.frames_to_time(beat_frames, sr=sr)

πŸ’Ύ Loading Audio #

librosa.load() is your gateway. It loads audio into a NumPy array.

_# Load an audio file
_# y = NumPy array (the audio time series)
_# sr = sampling rate (defaults to 22050 Hz)
_y, sr = librosa.load('path/to/your/audio.wav')

_# Load with a different sampling rate
_y_high_res, sr_high_res = librosa.load('path/to/audio.wav', sr=44100)

_# Load only the first 30 seconds
_y_short, sr_short = librosa.load('path/to/audio.wav', duration=30.0)

✨ Feature Extraction #

This is librosa’s core strength. Extract meaningful features from audio.

🎢 Spectral Features #

_# Mel-Frequency Cepstral Coefficients (MFCCs)
_mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)

_# Chroma Features (represents tonal content)
_chroma = librosa.feature.chroma_stft(y=y, sr=sr)

_# Mel Spectrogram
_mel_spec = librosa.feature.melspectrogram(y=y, sr=sr)

_# Spectral Centroid (indicates 'brightness')
_spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr)

_# Zero-Crossing Rate (correlates with percussive sounds)
_zcr = librosa.feature.zero_crossing_rate(y)

πŸ₯ Rhythm Features #

_# Estimate tempo (BPM)
_tempo, _ = librosa.beat.beat_track(y=y, sr=sr)

_# Get the tempogram (local tempo variations)
_tempogram = librosa.feature.tempogram(y=y, sr=sr)

πŸŽ›οΈ Audio Processing #

Manipulate the audio signal itself.

🎭 Harmonic-Percussive Source Separation (HPSS) #

Isolate the tonal (harmonic) and rhythmic (percussive) parts of the audio.

_# Separate the harmonic and percussive components
_y_harmonic, y_percussive = librosa.effects.hpss(y)

_# Now you can run feature extraction on each part separately!
_# e.g., Chroma on harmonics, beat tracking on percussives.
_chroma_harmonic = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr)
_tempo_percussive, _ = librosa.beat.beat_track(y=y_percussive, sr=sr)

πŸ”§ Utilities #

librosa is packed with helpful tools.

⏱️ Time & Frame Conversion #

Switch between time (seconds) and frame indices.

_# Convert time (in seconds) to frame index
_frame_index = librosa.time_to_frames(times_in_seconds, sr=sr)

_# Convert frame index to time (in seconds)
_time_in_seconds = librosa.frames_to_time(frame_indices, sr=sr)

πŸ”„ Feature Manipulation #

_# Delta Features (rate of change)
_mfcc_delta = librosa.feature.delta(mfccs)

_# Beat-Synchronous Features (aggregate features between beats)
_beat_mfccs = librosa.util.sync(mfccs, beat_frames, aggregate=np.median)

πŸ“Š Visualization #

While not covered in detail here, librosa.display is your tool for plotting waveforms, spectrograms, and features. It works beautifully with matplotlib.

import matplotlib.pyplot as plt
import librosa.display

_# Display a Mel Spectrogram
_fig, ax = plt.subplots()
_S_db = librosa.power_to_db(mel_spec, ref=np.max)
_img = librosa.display.specshow(S_db, x_axis='time',
                             y_axis='mel', sr=sr,
                             fmax=8000, ax=ax)
fig.colorbar(img, ax=ax, format='%+2.0f dB')
ax.set(title='Mel-frequency spectrogram')
plt.show()

πŸ’‘ Pro-Tips #

πŸ§ͺ Use Example Audio #

librosa comes with built-in audio examples for quick testing.

_# List all available examples
_print(librosa.list_examples())

_# Load a specific example
_y, sr = librosa.load(librosa.ex('trumpet'))