how to extract frequency from audio samples?

Mar 20, 2013 at 8:24 AM
Edited Mar 20, 2013 at 8:25 AM
hello

I use this code to extract audio samples from mic... I want the frequency of each sample ... Must I use FFT or something like it , Or just there is simple way from Naudio to do it?
 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
  
            int waveInDevices = WaveIn.DeviceCount;
            for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
            {
                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
              this.Title+=string.Format("Device {0}: {1}, {2} channels",
                    waveInDevice, deviceInfo.ProductName, deviceInfo.Channels);
            }

           WaveIn waveIn = new WaveIn();
            waveIn.DeviceNumber = 0;
            waveIn.DataAvailable += waveIn_DataAvailable;
            int sampleRate = 8000; // 8 kHz
            int channels = 1; // mono
            waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
            waveIn.StartRecording();

        }
        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {

            for (int index = 0; index < e.BytesRecorded; index += 2)
            {
                short sample = (short)((e.Buffer[index + 1] << 8) |
                                        e.Buffer[index + 0]);
                float sample32 = sample / 32768f;
                ProcessSample(sample32);
            }
        }

        void ProcessSample(float x)
        {
            label1.Content = x.ToString() + "   ";
            progressBar1.Value = (x + 1) * 50;
        }
thank you :)
Coordinator
Mar 20, 2013 at 3:48 PM
Yes, FFT is a very good way of getting frequency information. There are other approaches, particularly if your incoming signal has a single tone (e.g. you are making a guitar tuner). Look at the demo code that comes with NAudio to see FFT in action to draw a spectrum analyzer with NAudio.

Mark
Mar 20, 2013 at 6:08 PM
I thank you very much..that was helpfull