#[cfg(feature = "alloc")]
use crate::Vec;
use crate::{AudioChannels, PcmRaw, PcmRawError, PcmSample, PcmSpec};
#[doc = crate::_tags!(audio data)]
#[doc = crate::_doc_meta!{
location("media/audio"),
#[cfg(target_pointer_width = "32")]
test_size_of(PcmRawBuf_Slice_x32: PcmRawBuf<&[u8]> = 16|128),
#[cfg(target_pointer_width = "64")]
test_size_of(PcmRawBuf_Slice_x64: PcmRawBuf<&[u8]> = 24|192),
#[cfg(target_pointer_width = "32")]
test_size_of(PcmRawBuf_Vec_x32: PcmRawBuf<Vec<u8>> = 20|160),
#[cfg(target_pointer_width = "64")]
test_size_of(PcmRawBuf_Vec_x64: PcmRawBuf<Vec<u8>> = 32|256),
}]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PcmRawBuf<B> {
bytes: B,
spec: PcmSpec,
}
#[rustfmt::skip]
impl<B> PcmRawBuf<B> {
pub(crate) const fn _new(bytes: B, spec: PcmSpec) -> Self { Self { bytes, spec } }
pub const fn spec(&self) -> PcmSpec { self.spec }
pub const fn sample(&self) -> PcmSample { self.spec.sample }
#[must_use]
pub const fn channels(&self) -> AudioChannels { self.spec.channels }
#[must_use]
pub const fn sample_rate(&self) -> u32 { self.spec.sample_rate }
#[must_use]
pub const fn frame_bytes(&self) -> usize { self.spec.frame_bytes() }
}
#[rustfmt::skip]
impl<B: AsRef<[u8]>> PcmRawBuf<B> {
#[must_use]
pub fn bytes(&self) -> &[u8] { self.bytes.as_ref() }
#[must_use]
pub fn is_empty(&self) -> bool { self.bytes.as_ref().is_empty() }
#[must_use]
pub fn len(&self) -> usize { self.bytes.as_ref().len() }
pub fn frames(&self) -> Result<usize, PcmRawError> {
self.spec.frames_for_data_len(self.len()).ok_or(PcmRawError::InvalidDataLength)
}
#[must_use]
pub fn has_complete_frames(&self) -> bool {
self.spec.has_complete_frames_for_data_len(self.len())
}
pub fn decode_u8_into(&self, dst: &mut [u8]) -> Result<usize, PcmRawError> {
PcmRaw::decode_u8_into(self.bytes(), self.spec(), dst)
}
pub fn decode_i8_into(&self, dst: &mut [i8]) -> Result<usize, PcmRawError> {
PcmRaw::decode_i8_into(self.bytes(), self.spec(), dst)
}
pub fn decode_i16_le_into(&self, dst: &mut [i16]) -> Result<usize, PcmRawError> {
PcmRaw::decode_i16_le_into(self.bytes(), self.spec(), dst)
}
pub fn decode_i24_le_into(&self, dst: &mut [i32]) -> Result<usize, PcmRawError> {
PcmRaw::decode_i24_le_into(self.bytes(), self.spec(), dst)
}
pub fn decode_i32_le_into(&self, dst: &mut [i32]) -> Result<usize, PcmRawError> {
PcmRaw::decode_i32_le_into(self.bytes(), self.spec(), dst)
}
pub fn decode_f32_le_into(&self, dst: &mut [f32]) -> Result<usize, PcmRawError> {
PcmRaw::decode_f32_le_into(self.bytes(), self.spec(), dst)
}
pub fn decode_f64_le_into(&self, dst: &mut [f64]) -> Result<usize, PcmRawError> {
PcmRaw::decode_f64_le_into(self.bytes(), self.spec(), dst)
}
}
#[rustfmt::skip]
impl<'a> PcmRawBuf<&'a [u8]> {
#[must_use]
pub const fn bytes_const(&self) -> &'a [u8] { self.bytes }
#[must_use]
pub const fn is_empty_const(&self) -> bool { self.bytes.is_empty() }
#[must_use]
pub const fn len_const(&self) -> usize { self.bytes.len() }
pub const fn frames_const(&self) -> Result<usize, PcmRawError> {
match self.spec.frames_for_data_len(self.bytes.len()) {
Some(frames) => Ok(frames),
None => Err(PcmRawError::InvalidDataLength),
}
}
}
#[cfg(feature = "alloc")]
impl PcmRawBuf<Vec<u8>> {
pub fn as_borrowed(&self) -> PcmRawBuf<&[u8]> {
PcmRawBuf::_new(self.bytes.as_slice(), self.spec)
}
#[must_use]
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
}