fix: eliminate full-volume transient on keepalive oscillator start#231
Open
DasAmpharos wants to merge 1 commit into
Open
fix: eliminate full-volume transient on keepalive oscillator start#231DasAmpharos wants to merge 1 commit into
DasAmpharos wants to merge 1 commit into
Conversation
The GainNode default gain is 1.0. Setting gain.value = 1e-5 is equivalent to setValueAtTime(1e-5, currentTime), which may not take effect until the next rendering quantum (~2.9ms). During that window the oscillator outputs at gain=1.0, producing an intermittent loud clip that users hear when the timer is started. Fix by scheduling the gain at t=0 (setValueAtTime(1e-5, 0)), which is always in the past. The Web Audio API spec guarantees this event is applied from the very first quantum the node renders, eliminating the race entirely. Fixes #229 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
🚀 Preview deploymenthttps://dasampharos.github.io/EonTimer-dev/pr-231/ Updated on every push to this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #229
When starting the timer, users occasionally heard a loud clipping sound. This happened ~50% of the time due to a race condition introduced in #227.
Root Cause
In
startKeepAlive(), the keepaliveGainNodeis created with a default gain of 1.0. The line:is equivalent to
setValueAtTime(1e-5, audioCtx.currentTime). The Web Audio API processes audio in 128-sample rendering quanta (~2.9ms at 44.1kHz). IfcurrentTimeis at or near a quantum boundary when this runs, the gain change may not take effect until the next quantum.Meanwhile,
keepAliveOsc.start()starts the oscillator immediately (at time 0). For that one-quantum window, the oscillator outputs at the default gain of 1.0 (full volume, 440 Hz), producing an audible transient that clips audio systems near their limits.The ~50% probability matches the chance that JS execution falls near a quantum boundary when
currentTimeis read.Fix
Schedule the gain at
t=0instead:t=0is always in the past relative to any rendering quantum. The Web Audio API spec guarantees that asetValueAtTimeevent att=0is applied from the very first quantum the node renders, eliminating the race entirely.