first wavetable synth demo

This commit is contained in:
sigil-03 2025-10-27 21:10:36 -06:00
parent 3ea7aac1f4
commit 58579ae6c2
6 changed files with 84 additions and 20 deletions

View file

@ -0,0 +1,36 @@
use crate::insert_coin::SimplePwmCore;
use ch32_hal::println;
use ch32_hal::timer::GeneralInstance16bit;
use wavetable_synth::{synthesizer::SimpleWavetableSynthesizer, wavetable::SimpleWavetable};
const SQUARE_WAVETABLE: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100,
];
pub struct AppSynthesizers<'a, T: GeneralInstance16bit> {
pub square: SimpleWavetableSynthesizer<SimpleWavetable<'static, u8>>,
output: SimplePwmCore<'a, T>,
}
impl<'a, T: GeneralInstance16bit> AppSynthesizers<'a, T> {
pub fn new(clock_freq_hz: usize, output: SimplePwmCore<'a, T>) -> Self {
let square_wt = SimpleWavetable::new(&SQUARE_WAVETABLE);
let square = SimpleWavetableSynthesizer::new(square_wt, clock_freq_hz);
Self { square, output }
}
pub fn service(&mut self) {
self.square.tick();
if self.square.has_new_output() {
let out = self.square.get_output();
// println!("OUTPUT: {out}");
self.output.write_amplitude(
ch32_hal::timer::Channel::Ch4,
out / 2,
// (out as f32 * (u8::MAX as f32 / u32::MAX as f32)) as u8,
);
}
}
}