Skip to content

Perf: Move resampling/VAD off main thread (AudioEngine onmessage spikes) #37

Description

@ysdede

Problem

Audio chunk handling on the main thread is still doing resampling + VAD + buffer updates in workletNode.port.onmessage. In the trace this handler spikes to ~20ms, which can drop frames even before any long-run effects.

Evidence

  • Trace: workletNode.port.onmessage (AudioEngine) max ~20ms, avg ~0.7ms across ~184 calls.
  • Main-thread Handler includes resampleLinear, ring buffer writes, VAD, segment processing, UI updates.

Proposed change

  • Move resampling and/or energy/VAD computation into the AudioWorklet or a dedicated worker.
  • Make the main-thread handler minimal: enqueue chunk, update counters, and return.
  • Reuse scratch buffers for resampling to avoid per-chunk allocations.

Acceptance criteria

  • Max workletNode.port.onmessage handler time <5ms under normal load.
  • Main thread no longer does heavy resampling and VAD per chunk.
  • CPU usage at startup (fresh session) drops measurably.

Files

  • src/lib/audio/AudioEngine.ts
  • AudioWorklet processor in AudioEngine (inline processor code)

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:criticalMust-fix regressions, leaks, or major UX/perf blockers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions