public void OnStopPlaying(MySourceVoice voice)
 {
     if (m_playingVoices.Contains(voice))
     {
         m_availableVoices.Enqueue(voice);
         m_playingVoices.Remove(voice);
     }
 }
 public void OnStopPlaying(MySourceVoice voice)
 {
     if (m_playingVoices.Contains(voice))
     {
         m_availableVoices.Enqueue(voice);
         m_playingVoices.Remove(voice);
     }
 }
 public void StopSound(bool forced)
 {
     if ((m_sound != null) && m_sound.IsPlaying)
     {
         m_sound.Stop(forced);
         m_sound = null;
         CueEnum = MySoundCuesEnum.None;
     }
 }
Exemple #4
0
 public void StopSound(bool forced)
 {
     if ((m_sound != null) && m_sound.IsPlaying)
     {
         m_sound.Stop(forced);
         m_sound = null;
         CueEnum = MySoundCuesEnum.None;
     }
 }
        public MySourceVoice NextAvailable()
        {
            if (m_availableVoices.Count == 0)
            {
                SourceVoice sourceVoice = new SourceVoice(m_audioEngine, m_waveFormat, VoiceFlags.None, XAudio2.MaximumFrequencyRatio, true);
                MySourceVoice voice = new MySourceVoice(this, sourceVoice);
                sourceVoice.BufferEnd += voice.OnStopPlaying;
                m_availableVoices.Enqueue(voice);
                ++m_maxCount;
            }
            MySourceVoice next = m_availableVoices.Dequeue();
            m_playingVoices.Add(next);

            return next;
        }
        public MySourceVoice NextAvailable()
        {
            if (m_availableVoices.Count == 0)
            {
                SourceVoice   sourceVoice = new SourceVoice(m_audioEngine, m_waveFormat, VoiceFlags.None, XAudio2.MaximumFrequencyRatio, true);
                MySourceVoice voice       = new MySourceVoice(this, sourceVoice);
                sourceVoice.BufferEnd += voice.OnStopPlaying;
                m_availableVoices.Enqueue(voice);
                ++m_maxCount;
            }
            MySourceVoice next = m_availableVoices.Dequeue();

            m_playingVoices.Add(next);

            return(next);
        }
        public MySourceVoice GetVoice(MySoundCuesEnum cueEnum)
        {
            MyObjectBuilder_CueDefinition cue = GetCue(cueEnum);

            if ((cue == null) || (cue.Waves == null) || (cue.Waves.Length == 0))
            {
                return(null);
            }

            MyInMemoryWave wave = GetRandomWave(cue);

            if (wave == null)
            {
                return(null);
            }

            MyWaveFormat myWaveFormat = new MyWaveFormat()
            {
                Encoding   = wave.WaveFormat.Encoding,
                Channels   = wave.WaveFormat.Channels,
                SampleRate = wave.WaveFormat.SampleRate,
                WaveFormat = wave.WaveFormat
            };

            MySourceVoice voice = m_voicePools[myWaveFormat].NextAvailable();

            if (voice == null)
            {
                return(null);
            }

            voice.SubmitSourceBuffer(cueEnum, wave.Buffer, wave.Stream.DecodedPacketsInfo, wave.WaveFormat.SampleRate);
            if (m_applyReverb)
            {
                voice.Voice.SetEffectChain(m_effectDescriptor);
                voice.Voice.EnableEffect(0);
            }
            else
            {
                voice.Voice.SetEffectChain(null);
            }

            return(voice);
        }
        public void Update()
        {
            int id = 0;

            while (id < m_fadingOutVoices.Count)
            {
                MySourceVoice voice = m_fadingOutVoices[id];
                if (voice.Voice.Volume < 0.01f)
                {
                    voice.Voice.Stop();
                    voice.Voice.FlushSourceBuffers(); // triggers voice's BufferEnd event
                    m_fadingOutVoices.RemoveAt(id);
                    --id;
                }
                else
                {
                    voice.Voice.SetVolume(0.75f * voice.Voice.Volume);
                }

                ++id;
            }
        }
Exemple #9
0
 public void PlaySingleSound(MySoundCuesEnum cueEnum, bool update)
 {
     CueEnum = cueEnum;
     Sound   = MyAudio.PlayCue3D(this, CueEnum, update);
 }
 public void AddToFadeoutList(MySourceVoice voice)
 {
     m_fadingOutVoices.Add(voice);
 }
        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.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)
                    {
                        // we play current transition in loop
                        Debug.Assert(m_currentTransition != null);
                        PlayMusicByTransition(m_currentTransition.Value);
                    }
                    else
                    {
                        // switches to another, random, track
                        m_currentTransition = null;
                        MyMusicTransitionEnum? newTransitionEnum = GetRandomTransitionEnum();
                        if (newTransitionEnum.HasValue)
                            ApplyTransition(newTransitionEnum.Value, 0, null, false);
                    }
                }
            }
        }
 public void PlaySingleSound(MySoundCuesEnum cueEnum, bool update)
 {
     CueEnum = cueEnum;
     Sound = MyAudio.PlayCue3D(this, CueEnum, update);
 }
 public void AddToFadeoutList(MySourceVoice voice)
 {
     m_fadingOutVoices.Add(voice);
 }
 private static void PlayMusicByTransition(MyMusicTransition transition)
 {
     m_musicCue = PlayCue2D(m_cueBank.GetTransitionCue(transition.TransitionEnum, transition.Category));
     if (m_musicCue != null)
     {
         m_musicCue.Voice.SetOutputVoices(m_musicAudioVoiceDesc);
         m_musicAudioVoice.SetVolume(m_volumeMusic);
     }
 }
Exemple #15
0
 public void PlayIntroLoopPair(MySoundCuesEnum introCueEnum, MySoundCuesEnum loopCueEnum)
 {
     CueEnum = loopCueEnum;
     Sound   = MyAudio.PlayIntroLoopPair3D(this, introCueEnum, loopCueEnum);
 }
 public void PlayIntroLoopPair(MySoundCuesEnum introCueEnum, MySoundCuesEnum loopCueEnum)
 {
     CueEnum = loopCueEnum;
     Sound = MyAudio.PlayIntroLoopPair3D(this, introCueEnum, loopCueEnum);
 }
        private static void CheckIfDeviceChanged()
        {
            if (MyAudio_Native.HasDeviceChanged(m_audioEngine, m_deviceDetails.DisplayName))
            {
                try
                {
                    Init();
                }
                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)
                {
                    if (m_cueBank != null)
                        m_cueBank.SetAudioEngine(m_audioEngine);

                    m_gameAudioVoice.SetVolume(m_volumeDefault);
                    m_hudAudioVoice.SetVolume(m_volumeHud);
                    m_musicAudioVoice.SetVolume(m_volumeMusic);

                    if ((m_musicCue != null) && m_musicCue.IsPlaying)
                    {
                        // restarts music cue
                        m_musicCue = PlayCue2D(m_musicCue.CueEnum);
                        if (m_musicCue != null)
                            m_musicCue.Voice.SetOutputVoices(m_musicAudioVoiceDesc);

                        UpdateMusic();
                    }
                }
            }
        }