87 lines
3.1 KiB
Rust
87 lines
3.1 KiB
Rust
//! Read-aloud bookkeeping, voice-agnostic: the cache key of a synthesized sentence, and the timeline that says which
|
|
//! sentence is being heard at a given moment (for the highlight), with pauses between sentences, after paragraphs
|
|
//! and after chapter titles. The voice itself runs in the app.
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use sha2::{Digest, Sha256};
|
|
|
|
/// Pauses inserted after a segment, by what ends there.
|
|
pub const PAUSE_SENTENCE_MS: u32 = 120;
|
|
pub const PAUSE_PARAGRAPH_MS: u32 = 600;
|
|
pub const PAUSE_TITLE_MS: u32 = 900;
|
|
|
|
/// The cache key of one synthesized sentence: everything that changes the audio is in it (the voice and its file
|
|
/// hash, the synthesis speed, the language, the exact text). Hex, so it is also a safe file name.
|
|
pub fn cache_key(voice: &str, voice_sha256: &str, speed: f32, lang: &str, text: &str) -> String {
|
|
let mut h = Sha256::new();
|
|
for part in [voice, voice_sha256, &format!("{speed:.3}"), lang, text] {
|
|
h.update((part.len() as u64).to_le_bytes());
|
|
h.update(part.as_bytes());
|
|
}
|
|
h.finalize().iter().map(|b| format!("{b:02x}")).collect()
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum Ends {
|
|
Sentence,
|
|
Paragraph,
|
|
Title,
|
|
}
|
|
|
|
/// One spoken segment: its place in the book (chapter and UTF-16 range, opaque here), how long its audio is, what it
|
|
/// ends.
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
pub struct Segment {
|
|
pub chapter: usize,
|
|
pub start: usize,
|
|
pub end: usize,
|
|
pub audio_ms: u32,
|
|
pub ends: Ends,
|
|
}
|
|
|
|
/// Segments laid end to end with their pauses.
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
pub struct Timeline {
|
|
/// (start_ms, segment index), increasing.
|
|
starts: Vec<(u64, usize)>,
|
|
pub segments: Vec<Segment>,
|
|
pub total_ms: u64,
|
|
}
|
|
|
|
impl Timeline {
|
|
pub fn new(segments: Vec<Segment>) -> Timeline {
|
|
let mut at = 0u64;
|
|
let mut starts = Vec::with_capacity(segments.len());
|
|
for (i, s) in segments.iter().enumerate() {
|
|
starts.push((at, i));
|
|
at += u64::from(s.audio_ms)
|
|
+ u64::from(match s.ends {
|
|
Ends::Sentence => PAUSE_SENTENCE_MS,
|
|
Ends::Paragraph => PAUSE_PARAGRAPH_MS,
|
|
Ends::Title => PAUSE_TITLE_MS,
|
|
});
|
|
}
|
|
Timeline { starts, segments, total_ms: at }
|
|
}
|
|
|
|
/// The segment being heard at `ms` (during its pause, still that segment); `None` past the end.
|
|
pub fn at(&self, ms: u64) -> Option<usize> {
|
|
if ms >= self.total_ms {
|
|
return None;
|
|
}
|
|
let i = self.starts.partition_point(|&(s, _)| s <= ms);
|
|
i.checked_sub(1).map(|k| self.starts[k].1)
|
|
}
|
|
|
|
/// Where a segment's audio starts.
|
|
pub fn start_of(&self, segment: usize) -> Option<u64> {
|
|
self.starts.get(segment).map(|&(s, _)| s)
|
|
}
|
|
|
|
/// Whether `ms` falls in a pause rather than in speech.
|
|
pub fn in_pause(&self, ms: u64) -> bool {
|
|
self.at(ms).is_some_and(|i| ms >= self.starts[i].0 + u64::from(self.segments[i].audio_ms))
|
|
}
|
|
}
|