This commit is contained in:
brevalferrari 2025-06-06 01:04:44 +02:00
parent 41aeef83d4
commit f55a8c7e47

View file

@ -15,7 +15,7 @@ use clap::Parser as _;
use cli::Cli;
use dasp_sample::Sample;
use hound::{SampleFormat, WavSpec, WavWriter};
use log::{debug, warn};
use log::{debug, info, warn};
use rodio::{OutputStream, Sink, buffer::SamplesBuffer};
use crate::cli::{ExportOpts, PlayOpts};
@ -29,22 +29,22 @@ fn main() -> anyhow::Result<()> {
Play(opts) => {
let (_stream, stream_handle) = OutputStream::try_default()
.context("Failed to find (or use) default audio device")?;
debug!("output stream acquired");
info!("output stream acquired");
let sink = Sink::try_new(&stream_handle).context("Epic audio playback failure")?;
debug!("audio sink acquired");
info!("audio sink acquired");
let samples: Vec<f32> = parse_and_compile(&opts)?
.into_iter()
.map(Sample::to_sample)
.collect();
debug!("result: {} samples", samples.len());
info!("result: {} samples", samples.len());
if samples.is_empty() {
warn!("0 samples generated");
}
debug!("appending samples to sink");
info!("appending samples to sink");
sink.append(SamplesBuffer::new(1, SAMPLE_RATE as u32, samples));
debug!("sleeping until end of sink");
info!("sleeping until end of sink");
sink.sleep_until_end();
}
Export(ExportOpts {
@ -53,6 +53,7 @@ fn main() -> anyhow::Result<()> {
output,
}) => {
let samples = parse_and_compile(&playopts)?;
info!("result: {} samples", samples.len());
let mut buff = Cursor::new(Vec::with_capacity(samples.len() * 8));
{
let mut writer = WavWriter::new(
@ -75,6 +76,7 @@ fn main() -> anyhow::Result<()> {
.map(Box::new)
.map(|b| b as Box<dyn Write>)
.unwrap_or(Box::new(stdout()));
info!("writing samples to output");
writer.write_all(buff.get_ref())?;
}
Memo(_opts) => todo!(),
@ -91,7 +93,7 @@ fn parse_and_compile(opts: &PlayOpts) -> anyhow::Result<Vec<f64>> {
('N', opts.notes().len() as f64),
];
debug!("building parser");
info!("building parser");
let parser = Parser::new(
opts.notes(),
opts.slopes()
@ -103,19 +105,20 @@ fn parse_and_compile(opts: &PlayOpts) -> anyhow::Result<Vec<f64>> {
.map(|(v, _)| *v)
.collect::<Vec<_>>(),
);
debug!("reading input");
info!("reading input");
let input = read_to_string(opts.input().get()).context("Failed to read input")?;
debug!("parsing tokens");
info!("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());
info!("found {} tokens", tokens.as_ref().len());
if tokens.as_ref().is_empty() {
warn!("0 tokens parsed");
}
debug!("tokens: {:#?}", tokens.as_ref());
debug!("building compiler");
info!("building compiler");
let compiler = Compiler::from(Context::new(
'L'.to_string(),
'n'.to_string(),
@ -128,7 +131,7 @@ fn parse_and_compile(opts: &PlayOpts) -> anyhow::Result<Vec<f64>> {
.map(|(_, (a, b))| (a.to_string(), b.clone()))
.chain(once(('L'.to_string(), opts.length().clone()))),
));
debug!("compiling to samples");
info!("compiling to samples");
compiler
.compile_all(tokens)
.inspect(|v| {