A speaker that buzzes on one bass note but sounds fine everywhere else is hard to diagnose with music, because music jumps around the spectrum. A slow frequency sweep fixes that: it walks a single sine tone from low to high so every frequency gets its turn, and a loose panel, a cracked cone or a vibrating shelf shows up as a buzz at one specific point. Here is a small Web Audio sweep you can run in any browser.
The idea
An OscillatorNode produces a pure sine wave. Its frequency is an AudioParam, which means you can schedule changes over time instead of updating it in a loop. exponentialRampToValueAtTime is the right ramp here, because we hear pitch logarithmically: an exponential sweep spends as long going from 50 to 100 Hz as it does from 1,000 to 2,000 Hz.
The code
const ctx = new AudioContext(); function sweep({ from = 40, to = 2000, seconds = 30, volume = 0.2 } = {}) { const osc = ctx.createOscillator(); const gain = ctx.createGain(); const t0 = ctx.currentTime; osc.type = 'sine'; osc.frequency.setValueAtTime(from, t0); osc.frequency.exponentialRampToValueAtTime(to, t0 + seconds); // short fades so the start and end do not click gain.gain.setValueAtTime(0, t0); gain.gain.linearRampToValueAtTime(volume, t0 + 0.05); gain.gain.setValueAtTime(volume, t0 + seconds - 0.05); gain.gain.linearRampToValueAtTime(0, t0 + seconds); osc.connect(gain).connect(ctx.destination); osc.start(t0); osc.stop(t0 + seconds); return { osc, t0, from, to, seconds }; } document.querySelector('#start').onclick = async () => { await ctx.resume(); // browsers need a user gesture first window.current = sweep(); }; Showing the current frequency
When you hear the buzz you want to know where it happened. Because the ramp is exponential you can compute the frequency at any moment instead of reading it back:
function freqNow({ t0, from, to, seconds }) { const p = Math.min(Math.max((ctx.currentTime - t0) / seconds, 0), 1); return from * Math.pow(to / from, p); } setInterval(() => { if (window.current) { document.querySelector('#hz').textContent = freqNow(window.current).toFixed(0) + ' Hz'; } }, 100); Add a button that stores freqNow() when pressed, and you have a buzz logger: press it every time something rattles and you get a list of problem frequencies.
Reading the results
- A buzz that follows the speaker wherever you move it is usually the driver itself or its housing.
- A buzz that disappears when you move the speaker is the furniture, a window or an object nearby resonating at that frequency.
- A rattle only below about 100 Hz on a phone or laptop is often just the small driver reaching its physical limit; lower the volume and check again before blaming the hardware.
Volume and safety
Keep the gain modest. Small speakers can be damaged by loud low-frequency tones, and pure sine waves are tiring to listen to. Start around 0.1 to 0.2 and raise it only as far as you need to hear the problem.
If you just want to play test tones without writing code, a browser tone generator does the same job with sliders for frequency and volume; I use a browser tone generator when I am checking speakers away from my own machine. Building it yourself, though, is a nice way to learn how AudioParam scheduling works, and the same pattern powers fades, LFOs and envelopes in any Web Audio project.
