godo57
godo57

Reputation: 548

Reduce audio playback latency from microphone input in javascript?

I need to capture some input from microphone in javascript (from a browser) and playback audio in real time. But the latency I get is really ugly (about 200ms).

How can I reduce this ? Is javascript a good option to expect latency like 20 ms ?

I simply tried to capture the audio using getUserMedia() and AudioContext utils from Web Audio API and play it :

const constraints = {
  video: false,
  audio: {
    echoCancellation: false,
    noiseSuppression: false,
    autoGainControl: false
  }
}

var context = new AudioContext({
  latencyHint: 'interactive',
  sampleRate: 44100,
});

navigator.mediaDevices.getUserMedia(constraints)
  .then((stream) => {
    var source = context.createMediaStreamSource(stream);
    source.connect(context.destination);
  });

Any suggestion is welcome to minimize latency. Thanks

Upvotes: 1

Views: 1766

Answers (1)

Raymond Toy
Raymond Toy

Reputation: 6056

Don't know if this will help, but you should definitely turn off echo cancellation and other input processing if you're sending the audio to WebAudio. See echoCancellation constraint for details. You probably also want to turn off gain control and noise suppression.

Upvotes: 1

Related Questions