Struct audio::io::ReadWrite

source ·
pub struct ReadWrite<B> { /* private fields */ }
Expand description

Make any mutable buffer into a write adapter that implements ReadBuf and WriteBuf.

Examples

use audio::{Buf, ReadBuf, WriteBuf};
use audio::io;

let from = audio::interleaved![[1.0f32, 2.0f32, 3.0f32, 4.0f32]; 2];
let to = audio::interleaved![[0.0f32; 4]; 2];

// Make `to` into a read / write adapter.
let mut to = io::ReadWrite::empty(to);

io::copy_remaining(io::Read::new((&from).skip(2).limit(1)), &mut to);
assert_eq!(to.remaining(), 1);

io::copy_remaining(io::Read::new((&from).limit(1)), &mut to);
assert_eq!(to.remaining(), 2);

assert_eq! {
    to.as_ref().as_slice(),
    &[3.0, 3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0],
};

// Note: 4 channels, 2 frames each.
let mut read_out = io::Write::new(audio::buf::Interleaved::with_topology(4, 2));

assert_eq!(read_out.remaining_mut(), 2);
assert!(read_out.has_remaining_mut());

assert_eq!(to.remaining(), 2);
assert!(to.has_remaining());

io::copy_remaining(&mut to, &mut read_out);

assert_eq!(read_out.remaining_mut(), 0);
assert!(!read_out.has_remaining_mut());

assert_eq!(to.remaining(), 0);
assert!(!to.has_remaining());

assert_eq! {
    read_out.as_ref().as_slice(),
    &[3.0, 3.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0],
}

Implementations§

source§

impl<B> ReadWrite<B>

source

pub fn new(buf: B) -> Self
where B: ExactSizeBuf,

Construct a new adapter that supports both reading and writing.

The constructed reader will be initialized so that the number of bytes available for reading are equal to what’s reported by ExactSizeBuf::frames.

Examples
use audio::{ReadBuf, ExactSizeBuf};
use audio::io;

let buf = audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]];
assert_eq!(buf.frames(), 4);

let buf = io::ReadWrite::new(buf);

assert!(buf.has_remaining());
assert_eq!(buf.remaining(), 4);
source

pub fn empty(buf: B) -> Self

Construct a new adapter that supports both reading and writing.

The constructed reader will be initialized so that there have been no frames written to it, so there will not be any frames available for reading.

Examples
use audio::{ReadBuf, ExactSizeBuf};
use audio::io;

let buf = audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]];
assert_eq!(buf.frames(), 4);

let buf = io::ReadWrite::empty(buf);

assert!(!buf.has_remaining());
assert_eq!(buf.remaining(), 0);
source

pub fn as_ref(&self) -> &B

Access the underlying buffer.

Examples
use audio::Buf;
use audio::io;

let buf: audio::buf::Interleaved<i16> = audio::interleaved![[1, 2, 3, 4]; 4];
let mut buf = io::ReadWrite::new(buf);

let from = audio::wrap::interleaved(&[1i16, 2i16, 3i16, 4i16][..], 2);

io::translate_remaining(from, &mut buf);

assert_eq!(buf.as_ref().channels(), 4);
source

pub fn as_mut(&mut self) -> &mut B

Access the underlying buffer mutably.

Examples
use audio::Buf;
use audio::io;

let to: audio::buf::Interleaved<i16> = audio::interleaved![[1, 2, 3, 4]; 4];
let mut to = io::ReadWrite::new(to);

let from = audio::wrap::interleaved(&[1i16, 2i16, 3i16, 4i16][..], 2);

io::translate_remaining(from, &mut to);

to.as_mut().resize_channels(2);

assert_eq!(to.channels(), 2);
source

pub fn into_inner(self) -> B

Convert into the underlying buffer.

Examples
use audio::Buf;
use audio::io;

let buf: audio::buf::Interleaved<i16> = audio::interleaved![[1, 2, 3, 4]; 4];
let mut buf = io::ReadWrite::new(buf);

let from = audio::wrap::interleaved(&[1i16, 2i16, 3i16, 4i16][..], 2);

io::translate_remaining(from, &mut buf);

let buf = buf.into_inner();

assert_eq!(buf.channels(), 4);
source

pub fn clear(&mut self)

Clear the state of the read / write adapter, setting both read and written to zero.

source

pub fn set_read(&mut self, read: usize)

Set the number of frames read.

This can be used to rewind the internal cursor to a previously written frame if needed. Or, if the underlying buffer has changed for some reason, like if it was written to through a call to ReadWrite::as_mut.

Examples
use audio::{Buf, ReadBuf};
use audio::io;

