Skip to content

Suspend the audio context when no sound is playing - #152

Merged
spuder merged 1 commit into
mainfrom
fix/suspend-idle-audio
Nov 17, 2026
Merged

spuder merged 1 commit into
mainfrom
fix/suspend-idle-audio

Conversation

@spuder

@spuder spuder commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Fixes #150.

What

The shared AudioContext in src/lib/sound.ts was 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's data-loop plus about 16% in WebKit's webaudioSrc threads, with over 1,000 wakeups per second.

Now the context is suspended once nothing is playing:

  • Every scheduled source (tone, bell partials, scratch, crumple) goes through track(), which counts sources that are still playing.
  • When the last source fires onended, a 2 s timer covers the 1.6 s reverb tail, then calls ac.suspend().
  • A new sound during the tail cancels the timer. If a sound starts while a suspend is already in flight, the context is resumed once the suspend settles.
  • play() already resumes a suspended context through wake(), with the fresh() 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.ts uses a minimal fake AudioContext and fake timers to check that:

  • the context suspends only after every source has ended and the tail has passed,
  • a new sound during the tail cancels the suspend,
  • the next sound resumes the same context instead of creating a new one,
  • nothing is created when sounds are muted.

Three of the four fail on main. npm test passes (256 tests) and npm run check reports 0 errors.

Not tested

  • I haven't run this in a real desktop build. To check by hand on Linux: play a sound, then within about 3 s pw-top should show the openthings node go idle.
  • The macOS and iOS shells should behave the same, because the existing wake() / fresh() path already handles a suspended context.

🤖 Generated with Claude Code

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>
@spuder
spuder enabled auto-merge November 17, 2026 23:15
@spuder
spuder merged commit 9020878 into main Nov 17, 2026
12 checks passed
@spuder spuder mentioned this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audio: AudioContext never suspends, keeping a silent stream running (constant CPU in PipeWire/WebKit)

1 participant