So I am writing a voice activated recording program, and one of the things I would like it to do is plot the current and past volume level on a JPanel.

Write now I am simply reading one byte from the audio buffer, converting it to an int, taking its absolute value, and more or less directly plotting the value on a JPanel. Things more or less work, but I get the feeling that my method is not very orthodox.

So my question basically boils down to the following:
how is audio data typically interpreted when used to represent the current volume level?

Here are some bits of code:

In the following code the byte read from the microphone input is converted to an int, and sent to the JPanel osc, for drawing.

int bufferSize = (int)format.getSampleRate() * format.getFrameSize();
        byte buffer[] = new byte[bufferSize];


while (running) {
              int count =
                line.read(buffer, 0, buffer.length);
              if (count > 0) {

                out.write(buffer, 0, count);

The following is the actual JPanel code used to plot the int.

public void addLevel(int i)

    public void draw()
        g = getGraphics();
        int bottom = getHeight();
        //int mult = bottom/128;

        int edge = getWidth()-1;
        for(int c = columns.size()-1; c>-1;  c--)
            System.out.println(edge+" "+bottom+" 1 "+columns.get(c));

The previous code results in this:

And the actual audio format:

float sampleRate = 8000;
        int sampleSizeInBits = 8;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = true;

So how should this be done? Should I read more bytes? Should I take an average of several bytes? Or is using plain byte information for the volume level foolish altogether?

8 Years
Discussion Span
Last Post by kimbokasteniv

It depends on why you are displaying the level - if it's to ensure the signal is loud enough, you can just pick a subset of the samples (say 10 / sec) and average them. If you're trying to avoid clipping you need to check every sample and keep the max. value (once again in maybe 10 values / second).
Check out this WikiP article http://en.wikipedia.org/wiki/VU_meter which gives links to useful data on how to scale the output and what time constants to use for different needs.


Alright, that is helpful information. I think that is all I need to know for my purposes, thanks for the help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.