fn read_from_buf(mut read: impl Buf<Sample = i16> + ReadBuf) {
    let mut out = audio::interleaved![[0; 4]; 2];
    io::copy_remaining(read, io::Write::new(&mut out));
}

let mut buf = io::ReadWrite::new(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
read_from_buf(&mut buf);

assert!(!buf.has_remaining());

buf.set_read(0);

assert!(buf.has_remaining());
source

pub fn set_written(&mut self, written: usize)

Set the number of frames written.

This can be used to rewind the internal cursor to a previously written frame if needed. Or, if the underlying buffer has changed for some reason, like if it was read into through a call to ReadWrite::as_mut.

Examples
use audio::{BufMut, WriteBuf};
use audio::io;

fn write_to_buf(mut write: impl BufMut<Sample = i16> + WriteBuf) {
    let mut from = audio::interleaved![[0; 4]; 2];
    io::copy_remaining(io::Read::new(&mut from), write);
}

let mut buf = io::ReadWrite::new(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
write_to_buf(&mut buf);

assert!(!buf.has_remaining_mut());

buf.set_written(0);

assert!(buf.has_remaining_mut());
source§

impl<B> ReadWrite<B>
where B: Buf,

source

pub fn iter(&self) -> Iter<'_, B>

Construct an iterator over all available channels.

source§

impl<B> ReadWrite<B>
where B: BufMut,

source

pub fn iter_mut(&mut self) -> IterMut<'_, B>

Construct a mutable iterator over all available channels.

Trait Implementations§

source§

impl<B> Buf for ReadWrite<B>
where B: Buf,

§

type Sample = <B as Buf>::Sample

The type of a single sample.
§

type Channel<'this> = <B as Buf>::Channel<'this> where Self: 'this

The type of the channel container.
§

type IterChannels<'this> = Iter<'this, B> where Self: 'this

An iterator over available channels.
source§

fn frames_hint(&self) -> Option<usize>

A typical number of frames for each channel in the buffer, if known. Read more
source§

fn channels(&self) -> usize

The number of channels in the buffer. Read more
source§

fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>>

Return a handler to the buffer associated with the channel. Read more
source§

fn iter_channels(&self) -> Self::IterChannels<'_>

Construct an iterator over all the channels in the audio buffer. Read more
source§

fn skip(self, n: usize) -> Skip<Self>
where Self: Sized,

Construct a wrapper around this buffer that skips the first n frames. Read more
source§

fn tail(self, n: usize) -> Tail<Self>
where Self: Sized,

Construct a wrapper around this buffer that skips to the last n frames. Read more
source§

fn limit(self, limit: usize) -> Limit<Self>
where Self: Sized,

Construct a wrapper around this buffer which stops after limit frames. Read more
source§

impl<B> BufMut for ReadWrite<B>
where B: ExactSizeBuf + BufMut,

§

type ChannelMut<'this> = <B as BufMut>::ChannelMut<'this> where Self: 'this

The type of the mutable channel container.
§

type IterChannelsMut<'this> = IterMut<'this, B> where Self: 'this

A mutable iterator over available channels.
source§

fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>>

Return a mutable handler to the buffer associated with the channel. Read more
source§

fn copy_channel(&mut self, from: usize, to: usize)
where Self::Sample: Copy,

Copy one channel into another. Read more
source§

fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_>

Construct a mutable iterator over available channels. Read more
source§

impl<B> ExactSizeBuf for ReadWrite<B>
where B: ExactSizeBuf,

source§

fn frames(&self) -> usize

The number of frames in a buffer. Read more
source§

impl<B> ReadBuf for ReadWrite<B>

source§

fn remaining(&self) -> usize

Get the number of frames remaining that can be read from the buffer. Read more
source§

fn advance(&mut self, n: usize)

Advance the read number of frames by n. Read more
source§

fn has_remaining(&self) -> bool

Test if there are any remaining frames to read. Read more
source§

impl<B> WriteBuf for ReadWrite<B>
where B: ExactSizeBuf,

source§

fn remaining_mut(&self) -> usize

Remaining number of frames that can be written. Read more
source§

fn advance_mut(&mut self, n: usize)

Advance the number of frames that have been written. Read more
source§

fn has_remaining_mut(&self) -> bool

Test if this buffer has remaining mutable frames that can be written. Read more

Auto Trait Implementations§

§

impl<B> RefUnwindSafe for ReadWrite<B>
where B: RefUnwindSafe,

§

impl<B> Send for ReadWrite<B>
where B: Send,

§

impl<B> Sync for ReadWrite<B>
where B: Sync,

§

impl<B> Unpin for ReadWrite<B>
where B: Unpin,

§

impl<B> UnwindSafe for ReadWrite<B>
where B: 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<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> 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, U> TryFrom<U> for T
where 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 T
where 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.