Function audio::wrap::interleaved

source ·
pub fn interleaved<T>(value: T, channels: usize) -> Interleaved<T>
where T: Slice,
Expand description

Wrap a slice as an interleaved buffer with the given number of channels.

Certain interleaved buffers can be used conveniently as implementors of ReadBuf and WriteBuf, due to the convenient nature of the buffer living linearly in memory.

Example using a buffer for linear I/O

use audio::{wrap, io};
use audio::ReadBuf as _;

let mut read_from = wrap::interleaved(&[0, 1, 2, 4, 5, 6, 7, 8][..], 2);
let mut write_to = io::Write::new(audio::sequential![[0i16; 4]; 2]);

assert!(read_from.has_remaining());
io::copy_remaining(&mut read_from, &mut write_to);
assert!(!read_from.has_remaining());

assert_eq! {
    write_to.as_ref().as_slice(),
    &[0, 2, 5, 7, 1, 4, 6, 8],
};

Or with a mutable slice for writing.

use audio::{wrap, io};
use audio::WriteBuf as _;

let mut vec = vec![0, 1, 2, 4, 5, 6, 7, 8];

let mut read_from = io::Read::new(audio::sequential![[0i16, 1i16, 2i16, 3i16]; 2]);
let mut write_to = wrap::interleaved(&mut vec[..], 2);

assert!(write_to.has_remaining_mut());
io::copy_remaining(&mut read_from, &mut write_to);
assert!(!write_to.has_remaining_mut());

assert_eq! {
    &vec[..],
    &[0, 0, 1, 1, 2, 2, 3, 3],
};