2024-11-15 17:08:54 +00:00
|
|
|
use rodio::mixer;
|
2024-11-13 23:08:50 +00:00
|
|
|
use rodio::source::{SineWave, Source};
|
2024-11-19 20:58:05 +00:00
|
|
|
use std::error::Error;
|
2023-01-11 17:32:39 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2024-11-19 20:58:05 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2023-01-11 17:32:39 +00:00
|
|
|
// Construct a dynamic controller and mixer, stream_handle, and sink.
|
2024-11-15 17:08:54 +00:00
|
|
|
let (controller, mixer) = mixer::mixer::<f32>(2, 44_100);
|
2024-11-21 17:43:24 +00:00
|
|
|
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
|
2024-11-09 22:34:49 +00:00
|
|
|
let sink = rodio::Sink::connect_new(&stream_handle.mixer());
|
2023-01-11 17:32:39 +00:00
|
|
|
|
|
|
|
// Create four unique sources. The frequencies used here correspond
|
|
|
|
// notes in the key of C and in octave 4: C4, or middle C on a piano,
|
|
|
|
// E4, G4, and A4 respectively.
|
|
|
|
let source_c = SineWave::new(261.63)
|
|
|
|
.take_duration(Duration::from_secs_f32(1.))
|
|
|
|
.amplify(0.20);
|
|
|
|
let source_e = SineWave::new(329.63)
|
|
|
|
.take_duration(Duration::from_secs_f32(1.))
|
|
|
|
.amplify(0.20);
|
|
|
|
let source_g = SineWave::new(392.0)
|
|
|
|
.take_duration(Duration::from_secs_f32(1.))
|
|
|
|
.amplify(0.20);
|
|
|
|
let source_a = SineWave::new(440.0)
|
|
|
|
.take_duration(Duration::from_secs_f32(1.))
|
|
|
|
.amplify(0.20);
|
|
|
|
|
|
|
|
// Add sources C, E, G, and A to the mixer controller.
|
|
|
|
controller.add(source_c);
|
|
|
|
controller.add(source_e);
|
|
|
|
controller.add(source_g);
|
|
|
|
controller.add(source_a);
|
|
|
|
|
|
|
|
// Append the dynamic mixer to the sink to play a C major 6th chord.
|
|
|
|
sink.append(mixer);
|
|
|
|
|
|
|
|
// Sleep the thread until sink is empty.
|
|
|
|
sink.sleep_until_end();
|
2024-11-19 20:58:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
2023-01-11 17:32:39 +00:00
|
|
|
}
|