【发布时间】:2019-03-22 17:55:50
【问题描述】:
所以我正在尝试测试一些分析某些 PCM 数据量的代码。我得到了一些奇怪的体积测量结果,这些测量结果对我从 Audacity 获得的数据没有意义。看来我的测量结果到处都是。
我不确定我的错误是在我读取 WAV 数据的方式上,还是在我计算音量的方式上。
这里是我以字节形式读取数据并转换为短裤的地方,因为它是 PCM 16 位。
InputStream pcmStream = this.getClass().getClassLoader().getResourceAsStream("Test-16Bit-PCM.wav");
ArrayList<Byte> bytes = new ArrayList<>();
int b = pcmStream.read();
while(b != -1)
{
bytes.add((byte)b);
b = pcmStream.read();
}
// First 44 bytes of WAV file are file info, we already know PCM properties since we recorded test audio
byte [] bytesArray = new byte[bytes.size()-44];
for(int i = 44; i < bytes.size(); i++)
{
bytesArray[i-44] = bytes.get(i);
}
bytes = null;
pcmStream = null;
short [] pcm = new short[bytesArray.length/2];
ByteBuffer bb = ByteBuffer.wrap(bytesArray).asShortBuffer().get(pcm);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.asShortBuffer().get(pcm);
bytesArray = null;
然后将 short [] 直接传递给我的分析器,然后我将数据拆分为 0.1 秒的时间步长,并对每个时间步长的音量进行平均。
这是我计算 RMS 和 dB 的地方
double sumOfSamples = 0;
double numOfSamples = settings.shortsPerTimeStep();
for(int i = start; i < start+settings.shortsPerTimeStep(); i++)
{
sumOfSamples = originalPcm[i]*originalPcm[i];
}
double rms = Math.sqrt(sumOfSamples/numOfSamples);
// Convert to decibels
calculatedVolume = 20*Math.log10(rms/20);
我正在阅读的音频是在 44100 MONO 录制的,并大胆保存为 WAV 16 Signed PCM。不知道我做错了什么。
任何帮助将不胜感激!谢谢
编辑:发现我读错了 WAV 数据。我通过添加 end little endianess 来修复它。但是我仍然对如何计算音量感到困惑。这些值更好,但仍然难以破译,我不确定我的 RMS 是什么单位,以及参考值应该是什么单位。
【问题讨论】: