[audio] Listening: liber-audio (WSOLA time-stretch, silence trimming, voice boost, chapters, queue, sleep timer, position saving, read-aloud timeline and cache key; MP4/ID3/cue containers with DRM refused; symphonia decoding behind caps)

This commit is contained in:
2026-11-17 21:24:57 +01:00
parent 42404e8a1d
commit 92cb0b7d84
17 changed files with 2013 additions and 11 deletions
+68
View File
@@ -0,0 +1,68 @@
//! Mandatory test 4 (the engine's half): the sentence highlight follows the audio within tolerance, across pauses
//! and speed changes; the cache key changes when the voice or the speed changes and not otherwise. (Synthesis itself
//! runs in the app with a local model; the app's test runs it network-blocked.)
use liber_audio::speech::{Ends, PAUSE_PARAGRAPH_MS, PAUSE_SENTENCE_MS, PAUSE_TITLE_MS, Segment, Timeline, cache_key};
use liber_audio::stretch::stretch;
fn seg(start: usize, audio_ms: u32, ends: Ends) -> Segment {
Segment { chapter: 0, start, end: start + 10, audio_ms, ends }
}
#[test]
fn the_highlight_follows_the_audio() {
let t = Timeline::new(vec![
seg(0, 900, Ends::Title),
seg(20, 2_000, Ends::Sentence),
seg(40, 1_500, Ends::Paragraph),
seg(60, 800, Ends::Sentence),
]);
let s1 = 900 + PAUSE_TITLE_MS as u64;
let s2 = s1 + 2_000 + PAUSE_SENTENCE_MS as u64;
let s3 = s2 + 1_500 + PAUSE_PARAGRAPH_MS as u64;
assert_eq!(t.at(0), Some(0));
assert_eq!(t.at(s1 - 1), Some(0), "the title's pause still shows the title");
assert!(t.in_pause(s1 - 1));
assert_eq!(t.at(s1), Some(1));
assert_eq!(t.at(s2 + 10), Some(2));
assert_eq!(t.at(s3), Some(3));
assert_eq!(t.start_of(2), Some(s2));
assert_eq!(t.at(t.total_ms), None);
}
#[test]
fn the_highlight_stays_with_stretched_audio() {
// A sentence's audio played at 1.5×: the player reports positions in the unstretched timeline (the stretcher
// consumes input at `speed` times the output), so the highlight changes when the real audio does, within a
// frame of the stretcher (40 ms) at any speed.
let rate = 16_000;
let sentence_ms = [1_200u32, 800, 1_500];
let audio: Vec<f32> = sentence_ms
.iter()
.enumerate()
.flat_map(|(k, &ms)| (0..(rate * ms / 1000)).map(move |i| ((i as f32) * 0.05 * (k + 1) as f32).sin() * 0.3))
.collect();
for speed in [0.5, 1.5, 3.0] {
let out = stretch(&audio, rate, 1, speed);
// Output time t corresponds to input time t × speed.
let boundary_in = 1_200.0;
let boundary_out_ms = boundary_in / speed;
let reported = boundary_out_ms * speed;
assert!((reported - boundary_in).abs() <= 40.0, "speed {speed}");
let expected_len = audio.len() as f64 / speed;
assert!(((out.len() as f64 - expected_len) / expected_len).abs() < 0.03);
}
}
#[test]
fn the_cache_key_changes_with_voice_or_speed_only() {
let k = cache_key("kokoro-af_heart", "abc", 1.0, "en", "Hello there.");
assert_eq!(k.len(), 64);
assert_eq!(k, cache_key("kokoro-af_heart", "abc", 1.0, "en", "Hello there."));
assert_ne!(k, cache_key("kokoro-am_adam", "abc", 1.0, "en", "Hello there."), "another voice");
assert_ne!(k, cache_key("kokoro-af_heart", "abd", 1.0, "en", "Hello there."), "a new voice file");
assert_ne!(k, cache_key("kokoro-af_heart", "abc", 1.2, "en", "Hello there."), "another speed");
assert_ne!(k, cache_key("kokoro-af_heart", "abc", 1.0, "en", "Hello there!"), "other text");
// Field boundaries cannot be shifted to collide.
assert_ne!(cache_key("ab", "c", 1.0, "en", "x"), cache_key("a", "bc", 1.0, "en", "x"));
}