w3c / w3c/webcodecs

Dealing with sample rates for WebCodecs + WebAudio

Open
#378 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension
Dominant language
HTML
Stars
1.3k
Forks
194
Avg merge
1d 13h
Merged PRs (30d)
3

Description

I'm summarizing a discussion w/ @hoch and adding some new questions at the bottom.

The underlying question: What guidance should we give to WebCodecs : WebAudio users on setting AudioContext.sampleRate? The answer so far is: it depends.

Background:

  • By default AudioContext will pick a sample rate that matches the underlying device.
  • If you construct AudioContext with a sample rate that does not match that default, it will create an internal SampleRateConverter (SRC).
  • Ideally: first create an AudioContext, check its default sample rate, then use that sample rate throughout the app.
  • WebCodecs users may not have this^ flexibility. Their sample rate can be a function of the original encoding and possibly the codec limitations (e.g. opus).

So what should WebCodecs users do...

Simple use cases (e.g. playing occasional short sounds) may want to render audio via a single AudioBuffer -> AudioBufferSourceNode. In this case, take the default AudioContext sampleRate. If your AudioBuffer's rate doesn't match your AudioContext, WebAudio will silently resample the buffer for you. You could instead set the AudioContext sampleRate to match your buffer, but this is at best the same amount of conversion and could actually be much more conversion if you're using that AudioContext for other things that wouldn't otherwise require it.

More involved cases may render via the SAB -> AudioWorklet pattern. AudioWorklet expects the sample rate of the AudioContext (default or otherwise). In this case, either (1) construct AudioContext with their desired sample rate, or (2) take care to do external SRC before passing to WebAudio.

@hoch @padenot - can you help me explore that last case more? What are the drawbacks to (1)? Are users likely to hit complexity reconstructing AudioContext for changes to sample rate? Is external resampling (JS or WASM) fairly common already? Should an API for sample rate conversion be considered (some prior art in https://github.com/WebAudio/web-audio-api/issues/118 and https://github.com/WebAudio/web-audio-api/issues/2398)?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the sample-rate guidance and unanswered questions in this issue, including the prior-art Web Audio issues linked in the discussion. Compare AudioContext reconstruction with external JavaScript or WebAssembly resampling, then define the guidance or API decision needed for WebCodecs and WebAudio users.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
audio-video-rtc
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.