Struct nih_plug::buffer::Buffer

source ·
pub struct Buffer<'a> { /* private fields */ }
Expand description

The audio buffers used during processing. This contains the output audio output buffers with the inputs already copied to the outputs. You can either use the iterator adapters to conveniently and efficiently iterate over the samples, or you can do your own thing using the raw audio buffers.

TODO: This lifetime makes zero sense because you’re going to need unsafe lifetime casts to use this either way. Maybe just get rid of it in favor for raw pointers.

Implementations§

source§

impl<'a> Buffer<'a>

source

pub fn samples(&self) -> usize

Returns the number of samples per channel in this buffer.

source

pub fn channels(&self) -> usize

Returns the number of channels in this buffer.

source

pub fn is_empty(&self) -> bool

Returns true if this buffer does not contain any samples.

source

pub fn as_slice(&mut self) -> &mut [&'a mut [f32]]

Obtain the raw audio buffers.

source

pub fn as_slice_immutable(&self) -> &[&'a mut [f32]]

The same as as_slice(), but for a non-mutable reference. This is usually not needed.

source

pub fn iter_samples<'slice>(&'slice mut self) -> SamplesIter<'slice, 'a>

Iterate over the samples, returning a channel iterator for each sample.

source

pub fn iter_blocks<'slice>( &'slice mut self, max_block_size: usize ) -> BlocksIter<'slice, 'a>

Iterate over the buffer in blocks with the specified maximum size. The ideal maximum block size depends on the plugin in question, but 64 or 128 samples works for most plugins. Since the buffer’s total size may not be cleanly divisible by the maximum size, the returned buffers may have any size in [1, max_block_size]. This is useful when using algorithms that work on entire blocks of audio, like those that would otherwise need to perform expensive per-sample branching or that can use per-sample SIMD as opposed to per-channel SIMD.

The parameter smoothers can also produce smoothed values for an entire block using Smoother::next_block().

You can use this to obtain block-slices from a buffer so you can pass them to a library:

for block in buffer.iter_blocks(128) {
    let mut block_channels = block.into_iter();
    let stereo_slice = &[
        block_channels.next().unwrap(),
        block_channels.next().unwrap(),
    ];

    // Do something cool with `stereo_slice`
}
source

pub unsafe fn set_slices( &mut self, num_samples: usize, update: impl FnOnce(&mut Vec<&'a mut [f32]>) )

Set the slices in the raw output slice vector. This vector needs to be resized to match the number of output channels during the plugin’s initialization. Then during audio processing, these slices should be updated to point to the plugin’s audio buffers. The num_samples argument should match the length of the inner slices.

Safety

The stored slices must point to live data when this object is passed to the plugins’ process function. The rest of this object also assumes all channel lengths are equal. Panics will likely occur if this is not the case.

Trait Implementations§

source§

impl<'a> Default for Buffer<'a>

source§

fn default() -> Buffer<'a>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Buffer<'a>

§

impl<'a> Send for Buffer<'a>

§

impl<'a> Sync for Buffer<'a>

§

impl<'a> Unpin for Buffer<'a>

§

impl<'a> !UnwindSafe for Buffer<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.
§

impl<T> Any for Twhere T: Any,

§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,