Massive tempo refactor - make beats canonical time rep and allow them to be non constant
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use super::node_trait::AudioNode;
|
||||
use super::types::{ConnectionError, SignalType};
|
||||
use crate::audio::midi::MidiEvent;
|
||||
use crate::time::Beats;
|
||||
use petgraph::algo::has_path_connecting;
|
||||
use petgraph::stable_graph::{NodeIndex, StableGraph};
|
||||
use petgraph::visit::{EdgeRef, IntoEdgeReferences};
|
||||
@@ -93,8 +94,8 @@ pub struct AudioGraph {
|
||||
/// UI positions for nodes (node_index -> (x, y))
|
||||
node_positions: std::collections::HashMap<u32, (f32, f32)>,
|
||||
|
||||
/// Current playback time (for automation nodes)
|
||||
playback_time: f64,
|
||||
/// Current playback time in beats (for automation nodes)
|
||||
playback_time: Beats,
|
||||
|
||||
/// Project tempo (synced from Engine via SetTempo)
|
||||
bpm: f32,
|
||||
@@ -123,7 +124,7 @@ impl AudioGraph {
|
||||
// Pre-allocate MIDI input buffers (max 128 events per port)
|
||||
midi_input_buffers: (0..16).map(|_| Vec::with_capacity(128)).collect(),
|
||||
node_positions: std::collections::HashMap::new(),
|
||||
playback_time: 0.0,
|
||||
playback_time: Beats::ZERO,
|
||||
bpm: 120.0,
|
||||
beats_per_bar: 4,
|
||||
topo_cache: None,
|
||||
@@ -475,7 +476,7 @@ impl AudioGraph {
|
||||
}
|
||||
|
||||
/// Process the graph and produce audio output
|
||||
pub fn process(&mut self, output_buffer: &mut [f32], midi_events: &[MidiEvent], playback_time: f64) {
|
||||
pub fn process(&mut self, output_buffer: &mut [f32], midi_events: &[MidiEvent], playback_time: Beats) {
|
||||
// Update playback time
|
||||
self.playback_time = playback_time;
|
||||
|
||||
@@ -484,6 +485,7 @@ impl AudioGraph {
|
||||
for node in self.graph.node_weights_mut() {
|
||||
if let Some(auto_node) = node.node.as_any_mut().downcast_mut::<AutomationInputNode>() {
|
||||
auto_node.set_playback_time(playback_time);
|
||||
auto_node.set_bpm(self.bpm as f64);
|
||||
} else if let Some(beat_node) = node.node.as_any_mut().downcast_mut::<BeatNode>() {
|
||||
beat_node.set_playback_time(playback_time);
|
||||
beat_node.set_tempo(self.bpm, self.beats_per_bar);
|
||||
@@ -737,18 +739,6 @@ impl AudioGraph {
|
||||
self.graph.node_indices()
|
||||
}
|
||||
|
||||
/// BPM changed: rescale all AutomationInput keyframe times to preserve beat positions.
|
||||
/// `from_bpm` is the BPM before the change (used to bootstrap beats if not yet populated).
|
||||
/// `to_bpm` is the new BPM (used to re-derive seconds from beats).
|
||||
pub fn apply_beats_to_automation_keyframes(&mut self, from_bpm: f64, to_bpm: f64, fps: f64) {
|
||||
use super::nodes::AutomationInputNode;
|
||||
for node in self.graph.node_weights_mut() {
|
||||
if let Some(auto_node) = node.node.as_any_mut().downcast_mut::<AutomationInputNode>() {
|
||||
auto_node.apply_beats_to_keyframes(from_bpm, to_bpm, fps);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reallocate a node's output buffers to match its current port list.
|
||||
///
|
||||
/// Must be called after `SubtrackInputsNode::update_subtracks` changes the port count,
|
||||
@@ -1029,7 +1019,7 @@ impl AudioGraph {
|
||||
serialized.automation_display_name = Some(auto_node.display_name().to_string());
|
||||
serialized.automation_keyframes = auto_node.keyframes().iter().map(|kf| {
|
||||
SerializedKeyframe {
|
||||
time: kf.time,
|
||||
time: kf.time.0,
|
||||
value: kf.value,
|
||||
interpolation: match kf.interpolation {
|
||||
InterpolationType::Linear => "linear",
|
||||
@@ -1369,9 +1359,7 @@ impl AudioGraph {
|
||||
auto_node.clear_keyframes();
|
||||
for kf in &serialized_node.automation_keyframes {
|
||||
auto_node.add_keyframe(AutomationKeyframe {
|
||||
time: kf.time,
|
||||
time_beats: 0.0,
|
||||
time_frames: 0.0,
|
||||
time: crate::time::Beats(kf.time),
|
||||
value: kf.value,
|
||||
interpolation: match kf.interpolation.as_str() {
|
||||
"bezier" => InterpolationType::Bezier,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType};
|
||||
use crate::audio::midi::MidiEvent;
|
||||
use crate::time::Beats;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
@@ -16,14 +17,7 @@ pub enum InterpolationType {
|
||||
/// A single keyframe in an automation curve
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AutomationKeyframe {
|
||||
/// Time in seconds (absolute project time)
|
||||
pub time: f64,
|
||||
/// Time in beats (derived; canonical in Measures mode)
|
||||
#[serde(default)]
|
||||
pub time_beats: f64,
|
||||
/// Time in frames (derived; canonical in Frames mode)
|
||||
#[serde(default)]
|
||||
pub time_frames: f64,
|
||||
pub time: Beats,
|
||||
/// CV output value
|
||||
pub value: f32,
|
||||
/// Interpolation type to next keyframe
|
||||
@@ -35,35 +29,15 @@ pub struct AutomationKeyframe {
|
||||
}
|
||||
|
||||
impl AutomationKeyframe {
|
||||
pub fn new(time: f64, value: f32) -> Self {
|
||||
pub fn new(time: Beats, value: f32) -> Self {
|
||||
Self {
|
||||
time,
|
||||
time_beats: 0.0,
|
||||
time_frames: 0.0,
|
||||
value,
|
||||
interpolation: InterpolationType::Linear,
|
||||
ease_out: (0.58, 1.0),
|
||||
ease_in: (0.42, 0.0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Populate beats/frames from the current seconds value.
|
||||
pub fn sync_from_seconds(&mut self, bpm: f64, fps: f64) {
|
||||
self.time_beats = self.time * bpm / 60.0;
|
||||
self.time_frames = self.time * fps;
|
||||
}
|
||||
|
||||
/// BPM changed; beats are canonical → recompute seconds and frames.
|
||||
pub fn apply_beats(&mut self, bpm: f64, fps: f64) {
|
||||
self.time = self.time_beats * 60.0 / bpm;
|
||||
self.time_frames = self.time * fps;
|
||||
}
|
||||
|
||||
/// FPS changed; frames are canonical → recompute seconds and beats.
|
||||
pub fn apply_frames(&mut self, fps: f64, bpm: f64) {
|
||||
self.time = self.time_frames / fps;
|
||||
self.time_beats = self.time * bpm / 60.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Automation Input Node - outputs CV signal controlled by timeline curves
|
||||
@@ -73,8 +47,10 @@ pub struct AutomationInputNode {
|
||||
keyframes: Vec<AutomationKeyframe>,
|
||||
outputs: Vec<NodePort>,
|
||||
parameters: Vec<Parameter>,
|
||||
/// Shared playback time (set by the graph before processing)
|
||||
playback_time: Arc<RwLock<f64>>,
|
||||
/// Shared playback time in beats (set by the graph before processing)
|
||||
playback_time: Arc<RwLock<Beats>>,
|
||||
/// Current BPM (set by the graph before processing, used for per-sample beat advancement)
|
||||
bpm: f64,
|
||||
/// Minimum output value (for UI display range)
|
||||
pub value_min: f32,
|
||||
/// Maximum output value (for UI display range)
|
||||
@@ -97,22 +73,26 @@ impl AutomationInputNode {
|
||||
Self {
|
||||
name: name.clone(),
|
||||
display_name: "Automation".to_string(),
|
||||
keyframes: vec![AutomationKeyframe::new(0.0, 0.0)],
|
||||
keyframes: vec![AutomationKeyframe::new(Beats::ZERO, 0.0)],
|
||||
outputs,
|
||||
parameters,
|
||||
playback_time: Arc::new(RwLock::new(0.0)),
|
||||
playback_time: Arc::new(RwLock::new(Beats::ZERO)),
|
||||
bpm: 120.0,
|
||||
value_min: -1.0,
|
||||
value_max: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the playback time (called by graph before processing)
|
||||
pub fn set_playback_time(&mut self, time: f64) {
|
||||
pub fn set_playback_time(&mut self, time: Beats) {
|
||||
if let Ok(mut playback) = self.playback_time.write() {
|
||||
*playback = time;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_bpm(&mut self, bpm: f64) {
|
||||
self.bpm = bpm;
|
||||
}
|
||||
|
||||
/// Get the display name (shown in UI)
|
||||
pub fn display_name(&self) -> &str {
|
||||
&self.display_name
|
||||
@@ -142,8 +122,7 @@ impl AutomationInputNode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove keyframe at specific time (with tolerance)
|
||||
pub fn remove_keyframe_at_time(&mut self, time: f64, tolerance: f64) -> bool {
|
||||
pub fn remove_keyframe_at_time(&mut self, time: Beats, tolerance: Beats) -> bool {
|
||||
if let Some(idx) = self.keyframes.iter().position(|kf| (kf.time - time).abs() < tolerance) {
|
||||
self.keyframes.remove(idx);
|
||||
true
|
||||
@@ -152,10 +131,8 @@ impl AutomationInputNode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Update an existing keyframe
|
||||
pub fn update_keyframe(&mut self, keyframe: AutomationKeyframe) {
|
||||
// Remove old keyframe at this time, then add new one
|
||||
self.remove_keyframe_at_time(keyframe.time, 0.001);
|
||||
self.remove_keyframe_at_time(keyframe.time, Beats(0.001));
|
||||
self.add_keyframe(keyframe);
|
||||
}
|
||||
|
||||
@@ -169,42 +146,20 @@ impl AutomationInputNode {
|
||||
self.keyframes.clear();
|
||||
}
|
||||
|
||||
/// Populate beats/frames on all keyframes from their current seconds values.
|
||||
pub fn sync_keyframes_from_seconds(&mut self, bpm: f64, fps: f64) {
|
||||
for kf in &mut self.keyframes {
|
||||
kf.sync_from_seconds(bpm, fps);
|
||||
}
|
||||
}
|
||||
|
||||
/// BPM changed: for each keyframe, bootstrap beats from seconds (using `from_bpm`) if not yet
|
||||
/// set, then re-derive seconds and frames from beats using `to_bpm`.
|
||||
pub fn apply_beats_to_keyframes(&mut self, from_bpm: f64, to_bpm: f64, fps: f64) {
|
||||
for kf in &mut self.keyframes {
|
||||
if kf.time_beats == 0.0 && kf.time.abs() > 1e-9 {
|
||||
kf.sync_from_seconds(from_bpm, fps);
|
||||
}
|
||||
kf.apply_beats(to_bpm, fps);
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate curve at a specific time
|
||||
fn evaluate_at_time(&self, time: f64) -> f32 {
|
||||
fn evaluate_at_time(&self, time: Beats) -> f32 {
|
||||
if self.keyframes.is_empty() {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Before first keyframe
|
||||
if time <= self.keyframes[0].time {
|
||||
return self.keyframes[0].value;
|
||||
}
|
||||
|
||||
// After last keyframe
|
||||
let last_idx = self.keyframes.len() - 1;
|
||||
if time >= self.keyframes[last_idx].time {
|
||||
return self.keyframes[last_idx].value;
|
||||
}
|
||||
|
||||
// Find bracketing keyframes
|
||||
for i in 0..self.keyframes.len() - 1 {
|
||||
let kf1 = &self.keyframes[i];
|
||||
let kf2 = &self.keyframes[i + 1];
|
||||
@@ -217,14 +172,12 @@ impl AutomationInputNode {
|
||||
0.0
|
||||
}
|
||||
|
||||
/// Interpolate between two keyframes
|
||||
fn interpolate(&self, kf1: &AutomationKeyframe, kf2: &AutomationKeyframe, time: f64) -> f32 {
|
||||
// Calculate normalized position between keyframes (0.0 to 1.0)
|
||||
fn interpolate(&self, kf1: &AutomationKeyframe, kf2: &AutomationKeyframe, time: Beats) -> f32 {
|
||||
let t = if kf2.time == kf1.time {
|
||||
0.0
|
||||
0.0f64
|
||||
} else {
|
||||
((time - kf1.time) / (kf2.time - kf1.time)) as f32
|
||||
};
|
||||
(time - kf1.time) / (kf2.time - kf1.time)
|
||||
} as f32;
|
||||
|
||||
match kf1.interpolation {
|
||||
InterpolationType::Linear => {
|
||||
@@ -301,19 +254,17 @@ impl AudioNode for AutomationInputNode {
|
||||
let output = &mut outputs[0];
|
||||
let length = output.len();
|
||||
|
||||
// Get the starting playback time
|
||||
let playhead = if let Ok(playback) = self.playback_time.read() {
|
||||
*playback
|
||||
} else {
|
||||
0.0
|
||||
Beats::ZERO
|
||||
};
|
||||
|
||||
// Calculate time per sample
|
||||
let sample_duration = 1.0 / sample_rate as f64;
|
||||
// Advance per sample in beats: beats_per_sample = bpm / 60 / sample_rate
|
||||
let beats_per_sample = self.bpm / 60.0 / sample_rate as f64;
|
||||
|
||||
// Evaluate curve for each sample
|
||||
for i in 0..length {
|
||||
let time = playhead + (i as f64 * sample_duration);
|
||||
let time = playhead + Beats(i as f64 * beats_per_sample);
|
||||
output[i] = self.evaluate_at_time(time);
|
||||
}
|
||||
}
|
||||
@@ -337,7 +288,8 @@ impl AudioNode for AutomationInputNode {
|
||||
keyframes: self.keyframes.clone(),
|
||||
outputs: self.outputs.clone(),
|
||||
parameters: self.parameters.clone(),
|
||||
playback_time: Arc::new(RwLock::new(0.0)),
|
||||
playback_time: Arc::new(RwLock::new(Beats::ZERO)),
|
||||
bpm: self.bpm,
|
||||
value_min: self.value_min,
|
||||
value_max: self.value_max,
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType};
|
||||
use crate::audio::midi::MidiEvent;
|
||||
use crate::time::Beats;
|
||||
|
||||
const PARAM_RESOLUTION: u32 = 0;
|
||||
|
||||
@@ -61,12 +62,12 @@ pub struct BeatNode {
|
||||
bpm: f32,
|
||||
beats_per_bar: u32,
|
||||
resolution: BeatResolution,
|
||||
/// Playback time in seconds, set by the graph before process()
|
||||
playback_time: f64,
|
||||
/// Playback time in beats, set by the graph before process()
|
||||
playback_time: Beats,
|
||||
/// Previous playback_time to detect paused state
|
||||
prev_playback_time: f64,
|
||||
/// Free-running time accumulator for when playback is stopped
|
||||
free_run_time: f64,
|
||||
prev_playback_time: Beats,
|
||||
/// Free-running beat accumulator for when playback is stopped
|
||||
free_run_time: Beats,
|
||||
inputs: Vec<NodePort>,
|
||||
outputs: Vec<NodePort>,
|
||||
parameters: Vec<Parameter>,
|
||||
@@ -92,16 +93,16 @@ impl BeatNode {
|
||||
bpm: DEFAULT_BPM,
|
||||
beats_per_bar: DEFAULT_BEATS_PER_BAR,
|
||||
resolution: BeatResolution::Quarter,
|
||||
playback_time: 0.0,
|
||||
prev_playback_time: -1.0,
|
||||
free_run_time: 0.0,
|
||||
playback_time: Beats::ZERO,
|
||||
prev_playback_time: Beats(-1.0),
|
||||
free_run_time: Beats::ZERO,
|
||||
inputs,
|
||||
outputs,
|
||||
parameters,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_playback_time(&mut self, time: f64) {
|
||||
pub fn set_playback_time(&mut self, time: Beats) {
|
||||
self.playback_time = time;
|
||||
}
|
||||
|
||||
@@ -169,8 +170,8 @@ impl AudioNode for BeatNode {
|
||||
let base_time = if paused { self.free_run_time } else { self.playback_time };
|
||||
|
||||
for i in 0..len {
|
||||
let time = base_time + i as f64 * sample_period;
|
||||
let beat_pos = time * beats_per_second;
|
||||
// base_time is already in beats; advance by beats_per_second per second
|
||||
let beat_pos = base_time.0 + i as f64 * beats_per_second * sample_period;
|
||||
|
||||
// Beat subdivision phase: 0→1 sawtooth
|
||||
let sub_phase = ((beat_pos * subs_per_beat) % 1.0) as f32;
|
||||
@@ -188,13 +189,13 @@ impl AudioNode for BeatNode {
|
||||
}
|
||||
|
||||
// Advance free-run time (always ticks, so it's ready when playback stops)
|
||||
self.free_run_time += len as f64 * sample_period;
|
||||
self.free_run_time += Beats(len as f64 * beats_per_second * sample_period);
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.playback_time = 0.0;
|
||||
self.prev_playback_time = -1.0;
|
||||
self.free_run_time = 0.0;
|
||||
self.playback_time = Beats::ZERO;
|
||||
self.prev_playback_time = Beats(-1.0);
|
||||
self.free_run_time = Beats::ZERO;
|
||||
}
|
||||
|
||||
fn node_type(&self) -> &str {
|
||||
@@ -211,9 +212,9 @@ impl AudioNode for BeatNode {
|
||||
bpm: self.bpm,
|
||||
beats_per_bar: self.beats_per_bar,
|
||||
resolution: self.resolution,
|
||||
playback_time: 0.0,
|
||||
prev_playback_time: -1.0,
|
||||
free_run_time: 0.0,
|
||||
playback_time: Beats::ZERO,
|
||||
prev_playback_time: Beats(-1.0),
|
||||
free_run_time: Beats::ZERO,
|
||||
inputs: self.inputs.clone(),
|
||||
outputs: self.outputs.clone(),
|
||||
parameters: self.parameters.clone(),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType, cv_input_or_default};
|
||||
use crate::audio::midi::MidiEvent;
|
||||
use crate::time::Beats;
|
||||
|
||||
const PARAM_MODE: u32 = 0;
|
||||
const PARAM_STEPS: u32 = 1;
|
||||
@@ -244,14 +245,14 @@ impl AudioNode for SequencerNode {
|
||||
// Note-off for notes no longer active
|
||||
for ¬e in &self.prev_active_notes {
|
||||
if !new_notes.contains(¬e) {
|
||||
midi_outputs[0].push(MidiEvent::note_off(0.0, 0, note, 0));
|
||||
midi_outputs[0].push(MidiEvent::note_off(Beats::ZERO, 0, note, 0));
|
||||
}
|
||||
}
|
||||
|
||||
// Note-on for newly active notes
|
||||
for ¬e in &new_notes {
|
||||
if !self.prev_active_notes.contains(¬e) {
|
||||
midi_outputs[0].push(MidiEvent::note_on(0.0, 0, note, self.velocity));
|
||||
midi_outputs[0].push(MidiEvent::note_on(Beats::ZERO, 0, note, self.velocity));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -348,7 +348,7 @@ impl AudioNode for VoiceAllocatorNode {
|
||||
|
||||
// Process this voice's graph with its MIDI events
|
||||
// Note: playback_time is 0.0 since voice allocator doesn't track time
|
||||
self.voice_instances[voice_idx].process(mix_slice, &midi_events, 0.0);
|
||||
self.voice_instances[voice_idx].process(mix_slice, &midi_events, crate::time::Beats::ZERO);
|
||||
|
||||
// Auto-deactivate releasing voices that have gone silent
|
||||
if voice_state.releasing {
|
||||
|
||||
Reference in New Issue
Block a user