This project has moved. For the latest updates, please go here.

Read WaveInProvider buffer

Oct 20, 2014 at 8:04 AM
Hello, i'm a newbie (really a newbie, just learn C# ~2 weeks). I'm developing CW demodulation program using NAudio library. I'm trying to read data that saved in WaveInProvider buffer ang display it on textbox. Can I read it? if I can how i can do that?

I'm doing this because i want to verify that the buffer has some data. The data taken from input device (stereo mix/ microphone). For now I have create a loopback program from input device.

Thank you do much
Coordinator
Oct 21, 2014 at 10:56 AM
To record audio, use WaveIn and handle DataAvailable.
There will be a lot of data, too much to fit into a textbox. But in the event args for DataAvailable you can find out how many bytes were recorded, and examine the raw sample data.
Oct 22, 2014 at 5:32 AM
Thank you for the reply mark. Now i can see the data from loopback. How to integrate the FFT function?

I have search on google and try every solution. But somehow the data can't go into FFT function and the complex[] result always zero although there is a data captured by loopback. I'm using sampleAggregator too.

Thank you so much
Coordinator
Oct 23, 2014 at 9:36 PM
Hi, I'm afraid the SampleAggregator in the WPF NAudio Demo is the only example of doing this I have available. Are you playing the sample aggregator? The audio does need to be pulled through it or FFTs won't be calculated.
Oct 28, 2014 at 1:33 AM
Edited Oct 28, 2014 at 9:31 AM
Hi, mark. yes I play with the SampleAggregator. Now I can do FFT and visualize the result using ZedGraph. I want to ask about the FFT result. I'm getting three same reult value in one time for one sample point. Is it possible? or there is something wrong with my program?

and one more, i want to put the FFT result into 3 seperate buffer so while the first buffer processed by a function, the result will fill the second and third buffer. After the first buffer finished, the second buffer will go into the function and so on. Is it better to use time based buffer like buffer for 100ms or using buffer with the same size of fft length and send to the function after it filled up?

and what is the size of e.buffer from WaveInEventArgs? can i change the size of the buffer?

thank you so much.
Nov 3, 2014 at 4:55 AM
Edited Nov 4, 2014 at 4:31 AM
Hi mark, now i'm trying to put the equalizer from the wpf demo to my program. But i can't put the setPeakingEq method from BiQuadFilter. Are you using different library for the demo?

And can someone please explain about gow to change IWaveIn to ISampleProvider/IWaveProvider?
Nov 4, 2014 at 2:10 PM
Don't know if this helped you, but this is how i implemented EQ:

EqualizerSampleProvider
/// <summary>
    /// Basic example of a multi-band eq
    /// uses the same settings for both channels in stereo audio
    /// Call Update after you've updated the bands
    /// Potentially to be added to NAudio in a future version
    /// </summary>
    internal class EqualizerSampleProvider : ISampleProvider
    {
        private readonly ISampleProvider sourceProvider;
        private readonly EqualizerBand[] bands;
        private readonly BiQuadFilter[,] filters;
        private readonly int channels;
        private readonly int bandCount;
        private bool updated;

        public EqualizerSampleProvider(ISampleProvider sourceProvider, EqualizerBand[] bands)
        {
            this.sourceProvider = sourceProvider;
            this.bands = bands;
            channels = sourceProvider.WaveFormat.Channels;
            bandCount = bands.Length;
            filters = new BiQuadFilter[channels, bands.Length];
            CreateFilters();
        }

        private void CreateFilters()
        {
            for (int bandIndex = 0; bandIndex < bandCount; bandIndex++)
            {
                var band = bands[bandIndex];
                for (int n = 0; n < channels; n++)
                {
                    if (filters[n, bandIndex] == null)
                        filters[n, bandIndex] = BiQuadFilter.PeakingEQ(sourceProvider.WaveFormat.SampleRate, band.Frequency, band.Bandwidth, band.Gain);
                    else
                        filters[n, bandIndex].SetPeakingEq(sourceProvider.WaveFormat.SampleRate, band.Frequency, band.Bandwidth, band.Gain);
                }
            }
        }

        public void Update()
        {
            updated = true;
            CreateFilters();
        }

        public WaveFormat WaveFormat { get { return sourceProvider.WaveFormat; } }

        public int Read(float[] buffer, int offset, int count)
        {
            int samplesRead = sourceProvider.Read(buffer, offset, count);

            if (updated)
            {
                CreateFilters();
                updated = false;
            }

            for (int n = 0; n < samplesRead; n++)
            {
                int ch = n % channels;

                for (int band = 0; band < bandCount; band++)
                {
                    buffer[offset + n] = filters[ch, band].Transform(buffer[offset + n]);
                }
            }
            return samplesRead;
        }
    }
EqualizerBand
public class EqualizerBand
    {
        private float frequency;
        private float gain;
        private float bandwidth;


        public event EventHandler Updated;

        public float Frequency
        {
            get { return this.frequency; }
            set
            {
                this.frequency = value;

                OnUpdated();
            }
        }

        public float Gain
        {
            get { return this.gain; }
            set
            {
                this.gain = value;

                OnUpdated();
            }
        }

        public float Bandwidth
        {
            get { return this.bandwidth; }
            set
            {
                this.bandwidth = value;

                OnUpdated();
            }
        }


        private void OnUpdated()
        {
            if (Updated != null)
            {
                Updated(this, EventArgs.Empty);
            }
        }
    }
equalizerBands = new EqualizerBand[]
                {
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 100, Gain = 0},
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 200, Gain = 0},
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 400, Gain = 0},
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 800, Gain = 0},
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 1200, Gain = 0},
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 2400, Gain = 0},
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 4800, Gain = 0},
                    new EqualizerBand {Bandwidth = 0.8f, Frequency = 9600, Gain = 0},
                };
waveToSample = new WaveToSampleProvider(waveChannel);
equalizerSampleProvider = new EqualizerSampleProvider(waveToSample, equalizerBands);