allow wavetable to be generic over data width

This commit is contained in:
sigil-03 2025-10-27 16:55:33 -06:00
parent 9db5d76c12
commit e9a9ee2264

View file

@ -4,15 +4,17 @@ pub trait Wavetable {
fn next(&mut self) -> Self::OutputType; fn next(&mut self) -> Self::OutputType;
} }
pub struct SimpleWavetable<'a> { /// non-interpolated, simple wavetable that produces the next sample
/// every time [`Wavetable::next()`] is called.
pub struct SimpleWavetable<'a, T: Copy> {
// byte array for waveform // byte array for waveform
table: &'a [u8], table: &'a [T],
// index in the waveform // index in the waveform
index: usize, index: usize,
} }
impl<'a> Wavetable for SimpleWavetable<'a> { impl<'a, T: Copy> Wavetable for SimpleWavetable<'a, T> {
type OutputType = u8; type OutputType = T;
fn next(&mut self) -> Self::OutputType { fn next(&mut self) -> Self::OutputType {
let value = self.table[self.index]; let value = self.table[self.index];
self.index += 1; self.index += 1;
@ -23,8 +25,8 @@ impl<'a> Wavetable for SimpleWavetable<'a> {
} }
} }
impl<'a> SimpleWavetable<'a> { impl<'a, T: Copy> SimpleWavetable<'a, T> {
pub fn new(table: &'a [u8]) -> Self { pub fn new(table: &'a [T]) -> Self {
Self { table, index: 0 } Self { table, index: 0 }
} }
} }
@ -36,8 +38,8 @@ pub mod test {
#[test] #[test]
fn wavetable_t1() { fn wavetable_t1() {
let table_data: [u8; 2] = [0, 255]; let table_data = [0, 255];
let mut w = SimpleWavetable::new(&table_data); let mut w = SimpleWavetable::<u32>::new(&table_data);
for point in table_data { for point in table_data {
assert!(w.next() == point); assert!(w.next() == point);
} }