Repository navigation
Add global AudioManager #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add global AudioManager #1009
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ea618e
Add global AudioManager, separate audio buses for music & sfx
ajhalme ab0bb56
Add UI and Ambience buses
ajhalme 78a74b3
Polyphonic audio, cleanup
ajhalme e12ec92
Merge branch 'Development' into global-audio
ajhalme d9d7d06
Sequencing, cleanup
ajhalme 04eb864
Fix music start-stop-start life cycling
ajhalme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| uid://d3ppdw6qcpc4k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
|
|
||
| using System; | ||
| using C7Engine; | ||
| using Godot; | ||
| using Serilog; | ||
|
|
||
| [GlobalClass] | ||
| public partial class AudioManager : Node { | ||
| private ILogger log; | ||
|
|
||
| [Export] AudioStreamPlayer musicPlayer; | ||
| [Export] AudioStreamPlayer sfxPlayer; | ||
| [Export] AudioStreamPlayer uiPlayer; | ||
| [Export] AudioStreamPlayer ambiencePlayer; | ||
|
|
||
| public static string MusicBus = "Music"; | ||
| public static string SfxBus = "Sfx"; | ||
| public static string UIBus = "UI"; | ||
| public static string AmbienceBus = "Ambience"; | ||
|
|
||
| private bool musicEnabled = true; | ||
| private bool soundEnabled = true; | ||
| private bool uiEnabled = true; | ||
| private bool ambienceEnabled = true; | ||
|
|
||
| public override void _Ready() { | ||
| log = LogManager.ForContext<AudioManager>(); | ||
| ConfigureMusic(); | ||
| ConfigureSound(); | ||
|
ajhalme marked this conversation as resolved.
Outdated
|
||
| ConfigureUI(); | ||
| ConfigureAmbience(); | ||
| } | ||
|
|
||
| private void ConfigureMusic() { | ||
| try { | ||
| string volume = C7Settings.GetSettingValue("audio", "musicVolume"); | ||
| float volumeDb = LogicalVolumeAsDecibel(volume); | ||
|
|
||
| if (volumeDb == float.MinValue) { | ||
| musicEnabled = false; | ||
| } | ||
|
|
||
| if (musicEnabled) { | ||
| log.Debug("setting music volume to {volume}, which is {offset} decibel (offset)", volume, volumeDb); | ||
| int busIndex = AudioServer.GetBusIndex(MusicBus); | ||
| AudioServer.SetBusVolumeDb(busIndex, volumeDb); | ||
| } | ||
| } catch (ApplicationException ex) { | ||
| log.Error(ex, "could not configure music"); | ||
| } | ||
| } | ||
|
|
||
| private void ConfigureSound() { | ||
| try { | ||
| string volume = C7Settings.GetSettingValue("audio", "soundVolume"); | ||
| float volumeDb = LogicalVolumeAsDecibel(volume); | ||
|
|
||
| if (volumeDb == float.MinValue) { | ||
| soundEnabled = false; | ||
| } | ||
|
|
||
| if (soundEnabled) { | ||
| log.Debug("setting sound volume to {volume}, which is {offset} decibel (offset)", volume, volumeDb); | ||
| int busIndex = AudioServer.GetBusIndex(SfxBus); | ||
| AudioServer.SetBusVolumeDb(busIndex, volumeDb); | ||
| } | ||
| } catch (ApplicationException ex) { | ||
| log.Error(ex, "could not configure sound"); | ||
| } | ||
| } | ||
|
|
||
| private void ConfigureUI() { | ||
| try { | ||
| string volume = C7Settings.GetSettingValue("audio", "uiVolume"); | ||
| float volumeDb = LogicalVolumeAsDecibel(volume); | ||
|
|
||
| if (volumeDb == float.MinValue) { | ||
| uiEnabled = false; | ||
| } | ||
|
|
||
| if (uiEnabled) { | ||
| log.Debug("setting UI volume to {volume}, which is {offset} decibel (offset)", volume, volumeDb); | ||
| int busIndex = AudioServer.GetBusIndex(UIBus); | ||
| AudioServer.SetBusVolumeDb(busIndex, volumeDb); | ||
| } | ||
| } catch (ApplicationException ex) { | ||
| log.Error(ex, "could not configure UI audio"); | ||
| } | ||
| } | ||
|
|
||
| private void ConfigureAmbience() { | ||
| try { | ||
| string volume = C7Settings.GetSettingValue("audio", "ambienceVolume"); | ||
| float volumeDb = LogicalVolumeAsDecibel(volume); | ||
|
|
||
| if (volumeDb == float.MinValue) { | ||
| ambienceEnabled = false; | ||
| } | ||
|
|
||
| if (ambienceEnabled) { | ||
| log.Debug("setting ambience volume to {volume}, which is {offset} decibel (offset)", volume, volumeDb); | ||
| int busIndex = AudioServer.GetBusIndex(AmbienceBus); | ||
| AudioServer.SetBusVolumeDb(busIndex, volumeDb); | ||
| } | ||
| } catch (ApplicationException ex) { | ||
| log.Error(ex, "could not configure ambience audio"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Godot uses a decibel offset volume system, described at https://docs.godotengine.org/en/stable/tutorials/audio/audio_buses.html | ||
| * This is what audio professionals would use, but is not intuitive to end users. | ||
| * In this system, a 6db difference halves or doubles the volume. | ||
| * Our users are probably more used to a 0% to 100% system. | ||
| * So this method converts between them. | ||
| */ | ||
| private float LogicalVolumeAsDecibel(string volume) { | ||
| if (volume == null) { | ||
| //First run. Save the setting. | ||
| C7Settings.SetValue("audio", "musicVolume", "100"); | ||
| C7Settings.SaveSettings(); | ||
| return 0; | ||
| } | ||
| int userVolumeSetting = int.Parse(volume); | ||
| if (userVolumeSetting == 100) { | ||
| return 0; | ||
| } else if (userVolumeSetting == 0) { | ||
| return float.MinValue; | ||
| } else { | ||
| //Conversion math based on https://stackoverflow.com/a/37810295/3534605 | ||
| return 20.0f * (float)(Math.Log10(userVolumeSetting / 100.0f)); | ||
| } | ||
| } | ||
|
|
||
| // TODO: playlists, mixing, transitions | ||
| // See: https://www.youtube.com/watch?app=desktop&v=07Kyqqg31FI&t=346s | ||
|
|
||
| // TODO: polyphony via AudioStreamPolyphonic + AudioStreamPlaybackPolyphonic | ||
| // https://docs.godotengine.org/en/4.0/classes/class_audiostreampolyphonic.html | ||
|
|
||
| public void PlayMusic(string configKey) { | ||
| AudioStream stream = AudioLoader.Load(configKey); | ||
|
|
||
| if (stream == null) | ||
| return; | ||
|
|
||
| musicPlayer.Stream = stream; | ||
| musicPlayer.Play(); | ||
| } | ||
|
|
||
| public void StopMusic() { | ||
| musicPlayer.Stop(); | ||
| } | ||
|
|
||
| public void PlaySound(string configKey) { | ||
|
ajhalme marked this conversation as resolved.
Outdated
|
||
| AudioStream stream = AudioLoader.Load(configKey); | ||
|
|
||
| if (stream == null) | ||
| return; | ||
|
|
||
| sfxPlayer.Stream = stream; | ||
| sfxPlayer.Play(); | ||
| } | ||
|
|
||
| public void PlayUI(string configKey) { | ||
|
ajhalme marked this conversation as resolved.
Outdated
|
||
| AudioStream stream = AudioLoader.Load(configKey); | ||
|
|
||
| if (stream == null) | ||
| return; | ||
|
|
||
| uiPlayer.Stream = stream; | ||
| uiPlayer.Play(); | ||
| } | ||
|
|
||
| public void PlayAmbience(string configKey) { | ||
| AudioStream stream = AudioLoader.Load(configKey); | ||
|
|
||
| if (stream == null) | ||
| return; | ||
|
|
||
| ambiencePlayer.Stream = stream; | ||
| ambiencePlayer.Play(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| uid://eyk3k7dd7tcy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [gd_scene load_steps=2 format=3 uid="uid://4hh28tdyqvfg"] | ||
|
|
||
| [ext_resource type="Script" uid="uid://eyk3k7dd7tcy" path="res://Audio/AudioManager.cs" id="1_l0aok"] | ||
|
|
||
| [node name="AudioManager" type="Node" node_paths=PackedStringArray("musicPlayer", "sfxPlayer", "uiPlayer", "ambiencePlayer")] | ||
| script = ExtResource("1_l0aok") | ||
| musicPlayer = NodePath("MusicPlayer") | ||
| sfxPlayer = NodePath("SfxPlayer") | ||
| uiPlayer = NodePath("UIPlayer") | ||
| ambiencePlayer = NodePath("AmbiencePlayer") | ||
|
|
||
| [node name="MusicPlayer" type="AudioStreamPlayer" parent="."] | ||
| bus = &"Music" | ||
|
|
||
| [node name="SfxPlayer" type="AudioStreamPlayer" parent="."] | ||
| bus = &"Sfx" | ||
|
|
||
| [node name="UIPlayer" type="AudioStreamPlayer" parent="."] | ||
| bus = &"UI" | ||
|
|
||
| [node name="AmbiencePlayer" type="AudioStreamPlayer" parent="."] | ||
| bus = &"Ambience" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| [gd_resource type="AudioBusLayout" format=3 uid="uid://cw11tyqth76rn"] | ||
|
|
||
| [resource] | ||
| bus/1/name = &"Music" | ||
| bus/1/solo = false | ||
| bus/1/mute = false | ||
| bus/1/bypass_fx = false | ||
| bus/1/volume_db = 0.0 | ||
| bus/1/send = &"Master" | ||
| bus/2/name = &"Sfx" | ||
| bus/2/solo = false | ||
| bus/2/mute = false | ||
| bus/2/bypass_fx = false | ||
| bus/2/volume_db = 0.0 | ||
| bus/2/send = &"Master" | ||
| bus/3/name = &"UI" | ||
| bus/3/solo = false | ||
| bus/3/mute = false | ||
| bus/3/bypass_fx = false | ||
| bus/3/volume_db = 0.0 | ||
| bus/3/send = &"Master" | ||
| bus/4/name = &"Ambience" | ||
| bus/4/solo = false | ||
| bus/4/mute = false | ||
| bus/4/bypass_fx = false | ||
| bus/4/volume_db = 0.0 | ||
| bus/4/send = &"Master" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.