How to get PCM data from a wav file?
Asked Answered
B

2

5

I have a .wav file. I want to get the PCM data from that sound file, so that I can get the individual data chunks from the sound and process it.

But I don't know how to do it. Can anyone tell me how to do it? I have done this so far:

public class test
{

    static int frameSample;
    static int timeofFrame;
    static int N;
    static int runTimes;
    static int bps;
    static int channels;
    static double times;
    static int bufSize;
    static int frameSize;
    static int frameRate;
    static long length;

    public static void main(String[] args)
    {
        try
        {
            AudioInputStream ais = AudioSystem.getAudioInputStream(new File("music/audio.wav"));
            AudioInputStream a;
            int numBytes = ais.available();
            System.out.println("numbytes: "+numBytes);
            byte[] buffer = new byte[numBytes];
            byte[] buffer1=new byte[numBytes] ;
            int k=0;
            int count=0;
            while(count!=-1){
                count=ais.read(buffer, 0, numBytes);
            }
            long value = 0;

            for (int i = 0; i < buffer.length; i++)
            {
               value = ((long) buffer[i] & 0xffL) << (8 * i);
               System.out.println("value: "+value);
            }
        } catch(Exception e) {

        }
    }
}
Barrada answered 25/8, 2012 at 8:25 Comment(2)
Do you want to do this using Java or J? I head never heard of 'J' before your question, and suspect you mean Java.Pericles
sorry. i did not mean j. i want to do it in java. sorry again.Barrada
P
6

This can be done with the Java Sound API.

  • Use the AudioSystem to get an AudioInputStream from the file.
  • Query the stream for the AudioFormat.
  • Create a byte[] to suit the format. E.G. 8 bit mono is a byte[1]. 16 bit stereo is byte[4].
  • Read the stream in chunks of the byte[] and it will contain the sound, frame by frame.
  • Proceed with further processing..
Pericles answered 25/8, 2012 at 12:7 Comment(4)
i have done this so far. is this correct? i want to collect the integer values from the bytes. the code is with the question.Barrada
Please use a consistent and logical indent for code blocks. Note that the frames are 'individual data chunks' so your question has been answered. You should ask a separate question as to how to turn the frames into an int (mono) or a pair of ints (stereo).Pericles
would you please tell me how can i seperate the header from the wav? please let me know.Barrada
The bytes read from an AudioInputStream are pure sound data, there is no need to ignore any of them. But why should I keep trying to help, when you ignore advice about code formatting etc.?Pericles
I
0

Here is a fully working example using the javax.sound.sampled package. If you need details on the audio format you can get them via audioInputStream.getFormat() which will return a AudioFormat object.

public static byte[] getPcmByteArray(String filename) throws UnsupportedAudioFileException, IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream(65536);
    File inputFile = new File(filename);
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = audioInputStream.read(buffer)) != -1) {
        baos.write(buffer, 0, bytesRead);
    }

    return baos.toByteArray();
}
Italian answered 20/11, 2021 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.