public void SubmitBuffer(byte[] buffer, int offset, int count) { IntPtr next = Marshal.AllocHGlobal(count); Marshal.Copy(buffer, offset, next, count); queuedBuffers.Add(next); if (State != SoundState.Stopped) { FAudio.FAudioBuffer buf = new FAudio.FAudioBuffer(); buf.AudioBytes = (uint)count; buf.pAudioData = next; buf.PlayLength = ( buf.AudioBytes / (uint)channels / (uint)(format.wBitsPerSample / 8) ); FAudio.FAudioSourceVoice_SubmitSourceBuffer( handle, ref buf, IntPtr.Zero ); } else { queuedSizes.Add((uint)count); } }
public void SubmitFloatBufferEXT(float[] buffer, int offset, int count) { /* Float samples are the typical format received from decoders. * We currently use this for the VideoPlayer. * -flibit */ if (State != SoundState.Stopped && format.wFormatTag == 1) { throw new InvalidOperationException( "Submit a float buffer before Playing!" ); } format.wFormatTag = 3; format.wBitsPerSample = 32; format.nBlockAlign = (ushort)(4 * format.nChannels); format.nAvgBytesPerSec = format.nBlockAlign * format.nSamplesPerSec; IntPtr next = Marshal.AllocHGlobal(count * sizeof(float)); Marshal.Copy(buffer, offset, next, count); lock (queuedBuffers) { queuedBuffers.Add(next); if (State != SoundState.Stopped) { FAudio.FAudioBuffer buf = new FAudio.FAudioBuffer(); buf.AudioBytes = (uint)count * sizeof(float); buf.pAudioData = next; buf.PlayLength = ( buf.AudioBytes / (uint)channels / (uint)(format.wBitsPerSample / 8) ); FAudio.FAudioSourceVoice_SubmitSourceBuffer( handle, ref buf, IntPtr.Zero ); } else { queuedSizes.Add((uint)count * sizeof(float)); } } }
internal void QueueInitialBuffers() { FAudio.FAudioBuffer buffer = new FAudio.FAudioBuffer(); for (int i = 0; i < queuedBuffers.Count; i += 1) { buffer.AudioBytes = queuedSizes[i]; buffer.pAudioData = queuedBuffers[i]; buffer.PlayLength = ( buffer.AudioBytes / (uint)channels / (uint)(format.wBitsPerSample / 8) ); FAudio.FAudioSourceVoice_SubmitSourceBuffer( handle, ref buffer, IntPtr.Zero ); } queuedSizes.Clear(); }
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; }
internal unsafe SoundEffect( string name, byte[] buffer, int offset, int count, byte[] extraData, ushort wFormatTag, ushort nChannels, uint nSamplesPerSec, uint nAvgBytesPerSec, ushort nBlockAlign, ushort wBitsPerSample, int loopStart, int loopLength ) { Device(); Name = name; channels = nChannels; sampleRate = nSamplesPerSec; this.loopStart = (uint)loopStart; this.loopLength = (uint)loopLength; /* Buffer format */ if (extraData == null) { formatPtr = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(FAudio.FAudioWaveFormatEx)) ); } else { formatPtr = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(FAudio.FAudioWaveFormatEx)) + extraData.Length ); Marshal.Copy( extraData, 0, formatPtr + Marshal.SizeOf(typeof(FAudio.FAudioWaveFormatEx)), extraData.Length ); } FAudio.FAudioWaveFormatEx *pcm = (FAudio.FAudioWaveFormatEx *)formatPtr; pcm->wFormatTag = wFormatTag; pcm->nChannels = nChannels; pcm->nSamplesPerSec = nSamplesPerSec; pcm->nAvgBytesPerSec = nAvgBytesPerSec; pcm->nBlockAlign = nBlockAlign; pcm->wBitsPerSample = wBitsPerSample; pcm->cbSize = (ushort)((extraData == null) ? 0 : extraData.Length); /* 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) ); } else if (wFormatTag == 0x166) { FAudio.FAudioXMA2WaveFormatEx *xma2 = (FAudio.FAudioXMA2WaveFormatEx *)formatPtr; // dwSamplesEncoded / nChannels / (wBitsPerSample / 8) doesn't always (if ever?) match up. handle.PlayLength = xma2->dwPlayLength; } /* Set by Instances! */ handle.LoopBegin = 0; handle.LoopLength = 0; handle.LoopCount = 0; }