Add automatable volume and pan control to default instruments

This commit is contained in:
Skyler Lehmkuhl
2026-03-23 23:27:05 -04:00
parent 434b488a4c
commit fa40173562
36 changed files with 4854 additions and 225 deletions
+52
View File
@@ -1009,6 +1009,29 @@ impl AudioGraph {
}
}
// For AutomationInput nodes, serialize display name and keyframes
if node.node_type() == "AutomationInput" {
use crate::audio::node_graph::nodes::{AutomationInputNode, InterpolationType};
use crate::audio::node_graph::preset::SerializedKeyframe;
if let Some(auto_node) = node.as_any().downcast_ref::<AutomationInputNode>() {
serialized.automation_display_name = Some(auto_node.display_name().to_string());
serialized.automation_keyframes = auto_node.keyframes().iter().map(|kf| {
SerializedKeyframe {
time: kf.time,
value: kf.value,
interpolation: match kf.interpolation {
InterpolationType::Linear => "linear",
InterpolationType::Bezier => "bezier",
InterpolationType::Step => "step",
InterpolationType::Hold => "hold",
}.to_string(),
ease_out: kf.ease_out,
ease_in: kf.ease_in,
}
}).collect();
}
}
// For AmpSim nodes, serialize the model path
if node.node_type() == "AmpSim" {
use crate::audio::node_graph::nodes::AmpSimNode;
@@ -1322,6 +1345,35 @@ impl AudioGraph {
}
}
// Restore AutomationInput display name and keyframes
if serialized_node.node_type == "AutomationInput" {
use crate::audio::node_graph::nodes::{AutomationInputNode, AutomationKeyframe, InterpolationType};
if let Some(graph_node) = graph.graph.node_weight_mut(node_idx) {
if let Some(auto_node) = graph_node.node.as_any_mut().downcast_mut::<AutomationInputNode>() {
if let Some(ref name) = serialized_node.automation_display_name {
auto_node.set_display_name(name.clone());
}
if !serialized_node.automation_keyframes.is_empty() {
auto_node.clear_keyframes();
for kf in &serialized_node.automation_keyframes {
auto_node.add_keyframe(AutomationKeyframe {
time: kf.time,
value: kf.value,
interpolation: match kf.interpolation.as_str() {
"bezier" => InterpolationType::Bezier,
"step" => InterpolationType::Step,
"hold" => InterpolationType::Hold,
_ => InterpolationType::Linear,
},
ease_out: kf.ease_out,
ease_in: kf.ease_in,
});
}
}
}
}
}
// Restore position
graph.set_node_position(node_idx, serialized_node.position.0, serialized_node.position.1);
}
@@ -1,4 +1,4 @@
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, SignalType};
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType};
use crate::audio::midi::MidiEvent;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock};
@@ -63,12 +63,17 @@ impl AutomationInputNode {
NodePort::new("CV Out", SignalType::CV, 0),
];
let parameters = vec![
Parameter::new(0, "Min", f32::NEG_INFINITY, f32::INFINITY, -1.0, ParameterUnit::Generic),
Parameter::new(1, "Max", f32::NEG_INFINITY, f32::INFINITY, 1.0, ParameterUnit::Generic),
];
Self {
name: name.clone(),
display_name: "Automation".to_string(),
keyframes: vec![AutomationKeyframe::new(0.0, 0.0)],
outputs,
parameters: Vec::new(),
parameters,
playback_time: Arc::new(RwLock::new(0.0)),
value_min: -1.0,
value_max: 1.0,
@@ -221,12 +226,20 @@ impl AudioNode for AutomationInputNode {
&self.parameters
}
fn set_parameter(&mut self, _id: u32, _value: f32) {
// No parameters
fn set_parameter(&mut self, id: u32, value: f32) {
match id {
0 => self.value_min = value,
1 => self.value_max = value,
_ => {}
}
}
fn get_parameter(&self, _id: u32) -> f32 {
0.0
fn get_parameter(&self, id: u32) -> f32 {
match id {
0 => self.value_min,
1 => self.value_max,
_ => 0.0,
}
}
fn process(
@@ -113,10 +113,8 @@ impl AudioNode for PanNode {
let frames_to_process = frames.min(output_frames);
for frame in 0..frames_to_process {
// 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);
// Pan CV input: -1..+1 directly (0 = center), defaults to parameter value when unconnected
let pan = cv_input_or_default(inputs, 1, frame, self.pan).clamp(-1.0, 1.0);
// Calculate gains using constant-power panning law
let angle = (pan + 1.0) * 0.5 * PI / 2.0;
@@ -100,6 +100,16 @@ fn default_version() -> u32 {
1
}
/// Serialized keyframe for AutomationInput nodes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializedKeyframe {
pub time: f64,
pub value: f32,
pub interpolation: String,
pub ease_out: (f32, f32),
pub ease_in: (f32, f32),
}
/// Serialized node representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializedNode {
@@ -141,6 +151,14 @@ pub struct SerializedNode {
/// Allows the UI to display actual track names on the node's output ports.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub port_names: Vec<String>,
/// For AutomationInput nodes: user-visible display name
#[serde(skip_serializing_if = "Option::is_none")]
pub automation_display_name: Option<String>,
/// For AutomationInput nodes: saved keyframes
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub automation_keyframes: Vec<SerializedKeyframe>,
}
/// Serialized group definition (frontend-only visual grouping, stored opaquely by backend)
@@ -239,6 +257,8 @@ impl SerializedNode {
nam_model_path: None,
num_ports: None,
port_names: Vec::new(),
automation_display_name: None,
automation_keyframes: Vec::new(),
}
}