Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microphone builds up delay over time #80173

Open
Tracked by #76797
edmundmtang opened this issue Aug 2, 2023 · 12 comments
Open
Tracked by #76797

Microphone builds up delay over time #80173

edmundmtang opened this issue Aug 2, 2023 · 12 comments

Comments

@edmundmtang
Copy link

edmundmtang commented Aug 2, 2023

Godot version

v4.1.1.stable.official [bd6af8e]

System information

Godot v4.1.1.stable - Windows 10.0.19045 - Vulkan (Mobile) - dedicated GeForce GTX 960 () - Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz (8 Threads)

Issue description

The delay between when a sound is produced in real life and when it is reproduced within Godot grows over time, gaining about 1 second of delay for every hour that the project runs.

I'd also note that I am using a USB mic (Blue Yeti) set to 2 channel, 16 bit, 48000 Hz. Likewise, the mix rate in project settings is set to 48000.

Steps to reproduce

  1. Create an AudioStreamPlayer node, assign a new AudioStreamMicrophone to Stream and turn on Autoplay.
  2. Go to Audio and Add Bus. Then add a Record effect to the new bus (Add Effect > Record).
  3. Enable audio input under Project > Project Settings... > Audio > Driver > Enable Input.
  4. Run project. You should be able to hear your microphone through the project. Make a sharp noise (like snapping your fingers) and note the delay (it should be very short, within milliseconds).
  5. Let the project run for an extended period of time. Make a sharp noise again and it should be apparent that it has taken longer to hear it reproduced within the project.

Minimal reproduction project

microphone_test.zip

@fire
Copy link
Member

fire commented Aug 2, 2023

Perhaps we need to rewrite AudioEffectRecord to be using AudioEffectCapture. @lyuma Any ideas?

@edmundmtang
Copy link
Author

Your comment prompted me to go back and look into something and I realized that I was using AudioEffectCapture in my own project, so this issue isn't due to using either and I can experience it without adding the extra audio bus.

@edmundmtang edmundmtang changed the title AudioEffectRecord builds up delay over time Microphone builds up delay over time Aug 2, 2023
@fire
Copy link
Member

fire commented Aug 2, 2023

The mix rate is very suspicious. Can you double check if they match and what the number is?

@edmundmtang
Copy link
Author

image
Yeah, I'm making sure they're matching at 48000.

@CalinLeafshade
Copy link

I can confirm this is a problem.
The issue here: edmundmtang/vosk-godot#6 (comment) by @edmundmtang recommends "restarting" the microphone periodically but I can't figure out a way to reliably do that.

Stopping the AudioStreamPlayer seems to just kill the microphone input entirely even if I restart it.

How can I "reset" the microphone to remove the delay?

@edmundmtang
Copy link
Author

edmundmtang commented Dec 22, 2023

How can I "reset" the microphone to remove the delay?

I call stop() and then play() on the AudioStreamPlayer. Often it works, but as you noted sometimes it just kills the microphone input entirely and nothing short of a full app restart will get it to work. So sadly the workaround doesn't really fix the problem because eventually the microphone will stop working if you restart it frequently enough to keep the desync low.

@Calinou
Copy link
Member

Calinou commented Dec 22, 2023

This reminds me of an issue I encounter on CS2 on Linux with -sdlaudiodriver pipewire (it uses SDL3). Changing audio output devices resolves latency until it slowly starts building up again, at a similar rate of 1 second per hour the game is running.

I have no idea why it's occurring either way, but I figure I'd leave this here nonetheless.

@Ghoulcel
Copy link

Ghoulcel commented May 19, 2024

image
Ran into this problem myself and toggling the "playing" variable on my AudioStreamPlayer every 30 seconds seems to have fixed it. Previously I'd get 30~ mins in before getting a 2+ sec delay, after I tried this I had the project running for several hours in the background and had no delay. However, this might need more testing since I wasn't actively talking during that period, where as when I ran into the issue originally I was talking consistently. Also the exact numbers of 30secs and a 0.1 await delay are complete guesses, but seems to work for me so I'm not touching them.

@Kaio20
Copy link

Kaio20 commented May 19, 2024

I also had that problem when I connected the microphone over a USB dongle. When I plugged in directly, it went away

@souplamp
Copy link
Contributor

Often it works, but as you noted sometimes it just kills the microphone input entirely and nothing short of a full app restart will get it to work.

Curious if #75603 is tangentially related, or the fix at #75628 would influence this at all

@CarltonYeung
Copy link

image Ran into this problem myself and toggling the "playing" variable on my AudioStreamPlayer every 30 seconds seems to have fixed it. Previously I'd get 30~ mins in before getting a 2+ sec delay, after I tried this I had the project running for several hours in the background and had no delay. However, this might need more testing since I wasn't actively talking during that period, where as when I ran into the issue originally I was talking consistently. Also the exact numbers of 30secs and a 0.1 await delay are complete guesses, but seems to work for me so I'm not touching them.

Thanks for this! It seems to work. I've been banging my head against the wall for a couple days trying to figuring out how to solve this. I have push-to-talk functionality that I handle within _input() and I found that toggling the AudioStreamPlayer's playing would fix it, but when I tried to do it in code at the start of every push-to-talk, it didn't seem to do anything. Somehow putting it in a timer like you suggested works. Previously, after exactly 10 minutes of running my game, the recording would be delayed, or garbled/staticky, or both. I would use the push-to-talk, and find that the saved recording was something I said 5 seconds earlier when I wasn't pushing-to-talk yet.

@Bitlytic
Copy link
Contributor

Bitlytic commented Oct 12, 2024

This issue is still happening on 4.3, is there any way that we could fix this from an AudioServer standpoint? The toggle solution works as a band-aid fix but something more engine level would be nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants