add voice allocator node

This commit is contained in:
Skyler Lehmkuhl
2026-02-15 23:10:00 -05:00
parent 0a27e4d328
commit 72f10db64d
10 changed files with 866 additions and 235 deletions
+84 -4
View File
@@ -1131,8 +1131,10 @@ impl Engine {
// Save position
graph.set_node_position(node_idx, x, y);
// Automatically set MIDI-receiving nodes as MIDI targets
if node_type == "MidiInput" || node_type == "VoiceAllocator" {
// Automatically set MIDI source nodes as MIDI targets
// VoiceAllocator receives MIDI through its input port via connections,
// not directly — it needs a MidiInput node connected to its MIDI In
if node_type == "MidiInput" {
graph.set_midi_target(node_idx, true);
}
@@ -1149,7 +1151,7 @@ impl Engine {
}
}
Command::GraphAddNodeToTemplate(track_id, voice_allocator_id, node_type, _x, _y) => {
Command::GraphAddNodeToTemplate(track_id, voice_allocator_id, node_type, x, y) => {
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
let graph = &mut track.instrument_graph;
{
@@ -1209,7 +1211,9 @@ impl Engine {
// Add node to VoiceAllocator's template graph
match graph.add_node_to_voice_allocator_template(va_idx, node) {
Ok(node_id) => {
println!("Added node {} (ID: {}) to VoiceAllocator {} template", node_type, node_id, voice_allocator_id);
// Set node position in the template graph
graph.set_position_in_voice_allocator_template(va_idx, node_id, x, y);
println!("Added node {} (ID: {}) to VoiceAllocator {} template at ({}, {})", node_type, node_id, voice_allocator_id, x, y);
let _ = self.event_tx.push(AudioEvent::GraphNodeAdded(track_id, node_id, node_type.clone()));
}
Err(e) => {
@@ -1298,6 +1302,58 @@ impl Engine {
}
}
Command::GraphDisconnectInTemplate(track_id, voice_allocator_id, from, from_port, to, to_port) => {
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
let graph = &mut track.instrument_graph;
let va_idx = NodeIndex::new(voice_allocator_id as usize);
match graph.disconnect_in_voice_allocator_template(va_idx, from, from_port, to, to_port) {
Ok(()) => {
let _ = self.event_tx.push(AudioEvent::GraphStateChanged(track_id));
}
Err(e) => {
let _ = self.event_tx.push(AudioEvent::GraphConnectionError(
track_id,
format!("Failed to disconnect in template: {}", e)
));
}
}
}
}
Command::GraphRemoveNodeFromTemplate(track_id, voice_allocator_id, node_index) => {
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
let graph = &mut track.instrument_graph;
let va_idx = NodeIndex::new(voice_allocator_id as usize);
match graph.remove_node_from_voice_allocator_template(va_idx, node_index) {
Ok(()) => {
let _ = self.event_tx.push(AudioEvent::GraphStateChanged(track_id));
}
Err(e) => {
let _ = self.event_tx.push(AudioEvent::GraphConnectionError(
track_id,
format!("Failed to remove node from template: {}", e)
));
}
}
}
}
Command::GraphSetParameterInTemplate(track_id, voice_allocator_id, node_index, param_id, value) => {
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
let graph = &mut track.instrument_graph;
let va_idx = NodeIndex::new(voice_allocator_id as usize);
if let Err(e) = graph.set_parameter_in_voice_allocator_template(va_idx, node_index, param_id, value) {
let _ = self.event_tx.push(AudioEvent::GraphConnectionError(
track_id,
format!("Failed to set parameter in template: {}", e)
));
}
}
}
Command::GraphDisconnect(track_id, from, from_port, to, to_port) => {
eprintln!("[AUDIO ENGINE] GraphDisconnect: track={}, from={}, from_port={}, to={}, to_port={}", track_id, from, from_port, to, to_port);
let graph = match self.project.get_track_mut(track_id) {
@@ -1346,6 +1402,14 @@ impl Engine {
}
}
Command::GraphSetNodePositionInTemplate(track_id, voice_allocator_id, node_index, x, y) => {
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
let graph = &mut track.instrument_graph;
let va_idx = NodeIndex::new(voice_allocator_id as usize);
graph.set_position_in_voice_allocator_template(va_idx, node_index, x, y);
}
}
Command::GraphSetMidiTarget(track_id, node_index, enabled) => {
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
let graph = &mut track.instrument_graph;
@@ -2945,6 +3009,18 @@ impl EngineController {
let _ = self.command_tx.push(Command::GraphConnectInTemplate(track_id, voice_allocator_id, from_node, from_port, to_node, to_port));
}
pub fn graph_disconnect_in_template(&mut self, track_id: TrackId, voice_allocator_id: u32, from_node: u32, from_port: usize, to_node: u32, to_port: usize) {
let _ = self.command_tx.push(Command::GraphDisconnectInTemplate(track_id, voice_allocator_id, from_node, from_port, to_node, to_port));
}
pub fn graph_remove_node_from_template(&mut self, track_id: TrackId, voice_allocator_id: u32, node_id: u32) {
let _ = self.command_tx.push(Command::GraphRemoveNodeFromTemplate(track_id, voice_allocator_id, node_id));
}
pub fn graph_set_parameter_in_template(&mut self, track_id: TrackId, voice_allocator_id: u32, node_id: u32, param_id: u32, value: f32) {
let _ = self.command_tx.push(Command::GraphSetParameterInTemplate(track_id, voice_allocator_id, node_id, param_id, value));
}
/// Remove a node from a track's instrument graph
pub fn graph_remove_node(&mut self, track_id: TrackId, node_id: u32) {
let _ = self.command_tx.push(Command::GraphRemoveNode(track_id, node_id));
@@ -2970,6 +3046,10 @@ impl EngineController {
let _ = self.command_tx.push(Command::GraphSetNodePosition(track_id, node_id, x, y));
}
pub fn graph_set_node_position_in_template(&mut self, track_id: TrackId, voice_allocator_id: u32, node_id: u32, x: f32, y: f32) {
let _ = self.command_tx.push(Command::GraphSetNodePositionInTemplate(track_id, voice_allocator_id, node_id, x, y));
}
/// Set which node receives MIDI events in a track's instrument graph
pub fn graph_set_midi_target(&mut self, track_id: TrackId, node_id: u32, enabled: bool) {
let _ = self.command_tx.push(Command::GraphSetMidiTarget(track_id, node_id, enabled));
+132
View File
@@ -352,6 +352,138 @@ impl AudioGraph {
Err("VoiceAllocator node not found".to_string())
}
/// Disconnect two nodes in a VoiceAllocator's template graph
pub fn disconnect_in_voice_allocator_template(
&mut self,
voice_allocator_idx: NodeIndex,
from_node: u32,
from_port: usize,
to_node: u32,
to_port: usize,
) -> Result<(), String> {
use crate::audio::node_graph::nodes::VoiceAllocatorNode;
if let Some(graph_node) = self.graph.node_weight_mut(voice_allocator_idx) {
if graph_node.node.node_type() != "VoiceAllocator" {
return Err("Node is not a VoiceAllocator".to_string());
}
let node_ptr = &mut *graph_node.node as *mut dyn AudioNode;
// SAFETY: We just checked that this is a VoiceAllocator
unsafe {
let va_ptr = node_ptr as *mut VoiceAllocatorNode;
let va = &mut *va_ptr;
let from_idx = NodeIndex::new(from_node as usize);
let to_idx = NodeIndex::new(to_node as usize);
va.template_graph_mut().disconnect(from_idx, from_port, to_idx, to_port);
va.rebuild_voices();
return Ok(());
}
}
Err("VoiceAllocator node not found".to_string())
}
/// Remove a node from a VoiceAllocator's template graph
pub fn remove_node_from_voice_allocator_template(
&mut self,
voice_allocator_idx: NodeIndex,
node_id: u32,
) -> Result<(), String> {
use crate::audio::node_graph::nodes::VoiceAllocatorNode;
if let Some(graph_node) = self.graph.node_weight_mut(voice_allocator_idx) {
if graph_node.node.node_type() != "VoiceAllocator" {
return Err("Node is not a VoiceAllocator".to_string());
}
let node_ptr = &mut *graph_node.node as *mut dyn AudioNode;
// SAFETY: We just checked that this is a VoiceAllocator
unsafe {
let va_ptr = node_ptr as *mut VoiceAllocatorNode;
let va = &mut *va_ptr;
let node_idx = NodeIndex::new(node_id as usize);
va.template_graph_mut().remove_node(node_idx);
va.rebuild_voices();
return Ok(());
}
}
Err("VoiceAllocator node not found".to_string())
}
/// Set a parameter on a node in a VoiceAllocator's template graph
pub fn set_parameter_in_voice_allocator_template(
&mut self,
voice_allocator_idx: NodeIndex,
node_id: u32,
param_id: u32,
value: f32,
) -> Result<(), String> {
use crate::audio::node_graph::nodes::VoiceAllocatorNode;
if let Some(graph_node) = self.graph.node_weight_mut(voice_allocator_idx) {
if graph_node.node.node_type() != "VoiceAllocator" {
return Err("Node is not a VoiceAllocator".to_string());
}
let node_ptr = &mut *graph_node.node as *mut dyn AudioNode;
// SAFETY: We just checked that this is a VoiceAllocator
unsafe {
let va_ptr = node_ptr as *mut VoiceAllocatorNode;
let va = &mut *va_ptr;
let node_idx = NodeIndex::new(node_id as usize);
if let Some(template_node) = va.template_graph_mut().get_graph_node_mut(node_idx) {
template_node.node.set_parameter(param_id, value);
} else {
return Err("Node not found in template".to_string());
}
va.rebuild_voices();
return Ok(());
}
}
Err("VoiceAllocator node not found".to_string())
}
/// Set the position of a node in a VoiceAllocator's template graph
pub fn set_position_in_voice_allocator_template(
&mut self,
voice_allocator_idx: NodeIndex,
node_id: u32,
x: f32,
y: f32,
) {
use crate::audio::node_graph::nodes::VoiceAllocatorNode;
if let Some(graph_node) = self.graph.node_weight_mut(voice_allocator_idx) {
if graph_node.node.node_type() != "VoiceAllocator" {
return;
}
let node_ptr = &mut *graph_node.node as *mut dyn AudioNode;
// SAFETY: We just checked that this is a VoiceAllocator
unsafe {
let va_ptr = node_ptr as *mut VoiceAllocatorNode;
let va = &mut *va_ptr;
let node_idx = NodeIndex::new(node_id as usize);
va.template_graph_mut().set_node_position(node_idx, x, y);
}
}
}
/// Process the graph and produce audio output
pub fn process(&mut self, output_buffer: &mut [f32], midi_events: &[MidiEvent], playback_time: f64) {
// Update playback time
@@ -72,8 +72,19 @@ impl VoiceAllocatorNode {
Parameter::new(PARAM_VOICE_COUNT, "Voices", 1.0, MAX_VOICES as f32, DEFAULT_VOICES as f32, ParameterUnit::Generic),
];
// Create empty template graph
let template_graph = AudioGraph::new(sample_rate, buffer_size);
// Create template graph with default TemplateInput and TemplateOutput nodes
let mut template_graph = AudioGraph::new(sample_rate, buffer_size);
{
use super::template_io::{TemplateInputNode, TemplateOutputNode};
let input_node = Box::new(TemplateInputNode::new("Template Input"));
let output_node = Box::new(TemplateOutputNode::new("Template Output"));
let input_idx = template_graph.add_node(input_node);
let output_idx = template_graph.add_node(output_node);
template_graph.set_node_position(input_idx, -200.0, 0.0);
template_graph.set_node_position(output_idx, 200.0, 0.0);
template_graph.set_midi_target(input_idx, true);
template_graph.set_output_node(Some(output_idx));
}
// Create voice instances (initially empty clones of template)
let voice_instances: Vec<AudioGraph> = (0..MAX_VOICES)
+17 -2
View File
@@ -3,7 +3,7 @@ use super::clip::{AudioClipInstance, AudioClipInstanceId};
use super::midi::{MidiClipInstance, MidiClipInstanceId, MidiEvent};
use super::midi_pool::MidiClipPool;
use super::node_graph::AudioGraph;
use super::node_graph::nodes::{AudioInputNode, AudioOutputNode};
use super::node_graph::nodes::{AudioInputNode, AudioOutputNode, MidiInputNode};
use super::node_graph::preset::GraphPreset;
use super::pool::AudioClipPool;
use serde::{Serialize, Deserialize};
@@ -365,12 +365,27 @@ impl MidiTrack {
// Use a large buffer size that can accommodate any callback
let default_buffer_size = 8192;
// Create default instrument graph with MidiInput and AudioOutput
let mut instrument_graph = AudioGraph::new(sample_rate, default_buffer_size);
// Add MidiInput node (entry point for MIDI events)
let midi_input_node = Box::new(MidiInputNode::new("MIDI Input"));
let midi_input_id = instrument_graph.add_node(midi_input_node);
instrument_graph.set_node_position(midi_input_id, 100.0, 150.0);
instrument_graph.set_midi_target(midi_input_id, true);
// Add AudioOutput node (final audio output)
let audio_output_node = Box::new(AudioOutputNode::new("Audio Output"));
let audio_output_id = instrument_graph.add_node(audio_output_node);
instrument_graph.set_node_position(audio_output_id, 700.0, 150.0);
instrument_graph.set_output_node(Some(audio_output_id));
Self {
id,
name,
clip_instances: Vec::new(),
instrument_graph_preset: None,
instrument_graph: AudioGraph::new(sample_rate, default_buffer_size),
instrument_graph,
volume: 1.0,
muted: false,
solo: false,
+8
View File
@@ -146,10 +146,18 @@ pub enum Command {
GraphConnectInTemplate(TrackId, u32, u32, usize, u32, usize),
/// Disconnect two nodes in a track's graph (track_id, from_node, from_port, to_node, to_port)
GraphDisconnect(TrackId, u32, usize, u32, usize),
/// Disconnect nodes in a VoiceAllocator template (track_id, voice_allocator_node_id, from_node, from_port, to_node, to_port)
GraphDisconnectInTemplate(TrackId, u32, u32, usize, u32, usize),
/// Remove a node from a VoiceAllocator's template graph (track_id, voice_allocator_node_id, node_index)
GraphRemoveNodeFromTemplate(TrackId, u32, u32),
/// Set a parameter on a node (track_id, node_index, param_id, value)
GraphSetParameter(TrackId, u32, u32, f32),
/// Set a parameter on a node in a VoiceAllocator's template graph (track_id, voice_allocator_node_id, node_index, param_id, value)
GraphSetParameterInTemplate(TrackId, u32, u32, u32, f32),
/// Set the UI position of a node (track_id, node_index, x, y)
GraphSetNodePosition(TrackId, u32, f32, f32),
/// Set the UI position of a node in a VoiceAllocator's template (track_id, voice_allocator_id, node_index, x, y)
GraphSetNodePositionInTemplate(TrackId, u32, u32, f32, f32),
/// Set which node receives MIDI events (track_id, node_index, enabled)
GraphSetMidiTarget(TrackId, u32, bool),
/// Set which node is the audio output (track_id, node_index)