I am trying to add a wave graph to my android app, that displays the wave form data for the currently playing audio file. I am currently trying to write a method to build an arraylist with the wave file amplitudes (1 amplitude for every 100 millieseconds of audio length), however it takes ages (minutes) to finish running. It is extremely inefficient.
This is the code:
public ArrayList <Integer> buildAudioWaveData(Recording recording){
final Recording finalRecording = recording;
(new Thread(){
@Override
public void run(){
File recFile = finalRecording.getFile();
ArrayList <Integer> dataSeries = new ArrayList<Integer>();
try {
InputStream bis = new BufferedInputStream(new FileInputStream(recFile));
DataInputStream dis = new DataInputStream(bis);
long sampleRate = finalRecording.getSampleRate(new RandomAccessFile(recFile, "rw"));
long samplesPerDatum = sampleRate / 10; // One sample for every 100 ms.
long fileLengthInBytes = recFile.length();
long fileDataRemaining = fileLengthInBytes / 2; // 16 bit wave file = 2 bytes per sample.
int max = 0;
while(fileDataRemaining > 0){
if(fileDataRemaining > samplesPerDatum) {
for (int i = 0; i < samplesPerDatum; i++) {
short temp = dis.readShort();
if (temp > max) {
max = temp;
}
}
Log.i("temp", Integer.toString(max));
dataSeries.add(max);
max = 0;
}
fileDataRemaining -= samplesPerDatum;
}
int x = 0;
}catch(Exception e){
}
}
}).start();
return null;
}
Does anyone know of a more efficient way in which i can generate the array for my graph?
Thanks heaps in advance. Corey B :)