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(); }
public void Pause() { lock (mutex) { if (sourceVoice != null) { if (isPaused) { sourceVoice.Start(); isPaused = false; } else { sourceVoice.Stop(); isPaused = true; } } } }