Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve(audio): skip single-source transcription normalization

vedanthb49cbbf85af7
diff --git a/crates/audio/examples/transcription-assembly-benchmark.rs b/crates/audio/examples/transcription-assembly-benchmark.rs
index 04beef561..0050e351c 100644
--- a/crates/audio/examples/transcription-assembly-benchmark.rs
+++ b/crates/audio/examples/transcription-assembly-benchmark.rs
@@ -5,11 +5,22 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut args = std::env::args().skip(1);
let seconds: usize = args
.next()
- .ok_or_else(|| io::Error::other("Expected duration in seconds and mic or mic-system"))?
+ .ok_or_else(|| {
+ io::Error::other("Expected duration in seconds and mic, system or mic-system")
+ })?
.parse()?;
let mode = args.next().unwrap_or_else(|| "mic".to_string());
- if !matches!(mode.as_str(), "mic" | "mic-system") {
- return Err(io::Error::other("Expected mic or mic-system").into());
+ let repetitions: usize = args.next().unwrap_or_else(|| "25".into()).parse()?;
+ if seconds == 0
+ || seconds > 3600
+ || !matches!(mode.as_str(), "mic" | "system" | "mic-system")
+ || repetitions == 0
+ || args.next().is_some()
+ {
+ return Err(io::Error::other(
+ "Expected seconds (1..=3600), mic/system/mic-system, repetitions (>0)",
+ )
+ .into());
}
let rate = 48_000;
let frames = seconds * rate;
@@ -19,36 +30,50 @@ fn main() -> Result<(), Box<dyn Error>> {
.collect()
};
let mut sources = Vec::new();
- if mode == "mic-system" {
+ if mode == "mic-system" || mode == "system" {
sources.push(TranscriptionAudioSource {
samples: samples(2),
channels: 2,
offset_secs: 0.0,
});
}
- sources.push(TranscriptionAudioSource {
- samples: samples(1),
- channels: 1,
- offset_secs: if mode == "mic-system" { -0.125 } else { 0.0 },
- });
+ if mode != "system" {
+ sources.push(TranscriptionAudioSource {
+ samples: samples(1),
+ channels: 1,
+ offset_secs: if mode == "mic-system" { -0.125 } else { 0.0 },
+ });
+ }
let take = TranscriptionAudioTake {
display_duration_secs: seconds as f64,
sources,
};
- let mut output = Vec::new();
- let started = Instant::now();
- append_transcription_audio(black_box(&mut output), black_box(&take), rate as u32)
- .map_err(io::Error::other)?;
- let elapsed = started.elapsed();
- assert_eq!(output.len(), frames);
- let checksum = black_box(&output).iter().fold(0_u64, |hash, sample| {
- hash.wrapping_mul(31)
- .wrapping_add(u64::from(sample.to_bits()))
- });
- println!(
- "synthetic,{mode},{seconds}s,assembly_ms={:.3},samples={},checksum={checksum:016x}",
- elapsed.as_secs_f64() * 1000.0,
- output.len(),
- );
+ println!("mode,seconds,sample,assembly_ms,frames,fingerprint");
+ let mut expected_checksum = None;
+ for sample in 0..repetitions + 3 {
+ let mut output = Vec::new();
+ let started = Instant::now();
+ append_transcription_audio(black_box(&mut output), black_box(&take), rate as u32)
+ .map_err(io::Error::other)?;
+ let elapsed = started.elapsed();
+ assert_eq!(output.len(), frames);
+ let checksum = black_box(&output).iter().fold(0_u64, |hash, sample| {
+ hash.wrapping_mul(31)
+ .wrapping_add(u64::from(sample.to_bits()))
+ });
+ if let Some(expected) = expected_checksum {
+ assert_eq!(checksum, expected);
+ } else {
+ expected_checksum = Some(checksum);
+ }
+ if sample >= 3 {
+ println!(
+ "{mode},{seconds},{},{:.6},{},{checksum:016x}",
+ sample - 3,
+ elapsed.as_secs_f64() * 1000.0,
+ output.len(),
+ );
+ }
+ }
Ok(())
}
diff --git a/crates/audio/src/transcription_timing.rs b/crates/audio/src/transcription_timing.rs
index 601a32573..f2552e0d2 100644
--- a/crates/audio/src/transcription_timing.rs
+++ b/crates/audio/src/transcription_timing.rs
@@ -105,7 +105,7 @@ pub fn append_transcription_audio(
let mut count = 0_u32;
let mut start = 0;
for (end, starting) in boundaries {
- if count > 0 {
+ if count > 1 {
for sample in &mut sums[start..end] {
*sample /= count as f32;
}
@@ -322,6 +322,47 @@ mod tests {
}
}
+ #[test]
+ fn single_source_intervals_preserve_special_samples_and_existing_output() {
+ let values = [
+ 0.0,
+ -0.0,
+ f32::from_bits(1),
+ -f32::from_bits(1),
+ f32::MIN_POSITIVE,
+ f32::MAX,
+ f32::INFINITY,
+ f32::NEG_INFINITY,
+ f32::from_bits(0x7fc0_1234),
+ f32::from_bits(0x7f80_1234),
+ ];
+ for channels in [1, 2, 6] {
+ let samples: Vec<_> = values.into_iter().cycle().take(20 * channels).collect();
+ for offset in [-3.0, -1.25, 0.0, 0.5, 2.0] {
+ let take = take(
+ 10.0,
+ vec![
+ source(samples.clone(), channels, offset),
+ source(samples.clone(), channels, -6.0),
+ ],
+ );
+ let mut actual = vec![-0.0, f32::from_bits(0x7fc0_4321)];
+ let prefix = bits(&actual);
+ append_transcription_audio(&mut actual, &take, 4).unwrap();
+ assert_eq!(bits(&actual[..2]), prefix);
+ let expected = reference(&take, 4);
+ assert_eq!(actual.len() - 2, expected.len());
+ for (actual, expected) in actual[2..].iter().zip(expected) {
+ if expected.is_nan() {
+ assert!(actual.is_nan());
+ } else {
+ assert_eq!(actual.to_bits(), expected.to_bits());
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn adjacent_overlapping_and_disjoint_source_boundaries_keep_exact_averages() {
let take = take(