Feat: polyphony + iterator reset

This commit is contained in:
2026-02-02 00:33:46 +01:00
parent 8452033473
commit ccce0df79d
9 changed files with 132 additions and 145 deletions

View File

@@ -42,13 +42,6 @@ fn get_sounds(outputs: &[String]) -> Vec<String> {
.collect()
}
fn get_param(outputs: &[String], param: &str) -> Vec<f64> {
outputs
.iter()
.map(|o| parse_params(o).get(param).copied().unwrap_or(0.0))
.collect()
}
const EPSILON: f64 = 1e-9;
fn approx_eq(a: f64, b: f64) -> bool {
@@ -156,7 +149,7 @@ fn at_single_delta() {
#[test]
fn at_list_deltas() {
let outputs = expect_outputs(r#"0 0.5 2 at "kick" s ."#, 2);
let outputs = expect_outputs(r#"0 0.5 at "kick" s ."#, 2);
let deltas = get_deltas(&outputs);
let step_dur = 0.125;
assert!(approx_eq(deltas[0], 0.0), "expected delta 0, got {}", deltas[0]);
@@ -165,7 +158,7 @@ fn at_list_deltas() {
#[test]
fn at_three_deltas() {
let outputs = expect_outputs(r#"0 0.33 0.67 3 at "kick" s ."#, 3);
let outputs = expect_outputs(r#"0 0.33 0.67 at "kick" s ."#, 3);
let deltas = get_deltas(&outputs);
let step_dur = 0.125;
assert!(approx_eq(deltas[0], 0.0), "expected delta 0");
@@ -175,70 +168,26 @@ fn at_three_deltas() {
#[test]
fn at_persists_across_emits() {
let outputs = expect_outputs(r#"0 0.5 2 at "kick" s . "hat" s ."#, 4);
let outputs = expect_outputs(r#"0 0.5 at "kick" s . "hat" s ."#, 4);
let sounds = get_sounds(&outputs);
assert_eq!(sounds, vec!["kick", "kick", "hat", "hat"]);
}
#[test]
fn tcycle_basic() {
let outputs = expect_outputs(r#"0 0.5 0.75 3 at 60 64 67 3 tcycle note sine s ."#, 3);
let notes = get_param(&outputs, "note");
assert_eq!(notes, vec![60.0, 64.0, 67.0]);
}
#[test]
fn tcycle_wraps() {
let outputs = expect_outputs(r#"0 0.33 0.67 3 at 60 64 2 tcycle note sine s ."#, 3);
let notes = get_param(&outputs, "note");
assert_eq!(notes, vec![60.0, 64.0, 60.0]);
}
#[test]
fn tcycle_with_sound() {
let outputs = expect_outputs(r#"0 0.5 2 at kick hat 2 tcycle s ."#, 2);
let sounds = get_sounds(&outputs);
assert_eq!(sounds, vec!["kick", "hat"]);
}
#[test]
fn tcycle_multiple_params() {
let outputs = expect_outputs(r#"0 0.5 0.75 3 at 60 64 67 3 tcycle note 0.5 1.0 2 tcycle gain sine s ."#, 3);
let notes = get_param(&outputs, "note");
let gains = get_param(&outputs, "gain");
assert_eq!(notes, vec![60.0, 64.0, 67.0]);
assert_eq!(gains, vec![0.5, 1.0, 0.5]);
}
#[test]
fn at_reset_with_zero() {
let outputs = expect_outputs(r#"0 0.5 2 at "kick" s . 0.0 at "hat" s ."#, 3);
let outputs = expect_outputs(r#"0 0.5 at "kick" s . 0.0 at "hat" s ."#, 3);
let sounds = get_sounds(&outputs);
assert_eq!(sounds, vec!["kick", "kick", "hat"]);
}
#[test]
fn tcycle_records_selected_spans() {
use cagire::forth::ExecutionTrace;
let f = forth();
let mut trace = ExecutionTrace::default();
let script = r#"0 0.5 2 at kick hat 2 tcycle s ."#;
f.evaluate_with_trace(script, &default_ctx(), &mut trace).unwrap();
// Should have 4 selected spans:
// - 2 for at deltas (0 and 0.5)
// - 2 for tcycle sound values (kick and hat)
assert_eq!(trace.selected_spans.len(), 4, "expected 4 selected spans (2 at + 2 tcycle)");
}
#[test]
fn at_records_selected_spans() {
use cagire::forth::ExecutionTrace;
let f = forth();
let mut trace = ExecutionTrace::default();
let script = r#"0 0.5 0.75 3 at "kick" s ."#;
let script = r#"0 0.5 0.75 at "kick" s ."#;
f.evaluate_with_trace(script, &default_ctx(), &mut trace).unwrap();
// Should have 6 selected spans: 3 for at deltas + 3 for sound (one per emit)