Skip to content

Dealing with sample rates for WebCodecs + WebAudio #378

Open
@chcunningham

Description

@chcunningham

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 WebAudio/web-audio-api#118 and WebAudio/web-audio-api#2398)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    extensionInterface changes that extend without breaking.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions