How do I use the AudioStreamPolyphonic/How can I audio pool

Godot Version

4.6

Question strong text

I’m trying to make a system where an object can request a sound to be played instead of having its own sound player to save performance since there can be hundreds or thousands of these. My Idea was to use the Polyphonic to do this, though I don’t know exactly how it works. If there’s another way I can achieve audio pooling or something please tell me.

If its the exact same sound that you want to play multiple times, you can increase max_polyphony on any AudioStreamPlayer and call its play() function multiple times.

For different AudioStreams or slightly modified ones (like different volume or pitch), you can use AudioStreamPolyphonic: Set the AudioStreamPlayer’s stream to AudioStreamPolyphonic, start playing it, then call get_stream_playback() to get the playback.

extends AudioStreamPlayer

var playback: AudioStreamPlaybackPolyphonic


func _ready() -> void:
	if stream is AudioStreamPolyphonic:
		play()
		playback = get_stream_playback()

On this playback, you can call play_stream() with the AudioStream as first argument (and multiple other arguments to modify the volume, pitch etc.):

	playback.play_stream(my_audio_stream, ...)