flesh out synthesizer module
This commit is contained in:
parent
96e26ce493
commit
9417e6edbf
3 changed files with 44 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#![no_std]
|
||||
mod synthesizer;
|
||||
mod wavetable;
|
||||
pub mod synthesizer;
|
||||
pub mod wavetable;
|
||||
|
||||
//TODO:
|
||||
// * patch mod w/ trait Input and trait Output(?)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@ pub struct SimpleWavetableSynthesizer<W: Wavetable> {
|
|||
clock_freq_hz: usize,
|
||||
output_freq_hz: usize,
|
||||
enable: bool,
|
||||
counter: usize,
|
||||
clock_per_sample: usize,
|
||||
current_output: <W as Wavetable>::OutputType,
|
||||
output_flag: bool,
|
||||
}
|
||||
|
||||
impl<W: Wavetable> SimpleWavetableSynthesizer<W> {
|
||||
|
|
@ -14,8 +18,36 @@ impl<W: Wavetable> SimpleWavetableSynthesizer<W> {
|
|||
Self {
|
||||
wavetable,
|
||||
clock_freq_hz,
|
||||
output_freq_hz: 0,
|
||||
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.current_output
|
||||
}
|
||||
|
||||
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...
|
||||
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.counter = 0;
|
||||
self.current_output = self.wavetable.next();
|
||||
self.output_flag = true;
|
||||
}
|
||||
self.counter += 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
pub trait Wavetable {
|
||||
type OutputType;
|
||||
type OutputType: Default + Copy;
|
||||
/// get next sample
|
||||
fn next(&mut self) -> Self::OutputType;
|
||||
fn size(&self) -> usize;
|
||||
}
|
||||
|
||||
/// non-interpolated, simple wavetable that produces the next sample
|
||||
|
|
@ -13,19 +14,23 @@ 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];
|
||||
self.index += 1;
|
||||
if self.index > self.table.len() {
|
||||
if self.index > self.table.len() - 1 {
|
||||
self.index = 0;
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue