简体   繁体   English

NAudio WAV文件的分贝频率

[英]NAudio WAV file Frequency to Decibels

I'm using NAudio to generate a WAV file. 我正在使用NAudio生成WAV文件。 The Wav file contains environment noise (detected and recorded via mic). Wav文件包含环境噪音(通过麦克风检测和记录)。 I need to process this file to show average loudness (dB) against different frequency bands. 我需要处理此文件以显示不同频段的平均响度(dB)。 I read much about 1:3 Octave band analysis where the frequency windows are 31, 62, 125, 250, 500 Hz and so on. 我阅读了很多有关1:3倍频程分析的信息,其中频率窗口分别为31、62、125、250、500 Hz等。 And we can have an average loudness against each window. 我们可以在每个窗口上获得平均响度。 This is exactly what I want to do but HOW to achieve this seems confusing. 这正是我想要做的,但是如何实现这一点似乎令人困惑。 What I have done so far is (using NAudio tutorial) to read a WAV file and process it. 到目前为止,我所做的是(使用NAudio教程)读取WAV文件并进行处理。 Here is the code: 这是代码:

private void RenderFile()
{
    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));
            double decibel = (double)(20 * Math.Log10(rms));
            if (calculatedBCount == 0)
            {
                dBList.Add(decibel);
                //  System.Diagnostics.Debug.WriteLine(decibel.ToString() + " in dB");
            }
        } while (bytesRead > 0);
        int totalSamples = (int)reader.Length / 2;
        TotalWaveFormSamples = totalSamples / sampleAggregator.NotificationCount;
        calculatedBCount++;
        SelectAll();

        //System.Diagnostics.Debug.WriteLine("Average Noise: " + avg.ToString() + " dB");
    }
    audioPlayer.LoadFile(this.voiceRecorderState.ActiveFile);
}


public int Read(byte[] buffer, int offset, int count)
{
    if (waveBuffer == null || waveBuffer.MaxSize < count)
    {
        waveBuffer = new WaveBuffer(count);
    }

    int bytesRead = source.Read(waveBuffer, 0, count);

    if (bytesRead > 0) bytesRead = count;

    int frames = bytesRead / sizeof(float); // MRH: was count
    float pitch = pitchDetector.DetectPitch(waveBuffer.FloatBuffer, frames);
    PitchList.Add(pitch);
    return frames * 4;
}

Using a 5 second WAV file, from above two methods, I get a list of Pitches and Decibels The decibels list contains 484 values like: 使用5秒的WAV文件,从上述两种方法中,我获得了音高和分贝的列表。分贝列表包含484个值,例如:

-56.19945639
-55.13139952
-55.06947441
-56.70789076
-57.24140093
-55.98546603
-55.67407176
-55.53060998
-55.98480268
-54.85796943
-57.00735818
-55.64980974
-57.07235475

PitchList contains 62 values which include: PitchList包含62个值,其中包括:

75.36621
247.631836
129.199219
75.36621
96.8994141
96.8994141
86.13281
75.36621
129.199219
107.666016

How can I use these results for identifying what is average loudness against 31Hz, 62Hz, 125Hz, 250Hz and so on. 我如何使用这些结果来识别31Hz,62Hz,125Hz,250Hz等的平均响度。

Am I doing some wrong or everything wrong, maybe? 我是在做错什么还是做错了什么?

Please correct me if I am wrong but...I'm afraid You cannot convert HZ to DB because there is no relation between them. 如果我错了,请纠正我,但是...恐怕您不能将HZ转换为DB,因为它们之间没有关系。 Hz is a measure of frequency and Db is a measure of amplitude, sort like kilos to meters. Hz是频率的度量,而Db是幅度的度量,以千分之几为单位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM