Skip to content

perf(core): open the preview's audio context when a clip with sound plays - #7658

Merged
miguel-heygen merged 1 commit into
mainfrom
dperf/lazy-audio-context
Nov 13, 2026
Merged

miguel-heygen merged 1 commit into
mainfrom
dperf/lazy-audio-context

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

What

The preview runtime opens its audio context when a clip with sound first plays, not each time a page loads. A page with no sound never opens one.

Why

Opening an AudioContext opens the audio device on the page's own thread. On Windows that can take seconds, and every preview load paid it, so a window that reloaded its preview while files changed froze again and again with nothing playing. The runtime built the context in init() for every page, sound or not.

Related work

Refs the Windows preview freezes; no open issue here.

How

WebAudioTransport.open() builds the context synchronously and returns whether it worked; init() now calls it. In init.ts play and a scene joining a playing film call openWebAudio() first, and only when the page has clips with sound; a rate change only reschedules if the context was already opened. A failed open is remembered, so a browser with no Web Audio is not retried on every play. Opening at play also means the first play already has its context, where the old load-time open was ready only a microtask later.

What I measured

  • init.webAudioLazy.test.ts: 3 of 3. A page with sound that only loads builds 0 contexts; a page with no sound that plays builds 0; a page with sound builds exactly 1 however often it plays.
  • src/runtime whole directory on a Linux box: 138 files, 3,181 tests, all pass (one mock in init.swapScenes.test.ts now stubs open instead of init).
  • fails without the fix: packages/core/src/runtime/init.webAudioLazy.test.ts "builds none while a page with sound only loads" fails on main with expected 1 to be +0, and "builds none when a page with no sound plays" fails the same way.

What I did NOT exercise

  • A real Windows audio device: the unit tests use a counting stand-in for AudioContext, so how many seconds the first play now waits on a slow device is unmeasured. The wait moves to the first play of a page with sound; it is not removed.
  • A real browser play with sound after the change.

Test plan

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what

@github-actions

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 92 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

Unstable (1)

  • seqab-none-pct-r0-root-z100: tracking 0.05, pressJump 0, drop 0, reload 0, render 0.03, renderKey -, undo true, teleport true / tracking 0.05, pressJump 0, drop 0, reload 0, render 0.03, renderKey -, undo true, teleport true / tracking 0.05, pressJump 0, drop 0, reload 0, render 0.03, renderKey -, undo true, teleport true

@heygengenesis heygengenesis Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at c8198641.

The "fails without the fix" line checks out. I ran packages/core/src/runtime/init.webAudioLazy.test.ts with init.ts and webAudioTransport.ts reverted to the parent commit. "builds none while a page with sound only loads" and "builds none when a page with no sound plays" both fail with expected 1 to be +0. At this head, that file and init.swapScenes.test.ts pass (173/173).

Correctness:

  • open() is idempotent (if (this._ctx) return true), and webAudioOpened ??= remembers a failed open, so no-Web-Audio browsers aren't retried on every play.
  • Every call into the transport before the context opens takes the path the old code already took when init() failed or hadn't resolved yet. That covers setVolume/setMuted/setGroupMuted, metering, ownsClock/context in the clock source, and decode with _ctx null. Volume, mute and metering are applied at open (applyMasterGain, attachMasterTap), so no state is lost.
  • The three scheduling entry points (play, a scene joining a playing film, rate change) are gated correctly. The rate-change path uses webAudioOpened === true rather than calling openWebAudio(). The PR description says it opens there, but checking state is the right behaviour: if nothing was opened, nothing is scheduled to reschedule.
  • Autoplay: before this PR the context was also created outside a user gesture, at load, so creating it at play doesn't change behaviour.

CI is green on this head: CI, Windows render verification, regression, preview-regression, Player perf, Safari perf, Safari audio, and CodeQL.

Non-blocking:

  • As the PR says, the device-open cost now moves to the first play of a page with sound, and nobody has measured it on a real Windows device. Worth checking the first Windows stall reports after this ships (PRINFRA-6303).
  • If the page had no sound when it started playing, audio added later only routes through Web Audio after the next play or a scene join. This is an edge case, and native playback covers it in the meantime.

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 13, 2026
Merged via the queue into main with commit ccdcf67 Nov 13, 2026
144 checks passed
@miguel-heygen
miguel-heygen deleted the dperf/lazy-audio-context branch November 13, 2026 19:15
SanHsien added a commit to SanHsien/hyperframes that referenced this pull request Dec 3, 2026
…gen-com#7658, ccdcf67)

Adopts upstream heygen-com#7658: a paused preview or a silent composition no longer
opens the Web Audio context. Adds regression tests for early opening and a
CLI docs note.

Implemented by Codex (gpt-6.2-sol); committed by the reviewer because the
Codex sandbox cannot write the worktree index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SanHsien added a commit to SanHsien/hyperframes that referenced this pull request Dec 3, 2026
Records the remaining batch 6 decisions: heygen-com#7658 adopted (7164b5a),
heygen-com#7684 adopted, heygen-com#7659 and heygen-com#7679 deferred with revisit conditions, and the
heygen-com#7655/heygen-com#7664 summary. Updates REVIEW.md notes and ROADMAP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SanHsien added a commit to SanHsien/hyperframes that referenced this pull request Dec 3, 2026
…en-com#7658, heygen-com#7673)

- core: create the preview AudioContext before starting the playback clock,
  so a slow context start no longer drops the opening audio (heygen-com#7658).
- cli: `check --samples` accepts 1-10,000; larger values are usage errors
  instead of a RangeError (heygen-com#7673; upstream also accepts 1e10).
- docs: preview audio startup, sample limit, REVIEW SHA for heygen-com#7684.

Upstream: ccdcf67, 564a597, 92816ed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant