Allow setting node cv inputs via slider, add preferences window

This commit is contained in:
Skyler Lehmkuhl
2025-12-17 07:38:10 -05:00
parent 88dc60f036
commit caba4305d8
19 changed files with 738 additions and 96 deletions
@@ -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_ATTACK: u32 = 0;
@@ -122,12 +122,9 @@ impl AudioNode for ADSRNode {
let frames = output.len();
for frame in 0..frames {
// Read gate input (if available)
let gate_high = if !inputs.is_empty() && frame < inputs[0].len() {
inputs[0][frame] > 0.5 // Gate is high if CV > 0.5
} else {
false
};
// Gate input: when unconnected, defaults to 0.0 (off)
let gate_cv = cv_input_or_default(inputs, 0, frame, 0.0);
let gate_high = gate_cv > 0.5;
// Detect gate transitions
if gate_high && !self.gate_was_high {
@@ -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;
use crate::dsp::biquad::BiquadFilter;
@@ -150,10 +150,19 @@ impl AudioNode for FilterNode {
output[..len].copy_from_slice(&input[..len]);
// Check for CV modulation (modulates cutoff)
if inputs.len() > 1 && !inputs[1].is_empty() {
// CV input modulates cutoff frequency
// For now, just use the base cutoff - per-sample modulation would be expensive
// TODO: Sample CV at frame rate and update filter periodically
// Sample CV at the start of the buffer - per-sample would be too expensive
let cutoff_cv = cv_input_or_default(inputs, 1, 0, self.cutoff);
if (cutoff_cv - self.cutoff).abs() > 0.01 {
// CV changed significantly, update filter
let new_cutoff = cutoff_cv.clamp(20.0, 20000.0);
match self.filter_type {
FilterType::Lowpass => {
self.filter.set_lowpass(new_cutoff, self.resonance, self.sample_rate as f32);
}
FilterType::Highpass => {
self.filter.set_highpass(new_cutoff, self.resonance, self.sample_rate as f32);
}
}
}
// Apply filter (processes stereo interleaved)
@@ -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;
use std::f32::consts::PI;
@@ -256,18 +256,11 @@ impl AudioNode for FMSynthNode {
let frames = output.len() / 2;
for frame in 0..frames {
// Read CV inputs
let voct = if inputs.len() > 0 && !inputs[0].is_empty() {
inputs[0][frame.min(inputs[0].len() / 2 - 1) * 2]
} else {
0.0
};
let gate = if inputs.len() > 1 && !inputs[1].is_empty() {
inputs[1][frame.min(inputs[1].len() / 2 - 1) * 2]
} else {
0.0
};
// Read CV inputs (both are mono signals)
// V/Oct: when unconnected, defaults to 0.0 (A4 440 Hz)
let voct = cv_input_or_default(inputs, 0, frame, 0.0);
// Gate: when unconnected, defaults to 0.0 (off)
let gate = cv_input_or_default(inputs, 1, frame, 0.0);
// Update state
self.current_frequency = Self::voct_to_freq(voct);
@@ -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_GAIN: u32 = 0;
@@ -90,15 +90,11 @@ impl AudioNode for GainNode {
let frames = input.len().min(output.len()) / 2;
for frame in 0..frames {
// Calculate final gain
let mut final_gain = self.gain;
// CV input acts as a VCA (voltage-controlled amplifier)
// CV ranges from 0.0 (silence) to 1.0 (full gain parameter value)
if inputs.len() > 1 && frame < inputs[1].len() {
let cv = inputs[1][frame];
final_gain *= cv; // Multiply gain by CV (0.0 = silence, 1.0 = full gain)
}
// When unconnected (NaN), defaults to 1.0 (no modulation, use gain parameter as-is)
let cv = cv_input_or_default(inputs, 1, frame, 1.0);
let final_gain = self.gain * cv;
// Apply gain to both channels
output[frame * 2] = input[frame * 2] * final_gain; // Left
@@ -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;
use std::f32::consts::PI;
@@ -124,26 +124,28 @@ impl AudioNode for OscillatorNode {
let frames = output.len() / 2;
for frame in 0..frames {
// Start with base frequency
let mut frequency = self.frequency;
// V/Oct input: Standard V/Oct (0V = A4 440Hz, ±1V per octave)
if !inputs.is_empty() && frame < inputs[0].len() {
let voct = inputs[0][frame]; // Read V/Oct CV (mono)
// Convert V/Oct to frequency: f = 440 * 2^(voct)
// Port 0: V/Oct CV input
// If connected, interprets the CV signal as V/Oct (440 * 2^voct)
// If unconnected, uses self.frequency directly as Hz
let voct = cv_input_or_default(inputs, 0, frame, f32::NAN);
let base_frequency = if voct.is_nan() {
// Unconnected: use frequency parameter directly
self.frequency
} else {
// Connected: convert V/Oct to frequency
// voct = 0.0 -> 440 Hz (A4)
// voct = 1.0 -> 880 Hz (A5)
// voct = -0.75 -> 261.6 Hz (C4, middle C)
frequency = 440.0 * 2.0_f32.powf(voct);
}
440.0 * 2.0_f32.powf(voct)
};
// FM input: modulates the frequency
if inputs.len() > 1 && frame < inputs[1].len() {
let fm = inputs[1][frame]; // Read FM CV (mono)
frequency *= 1.0 + fm;
}
let freq_mod = frequency;
// Port 1: FM CV input
// If connected, applies FM modulation (multiply by 1 + fm)
// If unconnected, no modulation (fm = 0.0)
let fm = cv_input_or_default(inputs, 1, frame, 0.0);
let freq_mod = base_frequency * (1.0 + fm);
// Generate waveform sample based on waveform type
let sample = match self.waveform {
+6 -12
View File
@@ -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;
use std::f32::consts::PI;
@@ -113,18 +113,12 @@ impl AudioNode for PanNode {
let frames_to_process = frames.min(output_frames);
for frame in 0..frames_to_process {
// Get base pan position
let mut pan = self.pan;
// Pan CV input: when connected, replaces parameter; when unconnected, uses parameter
// CV is in 0-1 range, mapped to -1 to +1 pan range
let cv_raw = cv_input_or_default(inputs, 1, frame, (self.pan + 1.0) * 0.5);
let pan = (cv_raw * 2.0 - 1.0).clamp(-1.0, 1.0);
// Add CV modulation if connected
if inputs.len() > 1 && frame < inputs[1].len() {
let cv = inputs[1][frame]; // CV is mono
// CV is 0-1, map to -1 to +1 range
pan += cv * 2.0 - 1.0;
pan = pan.clamp(-1.0, 1.0);
}
// Update gains if pan changed from CV
// Calculate gains using constant-power panning law
let angle = (pan + 1.0) * 0.5 * PI / 2.0;
let left_gain = angle.cos();
let right_gain = angle.sin();
@@ -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;
use std::sync::{Arc, Mutex};
@@ -202,18 +202,11 @@ impl AudioNode for SimpleSamplerNode {
let frames = output.len() / 2;
for frame in 0..frames {
// Read CV inputs
let voct = if !inputs.is_empty() && !inputs[0].is_empty() {
inputs[0][frame.min(inputs[0].len() / 2 - 1) * 2]
} else {
0.0 // Default to original pitch
};
let gate = if inputs.len() > 1 && !inputs[1].is_empty() {
inputs[1][frame.min(inputs[1].len() / 2 - 1) * 2]
} else {
0.0
};
// Read CV inputs (both are mono signals)
// V/Oct: when unconnected, defaults to 0.0 (original pitch)
let voct = cv_input_or_default(inputs, 0, frame, 0.0);
// Gate: when unconnected, defaults to 0.0 (off)
let gate = cv_input_or_default(inputs, 1, frame, 0.0);
// Detect gate trigger (rising edge)
let gate_active = gate > 0.5;
@@ -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;
use std::f32::consts::PI;
@@ -243,14 +243,11 @@ impl AudioNode for WavetableOscillatorNode {
let frames = output.len() / 2;
for frame in 0..frames {
// Read V/Oct input
let voct = if !inputs.is_empty() && !inputs[0].is_empty() {
inputs[0][frame.min(inputs[0].len() / 2 - 1) * 2]
} else {
0.0 // Default to A4 (440 Hz)
};
// V/Oct input: when unconnected, defaults to 0.0 (A4 440 Hz)
// CV signals are mono, so read from frame index directly
let voct = cv_input_or_default(inputs, 0, frame, 0.0);
// Calculate frequency
// Calculate frequency from V/Oct
let freq = self.voct_to_freq(voct);
// Read from wavetable