public override void Dispose(bool disposing) { if (disposing) { if (sourceVoice != null) { sourceVoice.FlushSourceBuffers(); sourceVoice.Stop(); sourceVoice.Dispose(); sourceVoice = null; } if (audioBuffer != null) { audioBuffer.AudioData.Dispose(); audioBuffer.AudioData = null; // Just to be clean… audioBuffer.Dispose(); audioBuffer = null; } if (xAudio != null) { xAudio.StopEngine(); xAudio.Dispose(); xAudio = null; } } }
private void DisposeWave() { lock (mutex) { if (dataStream != null) { dataStream.Dispose(); dataStream = null; } if (audioBuffer != null) { audioBuffer.Dispose(); audioBuffer = null; } if (sourceVoice != null) { if (currentStream != null) { sourceVoice.BufferEnd -= bufferEndCallback; } sourceVoice.Stop(); sourceVoice.FlushSourceBuffers(); sourceVoice.Dispose(); sourceVoice = null; } current = null; currentStream = null; } }
public Form1() { InitializeComponent(); var mainThread = Thread.CurrentThread; var audioThread = new Thread(() => { var xaDev = new XA.XAudio2(); xaDev.StartEngine(); var wf = new WaveFormat { FormatTag = WaveFormatTag.Pcm, Channels = 2, BitsPerSample = 16, BlockAlignment = 4, SamplesPerSecond = SampleRate, AverageBytesPerSecond = SampleRate * 4 }; // ReSharper disable once ObjectCreationAsStatement new XA.MasteringVoice(xaDev, 2, SampleRate, 0); var xaSv = new XA.SourceVoice(xaDev, wf, XA.VoiceFlags.None); var xaBuf = new XA.AudioBuffer(); var data = new byte[Samples * 4]; ReadWave(data); xaBuf.AudioData = new MemoryStream(data, false); xaBuf.AudioBytes = data.Length; xaSv.SubmitSourceBuffer(xaBuf); xaSv.BufferStart += delegate { if (!mainThread.IsAlive) { return; } ReadWave(data); xaBuf.AudioData.Position = 0; xaSv.SubmitSourceBuffer(xaBuf); }; xaSv.Start(); while (mainThread.IsAlive) { lock (tones) { MidiUpdate(); } Thread.Sleep(0); } xaSv.Stop(); xaSv.Dispose(); xaDev.Dispose(); midi.Dispose(); }); audioThread.Start(); }
protected override void EndBufferChange() { if (AudioBuffer != null) { if (audioBuffer == null) audioBuffer = new XAudioBuffer(); audioBuffer.AudioData = new DataStream(AudioBuffer.RawBuffer, true, true); audioBuffer.AudioBytes = (int)audioBuffer.AudioData.Length; audioBuffer.LoopLength = AudioBuffer.RawBuffer.Length / 2; audioBuffer.LoopCount = XAudio2.LoopInfinite; waveFormat.AverageBytesPerSecond = (waveFormat.BlockAlignment = (short)((waveFormat.BitsPerSample = (short)BitsPerSample) / 8 * (waveFormat.Channels = 2))) * (waveFormat.SamplesPerSecond = Frequency); sourceVoice = new SourceVoice(xAudio, waveFormat); sourceVoice.SubmitSourceBuffer(audioBuffer); sourceVoice.Start(); } }
public void Play(IPlayable wave) { lock (mutex) { if (wave == current) { if (isPaused) { sourceVoice.Start(); isPaused = false; return; } } DisposeWave(); xaudio.CommitChanges(); isPaused = false; current = wave; currentStream = wave as IPlayableStream; if (decodeThread != null) { System.Threading.Monitor.Pulse(mutex); decodeThread = null; } if (currentStream == null) { sourceVoice = new SourceVoice(xaudio, wave.WaveFormat); dataStream = new SlimDX.DataStream(wave.WaveData, true, false); audioBuffer = new SlimDX.XAudio2.AudioBuffer(); audioBuffer.AudioData = dataStream; audioBuffer.AudioBytes = wave.WaveData.Length; audioBuffer.Flags = BufferFlags.EndOfStream; sourceVoice.SubmitSourceBuffer(audioBuffer); sourceVoice.Start(); } else { streamLength = currentStream.StreamLength; streamBuffered = 0; sourceVoice = new SourceVoice(xaudio, wave.WaveFormat); sourceVoice.BufferEnd += bufferEndCallback; // Fill buffers initially bool isDone = false; for (var i = 0; i < NumStreamingBuffers; i++) { isDone = FillBuffer(i, currentStream); if (isDone) { break; } } sourceVoice.Start(); if (!isDone) { System.Threading.ParameterizedThreadStart threadProc = DecoderThread; decodeThread = new System.Threading.Thread(threadProc); decodeThread.Name = "Vorbis Decoder Thread"; decodeThread.Start(currentStream); } } } }