use proof_engine::audio::math_source::MathAudioSource;
use proof_engine::audio::wav::write_wav;
use proof_engine::audio::{AudioEvent, OfflineRenderer};
use proof_engine::prelude::Vec3;
fn main() -> std::io::Result<()> {
let path = std::env::args().nth(1).unwrap_or_else(|| "audio_bounce.wav".to_string());
let mut synth = OfflineRenderer::new(48_000);
let mut out = Vec::new();
let score: Vec<(f32, AudioEvent)> = vec![
(0.0, AudioEvent::SpawnSource {
source: MathAudioSource::chaos_tone(Vec3::ZERO).with_lifetime(5.5).with_fade(0.5, 1.0),
position: Vec3::ZERO,
}),
(1.5, AudioEvent::SpawnSource { source: MathAudioSource::victory(Vec3::ZERO), position: Vec3::ZERO }),
(4.0, AudioEvent::SpawnSource {
source: MathAudioSource::death_knell(Vec3::new(0.6, 0.0, 0.8)),
position: Vec3::new(0.6, 0.0, 0.8),
}),
];
let length = 7.5;
let block = 0.01;
let mut pending = score.into_iter().peekable();
while synth.time() < length {
while let Some((at, _)) = pending.peek() {
if *at > synth.time() {
break;
}
synth.emit(pending.next().unwrap().1);
}
out.extend(synth.render(block));
}
write_wav(&path, synth.sample_rate(), 2, &out)?;
let peak = out.iter().fold(0.0f32, |m, s| m.max(s.abs()));
println!(
"wrote {path}: {:.1} s of 48 kHz stereo, peak {:.2}",
out.len() as f32 / 2.0 / synth.sample_rate() as f32,
peak
);
Ok(())
}