Closed hbCyber closed 3 years ago
Looks like you're misunderstanding the first argument for start()
. For lowest latency the 1st arg should be undefined
, as any other value is interpreted as some future time/delay. So should be player.start()
.
Definitely keep Tone.context.lookAhead = 0
in there for lowest latency as the default value of 100ms is very noticeable especially in response to a button press.
On Windows there are additional quirks to be aware of. Some machines, especially laptops, include system-wide "audio enhancements" or some kind of spatialization DSP turned on by default which adds latency (could be anything from a surround effect to eq, crappy speaker compensation, etc). Check for it in your sound control panel and turn it off if found. Here's an example https://soundplant.org/images/AudioEnhancements.png
@marcelblum Thank you so much for your help! Your suggestion of tweaking the settings on Windows, i.e. uncheck "Enable audio enhancements" did the trick for me, my new screen recording shows that the latency has pretty much been eliminated! I have also changed my call to be player.start()
as you suggested, keeping Tone.context.lookAhead = 0
in my code.
In fact this also helps me with an entirely other issue, where I had input/audio lag in a game, which has also become eliminated by changing the above Windows setting. Thanks!
Describe the bug
I set up a basic webpage which plays a sound when either a keyboard key is pressed, or when a MIDI input command is detected where velocity is not zero. The webpage also changes the background color of a rectangle on the page when such events occur.
This is the code invoked:
I have noticed that there is a delay of about 130ms between the input and the time at which the sound begins to play. The background color changes instantly, but the sound begins playing a bit later. This is consistent even when the sound is played multiple times, i.e. cached.
I am running the webpage on Chrome under Windows 10, and while it's possible Windows has inherent audio lag, or perhaps even my audio card (integrated Realtek), I want to make sure it's really not because of a mistake in my code.
Expected behavior
I would be happy if there was a way to trigger the sound instantly, i.e. at the exact same time that the input is detected, the time at which the background changes.
Additional context
Please see attached video. If you analyze it frame by frame, you will notice that the background color changes a few frames before the sound begins. The video is 30fps and there are 4 frames where the background changes but still no sound, and therefore I presume the delay is about 130ms.
https://user-images.githubusercontent.com/7698618/133801589-dae04b00-b21c-4980-bb12-0fc12464c3c7.mp4