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 4 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,138 @@ | ||
|
|
||
| using System; | ||
| using C7Engine; | ||
| using Godot; | ||
| using Serilog; | ||
|
|
||
| [GlobalClass] | ||
| public partial class AudioManager : Node { | ||
| private ILogger log; | ||
|
|
||
| [Export] AudioStreamPlayer musicPlayer; | ||
| [Export] AudioStreamPlayer sfxAudioPlayer; | ||
| [Export] AudioStreamPlayer uiAudioPlayer; | ||
| [Export] AudioStreamPlayer ambienceAudioPlayer; | ||
|
|
||
| private PolyphonicAudioPlayer _polyMusicPlayer; | ||
| private PolyphonicAudioPlayer _polySfxAudioPlayer; | ||
| private PolyphonicAudioPlayer _polyUiAudioPlayer; | ||
| private PolyphonicAudioPlayer _polyAmbienceAudioPlayer; | ||
|
|
||
| private static string AudioSettingsSection = "audio"; | ||
|
|
||
| public static string MusicBus = "Music"; | ||
| public static string SfxAudioBus = "Sfx"; | ||
| public static string UIAudioBus = "UI"; | ||
| public static string AmbienceAudioBus = "Ambience"; | ||
|
|
||
| private bool musicEnabled = true; | ||
| private bool sfxAudioEnabled = true; | ||
| private bool uiAudioEnabled = true; | ||
| private bool ambienceAudioEnabled = true; | ||
|
|
||
| public override void _Ready() { | ||
| log = LogManager.ForContext<AudioManager>(); | ||
|
|
||
| musicEnabled = ConfigureVolume("musicVolume", MusicBus); | ||
| _polyMusicPlayer = new PolyphonicAudioPlayer(musicPlayer, 2); | ||
|
|
||
| sfxAudioEnabled = ConfigureVolume("sfxAudioVolume", SfxAudioBus); | ||
| _polySfxAudioPlayer = new PolyphonicAudioPlayer(sfxAudioPlayer, 32); | ||
|
|
||
| uiAudioEnabled = ConfigureVolume("uiAudioVolume", UIAudioBus); | ||
| _polyUiAudioPlayer = new PolyphonicAudioPlayer(uiAudioPlayer, 8); | ||
|
|
||
| ambienceAudioEnabled = ConfigureVolume("ambienceAudioVolume", AmbienceAudioBus); | ||
| _polyAmbienceAudioPlayer = new PolyphonicAudioPlayer(ambienceAudioPlayer, 8); | ||
| } | ||
|
|
||
| private bool ConfigureVolume(string volumeKey, string audioBus) { | ||
| try { | ||
| string volume = C7Settings.GetSettingValue(AudioSettingsSection, volumeKey); | ||
| float volumeDb = LogicalVolumeAsDecibel(volume, volumeKey); | ||
|
|
||
| if (volumeDb == float.MinValue) { | ||
| return false; | ||
| } | ||
|
|
||
| log.Debug("setting {volumeKey} to {volume}, which is {offset} decibel (offset)", | ||
| volumeKey, volume, volumeDb); | ||
|
|
||
| int busIndex = AudioServer.GetBusIndex(audioBus); | ||
| AudioServer.SetBusVolumeDb(busIndex, volumeDb); | ||
|
|
||
| return true; | ||
| } catch (ApplicationException ex) { | ||
| log.Error(ex, "could not configure {volumeKey}", volumeKey); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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, string volumeKey) { | ||
| if (volume == null) { | ||
| //First run. Save the setting. | ||
| C7Settings.SetValue(AudioSettingsSection, volumeKey, "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 | ||
|
|
||
| private long currentMusicId = AudioStreamPlaybackPolyphonic.InvalidId; | ||
|
|
||
| public void PlayMusic(string configKey) { | ||
| AudioStream stream = AudioLoader.Load(configKey); | ||
|
|
||
| if (stream == null) | ||
| return; | ||
|
|
||
| if (currentMusicId != AudioStreamPlaybackPolyphonic.InvalidId) { | ||
| _polyMusicPlayer.Stop(currentMusicId); | ||
| } | ||
|
|
||
| currentMusicId = _polyMusicPlayer.Play(stream); | ||
| } | ||
|
|
||
| public void StopMusic() { | ||
| _polyMusicPlayer.StopAll(); | ||
| } | ||
|
|
||
| public void PlaySfxAudio(string configKey) { | ||
| var stream = AudioLoader.Load(configKey); | ||
| if (stream != null) | ||
| _polySfxAudioPlayer.Play(stream); | ||
| } | ||
|
|
||
| public void PlayUIAudio(string configKey) { | ||
| var stream = AudioLoader.Load(configKey); | ||
| if (stream != null) | ||
| _polyUiAudioPlayer.Play(stream); | ||
| } | ||
|
|
||
| public void PlayAmbienceAudio(string configKey) { | ||
| var stream = AudioLoader.Load(configKey); | ||
| if (stream != null) | ||
| _polyAmbienceAudioPlayer.Play(stream); | ||
| } | ||
| } |
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", "sfxAudioPlayer", "uiAudioPlayer", "ambienceAudioPlayer")] | ||
| script = ExtResource("1_l0aok") | ||
| musicPlayer = NodePath("MusicPlayer") | ||
| sfxAudioPlayer = NodePath("SfxAudioPlayer") | ||
| uiAudioPlayer = NodePath("UIAudioPlayer") | ||
| ambienceAudioPlayer = NodePath("AmbienceAudioPlayer") | ||
|
|
||
| [node name="MusicPlayer" type="AudioStreamPlayer" parent="."] | ||
| bus = &"Music" | ||
|
|
||
| [node name="SfxAudioPlayer" type="AudioStreamPlayer" parent="."] | ||
| bus = &"Sfx" | ||
|
|
||
| [node name="UIAudioPlayer" type="AudioStreamPlayer" parent="."] | ||
| bus = &"UI" | ||
|
|
||
| [node name="AmbienceAudioPlayer" 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using Godot; | ||
|
|
||
| public class PolyphonicAudioPlayer { | ||
| private readonly AudioStreamPlayer player; | ||
| private readonly AudioStreamPlaybackPolyphonic playback; | ||
|
|
||
| public PolyphonicAudioPlayer(AudioStreamPlayer player, int polyphony = 32) { | ||
| this.player = player; | ||
|
|
||
| player.Stream = new AudioStreamPolyphonic { | ||
| Polyphony = polyphony | ||
| }; | ||
|
|
||
| player.Play(); | ||
|
|
||
| playback = player.GetStreamPlayback() | ||
| as AudioStreamPlaybackPolyphonic; | ||
| } | ||
|
|
||
| public long Play(AudioStream stream) { | ||
| return playback.PlayStream(stream); | ||
| } | ||
|
|
||
| public void Stop(long id) { | ||
| playback.StopStream(id); | ||
| } | ||
|
|
||
| public void StopAll() { | ||
| player.Stop(); | ||
| } | ||
| } |
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.