在录制声音时获取分贝值?

发布于 2024-12-25 12:15:15 字数 1745 浏览 3 评论 0原文

有没有办法在录制声音时获取声音的分贝值?我正在使用 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

Is there a way of getting the decibel value of sound whilst it's being recorded? I'm using MediaRecorder to record the sound at the moment

I can't use any applications on the Marketplace as I can't be sure the user will have it installed on their phone e.g. Audalyzer

I'm using the following formulas but not sure if they're correct or if my results are correct!

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);

Current results:

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好倦 2025-01-01 12:15:15

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

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

编辑(根据评论和额外的代码):

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

您不应该担心负值和正值,您应该将 data[] 声明为“无符号短整型”数组。

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

编辑(基于最新代码):

由于样本大小是16位,所以使用shorts...

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 的地方出现了。

Using AudioRecord, you can access the audio samples directly... from there, you can compute the volume of the sounds in decibels or whichever way you want to...

This seems to be about the same question too (and has the formula for the calculation)

EDIT (based on the comments and extra code):

Now, the variable data[] you are using is it declared as an array or bytes or of shorts? That will change which one of the read() function will be used. If you declare it as shorts, then that will take care of your 16 bits. If you declare it as an array of bytes, you'll have to combine two consecutive bytes.

You should not worry about negative and positive values, you should just declare data[] as an array of 'unsigned short'.

You need to understand the decibel value is comparing your current volume to something else. I am not really an expert, but I believe most of the time you compare it to the maximum possible amplitude. I believe, right now, the calculation you are doing is comparing two consecutive samples, that's why the value is rather low... Instead of p1, use value 65535 (that's the maximum value possible) instead. Then you should see the decibel value is a negative value when there is nothing and should increase with noise (but still remain negative).

EDIT (based on latest code):

Since the sample size is 16 bits, use shorts...

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);

Try to print all the values along the way (data[data.length-1], p2, p2/65535, Math.log10(p2/65535)...etc...) you'll find where there is a 0 coming up where there should not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文