Repository: godot
Issue: AudioStreamPlayer2D starts playback with a noticeable delay
Labels: discussion,topic:audio,performance

### Tested versions

Reproducible in: v4.2.2.stable.mono.official [15073afe3]


### System information

Godot v4.2.2.stable.mono - macOS 14.3.0 - Vulkan (Forward+) - integrated Apple M3 Max - Apple M3 Max (14 Threads)

### Issue description

When I use `AudioStreamPlayer2D` to play a sound, the sound can be heard around 110ms after the keypress. This is a noticeable delay, and a player action such as firing a gun or pressing a button doesn't quite feel "immediate". 

The sound sample I'm using does not have any silence at the beginning.

To measure the delay, I recorded an audio track with Audacity. I can clearly hear both the click of my keyboard and the sound being played, and also see the waveforms of both events in the audio track, so the delay is easy to calculate in Audacity's UI.

I measured this delay on three systems:
- MacBook Pro / M3 Max, macOS 14, Studio Display speakers
- Mac mini / M2 Pro, macOS 14, built-in speakers
- A recent gaming PC / Windows 11, wired headphones, onboard sound card

I measured a delay of around 110ms on both macs, and a delay of 160ms on the PC.

Is this something that can be improved, either in the engine, or in my game? Is the delay caused by my systems? Or is such a delay expected in Godot?

To get a few reference points, I did some research and an "Input lag" commonly refers to the latency between the physical input and the display reacting to that input. 60ms is generally considered pretty good, but this includes the response time from the display hardware itself.

In my small Godot test project, I not only play a sound but also toggle the visibility of a sprite on screen. I recorded a video at 60 fps showing how I press the button on a keyboard, and the sprite appears on the screen after 4 frames on each of the three systems, that is within 67ms.  

I also wrote a small command line tool that uses Apple's AVFoundation framework to play the same sound file when I press a key. The delay in that tool is around 70ms. This feels  more "immediate" than a delay of 110ms or even 160ms.

I don't know much about the signal chain and at which points in the chain various delays occur and their typical magnitudes, so I'm wondering if it's reasonable or not to expect faster response times. My naive expectation would be: 

- Around 30ms latency from the keyboard
- Up to 16.6ms latency for the next physics frame update in Godot
- Up to 15ms latency for Godot's audio output latency (default value)

So around 60ms in total.

As per danluu's keyboard latency test (https://danluu.com/keyboard-latency/), Apple's keyboards are pretty fast. I'm using an Apple USB keyboard on my gaming PC and the builtin keyboard on my MacBook, so that should probably not be an issue.


### Steps to reproduce

This is how I play the sound in GDScript:

```gdscript
extends Node2D

var sound = preload("res://sound.wav")
var player
@export var sprite: Sprite2D

func _ready():
	player = AudioStreamPlayer2D.new()
	player.stream = sound
	add_child(player)

func _physics_process(delta):
	# Assigned to 'M' key
	if Input.is_action_just_pressed("tap"):
		player.play()
		sprite.visible = !sprite.visible
```

See also the attached example project.


### Minimal reproduction project (MRP)

[InputLatencyTest.zip](https://github.com/user-attachments/files/16618761/InputLatencyTest.zip)