home ~ projects ~ socials

Web Audio API Tone Generator With Gain Control

This is a basic Tone Generator wrapped in a class. It uses the Oscillator node from the Web Audio API.

Notes

  • Some examples show using:

    gainNode.gain.value = #

    Doing that can cause popping/crackling sounds on every change. It's especially bad using an input range slider.

    The setTargetAtTime() show above prevents that. It's sent the target gain, a startTime, and a timeConstant. By setting the startTime to .currentTime() the change starts to take place immediately. The timeConstant defines an "exponential approach" to the target gain. Using a low value like "0.01" make it happen fast enough that it feels instant.

-- end of line --

References