use decibri_decode::decibri_resampler::{PolyphaseResampler, Resampler};
use decibri_decode::{
decode, downmix_to_mono, AudioBuffer, AudioSpec, AudioStreamDecoder, DecodeError, StreamSource,
WavCodec, WavWriter,
};
const TARGET_RATE: u32 = 16_000;
struct StreamPipeline {
target_rate: u32,
decoder: AudioStreamDecoder,
resampler: Option<PolyphaseResampler>,
batch: Vec<f32>,
mono: Vec<f32>,
samples: Vec<f32>,
}
impl StreamPipeline {
fn new(target_rate: u32) -> Self {
Self {
target_rate,
decoder: AudioStreamDecoder::new(),
resampler: None,
batch: Vec::new(),
mono: Vec::new(),
samples: Vec::new(),
}
}
fn push(&mut self, mut piece: &[u8]) -> Result<(), DecodeError> {
while !piece.is_empty() {
let taken = self.decoder.push(piece)?;
piece = &piece[taken..];
self.drain()?;
}
Ok(())
}
fn drain(&mut self) -> Result<(), DecodeError> {
let Some(spec) = self.decoder.spec() else {
return Ok(());
};
loop {
self.batch.clear();
if self.decoder.pull(&mut self.batch, usize::MAX)? == 0 {
break;
}
self.convert(spec)?;
}
Ok(())
}
fn convert(&mut self, spec: AudioSpec) -> Result<(), DecodeError> {
self.mono.clear();
downmix_to_mono(&self.batch, spec.channels, &mut self.mono);
if self.resampler.is_none() {
self.resampler = Some(PolyphaseResampler::new(spec.sample_rate, self.target_rate)?);
}
let resampler = self
.resampler
.as_mut()
.expect("constructed on the line above");
resampler.process(&self.mono, &mut self.samples)?;
Ok(())
}
fn finish(mut self) -> Result<AudioBuffer, DecodeError> {
self.batch.clear();
self.decoder.finish(&mut self.batch)?;
if let Some(spec) = self.decoder.spec() {
if !self.batch.is_empty() {
self.convert(spec)?;
}
}
if let Some(resampler) = self.resampler.as_mut() {
resampler.flush(&mut self.samples);
}
Ok(AudioBuffer::from_samples(
AudioSpec::mono(self.target_rate),
self.samples,
))
}
}
fn demo_wav() -> Result<Vec<u8>, DecodeError> {
const FRAMES: usize = 4_800;
let mut samples = Vec::with_capacity(FRAMES * 2);
for frame in 0..FRAMES {
let value = ((frame * 41) % 1_024) as f32 / 1_024.0 - 0.5;
samples.push(value); samples.push(value * 0.25); }
WavWriter::new(AudioSpec::new(48_000, 2), WavCodec::Float32).to_bytes(&samples)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = demo_wav()?;
let mut pipeline = StreamPipeline::new(TARGET_RATE);
for piece in file.chunks(509) {
pipeline.push(piece)?;
}
let streamed = pipeline.finish()?;
println!(
"streamed: {} samples of mono f32 at {} Hz",
streamed.samples().len(),
streamed.sample_rate()
);
let decoded = decode(&file)?;
let mut mono = Vec::new();
downmix_to_mono(decoded.samples(), decoded.channels(), &mut mono);
let mut resampler = PolyphaseResampler::new(decoded.sample_rate(), TARGET_RATE)?;
let mut whole = Vec::new();
resampler.process(&mono, &mut whole)?;
resampler.flush(&mut whole);
assert_eq!(
streamed.samples().len(),
whole.len(),
"the two paths must produce the same sample count"
);
assert!(
streamed
.samples()
.iter()
.zip(&whole)
.all(|(a, b)| a.to_bits() == b.to_bits()),
"the two paths must agree bit-for-bit"
);
println!("streamed output is bit-identical to the whole-file path");
Ok(())
}