0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

A left/right speaker test in the browser with StereoPannerNode

0
Posted at

"Is my left speaker dead or is it the file?" is one of the most common audio questions, and a browser can answer it in a few lines of code. The Web Audio API has a StereoPannerNode that moves a sound fully left or fully right, so you can build a channel test without any audio files.

The idea

Generate a short tone with an OscillatorNode, pass it through a StereoPannerNode, and set pan to -1 for left, 1 for right and 0 for both. A gain envelope keeps the start and stop click-free.

Minimal code

const ctx = new AudioContext();

function beep(pan, freq = 440, seconds = 1) {
  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.4, t + 0.02);
  gain.gain.setValueAtTime(0.4, t + seconds - 0.05);
  gain.gain.linearRampToValueAtTime(0, t + seconds);

  osc.start(t);
  osc.stop(t + seconds);
}

Wire three buttons to it:

<button onclick="ctx.resume(); beep(-1)">Left</button>
<button onclick="ctx.resume(); beep(0)">Both</button>
<button onclick="ctx.resume(); beep(1)">Right</button>

ctx.resume() is needed because browsers keep a new AudioContext suspended until the user interacts with the page.

Why each detail matters

  • Short ramps (20 ms in, 50 ms out): an abrupt start or stop makes an audible click, which people mistake for a crackling speaker.
  • Gain 0.4, not 1.0: a full-scale sine can distort on small phone speakers and make a healthy speaker sound broken.
  • 440 Hz default: most small speakers reproduce it well. Very low tones (under about 150 Hz) barely come out of phone speakers at all, so a missing bass tone is not a fault.

Reading the results

  • Left and right both play: wiring and channels are fine. If music still sounds one-sided, check the balance setting in the system accessibility options.
  • One side is silent on speakers but fine on headphones: the speaker itself, or debris in its grille, is the likely cause.
  • Both sides are quiet or muffled: look for dust or water in the grilles before assuming hardware failure.
  • Phones with a single bottom speaker: "left" and "right" may both come from one place, and the earpiece may only act as a second speaker in landscape. That is normal.

Sweep instead of a single tone

To hear rattles, sweep the frequency over a few seconds:

osc.frequency.setValueAtTime(200, t);
osc.frequency.exponentialRampToValueAtTime(2000, t + seconds);

A buzz or rattle at one point in the sweep usually means something loose near the speaker, not a dead driver.

A ready-made version

If you just want to run the check rather than build it, the online speaker test on Fix My Speaker has left, right and sweep tests ready in the browser.

MDN has the full reference for the node used here: https://developer.mozilla.org/en-US/docs/Web/API/StereoPannerNode

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?