4

我有一些代码应该从 AudioRecord 中获取幅度。问题是数学只返回-Infinity。请让我多看几眼看看:

private class measureSnoreAudio extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {


            Log.d(TAG, "Creating the buffer of size " + BUFFER_SIZE);
            byte[] buffer = new byte[BUFFER_SIZE];

            Log.d(TAG, "Creating the AudioRecord");
            recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    RECORDING_RATE, CHANNEL, FORMAT, BUFFER_SIZE * 10);

            Log.d(TAG, "AudioRecord recording...");
            recorder.startRecording();

            while (isRecordingSnore) {

                // read the data into the buffer
                int read = recorder.read(buffer, 0, buffer.length);
                int amplitude = (buffer[0] & 0xff) << 8 | buffer[1];

                // Determine amplitude
                double amplitudeDb = 20 * Math
                        .log10(Math.abs(amplitude) / 32768);
                String dbString = String.valueOf(amplitudeDb);
                Log.d("Snore DB", "dB " + dbString);
                //TextView textAmplitude = (TextView) findViewById(R.id.tvAmplitude);
                //textAmplitude.setText(dbString);
            }

            Log.d(TAG, "AudioRecord finished recording");
        return null;
    }
}
4

1 回答 1

7
double amplitudeDb = 20 * Math.log10(Math.abs(amplitude) / 32768);

我认为问题可能来自 Math.abs(amplitude) / 32768,幅度是整数,所以 Math.abs(amplitude) 也会返回整数,因为 Math.abs(amplitude) 小于 32768(也许我不正确, byte 最大 2^7 - 1,这里的幅值可以大于 32768 吗?)。所以 Math.abs(amplitude) / 32768 等于 0。Log10(0) 是 -Infinity,我已经在 Eclipse 中使用 Java 项目进行了测试。您可以更改为

double amplitudeDb = 20 * Math.log10((double)Math.abs(amplitude) / 32768);
于 2014-01-14T17:52:42.337 回答