Skip to main content

codewandler_audio/
buffer.rs

1use crate::AudioSink;
2use crate::format::SampleFormat;
3
4pub struct Buffer<T: SampleFormat>(Vec<T>);
5
6impl<T> Buffer<T>
7where
8    T: SampleFormat,
9{
10    pub fn new(data: Vec<T>) -> Self {
11        Self(data)
12    }
13
14    pub fn iter(&self) -> std::slice::Iter<T> {
15        self.0.iter()
16    }
17}
18
19pub trait BufferWriter<T>
20where
21    T: SampleFormat,
22{
23    fn audio_write_buffer(&self, buffer: &Buffer<T>) -> anyhow::Result<()>;
24}
25
26impl<F, T> BufferWriter<F> for T
27where
28    F: SampleFormat,
29    T: AudioSink<Format = F>,
30{
31    fn audio_write_buffer(&self, buffer: &Buffer<F>) -> anyhow::Result<()> {
32        for sample in buffer.iter() {
33            self.audio_write(*sample)?;
34        }
35        Ok(())
36    }
37}