DEV Community

Fix My Speaker
Fix My Speaker

Posted on

A Left/Right Stereo Speaker Test With the Web Audio API

A surprising number of "my speaker sounds wrong" reports turn out to be channel problems: one side is dead, the cables are swapped, or a balance slider was nudged months ago. A left/right test that runs in the browser answers the question in seconds, and the Web Audio API makes it about thirty lines of code.

The core: a panner node

StereoPannerNode moves a sound between the left (-1) and right (1) channels. Put one between an oscillator and the output and you can send a tone to either side:

const ctx = new AudioContext();

function playTone(pan, freq = 440, seconds = 1.2) {
  const osc = new OscillatorNode(ctx, { type: 'sine', frequency: freq });
  const gain = new GainNode(ctx, { gain: 0 });
  const panner = new StereoPannerNode(ctx, { pan });

  osc.connect(gain).connect(panner).connect(ctx.destination);

  const t = ctx.currentTime;
  gain.gain.linearRampToValueAtTime(0.25, t + 0.05);       // fade in
  gain.gain.setValueAtTime(0.25, t + seconds - 0.05);
  gain.gain.linearRampToValueAtTime(0, t + seconds);       // fade out

  osc.start(t);
  osc.stop(t + seconds);
}
Enter fullscreen mode Exit fullscreen mode

The short fades matter. A sine wave that starts or stops abruptly produces a click, and on a speaker that is already suspect, users will hear the click and blame the hardware.

Wire up the buttons

document.querySelector('#left').onclick = async () => {
  await ctx.resume();          // autoplay policy: needs a user gesture
  playTone(-1);
};
document.querySelector('#right').onclick = async () => {
  await ctx.resume();
  playTone(1);
};
Enter fullscreen mode Exit fullscreen mode

Browsers keep an AudioContext suspended until the user interacts with the page, so call resume() inside the click handler.

Make the result unambiguous

A pure tone is hard to place for some people, especially on small laptop speakers that sit close together. Two tricks help:

  • Use different pitches per side (for example 440 Hz left, 660 Hz right) so the user can say "the high one came from the left" and you know the channels are swapped.
  • Add a "centre" button with pan: 0. If centre sounds clearly off to one side, the balance setting in the operating system is the likely culprit.

Keep the volume sensible

Start with a gain around 0.25 and let the user raise system volume. Test tones at full scale are unpleasant on headphones and can strain small phone speakers.

What a channel test will not tell you

A left/right check confirms that each channel works. It does not catch a rattle, a blown cone or a muffled sound from dust. For those, a frequency sweep or a few music clips are more revealing. If you would rather not build any of this, a free online speaker test such as this one bundles the channel check with a sweep and other tones, and works on phones as well as laptops.

Either way, test channels before anything else. It takes ten seconds and rules out the most embarrassing kind of "broken" speaker.

Top comments (0)