Search code examples
androidaudioshort

Android: Convert Audio into float[]


I'm developing an app in Android that has to record sound and display in the screen some vaules that represents the frequency or the intensity of the sound.

For the record thing I use this piece of code:

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(name);
mRecorder.prepare();
mRecorder.start();

Then I was initially trying to convert the stored file into byte like this:

DataInputStream dis1 =  new DataInputStream ( new FileInputStream (name));
byte[] datainBytes1 = new byte[dis1.available()];
dis1.readFully(datainBytes1);
dis1.close();

But I want to convert that byte values into short or floats to display them by using a drawing method:

canvas.drawLine(xini,yini,xfinal,yfinal,paint)

Could you recommend me another way to convert the audio file into short values that I could draw?

Thank you very much for your help!!


Solution

  • I think the way you are trying to display recorded sounds is not correct. As starting point you need to study audio encoding format and have a look to the following android class: http://developer.android.com/reference/android/media/AudioTrack.html