从短数组中提取分贝,这些分贝来自来自Android Audiorecord的字节阵列

发布于 2025-01-22 20:25:09 字数 872 浏览 0 评论 0原文

我想在一段时间内获得平均分贝(假设1秒)。 编辑:这是要检测到大声的声音。

我有音频的字体,以16kHz的采样率和16位的深度。

我将其转换为短阵列,以使随着时间的流逝的幅度振幅:

fun bytesToShort(byteArray: ByteArray): ShortArray {
    val shortOut = ShortArray(byteArray.size / 2)
    val byteBuffer: ByteBuffer = ByteBuffer.wrap(byteArray)
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortOut)
    return shortOut
}

从我收集的内容中,这是获得振幅的正确方法。

现在,我的转换算法:

val amplitudeValues = bytesToShort(bytesForDecibels)
val rms = sqrt(amplitudeValues.sumOf { it * it } / amplitudeValues.size.toDouble())
val decibels = 20 * log10(rms)

它吐出明显的分贝值。但是,我尝试了一份录音,我在麦克风中尖叫,平均无法达到超过51分贝。我的算法是否正确,麦克风根本无法拾取比51dB的声音,还是算法不完整/不正确?

编辑:我没有提到我想检测到大声的声音。从 @gabesechan的评论中读取,我需要在基线振幅上检查振幅尖峰以检测大声的噪音。因此,我将根据麦克风活动设置一个基线,并且基线上的任何大尖峰都是大声的噪音。

I want to get the average decibels over a period of time (lets say 1 second). Edit: This is to detect loud noises.

I have a ByteArray of audio, at 16khz sample rate and 16 bit depth.

I convert it to a ShortArray to get the amplitude of over time, like so:

fun bytesToShort(byteArray: ByteArray): ShortArray {
    val shortOut = ShortArray(byteArray.size / 2)
    val byteBuffer: ByteBuffer = ByteBuffer.wrap(byteArray)
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortOut)
    return shortOut
}

From what I gather, this is the correct way to get the amplitude.

Now my converting algorithm:

val amplitudeValues = bytesToShort(bytesForDecibels)
val rms = sqrt(amplitudeValues.sumOf { it * it } / amplitudeValues.size.toDouble())
val decibels = 20 * log10(rms)

It spits out apparent decibel values. However, I tried a recording where I literally scream in the microphone, and can't reach an average of over 51 decibels. Is my algorithm correct and the microphone simply cannot pick up louder sounds then 51db, or is the algorithm incomplete/incorrect?

Edit: I didn't mention I wanted to detect loud noises. Reading from @GabeSechan's comment, I need to check amplitude spikes over a baseline amplitude to detect a loud noise. So I will set a baseline according to microphone activity and any large spikes over baselines are loud noises.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文