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;
}
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::<u32>::new(&table_data);
for point in table_data {
assert!(w.next() == point);
}