Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a wav file into smaller chunks using Java?

Tags:

java

audio

wav

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

like image 279
Snehal Avatar asked Oct 02 '09 05:10

Snehal


2 Answers

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.

like image 99
Vinko Vrsalovic Avatar answered Nov 10 '22 22:11

Vinko Vrsalovic


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.

like image 22
Toad Avatar answered Nov 10 '22 23:11

Toad