Skip to main content

Mono

Struct Mono 

Source
pub struct Mono<S: Sample> { /* private fields */ }
Expand description

A mono (single-channel) audio block that owns its data.

This is a simplified version of the multi-channel audio blocks, optimized for mono audio processing with a streamlined API that doesn’t require channel indexing.

  • Layout: [sample0, sample1, sample2, ...]
  • Interpretation: A simple sequence of samples representing a single audio channel.
  • Usage: Ideal for mono audio processing, side-chain signals, or any single-channel audio data.

§Example

use audio_blocks::*;

let mut block = Mono::new(512);

// Fill with a simple ramp
for (i, sample) in block.samples_mut().iter_mut().enumerate() {
    *sample = i as f32;
}

assert_eq!(block.sample(0), 0.0);
assert_eq!(block.sample(511), 511.0);

Implementations§

Source§

impl<S: Sample + Default> Mono<S>

Source

pub fn new(num_frames: usize) -> Self

Creates a new mono audio block with the specified number of frames.

Allocates memory for a new mono audio block with exactly the specified number of frames. The block is initialized with the default value (zero) for the sample type.

Do not use in real-time processes!

§Arguments
  • num_frames - The number of frames (samples)
§Example
use audio_blocks::{mono::Mono, AudioBlock};

let block = Mono::<f32>::new(1024);
assert_eq!(block.num_frames(), 1024);
Source§

impl<S: Sample> Mono<S>

Source

pub fn from_slice(samples: &[S]) -> Self

Creates a new mono audio block from a slice of samples.

Copies the provided slice into a new owned mono block.

§Warning

This function allocates memory and should not be used in real-time audio processing contexts.

§Arguments
  • samples - The slice of samples to copy
Source

pub fn from_slice_limited(samples: &[S], num_frames_visible: usize) -> Self

Creates a new mono audio block from a slice of samples.

Copies the provided slice into a new owned mono block.

§Warning

This function allocates memory and should not be used in real-time audio processing contexts.

§Arguments
  • samples - The slice of samples to copy
  • num_frames_visible - Number of audio frames to expose
Source

pub fn from_block(block: &impl AudioBlock<S>) -> Self

Creates a new mono audio block by copying data from another AudioBlock.

Extracts the first channel from any AudioBlock implementation. If the source block has no channels, creates an empty mono block.

§Warning

This function allocates memory and should not be used in real-time audio processing contexts.

§Arguments
  • block - The source audio block to copy data from (first channel will be used)
§Panics

Panics if the source block has zero channels.

Source

pub fn sample(&self, frame: usize) -> S

Returns the sample at the specified frame index.

§Panics

Panics if frame index is out of bounds.

Source

pub fn sample_mut(&mut self, frame: usize) -> &mut S

Returns a mutable reference to the sample at the specified frame index.

§Panics

Panics if frame index is out of bounds.

Source

pub fn samples(&self) -> &[S]

Provides direct access to the underlying samples as a slice.

Returns only the visible samples (up to num_frames).

Source

pub fn samples_mut(&mut self) -> &mut [S]

Provides direct mutable access to the underlying samples as a slice.

Returns only the visible samples (up to num_frames).

Source

pub fn raw_data(&self) -> &[S]

Provides direct access to all allocated memory, including reserved capacity.

This gives access to the full allocated buffer, including any frames beyond the visible range.

Source

pub fn raw_data_mut(&mut self) -> &mut [S]

Provides direct mutable access to all allocated memory, including reserved capacity.

This gives mutable access to the full allocated buffer, including any frames beyond the visible range.

Source

pub fn view(&self) -> MonoView<'_, S>

Creates a view of this mono audio block.

Source

pub fn view_mut(&mut self) -> MonoViewMut<'_, S>

Creates a mutable view of this mono audio block.

Trait Implementations§

Source§

impl<S: Sample> AudioBlock<S> for Mono<S>

Source§

type PlanarView = [S; 0]

Source§

fn num_channels(&self) -> u16

Returns the number of active audio channels.
Source§

fn num_frames(&self) -> usize

Returns the number of audio frames (samples per channel).
Source§

fn num_channels_allocated(&self) -> u16

Returns the total number of channels allocated in memory. Read more
Source§

fn num_frames_allocated(&self) -> usize

