android中的语音强度计算

时间:2012-06-05 11:46:44

标签: android audiorecord

我创建了一个示例应用程序,用于通过谷歌搜索等语音命令在网络中搜索任何内容。我使用了import android.media.AudioRecord;。用户说我根据语音的强度在Icon上显示动画。为此,我已经完成了从互联网上获得的数学计算。如果我正在进行此计算,我收到了错误的命令。请帮我找到原因。

请参阅我的代码。

minBufferSizeInBytes = AudioRecord.getMinBufferSize( 8000,
                AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
rec = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 8000,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, 8192);

//从此处开始重新编码

   public void run() {
        rec.startRecording();
        while (!done) {
            int nshorts = readBlock();
            if (nshorts <= 0)
                break;
        }
        rec.stop();
        rec.release();
    }

// readBlock()方法。计算在这里

       int readBlock() {
       short[] buf = new short[this.block_size];
       byte audioBuffer[]      = new  byte[this.minBufferSizeInBytes];
        float totalAbsValue = 0.0f;
           short sample = 0;
           int numberOfReadBytes = 0; 
           float tempFloatBuffer[] = new float[3];
           int tempIndex = 0;

       int nshorts = rec.read(buf, 0, buf.length);
       if (nshorts > 0) {
            q.add(buf);
       }
       numberOfReadBytes = rec.read( audioBuffer, 0,minBufferSizeInBytes );
        for( int i=0; i<numberOfReadBytes; i+=2 ) {
            sample = (short)( (audioBuffer[i]) | audioBuffer[i + 1] << 8 );
            if(sample!=0) {
                totalAbsValue += Math.abs( sample ) / (numberOfReadBytes/2);
            }
        }
        tempFloatBuffer[tempIndex%3] = totalAbsValue;
        temp = 0.0f;
        for( int i=0; i<3; ++i )
            temp += tempFloatBuffer[i];
        System.out.println("Temp: "+temp);
        mMyClass = MyClass.getInstance();
        mMyClass.handler.postDelayed(mMyClass.AnimateRunnable, 100);
        return nshorts;
 }

0 个答案:

没有答案