context: #region Copyright & License Information
  /*
   * Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
   * This file is part of OpenRA, which is free software. It is made
   * available to you under the terms of the GNU General Public License
   * as published by the Free Software Foundation. For more information,
   * see COPYING.
   */
  #endregion
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using OpenRA.FileFormats;
  using OpenRA.GameRules;
  using OpenRA.Traits;
  using Tao.OpenAl;
  
  namespace OpenRA
  {
  	public static class Sound
  	{
  		static ISoundEngine soundEngine;
  		static Cache<string, ISoundSource> sounds;
  		static ISoundSource rawSource;
  		static ISound music;
  		static ISound video;
  		static MusicInfo currentMusic;
  
  		static ISoundSource LoadSound(string filename)
  		{
  			if (!FileSystem.Exists(filename))
  			{
  				Log.Write("debug", "LoadSound, file does not exist: {0}", filename);
  				return null;
  			}
  
  			return LoadSoundRaw(AudLoader.LoadSound(FileSystem.Open(filename)));
  		}
  
  		static ISoundSource LoadSoundRaw(byte[] rawData)
  		{
  			return soundEngine.AddSoundSourceFromMemory(rawData, 1, 16, 22050);
  		}
  
  		static ISoundEngine CreateEngine(string engine)
  		{
  			engine = Game.Settings.Server.Dedicated?"Null":engine;
  			switch (engine)
  			{	/* todo: if someone cares about pluggable crap here, ship this out */
  			case "AL": return new OpenAlSoundEngine();
  			case "Null": return new NullSoundEngine();
  
  			default:
  				throw new InvalidOperationException("Unsupported sound engine: {0}".F (engine));
  			}
  		}
  
  		public static void Create(string engine)
  		{
  			soundEngine = CreateEngine(engine);
  		}
  
  		public static void Initialize()
  		{
  			sounds = new Cache<string, ISoundSource>(LoadSound);
  			music = null;
  			currentMusic = null;
  			video = null;
  		}
  
  		public static void SetListenerPosition(float2 position) { soundEngine.SetListenerPosition(position); }
  
  		static ISound Play(Player player, string name, bool headRelative, PPos pos, float volumeModifier)
  		{
  			if (String.IsNullOrEmpty(name))
  				return null;
  			if (player != null && player != player.World.LocalPlayer)
  				return null;
  
  			return soundEngine.Play2D(sounds[name],
  				false, headRelative, pos.ToFloat2(),
  				InternalSoundVolume * volumeModifier, true);
  		}
  
  		public static ISound Play(string name) { return Play(null, name, true, PPos.Zero, 1); }
  		public static ISound Play(string name, PPos pos) { return Play(null, name, false, pos, 1); }
  		public static ISound Play(string name, float volumeModifier) { return Play(null, name, true, PPos.Zero, volumeModifier); }
  		public static ISound Play(string name, PPos pos, float volumeModifier) { return Play(null, name, false, pos, volumeModifier); }
  		public static ISound PlayToPlayer(Player player, string name) { return Play(player, name, true, PPos.Zero, 1); }
  		public static ISound PlayToPlayer(Player player, string name, PPos pos) { return Play(player, name, false, pos, 1); }
  
  		public static void PlayVideo(byte[] raw)
  		{
  			rawSource = LoadSoundRaw(raw);
  			video = soundEngine.Play2D(rawSource, false, true, float2.Zero, InternalSoundVolume, false);
  		}
  
  		public static void PlayVideo()
  		{
  			if (video != null)
  				soundEngine.PauseSound(video, false);
  		}
  
  		public static void PauseVideo()
  		{
  			if (video != null)
  				soundEngine.PauseSound(video, true);
  		}
  
  		public static void StopVideo()
  		{
  			if (video != null)
  				soundEngine.StopSound(video);
  		}
  
  		public static void Tick()
  		{
  			// Song finished
  			if (MusicPlaying && !music.Playing)
  			{
  				StopMusic();
  				OnMusicComplete();
  			}
  		}
  
  		static Action OnMusicComplete;
  		public static bool MusicPlaying { get; private set; }
  		public static MusicInfo CurrentMusic { get { return currentMusic; } }
  
  		public static void PlayMusic(MusicInfo m)
  		{
  			PlayMusicThen(m, () => { });
  		}
  
  		public static void PlayMusicThen(MusicInfo m, Action then)
  		{
  			if (m == null || !m.Exists)
  				return;
  
  			OnMusicComplete = then;
  
  			if (m == currentMusic && music != null)
  			{
  				soundEngine.PauseSound(music, false);
  				return;
  			}
  			StopMusic();
  
  			var sound = sounds[m.Filename];
  			if (sound == null) return;
  
  			music = soundEngine.Play2D(sound, false, true, float2.Zero, MusicVolume, false);
  			currentMusic = m;
  			MusicPlaying = true;
  		}
  
  		public static void PlayMusic()
  		{
  			if (music == null)
  				return;
  			MusicPlaying = true;
  			soundEngine.PauseSound(music, false);
  		}
  
  		public static void StopSound(ISound sound)
  		{
  			if (sound != null)
  				soundEngine.StopSound(sound);
  		}
  
  		public static void StopMusic()
  		{
  			if (music != null)
  				soundEngine.StopSound(music);
  
  			MusicPlaying = false;
  			currentMusic = null;
  		}
  
  		public static void PauseMusic()
  		{
  			if (music == null)
  				return;
  
  			MusicPlaying = false;
  			soundEngine.PauseSound(music, true);
  		}
  
  		public static float GlobalVolume
  		{
  			get { return soundEngine.Volume; }
  			set { soundEngine.Volume = value; }
  		}
  
  		static float soundVolumeModifier = 1.0f;
  		public static float SoundVolumeModifier
  		{
  			get { return soundVolumeModifier; }
  			set
  			{
  				soundVolumeModifier = value;
  				soundEngine.SetSoundVolume(InternalSoundVolume, music, video);
  			}
  		}
  
  		static float InternalSoundVolume { get { return SoundVolume * soundVolumeModifier; } }
  		public static float SoundVolume
  		{
  			get { return Game.Settings.Sound.SoundVolume; }
  			set
  			{
  				Game.Settings.Sound.SoundVolume = value;
  				soundEngine.SetSoundVolume(InternalSoundVolume, music, video);
  			}
  		}
  
  		public static float MusicVolume
  		{
  			get { return Game.Settings.Sound.MusicVolume; }
  			set
  			{
  				Game.Settings.Sound.MusicVolume = value;
  
gt: 				if (music != null)