SoundVolume distinct from GlobalVolume. Move sound values into UserSettings.

This commit is contained in:
alzeih
2010-07-14 13:57:42 +12:00
parent f28eb7971b
commit 6b4a193c7c
3 changed files with 42 additions and 24 deletions

View File

@@ -40,7 +40,9 @@ namespace OpenRA.GameRules
public int2 FullscreenSize = new int2(0,0); public int2 FullscreenSize = new int2(0,0);
public int2 WindowedSize = new int2(1024,768); public int2 WindowedSize = new int2(1024,768);
//Sound Settings
public float SoundVolume = 0.5f;
public float MusicVolume = 0.5f;
public bool MusicPlayer = true; public bool MusicPlayer = true;
// Internal game settings // Internal game settings

View File

@@ -19,6 +19,7 @@
#endregion #endregion
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Traits; using OpenRA.Traits;
@@ -35,11 +36,6 @@ namespace OpenRA
static bool paused; static bool paused;
static bool stopped; static bool stopped;
//TODO: read these from somewhere?
static float soundVolume;
static float musicVolume;
static ISoundSource LoadSound(string filename) static ISoundSource LoadSound(string filename)
{ {
var data = AudLoader.LoadSound(FileSystem.Open(filename)); var data = AudLoader.LoadSound(FileSystem.Open(filename));
@@ -51,8 +47,6 @@ namespace OpenRA
soundEngine = new OpenAlSoundEngine(); soundEngine = new OpenAlSoundEngine();
sounds = new Cache<string, ISoundSource>(LoadSound); sounds = new Cache<string, ISoundSource>(LoadSound);
music = null; music = null;
soundVolume = soundEngine.Volume;
musicVolume = soundEngine.Volume;
paused = false; paused = false;
stopped = false; stopped = false;
} }
@@ -65,7 +59,7 @@ namespace OpenRA
return; return;
var sound = sounds[name]; 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) public static void Play(string name, float2 pos)
@@ -74,7 +68,7 @@ namespace OpenRA
return; return;
var sound = sounds[name]; 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) public static void PlayToPlayer(Player player, string name)
@@ -98,8 +92,7 @@ namespace OpenRA
soundEngine.StopSound(music); soundEngine.StopSound(music);
var sound = sounds[name]; var sound = sounds[name];
music = soundEngine.Play2D(sound, true, true, float2.Zero); music = soundEngine.Play2D(sound, true, true, float2.Zero, MusicVolume);
music.Volume = musicVolume;
} }
public static bool MusicPaused public static bool MusicPaused
@@ -122,22 +115,28 @@ namespace OpenRA
} }
} }
public static float Volume public static float GlobalVolume
{ {
get { return soundVolume; } get { return soundEngine.Volume; }
set { soundEngine.Volume = value;}
}
public static float SoundVolume
{
get { return Game.Settings.SoundVolume; }
set set
{ {
soundVolume = value; Game.Settings.SoundVolume = value;
soundEngine.Volume = value; soundEngine.SetSoundVolume(value, music);
} }
} }
public static float MusicVolume public static float MusicVolume
{ {
get { return musicVolume; } get { return Game.Settings.MusicVolume; }
set set
{ {
musicVolume = value; Game.Settings.MusicVolume = value;
if (music != null) if (music != null)
music.Volume = value; music.Volume = value;
} }
@@ -175,13 +174,14 @@ namespace OpenRA
interface ISoundEngine interface ISoundEngine
{ {
ISoundSource AddSoundSourceFromMemory(byte[] data, int channels, int sampleBits, int sampleRate); 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; } float Volume { get; set; }
void PauseSound(ISound sound, bool paused); void PauseSound(ISound sound, bool paused);
void StopSound(ISound sound); void StopSound(ISound sound);
void SetAllSoundsPaused(bool paused); void SetAllSoundsPaused(bool paused);
void StopAllSounds(); void StopAllSounds();
void SetListenerPosition(float2 position); void SetListenerPosition(float2 position);
void SetSoundVolume(float volume, ISound music);
} }
interface ISoundSource {} interface ISoundSource {}
@@ -257,10 +257,10 @@ namespace OpenRA
return new OpenAlSoundSource(data, channels, sampleBits, sampleRate); 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(); 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 public float Volume
@@ -293,6 +293,21 @@ namespace OpenRA
} }
} }
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) public void StopSound(ISound sound)
{ {
@@ -348,7 +363,7 @@ namespace OpenRA
public readonly int source = -1; public readonly int source = -1;
float volume = 1f; 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; if (source == -1) return;
this.source = source; this.source = source;
@@ -361,6 +376,7 @@ namespace OpenRA
Al.alSourcei(source, Al.AL_SOURCE_RELATIVE, relative ? 1 : 0); Al.alSourcei(source, Al.AL_SOURCE_RELATIVE, relative ? 1 : 0);
Al.alSourcef(source, Al.AL_REFERENCE_DISTANCE, 200); Al.alSourcef(source, Al.AL_REFERENCE_DISTANCE, 200);
Al.alSourcef(source, Al.AL_MAX_DISTANCE, 1500); Al.alSourcef(source, Al.AL_MAX_DISTANCE, 1500);
Volume = volume;
Al.alSourcePlay(source); Al.alSourcePlay(source);
} }

View File

@@ -45,8 +45,8 @@ namespace OpenRA.Widgets.Delegates
var audio = bg.GetWidget("AUDIO_PANE"); var audio = bg.GetWidget("AUDIO_PANE");
var soundslider = audio.GetWidget<SliderWidget>("SOUND_VOLUME"); var soundslider = audio.GetWidget<SliderWidget>("SOUND_VOLUME");
soundslider.OnChange += x => { Sound.Volume = x; }; soundslider.OnChange += x => { Sound.SoundVolume = x; };
soundslider.GetOffset = () => { return Sound.Volume; }; soundslider.GetOffset = () => { return Sound.SoundVolume; };
var musicslider = audio.GetWidget<SliderWidget>("MUSIC_VOLUME"); var musicslider = audio.GetWidget<SliderWidget>("MUSIC_VOLUME");
musicslider.OnChange += x => { Sound.MusicVolume = x; }; musicslider.OnChange += x => { Sound.MusicVolume = x; };