moda2016 2023-06-29 18:12 采纳率: 0%
浏览 276
已结题

ios获取麦克风音频输入数据(PCM)计算DBSPL后与真实值差距很大

iOS开发者请教,使用OC框架AVFAudio,获取设备32位麦克风的PCM数据,获取的结果均为-1~1之间(已经使用了固定噪声源真实值已经突破100分贝),根据换算的结果无法突破100分贝(20 * log10(PCM/ 0.00002)),请问各位知道是什么原因或问题吗?

import Foundation
import AVFAudio
import Accelerate
final class DecibelAudioManager: NSObject {
  private override init() {
  }
  @objc public static func shared() -> DecibelAudioManager {
    struct SingleStruct {
      static var single = DecibelAudioManager()
    }
    return SingleStruct.single
  }
  private var sampleRate: Double = 44100
  lazy var audioEngine = {
    let engine = AVAudioEngine()
    let inputNode = engine.inputNode
    let bus = 0
    let inputFormat = inputNode.inputFormat(forBus: bus)
    var setting = inputFormat.settings
    print("默认采用PCM格式\(inputFormat.settings)")
    inputNode.installTap(onBus: bus, bufferSize: AVAudioFrameCount(0.1 * sampleRate), format: inputFormat) { [weak self] (buffer, time) -> Void in
      guard let this = self else {
       return
      }
      let channels = UnsafeBufferPointer(start: buffer.floatChannelData, count: Int(buffer.format.channelCount))
      let floats = Array(UnsafeBufferPointer(start: channels[0], count: Int(buffer.frameLength)))
      for i in 0..<50 {
        let rms = floats[i]
        print("pcm:\(rms) ---- 32位")
      }
    }
    engine.prepare()
    return engine
  }()
  @objc public func startRecord() -> Void {
    let audiosession = AVAudioSession.sharedInstance()
    do{
      try audiosession.setCategory(.playAndRecord)
      try audiosession.setPreferredSampleRate(44100)
      try audiosession.setPreferredIOBufferDuration(0.1)
      try audiosession.setActive(true, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
    }catch{
      print(error)
    }
    try! self.audioEngine.start()
  }
  @objc public func stopRecord() -> Void {
    audioEngine.stop()
  }
}
结果输出
pcm:-0.82569194 ---- 32位
pcm:-0.82774025 ---- 32位
pcm:-0.83398014 ---- 32位
pcm:-0.87197787 ---- 32位
pcm:-0.90468484 ---- 32位
pcm:-0.9037836 ---- 32位
pcm:-0.9085202 ---- 32位
pcm:-0.913189 ---- 32位
pcm:-0.89893526 ---- 32位
pcm:-0.8763739 ---- 32位
pcm:-0.84504193 ---- 32位
pcm:-0.8274844 ---- 32位
pcm:-0.8118507 ---- 32位
pcm:-0.77658844 ---- 32位
pcm:-0.7597292 ---- 32位
pcm:-0.7476263 ---- 32位
pcm:-0.7350073 ---- 32位
pcm:-0.7227265 ---- 32位
pcm:-0.69862187 ---- 32位
pcm:-0.6939462 ---- 32位
pcm:-0.6953905 ---- 32位
pcm:-0.697331 ---- 32位
pcm:-0.71514064 ---- 32位
pcm:-0.7449271 ---- 32位
pcm:-0.765127 ---- 32位
pcm:-0.7787468 ---- 32位
pcm:-0.8179313 ---- 32位
pcm:-0.8538925 ---- 32位
pcm:-0.8675249 ---- 32位
pcm:-0.8712871 ---- 32位
pcm:-0.88458264 ---- 32位
pcm:-0.90770584 ---- 32位
pcm:-0.91529685 ---- 32位
pcm:-0.026977815 ---- 32位
pcm:0.024105616 ---- 32位
pcm:0.0715868 ---- 32位
pcm:0.13334471 ---- 32位
pcm:0.2160877 ---- 32位
pcm:0.3117581 ---- 32位
pcm:0.37369388 ---- 32位
  • 写回答

10条回答 默认 最新

  • 竹山全栈 2023-06-30 10:35
    关注
    获得1.50元问题酬金

    输出分贝试试看

    if let channelData = buffer.int32ChannelData {
      let channelCount = Int(buffer.format.channelCount)
      
      for channelIndex in 0..<channelCount {
        let channel = channelData[channelIndex]
        let channelPointer = channel.bindMemory(to: Int32.self, capacity: Int(buffer.frameLength))
        let pcmData = Array(UnsafeBufferPointer(start: channelPointer, count: Int(buffer.frameLength)))
        
        for i in 0..<pcmData.count {
          let pcm = pcmData[i]
          let decibel = 20 * log10(abs(Float(pcm)) / 0.00002)
          print("pcm:\(pcm) ---- 分贝值:\(decibel)")
        }
      }
    }
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月8日
  • 赞助了问题酬金50元 6月30日
  • 创建了问题 6月29日