2

有没有办法在录制声音时获得声音的分贝值?我现在MediaRecorder用来录声音

我无法使用 Marketplace 上的任何应用程序,因为我无法确定用户是否会将其安装在他们的手机上,例如 Audalyzer

我正在使用以下公式,但不确定它们是否正确或我的结果是否正确!

short data[] = new short[bufferSize];
read = recorder.read(data, 0, bufferSize);
double p2 = data[data.length-1];
System.out.println("p2: " + p2);
double decibel;

if (p2==0)
   decibel=Double.NEGATIVE_INFINITY;
else
   decibel = 20.0*Math.log10(p2/65535.0);
   System.out.println("p2/65535: " + (p2/65535.0));

System.out.println("decibel: " + decibel);

当前结果:

    01-11 16:43:03.821: I/System.out(14530): p2: 0.0
01-11 16:43:03.821: I/System.out(14530): p2/65535: 0.0
01-11 16:43:03.821: I/System.out(14530): decibel: -Infinity
01-11 16:43:03.911: I/System.out(14530): p2: 0.0
01-11 16:43:03.911: I/System.out(14530): p2/65535: 0.0
01-11 16:43:03.911: I/System.out(14530): decibel: -Infinity
01-11 16:43:04.001: I/System.out(14530): p2: 0.0
01-11 16:43:04.001: I/System.out(14530): p2/65535: 0.0
01-11 16:43:04.001: I/System.out(14530): decibel: -Infinity
01-11 16:43:04.091: I/System.out(14530): p2: 0.0
01-11 16:43:04.091: I/System.out(14530): p2/65535: 0.0
01-11 16:43:04.091: I/System.out(14530): decibel: -Infinity
01-11 16:43:04.191: I/System.out(14530): p2: 0.0
01-11 16:43:04.191: I/System.out(14530): p2/65535: 0.0
01-11 16:43:04.191: I/System.out(14530): decibel: -Infinity
01-11 16:43:04.281: I/System.out(14530): p2: 0.0
01-11 16:43:04.281: I/System.out(14530): p2/65535: 0.0
01-11 16:43:04.281: I/System.out(14530): decibel: -Infinity
01-11 16:43:04.371: I/System.out(14530): p2: 0.0
01-11 16:43:04.371: I/System.out(14530): p2/65535: 0.0
01-11 16:43:04.371: I/System.out(14530): decibel: -Infinity
4

1 回答 1

2

使用AudioRecord,您可以直接访问音频样本...从那里,您可以以分贝或任何您想要的方式计算声音的音量...

似乎也是同一个问题(并且有计算公式)

编辑(基于评论和额外代码):

现在,您正在使用的变量 data[] 是声明为数组或字节还是短裤?这将改变将使用哪一个 read() 函数。如果您将其声明为短裤,那么它将照顾您的 16 位。如果将其声明为字节数组,则必须组合两个连续的字节。

您不必担心负值和正值,只需将 data[] 声明为“无符号短”数组即可。

您需要了解分贝值是将您当前的音量与其他音量进行比较。我不是真正的专家,但我相信大多数时候你会将它与最大可能的幅度进行比较。我相信,现在,你正在做的计算是比较两个连续的样本,这就是为什么这个值相当低......而不是 p1,而是使用值 65535(这是可能的最大值)。然后你应该看到分贝值是负值,当什么都没有并且应该随着噪声增加(但仍然保持负值)。

编辑(基于最新代码):

由于样本大小为 16 位,因此请使用短裤...

short buffer[] = new short[bufferSize];
read = recorder.read(buffer, 0, bufferSize);
double p2 = data[data.length-1];
double decibel;
if (p2==0)
    decibel=Double.NEGATIVE_INFINITY;
else
    decibel = 20.0*Math.log10(p2/65535.0);

尝试沿途打印所有值(data[data.length-1]、p2、p2/65535、Math.log10(p2/65535)...等...)你会发现哪里有 0出现在不应该出现的地方。

于 2012-01-07T00:57:31.817 回答