asr-core 0.0.2

Unified streaming, offline, and cloud speech-to-text engine for Rust
Documentation
use super::*;
use crate::audio::{decode_wav_pcm16, encode_wav_pcm16};
use std::io::Cursor;

fn wav_bytes(
    channels: u16,
    sample_rate: u32,
    bits_per_sample: u16,
    sample_format: hound::SampleFormat,
    write: impl FnOnce(&mut hound::WavWriter<&mut Cursor<Vec<u8>>>),
) -> Vec<u8> {
    let mut bytes = Cursor::new(Vec::new());
    {
        let mut wav = hound::WavWriter::new(
            &mut bytes,
            hound::WavSpec {
                channels,
                sample_rate,
                bits_per_sample,
                sample_format,
            },
        )
        .unwrap();
        write(&mut wav);
        wav.finalize().unwrap();
    }
    bytes.into_inner()
}
#[test]
fn float_wav_decodes_instead_of_being_rejected() {
    let bytes = wav_bytes(1, 16000, 32, hound::SampleFormat::Float, |wav| {
        for sample in [-1.0f32, -0.25, 0.0, 0.25, 1.0] {
            wav.write_sample(sample).unwrap();
        }
    });
    let decoded = decode_audio(&bytes).unwrap();
    assert_eq!(decoded.spec.sample_rate, 16000);
    assert_eq!(decoded.samples, [-1.0, -0.25, 0.0, 0.25, 1.0]);
}
#[test]
fn int24_wav_decodes_with_symphonia_scaling() {
    let bytes = wav_bytes(1, 16000, 24, hound::SampleFormat::Int, |wav| {
        for value in [-8_388_608i32, -1_000_000, 0, 1_000_000, 8_388_607] {
            wav.write_sample(value).unwrap();
        }
    });
    let decoded = decode_audio(&bytes).unwrap();
    for (value, sample) in [-8_388_608i32, -1_000_000, 0, 1_000_000, 8_388_607]
        .into_iter()
        .zip(decoded.samples)
    {
        assert!((sample - value as f32 / 8_388_608.0).abs() < f32::EPSILON);
    }
}
#[test]
fn stereo_float_downmixes_by_frame_average() {
    let bytes = wav_bytes(2, 8000, 32, hound::SampleFormat::Float, |wav| {
        for (left, right) in [(0.5f32, -0.5), (0.25, 0.75), (0.0, 0.0)] {
            wav.write_sample(left).unwrap();
            wav.write_sample(right).unwrap();
        }
    });
    let decoded = decode_audio(&bytes).unwrap();
    assert_eq!(decoded.spec.sample_rate, 8000);
    assert_eq!(decoded.samples, [0.0, 0.5, 0.0]);
}
#[test]
fn mono_pcm16_matches_read_wav_pcm16() {
    let bytes = encode_wav_pcm16(&[-1.0, -0.5, 0.0, 0.5, 1.0], 24000).unwrap();
    assert_eq!(
        decode_audio(&bytes).unwrap().samples,
        decode_wav_pcm16(&bytes).unwrap().samples
    );
}
#[test]
fn probes_extensionless_file_by_content() {
    let bytes = encode_wav_pcm16(&[0.25, -0.25], 16000).unwrap();
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("speech");
    std::fs::write(&path, bytes).unwrap();
    let decoded = read_audio(&path).unwrap();
    assert_eq!(decoded.spec.sample_rate, 16000);
    assert_eq!(decoded.samples.len(), 2);
}
#[test]
fn garbage_and_empty_input_are_rejected() {
    for bytes in [b"definitely not audio".as_slice(), b"".as_slice()] {
        assert!(decode_audio(bytes).is_err());
    }
}
#[test]
fn sample_rate_outside_engine_bounds_is_rejected() {
    let bytes = wav_bytes(1, 4000, 16, hound::SampleFormat::Int, |wav| {
        wav.write_sample(0i32).unwrap();
    });
    let error = decode_audio(&bytes).unwrap_err();
    assert_eq!(error.kind, ErrorKind::InvalidInput);
    assert!(error.message.contains("sample rate"));
}
#[test]
fn empty_wav_decodes_to_empty_buffer() {
    let bytes = wav_bytes(1, 16000, 16, hound::SampleFormat::Int, |_| {});
    let decoded = decode_audio(&bytes).unwrap();
    assert_eq!(decoded.spec.sample_rate, 16000);
    assert!(decoded.samples.is_empty());
}