Reputation: 5538
I want to implement a VU meter
in my recording app. My app is recording in WAV format
and I want the VU meter to update itself at each second.
Please guide me how to do this:
let's say the sample rate is 44100. I find the max absolute value from audioData that comes in 1 sec. Should I do this:
int average = maxSampleValue / nrOfSamplesInOneSec;
?
I will convert the maxAmpl like this: powerDb = 20 * log10(getAmplitude());
My VU
meter will be a rectangle with a fill color(green - yellow - red)
. What is the maximum height of this rectangle(in dB)? And the minimum? Where(in dB) the green color should change in yellow?
Please help me with this. Also, a (short)tutorial will be very convenient for me.
Upvotes: 1
Views: 1271
Reputation: 9645
An analog VU meter shows approximately the average power in dB scale, where the average is taken over a specific time window. So you need to decide what will be your time window in order to get an informative and visually attractive result. I suggest you start with 300 milliseconds, and then play with the range a little bit. In order to find the amplitude in dB you need to average the absolute value of the signal over the time window, take the 10-base log of the results, and multiply it by 20. Note that unless you want to show actual values in addition to the green-yellow-red meter, the choice of base 10 and multiplying by 20 is arbitrary. Another thing to note is that the window length you use to average should be decoupled from the size of the audio buffer - you accumulate samples and find the average as soon as you have enough samples to fill a window (or more), regardless of the number of incoming samples you get from the hardware.
In order to translate dB level to your visual meter, you need to determine your minimal and maximal possible values. Theoretically the minimal value is -infinity (log(0)), but noise will make it a finite value which you can measure by recording a few seconds of silence. The maximal value is set by the number of bits and the representation (float or int). If you use float, the maximal amplitude is 1.0 so the max dB level is 0. If you use 16 bit integer, maximal value is 32768, which gives about 90dB. Note also that you might want to give the user an indication that he/she is close to the maximal allowed value, by making the maximal value of your meter some dB's less than the actual maximum, e.g 3 or 6 db less.
Regarding the yellow range, it is a matter of application, so you have to consider the use cases and what is actually indicated by the level you present to the user.
Upvotes: 1