public AudioPlayer(XAudio2 xaudio2, Stream audioStream)
{
this.xaudio2 = xaudio2;
audioDecoder = new AudioDecoder(audioStream);
sourceVoice = new SourceVoice(xaudio2, audioDecoder.WaveFormat);
localVolume = 1.0f;
crossfade = TimeSpan.FromSeconds(10);
sourceVoice.BufferEnd += sourceVoice_BufferEnd;
sourceVoice.Start();
bufferEndEvent = new AutoResetEvent(false);
playEvent = new ManualResetEvent(false);
waitForPlayToOutput = new ManualResetEvent(false);
clock = new Stopwatch();
// Pre-allocate buffers
audioBuffersRing = new AudioBuffer[3];
memBuffers = new DataPointer[audioBuffersRing.Length];
for (int i = 0; i < audioBuffersRing.Length; i++)
{
audioBuffersRing[i] = new AudioBuffer();
memBuffers[i].Size = 32 * 1024; // default size 32Kb
memBuffers[i].Pointer = Utilities.AllocateMemory(memBuffers[i].Size);
}
// Initialize to stopped
State = AudioPlayerState.Stopped;
// Starts the playing thread
playingTask = Task.Factory.StartNew(PlayAsync, TaskCreationOptions.LongRunning);
}