[英]NAudio C#: How to obtain a byte array from WaveInEventArgs for further manipulation
我已經開發了一個正在運行的Android應用程序,正在嘗試制作C#版本。 我被困在嘗試檢索緩沖區數據並將其傳遞到字節數組中。 我已將我的項目引用到NAudio等。
現在,我的項目使我能夠以幾乎無延遲的方式讀取麥克風輸入並將其通過揚聲器輸出,因為我已通過編程方式調整了延遲。 但是,我很難嘗試檢索緩沖區數據,如何處理waveInEventArgs? 我確實知道waveIn的數據會傳遞到waveInEventArgs.buffer中,但是我只是無法檢索緩沖區數據來放置它。 我該怎么辦?
這是我的代碼:
private void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
bufferedWaveProvider.AddSamples(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
}
public String processAudioFrame(short[] audioFrame)
{
double rms = 0;
for (int i = 0; i < audioFrame.Length; i++)
{
rms += audioFrame[i] * audioFrame[i];
}
rms = Math.Sqrt(rms / audioFrame.Length);
double mGain = 2500.0 / Math.Pow(10.0, 90.0 / 20.0);
double mAlpha = 0.9;
double mRmsSmoothed = 0;
//compute a smoothed version for less flickering of the display
mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
double rmsdB = 20.0 * Math.Log10(mGain * mRmsSmoothed);
//assign values from rmsdB to debels for comparison in errorCorrection() method
double debels = rmsdB + 20;
String value = debels.ToString();
return value;
}
變量值將作為字符串返回,以在我在設計中實現的文本框中顯示結果。
謝謝! 我剛剛在2天前着手這個項目,因此以更簡單的方式對其進行解釋非常感激。
可能最簡單的方法是使用Buffer.BlockCopy
將字節數組轉換為短數組,然后將其傳遞到processAudioFrame
函數。 像這樣:
short[] sampleData = new short[waveInEventArgs.BytesRecorded / 2];
Buffer.BlockCopy(waveInEventArgs.Buffer, 0, sampleData, 0, waveInEventArgs.BytesRecorded);
var decibels = processAudioFrame(sampleData)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.