use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use regex::Regex;
use std::sync::OnceLock;
use crate::error::Error;
use crate::media_helper::probe_video;
#[derive(Debug, Clone)]
pub struct PcmData {
pub samples: Vec<i16>,
pub sample_rate: u32,
pub channels: usize,
}
impl PcmData {
pub fn duration_seconds(&self) -> f64 {
if self.sample_rate == 0 || self.channels == 0 {
return 0.0;
}
self.samples.len() as f64 / (self.sample_rate as f64 * self.channels as f64)
}
}
#[derive(Debug, Clone)]
pub struct Probe {
pub format: String,
pub sample_rate: u32,
pub channels: usize,
pub duration_seconds: f64,
pub is_video: bool,
}
pub const VIDEO_EXT: &[&str] = &["mp4", "mov", "m4v", "mkv", "webm", "avi"];
pub const AUDIO_EXT: &[&str] = &["ogg", "mp3", "wav", "flac"];
static CONTAINERS: OnceLock<(Vec<String>, Vec<String>)> = OnceLock::new();
pub fn publish_containers(audio: Vec<String>, video: Vec<String>) {
let _ = CONTAINERS.set((audio, video));
}
fn audio_ext() -> Vec<String> {
CONTAINERS.get().map_or_else(
|| AUDIO_EXT.iter().map(|s| (*s).to_string()).collect(),
|(a, _)| a.clone(),
)
}
fn video_ext() -> Vec<String> {
CONTAINERS.get().map_or_else(
|| VIDEO_EXT.iter().map(|s| (*s).to_string()).collect(),
|(_, v)| v.clone(),
)
}
pub fn is_video_path(path: &Path) -> bool {
ext(path)
.map(|e| video_ext().iter().any(|v| v == &e))
.unwrap_or(false)
}
pub fn decode_file(path: &Path) -> Result<PcmData, Error> {
if !path.exists() {
return Err(Error::InputNotFound {
path: path.display().to_string(),
});
}
if is_video_path(path) {
return Err(Error::HelperMissing);
}
if let Some(e) = ext(path) {
if !audio_ext().iter().any(|a| a == &e) {
return Err(Error::UnsupportedFormat { format: e });
}
}
decode_native(path)
}
pub fn decode_file_with_format(path: &Path, forced: &str) -> Result<PcmData, Error> {
let mut file = std::fs::File::open(path).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
Error::InputNotFound {
path: path.display().to_string(),
}
} else {
Error::Io(e)
}
})?;
let mut header = [0u8; HEADER_PEEK];
let read = read_up_to(&mut file, &mut header)?;
let forced = validate_forced(&header[..read], Some(forced))?;
file.seek(std::io::SeekFrom::Start(0))?;
decode_cursor(file, forced.as_deref())
}
fn read_up_to<R: Read>(reader: &mut R, buf: &mut [u8]) -> Result<usize, Error> {
let mut filled = 0;
while filled < buf.len() {
match reader.read(&mut buf[filled..])? {
0 => break,
n => filled += n,
}
}
Ok(filled)
}
const HEADER_PEEK: usize = 12;
fn validate_forced(header: &[u8], forced: Option<&str>) -> Result<Option<String>, Error> {
if looks_video(header) {
return Err(Error::HelperMissing);
}
let forced = forced.map(normalize_format);
if let Some(fmt) = forced.as_deref() {
if video_ext().iter().any(|v| v == fmt) {
return Err(Error::HelperMissing);
}
if !audio_ext().iter().any(|a| a == fmt) {
return Err(Error::UnsupportedFormat {
format: fmt.to_string(),
});
}
if let Some(actual) = detect_container(header) {
if actual != fmt {
return Err(Error::FormatMismatch {
format: fmt.to_string(),
});
}
}
}
Ok(forced)
}
pub fn decode_bytes(bytes: &[u8], forced: Option<&str>) -> Result<PcmData, Error> {
if bytes.is_empty() {
return Err(Error::NoInput);
}
let head = &bytes[..bytes.len().min(HEADER_PEEK)];
let forced = validate_forced(head, forced)?;
decode_cursor(std::io::Cursor::new(bytes.to_vec()), forced.as_deref())
}
fn normalize_format(fmt: &str) -> String {
let f = fmt.trim().to_ascii_lowercase();
match f.as_str() {
"wave" => "wav".to_string(),
"oga" | "opus" | "vorbis" => "ogg".to_string(),
"mpeg3" | "mpga" => "mp3".to_string(),
_ => f,
}
}
fn detect_container(bytes: &[u8]) -> Option<&'static str> {
if bytes.len() >= 12 && &bytes[0..4] == b"RIFF" && &bytes[8..12] == b"WAVE" {
return Some("wav");
}
if bytes.starts_with(b"fLaC") {
return Some("flac");
}
if bytes.starts_with(b"OggS") {
return Some("ogg");
}
if bytes.starts_with(b"ID3") {
return Some("mp3");
}
if bytes.len() >= 2 && bytes[0] == 0xFF && (bytes[1] & 0xE0) == 0xE0 {
return Some("mp3");
}
None
}
pub fn inspect_file(path: &Path, helper: Option<&str>) -> Result<Probe, Error> {
if !path.exists() {
return Err(Error::InputNotFound {
path: path.display().to_string(),
});
}
if is_video_path(path) {
let format = ext(path).unwrap_or_else(|| "video".into());
let (duration_seconds, sample_rate, channels) =
probe_video(helper.unwrap_or_default(), path, None)?;
return Ok(Probe {
format,
sample_rate,
channels,
duration_seconds,
is_video: true,
});
}
match decode_native(path) {
Ok(pcm) => Ok(Probe {
format: detected_format(path),
sample_rate: pcm.sample_rate,
channels: pcm.channels,
duration_seconds: pcm.duration_seconds(),
is_video: false,
}),
Err(err) if err.is_decode_fault() => {
let (duration_seconds, sample_rate, channels) =
probe_video(helper.unwrap_or_default(), path, None).map_err(|_| err)?;
Ok(Probe {
format: detected_format(path),
sample_rate,
channels,
duration_seconds,
is_video: false,
})
}
Err(err) => Err(err),
}
}
fn detected_format(path: &Path) -> String {
let sniffed = std::fs::File::open(path).ok().and_then(|mut f| {
let mut head = [0u8; 16];
let read = std::io::Read::read(&mut f, &mut head).ok()?;
detect_container(&head[..read]).map(str::to_string)
});
sniffed
.or_else(|| ext(path))
.unwrap_or_else(|| "audio".into())
}
fn decode_native(path: &Path) -> Result<PcmData, Error> {
let file = std::fs::File::open(path).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
Error::InputNotFound {
path: path.display().to_string(),
}
} else {
Error::Io(e)
}
})?;
decode_cursor(file, ext(path).as_deref())
}
fn decode_cursor<R: Read + Seek + Send + Sync + 'static>(
mut reader: R,
hint_ext: Option<&str>,
) -> Result<PcmData, Error> {
let mut header = [0u8; 12];
let _ = reader.read(&mut header);
let _ = reader.seek(SeekFrom::Start(0));
if header.len() >= 12 && &header[0..4] == b"RIFF" && &header[8..12] == b"WAVE" {
return decode_wav_reader(reader);
}
decode_symphonia(reader, hint_ext)
}
fn collect_checked<I>(iter: I) -> Result<Vec<i16>, Error>
where
I: Iterator<Item = Result<i16, hound::Error>>,
{
let mut out = Vec::with_capacity(iter.size_hint().0);
for (i, sample) in iter.enumerate() {
if i % 65_536 == 0 {
if crate::signal::is_shutdown_requested() {
return Err(crate::signal::shutdown_error().unwrap_or(Error::Interrupted));
}
if crate::signal::deadline_exceeded() {
return Err(Error::Timeout);
}
if crate::gpu::memory_cap_exceeded() {
return Err(Error::MemoryCap);
}
}
out.push(sample.map_err(|e| Error::AudioDecode(anyhow::anyhow!(e)))?);
}
Ok(out)
}
fn decode_wav_reader<R: Read>(reader: R) -> Result<PcmData, Error> {
let mut wav =
hound::WavReader::new(reader).map_err(|e| Error::AudioDecode(anyhow::anyhow!(e)))?;
let spec = wav.spec();
let samples: Vec<i16> = match spec.sample_format {
hound::SampleFormat::Float => {
collect_checked(wav.samples::<f32>().map(|s| s.map(float_to_i16)))?
}
hound::SampleFormat::Int => {
let bits = spec.bits_per_sample;
collect_checked(
wav.samples::<i32>()
.map(|s| s.map(|v| scale_int_to_i16(v, bits))),
)?
}
};
Ok(PcmData {
samples,
sample_rate: spec.sample_rate,
channels: spec.channels as usize,
})
}
pub fn probe_duration(path: &Path) -> Option<f64> {
use symphonia::core::io::MediaSourceStream;
use symphonia::core::probe::Hint;
let file = std::fs::File::open(path).ok()?;
let mss = MediaSourceStream::new(Box::new(file), Default::default());
let mut hint = Hint::new();
if let Some(e) = ext(path) {
hint.with_extension(&e);
}
duration_from_probe(mss, &hint)
}
const PROBE_PREFIX: usize = 64 * 1024;
pub fn probe_prefix() -> usize {
PROBE_PREFIX
}
pub fn probe_duration_bytes(bytes: &[u8], forced: Option<&str>) -> Option<f64> {
use symphonia::core::io::MediaSourceStream;
use symphonia::core::probe::Hint;
let head = bytes[..bytes.len().min(PROBE_PREFIX)].to_vec();
let mss = MediaSourceStream::new(Box::new(std::io::Cursor::new(head)), Default::default());
let mut hint = Hint::new();
if let Some(f) = forced {
hint.with_extension(f);
}
duration_from_probe(mss, &hint)
}
fn duration_from_probe(
mss: symphonia::core::io::MediaSourceStream,
hint: &symphonia::core::probe::Hint,
) -> Option<f64> {
use symphonia::core::formats::FormatOptions;
use symphonia::core::meta::MetadataOptions;
let probed = symphonia::default::get_probe()
.format(
hint,
mss,
&FormatOptions::default(),
&MetadataOptions::default(),
)
.ok()?;
let track = probed
.format
.tracks()
.iter()
.find(|t| t.codec_params.codec != symphonia::core::codecs::CODEC_TYPE_NULL)?;
let frames = track.codec_params.n_frames?;
let rate = track.codec_params.sample_rate? as f64;
if rate <= 0.0 {
return None;
}
Some(frames as f64 / rate)
}
fn decode_symphonia<R: Read + Seek + Send + Sync + 'static>(
reader: R,
hint_ext: Option<&str>,
) -> Result<PcmData, Error> {
use symphonia::core::audio::AudioBufferRef;
use symphonia::core::codecs::DecoderOptions;
use symphonia::core::formats::FormatOptions;
use symphonia::core::io::MediaSourceStream;
use symphonia::core::meta::MetadataOptions;
use symphonia::core::probe::Hint;
let mss = MediaSourceStream::new(Box::new(ReadSeek(reader)), Default::default());
let mut hint = Hint::new();
if let Some(ext) = hint_ext {
hint.with_extension(ext);
}
let probed = symphonia::default::get_probe()
.format(
&hint,
mss,
&FormatOptions::default(),
&MetadataOptions::default(),
)
.map_err(|e| Error::AudioDecode(anyhow::anyhow!(e)))?;
let mut format = probed.format;
let track = format
.tracks()
.iter()
.find(|t| t.codec_params.codec != symphonia::core::codecs::CODEC_TYPE_NULL)
.ok_or_else(|| Error::AudioDecode(anyhow::anyhow!("no audio track")))?
.clone();
let mut decoder = symphonia::default::get_codecs()
.make(&track.codec_params, &DecoderOptions::default())
.map_err(|e| Error::AudioDecode(anyhow::anyhow!(e)))?;
let sample_rate = track.codec_params.sample_rate.unwrap_or(16_000);
let mut channels = track.codec_params.channels.map(|c| c.count()).unwrap_or(1);
let mut samples = Vec::new();
let mut packets = 0u32;
loop {
packets = packets.wrapping_add(1);
if packets.is_multiple_of(64) {
if crate::signal::is_shutdown_requested() {
return Err(
crate::signal::shutdown_error().unwrap_or(crate::error::Error::Interrupted)
);
}
if crate::signal::deadline_exceeded() {
return Err(crate::error::Error::Timeout);
}
if crate::gpu::memory_cap_exceeded() {
return Err(crate::error::Error::MemoryCap);
}
}
let packet = match format.next_packet() {
Ok(p) => p,
Err(symphonia::core::errors::Error::IoError(e))
if e.kind() == std::io::ErrorKind::UnexpectedEof =>
{
break;
}
Err(_) => break,
};
if packet.track_id() != track.id {
continue;
}
let Ok(decoded) = decoder.decode(&packet) else {
continue;
};
channels = match decoded {
AudioBufferRef::U8(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::U16(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::U24(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::U32(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::S8(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::S16(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::S24(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::S32(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::F32(buf) => interleave(buf.as_ref(), &mut samples),
AudioBufferRef::F64(buf) => interleave(buf.as_ref(), &mut samples),
};
}
if samples.is_empty() {
return Err(Error::AudioDecode(anyhow::anyhow!("empty decode")));
}
Ok(PcmData {
samples,
sample_rate,
channels: channels.max(1),
})
}
fn float_to_i16(value: f32) -> i16 {
(value.clamp(-1.0, 1.0) * i16::MAX as f32) as i16
}
fn scale_int_to_i16(value: i32, bits: u16) -> i16 {
match bits {
0..=15 => (value << (16 - bits.max(1))) as i16,
16 => value as i16,
_ => (value >> (bits - 16)) as i16,
}
}
fn interleave<S>(buf: &symphonia::core::audio::AudioBuffer<S>, out: &mut Vec<i16>) -> usize
where
S: symphonia::core::sample::Sample + symphonia::core::conv::IntoSample<i16> + Copy,
{
use symphonia::core::audio::Signal;
let channels = buf.spec().channels.count();
let frames = buf.frames();
out.reserve(frames * channels);
for i in 0..frames {
for ch in 0..channels {
out.push(buf.chan(ch)[i].into_sample());
}
}
channels
}
struct ReadSeek<R>(R);
impl<R: Read + Seek + Send + Sync> Read for ReadSeek<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.read(buf)
}
}
impl<R: Read + Seek + Send + Sync> Seek for ReadSeek<R> {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
self.0.seek(pos)
}
}
impl<R: Read + Seek + Send + Sync> symphonia::core::io::MediaSource for ReadSeek<R> {
fn is_seekable(&self) -> bool {
true
}
fn byte_len(&self) -> Option<u64> {
None
}
}
const I16_SCALE: f32 = 32_768.0;
pub const ENGINE_RATE: u32 = 16_000;
pub fn to_16k_mono(pcm: &PcmData) -> Result<Vec<f32>, Error> {
crate::gpu::ensure_headroom(pcm.samples.len() as u64 * 4)?;
let mono: Vec<f32> = if pcm.channels <= 1 {
pcm.samples.iter().map(|s| *s as f32 / I16_SCALE).collect()
} else {
pcm.samples
.chunks(pcm.channels)
.map(|c| c.iter().map(|s| *s as f32).sum::<f32>() / (c.len() as f32 * I16_SCALE))
.collect()
};
if pcm.sample_rate == ENGINE_RATE {
return Ok(mono);
}
if pcm.sample_rate == 0 {
return Err(Error::AudioDecode(anyhow::anyhow!("invalid sample rate")));
}
resample_linear(&mono, pcm.sample_rate, ENGINE_RATE)
}
const LOWPASS_TAPS: usize = 31;
fn lowpass(input: &[f32], from: u32, to: u32) -> Vec<f32> {
let cutoff = 0.45 * to as f64 / from as f64;
let half = (LOWPASS_TAPS / 2) as isize;
let mut taps = Vec::with_capacity(LOWPASS_TAPS);
for i in 0..LOWPASS_TAPS {
let n = i as isize - half;
let sinc = if n == 0 {
2.0 * cutoff
} else {
let x = std::f64::consts::PI * n as f64;
(2.0 * cutoff * x).sin() / x
};
let window =
0.5 - 0.5 * (2.0 * std::f64::consts::PI * i as f64 / (LOWPASS_TAPS - 1) as f64).cos();
taps.push((sinc * window) as f32);
}
let sum: f32 = taps.iter().sum();
if sum != 0.0 {
for t in &mut taps {
*t /= sum;
}
}
let mut out = Vec::with_capacity(input.len());
for i in 0..input.len() {
let mut acc = 0.0f32;
for (k, tap) in taps.iter().enumerate() {
let idx = (i as isize + k as isize - half).clamp(0, input.len() as isize - 1);
acc += input[idx as usize] * tap;
}
out.push(acc);
}
out
}
fn resample_linear(input: &[f32], from: u32, to: u32) -> Result<Vec<f32>, Error> {
if input.is_empty() {
return Ok(Vec::new());
}
let filtered;
let input = if to < from {
filtered = lowpass(input, from, to);
&filtered[..]
} else {
input
};
let ratio = to as f64 / from as f64;
let out_len = ((input.len() as f64) * ratio).round() as usize;
let mut out = Vec::with_capacity(out_len);
for i in 0..out_len {
let src = i as f64 / ratio;
let idx = src.floor() as usize;
let frac = (src - idx as f64) as f32;
let a = input[idx.min(input.len() - 1)];
let b = input[(idx + 1).min(input.len() - 1)];
out.push(a + (b - a) * frac);
}
Ok(out)
}
const VAD_RMS_MIN: f32 = 0.002;
const VAD_RMS_MAX: f32 = 0.10;
const VAD_FRAME_SAMPLES: usize = 480;
pub fn source_time(chunks: &[(usize, usize)], total_samples: usize, seconds: f64) -> f64 {
if chunks.is_empty() || !seconds.is_finite() || seconds <= 0.0 {
return seconds.max(0.0);
}
let target = (seconds * f64::from(ENGINE_RATE)).round() as usize;
let mut consumed = 0usize;
let mut last_end = 0usize;
for (start, end) in chunks {
let end = (*end).min(total_samples);
last_end = end;
let len = end.saturating_sub(*start);
if target < consumed + len {
let offset = target - consumed;
return (*start + offset) as f64 / f64::from(ENGINE_RATE);
}
consumed += len;
}
last_end as f64 / f64::from(ENGINE_RATE)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VadSettings {
pub frame_samples: usize,
pub rms_min: f32,
pub rms_max: f32,
}
impl Default for VadSettings {
fn default() -> Self {
Self {
frame_samples: VAD_FRAME_SAMPLES,
rms_min: VAD_RMS_MIN,
rms_max: VAD_RMS_MAX,
}
}
}
impl VadSettings {
pub fn from_ms(frame_ms: u32, rms_min: f32, rms_max: f32) -> Self {
let frame_samples = (u64::from(frame_ms) * u64::from(ENGINE_RATE) / 1000).max(1) as usize;
Self {
frame_samples,
rms_min,
rms_max,
}
}
fn floor(&self, threshold: f32) -> f32 {
let t = threshold.clamp(0.0, 1.0);
self.rms_min + t * (self.rms_max - self.rms_min)
}
}
pub fn ramp_edges(chunk: &mut [f32]) {
const RAMP_MS: usize = 5;
let ramp = (ENGINE_RATE as usize * RAMP_MS / 1000).min(chunk.len() / 2);
if ramp == 0 {
return;
}
let last = chunk.len() - 1;
for i in 0..ramp {
let gain = i as f32 / ramp as f32;
chunk[i] *= gain;
chunk[last - i] *= gain;
}
}
pub fn vad_chunks(samples: &[f32], threshold: f32, settings: &VadSettings) -> Vec<(usize, usize)> {
if samples.is_empty() || threshold <= 0.0 {
return Vec::new();
}
let floor = settings.floor(threshold);
let frame = settings.frame_samples;
let mut speech = Vec::with_capacity(samples.len().div_ceil(frame));
let mut i = 0;
while i < samples.len() {
let end = (i + frame).min(samples.len());
let energy: f32 = samples[i..end].iter().map(|s| s * s).sum::<f32>() / (end - i) as f32;
speech.push(energy.sqrt() >= floor);
i = end;
}
let mut ranges = Vec::new();
let mut start: Option<usize> = None;
for (idx, is_speech) in speech.iter().enumerate() {
if *is_speech && start.is_none() {
start = Some(idx * frame);
} else if !*is_speech {
if let Some(s) = start.take() {
ranges.push((s, idx * frame));
}
}
}
if let Some(s) = start {
ranges.push((s, samples.len()));
}
ranges
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilteredText {
pub text: String,
pub removed: usize,
}
pub fn filter_hallucinations(text: &str, re: Option<&Regex>) -> FilteredText {
let Some(re) = re else {
return FilteredText {
text: text.to_string(),
removed: 0,
};
};
let removed = text.lines().filter(|l| re.is_match(l)).count();
let filtered = re.replace_all(text, "");
let mut result = String::new();
let mut prev_empty = false;
for line in filtered.lines() {
let empty = line.trim().is_empty();
if empty {
if !prev_empty {
result.push('\n');
}
prev_empty = true;
} else {
result.push_str(line);
result.push('\n');
prev_empty = false;
}
}
FilteredText {
text: result.trim_end().to_string(),
removed,
}
}
pub fn collapse_consecutive_repeats(text: &str) -> String {
let mut result = String::new();
let mut last: Option<String> = None;
for line in text.lines() {
let content = line.trim();
if content.is_empty() {
result.push('\n');
last = None;
} else if last.as_deref() == Some(content) {
} else {
result.push_str(line);
result.push('\n');
last = Some(content.to_string());
}
}
result.trim_end().to_string()
}
pub const HALLUCINATION_PATTERNS: &[&str] = &[
r"legenda(?:s)? por\b",
r"legendas? pela comunidade",
r"transcri(?:ção|cao) por\b",
r"transcri(?:ção|cao) e legendas?\b",
r"amara\.org",
r"inscreva-se",
r"subscribe",
r"www\.",
r"subtítulos? (?:por|realizados)",
r"subtitulado por\b",
r"sous-titr(?:es|age) (?:par|de)\b",
r"untertitel (?:von|im auftrag)",
r"sottotitoli (?:e revisione )?a cura",
];
pub fn hallucination_regex_from(patterns: &[String]) -> Result<Regex, Error> {
let body = patterns.join("|");
Regex::new(&format!(r"(?im)^[ \t]*({body}).*$"))
.map_err(|e| Error::Config(format!("invalid hallucination pattern: {e}")))
}
pub fn default_hallucination_regex() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| {
let body = HALLUCINATION_PATTERNS.join("|");
Regex::new(&format!(r"(?im)^[ \t]*({body}).*$")).expect("seeded patterns must compile")
})
}
fn ext(path: &Path) -> Option<String> {
path.extension()
.and_then(|e| e.to_str())
.map(|s| s.to_ascii_lowercase())
}
fn looks_video(bytes: &[u8]) -> bool {
bytes.len() > 8 && &bytes[4..8] == b"ftyp"
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ramp_edges_lands_on_zero_at_both_ends() {
let mut chunk = vec![1.0f32; 16_000];
ramp_edges(&mut chunk);
assert_eq!(chunk[0], 0.0, "the leading edge must start at silence");
assert_eq!(
chunk[chunk.len() - 1],
0.0,
"the trailing edge must end at silence"
);
assert_eq!(chunk[8_000], 1.0, "the body of the chunk must not be faded");
}
#[test]
fn ramp_edges_survives_a_tiny_chunk() {
for len in [0usize, 1, 2, 3, 79, 160] {
let mut chunk = vec![1.0f32; len];
ramp_edges(&mut chunk);
assert_eq!(chunk.len(), len, "length changed for {len}");
}
}
#[test]
fn hallucination_strips_legendas() {
let input = "Hello world\nlegendas por comunidade\nGoodbye";
let out = filter_hallucinations(input, Some(default_hallucination_regex()));
assert!(out.text.contains("Hello world"));
assert!(!out.text.contains("legendas por"));
assert_eq!(out.removed, 1, "the drop must be counted, not silent");
}
#[test]
fn hallucination_filter_off_keeps_everything() {
let input = "Hello world\nlegendas por comunidade\nGoodbye";
let out = filter_hallucinations(input, None);
assert_eq!(out.text, input);
assert_eq!(out.removed, 0);
}
#[test]
fn hallucination_reaches_other_languages() {
for line in [
"Subtítulos por la comunidad",
"Sous-titrage par Amara",
"Untertitel von der Community",
"Sottotitoli a cura di qualcuno",
] {
let input = format!("Hola\n{line}\nAdios");
let out = filter_hallucinations(&input, Some(default_hallucination_regex()));
assert_eq!(out.removed, 1, "not filtered: {line}");
assert!(out.text.contains("Hola"), "content lost for: {line}");
}
}
#[test]
fn operator_patterns_replace_the_seed() {
let re = hallucination_regex_from(&["ruído meu".to_string()]).expect("compile");
let out = filter_hallucinations("keep\nruído meu\nkeep2", Some(&re));
assert_eq!(out.removed, 1);
let out2 = filter_hallucinations("legendas por alguem", Some(&re));
assert_eq!(out2.removed, 0);
}
#[test]
fn invalid_pattern_is_a_config_error() {
let err = hallucination_regex_from(&["(unclosed".to_string()]).expect_err("must fail");
assert_eq!(err.exit_code(), 78);
}
#[test]
fn collapse_repeats() {
let result = collapse_consecutive_repeats("a\na\nb");
assert_eq!(result, "a\nb");
}
#[test]
fn vad_empty_on_silence() {
let samples = vec![0.0f32; 16000];
assert!(vad_chunks(&samples, 0.5, &VadSettings::default()).is_empty());
}
#[test]
fn vad_empty_on_absolute_silence() {
let samples = vec![0.0f32; 48_000];
assert!(vad_chunks(&samples, 0.5, &VadSettings::default()).is_empty());
}
#[test]
fn vad_splits_speech_around_silence() {
let half = 8_000; let tone = |n: usize| -> Vec<f32> {
(0..n)
.map(|i| (i as f32 * 2.0 * std::f32::consts::PI * 200.0 / 16_000.0).sin() * 0.3)
.collect()
};
let mut samples = tone(half);
samples.extend(std::iter::repeat_n(0.0f32, half));
samples.extend(tone(half));
let chunks = vad_chunks(&samples, 0.5, &VadSettings::default());
assert!(chunks.len() >= 2, "expected split, got {chunks:?}");
}
#[test]
fn sensitivity_zero_never_cuts_even_around_silence() {
let half = 8_000;
let tone = |n: usize| -> Vec<f32> {
(0..n)
.map(|i| (i as f32 * 2.0 * std::f32::consts::PI * 200.0 / 16_000.0).sin() * 0.3)
.collect()
};
let mut samples = tone(half);
samples.extend(std::iter::repeat_n(0.0f32, half));
samples.extend(tone(half));
assert!(
vad_chunks(&samples, 0.5, &VadSettings::default()).len() >= 2,
"the fixture must be one that a live cut would split"
);
assert!(
vad_chunks(&samples, 0.0, &VadSettings::default()).is_empty(),
"sensitivity 0 must hand the whole recording to the engine"
);
}
#[test]
fn sensitivity_below_zero_is_also_off() {
let samples = vec![0.3f32; 16_000];
assert!(vad_chunks(&samples, -1.0, &VadSettings::default()).is_empty());
}
fn sine_i16(freq: f32, rate: u32, n: usize) -> Vec<i16> {
(0..n)
.map(|i| {
let v = (i as f32 * 2.0 * std::f32::consts::PI * freq / rate as f32).sin() * 0.3;
(v * i16::MAX as f32) as i16
})
.collect()
}
#[test]
fn stereo_48k_matches_mono_16k() {
let mono = PcmData {
samples: sine_i16(100.0, 16_000, 16_000),
sample_rate: 16_000,
channels: 1,
};
let frames = sine_i16(100.0, 48_000, 48_000);
let mut interleaved = Vec::with_capacity(frames.len() * 2);
for s in &frames {
interleaved.push(*s);
interleaved.push(*s);
}
let stereo = PcmData {
samples: interleaved,
sample_rate: 48_000,
channels: 2,
};
let a = to_16k_mono(&mono).expect("mono");
let b = to_16k_mono(&stereo).expect("stereo");
assert!(
(a.len() as i64 - b.len() as i64).abs() <= 1,
"lengths {} vs {}",
a.len(),
b.len()
);
let n = a.len().min(b.len());
for i in 0..n {
assert!(
(a[i] - b[i]).abs() < 0.01,
"sample {i}: {} vs {}",
a[i],
b[i]
);
}
}
#[test]
fn zero_sample_rate_is_decode_error() {
let pcm = PcmData {
samples: vec![0i16; 32],
sample_rate: 0,
channels: 1,
};
assert!(to_16k_mono(&pcm).is_err());
}
fn wav_bytes() -> Vec<u8> {
let spec = hound::WavSpec {
channels: 1,
sample_rate: 16_000,
bits_per_sample: 16,
sample_format: hound::SampleFormat::Int,
};
let mut cursor = std::io::Cursor::new(Vec::new());
{
let mut w = hound::WavWriter::new(&mut cursor, spec).expect("writer");
for s in sine_i16(200.0, 16_000, 1_600) {
w.write_sample(s).expect("sample");
}
w.finalize().expect("finalize");
}
cursor.into_inner()
}
#[test]
fn unknown_extension_is_unsupported_format() {
let path = std::env::temp_dir().join(format!(
"whisper-nvidia-audio-{}-{}.xyz",
std::process::id(),
line!()
));
std::fs::write(&path, wav_bytes()).expect("write");
let err = decode_file(&path).expect_err("must reject");
let _ = std::fs::remove_file(&path);
assert!(matches!(err, Error::UnsupportedFormat { .. }), "{err:?}");
assert_eq!(err.exit_code(), 65);
}
#[test]
fn lying_forced_format_is_a_mismatch_not_an_unsupported_format() {
let err = decode_bytes(&wav_bytes(), Some("mp3")).expect_err("must reject");
assert!(matches!(err, Error::FormatMismatch { .. }), "{err:?}");
assert_eq!(err.exit_code(), 65);
assert!(
err.to_string().contains("does not match"),
"the message must name the contradiction: {err}"
);
}
#[test]
fn a_format_outside_the_list_is_unsupported() {
let err = decode_bytes(&wav_bytes(), Some("banana")).expect_err("must reject");
assert!(matches!(err, Error::UnsupportedFormat { .. }), "{err:?}");
assert_eq!(err.exit_code(), 65);
}
#[test]
fn honest_forced_format_decodes() {
let pcm = decode_bytes(&wav_bytes(), Some("wav")).expect("wav decodes");
assert_eq!(pcm.sample_rate, 16_000);
assert_eq!(pcm.channels, 1);
}
}
#[cfg(test)]
mod antialias_tests {
use super::*;
fn sine(freq: f64, rate: u32, n: usize) -> Vec<f32> {
(0..n)
.map(|i| {
(2.0 * std::f64::consts::PI * freq * i as f64 / rate as f64).sin() as f32 * 0.3
})
.collect()
}
fn rms(x: &[f32]) -> f32 {
if x.is_empty() {
return 0.0;
}
(x.iter().map(|v| v * v).sum::<f32>() / x.len() as f32).sqrt()
}
#[test]
fn tones_above_nyquist_are_removed_rather_than_folded() {
let src = 48_000u32;
for freq in [12_000.0, 15_000.0, 20_000.0] {
let input = sine(freq, src, src as usize);
let before = rms(&input);
let out = resample_linear(&input, src, 16_000).expect("resample");
let after = rms(&out);
assert!(
after < before * 0.2,
"a {freq} Hz tone is above the 8 kHz Nyquist of the destination \
and must not come through: rms went {before:.4} -> {after:.4}"
);
}
}
#[test]
fn tones_below_nyquist_come_through() {
let src = 48_000u32;
for freq in [200.0, 1_000.0, 3_000.0] {
let input = sine(freq, src, src as usize);
let before = rms(&input);
let out = resample_linear(&input, src, 16_000).expect("resample");
let after = rms(&out);
assert!(
after > before * 0.7,
"a {freq} Hz tone is well inside the band and must survive: \
rms went {before:.4} -> {after:.4}"
);
}
}
#[test]
fn upsampling_does_not_filter() {
let input = sine(500.0, 8_000, 8_000);
let before = rms(&input);
let out = resample_linear(&input, 8_000, 16_000).expect("resample");
assert!(
rms(&out) > before * 0.9,
"upsampling must not attenuate a tone well inside the band: \
{before:.4} -> {:.4}",
rms(&out)
);
assert_eq!(out.len(), 16_000, "and must produce the right sample count");
}
}
#[cfg(test)]
mod normalization_tests {
use super::*;
#[test]
fn the_most_negative_sample_stays_inside_minus_one() {
let pcm = PcmData {
samples: vec![i16::MIN, i16::MAX, 0, i16::MIN],
sample_rate: 16_000,
channels: 1,
};
let out = to_16k_mono(&pcm).expect("convert");
for (i, v) in out.iter().enumerate() {
assert!(
(-1.0..=1.0).contains(v),
"sample {i} normalised to {v}, outside the range the engine accepts"
);
}
assert_eq!(out[0], -1.0, "the floor maps exactly to -1.0");
}
#[test]
fn a_stereo_fold_also_stays_in_range() {
let pcm = PcmData {
samples: vec![i16::MIN, i16::MIN, i16::MAX, i16::MAX],
sample_rate: 16_000,
channels: 2,
};
let out = to_16k_mono(&pcm).expect("convert");
assert_eq!(out.len(), 2, "two frames from four interleaved samples");
for v in &out {
assert!(
(-1.0..=1.0).contains(v),
"a folded frame normalised to {v}, outside the accepted range"
);
}
}
#[test]
fn full_scale_is_not_quietly_scaled_down() {
let pcm = PcmData {
samples: vec![i16::MAX],
sample_rate: 16_000,
channels: 1,
};
let out = to_16k_mono(&pcm).expect("convert");
assert!(
out[0] > 0.999,
"positive full scale must stay at the top of the range, got {}",
out[0]
);
}
}
#[cfg(test)]
mod source_time_tests {
use super::*;
fn gapped() -> (Vec<(usize, usize)>, usize) {
let r = ENGINE_RATE as usize;
(vec![(0, 2 * r), (10 * r, 12 * r)], 12 * r)
}
#[test]
fn a_timestamp_inside_the_first_chunk_is_unchanged() {
let (chunks, total) = gapped();
assert_eq!(source_time(&chunks, total, 1.5), 1.5);
}
#[test]
fn the_second_chunk_is_moved_past_the_pause() {
let (chunks, total) = gapped();
assert_eq!(source_time(&chunks, total, 2.0), 10.0);
assert_eq!(source_time(&chunks, total, 3.5), 11.5);
}
#[test]
fn a_timestamp_past_the_speech_lands_where_the_speech_ended() {
let (chunks, total) = gapped();
assert_eq!(source_time(&chunks, total, 99.0), 12.0);
}
#[test]
fn an_empty_cut_is_the_identity() {
assert_eq!(source_time(&[], 16_000, 7.25), 7.25);
}
#[test]
fn the_mapping_never_runs_backwards() {
let (chunks, total) = gapped();
let slack = 1.0 / f64::from(ENGINE_RATE);
let mut previous = 0.0f64;
for step in 0..=40 {
let t = f64::from(step) / 10.0;
let mapped = source_time(&chunks, total, t);
assert!(
mapped + slack >= t,
"cutting removes time, so {t} cannot map to an earlier {mapped}"
);
assert!(
mapped + slack >= previous,
"order broke: {t} mapped to {mapped}, behind the previous {previous}"
);
previous = mapped;
}
}
}