Godot: Play multiple sounds from one AudioStreamPlayer

Created on 3 Aug 2019  Â·  10Comments  Â·  Source: godotengine/godot

It's really annoying if you have a big amount of soundeffect files and you have to setup multiple AudioStreamPlayers to play them. It would be great if you could write in a script something like this: $SFX.play( "scream"). I know many people on Discord who also want that, so I thought this would be a great milestone for 3.2.

archived enhancement feature proposal audio

Most helpful comment

Not sure why it was removed.

Because audio engine got a total overhaul and not everything was reimplemented, probably.

All 10 comments

You can write a function to do this:

func play_sound(player : AudioStreamPlayer, sfx : AudioStream):
    player.stream = sfx
    player.play()

But yeah, I always have a function like that in my project, so having it built-in would be useful.

This was present in 2.1 actually:

sample_library

Not sure why it was removed.

Not sure why it was removed.

Because audio engine got a total overhaul and not everything was reimplemented, probably.

something like this: $SFX.play( "scream")

You can simply collect the streams, eg. in an array: export(Array, AudioStream) var a_streams.
Or even as a gdscript beginner just put them with the UI as children of a node and then type $Sounds/scream.play() $Sounds/cry.play() …

There's still an overhead of having to set up streams that way. Depending on the workflow, the user would probably like to import all their sounds into the SampleLibrary and reuse the same resource over multiple scenes.

I guess the other reason why it was removed might be related to how samples can be used in the updated animation system where you can add an Audio Track pointing to AudioStreamSample node inside the scene which might not be trivial to do with SampleLibrary resource.

Then make the AudioStreamPlayer a scene and attach a simple gdscript to it.

I think with a resource like SampleLibrary it would be possible to separate your samples into certain categories more easily, like explosion sounds, combat sounds etc. Then you can adjust the volume of each/group of samples at run-time in order to let the player configure the volume of those separately. That way, if such resource is modified and reused in multiple scenes, it's going to propagate these changes to all instances since it's shared by default, not duplicated.

The same could be done with the scene/node based system, but since instancing would introduce multiple copies of those AudioStreamPlayer2D nodes, then you'd likely have to add those nodes to groups per category and do some traversal via get_nodes_in_group or other method to manage volume of each manually. One could make such node a singleton perhaps, but I'm not sure how would this cause interrupting of played sounds with multiple instances using the same audio player.

Each approach has it's pros and cons which could be decided per project really.

...configure the volume of those separately.

This can be handled using multiple audio busses in the new system, no?

This can be handled using multiple audio buses in the new system, no?

Yes, though the point I'm trying to make is that individual AudioStreamPlayer2D nodes might not be as flexible as using a centralized SampleLibrary resource even with the bus system available.

For instance I'm not sure how easy would it be to selectively stop/play certain groups of sounds with the bus system alone (changing volume is possible though).

I haven't delved into this on concrete use cases, but I suspect I'd stumble upon some drawbacks eventually as the project grows with time, so these are just my two cents.

I am gonna close the issue for the following reason: This is suited better for an addon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gonzo191 picture gonzo191  Â·  3Comments

Spooner picture Spooner  Â·  3Comments

testman42 picture testman42  Â·  3Comments

blurymind picture blurymind  Â·  3Comments

mefihl picture mefihl  Â·  3Comments