min*_*ino 1 java audio android record
我已经构建了一个 AudioRecord 功能,对于录制的一分钟,我想获得以分贝为单位的平均音量。但我不知道该怎么做。我什至无法弄清楚 AudioRecord 对象是否为我提供了缓冲区数组中完整分钟的记录数据。
这是我到目前为止得到的代码,有人可以帮我修改它吗?我试图在网上找到一种方法,但我还没有找到任何东西。
Runnable mrHandlerRunnable = new Runnable() {
@Override
public void run() {
ar.stop();
short[] buffer = new short[minSize];
Log.d("LEN: " , ""+buffer.length);
ar.read(buffer, 0, minSize);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
Double avg = 0.0;
for (short s : buffer)
{
if (Math.abs(s) > max)
{
max = Math.abs(s);
}
if (Math.abs(s) < min)
{
min = Math.abs(s);
}
avg = avg+Math.abs(s);
}
avg = avg/buffer.length;
ar.startRecording();
}
};
mrHandler.postDelayed(mrHandlerRunnable, 60000);
Run Code Online (Sandbox Code Playgroud)
尝试这个 :
public class NoiseRecorder
{
private final String TAG = SoundOfTheCityConstants.TAG;
public static double REFERENCE = 0.00002;
public double getNoiseLevel() throws NoValidNoiseLevelException
{
Logging.e(TAG, "start new recording process");
int bufferSize = AudioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_DEFAULT,AudioFormat.ENCODING_PCM_16BIT);
//making the buffer bigger....
bufferSize=bufferSize*4;
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
short data [] = new short[bufferSize];
double average = 0.0;
recorder.startRecording();
//recording data;
recorder.read(data, 0, bufferSize);
recorder.stop();
Logging.e(TAG, "stop");
for (short s : data)
{
if(s>0)
{
average += Math.abs(s);
}
else
{
bufferSize--;
}
}
//x=max;
double x = average/bufferSize;
Logging.e(TAG, ""+x);
recorder.release();
Logging.d(TAG, "getNoiseLevel() ");
double db=0;
if (x==0){
NoValidNoiseLevelException e = new NoValidNoiseLevelException(x);
throw e;
}
// calculating the pascal pressure based on the idea that the max amplitude (between 0 and 32767) is
// relative to the pressure
double pressure = x/51805.5336; //the value 51805.5336 can be derived from asuming that x=32767=0.6325 Pa and x=1 = 0.00002 Pa (the reference value)
Logging.d(TAG, "x="+pressure +" Pa");
db = (20 * Math.log10(pressure/REFERENCE));
Logging.d(TAG, "db="+db);
if(db>0)
{
return db;
}
NoValidNoiseLevelException e = new NoValidNoiseLevelException(x);
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
这将计算 4 秒样本中音频的平均幅度。一旦获得平均幅度,就将其转换为db。希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
2543 次 |
| 最近记录: |