use audio_core::{Buf, BufMut, Channel, ExactSizeBuf, ReadBuf};
pub struct Read<B> {
buf: B,
available: usize,
}
impl<B> Read<B> {
#[inline]
pub fn new(buf: B) -> Self
where
B: ExactSizeBuf,
{
let available = buf.frames();
Self { buf, available }
}
#[inline]
pub const fn empty(buf: B) -> Self {
Self { buf, available: 0 }
}
#[inline]
pub fn as_ref(&self) -> &B {
&self.buf
}
#[inline]
pub fn as_mut(&mut self) -> &mut B {
&mut self.buf
}
#[inline]
pub fn into_inner(self) -> B {
self.buf
}
#[inline]
pub fn set_read(&mut self, read: usize)
where
B: ExactSizeBuf,
{
self.available = self.buf.frames().saturating_sub(read);
}
}
impl<B> Read<B>
where
B: Buf,
{
#[inline]
pub fn iter(&self) -> Iter<'_, B> {
Iter {
iter: self.buf.iter_channels(),
available: self.available,
}
}
}
impl<B> Read<B>
where
B: BufMut,
{
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, B> {
IterMut {
iter: self.buf.iter_channels_mut(),
available: self.available,
}
}
}
impl<B> ReadBuf for Read<B> {
#[inline]
fn remaining(&self) -> usize {
self.available
}
#[inline]
fn advance(&mut self, n: usize) {
self.available = self.available.saturating_sub(n);
}
}
impl<B> ExactSizeBuf for Read<B>
where
B: ExactSizeBuf,
{
#[inline]
fn frames(&self) -> usize {
self.buf.frames().saturating_sub(self.available)
}
}
impl<B> Buf for Read<B>
where
B: Buf,
{
type Sample = B::Sample;
type Channel<'this>
= B::Channel<'this>
where
Self: 'this;
type IterChannels<'this>
= Iter<'this, B>
where
Self: 'this;
#[inline]
fn frames_hint(&self) -> Option<usize> {
self.buf.frames_hint()
}
#[inline]
fn channels(&self) -> usize {
self.buf.channels()
}
#[inline]
fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>> {
Some(self.buf.get_channel(channel)?.tail(self.available))
}
#[inline]
fn iter_channels(&self) -> Self::IterChannels<'_> {
(*self).iter()
}
}
impl<B> BufMut for Read<B>
where
B: ExactSizeBuf + BufMut,
{
type ChannelMut<'a>
= B::ChannelMut<'a>
where
Self: 'a;
type IterChannelsMut<'a>
= IterMut<'a, B>
where
Self: 'a;
#[inline]
fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>> {
Some(self.buf.get_channel_mut(channel)?.tail(self.available))
}
#[inline]
fn copy_channel(&mut self, from: usize, to: usize)
where
Self::Sample: Copy,
{
self.buf.copy_channel(from, to);
}
#[inline]
fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_> {
(*self).iter_mut()
}
}
iter! {
available: usize,
=>
self.tail(available)
}
iter_mut! {
available: usize,
=>
self.tail(available)
}