foldback

Function foldback 

Source
pub fn foldback(props: Value, x: impl Into<ElemNode>) -> Node
Expand description

Recursive foldback shaper helper.

Computes a recursive soft-saturation using fold-back distortion. The shape depends on the threshold and gain parameters.

For best performance with the fast-path update system, supply a key prefix. This allows threshold and amplitude to be updated without rebuilding the graph.

Props:

  • key: optional prefix for stable node identity; enables direct updates via mounted.node_with_key("{key}_thresh") and mounted.node_with_key("{key}_amp")
  • thresh: fold threshold, must be positive
  • amp: output gain, defaults to 1 / thresh

§Example: Keyed foldback with parameter updates

use elemaudio_rs::{Graph, el, extra};
use serde_json::json;

// Create a graph with a keyed foldback
let graph = Graph::new().render(
    extra::foldback(
        json!({
            "key": "shaper",
            "thresh": 0.5,
            "amp": 2.0,
        }),
        el::cycle(el::const_(440.0)),
    )
);

// Mount the graph and get handles for direct updates
let mounted = graph.mount();
let batch = mounted.into_batch();
runtime.execute(&batch);

// Later, update threshold WITHOUT rebuilding the entire graph
if let Some(thresh_node) = mounted.node_with_key("shaper_thresh") {
    let update = thresh_node.set_const_value(0.7);
    runtime.execute(&update);
}

// Similarly for amplitude
if let Some(amp_node) = mounted.node_with_key("shaper_amp") {
    let update = amp_node.set_const_value(1.5);
    runtime.execute(&update);
}