The Audible Click in Synthesized Tones

If you've ever generated a pure tone directly in code and played it back, you've likely encountered a jarring click at the start, the end, or both. This isn't a glitch in your sine wave calculation; it's a well-understood audio artifact. The clean tone itself is mathematically pure, but its abrupt onset and decay create this audible imperfection. The solution lies in a fundamental audio synthesis technique: the envelope.

This explanation builds on the concept of generating a basic tone from scratch. We will examine the actual sample values that cause this click and then apply an envelope to achieve a smooth, artifact-free playback. All examples will use plain C++ with no external libraries, and the numerical data presented is derived from real code execution.

Understanding the Click's Origin

At its core, a synthesized tone is a sequence of numerical samples that trace a sine wave. When these samples are sent to a speaker, they dictate the physical movement of the speaker cone over time. A sample value of 0 typically represents the cone's neutral resting position. Positive values move it forward, and negative values pull it backward. A sine wave oscillates smoothly between these extremes, creating the perception of a continuous tone.

The click occurs because the wave doesn't always start or end at the zero-crossing point. Imagine the speaker cone is at its maximum forward position (a high positive sample value) when the tone abruptly stops. The cone is suddenly yanked back to its resting position without any deceleration. This rapid, un-damped movement creates a percussive impulse – the click. Similarly, if the tone begins with a high sample value when the cone is at rest, the sudden jump forward also causes a click.

Consider a simple sine wave that completes a full cycle over 100 samples. If the tone begins precisely at sample 0, which is the zero-crossing point, the transition is smooth. However, if the generation process starts at sample 50, where the sine wave is at its peak positive value, the speaker cone must instantly jump from rest to its maximum forward position. This instantaneous change in velocity is what the ear perceives as a click. The same issue arises at the end of the tone if the wave doesn't smoothly return to zero.

Visual representation of a sine wave starting abruptly at its peak positive value.

Introducing the Envelope

An audio envelope is essentially a control signal that modifies the amplitude (volume) of a sound over time. Think of it less like a volume knob that you twist up and down manually, and more like a programmed dimmer switch that follows a specific curve. This curve dictates how the sound's volume changes from its beginning to its end. The most common envelope shape is the ADSR (Attack, Decay, Sustain, Release) envelope, but for a simple tone, we primarily focus on the Attack and Release phases.

The Attack phase describes how the sound's amplitude increases from silence to its maximum level. A fast attack means the volume ramps up quickly, while a slow attack means it takes longer. For synthesized tones, we want a very short, but non-zero, attack time. This ensures the sound doesn't start at its full amplitude instantaneously, but rather ramps up smoothly from zero.

The Decay phase (often combined with Attack for simpler envelopes) is where the amplitude decreases from its peak to a sustain level. For a pure tone that should hold a constant volume, the sustain level is often the maximum amplitude itself, making the decay phase less critical, or it can be a slightly reduced level.

The Release phase is crucial for preventing clicks at the end of a sound. It defines how the amplitude decreases from its sustain level back to silence after the note is no longer being played. A non-zero release time allows the sound to fade out gradually, ensuring the speaker cone returns to its resting position smoothly, rather than snapping back.

Implementing an Envelope in Code

To implement an envelope, we modify the sample values generated for the sine wave. Instead of directly outputting the sine wave's amplitude, we multiply each sample by a corresponding amplitude value from our envelope curve. This amplitude value changes over time.

Let's consider a simple linear envelope for a tone that lasts N samples. The attack phase could last A samples, and the release phase could last R samples. The middle portion, N - A - R samples, would be at full amplitude (sustain).

  • Attack Phase (0 to A-1 samples): The amplitude multiplier increases linearly from 0 to 1. For sample i (where 0 <= i < A), the multiplier is i / A.
  • Sustain Phase (A to N-R-1 samples): The amplitude multiplier is 1.
  • Release Phase (N-R to N-1 samples): The amplitude multiplier decreases linearly from 1 to 0. For sample j (where N-R <= j < N), the multiplier is (N - j) / R.

The actual sample value output would be sine_wave_sample[i] * envelope_multiplier[i].

By applying this multiplicative factor, even if the sine wave starts at its peak, the initial sample value will be 0 (since 0 * peak_sine_value = 0). As the envelope multiplier increases, the volume ramps up. Similarly, at the end, the envelope multiplier will smoothly transition from 1 to 0, causing the tone to fade out gradually and preventing the abrupt stop that causes clicks.

The surprising detail here is not the complexity of the sine wave itself, but how a simple linear scaling factor applied over time can completely eliminate a noticeable audio artifact. This technique is foundational, akin to how a foundation bolt secures a skyscraper, ensuring stability where it matters most.

The Broader Impact

Envelopes are not just for simple tones. They are critical for shaping virtually any synthesized sound, from percussive hits that need a sharp attack and quick decay, to sustained synth pads that require a slow attack and long release. Understanding and implementing envelopes is a fundamental step for anyone serious about audio synthesis. It transforms raw, potentially clicky waveforms into polished, professional-sounding audio. If you are generating any form of synthesized sound, from simple beeps to complex musical instruments, you will need to implement an envelope to ensure a clean listening experience.