예제 #1
0
        public static StreamingSourceVoice Create(XAudio2 xaudio2, IWaveSource waveSource, int bufferSize = 100)
        {
            var    voiceCallback = new VoiceCallback();
            IntPtr ptr           = xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None,
                                                                XAudio2.DefaultFrequencyRatio, voiceCallback,
                                                                null, null);

            return(new StreamingSourceVoice(ptr, voiceCallback, waveSource, bufferSize));
        }
예제 #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="StreamingSourceVoice" /> class.
        /// </summary>
        /// <param name="ptr">Pointer to a <see cref="XAudio2SourceVoice" /> object.</param>
        /// <param name="voiceCallback">
        ///     <see cref="VoiceCallback" /> instance which receives notifications from the
        ///     <see cref="XAudio2SourceVoice" /> which got passed as a pointer (see the <paramref name="ptr" /> argument).
        /// </param>
        /// <param name="waveSource"><see cref="IWaveSource" /> which provides the audio data to stream.</param>
        /// <param name="bufferSize">
        ///     Buffersize of the internal used buffers in milliseconds. Values in the range from 70ms to
        ///     200ms are recommended.
        /// </param>
        /// <remarks>It is recommended to use the <see cref="Create" /> method instead of the this constructor.</remarks>
        public StreamingSourceVoice(IntPtr ptr, VoiceCallback voiceCallback, IWaveSource waveSource, int bufferSize)
        {
            BasePtr = ptr;
            _voiceCallback = voiceCallback;
            _waveSource = waveSource;

            var maxBufferBytes = (int) waveSource.WaveFormat.MillisecondsToBytes(bufferSize);
            _buffer = new byte[maxBufferBytes];

            for (int i = 0; i < _buffers.Length; i++)
            {
                var buffer = new XAudio2Buffer(maxBufferBytes);
                _buffers[i] = buffer;
            }

            InitializeForStreaming();
        }
예제 #3
0
        internal StreamingSourceVoice(XAudio2 xaudio2, IWaveSource waveSource, VoiceCallback voiceCallback, int bufferSize)
            : base(CreateSourceVoice(xaudio2, waveSource, voiceCallback), xaudio2.Version)
        {
            _voiceCallback = voiceCallback;
            _waveSource = waveSource;

            var maxBufferBytes = (int)waveSource.WaveFormat.MillisecondsToBytes(bufferSize);
            _buffer = new byte[maxBufferBytes];

            for (int i = 0; i < _buffers.Length; i++)
            {
                var buffer = new XAudio2Buffer(maxBufferBytes);
                _buffers[i] = buffer;
            }

            InitializeForStreaming();
        }
예제 #4
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="StreamingSourceVoice" /> class.
        /// </summary>
        /// <param name="ptr">Pointer to a <see cref="XAudio2SourceVoice" /> object.</param>
        /// <param name="voiceCallback">
        ///     <see cref="VoiceCallback" /> instance which receives notifications from the
        ///     <see cref="XAudio2SourceVoice" /> which got passed as a pointer (see the <paramref name="ptr" /> argument).
        /// </param>
        /// <param name="waveSource"><see cref="IWaveSource" /> which provides the audio data to stream.</param>
        /// <param name="bufferSize">
        ///     Buffersize of the internal used buffers in milliseconds. Values in the range from 70ms to
        ///     200ms are recommended.
        /// </param>
        /// <remarks>It is recommended to use the <see cref="Create(XAudio2,IWaveSource,int)" /> method instead of the this constructor.</remarks>
        internal StreamingSourceVoice(IntPtr ptr, VoiceCallback voiceCallback, IWaveSource waveSource, int bufferSize)
        {
            BasePtr        = ptr;
            _voiceCallback = voiceCallback;
            _waveSource    = waveSource;

            var maxBufferBytes = (int)waveSource.WaveFormat.MillisecondsToBytes(bufferSize);

            _buffer = new byte[maxBufferBytes];

            for (int i = 0; i < _buffers.Length; i++)
            {
                var buffer = new XAudio2Buffer(maxBufferBytes);
                _buffers[i] = buffer;
            }

            InitializeForStreaming();
        }
예제 #5
0
        internal StreamingSourceVoice(XAudio2 xaudio2, IWaveSource waveSource, VoiceCallback voiceCallback, int bufferSize)
            : base(CreateSourceVoice(xaudio2, waveSource, voiceCallback), xaudio2.Version)
        {
            _voiceCallback = voiceCallback;
            _waveSource    = waveSource;

            var maxBufferBytes = (int)waveSource.WaveFormat.MillisecondsToBytes(bufferSize);

            _buffer = new byte[maxBufferBytes];

            for (int i = 0; i < _buffers.Length; i++)
            {
                var buffer = new XAudio2Buffer(maxBufferBytes);
                _buffers[i] = buffer;
            }

            InitializeForStreaming();
        }
예제 #6
0
        private static IntPtr CreateSourceVoice(XAudio2 xaudio2, IWaveSource waveSource, VoiceCallback callback)
        {
            if (xaudio2 == null)
            {
                throw new ArgumentNullException("xaudio2");
            }
            if (waveSource == null)
            {
                throw new ArgumentNullException("waveSource");
            }

            return(xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None,
                                                XAudio2.DefaultFrequencyRatio, callback,
                                                null, null));
        }
예제 #7
0
파일: XAudio2Tests.cs 프로젝트: EQ4/cscore
        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.");
            }
        }
예제 #8
0
        /// <summary>
        ///     Creates an instance of the <see cref="StreamingSourceVoice" /> class.
        /// </summary>
        /// <param name="xaudio2">Instance of the <see cref="XAudio2" /> class.</param>
        /// <param name="waveSource"><see cref="IWaveSource" /> which provides the audio data to stream.</param>
        /// <param name="bufferSize">
        ///     Buffersize of the internal used buffers in milliseconds. Values in the range from 70ms to
        ///     200ms are recommended.
        /// </param>
        /// <returns>Configured <see cref="StreamingSourceVoice" /> instance.</returns>
        public static StreamingSourceVoice Create(XAudio2 xaudio2, IWaveSource waveSource, int bufferSize = 100)
        {
            var voiceCallback = new VoiceCallback();
            IntPtr ptr = xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None,
                XAudio2.DefaultFrequencyRatio, voiceCallback,
                null, null);

            return new StreamingSourceVoice(ptr, voiceCallback, waveSource, bufferSize);
        }
예제 #9
0
        private static IntPtr CreateSourceVoice(XAudio2 xaudio2, IWaveSource waveSource, VoiceCallback callback)
        {
            if (xaudio2 == null)
                throw new ArgumentNullException("xaudio2");
            if (waveSource == null)
                throw new ArgumentNullException("waveSource");

            return xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None,
                XAudio2.DefaultFrequencyRatio, callback,
                null, null);
        }