public DynamicSoundEffectInstance( int sampleRate, AudioChannels channels ) : base() { this.sampleRate = sampleRate; this.channels = channels; isDynamic = true; format = new FAudio.FAudioWaveFormatEx(); format.wFormatTag = 1; format.nChannels = (ushort)channels; format.nSamplesPerSec = (uint)sampleRate; format.wBitsPerSample = 16; format.nBlockAlign = (ushort)(2 * format.nChannels); format.nAvgBytesPerSec = format.nBlockAlign * format.nSamplesPerSec; format.cbSize = 0; queuedBuffers = new List <IntPtr>(); queuedSizes = new List <uint>(); InitDSPSettings(format.nChannels); }
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; }
internal SoundEffect( string name, byte[] buffer, int offset, int count, ushort wFormatTag, ushort nChannels, uint nSamplesPerSec, uint nAvgBytesPerSec, ushort nBlockAlign, ushort wBitsPerSample, int loopStart, int loopLength ) { Device(); Name = name; this.loopStart = (uint)loopStart; this.loopLength = (uint)loopLength; /* Buffer format */ format = new FAudio.FAudioWaveFormatEx(); format.wFormatTag = wFormatTag; format.nChannels = nChannels; format.nSamplesPerSec = nSamplesPerSec; format.nAvgBytesPerSec = nAvgBytesPerSec; format.nBlockAlign = nBlockAlign; format.wBitsPerSample = wBitsPerSample; format.cbSize = 0; /* May be needed for ADPCM? */ /* Easy stuff */ handle = new FAudio.FAudioBuffer(); handle.Flags = FAudio.FAUDIO_END_OF_STREAM; handle.pContext = IntPtr.Zero; /* Buffer data */ handle.AudioBytes = (uint)count; handle.pAudioData = Marshal.AllocHGlobal(count); Marshal.Copy( buffer, offset, handle.pAudioData, count ); /* Play regions */ handle.PlayBegin = 0; if (wFormatTag == 1) { handle.PlayLength = (uint)( count / nChannels / (wBitsPerSample / 8) ); } else if (wFormatTag == 2) { handle.PlayLength = (uint)( count / nBlockAlign * (((nBlockAlign / nChannels) - 6) * 2) ); } /* Set by Instances! */ handle.LoopBegin = 0; handle.LoopLength = 0; handle.LoopCount = 0; }