simple wavetable

This commit is contained in:
sigil-03 2025-10-27 16:55:32 -06:00
commit 9db5d76c12
5 changed files with 68 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "wavetable-synth"
version = "0.1.0"
edition = "2024"
[dependencies]

10
README.md Normal file
View file

@ -0,0 +1,10 @@
# WAVETABLE SYNTH
synthesizes sounds using a wavetable.
## REQUIREMENTS
`no_std` compatible
## MODULES
### `wavetable`
trait definitions for a wavetable manager

2
src/lib.rs Normal file
View file

@ -0,0 +1,2 @@
#![no_std]
mod wavetable;

48
src/wavetable.rs Normal file
View file

@ -0,0 +1,48 @@
pub trait Wavetable {
type OutputType;
/// get next sample
fn next(&mut self) -> Self::OutputType;
}
pub struct SimpleWavetable<'a> {
// byte array for waveform
table: &'a [u8],
// index in the waveform
index: usize,
}
impl<'a> Wavetable for SimpleWavetable<'a> {
type OutputType = u8;
fn next(&mut self) -> Self::OutputType {
let value = self.table[self.index];
self.index += 1;
if self.index > self.table.len() {
self.index = 0;
}
value
}
}
impl<'a> SimpleWavetable<'a> {
pub fn new(table: &'a [u8]) -> Self {
Self { table, index: 0 }
}
}
// TESTS
#[cfg(test)]
pub mod test {
use super::*;
#[test]
fn wavetable_t1() {
let table_data: [u8; 2] = [0, 255];
let mut w = SimpleWavetable::new(&table_data);
for point in table_data {
assert!(w.next() == point);
}
}
}
// TODO:
// - interpolated wavetable