//! A recording made from nothing: a mono 16-bit PCM WAV of soft hums, so a //! synthetic meeting or voice note has bytes a phone fetches and plays without //! one real voice in the tree. WAV because it needs no encoder — a 44-byte //! header and the samples — and because every player the apps use (AVPlayer, //! Android's MediaPlayer) plays linear PCM at any sample rate. //! //! Each [`Hum`] is one line of "speech": a tone at the speaker's pitch that //! swells and falls four times a second like syllables, with silence between //! lines — so seeking to a transcript line lands audibly on its start. /// Samples a second. Low on purpose: a forty-second meeting is 640 KB. pub const RATE: u32 = 8_000; /// One stretch of sound: from and to (milliseconds into the recording) and /// the pitch it hums at. #[derive(Debug, Clone, Copy)] pub struct Hum { pub from_ms: i64, pub to_ms: i64, pub hz: f64, } fn frame_of(ms: i64) -> usize { usize::try_from(ms.max(0)).unwrap_or_default() * RATE as usize / 1000 } /// The samples of a recording `length_ms` long: silence, and each hum. pub fn samples(length_ms: i64, hums: &[Hum]) -> Vec { let frames = frame_of(length_ms); let mut out = vec![0i16; frames]; let tau = std::f64::consts::TAU; let fade = f64::from(RATE) * 0.03; for hum in hums { let from = frame_of(hum.from_ms).min(frames); let to = frame_of(hum.to_ms).min(frames); for (i, sample) in out.iter_mut().enumerate().take(to).skip(from) { let t = (i - from) as f64 / f64::from(RATE); // A 30 ms fade at each end, so a line starts and stops without a click. let edge = ((i - from) as f64 / fade).min((to - i) as f64 / fade).min(1.0); let syllables = 0.35 + 0.65 * (tau * 2.0 * t).sin().abs(); let tone = 0.7 * (tau * hum.hz * t).sin() + 0.3 * (tau * 2.0 * hum.hz * t).sin(); *sample = (tone * syllables * edge * 0.3 * f64::from(i16::MAX)) as i16; } } out } /// The samples as a WAV file. pub fn wav(samples: &[i16]) -> Vec { let data = u32::try_from(samples.len() * 2).expect("a short recording"); let mut out = Vec::with_capacity(44 + samples.len() * 2); out.extend_from_slice(b"RIFF"); out.extend_from_slice(&(36 + data).to_le_bytes()); out.extend_from_slice(b"WAVEfmt "); out.extend_from_slice(&16u32.to_le_bytes()); out.extend_from_slice(&1u16.to_le_bytes()); // linear PCM out.extend_from_slice(&1u16.to_le_bytes()); // mono out.extend_from_slice(&RATE.to_le_bytes()); out.extend_from_slice(&(RATE * 2).to_le_bytes()); out.extend_from_slice(&2u16.to_le_bytes()); out.extend_from_slice(&16u16.to_le_bytes()); out.extend_from_slice(b"data"); out.extend_from_slice(&data.to_le_bytes()); for sample in samples { out.extend_from_slice(&sample.to_le_bytes()); } out } /// How long a WAV this module wrote plays, read back off its header. #[cfg(test)] pub fn wav_duration_ms(bytes: &[u8]) -> Option { if bytes.len() < 44 || &bytes[..4] != b"RIFF" || &bytes[8..12] != b"WAVE" { return None; } let rate = u32::from_le_bytes(bytes[24..28].try_into().ok()?); let per_second = u32::from_le_bytes(bytes[28..32].try_into().ok()?); let data = u32::from_le_bytes(bytes[40..44].try_into().ok()?); (rate > 0 && per_second > 0).then(|| i64::from(data) * 1000 / i64::from(per_second)) } /// Sixty-four bars in 0..1 — the loudness of each sixty-fourth, as a voice /// note draws its waveform. pub fn waveform(samples: &[i16]) -> Vec { let width = samples.len().div_ceil(64).max(1); let peaks: Vec = samples .chunks(width) .map(|chunk| chunk.iter().map(|s| f64::from(s.unsigned_abs())).fold(0.0, f64::max)) .collect(); let loudest = peaks.iter().copied().fold(1.0, f64::max); let mut bars: Vec = peaks.iter().map(|peak| ((peak / loudest) * 100.0).round() / 100.0).collect(); bars.resize(64, 0.0); bars }