use crate::prelude::*;
use std::ops::{Index, IndexMut};
pub mod interpolate;
mod ring;
#[cfg(feature = "hound")]
pub mod wav;
pub use interpolate as int;
pub use ring::*;
pub trait Buffer: AsRef<[Self::Item]> + std::ops::Index<usize, Output = Self::Item> {
type Item: Audio;
fn len(&self) -> usize {
self.as_ref().len()
}
fn is_empty(&self) -> bool {
self.as_ref().is_empty()
}
#[must_use]
fn time(&self) -> unt::Time {
unt::Time::new(crate::units::FracInt::new(self.len() as u64))
}
#[must_use]
fn as_slice(&self) -> &[Self::Item] {
self.as_ref()
}
#[must_use]
fn get(&self, index: usize) -> Option<Self::Item> {
self.as_ref().get(index).copied()
}
#[must_use]
fn peak(&self) -> <Self::Item as smp::Array>::Array<unt::Vol> {
fn peak<A: Audio>(buf: &[A]) -> <A as smp::Array>::Array<unt::Vol> {
let mut res = A::new_default();
for sample in buf {
A::for_each(|index| {
let peak = &mut res[index];
let new = sample[index].abs();
if *peak > new {
*peak = new;
}
});
}
res.map_array(|&x| unt::Vol::new(x))
}
peak(self.as_ref())
}
#[must_use]
fn rms(&self) -> <Self::Item as smp::Array>::Array<unt::Vol> {
fn rms<A: Audio>(buf: &[A]) -> <A as smp::Array>::Array<unt::Vol> {
let mut res: <A as Array>::Array<f64> = Array::new_default();
for sample in buf {
A::for_each(|index| {
let new = sample[index];
res[index] += new * new;
});
}
#[allow(clippy::cast_precision_loss)]
A::for_each(|index| {
res[index] = (res[index] / buf.len() as f64).sqrt();
});
res.map_array(|&x| unt::Vol::new(x))
}
rms(self.as_ref())
}
}
pub trait BufferMut:
Buffer + AsMut<[Self::Item]> + std::ops::IndexMut<usize, Output = Self::Item>
{
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
self.as_mut()
}
fn _as_mut(&mut self) -> &mut [Self::Item] {
self.as_mut()
}
fn get_mut(&mut self, index: usize) -> Option<&mut Self::Item> {
self._as_mut().get_mut(index)
}
fn overwrite_time<F: FnMut(unt::Time) -> Self::Item>(
&mut self,
time: &mut unt::Time,
mut song: F,
) {
for sample in self._as_mut() {
*sample = song(*time);
time.advance();
}
}
fn overwrite<F: FnMut(unt::Time) -> Self::Item>(&mut self, song: F) {
let mut time = unt::Time::ZERO;
self.overwrite_time(&mut time, song);
}
fn overwrite_from_sgn<S: SignalMut<Sample = Self::Item>>(&mut self, sgn: &mut S) {
self.overwrite(|_| sgn.next());
}
fn clear(&mut self) {
self.overwrite(|_| smp::SampleBase::ZERO);
}
}
#[derive(Clone, Debug)]
pub struct Slice<'a, A: Audio> {
pub data: &'a [A],
}
#[derive(Debug)]
pub struct SliceMut<'a, A: Audio> {
pub data: &'a mut [A],
}
#[derive(Clone, Copy, Debug)]
pub struct Stc<A: Audio, const N: usize> {
pub data: [A; N],
}
#[derive(Clone, Debug, Default)]
pub struct Dyn<A: Audio> {
pub data: Vec<A>,
}
impl<'a, A: Audio> AsRef<[A]> for Slice<'a, A> {
fn as_ref(&self) -> &[A] {
self.data
}
}
impl<'a, A: Audio> AsRef<[A]> for SliceMut<'a, A> {
fn as_ref(&self) -> &[A] {
self.data
}
}
impl<A: Audio, const N: usize> AsRef<[A]> for Stc<A, N> {
fn as_ref(&self) -> &[A] {
&self.data
}
}
impl<A: Audio> AsRef<[A]> for Dyn<A> {
fn as_ref(&self) -> &[A] {
&self.data
}
}
impl<'a, A: Audio> AsMut<[A]> for SliceMut<'a, A> {
fn as_mut(&mut self) -> &mut [A] {
self.data
}
}
impl<A: Audio, const N: usize> AsMut<[A]> for Stc<A, N> {
fn as_mut(&mut self) -> &mut [A] {
&mut self.data
}
}
impl<A: Audio> AsMut<[A]> for Dyn<A> {
fn as_mut(&mut self) -> &mut [A] {
&mut self.data
}
}
impl<'a, A: Audio> Index<usize> for Slice<'a, A> {
type Output = A;
fn index(&self, index: usize) -> &A {
&self.as_ref()[index]
}
}
impl<'a, A: Audio> Index<usize> for SliceMut<'a, A> {
type Output = A;
fn index(&self, index: usize) -> &A {
&self.as_ref()[index]
}
}
impl<A: Audio, const N: usize> Index<usize> for Stc<A, N> {
type Output = A;
fn index(&self, index: usize) -> &A {
&self.as_ref()[index]
}
}
impl<A: Audio> Index<usize> for Dyn<A> {
type Output = A;
fn index(&self, index: usize) -> &A {
&self.as_ref()[index]
}
}
impl<'a, A: Audio> IndexMut<usize> for SliceMut<'a, A> {
fn index_mut(&mut self, index: usize) -> &mut A {
&mut self.as_mut()[index]
}
}
impl<A: Audio, const N: usize> IndexMut<usize> for Stc<A, N> {
fn index_mut(&mut self, index: usize) -> &mut A {
&mut self.as_mut()[index]
}
}
impl<A: Audio> IndexMut<usize> for Dyn<A> {
fn index_mut(&mut self, index: usize) -> &mut A {
&mut self.as_mut()[index]
}
}
impl<'a, A: Audio> Buffer for Slice<'a, A> {
type Item = A;
}
impl<'a, A: Audio> Buffer for SliceMut<'a, A> {
type Item = A;
}
impl<A: Audio, const N: usize> Buffer for Stc<A, N> {
type Item = A;
}
impl<A: Audio> Buffer for Dyn<A> {
type Item = A;
}
impl<'a, A: Audio> BufferMut for SliceMut<'a, A> {}
impl<A: Audio, const N: usize> BufferMut for Stc<A, N> {}
impl<A: Audio> BufferMut for Dyn<A> {}
impl<A: Audio, const N: usize> Default for Stc<A, N> {
fn default() -> Self {
Self::new()
}
}
impl<'a, A: Audio> Slice<'a, A> {
pub const fn new(data: &'a [A]) -> Self {
Self { data }
}
}
impl<'a, A: Audio> SliceMut<'a, A> {
pub fn new(data: &'a mut [A]) -> Self {
Self { data }
}
#[must_use]
pub const fn buf_ref(self) -> Slice<'a, A> {
Slice::new(self.data)
}
}
impl<A: Audio, const N: usize> Stc<A, N> {
pub const fn from_data(data: [A; N]) -> Self {
Self { data }
}
#[must_use]
pub const fn new() -> Self {
Self::from_data([A::ZERO; N])
}
#[must_use]
pub fn slice(&self) -> Slice<A> {
Slice::new(&self.data)
}
#[must_use]
pub fn slice_mut(&mut self) -> SliceMut<A> {
SliceMut::new(&mut self.data)
}
}
impl<A: Audio> Dyn<A> {
#[must_use]
pub const fn from_data(data: Vec<A>) -> Self {
Self { data }
}
pub fn new(samples: usize) -> Self {
Self::from_data(vec![A::ZERO; samples])
}
#[must_use]
pub fn new_time(time: unt::Time) -> Self {
Self::new(time.samples.int().try_into().expect("buffer too large"))
}
#[must_use]
pub const fn empty() -> Self {
Self::from_data(Vec::new())
}
#[must_use]
pub fn slice(&self) -> Slice<A> {
Slice::new(&self.data)
}
#[must_use]
pub fn slice_mut(&mut self) -> SliceMut<A> {
SliceMut::new(&mut self.data)
}
pub fn iter(&self) -> std::slice::Iter<A> {
self.into_iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<A> {
self.into_iter()
}
pub fn create<F: FnMut(unt::Time) -> A>(length: unt::Time, mut song: F) -> Self {
let length = length.samples.int();
let mut data = Vec::with_capacity(usize::try_from(length).expect("buffer too large"));
let mut time = unt::Time::ZERO;
for _ in 0..length {
data.push(song(time));
time.advance();
}
Self::from_data(data)
}
pub fn create_from_sgn<S: SignalMut<Sample = A>>(length: unt::Time, sgn: &mut S) -> Self {
Self::create(length, |_| sgn.next())
}
}
impl<A: Audio> From<Vec<A>> for Dyn<A> {
fn from(data: Vec<A>) -> Self {
Self::from_data(data)
}
}
impl<A: Audio> FromIterator<A> for Dyn<A> {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
Self::from_data(FromIterator::from_iter(iter))
}
}
impl<A: Audio> IntoIterator for Dyn<A> {
type IntoIter = std::vec::IntoIter<A>;
type Item = A;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}
impl<'a, A: Audio> IntoIterator for &'a Dyn<A> {
type IntoIter = std::slice::Iter<'a, A>;
type Item = &'a A;
fn into_iter(self) -> Self::IntoIter {
self.data.iter()
}
}
impl<'a, A: Audio> IntoIterator for &'a mut Dyn<A> {
type IntoIter = std::slice::IterMut<'a, A>;
type Item = &'a mut A;
fn into_iter(self) -> Self::IntoIter {
self.data.iter_mut()
}
}
pub type Empty<A> = Stc<A, 0>;