Reputation: 2527
So I am working on this application that deals with audio, and I have a buffer containing all of the data to be sent to the sound card. I was wondering if anybody has any suggestions as to the best way to plot this as a VU meter? If it makes any difference, I am using Java. Most of the examples I have seen have been for physical VU meters.
EDIT: I need to figure out how to get the volume of the audio buffer at any given point
Upvotes: 2
Views: 3981
Reputation: 3756
What my answer does is very roughly calculate a leaky integral of the absolute values of the buffer. Generally the 50% value is "off" as digital audio needs to reproduce both postive and negative sound pressures. See the wikipedia article on digital audio if you don't get this.
Real VU meaters are a leaky integrator of the amplitude of the signal. ( a simple buffer and a capacitor can suffice if the galvanometer or electonic VU meter chip has a sufficently high input resistance)
so for 16 bit samples the code might look something like.... (off the top of my head)
//set up
long total=0;
const long half = 32768; //2^(n-1)
const long decayInMilliseconds=30; // 30ms for the needle to fall back to zero.
// leak rate is enough to get the reported signal to decay to zero decayMilliseconds after
// the actual amplitude goes to zero.
int leakRate = (sample_rate*1000 /decayInMilliseconds) * half;
// goes in a loop to do the work
// can be executed on buffer-loads of data at less than the sampling rate, but the net number of calls to it persecond needs to equal the sampling rate.
int amplitude = buffer[i]-half;
total = total + abs(amplitude);
total = total - leakRate;
if( total > half) {
total = half;
}
//total is the current "vu level".
The value of total is generally displayed on a logarithmic scale.
Upvotes: 1