Skip to content

Instantly share code, notes, and snippets.

@jamesmunns
Last active August 23, 2022 02:47
Show Gist options
  • Save jamesmunns/582469bad0820259ffcb65f10b1e8243 to your computer and use it in GitHub Desktop.
Save jamesmunns/582469bad0820259ffcb65f10b1e8243 to your computer and use it in GitHub Desktop.
// Conversation piece.
//
// This is intended to be driven/rendered by a GUI.
fn fake_code() -> Result<(), ()> {
let mut oracle = Oracle::new();
// Setup the builder items. This would typically be done
// as a user adds components. Everything is the same type so it
// can go into a vec or array for the gui
let mut speaker: Box<dyn OpBuilder> = Speaker::alloc_builder(&mut oracle)?;
let mut osc: Box<dyn OpBuilder> = Osc::alloc_builder(&mut oracle)?;
let mut dial: Box<dyn OpBuilder> = Dial::alloc_builder(&mut oracle)?;
println!("{:?}", dial.outputs());
// Output { name: "pitch", kind: Pitch, uid: BufUid(123) }
println!("{:?}", osc.inputs());
// Input { name: "pitch", kind: Pitch, uid: None }
let dial_out: BufUid = dial.get_output("pitch")?;
osc.set_input("pitch", dial_out)?;
println!("{:?}", osc.inputs());
// Input { name: "pitch", kind: Pitch, uid: Some(BufUid(123)) }
println!("{:?}", osc.get_valid_settings_u32("kind")?);
// Settings {
// min: 0, max: 3, default: 0,
// values: Some([
// (0, "Sine"),
// (1, "Square"),
// (2, "Saw"),
// (3, "Triangle"),
// ]),
//}
osc.set_setting("kind", 1)?;
let osc_out = osc.get_output("tone")?;
speaker.set_input("audio", osc_out)?;
// Validate the builders, ensure there are no cycles in the graph,
// allocate the actual data buffers, connect them, and set up the
// control-flow.
let mut player: Player = oracle.build_player(&[&dial, &osc, &speaker])?;
let start = Instant::now();
while start.elapsed() <= Duration::from_secs(3) {
play(player.step());
}
// Now change the osc setting - doesn't require re-validating
// or re-building the player if ONLY settings are changed
osc.set_setting(kind, 2)?;
player.update_player(&osc)?;
while start.elapsed() <= Duration::from_secs(6) {
play(player.step());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment