public static void SwitchMusic() { var suitableMusic = GetSuitableMusicClips(); if (suitableMusic.Count > 1) { targetMusic = suitableMusic.Find(m => m != currentMusic); } }
private static void UpdateMusic() { if (musicClips == null) { return; } List <BackgroundMusic> suitableMusic = GetSuitableMusicClips(); if (suitableMusic.Count == 0) { targetMusic = null; } else if (!suitableMusic.Contains(currentMusic)) { int index = Rand.Int(suitableMusic.Count); if (currentMusic == null || suitableMusic[index].file != currentMusic.file) { targetMusic = suitableMusic[index]; } } if (targetMusic == null || currentMusic == null || targetMusic.file != currentMusic.file) { currMusicVolume = MathHelper.Lerp(currMusicVolume, 0.0f, MusicLerpSpeed); if (currentMusic != null) { Sound.StreamVolume(currMusicVolume); } if (currMusicVolume < 0.01f) { Sound.StopStream(); try { if (targetMusic != null) { Sound.StartStream(targetMusic.file, currMusicVolume); } } catch (FileNotFoundException e) { DebugConsole.ThrowError("Music clip " + targetMusic.file + " not found!", e); } currentMusic = targetMusic; } } else { currMusicVolume = MathHelper.Lerp(currMusicVolume, MusicVolume, MusicLerpSpeed); Sound.StreamVolume(currMusicVolume); } }
public static IEnumerable <object> Init() { OverrideMusicType = null; XDocument doc = XMLExtensions.TryLoadXml("Content/Sounds/sounds.xml"); if (doc == null) { yield return(CoroutineStatus.Failure); } SoundCount = 16 + doc.Root.Elements().Count(); startDrone = Sound.Load("Content/Sounds/startDrone.ogg", false); startDrone.Play(); yield return(CoroutineStatus.Running); waterAmbiences[0] = Sound.Load("Content/Sounds/Water/WaterAmbience1.ogg", false); yield return(CoroutineStatus.Running); waterAmbiences[1] = Sound.Load("Content/Sounds/Water/WaterAmbience2.ogg", false); yield return(CoroutineStatus.Running); flowSounds[0] = Sound.Load("Content/Sounds/Water/FlowSmall.ogg", false); yield return(CoroutineStatus.Running); flowSounds[1] = Sound.Load("Content/Sounds/Water/FlowMedium.ogg", false); yield return(CoroutineStatus.Running); flowSounds[2] = Sound.Load("Content/Sounds/Water/FlowLarge.ogg", false); yield return(CoroutineStatus.Running); for (int i = 0; i < 10; i++) { SplashSounds[i] = Sound.Load("Content/Sounds/Water/Splash" + (i) + ".ogg", false); yield return(CoroutineStatus.Running); } var xMusic = doc.Root.Elements("music").ToList(); if (xMusic.Any()) { musicClips = new BackgroundMusic[xMusic.Count]; int i = 0; foreach (XElement element in xMusic) { string file = element.GetAttributeString("file", ""); string type = element.GetAttributeString("type", "").ToLowerInvariant(); Vector2 priority = element.GetAttributeVector2("priorityrange", new Vector2(0.0f, 100.0f)); musicClips[i] = new BackgroundMusic(file, type, priority); yield return(CoroutineStatus.Running); i++; } } List <KeyValuePair <string, Sound> > miscSoundList = new List <KeyValuePair <string, Sound> >(); damageSounds = new List <DamageSound>(); foreach (XElement subElement in doc.Root.Elements()) { yield return(CoroutineStatus.Running); switch (subElement.Name.ToString().ToLowerInvariant()) { case "music": continue; case "damagesound": Sound damageSound = Sound.Load(subElement.GetAttributeString("file", ""), false); if (damageSound == null) { continue; } DamageSoundType damageSoundType = DamageSoundType.None; Enum.TryParse <DamageSoundType>(subElement.GetAttributeString("damagesoundtype", "None"), false, out damageSoundType); damageSounds.Add(new DamageSound( damageSound, subElement.GetAttributeVector2("damagerange", new Vector2(0.0f, 100.0f)), damageSoundType, subElement.GetAttributeString("requiredtag", ""))); break; default: Sound sound = Sound.Load(subElement.GetAttributeString("file", ""), false); if (sound != null) { miscSoundList.Add(new KeyValuePair <string, Sound>(subElement.Name.ToString().ToLowerInvariant(), sound)); } break; } } miscSounds = miscSoundList.ToLookup(kvp => kvp.Key, kvp => kvp.Value); Initialized = true; yield return(CoroutineStatus.Success); }
public static IEnumerable <object> Init() { OverrideMusicType = null; var soundFiles = GameMain.Instance.GetFilesOfType(ContentType.Sounds); List <XElement> soundElements = new List <XElement>(); foreach (ContentFile soundFile in soundFiles) { XDocument doc = XMLExtensions.TryLoadXml(soundFile.Path); if (doc == null) { continue; } var mainElement = doc.Root; if (doc.Root.IsOverride()) { mainElement = doc.Root.FirstElement(); DebugConsole.NewMessage($"Overriding all sounds with {soundFile.Path}", Color.Yellow); soundElements.Clear(); } soundElements.AddRange(mainElement.Elements()); } SoundCount = 1 + soundElements.Count(); var startUpSoundElement = soundElements.Find(e => e.Name.ToString().Equals("startupsound", StringComparison.OrdinalIgnoreCase)); if (startUpSoundElement != null) { startUpSound = GameMain.SoundManager.LoadSound(startUpSoundElement, false); startUpSound?.Play(); } yield return(CoroutineStatus.Running); List <KeyValuePair <string, Sound> > miscSoundList = new List <KeyValuePair <string, Sound> >(); damageSounds ??= new List <DamageSound>(); musicClips ??= new List <BackgroundMusic>(); bool firstWaterAmbienceLoaded = false; foreach (XElement soundElement in soundElements) { yield return(CoroutineStatus.Running); if (loadedSoundElements != null && loadedSoundElements.Any(e => SoundElementsEquivalent(e, soundElement))) { continue; } try { switch (soundElement.Name.ToString().ToLowerInvariant()) { case "music": var newMusicClip = new BackgroundMusic(soundElement); musicClips.AddIfNotNull(newMusicClip); if (loadedSoundElements != null) { if (newMusicClip.Type.Equals("menu", StringComparison.OrdinalIgnoreCase)) { targetMusic[0] = newMusicClip; } } break; case "splash": SplashSounds.AddIfNotNull(GameMain.SoundManager.LoadSound(soundElement, false)); break; case "flow": FlowSounds.AddIfNotNull(GameMain.SoundManager.LoadSound(soundElement, false)); break; case "waterambience": //backwards compatibility (1st waterambience used to be played both inside and outside, 2nd when moving) if (!firstWaterAmbienceLoaded) { waterAmbienceIn?.Dispose(); waterAmbienceOut?.Dispose(); if (File.Exists(soundElement.GetAttributeString("file", ""))) { waterAmbienceIn = GameMain.SoundManager.LoadSound(soundElement, false); waterAmbienceOut = GameMain.SoundManager.LoadSound(soundElement, false); } else { waterAmbienceIn = GameMain.SoundManager.LoadSound(soundElement, false, "Content/Sounds/Water/WaterAmbienceIn.ogg"); waterAmbienceOut = GameMain.SoundManager.LoadSound(soundElement, false, "Content/Sounds/Water/WaterAmbienceOut.ogg"); } firstWaterAmbienceLoaded = true; } else { waterAmbienceMoving?.Dispose(); if (File.Exists(soundElement.GetAttributeString("file", ""))) { waterAmbienceMoving = GameMain.SoundManager.LoadSound(soundElement, false); } else { waterAmbienceMoving = GameMain.SoundManager.LoadSound(soundElement, false, "Content/Sounds/Water/WaterAmbienceMoving.ogg"); } } break; case "waterambiencein": waterAmbienceIn?.Dispose(); waterAmbienceIn = GameMain.SoundManager.LoadSound(soundElement, false); break; case "waterambienceout": waterAmbienceOut?.Dispose(); waterAmbienceOut = GameMain.SoundManager.LoadSound(soundElement, false); break; case "waterambiencemoving": waterAmbienceMoving?.Dispose(); waterAmbienceMoving = GameMain.SoundManager.LoadSound(soundElement, false); break; case "damagesound": Sound damageSound = GameMain.SoundManager.LoadSound(soundElement, false); if (damageSound == null) { continue; } string damageSoundType = soundElement.GetAttributeString("damagesoundtype", "None"); damageSounds.Add(new DamageSound( damageSound, soundElement.GetAttributeVector2("damagerange", Vector2.Zero), damageSoundType, soundElement.GetAttributeString("requiredtag", ""))); break; default: Sound sound = GameMain.SoundManager.LoadSound(soundElement, false); if (sound != null) { miscSoundList.Add(new KeyValuePair <string, Sound>(soundElement.Name.ToString().ToLowerInvariant(), sound)); } break; } } catch (System.IO.FileNotFoundException e) { DebugConsole.ThrowError("Error while initializing SoundPlayer.", e); } } musicClips.RemoveAll(mc => !soundElements.Any(e => SoundElementsEquivalent(mc.Element, e))); for (int i = 0; i < currentMusic.Length; i++) { if (currentMusic[i] != null && !musicClips.Any(mc => mc.File == currentMusic[i].Filename)) { DisposeMusicChannel(i); } } SplashSounds.ForEach(s => { if (!soundElements.Any(e => SoundElementsEquivalent(s.XElement, e))) { s.Dispose(); } }); SplashSounds.RemoveAll(s => s.Disposed); FlowSounds.ForEach(s => { if (!soundElements.Any(e => SoundElementsEquivalent(s.XElement, e))) { s.Dispose(); } }); FlowSounds.RemoveAll(s => s.Disposed); damageSounds.ForEach(s => { if (!soundElements.Any(e => SoundElementsEquivalent(s.sound.XElement, e))) { s.sound.Dispose(); } }); damageSounds.RemoveAll(s => s.sound.Disposed); miscSounds?.ForEach(g => g.ForEach(s => { if (!soundElements.Any(e => SoundElementsEquivalent(s.XElement, e))) { s.Dispose(); } else { miscSoundList.Add(new KeyValuePair <string, Sound>(g.Key, s)); } })); flowSoundChannels = new SoundChannel[FlowSounds.Count]; flowVolumeLeft = new float[FlowSounds.Count]; flowVolumeRight = new float[FlowSounds.Count]; fireSoundChannels = new SoundChannel[2]; fireVolumeLeft = new float[2]; fireVolumeRight = new float[2]; miscSounds = miscSoundList.ToLookup(kvp => kvp.Key, kvp => kvp.Value); Initialized = true; loadedSoundElements = soundElements; yield return(CoroutineStatus.Success); }
private static void UpdateMusic(float deltaTime) { if (musicClips == null) { return; } if (OverrideMusicType != null && OverrideMusicDuration.HasValue) { OverrideMusicDuration -= deltaTime; if (OverrideMusicDuration <= 0.0f) { OverrideMusicType = null; OverrideMusicDuration = null; } } updateMusicTimer -= deltaTime; if (updateMusicTimer <= 0.0f) { List <BackgroundMusic> suitableMusic = GetSuitableMusicClips(); if (suitableMusic.Count == 0) { targetMusic = null; } else if (!suitableMusic.Contains(currentMusic)) { int index = Rand.Int(suitableMusic.Count); if (currentMusic == null || suitableMusic[index].file != currentMusic.file) { targetMusic = suitableMusic[index]; } } updateMusicTimer = UpdateMusicInterval; } if (targetMusic == null || currentMusic == null || targetMusic.file != currentMusic.file) { currMusicVolume = MathHelper.Lerp(currMusicVolume, 0.0f, MusicLerpSpeed * deltaTime); if (currentMusic != null) { Sound.StreamVolume(currMusicVolume); } if (currMusicVolume < 0.01f) { Sound.StopStream(); try { if (targetMusic != null) { Sound.StartStream(targetMusic.file, currMusicVolume); } } catch (FileNotFoundException e) { DebugConsole.ThrowError("Music clip " + targetMusic.file + " not found!", e); } currentMusic = targetMusic; } } else { currMusicVolume = MathHelper.Lerp(currMusicVolume, MusicVolume, MusicLerpSpeed * deltaTime); Sound.StreamVolume(currMusicVolume); } }