Why our browser pet monitor turns off echo cancellation (and other Web Audio lessons)

2026-12-02来源 dev.toEnglish

We build Bugabot at Tainatec: an old phone in the browser listens to a room with a noisy pet, answers the noise with a
calm voice or a soft sound, and sends the owner a Telegram message. No app, just a tab. Here are the browser details that
took us the longest to get right.

1. Turn off what the browser does for calls

getUserMedia defaults are tuned for video calls. Echo cancellation, noise suppression and automatic gain control all try
to keep a human voice steady and remove everything else. For us "everything else" is exactly the signal: a bark, a parrot
scream, a cage being chewed. So the microphone opens with all three switched off, mono, at 48 kHz:

navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: false, noiseSuppression: false, autoGainControl: false, channelCount: 1, sampleRate: 48000 } }); 

With automatic gain on, a quiet room slowly gets amplified until the fridge looks like a dog. With it off, the level means
the same thing at 3 a.m. as at noon.

2. One number the owner can understand

An AnalyserNode reads the time-domain samples on every animation frame, and we turn them into a level from 0 to 100.
The owner sets one sensitivity slider; when the level crosses it, the bot reacts. One number is easier to tune at night
than a frequency profile, and the owner sees the level move in real time while the pet makes noise.

3. Do not hear yourself

The bot answers with sound, and a microphone next to a speaker will hear that answer. While our own phrase plays, and for
800 ms after it ends, detection is paused. After a reaction the bot waits for 3 seconds of quiet before it goes back to
listening, so one long bark does not become ten reactions.

4. Keep the phone awake

A phone left on a shelf wants to sleep. The Screen Wake Lock API (navigator.wakeLock.request("screen")) keeps the screen
on while the bot runs, and we request it again when the tab becomes visible, because the lock is released whenever the
page is hidden.

5. One bot per phone

People open the page twice. A BroadcastChannel lets the tabs talk to each other, so a second tab knows a bot is already
running instead of fighting the first one for the microphone.

6. Video only when someone watches

Live video from the room goes over WebRTC (RTCPeerConnection) and starts only when the owner opens it from their own
phone. Nothing from the room is saved: the bot reacts, notifies and streams, and that is all.

If you have a pet that is loud when you are out, the guide behind all this is here:
How to stop your dog barking when you're not home.
The bot itself runs at bugabot.com.

原文:https://www.a8q9.cn/en/article/why-our-browser-pet-monitor-turns-off-echo-cancellation-and-other-web-audio-less-794c0a.html

相关文章