sawtooth oscillator

The brightest, buzziest of the basic tones, full of harmonics, and the starting point for most synthesizer strings, brass, basses and leads.

The sawtooth oscillator node, at 220 Hz

In the menu: sources, then oscillators.

Inputs

Each setting has a knob, which disappears when something is connected to its input.

Outputs

What it does

A sawtooth wave rises steadily, then drops suddenly, then rises again, like the teeth of a saw. That sudden drop, once every cycle, fills it with harmonics: every whole-number multiple of the note's pitch is in there, each a little weaker than the one before. It sounds bright, buzzy and brassy, the most "electronic" of the basic waves on its own, but also the richest raw material: a bowed string and a brass instrument both make waves much like it.

A sawtooth wave at 220 Hz on a scope
Its spectrum: every harmonic, each a little weaker than the last

Added together, its harmonics build up the ramp and the drop. With only a few, the wave is a wobbly approximation; with more, it gets sharper:

A sawtooth built up from 1, 2, 4 and 32 of its harmonics
The four waveforms, and the harmonics in each

Listen: sine, triangle, square and sawtooth, each playing the same A (8 seconds)


Listen: a phrase on a sawtooth wave (5 seconds)

Starting bright, and taking away

Because it has every harmonic, a sawtooth is the usual place to start a sound and then shape it with a filter, taking away the harmonics you don't want. A low pass filter turns it from a buzz into something warm and muted; an envelope on the filter's cutoff makes each note start bright and then mellow, the sound of a synth brass stab or a plucked bass. This way of making sounds, starting rich and filtering, is called subtractive synthesis, and the sawtooth is its workhorse. The ladder filter page and the adsr page have examples.

Clean all the way up

Making a sawtooth on a computer is harder than it looks. Its sudden drop has harmonics going on forever, but a computer making 44,100 samples a second can only play pitches up to half that, 22,050 Hz. Harmonics above that don't just disappear; they fold back down as false, out-of-tune pitches, called aliases, that sound harsh, gritty and whistling, and get worse the higher the note.

Top: a high sawtooth worked out naively, full of aliases; below: the same note from this oscillator, with only its true harmonics

The sawtooth oscillator avoids this: it only ever plays harmonics that fit, dropping the highest ones as a note goes higher. This synth sweeps a sawtooth from 60 Hz up to 8,000 Hz and back, into a spectrogram, which shows the harmonics over time as bright lines. They all move together, in parallel, as true harmonics should, with none sliding the wrong way:

A triangle lfo sweeps a sawtooth from 60 Hz to 8 kHz, into a spectrogram

Watch and listen: the sweep (22 seconds, WebM video)


When you might use it

For slow ramps, to sweep something up and then snap it back, use a sawtooth lfo; for its phase, as a steady 0-to-1 ramp to drive a sine shaper or a factor power, connect to the phase output.

For music nerds

All the harmonics

The sawtooth has every harmonic at a level of 1/k: the second at half the fundamental (−6 dB), the third a third (−9.5 dB), and so on, falling at 6 dB per octave. That's close to the spectrum of a bowed string, whose Helmholtz motion makes a sawtooth-like wave at the bridge, and of brass instruments when played loudly, which is why both are the classic subtractive patches on a sawtooth. Because all the harmonics are there, every filter setting finds something to act on: a sawtooth shows a filter's character more clearly than any other wave.

Detuning and doubling

Two sawtooths a few cents apart beat against each other, every harmonic at a different rate (the kth harmonic beats k times as fast as the fundamental), so the tone gains a slow, rich swirl. This is the start of the "supersaw"; the unison oscillator does it with up to seven, and spreads them evenly.

Band-limiting and brightness

To keep aliases out, the oscillator plays no harmonic above 26 kHz. That's above half the sample rate, but a harmonic between 22.05 kHz and 26 kHz folds back to between 18.1 kHz and 22.05 kHz, above what nearly anyone can hear, and allowing it keeps more of the true harmonics below it: everything up to 11 kHz at least, and usually much more. As a note rises, the oscillator drops its top octave of harmonics in steps; just below each step, it fades them out over a quarter of an octave, so a note sliding up or down (a glide, vibrato, or a sweep like the one above) changes smoothly, without a click. This graph shows, for every note, the highest harmonic played at full level, and at all:

The highest harmonic played, at full level and at all, for notes from 20 Hz to 20 kHz

At every pitch, then, a sawtooth here is complete up to somewhere between 11 and 22 kHz. If you'd like a softer, more rounded top, a low pass filter at 10 to 12 kHz gives it.

For math nerds

The series

The oscillator keeps a phase φ like the others (see the sine oscillator), and plays

2φ − 1 = −(2/π) Σ sin 2πkφ / k

truncated to the harmonics that fit. A reset sets φ to ½, where the ramp crosses zero.

The tables

Rather than adding up harmonics each sample, the oscillator reads tables made when Sonido starts (in src/wavetables.lisp). One cycle of the ideal sawtooth is sampled at 2,048 points and taken through an FFT; then, for each of eleven levels L = 0 … 10, the spectrum is cut to the harmonics k ≤ 1024/2^L (and below 1,024) and transformed back into a table of 2,048 samples. Level 0 keeps 1,023 harmonics; level 10 just the fundamental. Each table has a copy of its first sample at its end, so reading between samples needs no wrapping, and is read by linear interpolation between the two samples around the phase.

The level is chosen from the phase step, the frequency f over the sample rate. Level L keeps harmonics up to (1024/2^L) f, and has to keep them below 26 kHz:

L = ⌈log₂ ρ⌉, ρ = 1024 f / 26000 Hz; level L keeps harmonics k ≤ 1024 / 2^L

so level L serves notes with ρ between 2^(L−1) and 2^L; at the bottom of that octave its highest harmonic is at 13 kHz, and at the top, 26 kHz.

The fade between levels

Switching straight from one level to the next would drop the top half of the level's harmonics in one sample: a step in the waveform of up to 0.44 of its peak, depending on where in the cycle it happens, which you hear as a click every time a gliding note crosses one of the boundaries (at about 25, 51, 102, 203, 406, 813, 1625, 3250, 6500 and 13,000 Hz). Instead, over the last quarter octave below each boundary, the oscillator crossfades to the next level:

y = (1 − b) T_L(φ) + b T_(L+1)(φ), b = clamp(4 (log₂ ρ − L) + 1, 0, 1)

b reaches 1 exactly at the boundary, where the next level takes over with b = 0, so the output is a continuous function of the frequency. Since the next level keeps a subset of this one's harmonics, the crossfade simply scales the top octave's harmonics by 1 − b; nothing is added that could alias. It costs a second table read during the fade, and nothing otherwise. The level and the fade are worked out again only when the frequency changes.

Errors

Linear interpolation between 2,048 samples is not perfect. It acts as a gentle low pass on the table, scaling harmonic k by about sinc²(k/2048): −7.8 dB at the very top of level 0, which only notes below about 25 Hz play (where those harmonics are above 20 kHz), at most about −0.45 dB for the highest harmonics of notes above 50 Hz, and less than 0.12 dB for notes above about 100 Hz. It also adds faint images of the harmonics, far below the band-limited signal at any normal pitch. The phase is kept in double precision, so there's no audible pitch error or drift.

Back to Sources: oscillators
Node reference
Contents