Struct Channel

Source
pub struct Channel<'a, T> { /* private fields */ }
Expand description

The buffer of a single channel.

This doesn’t provide direct access to the underlying buffer, but rather allows us to copy data usinga number of utility functions.

Implementations§

Source§

impl<'a, T> Channel<'a, T>

Source

pub fn linear(buf: &'a [T]) -> Self

Construct a linear channel buffer.

The buffer provided as-is constitutes the frames of the channel.

§Examples
use rotary::Channel;

let buf = &mut [1, 3, 5, 7];
let channel = Channel::linear(buf);

assert_eq!(channel[1], 3);
assert_eq!(channel[2], 5);
Source

pub fn interleaved(buf: &'a [T], channels: usize, channel: usize) -> Self

Construct an interleaved channel buffer.

The provided buffer must be the complete buffer, which includes all other channels. The provided channels argument is the total number of channels in this buffer, and channel indicates which specific channel this buffer belongs to.

Note that this is typically not used directly, but instead through an abstraction which makes sure to provide the correct parameters.

§Examples
use rotary::Channel;

let buf = &[1, 2, 3, 4, 5, 6, 7, 8];
let channel = Channel::interleaved(buf, 2, 1);

assert_eq!(channel[1], 4);
assert_eq!(channel[2], 6);
Source

pub fn frames(&self) -> usize

Access the number of frames on the current channel.

§Examples
use rotary::Buf;

fn test(buf: &dyn Buf<f32>) {
    let left = buf.channel(0);
    let right = buf.channel(1);

    assert_eq!(left.frames(), 16);
    assert_eq!(right.frames(), 16);
}

test(&rotary::dynamic![[0.0; 16]; 2]);
test(&rotary::sequential![[0.0; 16]; 2]);
test(&rotary::interleaved![[0.0; 16]; 2]);
Source

pub fn iter(self) -> Iter<'a, T>

Construct an iterator over the channel.

§Examples
use rotary::{Buf as _, BufMut as _};

let mut left = rotary::interleaved![[0.0f32; 4]; 2];
let mut right = rotary::dynamic![[0.0f32; 4]; 2];

for (l, r) in left.channel_mut(0).iter_mut().zip(right.channel_mut(0)) {
    *l = 1.0;
    *r = 1.0;
}

assert!(left.channel(0).iter().eq(right.channel(0).iter()));

assert_eq!(left.as_slice(), &[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]);
assert_eq!(&right[0], &[1.0, 1.0, 1.0, 1.0]);
assert_eq!(&right[1], &[0.0, 0.0, 0.0, 0.0]);
Source

pub fn as_ref(&self) -> Channel<'_, T>

Construct a new Channel reference with a lifetime associated with the current channel instance instead of the underlying buffer.

Most of the time it is not necessary to use this, since Channel implements Copy and its lifetime would coerce to any compatible lifetime. This method is currently just here for completeness sake.

Both of these work equally well:

use rotary::Channel;

struct Foo<'a> {
    channel: Channel<'a, i16>,
}

impl<'a> Foo<'a> {
    fn channel(&self) -> Channel<'_, i16> {
        self.channel.as_ref()
    }

    fn coerced_channel(&self) -> Channel<'_, i16> {
        self.channel
    }
}
Source

pub fn skip(self, n: usize) -> Self

Construct a channel buffer where the first n frames are skipped.

§Examples
use rotary::{Buf as _, BufMut as _};

let mut from = rotary::interleaved![[0.0f32; 4]; 2];
*from.frame_mut(0, 2).unwrap() = 1.0;
*from.frame_mut(0, 3).unwrap() = 1.0;

let mut to = rotary::interleaved![[0.0f32; 4]; 2];

