fix(audio): align AudioContext to 48kHz up front for Opus streams

Opus resolved its 48kHz context lazily on the first chunk, close()ing and rebuilding the live graph mid-decode. Move the recreate into initializeStreaming so it runs before any bytes flow; the lazy call early-returns. WAV path unchanged.
This commit is contained in:
daniel-c-harvey
2026-06-24 23:26:42 -04:00
parent 8a6acd5f5f
commit 0800167511
3 changed files with 24 additions and 5 deletions
@@ -123,7 +123,11 @@ export class OpusStreamDecoder implements IStreamingDecoder {
// Copy the OpusHead into a standalone buffer — the sidecar subarray is a view we keep.
this.opusHeadDescription = opusHead.slice();
// Opus decodes at 48 kHz; align the context so no resample is needed.
// Opus decodes at 48 kHz; align the context so no resample is needed. AudioPlayer.initializeStreaming
// already aligned it to 48 kHz up front (the format is resolved before any bytes flow), so in the
// common path this is an early-return no-op — the live graph is NOT close()'d and rebuilt mid-decode.
// Kept as the defensive backstop for any path that reaches a configured decoder on a non-48 kHz
// context (the same recreate seam the WAV path uses for non-44.1 sources).
if (this.contextManager.sampleRate !== OPUS_SAMPLE_RATE) {
await this.contextManager.recreateWithSampleRate(OPUS_SAMPLE_RATE);
}