NAudio C#:如何从WaveInEventArgs中获取字节数组以进行进一步操作



我开发了一个正在运行的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;
    }

变量值将作为字符串返回,以在我在设计中实现的文本框中显示结果。

谢谢!我两天前刚刚开始这个项目,所以用更简单的语言解释它是非常感谢的。

可能最简单的方法是使用Buffer.BlockCopy将字节数组转换为短数组,然后将其传递到processAudioFrame函数中。类似这样的东西:

 short[] sampleData = new short[waveInEventArgs.BytesRecorded / 2];
 Buffer.BlockCopy(waveInEventArgs.Buffer, 0, sampleData, 0, waveInEventArgs.BytesRecorded);
 var decibels = processAudioFrame(sampleData)

最新更新