DEV Community

Fix My Speaker
Fix My Speaker

Posted on

Building a Left/Right Speaker Test with the Web Audio API

A stereo test is one of the most useful small tools you can build with the Web Audio API. It answers a question people ask all the time: is my left speaker actually working, or is the sound only coming from one side? Here is a compact way to build one, plus the details that trip people up.

The core idea: one oscillator, one panner

You need three nodes: a sound source, a StereoPannerNode to push the sound fully left or right, and the output.

const ctx = new AudioContext();

function playSide(pan, seconds = 1.5) {
  const osc = ctx.createOscillator();
  const gain = ctx.createGain();
  const panner = new StereoPannerNode(ctx, { pan }); // -1 left, 1 right

  osc.type = "sine";
  osc.frequency.value = 440;

  const now = ctx.currentTime;
  gain.gain.setValueAtTime(0, now);
  gain.gain.linearRampToValueAtTime(0.3, now + 0.05);
  gain.gain.setValueAtTime(0.3, now + seconds - 0.05);
  gain.gain.linearRampToValueAtTime(0, now + seconds);

  osc.connect(gain).connect(panner).connect(ctx.destination);
  osc.start(now);
  osc.stop(now + seconds);
}
Enter fullscreen mode Exit fullscreen mode

The short gain ramps at the start and end matter. Without them you hear a click every time the tone starts or stops, and users often mistake that click for a speaker fault.

Wire it to buttons, and resume the context

Browsers block audio until the user interacts with the page, so create or resume the AudioContext inside a click handler:

document.querySelector("#left").addEventListener("click", async () => {
  await ctx.resume();
  playSide(-1);
});
document.querySelector("#right").addEventListener("click", async () => {
  await ctx.resume();
  playSide(1);
});
Enter fullscreen mode Exit fullscreen mode

If you skip ctx.resume(), the first click often does nothing on Chrome and Safari, which looks exactly like a broken speaker.

Pick a frequency people can hear

440 Hz is a comfortable middle choice. Very low tones (under about 100 Hz) are almost silent on phone and laptop speakers because they are physically too small to move that much air, so a low tone can make a perfectly healthy speaker seem dead. Very high tones are unpleasant and some adults cannot hear them well. If you offer a sweep, start around 200 Hz and stay below 8 kHz for a general audience.

Things that confuse the result

  • Mono audio settings. iOS, Android, Windows and macOS all have an accessibility option that mixes stereo into mono. When it is on, both speakers play everything and a left/right test looks broken. Mention it in your UI.
  • Phones with one real speaker. Many phones use the earpiece as the second channel, and it is much quieter. Uneven volume is normal there.
  • Bluetooth balance. Some headphones and car systems have their own balance setting that overrides the pan.

Testing without writing code

If you just want to check a device rather than build the feature, you don't need to set up a page yourself. I keep this free tool bookmarked for quick checks: it plays left, right and sweep tests in the browser using the same approach described above. Either way, run the test at moderate volume first and turn it up slowly; a distorting speaker is easier to diagnose before it gets loud.

Top comments (0)