Search code examples
javaaudiofrequencyjavasoundwaveform

Drawing a graphic waveform from microphone or speakers


So i've been looking into the java sound api and I can get input,record and play back sounds and i can draw a graph using coordinates or sin.

I want to be able to have a live "Oscilloscope" as such which takes input from the mic and converts it into a waveform which scrolls left over time and when a noise is made the sound (frequency?) is added on to the left hand side of the graph and then goes back to normal when no more noise is made.

how can I convert an array of bytes into a graph?

so far i have a file that can record and play back sound (saved in a buffer not file) and then save it to a .wav and draw a wave .png from the .wav however this isn't a live feed and i'm not sure where to go from here.


Solution

  • Here is a (relatively small) application that will play back sound and draw a simple graphic on a panel:

    WaveformDemo on github

    It has some sparse comments to point out what is going on but it is generally straightforward. Really the best thing is just to look at the code. It does most of what you're asking about sans the recording but the code can be easily adapted to that. Only difference is you will be pulling the bytes from your TargetDataLine instead of an AudioInputStream.

    Some of the stuff like the window function and quantizing might require a lengthy explanation but that's all covered in plenty of literature. In my experience while there are plenty of technical texts there are not a whole lot of code examples around on the web for this kind of thing. Java Sound Resources is a pretty good resource for Java in particular but it is a bit outdated. So I hope this can get you started.