Skip to content

Add global AudioManager - #1009

Merged
ajhalme merged 6 commits into
C7-Game:Developmentfrom
ajhalme:global-audio
Oct 3, 2026
Merged

ajhalme merged 6 commits into
C7-Game:Developmentfrom
ajhalme:global-audio

Conversation

@ajhalme

@ajhalme ajhalme commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Adds a global AudioManager class, wires it up to Godot autoloading.

Enables audio playback across scene changes, most notably in the main game menus.

Also splits music and sound effects on separate audio buses, for independent volume control.

@WildWeazel

Copy link
Copy Markdown
Member

This should cover #32

Is the Sfx channel meant for ambience, UI sounds, unit sounds, all of the above? I wonder if we should break out each of those into separate channels. That would allow preferences to adjust each source independently.

While you're at this, I noticed in the previous implementation that on the first launch after selecting a mode that the music did not play until I toggled the media source, because of the way the scene was loaded. This approach should be able to correct that.

@ajhalme

ajhalme commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

This should cover #32

Yea, maybe. The ticket is a bit loosely defined.

Is the Sfx channel meant for ambience, UI sounds, unit sounds, all of the above? I wonder if we should break out each of those into separate channels. That would allow preferences to adjust each source independently.

I was thinking Music on one bus and everything else on another, but sure, we can add more buses. What set of buses would work for now? We don't want to make this too complicated.

While you're at this, I noticed in the previous implementation that on the first launch after selecting a mode that the music did not play until I toggled the media source, because of the way the scene was loaded. This approach should be able to correct that.

I can look into this. I believe the auto-loader makes it run at once, but I do think there's an auto-play option to set as well.

--

BTW, got into all this after seeing this:
https://www.youtube.com/watch?app=desktop&v=07Kyqqg31FI&t=346s

@stavrosfa

Copy link
Copy Markdown
Contributor

I am asking because I hadn't had the time yet to go throught this too deep.

When we play a sound on 1 bus, do the other sounds that are currently playing stop? This perhaps is somewhat redudant for the music bus let's say, but the sfx bus should be able to play like a lot of sounds at once. A settler's building a city sfx alone has like 4 sounds that play at once.
I remember doing tests that pretty much were spawning threads and this was achievable, but the way I did it was a hack and I only did this to verify I was reading the .amb files properly.
Another approach is object pooling, but that was in Unity and I am not sure if it can be done here.

For buses, I'd say, 1 for music, 1 for ambience, 1 for UI, and 1 for sfx. I don't think I am forgetting any major ones here.

In your experience implamenting this, does adding buses make it more complicated? Or is it as easy as adding two more in there and that's that?

Obviously we don't need to implement these right way, but since this is the PR that introduces the Global Audio Manager, we should at least somewhat plan for the future of it.

@ajhalme

ajhalme commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

A bus can handle multiple simultaneous inputs: I think of it like a "sink" for audio output. Previously everything was going to the default "sink", the buses just allow separate "sinks" with individual volume control, etc.

The limitation is the audioplayer. If we have the same player trying to play multiple audio streams, I'm not sure what happens when the stream is swapped on the fly. I believe it should be possible to configure one player for polyphony, so "in flight" clips run to completion while the new stuff is loaded in for play.

Multiple audio players targeting the same bus definitely works, as this is what we were doing previously with the default master bus. So we can change the implementation of the sound playing such that it, for example, always spawns a new audio player. But that's probably overkill - hence the comment about a play queue and a pool of players.

After all, there's only so many Hawk.wav streams I want ringing at the same time at any one point!

Adding buses is not complicated at all, we can add and remove buses very easily. Perhaps a better question is: To what extent do we want to centralise audio playback? We can just have independent audio players all over and have them target a specific bus, OR we can try and have all audio calls go through the central hub, the AudioManager. Both have their pros and cons. If we centralise, I've implemented these helper functions (PlayMusic, PlaySound, etc.), so I guess we should extend that interface to cover all the buses.

@ajhalme

ajhalme commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Added extra buses for UI and Ambience.

--

The polyphony issue can be handled elegantly with native Godot support:
https://docs.godotengine.org/en/4.4/classes/class_audiostreampolyphonic.html

The robot has a good suggestion for how to make this happen without changing the AudioManager interface - I'll look into it.

(And we should still be OK to mix and match, i.e., have random AudioStreamPlayers blast stuff into target buses past the AudioManager, as needed.)

@stavrosfa

Copy link
Copy Markdown
Contributor

After all, there's only so many Hawk.wav streams I want ringing at the same time at any one point!

I have to say, the hawk sound and this here https://youtu.be/I_qxPRShwUk?list=PLC50F7DF5BA99F078 are the most defining sounds for me of this game 😄

Ok, regarding implentation, I think that we need both the pool (pretty much for the sfx bus), and the queue for the music/ambiance buses. The API can be refined as we move along, the simple PlayMusic, PlaySFX, etc is fine for now I think.

You are the one defining the scope of this PR, and I think a simple interface as a first step is more than fine.

@stavrosfa

Copy link
Copy Markdown
Contributor

The polyphony issue can be handled elegantly with native Godot support:
https://docs.godotengine.org/en/4.4/classes/class_audiostreampolyphonic.html

I remember seeing this at some point, I think there was something there I didn't like, but I can't remember what it was, feel free to explore.

@stavrosfa stavrosfa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is regarding the last commit, not a general review

Comment thread C7/Audio/AudioManager.cs Outdated
Comment thread C7/Audio/AudioManager.cs Outdated
Comment thread C7/Audio/AudioManager.cs Outdated
Comment thread C7/Audio/AudioManager.cs Outdated
# Conflicts:
#	C7/UIElements/MainMenu/main_menu.tscn
@ajhalme

ajhalme commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Added basic polyphonic players on each bus, cleaned up the names.

I'm happy with how this looks now.

@stavrosfa stavrosfa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested it, but other than my one comment it looks good.

I will try to set up a test tomorrow using some of the .amb files from the units that contain multiple sfx, and check it out further.

Comment thread C7/UIElements/MainMenu/MainMenu.cs Outdated
@ajhalme

ajhalme commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Bah, the start-stop music is busted, have to look at it more

@ajhalme

ajhalme commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

I think I got it, just some audio stream life cycle stuff. Music starts and stops fine going into the game (via different routes) and coming back to main menu. From here, should be straightforward enough to add some in-game music playlists, etc.

Even if there's some corner case that isn't handled correctly, I feel like this setup gives us pretty good control to fix or work around.

(Note that all the audio NOT handled by AudioManager is pushing stuff into the master bus, which fine - switching things to AudioManager can be piecemeal, or maybe a single PR can migrate everything over so we have good control across the codebase.)

@ajhalme
ajhalme merged commit 4fd1ec6 into C7-Game:Development Oct 3, 2026
3 checks passed
@ajhalme
ajhalme deleted the global-audio branch October 3, 2026 10:55
@ajhalme ajhalme mentioned this pull request Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants