This discussion is archived
9 Replies Latest reply: Mar 16, 2011 7:52 AM by captfoss

# How to show the volume in a Meter?

Currently Being Moderated
Hello,

I m newbie in programming in java sound,

And i would like to know how to show the volume from a recording in real time in a meter? I allready have the meter but now i don´t know how to pass the volume and show in the meter.

Somebody can help me?

Thanks
• ###### 1. Re: How to show the volume in a Meter?
Currently Being Moderated
844429 wrote:
..And i would like to know how to show the volume from a recording in real time..
It can never be truly 'real time' since the calculations require a 'chunk' of sound samples to produce a value. The shorter the chunk, the more arbitrary the value, but the more 'real time' it seems.
..in a meter? I allready have the meter..
(chuckle) That is the easy part.
..but now i don´t know how to pass the volume and show in the meter.
In what units? dB, RMS, tuna? I did an RMS calculation for the left/right channels of a more complex audio trace. RMS is relatively easy to calculate. E.G.
``````/** Computes the RMS volume of a group of signal sizes ranging from -1 to 1. */
public double volumeRMS(double[] raw) {
double sum = 0d;
if (raw.length==0) {
return sum;
} else {
for (int ii=0; ii<raw.length; ii++) {
sum += raw[ii];
}
}
double average = sum/raw.length;

double[] meanSquare = new double[raw.length];
double sumMeanSquare = 0d;
for (int ii=0; ii<raw.length; ii++) {
sumMeanSquare += Math.pow(raw[ii]-average,2d);
meanSquare[ii] = sumMeanSquare;
}
double averageMeanSquare = sumMeanSquare/raw.length;
double rootMeanSquare = Math.pow(averageMeanSquare,0.5d);

return rootMeanSquare;
}``````
Of course, you will need to get directly into the sound data streams then convert the signal to PCM, split the channels and scale the values to a range of -1 to 1 to use that exact method shown. ;)
• ###### 2. Re: How to show the volume in a Meter?
Currently Being Moderated
Hello Andrew.

Thanks for the information really helpful :) .

I would like to show the volume in dB.

But tell me i need to:

1 - capture the sound
2 - read the TargetDataLine
3 - Convert to PCM
4 - calculate the volume
5 - show in the panel

right?

Thanks.
• ###### 3. Re: How to show the volume in a Meter?
Currently Being Moderated
Hello Andrew.

Thanks for the information really helpful .

I would like to show the volume in dB.

But tell me i need to:

1 - capture the sound
2 - read the TargetDataLine
3 - Convert to PCM
4 - calculate the volume
5 - show in the panel

right?

Thanks.
• ###### 4. Re: How to show the volume in a Meter?
Currently Being Moderated
Than how i show the value in the panel? I can't understand how i will pass the value from the volume (rms) to the panel, in this case to the meter.

Thanks
• ###### 5. Re: How to show the volume in a Meter?
Currently Being Moderated
844429 wrote:
1 - capture the sound
2 - read the TargetDataLine
3 - Convert to PCM
To convert what you just said into fishing terms, you just suggested you need to...

1 - Catch a fish
2 - Cast my lure into the water
3 - Convert my fish into a fish

Reading the TDL is captureing the sound, and it'll already be in PCM when you get it...
4 - calculate the volume
5 - show in the panel
You need to
1 - Capture the sound by reading the PCM byte stream from the TDL
2 - Convert the byte stream into a sample stream based on the format of the audio
3 - Run whatever calculations you want to determine the volume
4 - Update your panel to show the new volume calculations...
• ###### 6. Re: How to show the volume in a Meter?
Currently Being Moderated
Your reply have some fun :) but was useful!

Thanks for all the help ;)
• ###### 7. Re: How to show the volume in a Meter?
Currently Being Moderated
DavidHenriques wrote:
Your reply have some fun :) ..
'The guru' often does. ;)
..but was useful!
If you found captfoss' (or any) reply to be either useful or correct, please mark it as such when you are logged in. Also, a thread can be marked as 'answered' (if it has a correct answer).

BTW - much prefer the 'DavidHenriques' handle to 'userNNNN..' :-)
• ###### 8. Re: How to show the volume in a Meter?
Currently Being Moderated
Hello,

Yes is much better a nice nickname instead some random numbers :)
• ###### 9. Re: How to show the volume in a Meter?
Currently Being Moderated
DavidHenriques wrote:
Yes is much better a nice nickname instead some random numbers :)
Personally, most of my friends call me 845763128...

#### Legend

• Correct Answers - 10 points
• Helpful Answers - 5 points