tags:

views:

613

answers:

3

I have a very huge WAV file, about 100MB in size. I would like to use Java to read this wav file and split it into smaller chunks for every 2 seconds of audio.

Is it possible to do this in Java? Could you please suggest me an API with which I can achieve it?

Thanks in advance, Snehal

+2  A: 

You can use AudioInputStream and its AudioFileFormat member (which contains an AudioFormat instance) to know what to write (format, sample rate), you can use AudioSystem to write it.

Based on the sample rate of the format you can find out how many bytes of audio are 2 seconds, and go on a loop of reading that many bytes from the AudioInputStream, writing them to a new file.

Vinko Vrsalovic
Through AudioFileFormat I could find out the sample rate is 8000. What is the relation between sample rate and the number of bytes it occupies? I'm a total newbie in Audio handling, sorry for my ignorance
Snehal
Besides the sample rate (f) you also need the bits per sample (bps) and the number of channels (n), also provided by AudioFormat. For one second of audio you then need bps * n * f / 8 bytes.
eljenso
Thanks Elijenso. I could find out the number of bytes required for 2 seconds of audio. Now, i'm trying to write the read bytes using AudioSystem's write method. However, one of the input argument is AudioInputStream. I'm unable to create that object based on the read bytes. Any pointers would be really helpful. Thanks!
Snehal
A: 

If you don't care about the longevity of your code, then Quicktime For Java is a good bet for media. It runs on Windows and Mac and will read and write pretty much any audio (and video) format. The downside is that Apple have not supported it for years, so while it still works, you're investing in a dying technology.

Duncan McGregor
A: 

You could also look up the specification for a wav file which is really basic and simple. And then binary read the file, and save it again in smaller bits.

I think it's a better learning experience to do it this way instead of always relying on libraries.

Toad