Wacky Knobs

← Lab / Understanding audio DSP code, sorry it’s only math :)

· 9 min read

Chorus and flanger in code: LFO, interpolation, comb filter

← Delay in code

Chorus and flanger sound completely different: one is soft and wide, the other metallic, like a jet plane flying past. But in code they are the same effect, and a very small one. Take the delay from the last article, shorten it to a few milliseconds and let the read head keep swinging gently back and forth. Everything else is just different numbers.

A moving head changes the pitch

At the end of the last article you noticed a click when you change DELAY_MS suddenly, and that real pedals move the head gradually, so the pitch “stretches” a little. That stretch is the key. When the read head moves away from the write head, it reads the sound more slowly than it arrives, so the copy is slightly flat. When it moves closer, it reads faster, so the copy is slightly sharp. It is the same Doppler effect as a siren passing by.

If the head swings up and down, the copy keeps floating around the true pitch. The change is small: with a DEPTH of 3 ms and a RATE of 0.8 Hz, the copy drifts at most about 13 cents, an eighth of a semitone. On its own it sounds like a gentle vibrato. Mixed with the clean guitar, it sounds like two guitars that are never perfectly in tune with each other. That is chorus.

Chorus, flanger and vibrato: same code, different numbers

EffectDELAYDEPTHRATEFEEDBACKMIXWhat you hear
Chorus10–25 ms2–6 ms0.3–1.5 Hz00.5two guitars, a wide, soft sound
Flanger0.3–3 ms1–5 ms0.1–1 Hz0.5–0.90.5a jet plane, a metallic “whoosh”
Vibrato1–3 ms1–3 ms4–7 Hz01only the copy: a pitch that wobbles

The only difference between chorus and flanger is the length of the delay. In a chorus the copy is late enough for the ear to hear it as a second guitar. In a flanger it is so close (under 3 ms) that the ear can no longer tell it from the original. Instead, the original and the copy add up and cancel out at certain frequencies, and you hear that as a tone color, not as a second guitar.

Vibrato is the simplest: MIX at 1, so you do not hear the original, only the floating copy. With no original there is nothing to mix with, so only the pitch change is left.

What to try in the simulator

  • Chorus on chords. Play “Chords” and switch between effect and bypass. The chord suddenly gets wider, as if two guitars were playing.
  • Flanger on noise. Pick “Flanger (jet plane)”. Noise has every frequency in it, so you hear most clearly how the notches travel up and down. On the right you see them move.
  • A frozen flanger. “Frozen (no LFO)” keeps d at 1 ms. The sound is still metallic, but nothing moves. That is not an effect, just a filter. The LFO makes the movement.
  • Feedback. On the flanger, raise FEEDBACK from 0 to 0.85. The notches get deeper and sharp peaks grow between them. That is the difference between a gentle and an “aggressive” flanger.
  • Vibrato. RATE around 5 Hz and MIX at 1. That is how a Boss VB-2 or the vibrato mode of a Uni-Vibe sounds, even though those use analog electronics. (Fun fact: the “vibrato” on old Fender amps is really tremolo: it changes the volume, not the pitch.)

The LFO: a wave that moves the head

An LFO (low frequency oscillator) is an oscillator too slow to be heard as a tone. Here it runs between 0.05 and 10 times a second. In code it is just a number, phase, that slowly goes from 0 to 1 and wraps back to 0, and its cosine gives a smooth wave. RATE says how fast the wave goes, DEPTH how far it moves the head, and DELAY where it starts.

The wave here is a sine, as in most chorus pedals. Some pedals use a triangle instead: the head then moves at a constant speed, so the pitch jumps between “a little sharp” and “a little flat” instead of changing smoothly. On a flanger you usually cannot hear it, but on a chorus you notice.

Interpolation: reading between two cells

In the delay, d was always a whole number: you read the cell 24,000 places back. Here d changes smoothly, so it is often something like 47.3 cells back. That cell does not exist. If you simply rounded to 47, the head would jump cell by cell and you would hear a quiet crackle, especially with a slow LFO.

The fix is linear interpolation. Read two neighboring cells, a and b, and take a value between them: 30% of the way from a to b if the remainder frac is 0.3. It is one line of code and one multiplication, and the crackle is gone. Better pedals use finer methods too (allpass or cubic interpolation), but the idea is the same.

The comb filter: why a flanger sounds like a jet

When you add a signal to a copy that is d late, some frequencies line up and get louder, and some arrive exactly out of phase and cancel. The cancelling happens where the copy is late by exactly half a period. The first dip (notch) is at 1 / (2 × d). At d = 1 ms that is 500 Hz, then the next ones at 1.5 kHz, 2.5 kHz and so on, evenly spaced. Drawn out, the response looks like a comb, which is where the name comes from.

A flanger is that comb, moving. As d grows, the notches crowd together and slide down towards the lows. As d shrinks, they spread out and climb. The ear hears that as a sound that “sucks in” and “blows out”, which is why noise sounds like a jet. Feedback puts the copy back into the buffer, so the same frequencies get boosted again and again and the comb’s peaks get sharper.

A chorus makes the same comb, but much denser: at 12 ms the notches are every 83 Hz. Notches that close together are not heard as a tone color but as a second guitar drifting slightly. That is why the line between chorus and flanger sits somewhere around 5 to 10 ms.

The code, line by line

  1. The LFO. lfo is a wave from 0 to 1, and phase moves on by RATE_HZ / sampleRate after every sample. That way the LFO makes RATE_HZ rounds per second.
  2. Where the head is. d is DELAY_MS when lfo is 0 and DELAY_MS + DEPTH_MS when it is 1, turned into samples.
  3. Whole part and the rest. i says which cell, frac how far we are between it and the next.
  4. Read between two cells. y is the moving copy, interpolated.
  5. Write the input plus feedback. Same as in the delay. For a chorus FEEDBACK is usually 0; for a flanger it is what gives the “edge”.
  6. Mix the clean signal and the copy. The notches are deepest when MIX is exactly 0.5, because then the original and the copy cancel best.

And inside a pedal?

The classic chorus and flanger pedals (Boss CE-2, MXR Flanger, Electro-Harmonix Electric Mistress) are analog, but they do exactly the same thing. Instead of a ring buffer they have a BBD chip, a row of capacitors the signal travels through, and instead of d the LFO changes the BBD’s clock. A faster clock means a shorter delay. The math is the same, only built out of capacitors.

For a digital pedal this is one of the cheapest effects. 50 ms of memory at 48 kHz is 2,400 samples, under 10 KB, which fits in any microcontroller. The work per sample is a few multiplications and one cosine (which in practice is read from a table). The Spin FV-1 even has a dedicated instruction, CHO, that reads the buffer at the place the LFO says, interpolated, in a single step. That is why almost every FV-1 pedal has a chorus or flanger mode.

Next step: tremolo, an effect where the LFO does not move the head but the volume →

More in Understanding audio DSP code, sorry it’s only math :)

← Lab