fix precise note length, add test

This commit is contained in:
brevalferrari 2025-05-28 20:56:48 +02:00
parent de97e43b63
commit 9d02a2faaf

View file

@ -5,13 +5,20 @@ use std::{
str::FromStr,
};
use cfg_if::cfg_if;
use derive_new::new;
use derive_wrapper::{AsRef, From};
use fasteval::{Compiler as _, EvalNamespace, Evaler, Instruction, Slab};
use lazy_static::lazy_static;
use thiserror::Error;
cfg_if! {
if #[cfg(test)] {
const SAMPLE_RATE: u16 = 10;
} else {
const SAMPLE_RATE: u16 = 48000;
}
}
#[derive(From, AsRef, Default)]
#[cfg_attr(test, derive(Debug))]
@ -383,19 +390,15 @@ impl Context {
let curr_t = *self.get('t')?;
if let Some(note) = n {
let mut result = Vec::new();
while (self.current_length()? * SAMPLE_RATE as f64) > *self.get('t')? - curr_t {
{
result.push(self.eval_with(&self.instrument, &mut {
let mut map = self.namespace_generator();
map.insert('n'.to_string(), note as f64);
map
})?);
}
while self.current_length()? > *self.get('t')? - curr_t + (1f64 / SAMPLE_RATE as f64) {
result.push(self.eval_with(&self.instrument, &mut map)?);
self.tick()?;
}
Ok(result)
} else {
while (self.current_length()?) > *self.get('t')? - curr_t {
while self.current_length()? > *self.get('t')? - curr_t {
self.tick()?;
}
Ok(vec![
@ -452,7 +455,6 @@ mod tests {
[
('a', 5.0),
('t', 0.0),
('n', 0.0),
('N', 12.0),
('L', 0.0),
('l', 4.0),
@ -501,4 +503,21 @@ mod tests {
);
Ok(())
}
#[test]
fn reproducible_note() -> Result<(), CompilerError> {
let note = Note(3);
let mut compiler = Compiler::from(context_generator());
compiler = compiler.step(note)?;
let first = compiler.0.result.clone();
*compiler.0.get_mut('t')? = 0.0;
compiler.0.result.clear();
compiler = compiler.step(note)?;
let second = compiler.0.result.clone();
assert_eq!(first, second);
Ok(())
}
}