How to play Polyphonic Audio through code

Godot Version

v4.1.3.stable.mono.official [f06b6836a]

Question

Hi again, this is a simple question because I haven’t been able to find a single tutorial online on how to use this: the AudioStreamPolyphonic. From older tutorials (I assume for Godot 3), it suggests that the resource should have a “tracks” variable that it doesn’t in Godot 4, not even if I try and access it through GDScript.

If someone could let me know how it works, I’d appreciate it a ton. If I misunderstand how you’re meant to do Polyphony through code, also let me know, because I’d prefer keeping audio sources low and playing them through code for how my game’s architecture is set up.

I’m aware the reason why polyphony through code doesn’t work as expected is because when I try and play a new sound through the same source, I am technically swapping out the stream, cutting the sound of prematurely even if the Max Polyphony property is increased above 1. I assume that variable only is considered when you play the same stream multiple times in the same source.

TL;DR: I’m wondering how to you’d polyphonic playback of multiple different audio streams in a single audio stream player using code (preferably C#, but I could hack together a system cross-script with GDScript too).

Thanks in advance,
Ema Beckett

Are you following this part of the manual? AudioStreamPlaybackPolyphonic — Godot Engine (stable) documentation in English
It seems straightforward. You set the stream, then you can play it multiple times. If you need a different stream, you use a different AudioStream.
What is the use case you need polyphony for?

It doesn’t seem that straight-forward for me? That’s sort-of why I posted this to see if someone could explain it :sweat_smile:. My use case for wanting Polyphony is in order to play multiple sound effects part of the same animation in my game.
Would the solution then be to make multiple sound sources for each unique sound stream?

I think you may be overcomplicating it. If you play different sounds from different AudioStream players, the engine will merge the sounds automatically. The polyphony sound interface is for making something like a tracker that plays the same sound at different pitches, or sounds that may have a complex source, like friction sounds, vehicle sounds, machinery sounds. If you’re just playing two different sounds at different times, that has nothing to do with the polyphony stream.

Hello! maybe you already quitted with this topic but Im starting with Godot and Im working on my second newbie game, after a lot of research, this the solution I implemented for playing multiple audios in parallel:
Declare the AudioPlayer:

	private AudioStreamPlayer2D SFXPlayer;

Initialize and configure it in you ready or init method (you can use GetNode(“XXX”) if you have your AudioStreamPlayer in your Scene and not creating it via code):

		SFXPlayer = new AudioStreamPlayer2D();
		SFXPlayer.Bus = "SFX";
		//Max number of sounds in parallel
		SFXPlayer.MaxPolyphony = 20;
		//Needed for playing multiple sounds in parallel, PlaybackPolyphonic assigned to  AudioPlayer's Stream
		SFXPlayer.Stream = new AudioStreamPolyphonic();

Add it to the scene (skip this step if you initialized it via GetNode<>()):

		AddChild(SFXPlayer);

Now use the AudioStreamPlayer2D once so u can assign it to the PlaybackPolyphonic and the first sound its not bugged nor triggers NULL exception:

		//Playing it without AudioStream for later PolyphonicPlayback assignment without errors
		SFXPlayer.Play();

Then in your play method you just need to get your AudioStream resource and play it:

		AudioStream sfx = YOUR AUDIO RESOURCE;
			var playback = (AudioStreamPlaybackPolyphonic)SFXPlayer.GetStreamPlayback();
			playback.PlayStream(sfx, 0, volume, 1, 0, "SFX");

The params are easy: AudioResource,Offset,Volume,Pitch,PlaybackType (default is 0 you can confiure more with AudioServer.PlaybackType enums) and the Bus.
I hope I helped! I was a bit lost at the beginning with audios, i was creating one AudioStreamPlayer2D Node per each effect i wanted in the Scenes xdd bad approach, now I created an Asset Manager and handled the audio logic there, c u!