Allow setting node cv inputs via slider, add preferences window
This commit is contained in:
@@ -386,10 +386,19 @@ impl AudioGraph {
|
||||
let num_audio_cv_inputs = inputs.iter().filter(|p| p.signal_type != SignalType::Midi).count();
|
||||
let num_midi_inputs = inputs.iter().filter(|p| p.signal_type == SignalType::Midi).count();
|
||||
|
||||
// Clear audio/CV input buffers
|
||||
for i in 0..num_audio_cv_inputs {
|
||||
if i < self.input_buffers.len() {
|
||||
self.input_buffers[i].fill(0.0);
|
||||
// Clear input buffers
|
||||
// - Audio inputs: fill with 0.0 (silence) when unconnected
|
||||
// - CV inputs: fill with NaN to indicate "no connection" (allows nodes to use parameter values)
|
||||
let mut audio_cv_idx = 0;
|
||||
for port in inputs.iter().filter(|p| p.signal_type != SignalType::Midi) {
|
||||
if audio_cv_idx < self.input_buffers.len() {
|
||||
let fill_value = match port.signal_type {
|
||||
SignalType::Audio => 0.0, // Silence for audio
|
||||
SignalType::CV => f32::NAN, // Sentinel for CV
|
||||
SignalType::Midi => unreachable!(), // Already filtered out
|
||||
};
|
||||
self.input_buffers[audio_cv_idx].fill(fill_value);
|
||||
audio_cv_idx += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,7 +428,12 @@ impl AudioGraph {
|
||||
let source_buffer = &source_node.output_buffers[conn.from_port];
|
||||
if conn.to_port < self.input_buffers.len() {
|
||||
for (dst, src) in self.input_buffers[conn.to_port].iter_mut().zip(source_buffer.iter()) {
|
||||
*dst += src;
|
||||
// If dst is NaN (unconnected), replace it; otherwise add (for mixing)
|
||||
if dst.is_nan() {
|
||||
*dst = *src;
|
||||
} else {
|
||||
*dst += src;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@ pub mod nodes;
|
||||
pub mod preset;
|
||||
|
||||
pub use graph::{Connection, GraphNode, AudioGraph};
|
||||
pub use node_trait::AudioNode;
|
||||
pub use node_trait::{AudioNode, cv_input_or_default};
|
||||
pub use preset::{GraphPreset, PresetMetadata, SerializedConnection, SerializedNode};
|
||||
pub use types::{ConnectionError, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType};
|
||||
|
||||
@@ -77,3 +77,34 @@ pub trait AudioNode: Send {
|
||||
/// Downcast to `&dyn Any` for type-specific read-only operations
|
||||
fn as_any(&self) -> &dyn std::any::Any;
|
||||
}
|
||||
|
||||
/// Helper function for CV inputs with optional connections
|
||||
///
|
||||
/// Returns the input value if connected (not NaN), otherwise returns the default value.
|
||||
/// This implements "Blender-style" input behavior where parameters are replaced by
|
||||
/// connected inputs.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `inputs` - Input buffer array from process()
|
||||
/// * `port` - Input port index
|
||||
/// * `frame` - Current frame index
|
||||
/// * `default` - Default value to use when input is unconnected
|
||||
///
|
||||
/// # Returns
|
||||
/// The input value if connected, otherwise the default value
|
||||
#[inline]
|
||||
pub fn cv_input_or_default(inputs: &[&[f32]], port: usize, frame: usize, default: f32) -> f32 {
|
||||
if port < inputs.len() && frame < inputs[port].len() {
|
||||
let value = inputs[port][frame];
|
||||
if value.is_nan() {
|
||||
// Unconnected: use default parameter value
|
||||
default
|
||||
} else {
|
||||
// Connected: use input signal
|
||||
value
|
||||
}
|
||||
} else {
|
||||
// No input buffer: use default
|
||||
default
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user