Google 検索のような音声コマンドを使用して Web 内のあらゆるものを検索するためのサンプル アプリケーションを作成しました。これに使いimport android.media.AudioRecord;
ました。ユーザーは、声の強さに基づいてアイコンにアニメーションを表示していると言っています。そのために、インターネットから取得した数学計算を行いました。この計算を行っている場合、間違ったコマンドを取得しています。その理由を見つけるのを手伝ってください。
私のコードを見てください。
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;
}