← View all posts
June 2, 2017

Fiddle-of-the-week: microphone constraints

Contributed by Jan-Ivar Bruaroey,

Two new microphone constraints got standardized last week, available now with adapter.js or Firefox Nightly:

{ autoGainControl: true, noiseSuppression: false }

Handy if you’re a musician or a doctor (or both), since audio in WebRTC, unsurprisingly, is tuned for talking heads by default, not guitar riffs or listening to heartbeats remotely.

Basically, Firefox and Chrome have had these since forever with vendor prefixes, but they’ve remained underappreciated. Maybe hearing the difference will help?

The following shows off other bits of the media capture spec as well. This demo only works in Firefox right now because it uses applyConstraints, which Chrome doesn’t support just yet.

Warning: Put on some headphones before you hit the “Result” tab, or it may feedback!

After mic permission is granted, we use track.getSettings() to make the checkboxes reflect current settings:

      ☑ echoCancellation noiseSuppression autoGainControl mute

The first two will likely be on by default in Firefox. However, like all device settings, defaults can vary from browser to browser1, device to device, or even the situation, so never rely on them. If you care about a setting, constrain it.

The best way to test they’re working is probably a headset and background noise:

  • With ☐ noiseSuppression you should hear background noise noticeably appear.
  • Then with ☑ autoGainControl you should hear the background noise amplified a bit.
  • Finally, with ☑ noiseSuppression back on: amplification without the noise.

Even without audio, you should be able to see the difference, thanks to the wonderfully simple spectrum visualizer originally written by Adam Roach!

What to set them to

Historically, the most common use has been to turn off all filtering, on the basis that filters sacrifice fidelity. Especially with stereo, where some implementations may revert to mono when particular filters are used.

But tailor them to what you’re recording: talking heads vs. music performances, nature sounds (beating hearts), ambient noise, a recording booth. Also consider the sink (local playback, peer connection, MediaRecorder, etc.). E.g. if it will go through Web Audio, you may want the raw signal and do your own filtering.

Next, we’re working on the channelCount constraint, so you’ll know when you’re getting stereo!

1) For instance, Chrome turns off all audio processing in response to echoCancellation.