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§

source§

impl DSPFunction

source

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")).

source

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.

source

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.

source

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.

source

pub fn reset(&mut self)

If the DSP state needs to be resetted, call this on the audio thread.

source

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.

source

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!

source

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();
source

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.

source

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.

source

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)).

source

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.

source

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.

Trait Implementations§

source§

impl Drop for DSPFunction

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for DSPFunction

source§

impl Sync for DSPFunction

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.