Make beats canonical representation rather than seconds
This commit is contained in:
@@ -113,6 +113,8 @@ pub struct Engine {
|
||||
// Current tempo map — kept in sync with SetTempo/SetTempoMap commands.
|
||||
tempo_map: crate::TempoMap,
|
||||
current_fps: f64,
|
||||
// Current time signature — updated by SetTempo, used when SetTempoMap fires.
|
||||
time_sig: (u32, u32),
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
@@ -191,6 +193,7 @@ impl Engine {
|
||||
timing_overrun_count: 0,
|
||||
tempo_map: crate::TempoMap::constant(120.0),
|
||||
current_fps: 30.0,
|
||||
time_sig: (4, 4),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1290,11 +1293,19 @@ impl Engine {
|
||||
}
|
||||
|
||||
Command::SetTempo(bpm, time_sig) => {
|
||||
self.time_sig = time_sig;
|
||||
self.metronome.update_timing(bpm, time_sig);
|
||||
self.project.set_tempo(bpm, time_sig.0);
|
||||
self.tempo_map.set_global_bpm(bpm as f64);
|
||||
}
|
||||
|
||||
Command::SetTempoMap(map) => {
|
||||
let bpm0 = map.global_bpm() as f32;
|
||||
self.metronome.update_timing(bpm0, self.time_sig);
|
||||
self.project.set_tempo(bpm0, self.time_sig.0);
|
||||
self.tempo_map = map;
|
||||
}
|
||||
|
||||
// Node graph commands
|
||||
Command::GraphAddNode(track_id, node_type, x, y) => {
|
||||
eprintln!("[DEBUG] GraphAddNode received: track_id={}, node_type={}, x={}, y={}", track_id, node_type, x, y);
|
||||
@@ -1846,9 +1857,17 @@ impl Engine {
|
||||
let buffer_size = self.buffer_pool.buffer_size();
|
||||
if let Some(TrackNode::Group(metatrack)) = self.project.get_track_mut(track_id) {
|
||||
let current = metatrack.current_subtracks();
|
||||
let has_subtrack_node = metatrack.audio_graph.node_indices().any(|idx| {
|
||||
metatrack.audio_graph.get_graph_node(idx)
|
||||
.map(|n| n.node.node_type() == "SubtrackInputs")
|
||||
.unwrap_or(false)
|
||||
});
|
||||
|
||||
// No-op if subtrack list is unchanged (prevents every-frame graph rebuilds)
|
||||
if current == subtracks {
|
||||
// No-op if subtrack list is unchanged AND the graph is already initialised.
|
||||
// Without the `has_subtrack_node` guard a freshly-created metatrack
|
||||
// (empty graph, no SubtrackInputs) with zero subtracks would never get
|
||||
// its default Volume automation node.
|
||||
if current == subtracks && has_subtrack_node {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2156,8 +2175,13 @@ impl Engine {
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
|
||||
let graph = &mut track.instrument_graph;
|
||||
let graph = match self.project.get_track_mut(track_id) {
|
||||
Some(TrackNode::Midi(t)) => Some(&mut t.instrument_graph),
|
||||
Some(TrackNode::Audio(t)) => Some(&mut t.effects_graph),
|
||||
Some(TrackNode::Group(t)) => Some(&mut t.audio_graph),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(graph) = graph {
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
|
||||
if let Some(graph_node) = graph.get_graph_node_mut(node_idx) {
|
||||
@@ -2181,8 +2205,13 @@ impl Engine {
|
||||
Command::AutomationRemoveKeyframe(track_id, node_id, time) => {
|
||||
use crate::audio::node_graph::nodes::AutomationInputNode;
|
||||
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
|
||||
let graph = &mut track.instrument_graph;
|
||||
let graph = match self.project.get_track_mut(track_id) {
|
||||
Some(TrackNode::Midi(t)) => Some(&mut t.instrument_graph),
|
||||
Some(TrackNode::Audio(t)) => Some(&mut t.effects_graph),
|
||||
Some(TrackNode::Group(t)) => Some(&mut t.audio_graph),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(graph) = graph {
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
|
||||
if let Some(graph_node) = graph.get_graph_node_mut(node_idx) {
|
||||
@@ -2198,8 +2227,13 @@ impl Engine {
|
||||
Command::AutomationSetName(track_id, node_id, name) => {
|
||||
use crate::audio::node_graph::nodes::AutomationInputNode;
|
||||
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
|
||||
let graph = &mut track.instrument_graph;
|
||||
let graph = match self.project.get_track_mut(track_id) {
|
||||
Some(TrackNode::Midi(t)) => Some(&mut t.instrument_graph),
|
||||
Some(TrackNode::Audio(t)) => Some(&mut t.effects_graph),
|
||||
Some(TrackNode::Group(t)) => Some(&mut t.audio_graph),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(graph) = graph {
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
|
||||
if let Some(graph_node) = graph.get_graph_node_mut(node_idx) {
|
||||
@@ -2508,12 +2542,15 @@ impl Engine {
|
||||
use crate::audio::node_graph::nodes::{AutomationInputNode, InterpolationType};
|
||||
use crate::command::types::AutomationKeyframeData;
|
||||
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track(track_id) {
|
||||
let graph = &track.instrument_graph;
|
||||
let graph = match self.project.get_track(track_id) {
|
||||
Some(TrackNode::Midi(t)) => Some(&t.instrument_graph),
|
||||
Some(TrackNode::Audio(t)) => Some(&t.effects_graph),
|
||||
Some(TrackNode::Group(t)) => Some(&t.audio_graph),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(graph) = graph {
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
|
||||
if let Some(graph_node) = graph.get_graph_node(node_idx) {
|
||||
// Downcast to AutomationInputNode
|
||||
if let Some(auto_node) = graph_node.node.as_any().downcast_ref::<AutomationInputNode>() {
|
||||
let keyframes: Vec<AutomationKeyframeData> = auto_node.keyframes()
|
||||
.iter()
|
||||
@@ -2524,7 +2561,6 @@ impl Engine {
|
||||
InterpolationType::Step => "step",
|
||||
InterpolationType::Hold => "hold",
|
||||
}.to_string();
|
||||
|
||||
AutomationKeyframeData {
|
||||
time: kf.time.0,
|
||||
value: kf.value,
|
||||
@@ -2534,7 +2570,6 @@ impl Engine {
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
QueryResponse::AutomationKeyframes(Ok(keyframes))
|
||||
} else {
|
||||
QueryResponse::AutomationKeyframes(Err(format!("Node {} is not an AutomationInputNode", node_id)))
|
||||
@@ -2543,19 +2578,22 @@ impl Engine {
|
||||
QueryResponse::AutomationKeyframes(Err(format!("Node {} not found in track {}", node_id, track_id)))
|
||||
}
|
||||
} else {
|
||||
QueryResponse::AutomationKeyframes(Err(format!("Track {} not found or is not a MIDI track", track_id)))
|
||||
QueryResponse::AutomationKeyframes(Err(format!("Track {} not found", track_id)))
|
||||
}
|
||||
}
|
||||
|
||||
Query::GetAutomationName(track_id, node_id) => {
|
||||
use crate::audio::node_graph::nodes::AutomationInputNode;
|
||||
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track(track_id) {
|
||||
let graph = &track.instrument_graph;
|
||||
let graph = match self.project.get_track(track_id) {
|
||||
Some(TrackNode::Midi(t)) => Some(&t.instrument_graph),
|
||||
Some(TrackNode::Audio(t)) => Some(&t.effects_graph),
|
||||
Some(TrackNode::Group(t)) => Some(&t.audio_graph),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(graph) = graph {
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
|
||||
if let Some(graph_node) = graph.get_graph_node(node_idx) {
|
||||
// Downcast to AutomationInputNode
|
||||
if let Some(auto_node) = graph_node.node.as_any().downcast_ref::<AutomationInputNode>() {
|
||||
QueryResponse::AutomationName(Ok(auto_node.display_name().to_string()))
|
||||
} else {
|
||||
@@ -2565,17 +2603,21 @@ impl Engine {
|
||||
QueryResponse::AutomationName(Err(format!("Node {} not found in track {}", node_id, track_id)))
|
||||
}
|
||||
} else {
|
||||
QueryResponse::AutomationName(Err(format!("Track {} not found or is not a MIDI track", track_id)))
|
||||
QueryResponse::AutomationName(Err(format!("Track {} not found", track_id)))
|
||||
}
|
||||
}
|
||||
|
||||
Query::GetAutomationRange(track_id, node_id) => {
|
||||
use crate::audio::node_graph::nodes::AutomationInputNode;
|
||||
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track(track_id) {
|
||||
let graph = &track.instrument_graph;
|
||||
let graph = match self.project.get_track(track_id) {
|
||||
Some(TrackNode::Midi(t)) => Some(&t.instrument_graph),
|
||||
Some(TrackNode::Audio(t)) => Some(&t.effects_graph),
|
||||
Some(TrackNode::Group(t)) => Some(&t.audio_graph),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(graph) = graph {
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
|
||||
if let Some(graph_node) = graph.get_graph_node(node_idx) {
|
||||
if let Some(auto_node) = graph_node.node.as_any().downcast_ref::<AutomationInputNode>() {
|
||||
QueryResponse::AutomationRange(Ok((auto_node.value_min, auto_node.value_max)))
|
||||
@@ -2586,7 +2628,7 @@ impl Engine {
|
||||
QueryResponse::AutomationRange(Err(format!("Node {} not found", node_id)))
|
||||
}
|
||||
} else {
|
||||
QueryResponse::AutomationRange(Err(format!("Track {} not found or is not a MIDI track", track_id)))
|
||||
QueryResponse::AutomationRange(Err(format!("Track {} not found", track_id)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3665,6 +3707,11 @@ impl EngineController {
|
||||
let _ = self.command_tx.push(Command::SetTempo(bpm, time_signature));
|
||||
}
|
||||
|
||||
/// Replace the full tempo map (multi-entry variable tempo)
|
||||
pub fn set_tempo_map(&mut self, map: crate::TempoMap) {
|
||||
let _ = self.command_tx.push(Command::SetTempoMap(map));
|
||||
}
|
||||
|
||||
/// After a BPM change: update MIDI clip durations, sync clip beats/frames, and rescale
|
||||
/// automation keyframe times to preserve beat positions.
|
||||
/// `from_bpm` is the BPM before the change; `to_bpm` is the new BPM.
|
||||
|
||||
@@ -263,7 +263,11 @@ impl Metatrack {
|
||||
graph
|
||||
}
|
||||
|
||||
/// Build the explicit subtrack mixing graph: SubtrackInputs → Mixer → AudioOutput.
|
||||
/// Build the default subtrack mixing graph: SubtrackInputs → Mixer → Gain → AudioOutput,
|
||||
/// with an AutomationInput ("Volume", range 0..2) feeding the Gain's CV port.
|
||||
///
|
||||
/// Existing Volume keyframes are preserved across rebuilds so that adding/removing
|
||||
/// a child track doesn't reset the automation.
|
||||
///
|
||||
/// `subtracks` is an ordered list of (backend TrackId, display name) for each child.
|
||||
/// Replaces the current graph and marks `graph_is_default = true`.
|
||||
@@ -273,35 +277,60 @@ impl Metatrack {
|
||||
sample_rate: u32,
|
||||
buffer_size: usize,
|
||||
) {
|
||||
use super::node_graph::nodes::{SubtrackInputsNode, MixerNode};
|
||||
use super::node_graph::nodes::{SubtrackInputsNode, MixerNode, GainNode, AutomationInputNode};
|
||||
use super::node_graph::nodes::AutomationKeyframe;
|
||||
use crate::time::Beats;
|
||||
|
||||
// Preserve existing Volume keyframes before rebuilding.
|
||||
let existing_volume_kfs = self.get_volume_automation_keyframes();
|
||||
|
||||
let n = subtracks.len();
|
||||
let mut graph = AudioGraph::new(sample_rate, buffer_size);
|
||||
|
||||
// SubtrackInputs node (N outputs, one per child)
|
||||
// NOTE: `new()` initialises buffers as zero-length; call `update_subtracks` immediately
|
||||
// to allocate stereo interleaved buffers (buffer_size * 2 samples each).
|
||||
let mut inputs_node = SubtrackInputsNode::new("Subtrack Inputs", subtracks);
|
||||
let subtracks_copy = inputs_node.subtracks().to_vec();
|
||||
inputs_node.update_subtracks(subtracks_copy, buffer_size);
|
||||
let inputs_id = graph.add_node(Box::new(inputs_node));
|
||||
graph.set_node_position(inputs_id, 100.0, 150.0);
|
||||
|
||||
// Mixer node (starts with 1 spare; grows as connections are made)
|
||||
// Mixer node
|
||||
let mixer_node = Box::new(MixerNode::new("Mixer"));
|
||||
let mixer_id = graph.add_node(mixer_node);
|
||||
graph.set_node_position(mixer_id, 350.0, 150.0);
|
||||
graph.set_node_position(mixer_id, 330.0, 150.0);
|
||||
|
||||
// Gain node — group volume control
|
||||
let gain_id = graph.add_node(Box::new(GainNode::new("Volume")));
|
||||
graph.set_node_position(gain_id, 520.0, 150.0);
|
||||
|
||||
// AutomationInput — drives the Gain's CV port
|
||||
let mut auto_node = AutomationInputNode::new("Volume CV");
|
||||
auto_node.set_display_name("Volume".to_string());
|
||||
auto_node.value_min = 0.0;
|
||||
auto_node.value_max = 2.0;
|
||||
auto_node.clear_keyframes();
|
||||
if existing_volume_kfs.is_empty() {
|
||||
auto_node.add_keyframe(AutomationKeyframe::new(Beats::ZERO, 1.0));
|
||||
} else {
|
||||
for kf in existing_volume_kfs {
|
||||
auto_node.add_keyframe(kf);
|
||||
}
|
||||
}
|
||||
let auto_id = graph.add_node(Box::new(auto_node));
|
||||
graph.set_node_position(auto_id, 520.0, 320.0);
|
||||
|
||||
// AudioOutput node
|
||||
let output_node = Box::new(AudioOutputNode::new("Audio Output"));
|
||||
let output_id = graph.add_node(output_node);
|
||||
graph.set_node_position(output_id, 600.0, 150.0);
|
||||
graph.set_node_position(output_id, 720.0, 150.0);
|
||||
|
||||
// Connect SubtrackInputs[i] → Mixer[i] for each subtrack
|
||||
for i in 0..n {
|
||||
let _ = graph.connect(inputs_id, i, mixer_id, i);
|
||||
}
|
||||
let _ = graph.connect(mixer_id, 0, output_id, 0);
|
||||
let _ = graph.connect(mixer_id, 0, gain_id, 0); // Mixer → Gain audio
|
||||
let _ = graph.connect(auto_id, 0, gain_id, 1); // AutomationInput → Gain CV
|
||||
let _ = graph.connect(gain_id, 0, output_id, 0); // Gain → Audio Out
|
||||
graph.set_output_node(Some(output_id));
|
||||
|
||||
self.audio_graph = graph;
|
||||
@@ -309,6 +338,22 @@ impl Metatrack {
|
||||
self.graph_is_default = true;
|
||||
}
|
||||
|
||||
/// Extract Volume AutomationInput keyframes from the current graph (if any),
|
||||
/// so they can be preserved across `set_subtrack_graph` rebuilds.
|
||||
fn get_volume_automation_keyframes(&self) -> Vec<super::node_graph::nodes::AutomationKeyframe> {
|
||||
use super::node_graph::nodes::AutomationInputNode;
|
||||
for idx in self.audio_graph.node_indices() {
|
||||
if let Some(node) = self.audio_graph.get_graph_node(idx) {
|
||||
if node.node.node_type() == "AutomationInput" {
|
||||
if let Some(auto_node) = node.node.as_any().downcast_ref::<AutomationInputNode>() {
|
||||
return auto_node.keyframes().to_vec();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Add a new subtrack port to the existing graph.
|
||||
///
|
||||
/// If `graph_is_default`: also connects the new port to a new Mixer input.
|
||||
|
||||
Reference in New Issue
Block a user