audio_mixer/
audio_mixer.rs

1// examples/mixer.rs
2//! Real-time Mixer Example
3//!
4//! Demonstrates mixing two oscillators using Auxide + Auxide IO.
5//! Plays a chord created by mixing 440Hz and 554Hz (A and C#).
6//! Run with: cargo run --example mixer
7
8use auxide::graph::{Graph, NodeType, PortId, Rate};
9use auxide::plan::Plan;
10use auxide::rt::Runtime;
11use auxide_io::stream_controller::StreamController;
12use std::thread;
13use std::time::Duration;
14
15fn main() -> anyhow::Result<()> {
16    println!("Auxide Mixer Demo");
17    println!("Playing a chord: 440Hz (A) + 554Hz (C#)...");
18
19    // Create graph: two oscillators + mixer + output
20    let mut graph = Graph::new();
21
22    // Oscillator 1: 440Hz (A note)
23    let osc1 = graph.add_node(NodeType::SineOsc { freq: 440.0 });
24
25    // Oscillator 2: 554Hz (C# note) - creates a major sixth interval
26    let osc2 = graph.add_node(NodeType::SineOsc { freq: 554.0 });
27
28    // Mixer to combine the two oscillators
29    let mix = graph.add_node(NodeType::Mix);
30
31    // Output sink
32    let sink = graph.add_node(NodeType::OutputSink);
33
34    // Connect: osc1 -> mix (input 0)
35    graph.add_edge(auxide::graph::Edge {
36        from_node: osc1,
37        from_port: PortId(0),
38        to_node: mix,
39        to_port: PortId(0),
40        rate: Rate::Audio,
41    }).unwrap();
42
43    // Connect: osc2 -> mix (input 1)
44    graph.add_edge(auxide::graph::Edge {
45        from_node: osc2,
46        from_port: PortId(0),
47        to_node: mix,
48        to_port: PortId(1),
49        rate: Rate::Audio,
50    }).unwrap();
51
52    // Connect: mix -> output
53    graph.add_edge(auxide::graph::Edge {
54        from_node: mix,
55        from_port: PortId(0),
56        to_node: sink,
57        to_port: PortId(0),
58        rate: Rate::Audio,
59    }).unwrap();
60
61    let plan = Plan::compile(&graph, 512).unwrap();
62    let runtime = Runtime::new(plan, &graph, 44100.0);
63
64    // Play the mixed oscillators
65    let controller = StreamController::play(runtime)?;
66    controller.start()?;
67
68    println!("Playing for 10 seconds...");
69    thread::sleep(Duration::from_secs(10));
70
71    controller.stop();
72    println!("Mixer demo complete!");
73
74    Ok(())
75}