Commit fabeaa47 authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Discovery: boost mic sensitivity and filter noise via real-time capture constraints

autoGainControl (sensitivity - boosts a quiet/distant snippet) and
noiseSuppression (filters steady background noise) are now requested
on the getUserMedia call, plus a 44.1kHz sampleRate hint since some
phones default the mic to a voice-optimized rate that hurts music
fingerprint matching. echoCancellation is explicitly off - it's built
for cancelling a device's own call/speaker output, not relevant here,
and can color music it doesn't need to touch.

Chose this over any server-side denoising step deliberately: these are
real-time browser/WebRTC audio-pipeline features applied during
capture, not post-processing, so they add zero time on top of the
fixed 5-second recording window - the exact opposite of what a backend
audio-processing pass would do to the "must stay ~5s total" budget.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent 0ccd9d7f
Pipeline #1120 canceled with stage
......@@ -86,6 +86,30 @@ describe('Discovery', () => {
expect(screen.queryByLabelText(/Listen for what/)).not.toBeInTheDocument()
})
it('requests noise suppression and auto gain, not echo cancellation, when starting to listen', async () => {
const { getUserMedia } = mockMicSupport()
vi.spyOn(discoveryApi, 'listen').mockResolvedValue({ status: 'success', detected: {}, discovery_id: 1 })
renderDiscovery()
const button = await screen.findByLabelText(/Listen for what/)
vi.useFakeTimers()
try {
await act(async () => {
fireEvent.click(button)
})
} finally {
vi.useRealTimers()
}
expect(getUserMedia).toHaveBeenCalledWith({
audio: expect.objectContaining({
autoGainControl: true,
noiseSuppression: true,
echoCancellation: false,
}),
})
})
it('records 5 seconds automatically, shows a listening state, and submits the clip', async () => {
mockMicSupport()
const listenSpy = vi.spyOn(discoveryApi, 'listen').mockResolvedValue({
......
......@@ -7,6 +7,21 @@ import { EmptyState } from '../components/ui/EmptyState'
const LISTEN_SECONDS = 5
// Real-time capture constraints, not post-processing - the browser's own audio pipeline
// applies these while recording, so they cost nothing on top of the fixed 5-second window.
// autoGainControl boosts a quiet/distant snippet ("sensitivity"); noiseSuppression removes
// steady background noise (traffic, HVAC, chatter) before it ever reaches the recorded clip.
// echoCancellation is explicitly off - it's built for cancelling a device's own call/speaker
// output, not relevant here, and can subtly color music it doesn't need to touch. A higher
// sampleRate is requested too, since some phones default the mic to a voice-call-optimized
// (lower) rate that hurts fingerprint matching on music.
const LISTEN_AUDIO_CONSTRAINTS: MediaTrackConstraints = {
autoGainControl: true,
noiseSuppression: true,
echoCancellation: false,
sampleRate: 44100,
}
function checkMicSupport(): boolean {
return typeof navigator !== 'undefined' && !!navigator.mediaDevices?.getUserMedia && typeof MediaRecorder !== 'undefined'
}
......@@ -76,7 +91,7 @@ export function Discovery() {
setMessage(null)
let stream: MediaStream
try {
stream = await navigator.mediaDevices.getUserMedia({ audio: true })
stream = await navigator.mediaDevices.getUserMedia({ audio: LISTEN_AUDIO_CONSTRAINTS })
} catch {
setMessage("Couldn't access the microphone - check your browser's permission for this site.")
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment