3

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.

Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
Estra
  • 155
  • 1
  • 3
  • 10
  • seriously, google has a lot of hits with just "how to read an audio file in java" – peroija Oct 31 '12 at 21:47
  • 1
    refer to this link here http://stackoverflow.com/questions/86083/any-good-recommendations-for-mp3-sound-libraries-for-java – PermGenError Oct 31 '12 at 21:52
  • There are lots of hits but I have only found unanswered questions especially when storing the audio data into a byte array. – Estra Oct 31 '12 at 22:20

2 Answers2

6

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)

speakingcode
  • 1,518
  • 13
  • 12
2

Both your questions have been answered on SO. See my answers to these questions:

Basics of audio and java:

How can I create a sound file in Java

Drawing waveforms and waveform overviews:

How can I draw sound data from my wav file?

Community
  • 1
  • 1
Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
  • 2
    May I point out that [How can I create a sound file in Java](http://stackoverflow.com/questions/13090757/how-can-i-create-a-sound-file-in-java) has no answers – Dan Apr 02 '17 at 16:12
  • 1
    Hmmm, must have been deleted. – Bjorn Roche Apr 03 '17 at 14:11