This commit is contained in:
brevalferrari 2025-06-04 12:24:39 +02:00
parent 0ccc81551a
commit 9ff44f5182

View file

@ -9,20 +9,25 @@ use bliplib::{
use clap::Parser as _;
use cli::Cli;
use dasp_sample::Sample;
use log::{debug, warn};
use rodio::{OutputStream, Sink, buffer::SamplesBuffer};
fn main() -> anyhow::Result<()> {
env_logger::init();
let cli = Cli::parse();
debug!("options: {cli:?}");
use Cli::*;
match cli {
Play(opts) => {
let (_stream, stream_handle) = OutputStream::try_default()
.context("Failed to find (or use) default audio device")?;
debug!("output stream acquired");
let sink = Sink::try_new(&stream_handle).context("Epic audio playback failure")?;
debug!("audio sink acquired");
let default_variables = HashMap::from([('l', 4f64), ('L', 0.0), ('t', 0.0)]);
debug!("building parser");
let parser = Parser::new(
opts.notes(),
opts.slopes()
@ -33,12 +38,19 @@ fn main() -> anyhow::Result<()> {
.map(|(v, _)| *v)
.collect::<Vec<_>>(),
);
debug!("reading input");
let input = read_to_string(opts.input().get()).context("Failed to read input")?;
debug!("parsing tokens");
let tokens = parser
.parse_all(&input)
.map_err(|e| anyhow!("{e}"))
.context("Failed to parse input")?;
debug!("found {} tokens", tokens.as_ref().len());
if tokens.as_ref().len() == 0 {
warn!("0 tokens parsed");
}
debug!("building compiler");
let compiler = Compiler::from(Context::new(
'L',
'n',
@ -48,14 +60,21 @@ fn main() -> anyhow::Result<()> {
opts.instrument().clone(),
opts.slopes().map(|(_, (a, b))| (*a, b.clone())),
));
debug!("compiling to samples");
let samples: Vec<f32> = compiler
.compile_all(tokens)
.context("Failed to process input tokens")?
.into_iter()
.map(Sample::to_sample)
.collect();
debug!("result: {} samples", samples.len());
if samples.len() == 0 {
warn!("0 samples generated");
}
debug!("appending samples to sink");
sink.append(SamplesBuffer::new(1, SAMPLE_RATE as u32, samples));
debug!("sleeping until end of sink");
sink.sleep_until_end();
}
Export(_opts) => todo!(),