Struct hexodsp::synth_constructor::SynthConstructor
source · [−]pub struct SynthConstructor {
config: NodeConfigurator,
exec: Option<NodeExecutor>,
nodes: HashMap<NodeId, Box<NodeConfig>>,
graph_ordering: NodeGraphOrdering,
}
Expand description
A convenient highlevel API to HexoDSP for building custom synthesizers from HexoDSP nodes.
This API combines with the crate::build API for defining compile time checked HexoDSP graphs in Rust.
You also have the possibility of providing your own DSP nodes using the SynthConstructor::set_dynamic_node1x1 function and the crate::DynamicNode1x1 trait.
Fields
config: NodeConfigurator
exec: Option<NodeExecutor>
nodes: HashMap<NodeId, Box<NodeConfig>>
graph_ordering: NodeGraphOrdering
Implementations
sourceimpl SynthConstructor
impl SynthConstructor
pub fn new() -> Self
sourcepub fn executor(&mut self) -> Option<NodeExecutor>
pub fn executor(&mut self) -> Option<NodeExecutor>
Retrieves the NodeExecutor for handing to your audio thread.
fn walk_upload(
&mut self,
node: &ConstructorNode,
only_update_params: bool
) -> Result<bool, SynthError>
fn update_node_params(&mut self, node_id: NodeId) -> Result<bool, SynthError>
sourcepub fn update_params(
&mut self,
node: &dyn ConstructorNodeBuilder
) -> Result<bool, SynthError>
pub fn update_params(
&mut self,
node: &dyn ConstructorNodeBuilder
) -> Result<bool, SynthError>
Try to update only DSP node parameters. If you did any graph changes, a new DSP graph will
be uploaded on the fly though. Call this if you only want to update specific parameters.
The function returns a boolean flag, that is true
if the DSP graph was reuploaded.
It returns an error if something is wrong with the input graph to this function.
use hexodsp::SynthConstructor;
use hexodsp::build::*;
let mut sc = SynthConstructor::new();
// Set the `freq` parameter of the BOsc(0) node to 400Hz:
let uploaded_new_graph = sc.update_params(&bosc(0).set().freq(400.0)).unwrap();
sourcepub fn set_dynamic_node1x1(&self, index: usize, node: Box<dyn DynamicNode1x1>)
pub fn set_dynamic_node1x1(&self, index: usize, node: Box<dyn DynamicNode1x1>)
Updates the dynamic node for the Rust1x1 nodes. The index
refers to the
instance NodeId::Rust1x1(index)
. The new DSP code is preserved for the first use
of that node, and can also be swapped out at runtime anytime while the DSP graph is working.
use hexodsp::{SynthConstructor, DynamicNode1x1, DynNode1x1Context};
use hexodsp::build::*;
let mut sc = SynthConstructor::new();
// Setup the input of the Rust1x1 node to receive an 880Hz sine.
let sine_gen_out = &sin(0).set().freq(880.0).output().sig();
let r1x1 = rust1x1(0).input().inp(sine_gen_out);
// you can provide up to 4 extra parameters (alpha, beta, gamma, delta):
let r1x1 = r1x1.set().alpha(0.75);
// You may replace this function anytime at runtime:
sc.set_dynamic_node1x1(0, Box::new(|inp: &[f32], out: &mut [f32], ctx: &DynNode1x1Context| {
let alpha = ctx.alpha_slice();
for (i, in_sample) in inp.iter().enumerate() {
out[i] = in_sample * alpha[i];
}
// This sets an atomic float that can be read out using SynthConstructor::led_value()!
ctx.led_value().set(out[0]);
}));
sc.upload(&out(0).input().ch1(&r1x1.output().sig()));
sourcepub fn phase_value(&self, ni: &NodeId) -> f32
pub fn phase_value(&self, ni: &NodeId) -> f32
Returns the atomic phase value for NodeId.
pub fn upload(
&mut self,
node: &dyn ConstructorNodeBuilder
) -> Result<(), SynthError>
sourcepub fn poll(&mut self)
pub fn poll(&mut self)
Updates internal states. For instance for providing feedback values for SynthConstructor::output_feedback and SynthConstructor::output_feedback_minmax.
sourcepub fn output_feedback(&self, node_id: &NodeId, out: u8) -> Option<f32>
pub fn output_feedback(&self, node_id: &NodeId, out: u8) -> Option<f32>
Retrieves the output port feedback for a specific output of the given NodeId.
Make sure to call SynthConstructor::poll regularily to update the feedback values.
See also NodeConfigurator::out_fb_for.
use hexodsp::{NodeId, SynthConstructor};
let mut sc = SynthConstructor::new();
// Setup stuff with sc here..
// In a loop call eg.:
sc.poll();
let node_id = NodeId::BOsc(0);
let out_idx = NodeId::BOsc(0).out("sig").unwrap();
if let Some(value) = sc.output_feedback(&node_id, out_idx) {
println!("Current min/max output of BOsc(0).sig={}", value);
}
sourcepub fn output_feedback_minmax(&mut self, node_id: &NodeId, out: u8) -> (f32, f32)
pub fn output_feedback_minmax(&mut self, node_id: &NodeId, out: u8) -> (f32, f32)
Retrieves the output port feedback for a specific output of the given NodeId. The output is slightly filtered and provides the min and max values over a few of the most recent calls to SynthConstructor::poll.
Make sure to call SynthConstructor::poll regularily to update the feedback values.
See also NodeConfigurator::out_fb_for.
use hexodsp::{NodeId, SynthConstructor};
let mut sc = SynthConstructor::new();
// Setup stuff with sc here..
// In a loop call eg.:
sc.poll();
let node_id = NodeId::BOsc(0);
let out_idx = NodeId::BOsc(0).out("sig").unwrap();
let (min, max) = sc.output_feedback_minmax(&node_id, out_idx);
println!("Current min/max output of BOsc(0).sig={}/{}", min, max);