public unsafe void Calculate(Listener listener, Emitter emitter, CalculateFlags flags, DspSettings* result) { Debug.Assert((flags & CalculateFlags.Delay) == 0 || (result->DelayTimesPointer != IntPtr.Zero), "When CalculateFlags.Delay is specified, DelayTimesPointer must point to float[DstChannelCount]"); Debug.Assert((flags & CalculateFlags.Matrix) == 0 || (result->MatrixCoefficientsPointer != IntPtr.Zero), "When CalculateFlags.Matrix is specified, MatrixCoefficientsPointer must point to float[SrcChannelCount*DstChannelCount]"); Cone emitterCone; Emitter.Native nativeEmitter = new Emitter.Native(); Cone listenerCone; Listener.Native nativeListener = new Listener.Native(); { if (emitter.Cone.HasValue) { emitterCone = emitter.Cone.Value; nativeEmitter.ConePointer = new IntPtr(&emitterCone); } else { nativeEmitter.ConePointer = IntPtr.Zero; } nativeEmitter.OrientFront = emitter.OrientFront; nativeEmitter.OrientTop = emitter.OrientTop; nativeEmitter.Position = emitter.Position; nativeEmitter.Velocity = emitter.Velocity; nativeEmitter.InnerRadius = emitter.InnerRadius; nativeEmitter.InnerRadiusAngle = emitter.InnerRadiusAngle; nativeEmitter.ChannelCount = emitter.ChannelCount; nativeEmitter.ChannelRadius = emitter.ChannelRadius; nativeEmitter.ChannelAzimuthsPointer = emitter.ChannelAzimuths.ToPointer(); nativeEmitter.VolumeCurvePointer = emitter.VolumeCurve.ToPointer(); nativeEmitter.LFECurvePointer = emitter.LFECurve.ToPointer(); nativeEmitter.LPFDirectCurvePointer = emitter.LPFDirectCurve.ToPointer(); nativeEmitter.LPFReverbCurvePointer = emitter.LPFReverbCurve.ToPointer(); nativeEmitter.ReverbCurvePointer = emitter.ReverbCurve.ToPointer(); nativeEmitter.CurveDistanceScaler = emitter.CurveDistanceScaler; nativeEmitter.DopplerScaler = emitter.DopplerScaler; if (listener.Cone.HasValue) { listenerCone = listener.Cone.Value; nativeListener.ConePointer = new IntPtr(&listenerCone); } else { nativeListener.ConePointer = IntPtr.Zero; } nativeListener.OrientFront = listener.OrientFront; nativeListener.OrientTop = listener.OrientTop; nativeListener.Position = listener.Position; nativeListener.Velocity = listener.Velocity; fixed (X3DAudioHandle* handle = &m_x3dAudioHandle) { X3DAudioCalculate_(handle, &nativeListener, &nativeEmitter, (int)flags, result); } } }
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 float Apply3D(SourceVoice voice, Listener listener, Emitter emitter, int srcChannels, int dstChannels, CalculateFlags flags, float maxDistance, float frequencyRatio, bool silent) { unsafe { DspSettings settings; int matrixCoefficientCount = srcChannels * dstChannels; float* matrixCoefficients = stackalloc float[matrixCoefficientCount]; float* delay = stackalloc float[dstChannels]; settings.SrcChannelCount = srcChannels; settings.DstChannelCount = dstChannels; settings.MatrixCoefficientsPointer = new IntPtr(matrixCoefficients); settings.DelayTimesPointer = new IntPtr(delay); Calculate(listener, emitter, flags, &settings); if (emitter.InnerRadius == 0f) { // approximated decay by distance float decay; if (silent) decay = 0f; else decay = MathHelper.Clamp(1f - settings.EmitterToListenerDistance / maxDistance, 0f, 1f); for (int i = 0; i < matrixCoefficientCount; i++) { matrixCoefficients[i] *= decay; } } voice.SetOutputMatrix(null, settings.SrcChannelCount, settings.DstChannelCount, matrixCoefficients); voice.SetFrequencyRatio(frequencyRatio * settings.DopplerFactor); return settings.EmitterToListenerDistance; } }
public float Apply3D(SourceVoice voice, Listener listener, Emitter emitter, int srcChannels, int dstChannels, CalculateFlags flags, float maxDistance, float frequencyRatio, bool silent, bool use3DCalculation = true) { unsafe { DspSettings settings; int matrixCoefficientCount = srcChannels * dstChannels; float* matrixCoefficients = stackalloc float[matrixCoefficientCount]; float* delay = stackalloc float[dstChannels]; settings.SrcChannelCount = srcChannels; settings.DstChannelCount = dstChannels; settings.MatrixCoefficientsPointer = new IntPtr(matrixCoefficients); settings.DelayTimesPointer = new IntPtr(delay); if (use3DCalculation) { Calculate(listener, emitter, flags, &settings); voice.SetFrequencyRatio(frequencyRatio * settings.DopplerFactor); } else { //realistic sounds settings.EmitterToListenerDistance = Vector3.Distance(new Vector3(listener.Position.X, listener.Position.Y, listener.Position.Z), new Vector3(emitter.Position.X, emitter.Position.Y, emitter.Position.Z)); for(int i = 0; i < matrixCoefficientCount; i++) matrixCoefficients[i] = 1f; } if (emitter.InnerRadius == 0f) { // approximated decay by distance float decay; if (silent) decay = 0f; else decay = MathHelper.Clamp(1f - settings.EmitterToListenerDistance / maxDistance, 0f, 1f); for (int i = 0; i < matrixCoefficientCount; i++) { matrixCoefficients[i] *= decay; } } #if !XB1 voice.SetOutputMatrix(null, settings.SrcChannelCount, settings.DstChannelCount, matrixCoefficients); #else // XB1 var matCoefs = new float[matrixCoefficientCount]; for (int i = 0; i < matrixCoefficientCount; i++) matCoefs[i] = matrixCoefficients[i]; voice.SetOutputMatrix(null, settings.SrcChannelCount, settings.DstChannelCount, matCoefs); #endif // XB1 return settings.EmitterToListenerDistance; } }