提问人:Jon Doe 提问时间:9/16/2023 最后编辑:Jon Doe 更新时间:11/14/2023 访问量:20
录制时检测和计数噪音
Detect and count noises while recording
问:
我正在尝试编写一个应用程序,该应用程序可以通过麦克风检测何时发生了很大的噪音(即拍手声),以及在录制用户麦克风时实际发生了多少噪音。
这是我根据找到的解决方案编写的代码:
func startRecording(timerCheckInterval: TimeInterval = 0.3) {
let audioURL = FileManager.default.demoAudioURL
print("demoAudioURL: ", audioURL.absoluteString)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioURL, settings: settings)
audioRecorder?.isMeteringEnabled = true
audioRecorder?.delegate = self
audioRecorder?.prepareToRecord()
audioRecorder?.record()
timerCancellable = Timer.publish(every: timerCheckInterval, on: .main, in: .default)
.autoconnect()
.receive(on: DispatchQueue.main)
.sink { [weak self] timestamp in
guard let self, let audioRecorder = self.audioRecorder else {
return
}
audioRecorder.updateMeters()
let averagePower = Double(audioRecorder.averagePower(forChannel: 0))
let peakPower = Double(audioRecorder.peakPower(forChannel: 0))
let alpha: Double = 0.05
let peakPowerForChannel = pow(10, (0.05 * peakPower))
self.lowPassResults = alpha * peakPowerForChannel + (1.0 - alpha) * self.lowPassResults
if peakPower > 30 {
print(String(format:">>>+++Peak power: %.2f Average power: %.2f, currentTime: %.2f", peakPower, averagePower, audioRecorder.currentTime))
}
}
} catch {
//...
}
}
这似乎为我提供了一些信息。但是,如果,例如鼓掌,我鼓掌的声音足够大,可能会记录超过 30 次,比如 3-4 次。我试图研究 AVAudioEngine,但我的菜鸟搜索没有结果。
所以我的问题是,在录制音频时,有没有办法区分一个响亮的噪音何时结束,什么时候一个新的噪音开始了?或者也许有一个可以做到这一点的图书馆?peakPower
答: 暂无答案
评论