I'm racking my brain in order to solve a knotty problem (at least for me).
While playing an audio file (using Java) I want the signal amplitude to be displayed against time.
I mean I'd like to implement a small panel showing a sort of oscilloscope (spectrum analyzer). The audio signal should be viewed in the time domain (vertical axis is amplitude and the horizontal axis is time).
Does anyone know how to do it? Is there a good tutorial I can rely on? Since I know very little about Java, I hope someone can help me.
Basically what I want is amplitude against time, not frequency. The audio signal should be viewed in the time domain (vertical axis is amplitude and the horizontal axis is time).
The basic approach is this:
collect your audio data that you want to display. This may be the raw audio samples or may be the result of processing (e.g. averaging across N samples, or taking every Nth sample) to provide scaling on the x-axis. (Without scaling, you will need 48000 pixels to display 1 second of audio.)
Given this array of samples, you have to turn it into a aplitude-time plot. As a start you can create a new JPanel and override the paint method to draw the plot.
E.g.
public void paint(Graphics g)
{
short[] samples; // the samples to render - get this from step 1.
int height = getHeight();
int middle = height/2;
for (int i=0; i<samples.length; i++)
{
int sample = samples[i];
int amplitude = sample*middle;
g.drawLine(i, middle+amplitide, i, middle-amplitude);
}
}
This will draw a amplitude-time graph in your component. it will be as heigh as the component, and as wide as the number of samples.
To get animation, you regularly drop in a new short[] array into the component and call repaint() (All on the Swing Event thread, of course!).
Good luck!
Spectrum analyzers are based on Fast Fourier Transforms. You'll do well to read up on those. They transform signals from the time to the frequency domain.
So is it time or frequency that you want?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With