to.channel_mut(0).copy_from(from.channel(0).skip(2));
assert_eq!(to.as_slice(), &[1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
Source

pub fn tail(self, n: usize) -> Self

Construct a channel buffer where the last n frames are included.

§Examples
use rotary::{Buf as _, BufMut as _};

let from = rotary::interleaved![[1.0f32; 4]; 2];
let mut to = rotary::interleaved![[0.0f32; 4]; 2];

to.channel_mut(0).as_mut().tail(2).copy_from(from.channel(0));
assert_eq!(to.as_slice(), &[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0]);
Source

pub fn limit(self, limit: usize) -> Self

Limit the channel bufferto limit number of frames.

§Examples
use rotary::{Buf as _, BufMut as _};

let from = rotary::interleaved![[1.0f32; 4]; 2];
let mut to = rotary::interleaved![[0.0f32; 4]; 2];

to.channel_mut(0).copy_from(from.channel(0).limit(2));
assert_eq!(to.as_slice(), &[1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
Source

pub fn chunk(self, n: usize, len: usize) -> Self

Construct a range of frames corresponds to the chunk with len and position n.

Which is the range n * len .. n * len + len.

Source

pub fn chunks(&self, chunk: usize) -> usize

How many chunks of the given size can you divide buf into.

This includes one extra chunk even if the chunk doesn’t divide the frame length evenly.

§Examples
use rotary::Buf;

fn test(buf: &dyn Buf<f32>) {
    let left = buf.channel(0);
    let right = buf.channel(1);

    assert_eq!(left.chunks(4), 4);
    assert_eq!(right.chunks(4), 4);

    assert_eq!(left.chunks(6), 3);
    assert_eq!(right.chunks(6), 3);
}

test(&rotary::dynamic![[0.0; 16]; 2]);
test(&rotary::sequential![[0.0; 16]; 2]);
test(&rotary::interleaved![[0.0; 16]; 2]);
Source

pub fn copy_into_slice(&self, out: &mut [T])
where T: Copy,

Copy into the given slice of output.

§Examples
use rotary::Buf;

fn test(buf: &dyn Buf<f32>) {
    let channel = buf.channel(0);

    let mut buf = vec![0.0; 16];
    channel.copy_into_slice(&mut buf[..]);

    assert!(buf.iter().all(|f| *f == 1.0));
}

test(&rotary::dynamic![[1.0; 16]; 2]);
test(&rotary::sequential![[1.0; 16]; 2]);
test(&rotary::interleaved![[1.0; 16]; 2]);
Source

pub fn copy_into_iter<'out, I>(&self, iter: I)
where I: IntoIterator<Item = &'out mut T>, T: 'out + Copy,

Copy into the given iterator.

§Examples
use rotary::Buf;

fn test(buf: &dyn Buf<f32>) {
    let channel = buf.channel(0);

    let mut buf = vec![0.0; 16];

    // Copy into every other position in `buf`.
    channel.copy_into_iter(buf.iter_mut().step_by(2));

    for (n, f) in buf.into_iter().enumerate() {
        if n % 2 == 0 {
            assert_eq!(f, 1.0);
        } else {
            assert_eq!(f, 0.0);
        }
    }
}

test(&rotary::dynamic![[1.0; 16]; 2]);
test(&rotary::sequential![[1.0; 16]; 2]);
test(&rotary::interleaved![[1.0; 16]; 2]);

Trait Implementations§

Source§

impl<'a, T: Clone> Clone for Channel<'a, T>

Source§

fn clone(&self) -> Channel<'a, T>

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<'a, T: Debug> Debug for Channel<'a, T>

Source§

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

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

impl<T> Index<usize> for Channel<'_, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a Channel<'_, T>
where T: Copy,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for Channel<'a, T>
where T: Copy,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T: Copy> Copy for Channel<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for Channel<'a, T>

§

impl<'a, T> RefUnwindSafe for Channel<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Channel<'a, T>
where T: Sync,

§

impl<'a, T> Sync for Channel<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Channel<'a, T>

§

impl<'a, T> UnwindSafe for Channel<'a, T>
where T: RefUnwindSafe,

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