Indie dev efficiency3 min read

Why Web Audio Decodes a WAV at the Context’s Sample Rate

Decode the same WAV in two offline contexts to compare sample rate, frame count and duration.

Why Web Audio Decodes a WAV at the Context’s Sample Rate
A light sweeps across two sampling wheels with different tick spacing
Concept illustration

A WAV file says 44.1 kHz, yet the browser returns an AudioBuffer at 48 kHz. The context controls the rate of the decoded buffer. Keep the file metadata separate from the samples you will actually edit.

Decode one WAV twice

Use an exact one-second 44.1 kHz PCM WAV with 44,100 source sample frames. Create two OfflineAudioContexts at 44,100 and 48,000 Hz. Pass a fresh slice(0) copy of the file’s ArrayBuffer to each decodeAudioData call, since the input buffer is detached. You do not need to start offline rendering.

Record sampleRate, length and duration. Expect approximately 44,100 frames in the first result and 48,000 in the second, both near one second. Read the original WAV header separately when checking the file’s delivery specification.

Convert an edit point through time

Frame 22,050 in the source is about half a second in. In a 48 kHz buffer, that lands near frame 24,000. Copying the old frame index would move the edit earlier. Convert through seconds, apply a consistent rounding rule and check the result in the target player.

Use the game sound effects generator to prepare a representative effect and run the same comparison. Record both the file header and the decoded buffer fields when reporting any discrepancy.

Recommended tools

Ready to try "Why Web Audio Decodes a WAV at the Context’s Sample Rate"?

Use our online tools to apply this tutorial to your indie game project right away.