Repository navigation
Suspend the audio context when no sound is playing - #152
Merged
Merged
Conversation
A running AudioContext keeps an output stream open and feeds silence to the audio server for as long as it lives. On Linux (WebKitGTK -> PipeWire) that cost ~40% CPU in PipeWire plus ~16% in WebKit's audio threads with the app idle. Count playing sources; once the last one ends and the reverb tail has passed, suspend the context. play() already resumes a suspended context. Fixes #150 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #150.
What
The shared
AudioContextinsrc/lib/sound.tswas created on the first UI sound and never suspended afterwards. A running context keeps an output stream open and feeds silence to the audio server indefinitely. On Linux, with the app idle, that measured about 40% CPU in PipeWire'sdata-loopplus about 16% in WebKit'swebaudioSrcthreads, with over 1,000 wakeups per second.Now the context is suspended once nothing is playing:
tone,bellpartials, scratch, crumple) goes throughtrack(), which counts sources that are still playing.onended, a 2 s timer covers the 1.6 s reverb tail, then callsac.suspend().play()already resumes a suspended context throughwake(), with thefresh()fallback, so the next sound plays as before. The resume happens inside the user gesture, so autoplay rules are still met.fresh()resets the count. Sources from a replaced context are ignored.The issue proposed a fixed 4 s idle delay. That would cut off the Pomodoro
playBell, which rings for about 5.5 s with reverb. Counting sources avoids that.Tests
src/lib/sound.test.tsuses a minimal fakeAudioContextand fake timers to check that:Three of the four fail on
main.npm testpasses (256 tests) andnpm run checkreports 0 errors.Not tested
pw-topshould show theopenthingsnode go idle.wake()/fresh()path already handles a suspended context.🤖 Generated with Claude Code