AudioStreamPlayer Array / Polyphonic Multistream

Godot Version

4.2.1

Question

The AudioStreamPlayer node doesn’t seem to have a native array property in anything other than AudioStreamRandomizer. I’m wondering if there is a way to make one for other stream types, or to utilize the AudioStreamRandomizer to play based on the array position provided in script rather than auto-random/sequential. This could be much more efficient for determining playback based on events while maintaining a clean group structure.

I’m most likely overlooking an easy way to do this! Please Tell Me The Easy Way To Do This!!!

@export an Array[AudioStream] and change the stream based on it.

Example:

extends AudioStreamPlayer


@export var streams:Array[AudioStream]


func _ready() -> void:
	play_index(1)
	await get_tree().create_timer(1).timeout
	play_index(1)
	await get_tree().create_timer(1).timeout
	play_index(0)


func play_index(i:int) -> void:
	stream = streams[i]
	play()

if you need polyphony then you can use an AudioStreamPolyphonic stream and play the streams using its AudioStreamPlaybackPolyphonic

Example:

extends AudioStreamPlayer

@export var streams:Array[AudioStream]

var playback:AudioStreamPlaybackPolyphonic

func _ready() -> void:
	stream = AudioStreamPolyphonic.new()
	play()
	
	playback = get_stream_playback()

	play_index(1)
	await get_tree().create_timer(1).timeout
	play_index(1)
	await get_tree().create_timer(1).timeout
	play_index(0)


func play_index(i:int) -> void:
	playback.play_stream(streams[i])