Reading Wav File in Java

Reading wav file in Java

The official Java Sound Programmer Guide walks through reading and writing audio files.

This article by A Greensted: Reading and Writing Wav Files in java should be helpful. The WavFile class is very useful and it can be tweaked to return the entire data array instead of buffered fragments.

reading a wav file in java

You need to download the java files from HERE and add them to the same package as your code.

then change this line:

WavFile wavFile = WavFile.openWavFile(new File(args[0]));

to:

WavFile wavFile = WavFile.openWavFile(new File("D:/p.wav"));

reading wav/wave file into short[] array

Bring it in via an AudioInputStream as one normally does for sound.

With each buffer load you grab from the stream, iterate through it, two bytes at a time (if it is 16-bit encoding) and use your favorite algorithm to convert the two bytes to a single short int. (Depends if file is Big-Endian or Little-Endian.)

Then, save the results by appending to your preferred form of short array, instead of posting to a SourceDataLine like is done in the more usual playback situations.

Quote from Java Tutorials:

Because the Java Sound API gives you access to the audio data as an
array of bytes, you can alter these bytes in any way you choose.

You might check out the code in this Java sound tutorial: Using Files and Format Converters, the section "Reading Sound Files" and note the comment: "Here, do something useful with the audio data that's now in the audioBytes array..."

For example, that something useful might be:

    myShortArray[i] = (short)(( buffer[i*2] & 0xff )|( buffer[i*2 + 1] << 8 ));

buffer = byte buffer receiving the AudioInputStream.

i = index into the arrays.

How to read compressed .wav audio files in Java?

The issue is perhaps you have not used java to downsample audio.

Either

downsample .wav using java and then check its
working.
Good reference for Java-based downsampling and
this

OR

resample .wav using third party libraries and test. Reference for
Re-sample using third-party library



Related Topics



Leave a reply



Submit