Skip to main content

render

Function render 

Source
pub fn render<S>(
    initial_state: S,
    sample_rate: u32,
    num_samples: usize,
    step: impl Fn(S, f64) -> (f32, S),
) -> Vec<f32>
Expand description

Render a mono audio buffer by folding a pure step function over time.

This is the core ADVANCE operation for Score’s temporal assembly thesis. The output is a deterministic function of initial_state, sample_rate, and step — no hidden state, no I/O, no side effects.

§Math

dt = 1 / sample_rate
output[n], state_{n+1} = step(state_n, n * dt)

§Arguments

  • initial_state — starting DSP state (e.g. oscillator phase, envelope stage)
  • sample_rate — samples per second (e.g. 44100)
  • num_samples — number of output samples to generate
  • step — pure function: (state, time_in_seconds) → (sample, next_state)

§Returns

Vec<f32> of exactly num_samples mono samples in [-1.0, 1.0].

§Edge cases

  • num_samples == 0 → returns empty Vec
  • sample_rate == 0dt = infinity; step receives f64::INFINITY for t > 0

§Example

// Render 4 samples of a 440 Hz sine at 4 Hz (for clarity)
use prime_render::render;
use std::f64::consts::TAU;

let samples = render(0.0_f64, 4, 4, |phase, _t| {
    let sample = phase.sin() as f32;
    let next = (phase + TAU * 440.0 / 4.0) % TAU;
    (sample, next)
});
assert_eq!(samples.len(), 4);