diff --git a/OpenRA.Game/GameRules/UserSettings.cs b/OpenRA.Game/GameRules/UserSettings.cs index ab7745dfd700..518c78558e70 100644 --- a/OpenRA.Game/GameRules/UserSettings.cs +++ b/OpenRA.Game/GameRules/UserSettings.cs @@ -40,7 +40,9 @@ public class UserSettings public int2 FullscreenSize = new int2(0,0); public int2 WindowedSize = new int2(1024,768); - + //Sound Settings + public float SoundVolume = 0.5f; + public float MusicVolume = 0.5f; public bool MusicPlayer = true; // Internal game settings diff --git a/OpenRA.Game/Sound.cs b/OpenRA.Game/Sound.cs index b72cf437e8f8..7dc88e33d980 100644 --- a/OpenRA.Game/Sound.cs +++ b/OpenRA.Game/Sound.cs @@ -19,6 +19,7 @@ #endregion using System; +using System.Linq; using System.Collections.Generic; using OpenRA.FileFormats; using OpenRA.Traits; @@ -35,11 +36,6 @@ public static class Sound static bool paused; static bool stopped; - //TODO: read these from somewhere? - static float soundVolume; - static float musicVolume; - - static ISoundSource LoadSound(string filename) { var data = AudLoader.LoadSound(FileSystem.Open(filename)); @@ -51,8 +47,6 @@ public static void Initialize() soundEngine = new OpenAlSoundEngine(); sounds = new Cache(LoadSound); music = null; - soundVolume = soundEngine.Volume; - musicVolume = soundEngine.Volume; paused = false; stopped = false; } @@ -65,7 +59,7 @@ public static void Play(string name) return; var sound = sounds[name]; - soundEngine.Play2D(sound, false, true, float2.Zero); + soundEngine.Play2D(sound, false, true, float2.Zero, SoundVolume); } public static void Play(string name, float2 pos) @@ -74,7 +68,7 @@ public static void Play(string name, float2 pos) return; var sound = sounds[name]; - soundEngine.Play2D(sound, false, false, pos); + soundEngine.Play2D(sound, false, false, pos, SoundVolume); } public static void PlayToPlayer(Player player, string name) @@ -98,8 +92,7 @@ public static void PlayMusic(string name) soundEngine.StopSound(music); var sound = sounds[name]; - music = soundEngine.Play2D(sound, true, true, float2.Zero); - music.Volume = musicVolume; + music = soundEngine.Play2D(sound, true, true, float2.Zero, MusicVolume); } public static bool MusicPaused @@ -122,22 +115,28 @@ public static bool MusicStopped } } - public static float Volume + public static float GlobalVolume + { + get { return soundEngine.Volume; } + set { soundEngine.Volume = value;} + } + + public static float SoundVolume { - get { return soundVolume; } + get { return Game.Settings.SoundVolume; } set { - soundVolume = value; - soundEngine.Volume = value; + Game.Settings.SoundVolume = value; + soundEngine.SetSoundVolume(value, music); } } public static float MusicVolume { - get { return musicVolume; } + get { return Game.Settings.MusicVolume; } set { - musicVolume = value; + Game.Settings.MusicVolume = value; if (music != null) music.Volume = value; } @@ -175,13 +174,14 @@ public static void PlayVoice(string phrase, Actor voicedUnit) interface ISoundEngine { ISoundSource AddSoundSourceFromMemory(byte[] data, int channels, int sampleBits, int sampleRate); - ISound Play2D(ISoundSource sound, bool loop, bool relative, float2 pos); + ISound Play2D(ISoundSource sound, bool loop, bool relative, float2 pos, float volume); float Volume { get; set; } void PauseSound(ISound sound, bool paused); void StopSound(ISound sound); void SetAllSoundsPaused(bool paused); void StopAllSounds(); void SetListenerPosition(float2 position); + void SetSoundVolume(float volume, ISound music); } interface ISoundSource {} @@ -257,10 +257,10 @@ public ISoundSource AddSoundSourceFromMemory(byte[] data, int channels, int samp return new OpenAlSoundSource(data, channels, sampleBits, sampleRate); } - public ISound Play2D(ISoundSource sound, bool loop, bool relative, float2 pos) + public ISound Play2D(ISoundSource sound, bool loop, bool relative, float2 pos, float volume) { int source = GetSourceFromPool(); - return new OpenAlSound(source, (sound as OpenAlSoundSource).buffer, loop, relative, pos); + return new OpenAlSound(source, (sound as OpenAlSoundSource).buffer, loop, relative, pos, volume); } public float Volume @@ -293,6 +293,21 @@ public void SetAllSoundsPaused(bool paused) } } + + public void SetSoundVolume(float volume, ISound music) + { + var sounds = sourcePool.Select(s => s.Key).Where( b => + { + int state; + Al.alGetSourcei(b, Al.AL_SOURCE_STATE, out state); + return ((state == Al.AL_PLAYING || state == Al.AL_PAUSED) && + ((music != null)? b != ((OpenAlSound) music).source : true)); + }).ToList(); + foreach (var s in sounds) + { + Al.alSourcef(s, Al.AL_GAIN, volume); + } + } public void StopSound(ISound sound) { @@ -348,7 +363,7 @@ class OpenAlSound : ISound public readonly int source = -1; float volume = 1f; - public OpenAlSound(int source, int buffer, bool looping, bool relative, float2 pos) + public OpenAlSound(int source, int buffer, bool looping, bool relative, float2 pos, float volume) { if (source == -1) return; this.source = source; @@ -361,6 +376,7 @@ public OpenAlSound(int source, int buffer, bool looping, bool relative, float2 p Al.alSourcei(source, Al.AL_SOURCE_RELATIVE, relative ? 1 : 0); Al.alSourcef(source, Al.AL_REFERENCE_DISTANCE, 200); Al.alSourcef(source, Al.AL_MAX_DISTANCE, 1500); + Volume = volume; Al.alSourcePlay(source); } diff --git a/OpenRA.Game/Widgets/Delegates/SettingsMenuDelegate.cs b/OpenRA.Game/Widgets/Delegates/SettingsMenuDelegate.cs index bd365b35083e..2933cb6f7bb3 100644 --- a/OpenRA.Game/Widgets/Delegates/SettingsMenuDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/SettingsMenuDelegate.cs @@ -45,8 +45,8 @@ public SettingsMenuDelegate() var audio = bg.GetWidget("AUDIO_PANE"); var soundslider = audio.GetWidget("SOUND_VOLUME"); - soundslider.OnChange += x => { Sound.Volume = x; }; - soundslider.GetOffset = () => { return Sound.Volume; }; + soundslider.OnChange += x => { Sound.SoundVolume = x; }; + soundslider.GetOffset = () => { return Sound.SoundVolume; }; var musicslider = audio.GetWidget("MUSIC_VOLUME"); musicslider.OnChange += x => { Sound.MusicVolume = x; };