| 1 | import Foundation |
| 2 | |
| 3 | /// What the received audio actually sounded like, as far as the counters |
| 4 | /// can say — the distinction a person reporting "it growled" cannot make |
| 5 | /// for us. |
| 6 | /// |
| 7 | /// Two failures wear the same description. Loss is the decoder inventing |
| 8 | /// audio that never arrived, and sounds like dropouts and robotic |
| 9 | /// stuttering. Drift is the jitter buffer stretching or compressing the |
| 10 | /// signal because the sender's clock and this device's clock disagree, |
| 11 | /// and stretching a voice is what a growl *is*. Bytes arriving tells you |
| 12 | /// neither. |
| 13 | public struct AudioHealth: Equatable, Sendable { |
| 14 | /// Samples the decoder invented, against samples it received. |
| 15 | public let concealed: UInt64 |
| 16 | public let total: UInt64 |
| 17 | /// Samples the jitter buffer added or dropped to track the sender's |
| 18 | /// clock. Either one running is drift; which one says which way. |
| 19 | public let inserted: UInt64 |
| 20 | public let removed: UInt64 |
| 21 | public let jitterBufferMs: UInt64 |
| 22 | |
| 23 | public init(concealed: UInt64, total: UInt64, inserted: UInt64, removed: UInt64, jitterBufferMs: UInt64) { |
| 24 | self.concealed = concealed |
| 25 | self.total = total |
| 26 | self.inserted = inserted |
| 27 | self.removed = removed |
| 28 | self.jitterBufferMs = jitterBufferMs |
| 29 | } |
| 30 | |
| 31 | public init(_ stats: [String: UInt64]) { |
| 32 | self.init( |
| 33 | concealed: stats["concealedSamples"] ?? 0, |
| 34 | total: stats["totalSamplesReceived"] ?? 0, |
| 35 | inserted: stats["insertedSamplesForDeceleration"] ?? 0, |
| 36 | removed: stats["removedSamplesForAcceleration"] ?? 0, |
| 37 | jitterBufferMs: stats["jitterBufferDelayMs"] ?? 0) |
| 38 | } |
| 39 | |
| 40 | /// The fraction of received audio the decoder had to invent, 0 to 1. |
| 41 | /// Zero when nothing has been received, because no audio is not clean |
| 42 | /// audio and a reader should not be told otherwise by a division. |
| 43 | public var concealmentRate: Double { |
| 44 | total == 0 ? 0 : Double(concealed) / Double(total) |
| 45 | } |
| 46 | |
| 47 | /// The fraction of received audio the jitter buffer stretched or |
| 48 | /// squeezed. Both directions count: what matters is that the signal was |
| 49 | /// rescaled, not which way it went. |
| 50 | public var driftRate: Double { |
| 51 | total == 0 ? 0 : Double(inserted + removed) / Double(total) |
| 52 | } |
| 53 | |
| 54 | /// Nothing received yet. Distinct from healthy, and said differently. |
| 55 | public var silent: Bool { total == 0 } |
| 56 | |
| 57 | /// One plain sentence, for the person who has opened this panel because |
| 58 | /// something sounds wrong. Thresholds are deliberately blunt: this is a |
| 59 | /// pointer towards which half of the problem to look at, not a |
| 60 | /// measurement anyone should tune against. |
| 61 | /// |
| 62 | /// Drift is named first when both are up. Loss is common, usually |
| 63 | /// transient and widely understood; sustained drift is rarer, is what |
| 64 | /// makes a voice sound wrong rather than broken, and is the one nobody |
| 65 | /// thinks to look for. |
| 66 | public var summary: String { |
| 67 | if silent { return "No audio received yet." } |
| 68 | let loss = Int((concealmentRate * 100).rounded()) |
| 69 | let drift = Int((driftRate * 100).rounded()) |
| 70 | if driftRate >= 0.02 { |
| 71 | return "Audio is being resampled to track the sender's clock (\(drift)%). " |
| 72 | + "That is what makes a voice sound slowed or growling." |
| 73 | } |
| 74 | if concealmentRate >= 0.05 { |
| 75 | return "\(loss)% of audio did not arrive and was filled in. That sounds like dropouts." |
| 76 | } |
| 77 | return "Audio is arriving cleanly (\(loss)% filled in, \(drift)% resampled)." |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// The one line the device panel shows about audio, and only when |
| 82 | /// something is actually wrong; the full readout is a tap away behind |
| 83 | /// "Audio details". Nil keeps the panel clean. Worst first: a voice that |
| 84 | /// sounds wrong, then one that breaks up, then processing that is not |
| 85 | /// doing what was asked. |
| 86 | public enum AudioWarning { |
| 87 | public static func line( |
| 88 | health: AudioHealth?, processing: ProcessingReport?, reduceNoise: Bool |
| 89 | ) -> String? { |
| 90 | if let health, !health.silent { |
| 91 | if health.driftRate >= 0.02 { |
| 92 | return "Incoming audio is being stretched to keep time, so voices may sound slowed or growling." |
| 93 | } |
| 94 | if health.concealmentRate >= 0.05 { |
| 95 | return "\(Int((health.concealmentRate * 100).rounded()))% of incoming audio is going missing." |
| 96 | } |
| 97 | } |
| 98 | guard let processing, processing.started else { return nil } |
| 99 | if processing.refused != nil { return "WebRTC refused the audio processing Calls asked for." } |
| 100 | if processing.echoCancellation == "off" { |
| 101 | return "Echo cancellation is off, so others may hear themselves." |
| 102 | } |
| 103 | if reduceNoise, case .unsupportedRate(let rate) = processing.suppression { |
| 104 | return "Noise reduction is bypassed: the microphone runs at \(rate / 1000) kHz." |
| 105 | } |
| 106 | return nil |
| 107 | } |
| 108 | } |
| 109 | |