Search code examples
javascriptweb-audio-api

Javascript, Web Audio API, Oscillator.stop() has an annoying end tick noise


When I call the stop function of an oscillator, it ends the note with a glitchy disconnect noise.

Is there any workaround?


Solution

  • +Crack0dks has the right idea - you should add a gain node after the oscillator, set its value to 0 (this has a dezippering on the setter - i.e. it doesn't set gain to zero instantly, it ramps it very quickly) before calling stop.

    HOWEVER, the reason it's not working for you trivially is because then you need to make sure the oscillator source that's going through the gain node doesn't stop instantly too - you shouldn't call stop() or stop(0), you should add a bit of time to let the gain node ramp its audio down before cutting it off. Try "stop( audiocontext.currentTime + 0.01 )" as a starting point. You may want to play with that last value a bit - but that's ten milliseconds, which is a good starting point.