use nam_rs::{NamModel, WaveNet};
const BLOCK: usize = 128;
fn main() -> Result<(), nam_rs::Error> {
let path = std::env::args()
.nth(1)
.expect("usage: streaming <model.nam>");
let model = NamModel::from_file(&path)?;
let mut amp = WaveNet::new(&model)?;
println!(
"loaded {path}: {} layer-arrays, receptive field {} samples \
(~{:.1} ms at {} Hz) — the startup transient before output settles",
model.config.layers.len(),
amp.receptive_field(),
amp.receptive_field() as f64 / model.sample_rate() * 1000.0,
model.sample_rate(),
);
let signal: Vec<f32> = (0..8 * BLOCK)
.map(|i| 0.5 * (i as f32 * 0.05).sin())
.collect();
let mut streamed = signal.clone();
for block in streamed.chunks_mut(BLOCK) {
amp.process_buffer(block);
}
amp.reset();
let mut oneshot = signal.clone();
amp.process_buffer(&mut oneshot);
let max_diff = streamed
.iter()
.zip(&oneshot)
.map(|(a, b)| (a - b).abs())
.fold(0.0_f32, f32::max);
println!(
"streamed {} samples in {}-sample blocks; max deviation from a single \
whole-buffer call: {max_diff:e}",
signal.len(),
BLOCK,
);
assert_eq!(max_diff, 0.0, "block size must not change the output");
Ok(())
}