2013-07-03 40 views
0

我很新,所以我會盡力解釋。 我想統計音頻電平超過特定電平的次數。我發現/創建了一些代碼,可以檢測到該級別是否超過了某個閾值,但無法確定如何可靠地計算出超過該閾值的次數。聲音/噪音是由連接到麥克風的開關產生的,每次切換噪音都是由切換產生的。我需要使用某種過濾嗎?Naudio - 計數音頻電平上升到一個水平

C#.NET n音訊庫 史蒂夫。

public void StartListening() 
    { 
     WaveIn waveInStream = new WaveIn(); 
     waveInStream.BufferMilliseconds = 500; 
     waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable); 
     waveInStream.StartRecording(); 
    } 

    //Handler for the sound listener 
    private void waveInStream_DataAvailable(object sender, WaveInEventArgs e) 
    { 
     bool result = ProcessData(e); 
     if (result) 
     { 
      intCounter++; 
      label1.Text = intCounter.ToString(); 
     } 
     else 
     { 
      //no peak in sound 
     } 
    } 

    //calculate the sound level based on the AudioThresh 
    private bool ProcessData(WaveInEventArgs e) 
    { 
     bool result = false; 

     bool Tr = false; 
     double Sum2 = 0; 
     int Count = e.BytesRecorded/2; 
     for (int index = 0; index < e.BytesRecorded; index += 2) 
     { 
      double Tmp = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]); 
      Tmp /= 32768.0; 
      Sum2 += Tmp * Tmp; 
      if (Tmp > AudioThresh) 
       Tr = true; 
     } 

     Sum2 /= Count; 

     // If the Mean-Square is greater than a threshold, set a flag to indicate that noise has happened 
     if (Sum2 > AudioThresh) 
     { 
      result = true; 
     } 
     else 
     { 
      result = false; 
     } 
     return result; 
    } 
+0

這個問題應該與編程有關,你應該更具體。 – mor

回答

0

首先,你應該用分貝來處理音頻。這裏 這篇文章包含有關如何計算您的音頻緩衝器中作爲分貝的RMS值一個C#銳利例如:

Calculate decibels in c#

其次,分割你的方法,使他們的名字是如此有意義更容易閱讀和管理。您已將您的方法命名爲ProcessData,並返回一個布爾值,但方法的評論是:根據AudioThresh計算聲級。我建議你把它分解了,例如:

private float calculateDBinRMS(buffer){ 
// This method calculates and returns the RMS value of the buffer in DB 
} 

private bool hasReachedThreshold(buffer, AudioThres) { 
if(calculateDBinRMS(buffer) >= AudioThres) 
    return true; 
return false; 
} 

我不知道您的AudioThres從擺在首位來,但使用上面的代碼,它應該是在分貝。

過濾不會完全消除瞬態信號,即使他們往往居住在較高的頻率範圍(一般來講,記錄麥克風國內音響)。緩衝區長度對確定總峯值與平均RMS值有最大的影響。一個小緩衝區會給你更多的'峯值'結果,而更大的緩衝區會給你更多的平均整體(RMS)值,這可能是你以後的情況。

而且,一般而言,你是從你的麥克風遇到噪音可能會從一個骯髒的開關或髒的連接從麥克風到你的電腦是未來。

這一切後認爲,計算的時間達到閾值應該是微不足道的數目。