mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-19 08:03:50 +02:00
#4096 - Extracted logic from the VoiceMessagePlaybackView. Exposed power levels from audio recorder.
This commit is contained in:
@@ -39,6 +39,10 @@ class VoiceMessageAudioRecorder: NSObject, AVAudioRecorderDelegate {
|
||||
return audioRecorder?.currentTime ?? 0
|
||||
}
|
||||
|
||||
var isRecording: Bool {
|
||||
return audioRecorder?.isRecording ?? false
|
||||
}
|
||||
|
||||
weak var delegate: VoiceMessageAudioRecorderDelegate?
|
||||
|
||||
func recordWithOuputURL(_ url: URL) {
|
||||
@@ -52,6 +56,7 @@ class VoiceMessageAudioRecorder: NSObject, AVAudioRecorderDelegate {
|
||||
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
|
||||
audioRecorder = try AVAudioRecorder(url: url, settings: settings)
|
||||
audioRecorder?.delegate = self
|
||||
audioRecorder?.isMeteringEnabled = true
|
||||
audioRecorder?.record()
|
||||
delegate?.audioRecorderDidStartRecording(self)
|
||||
} catch {
|
||||
@@ -59,11 +64,31 @@ class VoiceMessageAudioRecorder: NSObject, AVAudioRecorderDelegate {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func stopRecording() {
|
||||
audioRecorder?.stop()
|
||||
}
|
||||
|
||||
func peakPowerForChannelNumber(_ channelNumber: Int) -> Float {
|
||||
guard self.isRecording, let audioRecorder = audioRecorder else {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
audioRecorder.updateMeters()
|
||||
|
||||
return self.normalizedPowerLevelFromDecibels(audioRecorder.peakPower(forChannel: channelNumber))
|
||||
}
|
||||
|
||||
func averagePowerForChannelNumber(_ channelNumber: Int) -> Float {
|
||||
guard self.isRecording, let audioRecorder = audioRecorder else {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
audioRecorder.updateMeters()
|
||||
|
||||
return self.normalizedPowerLevelFromDecibels(audioRecorder.averagePower(forChannel: channelNumber))
|
||||
}
|
||||
|
||||
// MARK: - AVAudioRecorderDelegate
|
||||
|
||||
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully success: Bool) {
|
||||
@@ -77,6 +102,14 @@ class VoiceMessageAudioRecorder: NSObject, AVAudioRecorderDelegate {
|
||||
func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) {
|
||||
delegate?.audioRecorder(self, didFailWithError: VoiceMessageAudioRecorderError.genericError)
|
||||
}
|
||||
|
||||
private func normalizedPowerLevelFromDecibels(_ decibels: Float) -> Float {
|
||||
if decibels < -60.0 || decibels == 0.0 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
return powf((powf(10.0, 0.05 * decibels) - powf(10.0, 0.05 * -60.0)) * (1.0 / (1.0 - powf(10.0, 0.05 * -60.0))), 1.0 / 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
extension String: LocalizedError {
|
||||
|
||||
Reference in New Issue
Block a user