Skip to content

Commit

Permalink
SoundVolume distinct from GlobalVolume. Move sound values into UserSe…
Browse files Browse the repository at this point in the history
…ttings.
  • Loading branch information
alzeih committed Jul 14, 2010
1 parent f28eb79 commit 6b4a193
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
4 changes: 3 additions & 1 deletion OpenRA.Game/GameRules/UserSettings.cs
Expand Up @@ -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
Expand Down
58 changes: 37 additions & 21 deletions OpenRA.Game/Sound.cs
Expand Up @@ -19,6 +19,7 @@
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using OpenRA.FileFormats;
using OpenRA.Traits;
Expand All @@ -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));
Expand All @@ -51,8 +47,6 @@ public static void Initialize()
soundEngine = new OpenAlSoundEngine();
sounds = new Cache<string, ISoundSource>(LoadSound);
music = null;
soundVolume = soundEngine.Volume;
musicVolume = soundEngine.Volume;
paused = false;
stopped = false;
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Widgets/Delegates/SettingsMenuDelegate.cs
Expand Up @@ -45,8 +45,8 @@ public SettingsMenuDelegate()
var audio = bg.GetWidget("AUDIO_PANE");

var soundslider = audio.GetWidget<SliderWidget>("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<SliderWidget>("MUSIC_VOLUME");
musicslider.OnChange += x => { Sound.MusicVolume = x; };
Expand Down

0 comments on commit 6b4a193

Please sign in to comment.