我有一些代码,应该是从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;
}
}发布于 2014-01-14 17:52:42
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,我在中进行了测试。你可以换到
double amplitudeDb = 20 * Math.log10((double)Math.abs(amplitude) / 32768);https://stackoverflow.com/questions/21119846
复制相似问题