refactor: clean separation between lib and CLI
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "raust-cli"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
repository.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
clap = { version = "4.6.7", features = ["derive"] }
|
||||
cpal = { workspace = true }
|
||||
raust-engine = { workspace = true }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,43 @@
|
||||
use clap::Parser;
|
||||
use cpal::traits::DeviceTrait;
|
||||
use raust_engine::{AudioPlayer, Note};
|
||||
|
||||
/// Play a single tone through the default audio output device.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Frequency of the tone to play, in Hertz.
|
||||
#[arg(short, long, default_value = "440.0")]
|
||||
frequency: f32,
|
||||
|
||||
/// How long to play the tone, in milliseconds.
|
||||
#[arg(short, long, default_value = "1000")]
|
||||
length_milli: u64,
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let args = Args::parse();
|
||||
let note = Note {
|
||||
pitch: args.frequency,
|
||||
length: args.length_milli,
|
||||
};
|
||||
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))?;
|
||||
|
||||
audio_player.play()?;
|
||||
std::thread::sleep(std::time::Duration::from_millis(note.length));
|
||||
audio_player.pause()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user