public NAudioStreamAudioSourceToWaveProviderAdapterSimple(IStreamAudioSource source)
 {
     this.WaveFormat = NAudioUtilities.ToNAudioWaveFormat(source.Format);
 }
Esempio n. 2
0
        /// <summary>
        /// Initializes this resampler with the given input audio source and output format.
        /// Attaches to the given source's event to start resampling as soon as <see cref="IStreamAudioSource.DataAvailable"/> is raised.
        /// </summary>
        /// <param name="audioSource"></param>
        /// <param name="outputFormat"></param>
        public void Initialize(IStreamAudioSource audioSource, WaveStreamAudioFormat outputFormat)
        {
            this.WaveProviderAdapter = new NAudioStreamAudioSourceToWaveProviderAdapterSimple(audioSource);
            this.Resampler           = new MediaFoundationResampler(this.WaveProviderAdapter, NAudioUtilities.ToNAudioWaveFormat(outputFormat));

            //set this *after* we initialize the resampler. if it throws, we won't dispose the input audio source by accident
            this.WrappedAudioSource = audioSource;
            this.Format             = outputFormat;

            //handle events from the wrapped source
            audioSource.DataAvailable += (s, e) =>
            {
                //feed into our adapter
                WaveProviderAdapter.Write(e);

                //read from resampler and trigger our own output event
                int read;
                while ((read = Resampler.Read(Buffer, 0, Buffer.Length)) > 0)
                {
                    DataAvailable?.Invoke(this, new StreamAudioSourceDataEvent()
                    {
                        Buffer = new ArraySegment <byte>(Buffer, 0, read),
                        Format = Format
                    });
                }
            };
        }