0

I have been searching for this but none seems to answer my question. I have been trying to graph/plot a wav file by this:

int result = 0;
try {
    result = audioInputStream.read(bytes);
} catch (Exception e) {
    e.printStackTrace();
}

and then using the result to be a variable for a graph. I've been thinking if it is correct to change first the result to decibels. Also, am I right to use the result as a variable to be use in the graph? Or is there any way that has to be use in graphing the wav file?

dimmed
  • 47
  • 5
  • You need to know the min and max values, this is going to mean that you will need to read the entire file and "normalise" the results – MadProgrammer Dec 17 '14 at 02:15
  • 1
    And no *"am I right to use the result as a variable to be use in the graph?"* - `result` is would be the number of bytes read from the stream (into the `bytes` array) – MadProgrammer Dec 17 '14 at 02:20
  • Also, what GUI frame work? Swing/AWT/GWT/HTML/JavaFX/SWT? – MadProgrammer Dec 17 '14 at 02:30
  • You'll also need to parse the wav file to extract the audio samples. http://stackoverflow.com/questions/8754111/how-to-read-the-data-in-a-wav-file-to-an-array – dnault Dec 17 '14 at 02:51
  • @MadProgrammer how would i know the min and max values? and what would i use as a variable to use in the graph in reading the wav file? – dimmed Dec 17 '14 at 02:54
  • @dnault how would i parse it? i can't seem to understand on how to get the audio samples. is that using getSampleSizeInBits()? – dimmed Dec 17 '14 at 03:00
  • @MadProgrammer im trying to graph it by, supposedly, saving the **result** into a text file then using gnuplot to visualize it. – dimmed Dec 17 '14 at 03:02

1 Answers1

4

The first thing you need to do is read the samples of the file, this will give you the min/max ranges of the waveform (sound wave)...

    File file = new File("...");
    AudioInputStream ais = null;
    try {
        ais = AudioSystem.getAudioInputStream(file);
        int frameLength = (int) ais.getFrameLength();
        int frameSize = (int) ais.getFormat().getFrameSize();
        byte[] eightBitByteArray = new byte[frameLength * frameSize];

        int result = ais.read(eightBitByteArray);

        int channels = ais.getFormat().getChannels();
        int[][] samples = new int[channels][frameLength];

        int sampleIndex = 0;
        for (int t = 0; t < eightBitByteArray.length;) {
            for (int channel = 0; channel < channels; channel++) {
                int low = (int) eightBitByteArray[t];
                t++;
                int high = (int) eightBitByteArray[t];
                t++;
                int sample = getSixteenBitSample(high, low);
                samples[channel][sampleIndex] = sample;
            }
            sampleIndex++;
        }

    } catch (Exception exp) {

        exp.printStackTrace();

    } finally {

        try {
            ais.close();
        } catch (Exception e) {
        }

    }

//...

protected int getSixteenBitSample(int high, int low) {
    return (high << 8) + (low & 0x00ff);
} 

Then you would need to determine the min/max values, the next example simply checks for channel 0, but you could use the same concept to check all the available channels...

    int min = 0;
    int max = 0;
    for (int sample : samples[0]) {

        max = Math.max(max, sample);
        min = Math.min(min, sample);

    }

FYI: It would be more efficient to populate this information when you read the file

Once you have this, you can model the samples...but that would depend on framework you intend to use...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • in my program, i intend to save the result in a text file and from there i would plot it using gnuplot. how can use the max/min to plot it in gnuplot? can i save the max/min values in a text file and proceed on how i graph it? – dimmed Dec 17 '14 at 03:18
  • Once you have the samples, you probably don't need the min/max as (I assume) gnuplot will figure it, but you'd need to check on it's requirements... – MadProgrammer Dec 17 '14 at 03:22
  • ok im gonna have to go with this. this might lessen the file processing of my program which is also a big help. thanks! – dimmed Dec 17 '14 at 04:05
  • int sample = getSixteenBitSample(high, low); This line trows a error,because the method doesn't exist. Could you help mi with this, please =) – Mike Brian Olivera Jun 06 '15 at 16:21
  • 1
    @MikeBrianOlivera Hmm, I wonder where that went to...never mind, I've added it – MadProgrammer Jun 06 '15 at 22:31