I have a function which plays a sound in Java using a variable vol
as you can see in the code below. This variable vol
is a decimal between 0 and 1. When I play a 0
, the sound is basically the lowest volume possible, and when I play it with a 1, it is as loud as possible. How can I know what decibles the program's sound is making? I want to know the decibles for a given vol
.
public static final AudioFormat DEFAULTAUDIOFORMAT = new AudioFormat(44100, 16, 2, true, true);
private void generateTone() {
int channels = DEFAULTAUDIOFORMAT.getChannels();
int sampleSizeInBytes = DEFAULTAUDIOFORMAT.getSampleSizeInBits() /8;
float sampleRate = DEFAULTAUDIOFORMAT.getSampleRate();
int timedSample = (int) (sampleRate * msecs / 1000f);
int len = timedSample * channels * sampleSizeInBytes;
int shapeLen = (int) (.05 * timedSample);
buf = new byte[len];
double scale;
for ( int i = 0, j = 0; i < timedSample; ++i )
{
int wave = (int)(vol * 32767.0 * Math.sin(2.0 * Math.PI * hz * i / sampleRate));
scale = 1.0;
if (i <= shapeLen) {
scale = i/(double)shapeLen;
}
else if (i >= timedSample - shapeLen) {
scale = 1.0-((i- timedSample + shapeLen)/(double)(shapeLen-1));
}
wave = (int)(scale * wave);
buf[j++] = (byte)(wave >>> 8);
buf[j++] = (byte) wave;
buf[j++] = (byte)(wave >>> 8);
buf[j++] = (byte) wave;
}
}