I Want To Check The Loudness Of Voice In Android
I am developing an application where I have to detect the loudness of voice. Means some on shouts on the mobile and it will show the loudness level on the screen. Which API can be
Solution 1:
Look at http://developer.android.com/reference/android/media/AudioRecord.html
When you read the buffer the byte values will represent the amplitude. The higher the byte value the louder the sound.
Here is a scaled-down version of something I used in an app I wrote a while back:
Add this to your mainifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
soundlevel.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><ToggleButtonandroid:id="@+id/togglebutton_record"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ToggleButton" /><ProgressBarandroid:id="@+id/progressbar_level"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
SoundLevel.java
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.ToggleButton;
publicclassSoundLevelextendsActivity {
privatestaticfinalintsampleRate=11025;
privatestaticfinalintbufferSizeFactor=10;
private AudioRecord audio;
privateint bufferSize;
private ProgressBar level;
privateHandlerhandler=newHandler();
privateintlastLevel=0;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soundlevel);
level = (ProgressBar) findViewById(R.id.progressbar_level);
level.setMax(32676);
ToggleButtonrecord= (ToggleButton) findViewById(R.id.togglebutton_record);
record.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
publicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stubif (isChecked) {
bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT) * bufferSizeFactor;
audio = newAudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
audio.startRecording();
Threadthread=newThread(newRunnable() {
publicvoidrun() {
readAudioBuffer();
}
});
thread.setPriority(Thread.currentThread().getThreadGroup().getMaxPriority());
thread.start();
handler.removeCallbacks(update);
handler.postDelayed(update, 25);
} elseif (audio != null) {
audio.stop();
audio.release();
audio = null;
handler.removeCallbacks(update);
}
}
});
}
privatevoidreadAudioBuffer() {
try {
short[] buffer = newshort[bufferSize];
int bufferReadResult;
do {
bufferReadResult = audio.read(buffer, 0, bufferSize);
for (inti=0; i < bufferReadResult; i++){
if (buffer[i] > lastLevel) {
lastLevel = buffer[i];
}
}
} while (bufferReadResult > 0 && audio.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING);
if (audio != null) {
audio.release();
audio = null;
handler.removeCallbacks(update);
}
} catch (Exception e) {
e.printStackTrace();
}
}
privateRunnableupdate=newRunnable() {
publicvoidrun() {
SoundLevel.this.level.setProgress(lastLevel);
lastLevel *= .5;
handler.postAtTime(this, SystemClock.uptimeMillis() + 500);
}
};
}
Post a Comment for "I Want To Check The Loudness Of Voice In Android"