pub trait AudioProcessor: Send {
    // Required method
    fn process(
        &mut self,
        inputs: &[AudioRenderQuantum],
        outputs: &mut [AudioRenderQuantum],
        params: AudioParamValues<'_>,
        scope: &RenderScope
    ) -> bool;

    // Provided method
    fn onmessage(&mut self, msg: Box<dyn Any + Send + 'static>) { ... }
}
Expand description

Interface for audio processing code that runs on the audio rendering thread.

Note that the AudioProcessor is typically constructed together with an AudioNode (the user facing object that lives in the control thread). See BaseAudioContext::register.

Check the examples/worklet.rs file for example usage of this trait.

Required Methods§

source

fn process( &mut self, inputs: &[AudioRenderQuantum], outputs: &mut [AudioRenderQuantum], params: AudioParamValues<'_>, scope: &RenderScope ) -> bool

Audio processing function

Arguments
  • inputs: readonly array of input buffers
  • outputs: array of output buffers
  • params: available AudioParams for this processor
  • timestamp: time of the start of this render quantum
  • sample_rate: sample rate of this render quantum
Return value

The return value (bool) of this callback controls the lifetime of the processor.

  • return false when the node only transforms their inputs, and as such can be removed when the inputs are disconnected (e.g. GainNode)
  • return true for some time when the node still outputs after the inputs are disconnected (e.g. DelayNode)
  • return true as long as this node is a source of output (e.g. OscillatorNode)

Provided Methods§

source

fn onmessage(&mut self, msg: Box<dyn Any + Send + 'static>)

Handle incoming messages from the linked AudioNode

By overriding this method you can add a handler for messages sent from the control thread via AudioContextRegistration::post_message. This will not be necessary for most processors.

This method is just a shim of the full MessagePort onmessage functionality of the AudioWorkletProcessor.

Implementors§