我的应用。正在计算输入声音的噪声水平和频率峰值。我使用快速傅立叶变换得到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格式的声音,如何测试它?我需要任何帮助,请提前谢谢:)
发布于 2013-04-21 17:05:40
代码看起来是正确的,但您可能应该处理缓冲区最初包含零的情况,这可能会导致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 signalhttps://stackoverflow.com/questions/16129480
复制相似问题