Skip to content

Reduce main-thread CPU in AudioEngine capture path #122

Description

@ysdede

Context

  • Audio capture drives AudioEngine.handleAudioChunk on the main thread, doing resample checks, energy + SMA, VAD (AudioSegmentProcessor), visualization summary, segment extraction, and subscriber callbacks.
  • Capture is active even when model is not ready (v4 path), so this work runs continuously.

Evidence

  • Main thread active time in trace: main_thread.total_dur_ms = 21958.8ms over 29.7s.
  • Hot path locations: src/lib/audio/AudioEngine.ts (handleAudioChunk + updateVisualizationBuffer + notifyVisualizationUpdate), src/lib/audio/AudioSegmentProcessor.ts.

Hypothesis

  • Doing VAD + visualization summary + segment extraction on the main thread contributes to high CPU during capture.

Actions

  • Move VAD + energy computation into AudioWorklet or a worker and send only small metrics back.
  • Split visualization summary into a worker or throttle it to <30fps.
  • Guard processing when not recording or when UI is hidden.

Acceptance

  • Main-thread CPU usage drops measurably when recording with model not ready.
  • 30s trace shows lower main-thread active time and fewer long tasks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestperformancePerformance optimization and profilingpriority: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