【问题标题】:How can I normalized decibel value and make it between 0 and 1如何标准化分贝值并使其介于 0 和 1 之间
【发布时间】:2015-07-23 21:32:30
【问题描述】:

我正在尝试通过循环播放音频样本并获取每个通道的平均功率来获取音频的功率。

我想获得一个介于 0 和 1 之间的值,以反映正在播放的音频的强度。现在我得到 dB(分贝值),它是一个从 -160(接近静音)到 0(非常响亮)的浮点值。

这是我用 Swift 编写的代码:

func configureAudio() {
    audioController.play()
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    audioController.numberOfLoops = 0
    audioController.meteringEnabled = true
    var audTimer = CADisplayLink(target: self, selector: "monitorAudio")
    audTimer.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
}

func monitorAudio() {
    audioController.updateMeters()

    var dB = Float(0)
    for i in 0..<audioController.numberOfChannels {
        dB += audioController.averagePowerForChannel(i)
    }

    dB /= Float(audioController.numberOfChannels)
//code that I copied from website but obviously it is not normalization
    var power = (Int(log10(dB+161)/log10(1.5) * 100) - 1220) * 6
    if power < 0 {
        power = 0
    }
    println(power)
}

我不确定我得到的 dB 值是准确还是我做错了什么?

现在我使用我找到的一些代码获得了 0-190 之间的值,但这对我来说没有多大意义。

我需要将我得到的 dB 值标准化为 0 - 1

编辑 - 随意在 Obective-C 中编写代码

【问题讨论】:

    标签: ios audio avaudioplayer decibel


    【解决方案1】:

    averagePowerForChannel 返回一个 dB 值,其中0 dB 表示数字满量程。如果您想要介于 0 和 1 之间的值,那么 dB 肯定不是适合您的比例。

    因为您的代码有点暗示将数字电平转换为 dB 的功能是

    dB = 20 * log10(ffs);
    

    所以当 ffs = 1.0 时,dB 为 0,当 ffs == 0 时,dB 为 -inf。

    你只需要走另一条路。

    var power = exp(10, dB/20);
    

    【讨论】:

    • 我不太明白你的意思。什么是 log10(ffs)?
    • 我的意思是,您开始使用的 dB 值已经应用了 log10,因此您再次调用 log10,这与您想要的完全相反 - exp 函数以 10 为基数。
    猜你喜欢
    • 2013-10-18
    • 2012-04-10
    • 2015-02-20
    • 2012-06-29
    • 2022-11-29
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多