Reading an audiofile and displaying the waveform with Java.

Estra picture Estra · Oct 31, 2012 · Viewed 10.3k times · Source

Can anyone advise me on how I could read an audio file, store it in a byte array and proceed to display its waveform? My knowledge of Java is very basic. If anyone can link me to learning materials for Java audio programming it would be great.

Answer

speakingcode picture speakingcode · Oct 31, 2012

You will need to understand some basics about Pulse Code Modulation, as that is how waves are represented digitally as discrete byte/values. Most waveform formats are just some header/metadata and then a stream of pulse codes.

Here is a primer on digital sampling and pulse code modulation:

http://www.speakingcode.com/2011/12/31/primer-on-digital-audio-and-pulse-code-modulation-pcm/

After that, it's relatively simple. Load the file, read through the meta data so you know the sampling rate, bit depth, etc. and then basically build up an array with the pcm values.. general java file reading techniques will work fine for that, such as described in the Java tutorials, many pages online, and several relevant questions on stack overflow.

http://docs.oracle.com/javase/tutorial/essential/io/

Once you have your array, your values effectively represent the amplitude position of the wave at that point in time... scale those values and translate to an image - there's so many ways to do that, it's really just up to you. Some things to consider are do you care about the time axis, or just the shape of the wave form? if you only care about the shape, it's easier, cause you don't need to appropriately mark time and scale on the x axis (assuming you follow the standard of time on x axis and amplitude on y axis)