~ Resampling audio via a Web Audio API Audio Worklet
» By Joren on Wednesday 27 September 2023The Web Audio API offers some great functionality for web based audio applications. The API also has a couple of quirks and is not always easy to use. One of those quirks is the limited support for resampling audio. When requesting a microphone stream of a certain sample rate the API only allows configurations your hardware supports. Ideally there should be an option to resample the incoming stream to a requested sample rate (and format) independent of hardware.
On macOS and Chrome the issue becomes even more confusing: when using multiple AudioContexts
they can only have the same sample rate. E.g. starting a microphone on 16kHz by itself is possible but not when there is also audio playback on the same page, then everything switches over to 48kHz. There even seems to be an effect of different browser tabs. Other browsers and platforms have similar issues. This is problematic when you need audio in a fixed sample rate.
The solution is to resample audio incoming samples in your code or use the OfflineAudioContext
as a resampler. The OfflineAudioContext
way needs a lot of code and, crucially, only works on the main browser thread and not in an AudioWorklet
. The AudioWorklet
should be the place for computationally intensive audio processing like resampling. To solve the resampling problem I have glued together an AudioWorklet
and libsamplerate-js to provide an easy to use audio resampling solution which is demo’d below:
The demo does not seem to do much but it reads incoming microphone data and uses a high quality audio resampling library to resample an audio stream into a requested audio sampling rate. The browser development console shows some info on this process. To get this working in an audio worklet, the libsamplerate-js needed to be recompiled and directly included in the AudioWorklet
. To inspect the source, check the Web Audio API AudioWorklet resampler.
The resampling issue came up in development the browser based component of Olaf, an audio fingerprinting solution.