C# - Microphone noise detection
Asked Answered
W

1

6

I am using nAudio Library to capture microphone input. But I have run into a problem. I am using the code(which I have modified slightly) from an nAudio sample app. The codes generates a WAV file based on mic input and renders it as a wave. Here is the code for that.

private void RenderFile()
{
        SampleAggregator.RaiseRestart();
        using (WaveFileReader reader = new WaveFileReader(this.voiceRecorderState.ActiveFile))
        {
            this.samplesPerSecond = reader.WaveFormat.SampleRate;
            SampleAggregator.NotificationCount = reader.WaveFormat.SampleRate/10;
            //Sample rate is 44100

            byte[] buffer = new byte[1024];
            WaveBuffer waveBuffer = new WaveBuffer(buffer);
            waveBuffer.ByteBufferCount = buffer.Length;
            int bytesRead;
            do
            {
                bytesRead = reader.Read(waveBuffer, 0, buffer.Length);
                int samples = bytesRead / 2;
                double sum = 0;
                for (int sample = 0; sample < samples; sample++)
                {
                    if (bytesRead > 0)
                    {
                        sampleAggregator.Add(waveBuffer.ShortBuffer[sample] / 32768f);
                        double sample1 = waveBuffer.ShortBuffer[sample] / 32768.0;
                        sum += (sample1 * sample1);

                    }
                }
                double rms = Math.Sqrt(sum / (SampleAggregator.NotificationCount));
                var decibel = 20 * Math.Log10(rms);



                System.Diagnostics.Debug.WriteLine(decibel.ToString() + " in dB");
            } while (bytesRead > 0);
            int totalSamples = (int)reader.Length / 2;
            TotalWaveFormSamples = totalSamples / sampleAggregator.NotificationCount;
            SelectAll();
        }
        audioPlayer.LoadFile(this.voiceRecorderState.ActiveFile);
 }

Below is a little chunk from a result of a 2second WAV file with no sound but only mic noise.

-54.089102453893 in dB
-51.9171950072361 in dB
-53.3478098666891 in dB
-53.1845794096928 in dB
-53.8851764055102 in dB
-57.5541358628342 in dB
-54.0121140454216 in dB
-55.5204248291508 in dB
-54.9012326746571 in dB
-53.6831017096011 in dB
-52.8728852678309 in dB
-55.7021600863786 in dB

As we can see, the db level hovers around -55 when there is no input sound, only silence. if I record saying "Hello" in mic in a normal tone, the db value will goto -20 or so. I read somewhere that average human talk is around 20dB and -3dB to -6dB is the ZERO value range for mic.

Question: Am I calculating the dB value correctly? (i used a formula proposed here by someone else)...Why dB is always coming up in negative? Am i missing a crucial concept or a mechanism?

I searched nAudio documentation at codeplex and didn't find an answer. In my observation, the documentation there needs to be more explanatory then just a bunch of Q&A [no offense nAudio :)]

Wildfowl answered 1/9, 2015 at 11:31 Comment(0)
S
3

If I understood the formula correctly, the actual value you're calculating is dBm, and that's absolutely ok since dB is just a unit to measure amplification and can't be used for measuring signal strength/amplitude (i.e. you can say I amplified the signal by 3 db, but can't say my signal strength is 6 dB).

The negative values are there just because of the logarithmic conversion part of the formula (converting watts/miliWatts to db) and since the signals you're dealing with are relativly weak.

So in conclusion, looks, like you've done everything right. Hope it helps.

EDIT: BTW, as you can see, there really is ~23-25dbm difference between silence and human speech

Sitton answered 1/9, 2015 at 12:2 Comment(2)
Thanks for quick response. @Felix Av, my assignment says that if mic noise <10db,display a "low noise" message, and if higher then 10db,display "high noise" message. just a silly question, is the assignment range is given from a human perspective???.... since i'm getting all values in negative from my program (mic's perspective), how should I correlate the two dB ranges i.e. my program generating range and assignment range?... thanks a lotWildfowl
I think, if the silence level is X (let's say mic not even connected and the signal is -80db, you probably should check the level), then low noise is everything < X + 10, high noise > X + 10.Sitton

© 2022 - 2024 — McMap. All rights reserved.