Elena' s AI Blog

Audio Signal Processing with Python's Librosa

05 Mar 2023 (updated: 17 Aug 2026) / 49 minutes to read

Elena Daehnhardt


Jasper AI-generated art, February 2023


If you click an affiliate link and subsequently make a purchase, I will earn a small commission at no additional cost (you pay nothing extra). This is important for promoting tools I like and supporting my blogging.

I thoroughly check the affiliated products' functionality and use them myself to ensure high-quality content for my readers. Thank you very much for motivating me to write.



TL;DR:
  • Use Librosa to extract audio features (MFCC, spectral features) from WAV files for ML tasks. Load with librosa.load(), extract features with librosa.feature functions—essential for genre/gender prediction.

Previous: Part 23 — Floating-point format and Mixed Precision in TensorFlow

Next: Part 25 — Bias-Variance Challenge

Librosa Python Tutorial: Introduction to Audio Signal Processing

Librosa is a Python library for music and audio analysis that loads audio files, extracts spectral features (MFCC, mel-spectrogram, chroma, spectral contrast), and applies effects such as pitch shifting and time stretching. This post is a practical, code-first Librosa Python tutorial covering audio feature extraction for machine learning workflows.

Are you ready to dive into audio processing with Python? A colleague recently sparked my interest in music-retrieval applications, and that sent me down the Librosa rabbit hole. This post is an introduction to Python’s Librosa library for extracting wave features commonly used in research and application tasks such as gender prediction, music genre prediction, and voice identification. Before tackling these complex tasks, though, we need to understand the basics of signal processing and how they relate to working with WAV files — so let’s start there.

How Digital Audio Signals Are Stored and Processed

What is an audio signal?

An audio signal is a representation of sound waves in the air. These sound waves are captured by a microphone and converted into an electrical signal, which can then be stored and manipulated digitally.

To store an audio signal digitally, the analogue electrical signal is first sampled at regular intervals, typically at 44,100 samples per second for CD-quality audio. Each sample is represented as a binary number with a certain bit depth, such as 16 bits. The higher the bit depth, the more accurately the analogue signal’s amplitude can be represented.

The binary numbers are then stored in a digital audio file format like WAV or MP3. The audio signal is typically compressed in these formats to reduce file size while maintaining acceptable audio quality. This compression can be lossless, meaning that no audio data is lost, or lossy, meaning that some audio data is discarded.

When the digital audio file is played back, the binary numbers are converted back into an analogue electrical signal by a digital-to-analogue converter, which can then be amplified and played through a speaker or headphones to produce sound waves in the air.

Audio file formats

Audio can be stored in files using different formats, depending on the application and the user’s requirements. Some of the most common formats used for storing audio in files include:

  • MP3: This compressed audio format is widely used for music playback and streaming. It offers high-quality audio with relatively small file sizes, making it a popular choice for storing and sharing music files.
  • WAV: This uncompressed audio format provides high-quality audio with no loss of fidelity. It is commonly used for recording and editing audio files, as well as for creating audio CDs.
  • AAC: This compressed audio format is similar to MP3 but offers better sound quality at lower bitrates. It is commonly used for streaming audio and video content.
  • FLAC: This lossless compressed audio format provides high-quality audio with no loss of fidelity. It is commonly used for storing and sharing high-resolution audio files.
  • OGG: This compressed audio format is commonly used for streaming audio and video content, and it offers high-quality audio with relatively small file sizes.
  • AIFF: This uncompressed audio format provides high-quality audio with no loss of fidelity. It is commonly used for recording and editing audio files on Apple computers.

The choice of format depends on factors such as the audio quality, the file size, and the compatibility with the playback device or software.

Python libraries for audio processing

There are several Python libraries for audio processing, each with its features and capabilities. Here are some of the most popular and widely used libraries for audio processing in Python:

  1. NumPy is a fundamental library in Python for numerical computing. It provides the ability to perform various numerical operations on arrays, such as filtering, resampling, and FFT (Fast Fourier Transform).
  2. SciPy is built on top of NumPy and provides additional scientific and technical computing functionalities, including digital signal processing (DSP), Fourier analysis, and filter design.
  3. Librosa is a library for analysing and processing audio signals. It includes functionality for feature extraction, beat tracking, pitch estimation, and more.
  4. Pydub is a simple and easy-to-use library for working with audio files in Python. It allows you to load, manipulate, and save various audio file formats, including MP3, WAV, and AIFF.
  5. Soundfile is a library for reading and writing sound files. It supports various file formats, such as WAV, FLAC, and OGG, and provides a simple and straightforward interface for working with audio data.
  6. PyAudio provides a Python interface to the PortAudio library, a cross-platform library for audio input and output. It allows you to record and playback audio in real-time and supports various input and output devices.
  7. FFMpeg: FFMpeg is a command-line tool for manipulating video and audio files. Several Python bindings for FFMpeg, including moviepy and ffmpeg-python, provide a simple and easy-to-use interface for working with FFMpeg from Python.

Which library is best depends on your specific use case and project requirements.

In this post, I focus on using Librosa, providing a great starting point for audio processing in Python. I will also use wave, sounddevice, soundfile and, of course, NumPy!

Installing Librosa and Audio Processing Libraries in Python

🔒 Subscribe to keep reading.

Working with WAV Files in Python

🔒 Subscribe to keep reading.

Librosa Use Cases: Loading and Analysing Audio Files

🔒 Subscribe to keep reading.

Final thoughts

🔒 Subscribe to keep reading.

Soundtracks

🔒 Subscribe to keep reading.

References

🔒 Subscribe to keep reading.

Subscribe to unlock the full article ❤️

The form below signs you up for the newsletter. It does not log you into the app — log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2023) 'Audio Signal Processing with Python's Librosa', daehnhardt.com, 05 March 2023. Available at: https://daehnhardt.com/blog/2023/03/05/python-audio-signal-processing-with-librosa/
All Posts