Use FFmpeg aevalsrc Filter to Generate Custom Audio

This article provides a practical guide on using FFmpeg’s audio evaluation source filter (aevalsrc) to generate custom audio waveforms. You will learn the basic syntax of the filter, how to construct mathematical expressions for standard wave shapes—such as sine, square, and sawtooth waves—and how to combine these expressions to synthesize unique, custom audio signals directly from the command line.

Understanding the aevalsrc Filter

In FFmpeg, the aevalsrc filter (audio evaluation source) generates audio signals by evaluating mathematical expressions. It operates within the Libavfilter virtual input device (-f lavfi), allowing you to define the amplitude of an audio channel as a function of time.

The most important variable in these expressions is t, which represents the current time in seconds.

The basic command syntax is:

ffmpeg -f lavfi -i aevalsrc="expression" -t duration output.wav

Generating Standard Waveforms

You can generate different types of audio waves by applying mathematical formulas to the time variable t. In these examples, we will generate a standard A440 pitch (440 Hz).

1. Sine Wave

A sine wave produces a clean, smooth, fundamental tone. The mathematical formula is sin(2 * PI * frequency * t).

ffmpeg -f lavfi -i aevalsrc="sin(440*2*PI*t)" -t 5 sine_440.wav

2. Square Wave

A square wave contains only odd harmonics, giving it a hollow, buzzy sound. You can generate it by taking the sign (sgn) of a sine wave.

ffmpeg -f lavfi -i aevalsrc="sgn(sin(440*2*PI*t))" -t 5 square_440.wav

3. Sawtooth Wave

A sawtooth wave contains both even and odd harmonics, producing a bright, harsh sound. It rises linearly and then drops sharply. This is achieved using the modulo/fractional part of the frequency over time.

ffmpeg -f lavfi -i aevalsrc="2*(t*440-floor(t*440))-1" -t 5 sawtooth_440.wav

Creating Custom Audio Waves

By combining different waves and mathematical functions, you can synthesize complex custom sounds.

Creating Chords (Additive Synthesis)

To play multiple frequencies simultaneously, add the wave expressions together. To prevent digital clipping, scale the volume of each wave so the combined amplitude does not exceed 1.0.

The following command generates a major triad chord (C4, E4, and G4) by mixing three sine waves together at reduced volumes:

ffmpeg -f lavfi -i aevalsrc="0.33*sin(261.63*2*PI*t) + 0.33*sin(329.63*2*PI*t) + 0.33*sin(392.00*2*PI*t)" -t 5 chord.wav

Frequency Modulation (FM Synthesis)

You can modulate the frequency of one wave using another wave. This technique creates complex, metallic, or sci-fi sound effects.

In this example, a 220 Hz carrier wave is modulated by a 5 Hz modulator wave:

ffmpeg -f lavfi -i aevalsrc="sin(2*PI*220*t + sin(2*PI*5*t)*10)" -t 5 fm_synth.wav

Generating Stereo Audio

You can generate distinct custom waves for the left and right channels by separating your expressions with a colon (:).

The following command plays a 440 Hz sine wave in the left channel and a 444 Hz sine wave in the right channel, creating a binaural beat effect:

ffmpeg -f lavfi -i aevalsrc="sin(440*2*PI*t):sin(444*2*PI*t)" -t 5 binaural.wav

Specifying Sample Rate and Channel Layout

By default, aevalsrc outputs audio at a sample rate of 44,100 Hz. You can explicitly set the sample rate using the s parameter and define the channel layout using the c parameter.

ffmpeg -f lavfi -i aevalsrc="sin(440*2*PI*t):s=48000:c=stereo" -t 5 output_48k.wav