Skip to content

Commit

Permalink
touch: hacky touch to quantized minor note/lpf works nicely :)
Browse files Browse the repository at this point in the history
  • Loading branch information
vk2seb committed Dec 7, 2023
1 parent 0749502 commit 483ee65
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
35 changes: 32 additions & 3 deletions firmware/polyvec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use litex_pac as pac;
use riscv_rt::entry;
use litex_hal::hal::digital::v2::OutputPin;
use heapless::String;
use heapless::Vec;
use embedded_midi::MidiIn;
use core::arch::asm;
use aligned_array::{Aligned, A4};
Expand Down Expand Up @@ -244,16 +245,44 @@ impl State {

let lpf = get_lpfs(&peripherals);

/*
while let Ok(event) = self.midi_in.read() {
self.voice_manager.event(event, uptime_ms);
}
self.voice_manager.tick(uptime_ms, opts);
*/

let pmod1 = &peripherals.EURORACK_PMOD1;
let pmod2 = &peripherals.EURORACK_PMOD2;
let pmod3 = &peripherals.EURORACK_PMOD3;

let touch1 = pmod1.touch();
let touch2 = pmod2.touch();
let touch3 = pmod3.touch();

let mut touch_concat: [u8; 8*3] = [0u8; 8*3];
touch_concat[0..8].copy_from_slice(&touch3);
touch_concat[8..16].copy_from_slice(&touch2);
touch_concat[16..24].copy_from_slice(&touch1);

// 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 minor_map: [usize; 24] = [
0, 2, 3, 5, 7, 8, 10, 12,
12+0, 12+2, 12+3, 12+5, 12+7, 12+8, 12+10, 24,
24+0, 24+2, 24+3, 24+5, 24+7, 24+8, 24+10, 36,
];

for n_voice in 0..N_VOICES {
let voice = &self.voice_manager.voices[n_voice];
shifter[n_voice].set_pitch(voice.pitch);
lpf[n_voice].set_cutoff((voice.amplitude * 8000f32) as i16);
//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);
shifter[n_voice].set_pitch(pitch);
lpf[n_voice].set_cutoff((ampl * 8000f32) as i16);
lpf[n_voice].set_resonance(opts.adsr.resonance.value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/polyvec/src/voice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl VoiceManager {
}
}

fn note_to_pitch(note: u8) -> i16 {
pub fn note_to_pitch(note: u8) -> i16 {
let scale = 2.0f32.powf(((note as i16) - 60) as f32 / 12.0f32);
((1.0f32 - scale) * 32768.0f32) as i16
}
Expand Down

0 comments on commit 483ee65

Please sign in to comment.