Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(verify): epoch-anchored audio keeps its head silence

Richie McIlroy9e9d9792ad64
diff --git a/crates/verify/src/pipeline.rs b/crates/verify/src/pipeline.rs
index 2d731c471..0426ab339 100644
--- a/crates/verify/src/pipeline.rs
+++ b/crates/verify/src/pipeline.rs
@@ -832,3 +832,61 @@ pub async fn record_instant(spec: &InstantSpec, dir: &Path) -> anyhow::Result<()
)?;
Ok(())
}
+
+/// Records one epoch-anchored audio track the way the product records
+/// loopback system audio (`AudioAnchor::PipelineEpoch`, no start gate):
+/// the first captured frame is stamped `head` seconds after the pipeline
+/// epoch and a continuous 1 kHz tone follows for `tone` seconds, in 20 ms
+/// chunks delivered in real time. Returns the file and the track start the
+/// pipeline reports, in seconds after the epoch.
+pub async fn record_epoch_audio(
+ dir: &Path,
+ head: f64,
+ tone: f64,
+) -> anyhow::Result<(std::path::PathBuf, f64)> {
+ let stereo_info = AudioInfo::new(Sample::F32(Type::Packed), AUDIO_RATE, 2)
+ .map_err(|e| anyhow!("audio info: {e:?}"))?;
+ let samples = (tone * f64::from(AUDIO_RATE)) as usize;
+ let track: Vec<f32> = (0..samples)
+ .map(|n| {
+ let t = n as f64 / f64::from(AUDIO_RATE);
+ (0.5 * (2.0 * std::f64::consts::PI * 1000.0 * t).sin()) as f32
+ })
+ .collect();
+ let path = dir.join("system_audio.ogg");
+ let timestamps = Timestamps::now();
+ let (mut tx, rx) = futures::channel::mpsc::channel::<AudioFrame>(32);
+ let pipeline = OutputPipeline::builder(path.clone())
+ .with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(stereo_info, rx))
+ .with_timestamps(timestamps)
+ .with_audio_anchor(cap_recording::AudioAnchor::PipelineEpoch)
+ .build::<OggMuxer>(())
+ .await
+ .map_err(|e| anyhow!("epoch audio pipeline: {e}"))?;
+ {
+ use futures::SinkExt;
+ let chunks = (tone / AUDIO_CHUNK_SECS).round() as usize;
+ for k in 0..chunks {
+ let t = k as f64 * AUDIO_CHUNK_SECS;
+ let at = timestamps.instant() + Duration::from_secs_f64(head + t);
+ tokio::time::sleep_until(at.into()).await;
+ let frame =
+ AudioFrame::new(audio_chunk(&track, t, &stereo_info), Timestamp::Instant(at));
+ tx.send(frame)
+ .await
+ .map_err(|e| anyhow!("send audio: {e}"))?;
+ }
+ }
+ tokio::time::sleep(Duration::from_millis(300)).await;
+ let finished = pipeline
+ .stop()
+ .await
+ .map_err(|e| anyhow!("epoch audio stop: {e}"))?;
+ drop(tx);
+ Ok((
+ path,
+ finished
+ .first_timestamp
+ .signed_duration_since_secs(timestamps),
+ ))
+}
diff --git a/crates/verify/tests/export.rs b/crates/verify/tests/export.rs
index d3de58653..c05bec974 100644
--- a/crates/verify/tests/export.rs
+++ b/crates/verify/tests/export.rs
@@ -1292,3 +1292,67 @@ fn recorded_studio_audio() {
}
common::finish(case);
}
+
+/// Epoch-anchored audio (the product's loopback system audio): the track's
+/// time 0 is the pipeline epoch, so silence must stand in for the time
+/// between the epoch and the first captured frame, or every sample plays
+/// early by that much. A head under the recorder's 70 ms gap threshold is
+/// covered only by that head silence; a longer one is also caught by gap
+/// filling. Recorded fresh every run (no fixture cache).
+#[test]
+fn recorded_epoch_audio_head() {
+ use cap_verify::media;
+ const TOLERANCE_SECS: f64 = 0.005;
+ let rt = common::runtime();
+ let mut failures = Vec::new();
+ for head in [0.04, 0.3] {
+ let id = format!("export/recorded-epoch-audio/head-{:.0}ms", head * 1000.0);
+ if !report::selected(&id) {
+ continue;
+ }
+ let mut case = Case::new(&id);
+ let dir = common::scratch_dir(&id);
+ match rt.block_on(cap_verify::pipeline::record_epoch_audio(
+ dir.path(),
+ head,
+ 0.6,
+ )) {
+ Err(e) => case.problem("error", format!("record: {e}")),
+ Ok((path, track_start)) => match media::decode_audio(&path, Some(48_000)) {
+ Err(e) => case.problem("error", format!("decode: {e:?}")),
+ Ok(decoded) => {
+ let mono = decoded.mono();
+ match mono.iter().position(|v| v.abs() > 0.05) {
+ None => case.problem("audio-missing", "the recorded tone is silent"),
+ Some(index) => {
+ let heard = track_start + decoded.start_secs + index as f64 / 48_000.0;
+ let error = heard - head;
+ if error.abs() > TOLERANCE_SECS {
+ case.problem(
+ "sync",
+ format!(
+ "audio captured {:.1} ms after the epoch is heard at {:.1} ms on the track ({:+.1} ms; track start {:.1} ms, file start {:.1} ms)",
+ head * 1000.0,
+ heard * 1000.0,
+ error * 1000.0,
+ track_start * 1000.0,
+ decoded.start_secs * 1000.0
+ ),
+ );
+ }
+ case.note(format!(
+ "tone heard at {:.2} ms (captured at {:.1} ms)",
+ heard * 1000.0,
+ head * 1000.0
+ ));
+ }
+ }
+ }
+ },
+ }
+ if let Err(message) = report::settle(case) {
+ failures.push(message);
+ }
+ }
+ common::assert_passed(failures);
+}