Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

java - How to get PCM data from a wav file?

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

        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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..

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...