Course contentsShow
← Machine Learning and Deep Learning
Lesson 2444 of 3,53853. Speech and Audio ProcessingFree lesson

Audio Processing Libraries: librosa and torchaudio

Loading audio, computing spectrograms, and building preprocessing pipelines with standard Python libraries.

Audio Processing Libraries: librosa and torchaudio

You'll learn how to load audio files and compute spectrograms using Python's most popular audio processing libraries.

Core Idea

When working with audio data in machine learning, you need tools to load audio files, extract features like spectrograms and MFCCs, and build preprocessing pipelines. Two libraries dominate this space: librosa (NumPy-based, research-friendly) and torchaudio (PyTorch-native, deep learning-optimized).

Think of these libraries as your audio workshop toolkit—librosa is like a versatile Swiss Army knife with utilities for analysis and visualization, while torchaudio is a specialized power tool that integrates seamlessly with PyTorch's training pipelines.

Loading Audio with librosa

import librosa

# Load audio file (automatically resamples to 22050 Hz by default)
audio, sr = librosa.load('speech.wav', sr=16000)

# Compute mel-spectrogram
mel_spec = librosa.feature.melspectrogram(y=audio, sr=sr, 
                                          n_mels=128, hop_length=512)

# Convert to log scale (dB)
log_mel = librosa.power_to_db(mel_spec)

Loading Audio with torchaudio

import torchaudio

# Load audio (returns tensor and sample rate)
waveform, sr = torchaudio.load('speech.wav')

# Compute mel-spectrogram using transforms
mel_transform = torchaudio.transforms.MelSpectrogram(
    sample_rate=sr, n_mels=128, hop_length=512)
mel_spec = mel_transform(waveform)

# Convert to log scale
log_mel = torchaudio.transforms.AmplitudeToDB()(mel_spec)

Key Differences

  • librosa returns NumPy arrays; ideal for exploration and visualization
  • torchaudio returns PyTorch tensors; perfect for GPU-accelerated training pipelines
  • Both support STFT, mel-spectrograms, and MFCCs with similar parameters

Key Takeaway: Use librosa for audio analysis and experimentation, and torchaudio when building PyTorch-based deep learning pipelines—both provide essential tools for loading audio and computing spectral features.