Skip to content

Commit

Permalink
Smoothen position response to control input
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Horacek <petr@zlosynth.com>
  • Loading branch information
phoracek committed Nov 11, 2023
1 parent b8ee707 commit f988d8c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ backwards compatibility.
* Pause/resume the delay line using a control input.
* Allow configuration of the ratio between the tap interval and delay length.
* Visualize progress of the buffer reset.
* Smoothen position response to control input.

## 1.2.0

Expand Down
24 changes: 15 additions & 9 deletions control/src/cache/reconcile/heads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ impl Store {
}

fn reconcile_position(&mut self, i: usize) {
// TODO: Only update the position if CV has changed significantly (above noise
// level) from the last value.
let pot = self.input.head[i].position.last_value_above_noise;

let control_index = self.control_index_for_attribute(AttributeIdentifier::Position(i));
let cv = if let Some(i) = control_index {
let control = &self.input.control[i];
if control.is_plugged {
Some(control.last_value_above_noise / 5.0)
} else {
None
}
} else {
None
};

self.cache.attributes.head[i].position = quantize(
calculate(
self.input.head[i].position.last_value_above_noise,
self.control_value_for_attribute(AttributeIdentifier::Position(i))
.map(|x| x / 5.0),
(0.0, 1.0),
None,
),
calculate(pot, cv, (0.0, 1.0), None),
Quantization::from((self.cache.options.quantize_6, self.cache.options.quantize_8)),
);
}
Expand Down
14 changes: 14 additions & 0 deletions control/src/input/control.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Manage control input's state.

#[allow(unused_imports)]
use micromath::F32Ext;

use super::buffer::Buffer;

/// Use this to hold control input's state over time.
Expand All @@ -12,6 +15,7 @@ pub struct Control {
pub is_plugged: bool,
pub was_plugged: bool,
pub was_unplugged: bool,
pub last_value_above_noise: f32,
buffer: Buffer<4>,
}

Expand All @@ -27,6 +31,16 @@ impl Control {
}
self.was_plugged = !was_plugged && self.is_plugged;
self.was_unplugged = was_plugged && !self.is_plugged;

let value = self.buffer.read();
let diff = (self.last_value_above_noise - value).abs();
if diff > 0.01 {
self.last_value_above_noise = value;
} else if value < -4.97 {
self.last_value_above_noise = -5.0;
} else if value > 4.97 {
self.last_value_above_noise = 5.0;
}
}

pub fn value(&self) -> f32 {
Expand Down
8 changes: 7 additions & 1 deletion firmware/src/system/inputs/cvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ impl CV {
}

fn transpose_adc(sample: u32, slope: u32) -> f32 {
// NOTE: The CV input spans between -5 and +5 V.
let min = -5.0;
let span = 10.0;
// NOTE: Based on the measuring, the real span of measured CV is -4.97 to
// +4.97 V. This compensation makes sure that control value can hit both
// extremes.
let compensation = 10.0 / 9.94;
let phase = (slope as f32 - sample as f32) / slope as f32;
min + phase * span
let scaled = (min + phase * span).clamp(min, min + span);
scaled * compensation
}
6 changes: 4 additions & 2 deletions firmware/tests/cv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ mod tests {

defmt::info!("Set the input to -5 V, then click the button");
sample_until_button_is_clicked(inputs);
defmt::assert!(inputs.cvs.cv[i].value.unwrap() < 0.01);
let value = inputs.cvs.cv[i].value.unwrap();
defmt::assert!(value < -4.9999, "{:?} > -5.0", value);

defmt::info!("Set the input to +5 V, then click the button");
sample_until_button_is_clicked(inputs);
defmt::assert!(inputs.cvs.cv[i].value.unwrap() > 0.98);
let value = inputs.cvs.cv[i].value.unwrap();
defmt::assert!(value > 4.9999, "{:?} < 5.0", value);

defmt::info!("OK");
}
Expand Down

0 comments on commit f988d8c

Please sign in to comment.