3

我的应用程序。正在计算输入声音的噪声级和频率峰值。我使用 FFT 来获取 shorts[] 缓冲区数组,这是代码:bufferSize = 1024, sampleRate = 44100

 int bufferSize = AudioRecord.getMinBufferSize(sapleRate,
                channelConfiguration, audioEncoding);
        AudioRecord audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT, sapleRate,
                channelConfiguration, audioEncoding, bufferSize);

这是转换代码:

short[] buffer = new short[blockSize];
        try {
            audioRecord.startRecording();
        } catch (IllegalStateException e) {
            Log.e("Recording failed", e.toString());
        }
        while (started) {
            int bufferReadResult = audioRecord.read(buffer, 0, blockSize);

            /*
             * Noise level meter begins here
             */
            // Compute the RMS value. (Note that this does not remove DC).
            double rms = 0;
            for (int i = 0; i < buffer.length; i++) {
                rms += buffer[i] * buffer[i];
            }
            rms = Math.sqrt(rms / buffer.length);
            mAlpha = 0.9;   mGain = 0.0044;
            /*Compute a smoothed version for less flickering of the
            // display.*/
            mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
            double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);

现在我想知道这个算法是否正常工作或者我错过了什么?而且我想知道它是否正确,并且我在手机上显示的声音以 dB 为单位,如何测试它?我需要任何帮助,在此先感谢:)

4

1 回答 1

1

代码看起来是正确的,但您可能应该处理缓冲区最初包含零的情况,这可能导致Math.log10失败,例如更改:

        double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);

至:

        double rmsdB = mGain * mRmsSmoothed >.0 0 ?
                           20.0 * Math.log10(mGain * mRmsSmoothed) :
                           -999.99;  // choose some appropriate large negative value here for case where you have no input signal
于 2013-04-21T09:05:39.797 回答