From e9a9ee2264072fe180f7f87be002d58b8d04ad15 Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Mon, 27 Oct 2025 16:55:33 -0600 Subject: [PATCH] allow wavetable to be generic over data width --- src/wavetable.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/wavetable.rs b/src/wavetable.rs index b101ce3..5105607 100644 --- a/src/wavetable.rs +++ b/src/wavetable.rs @@ -4,15 +4,17 @@ pub trait Wavetable { 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 - table: &'a [u8], + table: &'a [T], // index in the waveform index: usize, } -impl<'a> Wavetable for SimpleWavetable<'a> { - type OutputType = u8; +impl<'a, T: Copy> Wavetable for SimpleWavetable<'a, T> { + type OutputType = T; fn next(&mut self) -> Self::OutputType { let value = self.table[self.index]; self.index += 1; @@ -23,8 +25,8 @@ impl<'a> Wavetable for SimpleWavetable<'a> { } } -impl<'a> SimpleWavetable<'a> { - pub fn new(table: &'a [u8]) -> Self { +impl<'a, T: Copy> SimpleWavetable<'a, T> { + pub fn new(table: &'a [T]) -> Self { Self { table, index: 0 } } } @@ -36,8 +38,8 @@ pub mod test { #[test] fn wavetable_t1() { - let table_data: [u8; 2] = [0, 255]; - let mut w = SimpleWavetable::new(&table_data); + let table_data = [0, 255]; + let mut w = SimpleWavetable::::new(&table_data); for point in table_data { assert!(w.next() == point); }