Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Web Audio API latency

I want to make a Javascript program to process some audio from microphone. But when I make just a basic example, like get audio from microphone and play it back without any processing, just like this

source = audioCtx.createMediaStreamSource(stream);
source.connect(audioCtx.destination);

I get 200 milliseconds latency between input and output audio. I tried in Google Chrome and Firefox on 2 different PCs with Windows 7 and Windows 10 and it looks the same everywhere. I got this number (200ms) by recording and analysing audio with an external device, not just by my ears.

In official W3C specification here they tell about 3-50 ms latency. 30-50 ms is what I need. I do not really need 3-5 ms.

I tried to play with latency parameter like this

navigator.mediaDevices.getUserMedia({
  audio: {
    latency: 0.05,
    echoCancellation: false,
    mozNoiseSuppression: true,
    mozAutoGainControl: false
  }
});

but looks like it is ignored by Chrome and FireFox.

My full example is available here.

https://jsfiddle.net/xfq3ykp7/71/

Is it browser/hardware limitation or I am doing something wrong? Can anyone hear some latency with my example? Usually, it is easy to check it with hitting a table near a microphone. for 200 ms there is a noticeable delay between the hit and its sound.

I do not need complicated things like scheduled audio events etc which are needed for games or online musical instruments. I just need to decrease latency in my simple example from 200 ms to 50 ms or ideally to 20 ms.

UPDATE1:

I could make 70 ms on Windows 10 and 100 ms on Windows 7 in Firefox with that code change

var p = navigator.mediaDevices.getUserMedia({
  audio: {
    latency: 0.02,
    echoCancellation: false,
    mozNoiseSuppression: false,
    mozAutoGainControl: false
  }
});

Thanks to firefox support (https://bugzilla.mozilla.org/show_bug.cgi?id=1375466). But they tell that 30 ms should be possible. The question is still open "How to get 30 ms latency on Windows?" and "What is hardware requirements?".

like image 505
Zlelik Avatar asked Mar 18 '26 15:03

Zlelik


1 Answers

I know that this is a very old question, but the problem remains (8 years later, in 2025), at least in Firefox on Linux. I therefore did the following test with Python, i.e., avoiding the browser and the web audio API altogether:

import pyaudio
pa = pyaudio.PyAudio()
stream_in = pa.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=512)
stream_out = pa.open(format=pyaudio.paInt16, channels=1, rate=44100, output=True)
while True:
    stream_out.write(stream_in.read(512))

The latency seems to be pretty much the same as with direct playback in the browser. I conclude that the problem is likely caused by the OS and/or its drivers, not by the browser and the web audio implementation.

like image 52
tglas Avatar answered Mar 20 '26 05:03

tglas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!