我有一个使用变量在 Java 中播放声音的函数,vol
如下面的代码所示。这个变量vol
是 0 和 1 之间的小数。当我播放 a0
时,声音基本上是可能的最低音量,而当我用 1 播放时,它尽可能响亮。我怎么知道程序的声音是什么分贝?我想知道给定的分位数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;
}
}