How to determine the minimum supported sample rate of the HTML5 web audio API

46 Views Asked by At

For the longest time I have been converting my 8000 Hz audio to 24000 Hz because the HTML5 web audio API used to have a minimum sample rate of 22050 Hz. At some point over the years it seems that has changed to 8000 - 96000, which means I shouldn't have to convert my audio anymore. Unfortunately I can't find anything online stating when this changed or how to figure out what the browser supports. If this was a recent change I can't just assume the client supports 8K audio. Is there a way to figure out if the browser supports 8K before it attempts to load the audio? I would like to display some type of message to the user about their browser not supporting the audio in question if in fact 8K isn't supported. Also, does anyone know when this change was rolled out?

1

There are 1 best solutions below

2
J_H On

Is there a way to figure out if the browser supports 8K

You can readily assess this, during testing in the lab or via field measurements of production users.

Just provide multiple <source .../> tags for your <audio>. The browser will try them in sequence, until it finds a source it is able to play. The webserver log will tell you which were tried and which were ignored. Be sure to have nginx log the client's User-Agent: header, so you can verify that it predicts observed download behavior. <audio> attributes like preload, muted, and autoplay can be used to adjust how the browser responds.

You can begin with a brief test clip that lasts less than one second, and follow that with the intended audio of longer duration. If you want to get fancy you can even listen for audio events.


it seems that has changed to 8000 - 96000

That change to the W3C Web Audio spec certainly happened some years ago.

A 2012 draft says this about sampleRate:

An implementation must support sample-rates in at least the range 22050 to 96000.

The 2015 draft expanded the range:

An implementation must support sample rates in at least the range 8192 to 96000.

A subsequent 2018 draft revised that to the number you're using:

An implementation MUST support sample rates in at least the range 8000 to 96000.

This language is preserved in the final 2021 Recommendation.