简体   繁体   English

快速将AVAudioEngine AVAudioPCMBuffer转换为PowerLevel

[英]Swift AVAudioEngine AVAudioPCMBuffer to PowerLevel

I've been working on a project to create an application that displays the power level from the microphone. 我一直在进行一个项目,以创建一个显示麦克风功率电平的应用程序。 I'm aware of the AVAudioRecorder class what provides the average/peak power, but I want to be able only to record the power level for certain bands. 我知道AVAudioRecorder类提供了平均/峰值功率,但是我只希望记录某些频段的功率电平。 I used the AudioUnitEQ class to set up the bands, and have attached it to an AVAudioEngine and started recording. 我使用AudioUnitEQ类来设置频段,并将其附加到AVAudioEngine并开始录制。 Currently, I've been using the installTap method to get the AVAudioPCMBuffer, which is where I'm stuck. 当前,我一直在使用installTap方法来获取AVAudioPCMBuffer,这是我遇到的问题。 Is there a way to convert this to a power level? 有没有办法将其转换为功率电平? Also, could my approach to this be entirely wrong? 另外,我的方法完全错误吗?

You can get avg/peek like this code. 您可以像以下代码一样获得avg / peek。

engine.installTap(......) { buffer in 
    guard let data = buffer.floatChannelData?[0] else {
        return
    }
    var dbData = [Float](repeating: 0.0, count: data.count)
    var one: Float = 0.0
    vDSP_vdbcon(&data, 1, &one, &dbData, 1, data.count, 1)

    var avgLevel: Float = 0.0
    var peakLevel: Float = 0.0
    vDSP_rmsqv(dbData, 1, &avgLevel, vDSP_Length(buffer.frameLength))
    vDSP_maxmgv(dbData, 1, &peakLevel, vDSP_Length(buffer.frameLength))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM