audiorecord - Voice Intensity calculation in android -
i created sample application searching in web through voice command google search. used import android.media.audiorecord; this. while user says showing animation on icon based on intensity of voice. have done math calculation got internet. getting wrong command if doing calculation. please me find reason that.
please see code.
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); //recoding starting here
public void run() { rec.startrecording(); while (!done) { int nshorts = readblock(); if (nshorts <= 0) break; } rec.stop(); rec.release(); } // readblock() method. calculation here
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; }
Comments
Post a Comment