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)
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
workletNode.port.onmessage(AudioEngine) max ~20ms, avg ~0.7ms across ~184 calls.resampleLinear, ring buffer writes, VAD, segment processing, UI updates.Proposed change
Acceptance criteria
workletNode.port.onmessagehandler time <5ms under normal load.Files