IMySourceVoice IMyAudio.GetSound(IMy3DSoundEmitter source, int sampleRate, int channels, MySoundDimensions dimension) { if (!m_canPlay) { return(null); } var waveFormat = new WaveFormat(sampleRate, channels); source.SourceChannels = channels; var sourceVoice = new MySourceVoice(m_audioEngine, waveFormat); float volume = source.CustomVolume.HasValue ? source.CustomVolume.Value : 1; float maxDistance = source.CustomMaxDistance.HasValue ? source.CustomMaxDistance.Value : 0; sourceVoice.SetVolume(volume); if (dimension == MySoundDimensions.D3) { m_helperEmitter.UpdateValuesOmni(source.SourcePosition, source.Velocity, maxDistance, m_deviceDetails.OutputFormat.Channels, MyCurveType.Linear); m_x3dAudio.Apply3D(sourceVoice.Voice, m_listener, m_helperEmitter, source.SourceChannels, m_deviceDetails.OutputFormat.Channels, m_calculateFlags, maxDistance, sourceVoice.FrequencyRatio); Update3DCuesState(); Add3DCueToUpdateList(source); ++m_soundInstancesTotal3D; } return(sourceVoice); }
internal MySourceVoice GetVoice(MyCueId cueId, MyInMemoryWave wave, CuePart part = CuePart.Start) { 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.Flush(); voice.SubmitSourceBuffer(cueId, wave, part); if (m_applyReverb) { voice.Voice.SetEffectChain(m_effectDescriptor); voice.Voice.EnableEffect(0); } else { voice.Voice.SetEffectChain(null); } return(voice); }
private void CheckIfDeviceChanged() { // DeviceCount cannot be called on XAudio >= 2.8 (Windows 8) // Maybe call DeviceCount with XAudio 2.7 and use different API for windows 8 // http://blogs.msdn.com/b/chuckw/archive/2012/04/02/xaudio2-and-windows-8-consumer-preview.aspx // DeviceCount is very slow even on Windows 7 sometimes, don't know why if (m_deviceLost)// || m_lastDeviceCount != m_audioEngine.DeviceCount) { m_deviceLost = false; try { Init(); } 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) { if (m_cueBank != null) { m_cueBank.SetAudioEngine(m_audioEngine); } m_gameAudioVoice.SetVolume(m_volumeDefault); m_hudAudioVoice.SetVolume(m_volumeHud); m_musicAudioVoice.SetVolume(m_volumeMusic); //TODO: JN reinit sounds so they play m_3Dsounds.Clear(); 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); } } } }
public void OnStopPlaying(MySourceVoice voice) { #if DEBUG Debug.Assert(m_debugPlayingList.Contains(voice), string.Format("Debug only. Stopping not playing voice {0}", voice)); m_debugPlayingList.Remove(voice); #endif m_currentCount--; m_availableVoices.Enqueue(voice); }
public void OnStopPlaying(MySourceVoice voice) { #if DEBUG //Debug.Assert(m_debugPlayingList.Contains(voice), string.Format("Debug only. Stopping not playing voice {0}", voice)); m_debugPlayingList.Remove(voice); #endif m_currentCount--; m_availableVoices.Enqueue(voice); }
public IMySourceVoice PlayTestSound(MyStringId cue) { if (cue == MyStringId.NullOrEmpty) { return(null); } MySoundData cueDefinition = m_cueBank.GetCue(cue); // If this computer can't play sound, we don't create cues if (!m_canPlay) { return(null); } if (m_cueBank == null) { return(null); } MySourceVoice sound = m_cueBank.GetVoice(cue); if (sound == null) { sound = m_cueBank.GetVoice(cue, MySoundDimensions.D3); } if (sound == null) { return(null); } if (cueDefinition.PitchVariation != 0f) { float semitones = PitchVariation(cueDefinition); sound.FrequencyRatio = SemitonesToFrequencyRatio(semitones); } else { sound.FrequencyRatio = 1f; } float volume = cueDefinition.Volume; if (cueDefinition.VolumeVariation != 0f) { float variation = VolumeVariation(cueDefinition); volume = MathHelper.Clamp(volume + variation, 0f, 1f); } sound.SetVolume(volume); sound.SetOutputVoices(m_gameAudioVoiceDesc); // Play the cue sound.Start(false); return(sound); }
private void PlayMusicByTransition(MyMusicTransition transition) { m_musicCue = PlaySound(m_cueBank.GetTransitionCue(transition.TransitionEnum, transition.Category)); if (m_musicCue != null) { m_musicCue.SetOutputVoices(m_musicAudioVoiceDesc); m_musicAudioVoice.SetVolume(m_volumeMusic); } }
public MyEffectInstance CreateEffect(IMySourceVoice input, MyStringHash effect, MySourceVoice[] cues = null, float? duration = null) { if(!m_effects.ContainsKey(effect)) { Debug.Fail(string.Format("Effect not found: {0}", effect.ToString())); return null; } var instance = new MyEffectInstance(m_effects[effect], input, cues, duration, m_engine); m_activeEffects.Add(instance); return instance; }
internal MySourceVoice NextAvailable() { MySourceVoice voice = null; if (m_owner.DisablePooling || !m_availableVoices.TryDequeue(out voice)) { if (m_currentCount < MAX_COUNT) { voice = new MySourceVoice(this, m_audioEngine, m_waveFormat); m_currentCount++; } } #if DEBUG if (voice != null) m_debugPlayingList.Add(voice); #endif return voice; }
private static void AddVoiceForDebug(MySourceVoice voice) { StringBuilder v = new StringBuilder(voice.CueEnum.ToString()); if (lastSounds.Count < LAST_SOUND_COUNT) { lastSounds.Add(v); } else { lastSounds[lastSoundIndex] = v; } lastSoundIndex++; if (lastSoundIndex >= LAST_SOUND_COUNT) { lastSoundIndex = 0; } }
public void Update() { if (m_owner.DisablePooling) { MySourceVoice voice; for (int i = 0; i < m_availableVoices.Count; i++) { if (m_availableVoices.TryDequeue(out voice)) { voice.DestroyVoice(); } } } int id = 0; while (id < m_fadingOutVoices.Count) { MySourceVoice voice = m_fadingOutVoices[id]; if (!voice.IsValid) { m_fadingOutVoices.RemoveAt(id); //m_fadingOutVoices.Insert(id, m_owner.GetVoice(voice.CueEnum)); //voice = m_fadingOutVoices[id]; } else { if (voice.Voice.Volume < 0.01f) { voice.Voice.Stop(); voice.Voice.FlushSourceBuffers(); // triggers voice's BufferEnd event m_fadingOutVoices.RemoveAt(id); continue; } else { voice.Voice.SetVolume(0.65f * voice.Voice.Volume); } } ++id; } }
internal MySourceVoice NextAvailable() { MySourceVoice voice = null; if (m_owner.DisablePooling || !m_availableVoices.TryDequeue(out voice)) { if (m_currentCount < MAX_COUNT) { voice = new MySourceVoice(this, m_audioEngine, m_waveFormat); m_currentCount++; } } #if DEBUG if (voice != null) { m_debugPlayingList.Add(voice); } #endif return(voice); }
public MyEffectInstance(MyAudioEffect effect, IMySourceVoice input, MySourceVoice[] cues, float? duration, XAudio2 engine) { m_engine = engine; m_effect = effect; var inputSound = input as MySourceVoice; if (inputSound != null && inputSound.IsValid && inputSound.Voice != null && inputSound.Voice.IsValid()) { Debug.Assert(!inputSound.Voice.IsDisposed); var sd = new SoundData() { Sound = inputSound, Pivot = 0, CurrentEffect = 0, OrigVolume = inputSound.Volume, OrigFrequency = inputSound.FrequencyRatio, }; //FilterParameters fp = new FilterParameters(); m_sounds.Add(sd); } foreach(var sound in cues) { Debug.Assert(!sound.Voice.IsDisposed); sound.Start(false); //jn:todo effect command to start sound m_sounds.Add(new SoundData() { Sound = sound, Pivot = 0, CurrentEffect = 0, OrigVolume = sound.Volume, OrigFrequency = sound.FrequencyRatio, }); } if(OutputSound != null) OutputSound.StoppedPlaying += EffectFinished; ComputeDurationAndScale(duration); Update(0); }
internal MySourceVoice GetVoice(MyStringId hashedCue, MySoundDimensions type = MySoundDimensions.D2) { MySoundData cue = GetCue(hashedCue); if ((cue == null) || (cue.Waves == null) || (cue.Waves.Count == 0)) { return(null); } int waveNumber; CuePart part; MyInMemoryWave wave = GetRandomWave(cue, type, out waveNumber, out part); if (wave == null && type == MySoundDimensions.D2) { type = MySoundDimensions.D3; wave = GetRandomWave(cue, type, out waveNumber, out part); } if (wave == null) { return(null); } MySourceVoice voice = GetVoice(hashedCue, wave, part); if (voice == null) { return(null); } if (cue.Loopable) { wave = GetWave(cue, type, waveNumber, CuePart.Loop); if (wave != null) { Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding); if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding) { voice.SubmitSourceBuffer(hashedCue, wave, CuePart.Loop); } else { MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", hashedCue, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.Loop)); } } wave = GetWave(cue, type, waveNumber, CuePart.End); if (wave != null) { Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding); if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding) { voice.SubmitSourceBuffer(hashedCue, wave, CuePart.End); } else { MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", hashedCue, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.End)); } } } return(voice); }
public void Update() { if (m_owner == null || m_audioEngine == null) { return; } int i; if (m_owner.DisablePooling) { MySourceVoice voice; for (i = 0; i < m_voiceBuffer2.Count; i++) { m_voiceBuffer2[i].Dispose(); } m_voiceBuffer2 = m_voiceBuffer; m_voiceBuffer.Clear(); for (i = 0; i < m_availableVoices.Count; i++) { if (m_availableVoices.TryDequeue(out voice)) { voice.DestroyVoice(); m_voiceBuffer.Add(voice); } } } int id = 0; //fading out while (id < m_fadingOutVoices.Count) { MySourceVoice voice = m_fadingOutVoices[id]; if (!voice.IsValid) { m_fadingOutVoices.RemoveAt(id); //m_fadingOutVoices.Insert(id, m_owner.GetVoice(voice.CueEnum)); //voice = m_fadingOutVoices[id]; } else { if (voice.Voice.Volume < 0.01f) { voice.Voice.Stop(); voice.Voice.FlushSourceBuffers(); // triggers voice's BufferEnd event m_fadingOutVoices.RemoveAt(id); continue; } else { voice.Voice.SetVolume(0.65f * voice.Voice.Volume); } } ++id; } //check for invalid voices m_voicesToRemove.Clear(); foreach (MySourceVoice voice in m_allVoices) { if (voice.IsValid == false) { m_voicesToRemove.Add(voice); } } //remove invalid voices while (m_voicesToRemove.Count > 0) { m_allVoices.Remove(m_voicesToRemove[0]); m_voicesToRemove.RemoveAt(0); } //silent sounds playing in large number (sameSoundLimiterCount) if (UseSameSoundLimiter) { //add remaining voices to distance and sort them m_distancedVoices.Clear(); foreach (MySourceVoice voice in m_allVoices) { m_distancedVoices.Add(voice); } m_distancedVoices.Sort(delegate(MySourceVoice x, MySourceVoice y) { return(x.distanceToListener.CompareTo(y.distanceToListener)); }); //silent or un-silent voices MyCueId currentCueId; int j, limit; MySoundData cueDefinition; while (m_distancedVoices.Count > 0) { currentCueId = m_distancedVoices[0].CueEnum; i = 0; cueDefinition = MyAudio.Static.GetCue(currentCueId); limit = cueDefinition.SoundLimit; for (j = 0; j < m_distancedVoices.Count; j++) { if (m_distancedVoices[j].CueEnum.Equals(currentCueId)) { i++; if (limit > 0 && i > limit) { m_distancedVoices[j].Silent = true; } else { m_distancedVoices[j].Silent = false; } m_distancedVoices.RemoveAt(j); j--; } } } } }
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 AddToFadeoutList(MySourceVoice voice) { m_fadingOutVoices.Add(voice); }
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 IMySourceVoice PlayMusicCue(MyCueId musicCue, bool overrideMusicAllowed = false) { if (!m_canPlay || (!m_musicAllowed && !overrideMusicAllowed)) return null; Mute = false; m_musicCue = PlaySound(musicCue); if (m_musicCue != null) { m_musicCue.SetOutputVoices(m_musicAudioVoiceDesc); m_musicAudioVoice.SetVolume(m_volumeMusic); } return m_musicCue; }
IMySourceVoice IMyAudio.GetSound(IMy3DSoundEmitter source, int sampleRate, int channels, MySoundDimensions dimension) { if (!m_canPlay) return null; var waveFormat = new WaveFormat(sampleRate, channels); source.SourceChannels = channels; var sourceVoice = new MySourceVoice(m_audioEngine, waveFormat); float volume = source.CustomVolume.HasValue ? source.CustomVolume.Value : 1; float maxDistance = source.CustomMaxDistance.HasValue ? source.CustomMaxDistance.Value : 0; sourceVoice.SetVolume(volume); if (dimension == MySoundDimensions.D3) { m_helperEmitter.UpdateValuesOmni(source.SourcePosition, source.Velocity, maxDistance, m_deviceDetails.OutputFormat.Channels, MyCurveType.Linear); sourceVoice.distanceToListener = m_x3dAudio.Apply3D(sourceVoice.Voice, m_listener, m_helperEmitter, source.SourceChannels, m_deviceDetails.OutputFormat.Channels, m_calculateFlags, maxDistance, sourceVoice.FrequencyRatio, sourceVoice.Silent, source.Realistic); Update3DCuesState(); Add3DCueToUpdateList(source); ++m_soundInstancesTotal3D; } return sourceVoice; }
private void CheckIfDeviceChanged() { // DeviceCount cannot be called on XAudio >= 2.8 (Windows 8) // Maybe call DeviceCount with XAudio 2.7 and use different API for windows 8 // http://blogs.msdn.com/b/chuckw/archive/2012/04/02/xaudio2-and-windows-8-consumer-preview.aspx // DeviceCount is very slow even on Windows 7 sometimes, don't know why if (m_deviceLost)// || m_lastDeviceCount != m_audioEngine.DeviceCount) { m_deviceLost = false; try { Init(); } 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) { if (m_cueBank != null) m_cueBank.SetAudioEngine(m_audioEngine); m_gameAudioVoice.SetVolume(m_volumeDefault); m_hudAudioVoice.SetVolume(m_volumeHud); m_musicAudioVoice.SetVolume(m_volumeMusic); //TODO: JN reinit sounds so they play m_3Dsounds.Clear(); 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); } } } }
private void PlayMusicByTransition(MyMusicTransition transition) { if (m_cueBank != null && m_musicAllowed) { m_musicCue = PlaySound(m_cueBank.GetTransitionCue(transition.TransitionEnum, transition.Category)); if (m_musicCue != null) { m_musicCue.SetOutputVoices(m_musicAudioVoiceDesc); m_musicAudioVoice.SetVolume(m_volumeMusic); } } }
internal MySourceVoice GetVoice(MyCueId cueId, out int waveNumber, MySoundDimensions type = MySoundDimensions.D2, int tryIgnoreWaveNumber = -1) { waveNumber = -1; MySoundData cue = GetCue(cueId); if ((cue == null) || (cue.Waves == null) || (cue.Waves.Count == 0)) { return(null); } CuePart part; MyInMemoryWave wave = GetRandomWave(cue, type, out waveNumber, out part, tryIgnoreWaveNumber); if (wave == null && type == MySoundDimensions.D2) { type = MySoundDimensions.D3; wave = GetRandomWave(cue, type, out waveNumber, out part, tryIgnoreWaveNumber); } if (wave == null) { return(null); } MySourceVoice voice = GetVoice(cueId, wave, part); if (voice == null) { return(null); } if (cue.Loopable) { wave = GetWave(cue, type, waveNumber, CuePart.Loop); if (wave != null) { Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding); if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding) { voice.SubmitSourceBuffer(cueId, wave, CuePart.Loop); } else { MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", cueId, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.Loop)); } } wave = GetWave(cue, type, waveNumber, CuePart.End); if (wave != null) { Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding); if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding) { voice.SubmitSourceBuffer(cueId, wave, CuePart.End); } else { MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", cueId, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.End)); } } } #if DEBUG if (voice.CueEnum.IsNull == false) { AddVoiceForDebug(voice); } #endif return(voice); }
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 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); } } } } }