DEV Community

Fix My Speaker
Fix My Speaker

Posted on

Plotting a Speaker's Rough Frequency Response with the Web Audio AnalyserNode

Phone and laptop speakers are tiny, and most of them roll off hard below a few hundred hertz. If you are building anything that plays tones through them, it helps to see what the speaker actually reproduces instead of guessing. You can get a rough picture with nothing more than the browser, the Web Audio API and a second device's microphone.

This post walks through a minimal "listen and plot" page using AnalyserNode.

What we are measuring (and what we are not)

This is not lab-grade frequency response. The microphone has its own curve, the room adds reflections, and browsers apply processing to mic input by default. What you do get is a quick relative comparison: is this speaker producing anything at 150 Hz, is there a big dip at 2 kHz, did cleaning the grille change the high end?

Step 1: Get a clean microphone stream

Turn off the processing that browsers enable for calls, otherwise auto gain will flatten everything you are trying to see.

const stream = await navigator.mediaDevices.getUserMedia({
  audio: {
    echoCancellation: false,
    noiseSuppression: false,
    autoGainControl: false
  }
});

const ctx = new AudioContext();
const source = ctx.createMediaStreamSource(stream);
Enter fullscreen mode Exit fullscreen mode

Step 2: Add an AnalyserNode

const analyser = ctx.createAnalyser();
analyser.fftSize = 8192;              // ~5.9 Hz bins at 48 kHz
analyser.smoothingTimeConstant = 0.6; // steadier display
source.connect(analyser);

const bins = new Float32Array(analyser.frequencyBinCount);
Enter fullscreen mode Exit fullscreen mode

getFloatFrequencyData fills bins with dB values. Bin i corresponds to i * ctx.sampleRate / analyser.fftSize Hz.

Step 3: Track the peak while a sweep plays

Play a slow sine sweep on the speaker you are testing (another tab, another device, or a browser speaker test page). Then record the loudest level seen in each bin over the whole sweep:

const peak = new Float32Array(bins.length).fill(-Infinity);

function sample() {
  analyser.getFloatFrequencyData(bins);
  for (let i = 0; i < bins.length; i++) {
    if (bins[i] > peak[i]) peak[i] = bins[i];
  }
  requestAnimationFrame(sample);
}
sample();
Enter fullscreen mode Exit fullscreen mode

A "max hold" like this turns a moving sine into a curve once the sweep finishes.

Step 4: Plot on a log axis

Human hearing and speaker behaviour both make more sense on a logarithmic frequency axis.

function draw(canvas) {
  const g = canvas.getContext('2d');
  const w = canvas.width, h = canvas.height;
  const nyquist = ctx.sampleRate / 2;
  const minF = 50, maxF = 16000;
  g.clearRect(0, 0, w, h);
  g.beginPath();
  for (let i = 1; i < peak.length; i++) {
    const f = i * nyquist / peak.length;
    if (f < minF || f > maxF) continue;
    const x = Math.log(f / minF) / Math.log(maxF / minF) * w;
    const y = h - (peak[i] + 100) / 100 * h; // map -100..0 dB
    g.lineTo(x, y);
  }
  g.stroke();
}
Enter fullscreen mode Exit fullscreen mode

Reading the result

  • A steep slope on the left is normal for small speakers; the point where it falls off is roughly the lowest useful frequency.
  • Sharp spikes that appear only when you hold the phone a certain way are usually case or table resonances.
  • A broad drop in the top octave compared with a previous measurement can mean a partly blocked grille.

Gotchas

  • Keep the mic at the same distance and angle between runs, or comparisons are meaningless.
  • Some laptops ignore autoGainControl: false. Check track.getSettings() to see what you actually got.
  • Do not run the sweep loud. Small drivers distort quickly, and distortion shows up as false peaks at harmonics.

That is about 60 lines for a useful diagnostic tool, and it makes "it sounds muffled" a lot easier to pin down.

Top comments (0)