예제 #1
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();
        }
예제 #2
0
 static XAudio2PlaybackEngine()
 {
     device = new AudioDevice();
     voice  = new MasteringVoice(device);
     AppDomain.CurrentDomain.ProcessExit += (o, args) =>
     {
         voice.Dispose();
         device.Dispose();
     };
 }
예제 #3
0
        public void Dispose()
        {
            lock (mutex)
            {
                DisposeWave();

                if (decodeThread != null)
                {
                    decodeThread.Abort();
                    System.Threading.Monitor.Pulse(mutex);
                    decodeThread = null;
                }

                if (streamBuffers != null)
                {
                    foreach (AudioBuffer buffer in streamBuffers)
                    {
                        buffer.AudioData.Dispose();
                        buffer.Dispose();
                    }
                    byteBuffers   = null;
                    streamBuffers = null;
                }

                if (masteringVoice != null)
                {
                    masteringVoice.Dispose();
                    masteringVoice = null;
                }

                if (xaudio != null)
                {
                    xaudio.Dispose();
                    xaudio = null;
                }
            }
        }