use anyhow::Result;
#[allow(unused)]
use knyst::{
audio_backend::{CpalBackend, CpalBackendOptions, JackBackend},
controller::print_error_handler,
prelude::*,
};
fn main() -> Result<()> {
let _backend = setup();
let node0 = wavetable_oscillator_owned(Wavetable::sine()).freq(440.);
let modulator = oscillator(WavetableId::cos()).freq(5.);
graph_output(0, node0 * (modulator * 0.25 + 0.5));
let node1 = wavetable_oscillator_owned(Wavetable::sine()).freq(220.);
let modulator = oscillator(WavetableId::cos()).freq(3.);
graph_output(1, node1 * (modulator * 0.25 + 0.5));
println!("Playing a sine wave with 440 Hz and 220 Hz");
println!("Enter a new frequency for the left channel followed by [ENTER]");
println!("Press [q] to quit");
let mut input = String::new();
loop {
match std::io::stdin().read_line(&mut input) {
Ok(_n) => {
let input = input.trim();
if let Ok(freq) = input.parse::<usize>() {
node0.freq(freq as f32);
println!("Setting freq to {freq}");
} else if input == "q" {
break;
}
}
Err(error) => println!("error: {}", error),
}
input.clear();
}
Ok(())
}
fn setup() -> impl AudioBackend {
let mut backend =
CpalBackend::new(CpalBackendOptions::default()).expect("Unable to connect to CPAL backend");
let _sphere_id = KnystSphere::start(
&mut backend,
SphereSettings {
..Default::default()
},
print_error_handler,
)
.ok();
backend
}