diff --git a/src/synthesizer.rs b/src/synthesizer.rs index 3372632..265c903 100644 --- a/src/synthesizer.rs +++ b/src/synthesizer.rs @@ -7,7 +7,7 @@ pub struct SimpleWavetableSynthesizer { clock_freq_hz: usize, output_freq_hz: usize, enable: bool, - counter: usize, + pub counter: usize, clock_per_sample: usize, current_output: ::OutputType, output_flag: bool, @@ -33,7 +33,7 @@ impl SimpleWavetableSynthesizer { pub fn get_output(&mut self) -> ::OutputType { self.output_flag = false; - self.current_output + self.wavetable.get_value() } pub fn set_freq(&mut self, freq_hz: usize) { @@ -43,11 +43,11 @@ impl SimpleWavetableSynthesizer { } pub fn tick(&mut self) { - if self.counter == self.clock_per_sample { - self.counter = 0; - self.current_output = self.wavetable.next(); + if self.counter >= self.clock_per_sample { + self.wavetable.next(); self.output_flag = true; + self.counter = 0; } - self.counter += 1; + self.counter = self.counter.wrapping_add(1) } } diff --git a/src/wavetable.rs b/src/wavetable.rs index e7e9e67..b934e48 100644 --- a/src/wavetable.rs +++ b/src/wavetable.rs @@ -1,7 +1,8 @@ pub trait Wavetable { 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; } @@ -16,13 +17,15 @@ pub struct SimpleWavetable<'a, T: Copy> { 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() - 1 { self.index = 0; } - value + } + + fn get_value(&self) -> Self::OutputType { + self.table[self.index] } fn size(&self) -> usize {