Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

6 Web Audio API pitfalls I hit building a browser-only mic test

I built a small web tool that checks whether your microphone works, with nothing to install. It shows a level meter and a waveform, records 10 seconds so you can play it back, and all audio stays inside the browser. It

I built a small web tool that checks whether your microphone works, with nothing to install. It shows a level meter and a waveform, records 10 seconds so you can play it back, and all audio stays inside the browser.

It should have been just getUserMedia + AnalyserNode + MediaRecorder. But once real people tried it on their own machines, a handful of small pitfalls showed up. Here they are, in case you're building something similar.

1. Convert RMS to dB for a meter that feels right

Take the RMS of the samples from getFloatTimeDomainData and convert it to dB.

analyser.getFloatTimeDomainData(buf);
let sum = 0;
for (const v of buf) sum += v * v;
const rms = Math.sqrt(sum / buf.length);
const db = rms > 0 ? 20 * Math.log10(rms) : -100;

The meter maps -60 to 0 dB onto 0–100%. Normal speech mostly lands between -35 and -15 dB. I treat anything above -45 dB as "the mic is picking up sound".

2. At normal speaking volume, the waveform looks flat

If you draw the raw amplitude, speech (roughly 0.02–0.06) barely moves the line, and people assume the mic is broken. For display only, I multiply by 8 and squash it with tanh so it stays inside the box. Background noise stays flat; a voice clearly moves.

const y = h / 2 - Math.tanh(sample * 8) * h * 0.42;

3. "Default - ..." shows up as a duplicate device

Besides the real microphone, Chrome's enumerateDevices() returns extra entries whose deviceId is default or communications (labelled like "Default - MacBook Pro Microphone"). Seeing the same mic twice makes users wonder which one to pick. I hide those alias entries whenever a real device with the same groupId exists.

const alias = d => d.deviceId === 'default' || d.deviceId === 'communications';
const real = all.filter(d => !alias(d));
const list = all.filter(d => !alias(d) || !real.some(r => r.groupId === d.groupId));

4. MediaRecorder's mimeType differs per browser

Chrome supports audio/webm;codecs=opus, while Safari wants audio/mp4. Try them in order with isTypeSupported.

const list = ['audio/webm;codecs=opus', 'audio/mp4', 'audio/ogg;codecs=opus', 'audio/webm'];
const mime = list.find(t => MediaRecorder.isTypeSupported(t)) || '';

5. A Chrome extension popup can't show the permission prompt

I also made a Chrome extension version: a tiny toolbar popup that only shows the level meter. Inside the popup, getUserMedia fails, because the permission prompt can't be shown there. My fix: right after install, the extension opens its own page in a tab once so the user can grant access. After that, the popup works.

6. After a deploy: new HTML + old cached CSS = broken layout

Right after I updated the static site, some browsers combined the new HTML with an old cached CSS file, and the layout fell apart. The fix was to add a content hash to CSS/JS URLs at build time (style.css?v=6b8bec2e4c), so a changed file always gets fetched again.

Wrap-up

Even a tool as simple as a mic test runs into lots of small issues with clarity and device handling once ordinary people use it on their own setups. You can try it at maikutesuto.com. The interface is in Japanese, and audio never leaves your browser.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.