Skip to main content

LinearModel

Struct LinearModel 

Source
pub struct LinearModel {
    pub weights: AlignedVec<f32>,
    pub bias: f32,
    pub history: MirroredBuffer<f32>,
    pub write_pos: usize,
    pub receptive_field: usize,
    pub prewarm_on_reset: bool,
    pub implementation: LinearImplementation,
    pub mode: LinearMode,
    /* private fields */
}
Expand description

Linear Model — lightweight FIR-based neural model.

This is the simplest NAM architecture: a single linear layer (dot product) applied over the recent sample history with an optional scalar bias.

§RT-Safety

  • Zero allocation on the hot-path (process).
  • Uses MirroredBuffer for branch-free ring buffer access.
  • No locks, no unwrap(), no I/O.

Fields§

§weights: AlignedVec<f32>

FIR filter weights stored in reversed order (matching C++ internal layout). JSON weights are reversed on construction, so that dot(weights, oldest_to_newest_window) produces the FIR convolution. 64-byte aligned for AVX2/AVX-512 SIMD loads.

§bias: f32

Scalar bias added after the dot product.

§history: MirroredBuffer<f32>

Circular buffer of past input samples, backed by mirrored memory mapping for branch-free contiguous access across the wrap boundary.

§write_pos: usize

Current write position in the history ring buffer (0..receptive_field-1).

§receptive_field: usize

Number of input samples in the receptive field (= weights.len()).

§prewarm_on_reset: bool

Whether to execute prewarm during reset(). Default: true.

§implementation: LinearImplementation

Convolution implementation mode as configured in the JSON.

§mode: LinearMode

Runtime convolution mode — Direct or Fft with partitioned FFT state.

Implementations§

Source§

impl LinearModel

Source

pub unsafe fn process(&mut self, input: &[f32], output: &mut [f32])

Processes a block of audio samples.

§Safety

self.weights must be 64-byte aligned.

Source

pub fn prewarm(&mut self, _num_samples: usize)

Fills the history buffer with zeros, resets the write pointer, and reinitializes the FFT state (if active).

Source

pub fn reset(&mut self, _sample_rate: u32, _max_buffer_size: usize)

Resets internal state: zeroes the history buffer, write pointer, and FFT state (if active).

Source§

impl LinearModel

Source

pub fn new( weights: Vec<f32>, bias: f32, implementation: LinearImplementation, ) -> Result<Self>

Creates a new LinearModel with the given weights, bias, and implementation.

Weights are expected in forward-time order as stored in the .nam JSON (w[0] is the response at the current sample). They are reversed internally to match the C++ nam::Linear layout.

implementation controls the convolution strategy (Auto, Direct, Fft) as configured in the model’s JSON:

  • Direct: always uses time-domain dot product.
  • Auto: uses FFT when receptive_field >= 256, otherwise Direct.
  • Fft: uses FFT partitioned convolution; falls back to Direct with a warning if the receptive field is too small (< 256).

Allocates the MirroredBuffer for the input history. The buffer is initialized to zero (silence) by the operating system via mmap.

§Errors

Returns std::io::Error if the MirroredBuffer allocation fails (e.g., out of memory or virtual address space).

Trait Implementations§

Source§

impl NamModel for LinearModel

Source§

fn process(&mut self, input: &[f32], output: &mut [f32])

Invoked by the DSP RT-Thread to process acoustic sample blocks (Float32).
Source§

fn prewarm(&mut self, num_samples: usize)

“Heats up” the virtual tubes of the neural engine (prewarm).
Source§

fn reset(&mut self, sample_rate: u32, max_buffer_size: usize) -> Result<()>

Resets the model’s internal state with a new sample rate and max buffer size. Read more
Source§

fn prewarm_samples(&self) -> usize

Returns the number of samples needed to fully stabilize the model’s internal state (receptive field / recurrent memory depth). Read more
Source§

fn prewarm_on_reset(&self) -> bool

Returns whether prewarm should be executed on reset(). Read more
Source§

fn set_prewarm_on_reset(&mut self, val: bool)

Sets whether prewarm should be executed on reset(). Read more
Source§

fn set_max_buffer_size(&mut self, _max_buf: usize) -> Result<()>

Reallocates internal buffers to support the given maximum block size. Read more
Source§

fn slimmable_breakpoints(&self) -> Vec<f64>

Returns the breakpoints at which slimmable quality transitions occur. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(value: T, _simd: S) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

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

Source§

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>,

Source§

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.