Struct synfx_dsp_jit::DSPFunction
source · [−]pub struct DSPFunction { /* private fields */ }
Expand description
This is the result of the JIT compiled crate::ast::ASTNode tree. You can send this structure to the audio backend thread and execute it using DSPFunction::exec.
To execute this DSPFunction properly, you have to call DSPFunction::init once the newly allocated structure is received by the DSP executing thread.
If the sample rate changes or the stateful DSP stuff must be resetted, you should call DSPFunction::reset or DSPFunction::set_sample_rate. Of course also only on the DSP executing thread.
Implementations
sourceimpl DSPFunction
impl DSPFunction
sourcepub fn init(&mut self, srate: f64, previous_function: Option<&DSPFunction>)
pub fn init(&mut self, srate: f64, previous_function: Option<&DSPFunction>)
This function must be called before DSPFunction::exec! otherwise your states might not be properly initialized or preserved.
If you recompiled a function, pass the old one on the audio thread to
the previous_function
parameter here. It will take care of preserving
state, such as persistent variables (those that start with “*”: crate::build::var("*abc")
).
sourcepub fn swap_buffer(
&mut self,
index: usize,
new_buf: &mut Vec<f64>,
preserve_old_samples: bool
) -> Result<(), ()>
pub fn swap_buffer(
&mut self,
index: usize,
new_buf: &mut Vec<f64>,
preserve_old_samples: bool
) -> Result<(), ()>
Swaps out the buffer at the given index with the new buffer. The contents
of the Vec will be swapped with the current contents of the buffer, unless
you specify preserve_old_samples
which will try to preserve as many samples
from the previous buffer as possible.
sourcepub fn swap_table(
&mut self,
index: usize,
new_table: &mut Arc<Vec<f32>>
) -> Result<(), ()>
pub fn swap_table(
&mut self,
index: usize,
new_table: &mut Arc<Vec<f32>>
) -> Result<(), ()>
Swaps out the table at the given index with the new table.
sourcepub fn set_sample_rate(&mut self, srate: f64)
pub fn set_sample_rate(&mut self, srate: f64)
If the audio thread changes the sampling rate, call this function, it will update the DSPState and reset all [DSPNodeState]s.
sourcepub fn get_dsp_state_ptr(&self) -> *mut DSPState
pub fn get_dsp_state_ptr(&self) -> *mut DSPState
Use this to retrieve a pointer to the DSPState to access it between calls to DSPFunction::exec.
sourcepub unsafe fn with_dsp_state<R, F: FnMut(*mut DSPState) -> R>(
&mut self,
f: F
) -> R
pub unsafe fn with_dsp_state<R, F: FnMut(*mut DSPState) -> R>(
&mut self,
f: F
) -> R
Use this to access the DSPState pointer between calls to DSPFunction::exec.
Safety
You must not create multiple aliasing references from that DSP state!
sourcepub unsafe fn with_node_state<T, R, F: FnMut(*mut T) -> R>(
&mut self,
node_state_uid: u64,
f: F
) -> Result<R, ()>
pub unsafe fn with_node_state<T, R, F: FnMut(*mut T) -> R>(
&mut self,
node_state_uid: u64,
f: F
) -> Result<R, ()>
Use this to access the state of a specific DSP node state pointer between calls to DSPFunction::exec.
The node_state_uid
and the type you pass here must match! It’s your responsibility
to make sure this works!
Safety
You absolutely must know which ID has which DSPNodeType, otherwise this will badly go wrong!
use synfx_dsp_jit::*;
use synfx_dsp_jit::build::*;
use synfx_dsp_jit::stdlib::AccumNodeState;
let (ctx, mut fun) = instant_compile_ast(call("accum", 21, &[var("in1"), literal(0.0)])).unwrap();
fun.init(44100.0, None);
// Accumulate 42.0 here:
fun.exec_2in_2out(21.0, 0.0);
fun.exec_2in_2out(21.0, 0.0);
unsafe {
// Check 42.0 and set 99.0
fun.with_node_state(21, |state: *mut AccumNodeState| {
assert!(((*state).value - 42.0).abs() < 0.0001);
(*state).value = 99.0;
})
};
// Accumulate up to 100.0 here:
let (_, _, ret) = fun.exec_2in_2out(1.0, 0.0);
assert!((ret - 100.0).abs() < 0.0001);
ctx.borrow_mut().free();
sourcepub fn get_node_state_ptr(&self, node_state_uid: u64) -> Option<*mut u8>
pub fn get_node_state_ptr(&self, node_state_uid: u64) -> Option<*mut u8>
Retrieves the DSP node state pointer for a certain unique node state id.
Safety
You are responsible afterwards for knowing what type the actual pointer is of.
sourcepub fn exec_2in_2out(&mut self, in1: f64, in2: f64) -> (f64, f64, f64)
pub fn exec_2in_2out(&mut self, in1: f64, in2: f64) -> (f64, f64, f64)
Helper function, it lets you specify only the contents of the parameters
"in1"
and "in2"
. It also returns you the values for "&sig1"
and "&sig2"
after execution. The third value is the return value of the compiled expression.
sourcepub fn exec(
&mut self,
in1: f64,
in2: f64,
alpha: f64,
beta: f64,
delta: f64,
gamma: f64,
sig1: &mut f64,
sig2: &mut f64
) -> f64
pub fn exec(
&mut self,
in1: f64,
in2: f64,
alpha: f64,
beta: f64,
delta: f64,
gamma: f64,
sig1: &mut f64,
sig2: &mut f64
) -> f64
Executes the machine code and provides the following parameters in order:
"in1", "in2", "alpha", "beta", "delta", "gamma", "&sig1", "&sig2"
It returns the return value of the computation. For addition outputs you can
write to "&sig1"
or "&sig2"
with for instance: assign(var("&sig1"), literal(10.0))
.
sourcepub fn access_persistent_var(&mut self, idx: usize) -> Option<&mut f64>
pub fn access_persistent_var(&mut self, idx: usize) -> Option<&mut f64>
Gives you access to the persistent variables. To get the index of the persistent variable you must use DSPNodeContext::get_persistent_variable_index_by_name.
sourcepub fn has_dsp_node_state_uid(&self, uid: u64) -> bool
pub fn has_dsp_node_state_uid(&self, uid: u64) -> bool
Checks if the DSP function actually has the state for a certain unique DSP node state ID.