Пример #1
0
        public void CanRegisterSourceVoiceCallback()
        {
            const int lengthInSeconds = 2;
            bool      b0 = false, b1 = false;

            var callback = new VoiceCallback();

            using (var masteringVoice = _xaudio2.CreateMasteringVoice())
                using (var sourceVoice = _xaudio2.CreateSourceVoice(_source.WaveFormat, VoiceFlags.None, CSCore.XAudio2.XAudio2.DefaultFrequencyRatio, callback, null, null))
                {
                    callback.BufferStart += (s, e) => b0 = true;
                    callback.BufferEnd   += (s, e) => b1 = true;

                    byte[] rawBuffer = new byte[_source.WaveFormat.BytesPerSecond * lengthInSeconds];
                    int    read      = _source.Read(rawBuffer, 0, rawBuffer.Length);

                    var buffer = new XAudio2Buffer(read);
                    using (var stream = buffer.GetStream())
                    {
                        stream.Write(rawBuffer, 0, read);
                    }

                    sourceVoice.SubmitSourceBuffer(buffer);
                    sourceVoice.Start();

                    Thread.Sleep(lengthInSeconds * 1000 + 2000);

                    Assert.IsTrue(b0, "No BufferStart.");
                    Assert.IsTrue(b1, "No BufferEnd.");
                }
        }
Пример #2
0
        private static void PlayWithoutStreaming(IWaveSource waveSource)
        {
            using (var xaudio2 = XAudio2.CreateXAudio2())
                using (var masteringVoice = xaudio2.CreateMasteringVoice()) //ALWAYS create at least one masteringVoice.
                    using (var sourceVoice = xaudio2.CreateSourceVoice(waveSource.WaveFormat))
                    {
                        var buffer = waveSource.ToByteArray();
                        using (var sourceBuffer = new XAudio2Buffer(buffer.Length))
                        {
                            using (var stream = sourceBuffer.GetStream())
                            {
                                stream.Write(buffer, 0, buffer.Length);
                            }

                            sourceVoice.SubmitSourceBuffer(sourceBuffer);
                        }

                        sourceVoice.Start();

                        Console.WriteLine("Press any key to exit.");
                        Console.ReadKey();

                        sourceVoice.Stop();
                    }
        }
Пример #3
0
        public void CanRegisterEngineCallback()
        {
            const int lengthInSeconds = 2;
            bool      b0 = false, b1 = false;

            XAudio2EngineCallback engineCallback = new XAudio2EngineCallback();

            engineCallback.ProcessingPassStart           += (s, e) =>
                                                       b0 = true;
            engineCallback.ProcessingPassEnd             += (s, e) =>
                                                       b1 = true;

            _xaudio2.RegisterForCallbacks(engineCallback);

            using (var masteringVoice = _xaudio2.CreateMasteringVoice())
                using (var sourceVoice = _xaudio2.CreateSourceVoice(_source.WaveFormat))
                {
                    byte[] rawBuffer = new byte[_source.WaveFormat.BytesPerSecond * lengthInSeconds];
                    int    read      = _source.Read(rawBuffer, 0, rawBuffer.Length);

                    var buffer = new XAudio2Buffer(read);
                    using (var stream = buffer.GetStream())
                    {
                        stream.Write(rawBuffer, 0, read);
                    }

                    sourceVoice.SubmitSourceBuffer(buffer);
                    sourceVoice.Start();

                    Thread.Sleep((lengthInSeconds * 1000 + 500) / 2);

                    Assert.IsTrue(b0 || b1);

                    Console.WriteLine("Unregistered");
                    _xaudio2.UnregisterForCallbacks(engineCallback);
                    b0 = b1 = false;

                    Thread.Sleep((lengthInSeconds * 1000 + 500) / 4);

                    Assert.IsFalse(b0 && b1);

                    Console.WriteLine("Register");
                    _xaudio2.RegisterForCallbacks(engineCallback);

                    Thread.Sleep((lengthInSeconds * 1000 + 500) / 4);

                    Assert.IsTrue(b0 || b1);
                }

            _xaudio2.UnregisterForCallbacks(engineCallback);
        }
Пример #4
0
        public void CanSubmitSourceBuffer()
        {
            const int lengthInSeconds = 2;

            using (var masteringVoice = _xaudio2.CreateMasteringVoice())
                using (var sourceVoice = _xaudio2.CreateSourceVoice(_source.WaveFormat))
                {
                    byte[] rawBuffer = new byte[_source.WaveFormat.BytesPerSecond * lengthInSeconds];
                    int    read      = _source.Read(rawBuffer, 0, rawBuffer.Length);

                    var buffer = new XAudio2Buffer(read);
                    using (var stream = buffer.GetStream())
                    {
                        stream.Write(rawBuffer, 0, read);
                    }

                    sourceVoice.SubmitSourceBuffer(buffer);
                    sourceVoice.Start();

                    Thread.Sleep(lengthInSeconds * 1000 + 500);
                }
        }