Returns the total number of frames allocated in memory. Read more
Source§

fn layout(&self) -> BlockLayout

Returns the memory layout of this audio block (interleaved, sequential, or planar).
Source§

fn sample(&self, channel: u16, frame: usize) -> S

Returns the sample value at the specified channel and frame position. Read more
Source§

fn channel_iter(&self, channel: u16) -> impl ExactSizeIterator<Item = &S>

Returns an iterator over all samples in the specified channel. Read more
Source§

fn channels_iter( &self, ) -> impl ExactSizeIterator<Item = impl ExactSizeIterator<Item = &S>>

Returns an iterator that yields an iterator for each channel.
Source§

fn frame_iter(&self, frame: usize) -> impl ExactSizeIterator<Item = &S>

Returns an iterator over all samples in the specified frame (across all channels). Read more
Source§

fn frames_iter( &self, ) -> impl ExactSizeIterator<Item = impl ExactSizeIterator<Item = &S>>

Returns an iterator that yields an iterator for each frame.
Source§

fn as_view(&self) -> impl AudioBlock<S>

Creates a non-owning view of this audio block. Read more
Source§

fn as_interleaved_view(&self) -> Option<InterleavedView<'_, S>>

Attempts to downcast this generic audio block to a concrete interleaved view. This enables access to frame slices and the underlying raw data. Read more
Source§

fn as_planar_view(&self) -> Option<PlanarView<'_, S, Self::PlanarView>>

Attempts to downcast this generic audio block to a concrete planar view. This enables access to frame slices and the underlying raw data. Read more
Source§

fn as_sequential_view(&self) -> Option<SequentialView<'_, S>>

Attempts to downcast this generic audio block to a concrete sequential view. This enables access to frame slices and the underlying raw data. Read more
Source§

impl<S: Sample> AudioBlockMut<S> for Mono<S>

Source§

type PlanarViewMut = [S; 0]

Source§

fn set_num_channels_visible(&mut self, num_channels: u16)

Sets the visible size of the audio block to the specified number of channels. Read more
Source§

fn set_num_frames_visible(&mut self, num_frames: usize)

Sets the visible size of the audio block to the specified number of frames. Read more
Source§

fn sample_mut(&mut self, channel: u16, frame: usize) -> &mut S

Returns a mutable reference to the sample at the specified channel and frame position. Read more
Source§

fn channel_iter_mut( &mut self, channel: u16, ) -> impl ExactSizeIterator<Item = &mut S>

Returns a mutable iterator over all samples in the specified channel. Read more
Source§

fn channels_iter_mut( &mut self, ) -> impl ExactSizeIterator<Item = impl ExactSizeIterator<Item = &mut S>>

Returns a mutable iterator that yields mutable iterators for each channel.
Source§

fn frame_iter_mut( &mut self, frame: usize, ) -> impl ExactSizeIterator<Item = &mut S>

Returns a mutable iterator over all samples in the specified frame (across all channels). Read more
Source§

fn frames_iter_mut( &mut self, ) -> impl ExactSizeIterator<Item = impl ExactSizeIterator<Item = &mut S>>

Returns a mutable iterator that yields mutable iterators for each frame.
Source§

fn as_view_mut(&mut self) -> impl AudioBlockMut<S>

Creates a non-owning mutable view of this audio block. Read more
Source§

fn set_visible(&mut self, num_channels: u16, num_frames: usize)

Sets the visible size of the audio block to the specified number of channels and frames. Read more
Source§

fn as_interleaved_view_mut(&mut self) -> Option<InterleavedViewMut<'_, S>>

Attempts to downcast this generic audio block to a concrete interleaved view. This enables access to frame slices and the underlying raw data. Read more
Source§

fn as_planar_view_mut( &mut self, ) -> Option<PlanarViewMut<'_, S, Self::PlanarViewMut>>

Attempts to downcast this generic audio block to a concrete planar view. This enables access to frame slices and the underlying raw data. Read more
Source§

fn as_sequential_view_mut(&mut self) -> Option<SequentialViewMut<'_, S>>

Attempts to downcast this generic audio block to a concrete sequential view. This enables access to frame slices and the underlying raw data. Read more
Source§

impl<S: Clone + Sample> Clone for Mono<S>

