// A meeting's waveform: the shape the list, the player and any slip with a// `player` (`SlipFace`) draw, and the arithmetic that fits it to a width. In// Faces because a slip draws it for whatever table names a player.//// THE SAMPLES ARE NOT THE RECORDING'S. Nothing in the file stores a loudness// envelope for a meeting — no column, no media sidecar — and reading an hour of// m4a to draw a strip on a list row is the whole-body read the app refuses. So// the shape is a DETERMINISTIC function of the meeting's id: phrases and pauses// from a slow seeded envelope, the bars inside a phrase from a fast seeded// stream. The same meeting is the same strip on every launch, every device and// every screenshot, and the list and the open meeting draw one strip, not two.// It is a picture of "a conversation", never a claim about where the loud part// of THIS one was; the player's playhead is the only fact drawn on it.//// `bars` is the one reduction both screens use: N samples folded into however// many bars the width fits, by the loudest sample in each bucket.importCoreGraphicsimportFoundationenumMeetingWaveform {/// How many samples a meeting's shape is made of — enough for the widest/// player on a Mac column, folded down for a phone.staticlet sampleCount = 180/// The quietest a bar is drawn: a pause in a conversation is background hiss,/// never a gap in the paper.staticlet quietest = 0.05/// The meeting's shape: `count` values in 0…1, a function of `id` alone.staticfunc samples(id: String, count: Int = sampleCount) -> [Double] {guard count > 0else { return [] }// The phrases: a slow envelope, about one control point every fourteen// samples, so a strip reads as runs of speech with breaths between.let phrases = PaperNoise(seed: id, salt: envelopeSalt, count: max(4, count / 14))var state = PaperNoise.fnv1a(id) ^ (burstSalt &* 0x9E37_79B9_7F4A_7C15)if state == 0 { state = 0x9E37_79B9_7F4A_7C15 }return (0..<count).map { index in state ^= state << 13 state ^= state >> 7 state ^= state << 17let jitter = Double(state % 10_000) / 10_000let envelope = phrases.value(at: Double(index) / Double(count))// Below the threshold is a pause; above it, the louder the phrase// the taller its bars can reach.let speaking = max(0, (envelope - 0.3) / 0.7)let value = quietest + speaking * (0.3 + 0.7 * jitter)return min(1, max(0, value)) } }/// `samples` folded into at most `count` bars, each the LOUDEST sample in/// its bucket — a peak meter, so a short burst of speech never averages/// away into the pause around it.////// Fewer samples than bars are returned as they are: a reduction never/// invents a sample it was not given. Zero bars, or no samples, is nothing.staticfunc bars(_ samples: [Double], count: Int) -> [Double] {guard count > 0, !samples.isEmpty else { return [] }guard samples.count > count else { return samples }return (0..<count).map { bar inlet start = bar * samples.count / countlet end = max(start + 1, (bar + 1) * samples.count / count)return samples[start..<end].max() ?? 0 } }/// How many bars `width` points holds at `pitch` points a bar.staticfunc barCount(width: CGFloat, pitch: CGFloat) -> Int {guard width.isFinite, pitch.isFinite, width > 0, pitch > 0else { return0 }returnInt((width / pitch).rounded(.down)) }/// Where the playhead stands, 0…1. An unknown length stands at the start.staticfunc fraction(positionMs: Double, durationMs: Double) -> Double {guard positionMs.isFinite, durationMs.isFinite, durationMs > 0else { return0 }return min(1, max(0, positionMs / durationMs)) }/// The millisecond a touch at `fraction` of the strip names.staticfunc positionMs(fraction: Double, durationMs: Double) -> Double {guard fraction.isFinite, durationMs.isFinite, durationMs > 0else { return0 }return min(1, max(0, fraction)) * durationMs }staticlet envelopeSalt: UInt64 = 0x7068_7273// "phrs"staticlet burstSalt: UInt64 = 0x6275_7273// "burs"}