set up for better async handling

This commit is contained in:
sigil-03 2025-10-28 15:33:51 -06:00
parent 9417e6edbf
commit aa57096e4b
2 changed files with 13 additions and 10 deletions

View file

@ -7,7 +7,7 @@ pub struct SimpleWavetableSynthesizer<W: Wavetable> {
clock_freq_hz: usize, clock_freq_hz: usize,
output_freq_hz: usize, output_freq_hz: usize,
enable: bool, enable: bool,
counter: usize, pub counter: usize,
clock_per_sample: usize, clock_per_sample: usize,
current_output: <W as Wavetable>::OutputType, current_output: <W as Wavetable>::OutputType,
output_flag: bool, output_flag: bool,
@ -33,7 +33,7 @@ impl<W: Wavetable> SimpleWavetableSynthesizer<W> {
pub fn get_output(&mut self) -> <W as Wavetable>::OutputType { pub fn get_output(&mut self) -> <W as Wavetable>::OutputType {
self.output_flag = false; self.output_flag = false;
self.current_output self.wavetable.get_value()
} }
pub fn set_freq(&mut self, freq_hz: usize) { pub fn set_freq(&mut self, freq_hz: usize) {
@ -43,11 +43,11 @@ impl<W: Wavetable> SimpleWavetableSynthesizer<W> {
} }
pub fn tick(&mut self) { pub fn tick(&mut self) {
if self.counter == self.clock_per_sample { if self.counter >= self.clock_per_sample {
self.counter = 0; self.wavetable.next();
self.current_output = self.wavetable.next();
self.output_flag = true; self.output_flag = true;
self.counter = 0;
} }
self.counter += 1; self.counter = self.counter.wrapping_add(1)
} }
} }

View file

@ -1,7 +1,8 @@
pub trait Wavetable { pub trait Wavetable {
type OutputType: Default + Copy; type OutputType: Default + Copy;
/// get next sample /// get next sample
fn next(&mut self) -> Self::OutputType; fn next(&mut self);
fn get_value(&self) -> Self::OutputType;
fn size(&self) -> usize; fn size(&self) -> usize;
} }
@ -16,13 +17,15 @@ pub struct SimpleWavetable<'a, T: Copy> {
impl<'a, T: Copy + Default> Wavetable for SimpleWavetable<'a, T> { impl<'a, T: Copy + Default> Wavetable for SimpleWavetable<'a, T> {
type OutputType = T; type OutputType = T;
fn next(&mut self) -> Self::OutputType { fn next(&mut self) {
let value = self.table[self.index];
self.index += 1; self.index += 1;
if self.index > self.table.len() - 1 { if self.index > self.table.len() - 1 {
self.index = 0; self.index = 0;
} }
value }
fn get_value(&self) -> Self::OutputType {
self.table[self.index]
} }
fn size(&self) -> usize { fn size(&self) -> usize {