154 lines
5.2 KiB
Rust
154 lines
5.2 KiB
Rust
//! Silence trimming: pauses longer than `long` are shortened to `keep`; any frame louder than the threshold is never
|
|
//! removed. Streaming: the first `keep` of a pause goes out at once, the next part is held, and the held part is
|
|
//! dropped only once the pause has proved longer than `long`. A map turns output time back into input time, so a
|
|
//! position is always in the book's own seconds.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct TrimConfig {
|
|
/// A 10 ms frame quieter than this (RMS, dBFS) is silence.
|
|
pub threshold_db: f32,
|
|
/// Pauses at least this long are shortened…
|
|
pub long_ms: u32,
|
|
/// …to this.
|
|
pub keep_ms: u32,
|
|
}
|
|
|
|
impl Default for TrimConfig {
|
|
fn default() -> Self {
|
|
TrimConfig { threshold_db: -45.0, long_ms: 300, keep_ms: 150 }
|
|
}
|
|
}
|
|
|
|
pub struct Trimmer {
|
|
cfg: TrimConfig,
|
|
channels: usize,
|
|
frame: usize,
|
|
threshold: f32,
|
|
/// Input not yet a whole frame.
|
|
pending: Vec<f32>,
|
|
/// Frames of the current pause held back (past `keep`, not yet `long`).
|
|
held: Vec<f32>,
|
|
/// Silent frames in the current pause so far.
|
|
run: usize,
|
|
keep_frames: usize,
|
|
long_frames: usize,
|
|
out: Vec<f32>,
|
|
/// Input and output frame counters, and (output, input) points where the two diverge.
|
|
in_frames: u64,
|
|
out_frames: u64,
|
|
map: Vec<(u64, u64)>,
|
|
pub enabled: bool,
|
|
}
|
|
|
|
impl Trimmer {
|
|
pub fn new(rate: u32, channels: u16, cfg: TrimConfig) -> Trimmer {
|
|
let frame = (rate as usize / 100).max(1);
|
|
Trimmer {
|
|
cfg,
|
|
channels: channels.max(1) as usize,
|
|
frame,
|
|
threshold: 10f32.powf(cfg.threshold_db / 20.0),
|
|
pending: Vec::new(),
|
|
held: Vec::new(),
|
|
run: 0,
|
|
keep_frames: (cfg.keep_ms as usize).div_ceil(10),
|
|
long_frames: (cfg.long_ms as usize).div_ceil(10),
|
|
out: Vec::new(),
|
|
in_frames: 0,
|
|
out_frames: 0,
|
|
map: vec![(0, 0)],
|
|
enabled: true,
|
|
}
|
|
}
|
|
|
|
pub fn config(&self) -> TrimConfig {
|
|
self.cfg
|
|
}
|
|
|
|
fn emit(&mut self, block: &[f32]) {
|
|
self.out.extend_from_slice(block);
|
|
self.out_frames += (block.len() / self.channels) as u64;
|
|
}
|
|
|
|
fn release_held(&mut self) {
|
|
let held = std::mem::take(&mut self.held);
|
|
self.emit(&held);
|
|
}
|
|
|
|
fn step(&mut self, frame: Vec<f32>) {
|
|
let n = frame.len() / self.channels;
|
|
if !self.enabled {
|
|
self.in_frames += n as u64;
|
|
self.emit(&frame);
|
|
return;
|
|
}
|
|
let rms = (frame.iter().map(|x| x * x).sum::<f32>() / frame.len().max(1) as f32).sqrt();
|
|
let silent = rms < self.threshold;
|
|
if !silent {
|
|
// Speech: whatever was held belonged to a short pause; it goes out too.
|
|
self.release_held();
|
|
self.run = 0;
|
|
self.in_frames += n as u64;
|
|
self.emit(&frame);
|
|
return;
|
|
}
|
|
self.run += 1;
|
|
self.in_frames += n as u64;
|
|
if self.run <= self.keep_frames {
|
|
self.emit(&frame);
|
|
} else if self.run < self.long_frames {
|
|
self.held.extend_from_slice(&frame);
|
|
} else {
|
|
// A long pause: the held part and everything after it until speech is dropped.
|
|
self.held.clear();
|
|
// One map point per dropped pause: output stands still while input moves on.
|
|
match self.map.last_mut() {
|
|
Some(last) if last.0 == self.out_frames => last.1 = self.in_frames,
|
|
_ => self.map.push((self.out_frames, self.in_frames)),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Adds interleaved samples.
|
|
pub fn push(&mut self, samples: &[f32]) {
|
|
self.pending.extend_from_slice(samples);
|
|
let size = self.frame * self.channels;
|
|
while self.pending.len() >= size {
|
|
let rest = self.pending.split_off(size);
|
|
let frame = std::mem::replace(&mut self.pending, rest);
|
|
self.step(frame);
|
|
}
|
|
}
|
|
|
|
/// End of input: a held part of a short pause and the last partial frame go out.
|
|
pub fn finish(&mut self) {
|
|
self.release_held();
|
|
let rest = std::mem::take(&mut self.pending);
|
|
self.in_frames += (rest.len() / self.channels) as u64;
|
|
self.emit(&rest);
|
|
}
|
|
|
|
pub fn pull(&mut self) -> Vec<f32> {
|
|
std::mem::take(&mut self.out)
|
|
}
|
|
|
|
/// The input frame an output frame came from (to report positions in the book's own time).
|
|
pub fn input_frame(&self, out_frame: u64) -> u64 {
|
|
let i = self.map.partition_point(|&(o, _)| o <= out_frame).saturating_sub(1);
|
|
let (o, inp) = self.map[i];
|
|
// Between map points output and input run together, except across a dropped pause where the last point
|
|
// already accounts for it.
|
|
let later = self.map.get(i + 1).map(|&(_, ni)| ni);
|
|
let guess = inp + (out_frame - o);
|
|
later.map_or(guess, |ni| guess.min(ni))
|
|
}
|
|
}
|
|
|
|
/// The whole of `samples`, trimmed (tests and short clips).
|
|
pub fn trim(samples: &[f32], rate: u32, channels: u16, cfg: TrimConfig) -> Vec<f32> {
|
|
let mut t = Trimmer::new(rate, channels, cfg);
|
|
t.push(samples);
|
|
t.finish();
|
|
t.pull()
|
|
}
|