0

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 :)

Fishingfon
  • 1,034
  • 3
  • 19
  • 33
  • possible duplicate of [How to draw waveform of Android's music player?](http://stackoverflow.com/questions/6317842/how-to-draw-waveform-of-androids-music-player) – Phantômaxx Dec 29 '14 at 10:44
  • Hey +Der Golem, The problem with using the Visualiser class is that i need an array of amplitudes for the entire file - not just the currently playing section of the file. The visualiser class does not support this. The graph i am adding should show the entire waveform data in an XY graph, that the user can scroll through, and then selectively edit, etc. So i need to be able to get the amplitude data for the entire file. Thanks anyway though, Corey – Fishingfon Dec 29 '14 at 11:08

0 Answers0