Reputation: 2196
Hello I am trying to monitor the db. Recording is not my main purpose.I am using avfoundation to do this. Below is the code in which I am trying to print the db. However I am only getting -160dbfs. Is there something that I am missing? Thank you!
override func viewDidLoad() {
super.viewDidLoad()
recordingSession = AVAudioSession.sharedInstance()
AVAudioSession.sharedInstance().requestRecordPermission { (hasPermission) in
if hasPermission{
print("Accpeted")
}
}
let filename = getDocumentsDirectory().appendingPathComponent("recording.m4a")
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey:12000,AVNumberOfChannelsKey:1,AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue]
do{
audioRecorder = try AVAudioRecorder(url: filename,settings:settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
timerOne()
}catch{
print("failed to initialize")
}
audioRecorder.record()
print(audioRecorder.isRecording)
}
func timerOne() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateMonitorDb), userInfo: nil, repeats: true)
}
@objc func updateMonitorDb(){
audioRecorder.updateMeters()
let monitorDb = audioRecorder.averagePower(forChannel: 1)
print(monitorDb)
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
Upvotes: 1
Views: 243
Reputation: 1118
You record only 1 channel and averagePower(forChannel:)
is 0-based.
@objc func updateMonitorDb(){
audioRecorder.updateMeters()
let monitorDb = audioRecorder.averagePower(forChannel: 0)
print(monitorDb)
}
Upvotes: 0