Work on sampler nodes, fix slew limiter

This commit is contained in:
Skyler Lehmkuhl
2026-02-16 18:45:11 -05:00
parent 93a29192fd
commit 2c0d53fb84
8 changed files with 557 additions and 33 deletions
@@ -101,8 +101,7 @@ impl OscilloscopeNode {
let inputs = vec![
NodePort::new("Audio In", SignalType::Audio, 0),
NodePort::new("V/oct", SignalType::CV, 1),
NodePort::new("CV In", SignalType::CV, 2),
NodePort::new("CV In", SignalType::CV, 1),
];
let outputs = vec![
@@ -223,13 +222,24 @@ impl AudioNode for OscilloscopeNode {
let output = &mut outputs[0];
let len = input.len().min(output.len());
// Read V/oct input if available and update trigger period
// Read CV input if available (port 1) — used for both display and V/Oct triggering
if inputs.len() > 1 && !inputs[1].is_empty() {
self.voct_value = inputs[1][0]; // Use first sample of V/oct input
let frequency = Self::voct_to_frequency(self.voct_value);
// Calculate period in samples, clamped to reasonable range
let period_samples = (sample_rate as f32 / frequency).max(1.0);
self.trigger_period = period_samples as usize;
let cv_input = inputs[1];
let cv_len = len.min(cv_input.len());
// Check if connected (not NaN sentinel)
if cv_len > 0 && !cv_input[0].is_nan() {
// Update V/Oct trigger period from CV value
self.voct_value = cv_input[0];
let frequency = Self::voct_to_frequency(self.voct_value);
let period_samples = (sample_rate as f32 / frequency).max(1.0);
self.trigger_period = period_samples as usize;
// Capture CV samples to buffer
if let Ok(mut cv_buffer) = self.cv_buffer.lock() {
cv_buffer.write(&cv_input[..cv_len]);
}
}
}
// Update sample counter for V/oct triggering
@@ -245,14 +255,6 @@ impl AudioNode for OscilloscopeNode {
buffer.write(&input[..len]);
}
// Capture CV samples if CV input is connected (input 2)
if inputs.len() > 2 && !inputs[2].is_empty() {
let cv_input = inputs[2];
if let Ok(mut cv_buffer) = self.cv_buffer.lock() {
cv_buffer.write(&cv_input[..len.min(cv_input.len())]);
}
}
// Update last sample for trigger detection (use left channel, frame 0)
if !input.is_empty() {
self.last_sample = input[0];
@@ -25,6 +25,7 @@ pub struct SimpleSamplerNode {
gain: f32,
loop_enabled: bool,
pitch_shift: f32, // Additional pitch shift in semitones
root_note: u8, // MIDI note for original pitch playback (default 69 = A4)
inputs: Vec<NodePort>,
outputs: Vec<NodePort>,
@@ -61,6 +62,7 @@ impl SimpleSamplerNode {
gain: 1.0,
loop_enabled: false,
pitch_shift: 0.0,
root_note: 69, // A4 — V/Oct 0.0 from MIDI-to-CV
inputs,
outputs,
parameters,
@@ -101,13 +103,25 @@ impl SimpleSamplerNode {
}
/// Convert V/oct CV to playback speed multiplier
/// 0V = 1.0 (original speed), +1V = 2.0 (one octave up), -1V = 0.5 (one octave down)
/// Accounts for root_note: when the incoming MIDI note matches root_note,
/// the sample plays at original speed. V/Oct 0.0 = A4 (MIDI 69) by convention.
fn voct_to_speed(&self, voct: f32) -> f32 {
// Add pitch shift parameter
let total_semitones = voct * 12.0 + self.pitch_shift;
// Offset so root_note plays at original speed
let root_offset = (self.root_note as f32 - 69.0) / 12.0;
let total_semitones = (voct - root_offset) * 12.0 + self.pitch_shift;
2.0_f32.powf(total_semitones / 12.0)
}
/// Set the root note (MIDI note number for original-pitch playback)
pub fn set_root_note(&mut self, note: u8) {
self.root_note = note.min(127);
}
/// Get the current root note
pub fn root_note(&self) -> u8 {
self.root_note
}
/// Read sample at playhead with linear interpolation
fn read_sample(&self, playhead: f32, sample: &[f32]) -> f32 {
if sample.is_empty() {
@@ -1,4 +1,4 @@
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType};
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType, cv_input_or_default};
use crate::audio::midi::MidiEvent;
const PARAM_RISE_TIME: u32 = 0;
@@ -90,9 +90,8 @@ impl AudioNode for SlewLimiterNode {
return;
}
let input = inputs[0];
let output = &mut outputs[0];
let length = input.len().min(output.len());
let length = output.len();
// Calculate maximum change per sample
let sample_duration = 1.0 / sample_rate as f32;
@@ -111,7 +110,9 @@ impl AudioNode for SlewLimiterNode {
};
for i in 0..length {
let target = input[i];
// Use cv_input_or_default to handle unconnected inputs (NaN sentinel)
// Default to last_value so output holds steady when unconnected
let target = cv_input_or_default(inputs, 0, i, self.last_value);
let difference = target - self.last_value;
let max_change = if difference > 0.0 {