Wacky Knobs

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

· 8 min read

Tremolo in code: wave shape, smoothing and stereo pan

← Chorus and flanger in code

Tremolo is one of the oldest guitar effects. It was built into amps before pedals existed, and the DeArmond Trem-Trol from the 1940s is considered one of the first standalone effects of all. In code it is also the smallest. No memory and no delay. The same LFO from the last article does not move a read head here but the volume. The whole effect is one multiplication per sample.

Volume that waves

You multiply every sample by a number g. When g is 1 the sound passes as it is. At 0.4 it is quieter, and at 0 it is gone. Tremolo just lets g keep waving between 1 and something smaller:

g = 1 − DEPTH × lfo

lfo is a wave from 0 to 1, the same as in the chorus. DEPTH says how deep the volume drops. With DEPTH at 0.6 the volume goes from 1 to 0.4 and back. With DEPTH at 1 it falls all the way to silence, so the sound gets “chopped” into pieces. RATE says how many times a second that happens.

The wave shape: from a wave to a chop

Every tremolo pedal has RATE and DEPTH. The third knob you often see is the wave shape. The Boss TR-2, for example, has RATE, WAVE and DEPTH, and WAVE goes from a smooth wave to a square. The shape changes the character more than you would expect:

ShapeSHAPEWhat you hearWhere you meet it
Sine0a soft wave, the sound “breathes”old amps, surf, ambient
Rounded square0.3–0.6a clear pulse, but no sharp edgesmost pedals in the middle position
Square1on-off, a “helicopter”rhythmic chopping, the “How Soon Is Now” kind of sound

In code the shape takes one trick. The sine goes through tanh, the same curve we will later use for overdrive. The bigger k is, the harder the sine is squashed at the top and bottom, and the more it looks like a square. With SHAPE at 0, k is almost zero and the wave stays a clean sine.

What to try in the simulator

  • Old Fender amp. Play “Chords” and pick the first button. A soft wave around 5 Hz is the sound of surf music and old country records.
  • Square, no smoothing. SHAPE at 1 and SMOOTH at 0. Every jump in volume clicks. Then slowly raise SMOOTH to 2 or 3 ms: the clicks go away and the rhythm stays the same.
  • Chop. “Chop (helicopter)” is DEPTH 1 and almost a square. Try it on the riff: the notes get cut in rhythm.
  • Stereo pan. Put on headphones and turn on “Stereo (pan)”. The sound travels from left to right, because the right side gets the opposite wave.
  • Too fast. “Too fast: new tones” sets RATE to 35 Hz. It no longer sounds like tremolo but like a new, rough tone. Why is explained below.

Why a square wave clicks

A square wave jumps from 1 to 0 in a single sample. A change that sudden has every frequency in it at once, and the ear hears it as a click. The louder the guitar at the moment of the jump, the louder the click.

The fix is not to let g jump straight to the new value, but to follow it gently:

g = g + a × (target − g)

After every sample g covers part of the way to the target, say 1%. In a few milliseconds it is nearly there. That is the simplest digital filter, a one-pole lowpass, and it works just like the RC filter of a resistor and a capacitor in a pedal. The number a sets how slow it is: a SMOOTH_MS of 2 ms is enough to kill the clicks while the rhythm still sounds sharp.

Analog tremolo gets this for free. In an optical tremolo (Fender, more on that below) the volume is changed by a light-dependent resistor, and it reacts slowly. That is why the edges are never sharp and that tremolo sounds soft even when the wave is a square.

Too fast: when tremolo becomes a new tone

Below about 15 Hz the ear follows the change in volume and hears it as pulsing. Above that, something changes. Multiplying two waves creates new frequencies mathematically: a 440 Hz tone multiplied by a 35 Hz wave also gives 405 Hz and 475 Hz, next to the original. That is called amplitude modulation (AM), the same thing AM radio is named after.

At a slow RATE those new tones are so close to the original that they do not separate and you hear them as a wave. At a fast one they are far enough apart to hear as separate tones, and the sound gets rough and metallic. If you drop the original completely and keep only the new tones, that is a ring modulator. Same code, just without the one in 1 − DEPTH × lfo.

Stereo pan

If you give the left side 1 − DEPTH × lfo and the right side 1 − DEPTH × (1 − lfo), you get two tremolos in opposite phase. When the left is loud the right is quiet, and the other way round. The sum stays constant, so the overall volume does not wave; the sound travels from one side to the other instead. That is auto-pan, and it is what the stereo output of the Strymon Flint and similar pedals does.

RATE from the song’s tempo

Tremolo sounds best when it follows the rhythm. Pedals with tap tempo (you press the footswitch in time and the pedal measures the gap) work out RATE like this:

RATE_HZ = BPM / 60 × pulses per beat

TempoQuartersEighthsTripletsSixteenths
90 BPM1.5 Hz3 Hz4.5 Hz6 Hz
120 BPM2 Hz4 Hz6 Hz8 Hz
140 BPM2.33 Hz4.67 Hz7 Hz9.33 Hz

Why tremolo sounds quieter

When you switch tremolo on, the sound is quieter on average, because the volume only goes below 1, never above. With a sine the average volume is 1 − DEPTH / 2. At DEPTH 1 that is half, about 6 dB down. That is why many tremolo pedals also have a LEVEL knob, or boost the signal a little so you do not disappear in the band when you step on it.

The code, line by line

  1. The wave. s is a sine from -1 to 1, the same phase as in the chorus.
  2. The shape. tanh(k × s) / tanh(k) squashes the sine towards a square, and dividing by tanh(k) puts the peaks back at exactly 1. Then the wave is shifted into the range 0 to 1.
  3. The LFO moves on by RATE_HZ / sampleRate.
  4. The target. target is the volume the LFO asks for.
  5. Smoothing. g follows the target with step a. When SMOOTH_MS is 0, a is 1 and g jumps at once.
  6. The output is g × x. For the stereo pan the right side has its own g that follows the opposite wave.

And inside an amp or a pedal?

Old amps did tremolo in three ways, and each sounds a little different:

  • Optical. Fender blackface amps (Deluxe Reverb, Twin Reverb) have a small lamp and a light-dependent resistor sealed together, nicknamed the “roach” for its shape. The LFO turns the lamp on and off, and the resistor changes the volume. The resistor is slow, so the wave is rounded.
  • Bias. The Princeton Reverb changes the operating point (bias) of the output tubes. The tubes themselves get a little quieter and louder, so the wave is soft and a bit uneven, because the tone changes along with the volume.
  • Harmonic. Fender amps from the early sixties (brownface) split the signal into bass and treble and wave them in opposite phase. That is tremolo and a filter combined, and it is explained in the article on filters.

Analog tremolo pedals do the same: an op-amp oscillator makes the LFO, and the volume is changed by an LED with a light-dependent resistor (as in the Fender) or by a transistor working as a variable resistor. For a digital pedal tremolo is the cheapest effect there is. It needs no memory, just one sine (from a table) and one multiplication per sample. The Spin FV-1 has two sine LFOs built in, so tremolo is usually added as an extra mode next to chorus or reverb.

Next step: overdrive in code, the same tanh curve we used here to squash the LFO, applied to the sound itself →

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

← Lab