fix precise note length, add test
This commit is contained in:
parent
de97e43b63
commit
9d02a2faaf
1 changed files with 30 additions and 11 deletions
|
@ -5,13 +5,20 @@ use std::{
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use cfg_if::cfg_if;
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
use derive_wrapper::{AsRef, From};
|
use derive_wrapper::{AsRef, From};
|
||||||
use fasteval::{Compiler as _, EvalNamespace, Evaler, Instruction, Slab};
|
use fasteval::{Compiler as _, EvalNamespace, Evaler, Instruction, Slab};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
const SAMPLE_RATE: u16 = 48000;
|
cfg_if! {
|
||||||
|
if #[cfg(test)] {
|
||||||
|
const SAMPLE_RATE: u16 = 10;
|
||||||
|
} else {
|
||||||
|
const SAMPLE_RATE: u16 = 48000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(From, AsRef, Default)]
|
#[derive(From, AsRef, Default)]
|
||||||
#[cfg_attr(test, derive(Debug))]
|
#[cfg_attr(test, derive(Debug))]
|
||||||
|
@ -383,19 +390,15 @@ impl Context {
|
||||||
let curr_t = *self.get('t')?;
|
let curr_t = *self.get('t')?;
|
||||||
if let Some(note) = n {
|
if let Some(note) = n {
|
||||||
let mut result = Vec::new();
|
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();
|
let mut map = self.namespace_generator();
|
||||||
map.insert('n'.to_string(), note as f64);
|
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()?;
|
self.tick()?;
|
||||||
}
|
}
|
||||||
Ok(result)
|
Ok(result)
|
||||||
} else {
|
} else {
|
||||||
while (self.current_length()?) > *self.get('t')? - curr_t {
|
while self.current_length()? > *self.get('t')? - curr_t {
|
||||||
self.tick()?;
|
self.tick()?;
|
||||||
}
|
}
|
||||||
Ok(vec![
|
Ok(vec![
|
||||||
|
@ -452,7 +455,6 @@ mod tests {
|
||||||
[
|
[
|
||||||
('a', 5.0),
|
('a', 5.0),
|
||||||
('t', 0.0),
|
('t', 0.0),
|
||||||
('n', 0.0),
|
|
||||||
('N', 12.0),
|
('N', 12.0),
|
||||||
('L', 0.0),
|
('L', 0.0),
|
||||||
('l', 4.0),
|
('l', 4.0),
|
||||||
|
@ -501,4 +503,21 @@ mod tests {
|
||||||
);
|
);
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue