Skip to content

Commit

Permalink
touch: smooth amplitude over touches
Browse files Browse the repository at this point in the history
  • Loading branch information
vk2seb committed Dec 8, 2023
1 parent 483ee65 commit d5a205a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 8 additions & 0 deletions firmware/polyvec/src/gw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ pub trait WavetableOscillator {

pub trait PitchShift {
fn set_pitch(&self, value: i16);
fn pitch(&self) -> i16;
}

pub trait KarlsenLpf {
fn set_cutoff(&self, value: i16);
fn cutoff(&self) -> i16;
fn set_resonance(&self, value: i16);
}

Expand Down Expand Up @@ -137,6 +139,9 @@ macro_rules! pitch_shift {
self.csr_pitch().write(|w| w.csr_pitch().bits(value as u16));
}
}
fn pitch(&self) -> i16 {
(self.csr_pitch().read().bits() as u16) as i16
}
})+
};
}
Expand All @@ -149,6 +154,9 @@ macro_rules! karlsen_lpf {
self.csr_g().write(|w| w.csr_g().bits(value as u16));
}
}
fn cutoff(&self) -> i16 {
(self.csr_g().read().bits() as u16) as i16
}
fn set_resonance(&self, value: i16) {
unsafe {
self.csr_resonance().write(|w| w.csr_resonance().bits(value as u16));
Expand Down
11 changes: 7 additions & 4 deletions firmware/polyvec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ impl State {
// Create a vector of tuples where each tuple consists of the index and value.
let mut touch: Vec<(usize, u8), 24> = touch_concat.iter().enumerate().map(|(i, &item)| (i, item)).collect();
touch.sort_unstable_by(|a, b| b.1.cmp(&a.1));
let top4 = &touch[0..4];
let mut top4_by_pitch: Vec<(usize, u8), 4> = Vec::from_slice(&touch[0..4]).unwrap();
top4_by_pitch.sort_unstable_by(|a, b| b.0.cmp(&a.0));

let minor_map: [usize; 24] = [
0, 2, 3, 5, 7, 8, 10, 12,
Expand All @@ -279,10 +280,12 @@ impl State {

for n_voice in 0..N_VOICES {
//let voice = &self.voice_manager.voices[n_voice];
let ampl = (top4[n_voice].1 as f32) / 256.0f32;
let pitch = note_to_pitch((minor_map[top4[n_voice].0] + 36) as u8);
let ampl = (top4_by_pitch[n_voice].1 as f32) / 256.0f32;
let pitch = note_to_pitch((minor_map[top4_by_pitch[n_voice].0] + 36) as u8);
shifter[n_voice].set_pitch(pitch);
lpf[n_voice].set_cutoff((ampl * 8000f32) as i16);
let ampl_old = (lpf[n_voice].cutoff() as f32) / 8000f32;
let ampl_new = ampl*0.05 + ampl_old*0.95;
lpf[n_voice].set_cutoff((ampl_new * 8000f32) as i16);
lpf[n_voice].set_resonance(opts.adsr.resonance.value);
}
}
Expand Down

0 comments on commit d5a205a

Please sign in to comment.