use tono_core::dsl::SoundDoc;
use tono_core::runtime::{AudioSource, Engine, Tween};
fn peak(buf: &[f32]) -> f32 {
buf.iter().fold(0.0f32, |m, &x| m.max(x.abs()))
}
fn blip() -> SoundDoc {
serde_json::from_str(
r#"{ "name":"blip", "duration":0.4, "root":{ "type":"sine", "freq":440 } }"#,
)
.expect("valid doc")
}
fn main() {
let sr = 48_000;
let mut engine = Engine::new(sr);
let patch = engine.load(&blip());
let a = engine.play_looping(patch);
let b = engine.play_looping(patch);
engine.set_gain(b, 0.5, Tween::ms(20.0, sr));
engine.set_pan(a, -0.7, Tween::ms(20.0, sr));
engine.set_pan(b, 0.7, Tween::ms(20.0, sr));
let mut block = vec![0.0f32; 512 * 2];
let mut p = 0.0f32;
for _ in 0..20 {
engine.fill(&mut block);
p = p.max(peak(&block));
}
println!("direct mix: {} instances, peak {p:.3}", engine.active());
let mut engine = Engine::new(sr);
let patch = engine.load(&blip());
let (mut control, mut audio) = engine.split(2048);
control.play_looping(patch);
control.pump(2048); let mut out = vec![0.0f32; 512 * 2];
let frames = audio.fill(&mut out); println!("split mix: pulled {frames} frames, peak {:.3}", peak(&out));
}