feat: modularize sin wave generator

This commit is contained in:
2026-10-18 13:52:00 +02:00
parent 27ee2efd00
commit 1682a26c61
6 changed files with 78 additions and 21 deletions
+3 -11
View File
@@ -1,6 +1,6 @@
use clap::Parser;
use cpal::traits::DeviceTrait;
use raust_engine::{AudioPlayer, Note};
use raust_engine::{AudioPlayer, Note, generators::Generator};
/// Play a single tone through the default audio output device.
#[derive(Parser, Debug)]
@@ -24,16 +24,8 @@ fn main() -> anyhow::Result<()> {
let (device, config) = raust_engine::get_device_and_config()?;
println!("Output device: {}", device.id()?);
println!("Default output config: {config:?}");
let mut clock = 0f32;
// Sample rates are a few hundred kHz at most, nowhere near f32's
// 24-bit exact-integer range, so this cast never loses precision.
#[allow(clippy::cast_precision_loss, clippy::as_conversions)]
let sample_rate = config.sample_rate() as f32;
let generator = move || {
clock = (clock + 1.0) % sample_rate;
(clock * note.pitch * 2.0 * std::f32::consts::PI / sample_rate).sin()
};
let audio_player = AudioPlayer::try_new(&device, config, Box::new(generator))?;
let mut sin = raust_engine::generators::SinWave::new(config.sample_rate(), note.pitch);
let audio_player = AudioPlayer::try_new(&device, config, Box::new(move || sin.generate()))?;
audio_player.play()?;
std::thread::sleep(std::time::Duration::from_millis(note.length));