How to Use FFmpeg afir for Real-Time Room Correction
This article provides a practical guide on how to perform real-time
room correction on a live audio stream using the FFmpeg
afir filter. You will learn how to set up the command-line
interface, incorporate your room’s impulse response (IR) file, and
optimize the filter settings to ensure low-latency, real-time audio
playback.
Understanding the
afir Filter
The afir (FIR Filter Relation) filter in FFmpeg applies
a Finite Impulse Response (FIR) filter to an audio stream using a
secondary input as the frequency response curve. For room correction,
this secondary input is an Impulse Response (IR) WAV file, which
represents the acoustic characteristics of your physical listening
space. By convolving your live audio with the inverse of this IR, you
correct room resonances and frequency imbalances.
Prerequisites
Before running the command, you need: 1. An Impulse Response
(IR) File: A .wav file of your room’s impulse
response, typically generated using measurement software like Room EQ
Wizard (REW). 2. FFmpeg: Installed and configured with
support for your system’s audio interface (e.g., PulseAudio, ALSA, or
Jack on Linux; CoreAudio on macOS; DirectShow on Windows).
The Real-Time Command Structure
To process audio in real-time, you must capture the live system audio stream, pass it through the filter along with the IR file, and output it back to your physical playback device.
Below is the standard command structure using PulseAudio (Linux) as an example:
ffmpeg -f pulse -i default -i room_ir.wav -filter_complex "[0:a][1:a]afir=grms=1:maxp=1024[out]" -map "[out]" -f pulse defaultParameter Breakdown
-f pulse -i default: Captures the real-time audio stream from your default system input or monitor source.-i room_ir.wav: Loads your pre-recorded room impulse response file as the second input stream ([1:a]).-filter_complex: Initiates the filtergraph to handle multiple inputs.[0:a][1:a]: Feeds the live audio (input 0) and the IR file (input 1) into the filter.afir: Calls the FIR filter.grms=1: Automatically matches the output gain to the input level using RMS, preventing digital clipping.maxp=1024: Sets the maximum partition size for the FFT calculation. Lower values (like512or1024) reduce processing latency, which is critical for real-time audio stream alignment.
-map "[out]": Directs the processed audio to the output.-f pulse default: Outputs the corrected audio stream in real-time to your system’s default playback device.
Optimizing for Latency and CPU
To achieve the lowest possible latency during live playback, consider the following adjustments:
- Truncate the IR File: Ensure your
room_ir.wavfile is as short as possible (typically under 100–200 milliseconds). Longer IR files require more FFT points, which introduces audible delay. - Adjust Partition Sizes: Experiment with the
minpandmaxpparameters inside theafirfilter. For example,afir=grms=1:minp=512:maxp=512forces a small, fixed partition size to minimize buffer delay at the expense of slightly higher CPU usage. - Match Sample Rates: Ensure your live audio stream, your IR file, and your output hardware are all configured to the same sample rate (e.g., 44100 Hz or 48000 Hz) to avoid real-time resampling overhead.