Configure FFmpeg Binaural Filter Head and Mic Positions

This article provides a straightforward guide on how to configure the head model parameters and virtual speaker/microphone positions using FFmpeg’s spatial audio filters. You will learn how to load custom Head-Related Transfer Function (HRTF) files, adjust head radius dimensions, and define precise coordinates for virtual audio sources to achieve an immersive 3D binaural audio experience.


The FFmpeg sofalizer and headphone filters are the primary tools used to create binaural audio from multi-channel inputs. To customize the simulation, you must configure two main components: the head model (which dictates how sound waves interact with the shape of the head and ears) and the microphone/speaker positions (which define where the virtual sound sources are placed in the 3D space around the listener).

1. Configuring the Head Model

The head model in FFmpeg is configured using Spatially Oriented Format for Acoustics (SOFA) files and head dimension parameters.

Loading a Custom Head Model (HRTF)

By default, FFmpeg uses a utility default HRTF. To use a personalized or more accurate head model, you must download a .sofa file (such as those from the CIPIC or MIT HRTF databases) and reference it using the sofa option:

ffmpeg -i input.wav -af "sofalizer=sofa=/path/to/head_model.sofa" output.wav

Adjusting Head Radius

You can scale the virtual head model to match different head sizes. The default head radius is typically set to 0.0875 meters (8.75 cm). To adjust the head radius, use the radius option:

ffmpeg -i input.wav -af "sofalizer=sofa=/path/to/head_model.sofa:radius=0.095" output.wav

Note: Increasing the radius simulates a larger head, which increases the Interaural Time Difference (ITD) between the left and right ears.


2. Configuring Virtual Microphone and Speaker Positions

To position the audio channels around the listener’s head, you must define the coordinates for each virtual speaker (microphone source). FFmpeg uses a coordinate system based on: * Azimuth: The horizontal angle in degrees around the listener (0 is directly in front, 90 is to the right, 180 is directly behind, and 270/-90 is to the left). * Elevation: The vertical angle in degrees (-90 is directly below, 0 is at eye level, 90 is directly above). * Distance: The distance from the center of the head in meters.

Using the speakers Parameter

The speakers parameter allows you to manually map input channels to custom angles and elevations. The syntax follows a Channel_Name Azimuth Elevation|... pattern:

ffmpeg -i 5.1_input.wav -af "sofalizer=sofa=/path/to/head_model.sofa:speakers=FL 30 0|FR -30 0|FC 0 0|LFE 0 -10|BL 135 0|BR -135 0" output.wav

In this setup: * FL (Front Left) is placed 30 degrees to the left at eye level (0 elevation). * FR (Front Right) is placed 30 degrees to the right at eye level. * BL (Back Left) is placed 135 degrees to the left, slightly behind the head.


3. Alternative: Using the headphone Filter

If you do not want to use the SOFA-based sofalizer filter, you can use the headphone filter. This filter map-processes spatial positions using impulse response coefficient maps.

To configure custom microphone positions for a 5.1 layout using the headphone filter, map the channels to spatial positions using the map argument:

ffmpeg -i input_51.wav -i hrir_mics.wav -filter_complex "[0:a][1:a]headphone=map=FL|FR|FC|LFE|BL|BR[out]" -map "[out]" output.wav

In this command, hrir_mics.wav acts as the spatial audio map containing the impulse responses for the corresponding microphone positions specified in the map string.