1

I am trying to extract data from .wav file to draw a wave graph, but I am stuck as I get the only a stream of 0s with my code:

AudioInputStream ais = AudioSystem.getAudioInputStream(new File("audio/sine_-06_05_02000.wav"));
AudioFormat format = ais.getFormat();

format = new AudioFormat(format.getFrameRate(), format.getSampleSizeInBits(), format.getChannels(), true, true);

ais = AudioSystem.getAudioInputStream(format, ais);
int sample_size = format.getSampleSizeInBits() / 8;
ArrayList<Long> data = new ArrayList<Long>();
int size = 400;

while (data.size() < size)
{
    byte[] buffer = new byte[8];
    ais.read(buffer, 8-sample_size, sample_size);
    if (buffer[8-sample_size] < 0)
    {
        for (int i = 0; i < 8 - sample_size; i++)
        {
            buffer[i] = -1;
        }
    }
    data.add(ByteBuffer.wrap(buffer).getLong());
}
for(long value:data)
{
    System.out.println(value);
}

Please tell me why I cannot get the data and where my code is wrong if you could find out. Thank you!

Edit: I figured it out that my audio resource was digital, but the code was for analog audio.

Hiro
  • 11
  • 2
  • 1
    I don't know pretty much anything about `AudioInputStream`s and its related program logic, but this question might be relevant: [Graphing Wav Files in Java](https://stackoverflow.com/questions/27517265/graphing-wav-file-in-java) – Robo Mop Mar 23 '18 at 16:10
  • Does this answer your question? [Extract amplitude array from a wav File using JAVA](https://stackoverflow.com/questions/39313634/extract-amplitude-array-from-a-wav-file-using-java) – Zukaru Oct 10 '21 at 06:28

0 Answers0