private void StartTransition(bool forward) { m_transitionForward = forward; m_musicState = MyMusicState.Transition; m_timeFromTransitionStart = 0; m_volumeAtTransitionStart = m_volumeMusic; }
public void StopMusic() { m_currentTransition = null; m_nextTransitions.Clear(); m_musicState = MyMusicState.Stopped; if (m_musicCue != null) { try { m_musicCue.Stop(); } catch (Exception e) { MyLog.Default.WriteLine(e); if (m_audioEngine == null || m_audioEngine.IsDisposed) { MyLog.Default.WriteLine("Audio engine disposed!", LoggingOptions.AUDIO); } } } }
public bool ApplyTransition(MyStringId transitionEnum, int priority = 0, MyStringId? category = null, bool loop = true) { if (!m_canPlay) return false; if (!m_musicAllowed) return false; Debug.Assert(priority >= 0); if (category.HasValue) { if (category.Value == MyStringId.NullOrEmpty) category = null; else if (!m_cueBank.IsValidTransitionCategory(transitionEnum, category.Value)) { Debug.Fail("This category doesn't exist for this transition!"); MyLog.Default.WriteLine(string.Format("Category {0} doesn't exist for this transition!", category)); return false; } } // if we try apply same transition and priority and category if ((m_currentTransition != null) && (m_currentTransition.Value.Priority == priority) && (m_currentTransition.Value.TransitionEnum == transitionEnum) && ((category == null) || (m_currentTransition.Value.Category == category))) { if ((m_musicState == MyMusicState.Transition) && !m_transitionForward) { m_musicState = MyMusicState.Playing; return true; } else return false; } // if category not set, we take random category from transition cues MyStringId transitionCategory = category ?? m_cueBank.GetRandomTransitionCategory(ref transitionEnum, ref NO_RANDOM); // we set this transition as next m_nextTransitions[priority] = new MyMusicTransition(priority, transitionEnum, transitionCategory); MyTrace.Send(TraceWindow.Server, string.Format("Applying transition {0} {1} (priority = {2})", transitionEnum, transitionCategory, priority)); // if new transition has lower priority then current, we don't want apply new transition now if ((m_currentTransition != null) && (m_currentTransition.Value.Priority > priority)) return false; m_loopMusic = loop; if (m_musicState == MyMusicState.Playing) StartTransition(true); else if (m_musicState == MyMusicState.Transition) { } else if (m_musicState == MyMusicState.Stopped) { } else throw new InvalidBranchException(); return true; }
private void UpdateMusic(int stepSizeinMS) { if (m_musicState == MyMusicState.Transition) { m_timeFromTransitionStart += stepSizeinMS; // if transition time elapsed, we stop actual playing cue and set music state to stopped if (m_timeFromTransitionStart >= TRANSITION_TIME) { m_musicState = MyMusicState.Stopped; if ((m_musicCue != null) && m_musicCue.IsPlaying) { m_musicCue.Stop(true); m_musicCue = null; } } // we decrease music volume (because transition effect) else if ((m_musicCue != null) && m_musicCue.IsPlaying) { if ((m_musicAudioVoice.Volume > 0f) && m_musicOn) m_musicAudioVoice.SetVolume((1f - (float)m_timeFromTransitionStart / TRANSITION_TIME) * m_volumeAtTransitionStart); } } if (m_musicState == MyMusicState.Stopped) { MyMusicTransition? nextTransition = GetNextTransition(); // we save current transition as next transition, if we want apply transition with higher priority, so after new transition stop, then this old transition return back if ((m_currentTransition != null) && (m_nextTransitions.Count > 0) && (nextTransition != null) && (nextTransition.Value.Priority > m_currentTransition.Value.Priority)) m_nextTransitions[m_currentTransition.Value.Priority] = m_currentTransition.Value; m_currentTransition = nextTransition; // it there is current transition to play, we play it and set state to playing if (m_currentTransition != null) { m_musicAudioVoice.SetVolume(m_volumeMusic); PlayMusicByTransition(m_currentTransition.Value); m_nextTransitions.Remove(m_currentTransition.Value.Priority); m_musicState = MyMusicState.Playing; } } if (m_musicState == MyMusicState.Playing) { if ((m_musicCue == null) || !m_musicCue.IsPlaying) { if (m_loopMusic && m_currentTransition != null) { // we play current transition in loop Debug.Assert(m_currentTransition != null); PlayMusicByTransition(m_currentTransition.Value); } else { // switches to another, random, track m_currentTransition = null; //MyStringId? newTransitionEnum = GetRandomTransitionEnum(); MyStringId? newTransitionEnum = MyStringId.GetOrCompute("Default"); if (newTransitionEnum.HasValue) ApplyTransition(newTransitionEnum.Value, 0, null, false); } } } }
public void LoadData(MyAudioInitParams initParams, ListReader<MySoundData> sounds, ListReader<MyAudioEffect> effects) { MyLog.Default.WriteLine("MyAudio.LoadData - START"); MyLog.Default.IncreaseIndent(); m_initParams = initParams; m_sounds = sounds; m_effects = effects; m_canPlay = true; try { if (sounds.Count > 0) { Init(); } else { MyLog.Default.WriteLine("Unable to load audio data. Game continues, but without sound", LoggingOptions.AUDIO); m_canPlay = false; } } catch (Exception ex) { MyLog.Default.WriteLine("Exception during loading audio engine. Game continues, but without sound. Details: " + ex.ToString(), LoggingOptions.AUDIO); MyLog.Default.WriteLine("Device ID: " + m_deviceDetails.DeviceID, LoggingOptions.AUDIO); MyLog.Default.WriteLine("Device name: " + m_deviceDetails.DisplayName, LoggingOptions.AUDIO); MyLog.Default.WriteLine("Device role: " + m_deviceDetails.Role, LoggingOptions.AUDIO); MyLog.Default.WriteLine("Output format: " + m_deviceDetails.OutputFormat, LoggingOptions.AUDIO); // This exception is the only way I can know if we can play sound (e.g. if computer doesn't have sound card). // I didn't find other ways of checking it. m_canPlay = false; } if (m_initParams.SimulateNoSoundCard) m_canPlay = false; if (m_canPlay) { m_cueBank = new MyCueBank(m_audioEngine, sounds); m_cueBank.UseSameSoundLimiter = m_useSameSoundLimiter; m_cueBank.SetSameSoundLimiter(); m_cueBank.DisablePooling = initParams.DisablePooling; m_effectBank = new MyEffectBank(effects, m_audioEngine); m_3Dsounds = new List<IMy3DSoundEmitter>(); m_listener = new Listener(); m_listener.SetDefaultValues(); m_helperEmitter = new Emitter(); m_helperEmitter.SetDefaultValues(); m_musicOn = true; m_gameSoundsOn = true; m_musicAllowed = true; if ((m_musicCue != null) && m_musicCue.IsPlaying) { // restarts music cue m_musicCue = PlaySound(m_musicCue.CueEnum); if (m_musicCue != null) { m_musicCue.SetOutputVoices(m_musicAudioVoiceDesc); m_musicAudioVoice.SetVolume(m_volumeMusic); } UpdateMusic(0); } else { m_musicState = MyMusicState.Stopped; } m_loopMusic = true; m_transitionForward = false; m_timeFromTransitionStart = 0; m_soundInstancesTotal2D = 0; m_soundInstancesTotal3D = 0; } MyLog.Default.DecreaseIndent(); MyLog.Default.WriteLine("MyAudio.LoadData - END"); }
public bool ApplyTransition(MyStringId transitionEnum, int priority = 0, MyStringId?category = null, bool loop = true) { if (!m_canPlay) { return(false); } if (!m_musicAllowed) { return(false); } Debug.Assert(priority >= 0); if (category.HasValue) { if (category.Value == MyStringId.NullOrEmpty) { category = null; } else if (!m_cueBank.IsValidTransitionCategory(transitionEnum, category.Value)) { Debug.Fail("This category doesn't exist for this transition!"); MyLog.Default.WriteLine(string.Format("Category {0} doesn't exist for this transition!", category)); return(false); } } // if we try apply same transition and priority and category if ((m_currentTransition != null) && (m_currentTransition.Value.Priority == priority) && (m_currentTransition.Value.TransitionEnum == transitionEnum) && ((category == null) || (m_currentTransition.Value.Category == category))) { if ((m_musicState == MyMusicState.Transition) && !m_transitionForward) { m_musicState = MyMusicState.Playing; return(true); } else { return(false); } } // if category not set, we take random category from transition cues MyStringId transitionCategory = category ?? m_cueBank.GetRandomTransitionCategory(transitionEnum); // we set this transition as next m_nextTransitions[priority] = new MyMusicTransition(priority, transitionEnum, transitionCategory); MyTrace.Send(TraceWindow.Server, string.Format("Applying transition {0} {1} (priority = {2})", transitionEnum, transitionCategory, priority)); // if new transition has lower priority then current, we don't want apply new transition now if ((m_currentTransition != null) && (m_currentTransition.Value.Priority > priority)) { return(false); } m_loopMusic = loop; if (m_musicState == MyMusicState.Playing) { StartTransition(true); } else if (m_musicState == MyMusicState.Transition) { } else if (m_musicState == MyMusicState.Stopped) { } else { throw new InvalidBranchException(); } return(true); }
private void UpdateMusic(int stepSizeinMS) { if (m_musicState == MyMusicState.Transition) { m_timeFromTransitionStart += stepSizeinMS; // if transition time elapsed, we stop actual playing cue and set music state to stopped if (m_timeFromTransitionStart >= TRANSITION_TIME) { m_musicState = MyMusicState.Stopped; if ((m_musicCue != null) && m_musicCue.IsPlaying) { m_musicCue.Stop(true); m_musicCue = null; } } // we decrease music volume (because transition effect) else if ((m_musicCue != null) && m_musicCue.IsPlaying) { if ((m_musicAudioVoice.Volume > 0f) && m_musicOn) { m_musicAudioVoice.SetVolume((1f - (float)m_timeFromTransitionStart / TRANSITION_TIME) * m_volumeAtTransitionStart); } } } if (m_musicState == MyMusicState.Stopped) { MyMusicTransition?nextTransition = GetNextTransition(); // we save current transition as next transition, if we want apply transition with higher priority, so after new transition stop, then this old transition return back if ((m_currentTransition != null) && (m_nextTransitions.Count > 0) && (nextTransition != null) && (nextTransition.Value.Priority > m_currentTransition.Value.Priority)) { m_nextTransitions[m_currentTransition.Value.Priority] = m_currentTransition.Value; } m_currentTransition = nextTransition; // it there is current transition to play, we play it and set state to playing if (m_currentTransition != null) { m_musicAudioVoice.SetVolume(m_volumeMusic); PlayMusicByTransition(m_currentTransition.Value); m_nextTransitions.Remove(m_currentTransition.Value.Priority); m_musicState = MyMusicState.Playing; } } if (m_musicState == MyMusicState.Playing) { if ((m_musicCue == null) || !m_musicCue.IsPlaying) { if (m_loopMusic && m_currentTransition != null) { // we play current transition in loop Debug.Assert(m_currentTransition != null); PlayMusicByTransition(m_currentTransition.Value); } else { // switches to another, random, track m_currentTransition = null; MyStringId?newTransitionEnum = GetRandomTransitionEnum(); if (newTransitionEnum.HasValue) { ApplyTransition(newTransitionEnum.Value, 0, null, false); } } } } }
public void LoadData(MyAudioInitParams initParams, ListReader <MySoundData> sounds, ListReader <MyAudioEffect> effects) { MyLog.Default.WriteLine("MyAudio.LoadData - START"); MyLog.Default.IncreaseIndent(); m_initParams = initParams; m_sounds = sounds; m_effects = effects; m_canPlay = true; try { if (sounds.Count > 0) { Init(); } else { MyLog.Default.WriteLine("Unable to load audio data. Game continues, but without sound", LoggingOptions.AUDIO); m_canPlay = false; } } catch (Exception ex) { MyLog.Default.WriteLine("Exception during loading audio engine. Game continues, but without sound. Details: " + ex.ToString(), LoggingOptions.AUDIO); MyLog.Default.WriteLine("Device ID: " + m_deviceDetails.DeviceID, LoggingOptions.AUDIO); MyLog.Default.WriteLine("Device name: " + m_deviceDetails.DisplayName, LoggingOptions.AUDIO); MyLog.Default.WriteLine("Device role: " + m_deviceDetails.Role, LoggingOptions.AUDIO); MyLog.Default.WriteLine("Output format: " + m_deviceDetails.OutputFormat, LoggingOptions.AUDIO); // This exception is the only way I can know if we can play sound (e.g. if computer doesn't have sound card). // I didn't find other ways of checking it. m_canPlay = false; } if (m_initParams.SimulateNoSoundCard) { m_canPlay = false; } if (m_canPlay) { m_cueBank = new MyCueBank(m_audioEngine, sounds); m_cueBank.DisablePooling = initParams.DisablePooling; m_effectBank = new MyEffectBank(effects, m_audioEngine); m_3Dsounds = new List <IMy3DSoundEmitter>(); m_listener = new Listener(); m_listener.SetDefaultValues(); m_helperEmitter = new Emitter(); m_helperEmitter.SetDefaultValues(); // This is reverb turned to off, so we hear sounds as they are defined in wav files ApplyReverb = false; m_musicOn = true; m_gameSoundsOn = true; m_musicAllowed = true; if ((m_musicCue != null) && m_musicCue.IsPlaying) { // restarts music cue m_musicCue = PlaySound(m_musicCue.CueEnum); if (m_musicCue != null) { m_musicCue.SetOutputVoices(m_musicAudioVoiceDesc); } UpdateMusic(0); } else { m_musicState = MyMusicState.Stopped; } m_loopMusic = true; m_transitionForward = false; m_timeFromTransitionStart = 0; m_soundInstancesTotal2D = 0; m_soundInstancesTotal3D = 0; } MyLog.Default.DecreaseIndent(); MyLog.Default.WriteLine("MyAudio.LoadData - END"); }
private static void UpdateMusic() { if (m_musicState == MyMusicState.Transition) { m_timeFromTransitionStart += MyConstants.PHYSICS_STEP_SIZE_IN_MILLISECONDS; // if transition time elapsed, we stop actual playing cue and set music state to stopped if (m_timeFromTransitionStart >= TRANSITION_TIME) { m_musicState = MyMusicState.Stopped; if (m_musicCue != null && m_musicCue.Value.IsPlaying) { m_musicCue.Value.Stop(StopFlags.Immediate); m_musicCue = null; } } // we decrease music volume (because transition effect) else if (m_musicCue != null && m_musicCue.Value.IsPlaying) { //temporary disabled, volume decreasing now works strange (terible noise) if (m_volumeMusic > 0f && m_musicOn) { m_musicCategory.SetVolume((1f - (float)m_timeFromTransitionStart / TRANSITION_TIME) * m_volumeMusic); } } } if (m_musicState == MyMusicState.Stopped) { MyMusicTransition? nextTransition = GetNextTransition(); // we save current transition as next transition, if we want apply transition with higher priority, so after new transition stop, then this old transition return back if (m_currentTransition != null && m_nextTransitions.Count > 0 && nextTransition != null && nextTransition.Value.Priority > m_currentTransition.Value.Priority) { m_nextTransitions[m_currentTransition.Value.Priority] = m_currentTransition.Value; } m_currentTransition = nextTransition; // it there is current transition to play, we play it and set state to playing if (m_currentTransition != null) { PlayMusicByTransition(m_currentTransition.Value); m_nextTransitions.Remove(m_currentTransition.Value.Priority); m_musicState = MyMusicState.Playing; } } if (m_musicState == MyMusicState.Playing) { // we play current transition in loop //TODO: IsStopped makes peaks if (m_musicCue != null && (!m_musicCue.Value.IsValid || m_musicCue.Value.IsStopped)) { if (m_loopMusic) { Debug.Assert(m_currentTransition != null); PlayMusicByTransition(m_currentTransition.Value); } else { m_loopMusic = true; //default StopTransition(m_currentTransition.Value.Priority); } } } }
private static void StartTransition(bool forward) { m_transitionForward = forward; m_musicState = MyMusicState.Transition; m_timeFromTransitionStart = 0; }
public static bool ApplyTransition(MyMusicTransitionEnum transitionEnum, int priority = 0, string category = null, bool loop = true) { if (!m_canPlay) return false; if (!m_musicAllowed) return false; Debug.Assert(priority >= 0); if (category != null) { if (!m_musicTransitionCues[(int)transitionEnum].ContainsKey(category)) { Debug.Assert(false, "This category doesn't exist for this transition!"); return false; } } // if we try apply same transition and priority and category if (m_currentTransition != null && m_currentTransition.Value.Priority == priority && m_currentTransition.Value.TransitionEnum == transitionEnum && (category == null || m_currentTransition.Value.Category == category)) { if (m_musicState == MyMusicState.Transition && !m_transitionForward) { m_musicState = MyMusicState.Playing; return true; } else { return false; } } // if category not set, we take random category from this transition cues string transitionCategory = category != null ? category : GetRandomTransitionCategory(transitionEnum); // we set this transition as next m_nextTransitions[priority] = new MyMusicTransition(priority, transitionEnum, transitionCategory); // if new transition has lower priority then current, we don't want apply new transition now if (m_currentTransition != null && m_currentTransition.Value.Priority > priority) { return false; } m_loopMusic = loop; if (m_musicState == MyMusicState.Playing) { StartTransition(true); } else if (m_musicState == MyMusicState.Transition) { } else if (m_musicState == MyMusicState.Stopped) { } else { throw new MyMwcExceptionApplicationShouldNotGetHere(); } return true; }
static void ClearMusicTransitions() { m_musicState = MyMusicState.Stopped; m_musicCue = null; //m_nextTransition = null; m_nextTransitions.Clear(); m_currentTransition = null; }
public static new void LoadData() { MySandboxGame.Log.WriteLine("MyAudio.LoadData - START"); MySandboxGame.Log.IncreaseIndent(); m_canPlay = true; MyObjectBuilder_CueDefinitions ob = null; try { #if DEBUG bool result = false; try { result = MyObjectBuilder_Base.DeserializeXML(MyCreateFileAudioSBA.GetFilenameSBA(), out ob); } catch (FileNotFoundException) { // generates the Audio.sba file MyCreateFileAudioSBA.Create(); result = MyObjectBuilder_Base.DeserializeXML(MyCreateFileAudioSBA.GetFilenameSBA(), out ob); } #else bool result = MyObjectBuilder_Base.DeserializeXML(MyCreateFileAudioSBA.GetFilenameSBA(), out ob); #endif //DEBUG if (result) { Init(); } else { MySandboxGame.Log.WriteLine("Unable to load audio data. Game continues, but without sound", LoggingOptions.AUDIO); m_canPlay = false; } } catch (Exception ex) { MySandboxGame.Log.WriteLine("Exception during loading audio engine. Game continues, but without sound. Details: " + ex.ToString(), LoggingOptions.AUDIO); MySandboxGame.Log.WriteLine("Device ID: " + m_deviceDetails.DeviceID, LoggingOptions.AUDIO); MySandboxGame.Log.WriteLine("Device name: " + m_deviceDetails.DisplayName, LoggingOptions.AUDIO); MySandboxGame.Log.WriteLine("Device role: " + m_deviceDetails.Role, LoggingOptions.AUDIO); MySandboxGame.Log.WriteLine("Output format: " + m_deviceDetails.OutputFormat, LoggingOptions.AUDIO); // This exception is the only way I can know if we can play sound (e.g. if computer doesn't have sound card). // I didn't find other ways of checking it. m_canPlay = false; } if (MyFakes.SIMULATE_NO_SOUND_CARD) m_canPlay = false; if (m_canPlay) { m_cueBank = new MyCueBank(m_audioEngine, ob); m_3Dsounds = new List<IMy3DSoundEmitter>(); m_listener = new Listener(); m_listener.SetDefaultValues(); m_helperEmitter = new Emitter(); m_helperEmitter.SetDefaultValues(); // This is reverb turned to off, so we hear sounds as they are defined in wav files ApplyReverb = false; m_musicOn = true; m_gameSoundsOn = true; // Volume from config VolumeMusic = MyConfig.MusicVolume; VolumeGame = MyConfig.GameVolume; VolumeHud = MyConfig.GameVolume; MyConfig.MusicVolume = VolumeMusic; MyConfig.GameVolume = VolumeGame; m_mute = false; m_musicAllowed = true; m_musicState = MyMusicState.Stopped; m_loopMusic = true; m_transitionForward = false; m_timeFromTransitionStart = 0; m_soundInstancesTotal2D = 0; m_soundInstancesTotal3D = 0; } MySandboxGame.Log.DecreaseIndent(); MySandboxGame.Log.WriteLine("MyAudio.LoadData - END"); }