Audio files handling in Java – javax.sound.sampled.AudioSystem

Problem: Few days before i wrote a post about converting audio media file to different format using SoX. I also mentioned one limitation of Sox that it does not convert audio file formats and resample it when rates are same in input and output and gives following error:

sox: Input and Output rates must be different to use resample effect


See the solution in the post
but that will suit you if you are using Sox manually.

Solution: While using java, You first need to know about specs of audio file before converting it or applying some effects. javax.sound.sampled package solves this problem, which i figured out while solving the problem:

 
AudioFileFormat format =  AudioSystem.getAudioFileFormat(mediaFileToConvert);
 

Once you get AudioFileFormat from the File, you can get get different specs of Audio file:

 
AudioFormat audioformat = format.getFormat();
/* returns int - Obtains the number of channels.*/
audioformat.getChannels();
/* returns AudioFormat.Encoding - Obtains the type of encoding for sounds in this format.*/
audioformat.getEncoding();
/* returns float Obtains the frame rate in frames per second.*/
audioformat	.getFrameRate();
/* returns int Obtains the frame size in bytes.  */
audioformat	.getFrameSize();
/* returns Object Obtain the property value specified by the key.  */
audioformat.getProperty(String key);
/* returns float Obtains the sample rate.*/
audioformat.getSampleRate();
/* returns int Obtains the size of a sample.  */
audioformat.getSampleSizeInBits()
/* returns boolean Indicates whether the audio data is stored in big-endian or little-endian byte order.  */
audioformat.isBigEndian();
 

I have noted down very basic use of AudioSystem , I will look more and will write about mixing and conversion using Java soon.

Most Commented Posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)