Write 2-channel array of shorts to WAV file - java

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.

Related

How could I save generated PCM bytes[] to android supported audio file?

The bytes[] array is generated by myself at specific frequency. And now I want to save the bytes to .wav file,in order that the MediaPlayer could play it.
Could anyone have some idea?
While I'm sure there are libraries to do this somewhere, I normally just use my own WAV-file writing code, based on the file format described here. It can be a bit tricky if you want to support multiple bit-rates, etc., and you also need to bear in mind the little-endian byte order.

Java libraries to determine voice note in wav file

Is any library written in java to determine voice note in wav file,
illustration: I record my voice then result the wav file, then I can know what is the note my voice.
This website has implemented this scenario:
You may need to throw together your own custom class for that. I looked around and couldn't find any libraries specifically for that purpose, but creating your own shouldn't be to hard.
Here is a list of the frequencies for different musical notes:
http://www.seventhstring.com/resources/notefrequencies.html
You could create a class that uses these numbers and compares the sound to each frequency to see which it is.
Turning the wav file input to frequencies will require a FFT. Here is a link to a question that explains how to do just that.
Wav File As Frequency Image

How to divide a wav file into 1 second pieces with Java?

I had a question previously: Reading wav file in Java
Firstly, I want to read a wav file with Java and get its bytes to process into an array.
Secondly, I will remove the silences of it(this is another question's topic).
After that I will divide that wav file into 1 second pieces(I should handle header problem for that small wav files, this is one of the major problems)
I tried to read wav file with Java with that API, the answer of my previous question. However with that API should I do anything for header or API does it itself and how can I divide that wav file into 1 second pieces with Java. Another API or anything else is OK for me too.
The API you reference will return a double[] array containing the sample data from the original WAV file. All you have to do then is create a bunch of smaller arrays (each one second long) and copy from the appropriate position in the original array into each of the smaller arrays (so that all the original data is copied into the smaller ones). Then just use the writing methods of your API to create actual WAV files from each smaller array.
Using that API means you shouldn't have to worry about the headers yourself (which is a good thing, because they're easy to write but very complicated to read, potentially).
The size of each smaller array is dependent upon the format of the original. If the original WAV file is mono with a 44.1 KHz sample rate, then a 1-second array would contain 44,100 elements.

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());

How to split a wav file into smaller chunks using 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.

Categories