views:

298

answers:

6

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.

+1  A: 

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?

duffymo
A: 

I haven't read the whole article, but Sun's Audio Tutorial might seem a good place to start looking. Also, here is a request which seems similar to yours.

Hope this helps :)

npinti
A: 

Fscape - http://sourceforge.net/projects/fscape/

Midhat
A: 

Thanks very much. 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). I don't want to be a pain in the neck , so actually I'm looking for someone willing to teach me how to do it. This might sound a real nuisance..I can imagine ... Anyway ,how should I start with? As I said I know how to do the playback .. How can I get the amplitude ? how can I draw a diagram showing amplitude and time? Any help will be appreciated..

wsr74ws84
+2  A: 

The basic approach is this:

  1. 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.)

  2. 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!

mdma
A: 

This is a pretty cool tutorial that answers most of your questions: Digital Signal Processing and Fast Fourier Transform in Java

Garg Unzola