Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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();
        }