Skip to main content

render_fold

Function render_fold 

Source
pub fn render_fold<S, A>(
    initial_state: S,
    initial_acc: A,
    sample_rate: u32,
    num_samples: usize,
    step: impl Fn(S, f64) -> (f32, S),
    combine: impl Fn(A, f32) -> A,
) -> A
Expand description

Render a buffer and reduce it to a scalar — useful for envelope integration and level metering without allocating the full output.

Folds the same scan loop as render but accumulates into a single value A instead of collecting samples. The combine function receives the running accumulator and each new sample.

§Arguments

  • initial_state — starting DSP state
  • initial_acc — starting accumulator value
  • sample_rate — samples per second
  • num_samples — number of samples to process
  • step(state, t) → (sample, next_state)
  • combine(acc, sample) → next_acc

§Returns

Final accumulated value of type A.

§Example

use prime_render::render_fold;

// Sum all samples from a constant signal of 0.1
let total = render_fold((), 0.0_f32, 44100, 100, |s, _t| (0.1_f32, s), |acc, x| acc + x);
assert!((total - 10.0).abs() < 1e-4);