internal unsafe void INTERNAL_applyReverb(float rvGain) { if (handle == IntPtr.Zero) { return; } if (!usingReverb) { SoundEffect.Device().AttachReverb(handle); usingReverb = true; } // Re-using this float array... float *outputMatrix = (float *)dspSettings.pMatrixCoefficients; outputMatrix[0] = rvGain; if (dspSettings.SrcChannelCount == 2) { outputMatrix[1] = rvGain; } FAudio.FAudioVoice_SetOutputMatrix( handle, SoundEffect.Device().ReverbVoice, dspSettings.SrcChannelCount, 1, dspSettings.pMatrixCoefficients, 0 ); }
internal void InitDSPSettings(uint srcChannels) { dspSettings = new FAudio.F3DAUDIO_DSP_SETTINGS(); dspSettings.DopplerFactor = 1.0f; dspSettings.SrcChannelCount = srcChannels; dspSettings.DstChannelCount = SoundEffect.Device().DeviceDetails.OutputFormat.Format.nChannels; dspSettings.pMatrixCoefficients = Marshal.AllocHGlobal( 4 * (int)dspSettings.SrcChannelCount * (int)dspSettings.DstChannelCount ); memset( dspSettings.pMatrixCoefficients, '\0', (IntPtr)(4 * dspSettings.SrcChannelCount * dspSettings.DstChannelCount) ); SetPanMatrixCoefficients(); }
public void Apply3D(AudioListener listener, AudioEmitter emitter) { if (listener == null) { throw new ArgumentNullException("listener"); } if (emitter == null) { throw new ArgumentNullException("emitter"); } if (IsDisposed) { throw new ObjectDisposedException( "SoundEffectInstance" ); } is3D = true; SoundEffect.FAudioContext dev = SoundEffect.Device(); emitter.emitterData.CurveDistanceScaler = dev.CurveDistanceScaler; emitter.emitterData.ChannelCount = dspSettings.SrcChannelCount; FAudio.F3DAudioCalculate( dev.Handle3D, ref listener.listenerData, ref emitter.emitterData, ( FAudio.F3DAUDIO_CALCULATE_MATRIX | FAudio.F3DAUDIO_CALCULATE_DOPPLER ), ref dspSettings ); if (handle != IntPtr.Zero) { UpdatePitch(); FAudio.FAudioVoice_SetOutputMatrix( handle, SoundEffect.Device().MasterVoice, dspSettings.SrcChannelCount, dspSettings.DstChannelCount, dspSettings.pMatrixCoefficients, 0 ); } }
private void UpdatePitch() { float doppler; float dopplerScale = SoundEffect.Device().DopplerScale; if (!is3D || dopplerScale == 0.0f) { doppler = 1.0f; } else { doppler = dspSettings.DopplerFactor * dopplerScale; } FAudio.FAudioSourceVoice_SetFrequencyRatio( handle, (float)Math.Pow(2.0, INTERNAL_pitch) * doppler, 0 ); }
internal SoundEffectInstance(SoundEffect parent = null) { SoundEffect.Device(); selfReference = new WeakReference(this, true); parentEffect = parent; isDynamic = this is DynamicSoundEffectInstance; hasStarted = false; is3D = false; usingReverb = false; INTERNAL_state = SoundState.Stopped; if (!isDynamic) { InitDSPSettings(parentEffect.format.nChannels); } if (parentEffect != null) { parentEffect.Instances.Add(selfReference); } }
internal void InitDSPSettings(uint srcChannels) { dspSettings = new FAudio.F3DAUDIO_DSP_SETTINGS(); dspSettings.DopplerFactor = 1.0f; dspSettings.SrcChannelCount = srcChannels; dspSettings.DstChannelCount = SoundEffect.Device().DeviceDetails.OutputFormat.Format.nChannels; int memsize = ( 4 * (int)dspSettings.SrcChannelCount * (int)dspSettings.DstChannelCount ); dspSettings.pMatrixCoefficients = Marshal.AllocHGlobal(memsize); unsafe { byte *memPtr = (byte *)dspSettings.pMatrixCoefficients; for (int i = 0; i < memsize; i += 1) { memPtr[i] = 0; } } SetPanMatrixCoefficients(); }
public virtual void Play() { if (State == SoundState.Playing) { return; } if (State == SoundState.Paused) { /* Just resume the existing handle */ FAudio.FAudioSourceVoice_Start(handle, 0, 0); INTERNAL_state = SoundState.Playing; return; } SoundEffect.FAudioContext dev = SoundEffect.Device(); /* Create handle */ FAudio.FAudioWaveFormatEx fmt = isDynamic ? (this as DynamicSoundEffectInstance).format : parentEffect.format; FAudio.FAudio_CreateSourceVoice( dev.Handle, out handle, ref fmt, FAudio.FAUDIO_VOICE_USEFILTER, FAudio.FAUDIO_DEFAULT_FREQ_RATIO, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); if (handle == IntPtr.Zero) { return; /* What */ } /* Apply current properties */ FAudio.FAudioVoice_SetVolume(handle, INTERNAL_volume, 0); UpdatePitch(); if (is3D || Pan != 0.0f) { FAudio.FAudioVoice_SetOutputMatrix( handle, SoundEffect.Device().MasterVoice, dspSettings.SrcChannelCount, dspSettings.DstChannelCount, dspSettings.pMatrixCoefficients, 0 ); } /* For static effects, submit the buffer now */ if (isDynamic) { (this as DynamicSoundEffectInstance).QueueInitialBuffers(); } else { if (IsLooped) { parentEffect.handle.LoopCount = 255; parentEffect.handle.LoopBegin = parentEffect.loopStart; parentEffect.handle.LoopLength = parentEffect.loopLength; } else { parentEffect.handle.LoopCount = 0; parentEffect.handle.LoopBegin = 0; parentEffect.handle.LoopLength = 0; } FAudio.FAudioSourceVoice_SubmitSourceBuffer( handle, ref parentEffect.handle, IntPtr.Zero ); } /* Play, finally. */ FAudio.FAudioSourceVoice_Start(handle, 0, 0); INTERNAL_state = SoundState.Playing; hasStarted = true; }