4 android amplitude audiorecord
我有一些代码应该从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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
double amplitudeDb = 20 * Math.log10(Math.abs(amplitude) / 32768);
Run Code Online (Sandbox Code Playgroud)
我想问题可能来自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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4231 次 |
| 最近记录: |