Questions about MediaElementAudioSourceNode and resampling

30 views
Skip to first unread message

Mike Segal

unread,
Sep 8, 2025, 1:32:48 PMSep 8
to media-dev

Hi,

I’m working on a web project that plays audio files from different sources through the same player.

I have a MediaElementAudioSourceNode, created by an AudioContext with an initial sample rate of 48,000 Hz.

When setting the source of the audioElement to an audio file with a known sample rate of 8,000 Hz, my understanding is that the file will be upsampled to 48,000 Hz or to the native sample rate of the output device.

  • When does this upsampling occur?

  • Is it possible to measure the time it takes?

  • Can I verify what sample rate the audio element will actually play at (or upsample to)?

  • How can I ensure that I don’t degrade audio quality if the AudioContext is initialized at too low a sample rate?

Thanks,
Mike

Dale Curtis

unread,
Sep 8, 2025, 1:44:46 PMSep 8
to Mike Segal, Hongchan Choi, media-dev

--
You received this message because you are subscribed to the Google Groups "media-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to media-dev+...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/media-dev/36d9dd4e-dd3b-42b9-8475-aae172818b11n%40chromium.org.

Hongchan Choi

unread,
Sep 10, 2025, 1:36:12 PMSep 10
to Dale Curtis, Mike Segal, media-dev
Hello Mike,

Please find my response inline below:

On Mon, Sep 8, 2025 at 10:32 AM Mike Segal <mi...@segalcon.com> wrote:

Hi,

I’m working on a web project that plays audio files from different sources through the same player.

I have a MediaElementAudioSourceNode, created by an AudioContext with an initial sample rate of 48,000 Hz.

When setting the source of the audioElement to an audio file with a known sample rate of 8,000 Hz, my understanding is that the file will be upsampled to 48,000 Hz or to the native sample rate of the output device.

  • When does this upsampling occur?

The upsampling is handled by the MediaElementAudioSourceHandler (the internal impl of the MediaElementAudioSourceNode)
 
  • Is it possible to measure the time it takes?

This can't be measured directly from your JavaScript code because it's an internal browser process. For debugging, internal tracing points could be added to the implementation to measure the performance. 
  • Can I verify what sample rate the audio element will actually play at (or upsample to)?

Yes, the audio will always be resampled to match the sample rate of your AudioContext. You can confirm this value by checking the audioContext.sampleRate property. 
  • How can I ensure that I don’t degrade audio quality if the AudioContext is initialized at too low a sample rate?

To avoid quality loss, ensure the AudioContext sample rate is at least as high as your source audio's sample rate.

```js
const audioContext = new AudioContext({ sampleRate: mediaFileSR * 2 });
```
Since detecting a file's sample rate in the browser is not straightforward, the most reliable approach is to get this metadata from your server and use it to configure the AudioContext appropriately.

Hope it helps!

Best,
Hongchan

Mike Segal

unread,
Oct 12, 2025, 1:22:12 PMOct 12
to media-dev, Hongchan Choi, Mike Segal, media-dev, dalec...@chromium.org

Hello again,

Thank you for your previous helpful replies.

I noticed a discrepancy when playing audio through an AudioContext at 48,000 Hz. The original file at 8,000 Hz sounds different from the same file upsampled to 48,000 Hz using Adobe Audition.

Could this difference be caused by the browser’s resampling algorithm? Would using OfflineAudioContext produce a closer result to the high-quality offline upsampling?

Thanks again,
Mike

guest271314

unread,
Oct 12, 2025, 2:17:20 PMOct 12
to Mike Segal, media-dev, Hongchan Choi, dalec...@chromium.org
Any reason you don't use 8000 Hz to play back audio?
> To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/media-dev/2e7f6fb1-41d2-4d01-965f-9a36eddd2506n%40chromium.org.

Mike Segal

unread,
Oct 12, 2025, 2:29:22 PMOct 12
to media-dev, guest...@gmail.com, media-dev, Hongchan Choi, dalec...@chromium.org, Mike Segal
I mentioned this earlier, but the audio files don’t come in a consistent sample rate. To ensure uniform playback quality, I play all files through an AudioContext at a fixed sample rate (48,000 Hz).

guest271314

unread,
Oct 12, 2025, 2:39:05 PMOct 12
to Mike Segal, media-dev, Hongchan Choi, dalec...@chromium.org
I don't understand what the issue is.

You should be able to get the original sample rate of each media file,
then apply that sample rate at AudioContext contructor.

What is meant by sounds different? I think it is *very* difficult to
reproduce any two sounds exactly under ideal conditions. Maybe if you
re-encoded the files before playing the playback might be more
consistent between discrete media players.

Mike Segal

unread,
Oct 12, 2025, 3:02:08 PMOct 12
to media-dev, guest...@gmail.com, media-dev, Hongchan Choi, dalec...@chromium.org, Mike Segal

So there are a couple of issues here:

  1. The tested sound is a consistent 3,990 Hz tone. One file is at 8,000 Hz, the other is the same file, but upsampled to 48,000 Hz (using Adobe Audition). When played in the same AudioContext, the two files sound noticeably different. Spectrogram analysis of recorded output shows clear differences in the high frequencies and the intensity of the tone.

  2. Determining the actual sample rate of a media file on the client is not straightforward, as Hongchan noted.

  3. Even if this were simple, the media player instance is not recreated between files - the same instance is reused (like a playlist in Spotify). Changing the file alone does not change the player instance.

Other than that, my main question remains: Is there a difference between the resampling performed by OfflineAudioContext and the resampling of the main AudioContext?

Hongchan Choi

unread,
Oct 17, 2025, 5:53:38 PM (10 days ago) Oct 17
to Mike Segal, media-dev, guest...@gmail.com, dalec...@chromium.org
> Other than that, my main question remains: Is there a difference between the resampling performed by OfflineAudioContext and the resampling of the main AudioContext?

Are you getting a different result?

The difference is in the architectural layer where the resampling happens:
-   For `OfflineAudioContext`, all operations are fully contained within the Blink rendering engine.
-   For a real-time `AudioContext`, we've optimized the stack to apply resampling only once, at the very end of the output chain (within the browser process AudioService).

That said, both contexts use the same C++ class to perform the resampling. If you are observing sample-level differences with identical settings, I would consider that a bug.

Best,
Hongchan

Reply all
Reply to author
Forward
0 new messages