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

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

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.

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.

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.

Related

Write 2-channel array of shorts to WAV file

I have 2 arrays of shorts containing musical data, one for each channel, and I need the simplest Java method/library possible to write them to a WAV file without the fuss with headers. Does anyone know of such a method/library?
Java Sound can write WAV files. You may find what you are looking for here:
https://docs.oracle.com/javase/8/docs/technotes/guides/sound/programmer_guide/chapter7.html#a114602
Not sure if it takes shorts as I have only used it to playback files not save them.

How can I split WAV into smaller WAV files?

I have an android application, which records WAV files. This files can be up to 3 minutes long, but I need to split them by 30 seconds (This smaller WAV's must be playable too). I don't really care if it is splitted on a silent moment or not. Is there any way to do it?
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 30 seconds, and go on a loop of reading that many bytes from the AudioInputStream, writing them to a new file.

What is the best way for Files IO in java

I need to do some basic operations on image file in java . My requirements are like :
- opening a file.
- read bytes in some order.
- write the updated byte at the particular offset
- seeking at some offset in file.
Files can be of any size like 2 GB image files.
I want to know, which class in java can provide me the flexibility to do all these operations with ease and with performance efficiency, considering IO in java is slow.
Currently I am considering FileChannel, but I dont know about its performance with files of larger size like in GB. Also it use to read file bytes in ByteBuffer, but if file is large enough , is it appropriate to read all the bytes at same time or should read in chunks. If I read data in chunks, what is the proper size of a chunk?
Please guide me.
Thanks
You probably should use RandomAccessFile, it supports seek by position and write. Also I don't think it is accurate to describe IO in Java as slow, often you can achieve C like performance if you use java.io properly.
You can use Java 8 Stream features
Stream<String> lines = Files.lines(Paths.get(args[1]));
But, If you are not using Java-8. You use java.nio package
If I read data in chunks, what is the proper size of a chunk?
You can uses a buffer size of only 1 KB.

Reading wav file in Java

I want to read wav files in Java and I am going to classify them with K-means.
How can I read wav files in Java and assign them into an array or something like that(you can suggest ideas for it) to classify them?
EDIT: I want to use APIs for reading wav files and for K-means.
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.
Equivalent to matlab's wavread function:
http://web.archive.org/web/20120531113946/http://www.builogic.com/java/javasound-read-write.html
You could read the sound files using javax sound library and FileInputStream
(found a nice example here)
and treat the wave files as a vector of bits (0,1) or bytes.. using multiple sequence alignment (Wiki) create a distance matrix between every stream of bits/bytes, and from there, the clustering should be straight forward.
The Problem is, that this method is very sensitive to noise, etc, but it is worth a shot...
Not sure if this will help someone. Java JDK already provides AudioSystem class.
I used this as part of my tests to check generated WAV properties,
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(new File(response.get()));
assertEquals(1, audioFileFormat.getFormat().getChannels());
assertEquals(8000.0, audioFileFormat.getFormat().getSampleRate(), 0.0f);
assertEquals(8, audioFileFormat.getFormat().getSampleSizeInBits());

Java equivalent of C++ sf_readf_double()

I'm manipulating .wav audio files in Android.
So far things are going well (I can record a .wav file from the mic, add echoes etc.), but I'd like to mimic the behaviour of sf_readf_double() and sf_writef_double as per libsndfile in C++ to implement more complex filters.
I have a com.sun.media.sound.WaveFileReader and WaveFileWriter objects and I'm getting at the audio data using
stream = wfr.getAudioInputStream(new File(inputAudioFileName));
which returns an AudioInputStream.
Is it just a case of reading 8 bytes from the stream at a time into a double or is there more to it than that? I'm a little confused as other filters I'm using seem to manipulate shorts rather than doubles.
Try this:
What should I use in Android when porting C++ code written with libsndfile?
Check this:
android-ndk

Categories