Android 实时静音检测

Android Real time silence detection

我正在尝试从用户那里获取实时音频输入,并且每 300 毫秒我获取样本的平均值。我正在使用 PCM 16 位样本和 44100 采样率。 我正在将录音机中的数据读取到一个短数组中,并且每 300 毫秒打印一次所有样本的平均值。 但是,问题是平均值似乎是随机的,即使在无声的情况下它也会显示很大的值(这不应该是)。 我想知道我是否以正确的方式使用短数组。

public void startRecording() {
    recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, SAMPLE_RATE,
            channels, AUDIO_FORMAT, Buffersize);

    recorder.startRecording();

    isRecording = true;
    NoiseSuppressor.create(recorder.getAudioSessionId());
    AcousticEchoCanceler.create(recorder.getAudioSessionId());
    AutomaticGainControl.create(recorder.getAudioSessionId());

    recordingThread = new Thread(new Runnable()

    {
        public void run() {
            writeAudioData();
        }

    });
    recordingThread.start();

}
private void writeAudioData() {

    short data[] = new short[Buffersize/2];

    while(isRecording) {

        samplesread =0;
        value = 0;
        long ftime = System.currentTimeMillis()+300;
        while (ftime > System.currentTimeMillis()) { // loop for taking average of 300 ms

            if(isRecording){
                samplesread += recorder.read(data, 0, Buffersize/2); // reads the data from the recorder and srores in the short array
                int i=0;
                while(i<Buffersize/2) {

                    //value += (long)(data[i]);
                    value += (long)(data[i] & 0xFFFF); // considering the PCM samples are signed I tried this


                    i++;

                }

            }
        }
        show = value;
        div = samplesread;

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                TextView tv = (TextView) findViewById(R.id.textView);
                tv.setText(""+(show/(div))+" "+div);
                TextView tv2 = (TextView) findViewById(R.id.textView2);
                tv2.setText(show+" "+ System.currentTimeMillis());

            }
        },0);

    }

}

数组值由带符号的数字组成,因此,在取平均值时未显示所需的结果。当我取 RMS 值而不是取平均值时它起作用了。