When building web applications that capture or process sound, one of the first tools you will use is the AudioContext from the Web Audio API. It gives you the environment to create, process, and control audio right inside the browser.
One detail that often surprises developers is the sample rate. If you ignore it, your app might behave inconsistently across devices. If you handle it properly, you get more predictable and higher quality results.
What is AudioContext?
The AudioContext
is like the control room for all audio in your web app. It manages:
- The sample rate (how many times per second audio is measured)
- The processing graph (nodes that filter, analyze, or modify sound)
- Timing for audio playback and recording
Here’s a simple example:
const audioContext = new AudioContext();
console.log("Sample rate:", audioContext.sampleRate);
This prints the sample rate that the browser is currently using.
What is Sample Rate?
Audio is stored as a series of snapshots taken many times per second. The sample rate is how many of those snapshots are captured or played back every second.
- 44100 Hz (44.1 kHz) is common for music and matches CD quality.
- 48000 Hz (48 kHz) is often used in video and conferencing apps.
- Some devices (especially mobile) might use different rates like 16000 Hz or 22050 Hz.
If your app expects one sample rate but the browser provides another, audio can sound off, or features like speech recognition might perform poorly.
Why Sample Rate Matters in Web Apps
Consistency
If you are building a recording tool, different users might get recordings with different sample rates depending on their device.Audio Processing
Libraries that analyze sound (for example, pitch detection or voice activity detection) often assume a fixed sample rate. Mismatched values can lead to inaccurate results.Integration with Backends
If you send audio to a server for processing, you need to ensure the sample rate matches what the server expects, otherwise resampling will be required.
Handling Sample Rate with AudioContext
When you create an AudioContext
, the browser usually sets the sample rate based on the system’s audio hardware. You can check it like this:
const audioContext = new AudioContext();
console.log("Current sample rate:", audioContext.sampleRate);
If you need a specific sample rate, you can request it:
const audioContext = new AudioContext({ sampleRate: 16000 });
console.log("Requested sample rate:", audioContext.sampleRate);
Keep in mind that not all browsers or devices will honor your request. Some will stick to the system default.
Resampling in the Browser
If you cannot get the exact rate you need, you can resample using the Web Audio API. For example, you can record audio from the microphone, process it through an OfflineAudioContext
with your desired rate, and export it.
async function resampleAudioBuffer(audioBuffer, targetRate) {
const offlineCtx = new OfflineAudioContext(
audioBuffer.numberOfChannels,
audioBuffer.duration * targetRate,
targetRate
);
const bufferSource = offlineCtx.createBufferSource();
bufferSource.buffer = audioBuffer;
bufferSource.connect(offlineCtx.destination);
bufferSource.start(0);
return await offlineCtx.startRendering();
}
This ensures you always get audio in the sample rate your app or backend expects.
Wrapping Up
The AudioContext
is the entry point to audio processing in the browser, and the sample rate is one of its most important properties. Ignoring it can lead to unpredictable behavior across devices, but with the right handling you can make your app’s audio consistent and reliable.
Start by checking the sample rate in your development setup, experiment with requesting different values, and add resampling where consistency is critical. With these techniques, your web audio features will feel professional and stable no matter where they run.
If you're a software developer who enjoys exploring different technologies and techniques like this one, check out LiveReview.
LiveReview delivers high-quality feedback on your PRs/MRs within minutes.
It saves hours per review by providing fast, automated first-pass insights. This helps both junior and senior engineers move faster.
If you're tired of waiting on peer reviews or unsure about the quality of feedback you'll receive, LiveReview is here to help.
Top comments (0)