Source§

fn clone(&self) -> Mono<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Sample + Debug> Debug for Mono<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Default + Sample> Default for Mono<S>

Source§

fn default() -> Mono<S>

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

Auto Trait Implementations§

§

impl<S> Freeze for Mono<S>

§

impl<S> RefUnwindSafe for Mono<S>
where S: RefUnwindSafe,

§

impl<S> Send for Mono<S>
where S: Send,

§

impl<S> Sync for Mono<S>
where S: Sync,

§

impl<S> Unpin for Mono<S>

§

impl<S> UnsafeUnpin for Mono<S>

§

impl<S> UnwindSafe for Mono<S>
where S: UnwindSafe,

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<S, B> AudioBlockOps<S> for B
where S: Sample, B: AudioBlock<S>,

Source§

fn mix_to_mono(&self, dest: &mut MonoViewMut<'_, S>) -> Option<usize>
where S: AddAssign + Div<Output = S> + From<u16>,

Mix all channels to mono by averaging them. Only processes min(src_frames, dst_frames) frames. Returns None if all frames were processed (exact match), or Some(frames_processed) if a partial mix occurred.
Source§

fn mix_to_mono_exact(&self, dest: &mut MonoViewMut<'_, S>)
where S: AddAssign + Div<Output = S> + From<u16>,

Mix all channels to mono by averaging them. Panics if source and destination don’t have the same number of frames.
Source§

fn copy_channel_to_mono( &self, dest: &mut MonoViewMut<'_, S>, channel: u16, ) -> Option<usize>

Copy a specific channel to a mono buffer. Only copies min(src_frames, dst_frames) frames. Returns None if all frames were copied (exact match), or Some(frames_copied) if a partial copy occurred.
Source§

fn copy_channel_to_mono_exact( &self, dest: &mut MonoViewMut<'_, S>, channel: u16, )

Copy a specific channel to a mono buffer. Panics if source and destination don’t have the same number of frames.
Source§

impl<S, B> AudioBlockOpsMut<S> for B
where S: Sample, B: AudioBlockMut<S>,

Source§

fn copy_from_block( &mut self, block: &impl AudioBlock<S>, ) -> Option<(u16, usize)>

Copy samples from source block into destination. Only copies min(src, dst) channels and frames. Returns None if the entire block was copied (exact match), or Some((channels_copied, frames_copied)) if a partial copy occurred. Never panics - safely handles mismatched block sizes.
Source§

fn copy_from_block_exact(&mut self, block: &impl AudioBlock<S>)

Copy samples from source block, requiring exact size match. Panics if source and destination don’t have identical channels and frames.
Source§

fn copy_mono_to_all_channels(&mut self, mono: &MonoView<'_, S>) -> Option<usize>

Copy a mono block to all channels of this block. Only copies min(src_frames, dst_frames) frames. Returns None if all frames were copied (exact match), or Some(frames_copied) if a partial copy occurred.
Source§

fn copy_mono_to_all_channels_exact(&mut self, mono: &MonoView<'_, S>)

Copy a mono block to all channels of this block. Panics if blocks don’t have the same number of frames.
Source§

fn for_each(&mut self, f: impl FnMut(&mut S))

Gives access to all samples in the block.
Source§

fn enumerate(&mut self, f: impl FnMut(u16, usize, &mut S))

Gives access to all samples in the block while supplying the information about which channel and frame number the sample is stored in.
Source§

fn for_each_allocated(&mut self, f: impl FnMut(&mut S))

Iterate over all allocated samples using fast linear buffer iteration. Read more
Source§

fn enumerate_allocated(&mut self, f: impl FnMut(u16, usize, &mut S))

Iterate over all allocated samples with indices using fast linear buffer iteration. Read more
Source§

fn fill_with(&mut self, sample: S)

Sets all samples in the block to the specified value. Iterates over the entire allocated buffer for efficiency.
Source§

fn clear(&mut self)
where S: Default,

Sets all samples in the block to the default value (zero for numeric types). Iterates over the entire allocated buffer for efficiency.
Source§

fn gain(&mut self, gain: S)
where S: Mul<Output = S> + Copy,

Applies gain to all samples by multiplying each sample. Iterates over the entire allocated buffer for efficiency.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.