来自AudioRecord的幅度

时间:2014-01-14 17:22:34

标签: android audiorecord amplitude

我有一些代码应该从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;
    }
}

1 个答案:

答案 0 :(得分:7)

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

我想问题可能来自Math.abs(幅度)/ 32768,幅度是整数,所以Math.abs(幅度)也将返回整数,因为Math.abs(幅度)小于32768(也许我是不正确,字节最大2 ^ 7 - 1,这里幅度可以大于32768吗?)。所以Math.abs(幅度)/ 32768等于0.Log10(0)是-Infinity,我在Eclipse中用Java项目测试过。 您可以更改为

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