Compare commits

...

No commits in common. "main" and "synthesizer" have entirely different histories.

3 changed files with 75 additions and 9 deletions

View file

@ -1,2 +1,6 @@
#![no_std]
mod wavetable;
pub mod synthesizer;
pub mod wavetable;
//TODO:
// * patch mod w/ trait Input and trait Output(?)

54
src/synthesizer.rs Normal file
View file

@ -0,0 +1,54 @@
use crate::wavetable::Wavetable;
pub trait Synthesizer {}
pub struct SimpleWavetableSynthesizer<W: Wavetable> {
wavetable: W,
clock_freq_hz: usize,
output_freq_hz: usize,
enable: bool,
pub counter: usize,
clock_per_sample: usize,
current_output: <W as Wavetable>::OutputType,
output_flag: bool,
}
impl<W: Wavetable> SimpleWavetableSynthesizer<W> {
pub fn new(wavetable: W, clock_freq_hz: usize) -> Self {
Self {
wavetable,
clock_freq_hz,
output_freq_hz: 1,
enable: false,
counter: 0,
clock_per_sample: 0,
current_output: <W as Wavetable>::OutputType::default(),
output_flag: false,
}
}
pub fn has_new_output(&self) -> bool {
self.output_flag
}
pub fn get_output(&mut self) -> <W as Wavetable>::OutputType {
self.output_flag = false;
self.wavetable.get_value()
}
pub fn set_freq(&mut self, freq_hz: usize) {
self.output_freq_hz = freq_hz;
// probably a better way to do this with modulos or something...
// FIXME: use the length of the square table.. or whatever wavetable tbh
self.clock_per_sample = self.clock_freq_hz / self.output_freq_hz / self.wavetable.size();
}
pub fn tick(&mut self) {
if self.counter >= self.clock_per_sample {
self.wavetable.next();
self.output_flag = true;
self.counter = 0;
}
self.counter = self.counter.wrapping_add(1)
}
}

View file

@ -1,7 +1,9 @@
pub trait Wavetable {
type OutputType;
type OutputType: Default + Copy;
/// get next sample
fn next(&mut self) -> Self::OutputType;
fn next(&mut self);
fn get_value(&self) -> Self::OutputType;
fn size(&self) -> usize;
}
/// non-interpolated, simple wavetable that produces the next sample
@ -13,19 +15,25 @@ pub struct SimpleWavetable<'a, T: Copy> {
index: usize,
}
impl<'a, T: Copy> Wavetable for SimpleWavetable<'a, T> {
impl<'a, T: Copy + Default> Wavetable for SimpleWavetable<'a, T> {
type OutputType = T;
fn next(&mut self) -> Self::OutputType {
let value = self.table[self.index];
fn next(&mut self) {
self.index += 1;
if self.index > self.table.len() {
if self.index > self.table.len() - 1 {
self.index = 0;
}
value
}
fn get_value(&self) -> Self::OutputType {
self.table[self.index]
}
fn size(&self) -> usize {
self.table.len()
}
}
impl<'a, T: Copy> SimpleWavetable<'a, T> {
impl<'a, T: Copy + Default> SimpleWavetable<'a, T> {
pub fn new(table: &'a [T]) -> Self {
Self { table, index: 0 }
}