import Foundation /// What the received audio actually sounded like, as far as the counters /// can say — the distinction a person reporting "it growled" cannot make /// for us. /// /// Two failures wear the same description. Loss is the decoder inventing /// audio that never arrived, and sounds like dropouts and robotic /// stuttering. Drift is the jitter buffer stretching or compressing the /// signal because the sender's clock and this device's clock disagree, /// and stretching a voice is what a growl *is*. Bytes arriving tells you /// neither. public struct AudioHealth: Equatable, Sendable { /// Samples the decoder invented, against samples it received. public let concealed: UInt64 public let total: UInt64 /// Samples the jitter buffer added or dropped to track the sender's /// clock. Either one running is drift; which one says which way. public let inserted: UInt64 public let removed: UInt64 public let jitterBufferMs: UInt64 public init(concealed: UInt64, total: UInt64, inserted: UInt64, removed: UInt64, jitterBufferMs: UInt64) { self.concealed = concealed self.total = total self.inserted = inserted self.removed = removed self.jitterBufferMs = jitterBufferMs } public init(_ stats: [String: UInt64]) { self.init( concealed: stats["concealedSamples"] ?? 0, total: stats["totalSamplesReceived"] ?? 0, inserted: stats["insertedSamplesForDeceleration"] ?? 0, removed: stats["removedSamplesForAcceleration"] ?? 0, jitterBufferMs: stats["jitterBufferDelayMs"] ?? 0) } /// The fraction of received audio the decoder had to invent, 0 to 1. /// Zero when nothing has been received, because no audio is not clean /// audio and a reader should not be told otherwise by a division. public var concealmentRate: Double { total == 0 ? 0 : Double(concealed) / Double(total) } /// The fraction of received audio the jitter buffer stretched or /// squeezed. Both directions count: what matters is that the signal was /// rescaled, not which way it went. public var driftRate: Double { total == 0 ? 0 : Double(inserted + removed) / Double(total) } /// Nothing received yet. Distinct from healthy, and said differently. public var silent: Bool { total == 0 } /// One plain sentence, for the person who has opened this panel because /// something sounds wrong. Thresholds are deliberately blunt: this is a /// pointer towards which half of the problem to look at, not a /// measurement anyone should tune against. /// /// Drift is named first when both are up. Loss is common, usually /// transient and widely understood; sustained drift is rarer, is what /// makes a voice sound wrong rather than broken, and is the one nobody /// thinks to look for. public var summary: String { if silent { return "No audio received yet." } let loss = Int((concealmentRate * 100).rounded()) let drift = Int((driftRate * 100).rounded()) if driftRate >= 0.02 { return "Audio is being resampled to track the sender's clock (\(drift)%). " + "That is what makes a voice sound slowed or growling." } if concealmentRate >= 0.05 { return "\(loss)% of audio did not arrive and was filled in. That sounds like dropouts." } return "Audio is arriving cleanly (\(loss)% filled in, \(drift)% resampled)." } } /// The one line the device panel shows about audio, and only when /// something is actually wrong; the full readout is a tap away behind /// "Audio details". Nil keeps the panel clean. Worst first: a voice that /// sounds wrong, then one that breaks up, then processing that is not /// doing what was asked. public enum AudioWarning { public static func line( health: AudioHealth?, processing: ProcessingReport?, reduceNoise: Bool ) -> String? { if let health, !health.silent { if health.driftRate >= 0.02 { return "Incoming audio is being stretched to keep time, so voices may sound slowed or growling." } if health.concealmentRate >= 0.05 { return "\(Int((health.concealmentRate * 100).rounded()))% of incoming audio is going missing." } } guard let processing, processing.started else { return nil } if processing.refused != nil { return "WebRTC refused the audio processing Calls asked for." } if processing.echoCancellation == "off" { return "Echo cancellation is off, so others may hear themselves." } if reduceNoise, case .unsupportedRate(let rate) = processing.suppression { return "Noise reduction is bypassed: the microphone runs at \(rate / 1000) kHz." } return nil } }