Node graph improvements and fixes
This commit is contained in:
@@ -1204,6 +1204,15 @@ impl Engine {
|
||||
QueryResponse::GraphState(Err(format!("Track {} not found or is not a MIDI track", track_id)))
|
||||
}
|
||||
}
|
||||
Query::GetOscilloscopeData(track_id, node_id, sample_count) => {
|
||||
match self.project.get_oscilloscope_data(track_id, node_id, sample_count) {
|
||||
Some(data) => QueryResponse::OscilloscopeData(Ok(data)),
|
||||
None => QueryResponse::OscilloscopeData(Err(format!(
|
||||
"Failed to get oscilloscope data from track {} node {}",
|
||||
track_id, node_id
|
||||
))),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Send response back
|
||||
@@ -1759,4 +1768,26 @@ impl EngineController {
|
||||
|
||||
Err("Query timeout".to_string())
|
||||
}
|
||||
|
||||
/// Query oscilloscope data from a node
|
||||
pub fn query_oscilloscope_data(&mut self, track_id: TrackId, node_id: u32, sample_count: usize) -> Result<Vec<f32>, String> {
|
||||
// Send query
|
||||
if let Err(_) = self.query_tx.push(Query::GetOscilloscopeData(track_id, node_id, sample_count)) {
|
||||
return Err("Failed to send query - queue full".to_string());
|
||||
}
|
||||
|
||||
// Wait for response (with timeout)
|
||||
let start = std::time::Instant::now();
|
||||
let timeout = std::time::Duration::from_millis(100);
|
||||
|
||||
while start.elapsed() < timeout {
|
||||
if let Ok(QueryResponse::OscilloscopeData(result)) = self.query_response_rx.pop() {
|
||||
return result;
|
||||
}
|
||||
// Small sleep to avoid busy-waiting
|
||||
std::thread::sleep(std::time::Duration::from_micros(50));
|
||||
}
|
||||
|
||||
Err("Query timeout".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ pub enum TriggerMode {
|
||||
FreeRunning = 0,
|
||||
RisingEdge = 1,
|
||||
FallingEdge = 2,
|
||||
VoltPerOctave = 3,
|
||||
}
|
||||
|
||||
impl TriggerMode {
|
||||
@@ -20,6 +21,7 @@ impl TriggerMode {
|
||||
match value.round() as i32 {
|
||||
1 => TriggerMode::RisingEdge,
|
||||
2 => TriggerMode::FallingEdge,
|
||||
3 => TriggerMode::VoltPerOctave,
|
||||
_ => TriggerMode::FreeRunning,
|
||||
}
|
||||
}
|
||||
@@ -80,6 +82,9 @@ pub struct OscilloscopeNode {
|
||||
trigger_mode: TriggerMode,
|
||||
trigger_level: f32, // -1.0 to 1.0
|
||||
last_sample: f32, // For edge detection
|
||||
voct_value: f32, // Current V/oct input value
|
||||
sample_counter: usize, // Counter for V/oct triggering
|
||||
trigger_period: usize, // Period in samples for V/oct triggering
|
||||
|
||||
// Shared buffer for reading from Tauri commands
|
||||
buffer: Arc<Mutex<CircularBuffer>>,
|
||||
@@ -95,6 +100,7 @@ impl OscilloscopeNode {
|
||||
|
||||
let inputs = vec![
|
||||
NodePort::new("Audio In", SignalType::Audio, 0),
|
||||
NodePort::new("V/oct", SignalType::CV, 1),
|
||||
];
|
||||
|
||||
let outputs = vec![
|
||||
@@ -103,7 +109,7 @@ impl OscilloscopeNode {
|
||||
|
||||
let parameters = vec![
|
||||
Parameter::new(PARAM_TIME_SCALE, "Time Scale", 10.0, 1000.0, 100.0, ParameterUnit::Time),
|
||||
Parameter::new(PARAM_TRIGGER_MODE, "Trigger", 0.0, 2.0, 0.0, ParameterUnit::Generic),
|
||||
Parameter::new(PARAM_TRIGGER_MODE, "Trigger", 0.0, 3.0, 0.0, ParameterUnit::Generic),
|
||||
Parameter::new(PARAM_TRIGGER_LEVEL, "Trigger Level", -1.0, 1.0, 0.0, ParameterUnit::Generic),
|
||||
];
|
||||
|
||||
@@ -113,6 +119,9 @@ impl OscilloscopeNode {
|
||||
trigger_mode: TriggerMode::FreeRunning,
|
||||
trigger_level: 0.0,
|
||||
last_sample: 0.0,
|
||||
voct_value: 0.0,
|
||||
sample_counter: 0,
|
||||
trigger_period: 480, // Default to ~100Hz at 48kHz
|
||||
buffer: Arc::new(Mutex::new(CircularBuffer::new(BUFFER_SIZE))),
|
||||
inputs,
|
||||
outputs,
|
||||
@@ -141,6 +150,12 @@ impl OscilloscopeNode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert V/oct to frequency in Hz (matches oscillator convention)
|
||||
/// 0V = A4 (440 Hz), ±1V per octave
|
||||
fn voct_to_frequency(voct: f32) -> f32 {
|
||||
440.0 * 2.0_f32.powf(voct)
|
||||
}
|
||||
|
||||
/// Check if trigger condition is met
|
||||
fn is_triggered(&self, current_sample: f32) -> bool {
|
||||
match self.trigger_mode {
|
||||
@@ -151,6 +166,10 @@ impl OscilloscopeNode {
|
||||
TriggerMode::FallingEdge => {
|
||||
self.last_sample >= self.trigger_level && current_sample < self.trigger_level
|
||||
}
|
||||
TriggerMode::VoltPerOctave => {
|
||||
// Trigger at the start of each period
|
||||
self.sample_counter == 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -196,7 +215,7 @@ impl AudioNode for OscilloscopeNode {
|
||||
outputs: &mut [&mut [f32]],
|
||||
_midi_inputs: &[&[MidiEvent]],
|
||||
_midi_outputs: &mut [&mut Vec<MidiEvent>],
|
||||
_sample_rate: u32,
|
||||
sample_rate: u32,
|
||||
) {
|
||||
if inputs.is_empty() || outputs.is_empty() {
|
||||
return;
|
||||
@@ -206,6 +225,20 @@ impl AudioNode for OscilloscopeNode {
|
||||
let output = &mut outputs[0];
|
||||
let len = input.len().min(output.len());
|
||||
|
||||
// Read V/oct input if available and update trigger period
|
||||
if inputs.len() > 1 && !inputs[1].is_empty() {
|
||||
self.voct_value = inputs[1][0]; // Use first sample of V/oct input
|
||||
let frequency = Self::voct_to_frequency(self.voct_value);
|
||||
// Calculate period in samples, clamped to reasonable range
|
||||
let period_samples = (sample_rate as f32 / frequency).max(1.0);
|
||||
self.trigger_period = period_samples as usize;
|
||||
}
|
||||
|
||||
// Update sample counter for V/oct triggering
|
||||
if self.trigger_mode == TriggerMode::VoltPerOctave {
|
||||
self.sample_counter = (self.sample_counter + len) % self.trigger_period;
|
||||
}
|
||||
|
||||
// Pass through audio (copy input to output)
|
||||
output[..len].copy_from_slice(&input[..len]);
|
||||
|
||||
@@ -222,6 +255,8 @@ impl AudioNode for OscilloscopeNode {
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.last_sample = 0.0;
|
||||
self.voct_value = 0.0;
|
||||
self.sample_counter = 0;
|
||||
self.clear_buffer();
|
||||
}
|
||||
|
||||
@@ -240,6 +275,9 @@ impl AudioNode for OscilloscopeNode {
|
||||
trigger_mode: self.trigger_mode,
|
||||
trigger_level: self.trigger_level,
|
||||
last_sample: 0.0,
|
||||
voct_value: 0.0,
|
||||
sample_counter: 0,
|
||||
trigger_period: 480,
|
||||
buffer: Arc::new(Mutex::new(CircularBuffer::new(BUFFER_SIZE))),
|
||||
inputs: self.inputs.clone(),
|
||||
outputs: self.outputs.clone(),
|
||||
|
||||
@@ -207,6 +207,8 @@ pub enum Query {
|
||||
GetGraphState(TrackId),
|
||||
/// Get a voice allocator's template graph state as JSON (track_id, voice_allocator_id)
|
||||
GetTemplateState(TrackId, u32),
|
||||
/// Get oscilloscope data from a node (track_id, node_id, sample_count)
|
||||
GetOscilloscopeData(TrackId, u32, usize),
|
||||
}
|
||||
|
||||
/// Responses to synchronous queries
|
||||
@@ -214,4 +216,6 @@ pub enum Query {
|
||||
pub enum QueryResponse {
|
||||
/// Graph state as JSON string
|
||||
GraphState(Result<String, String>),
|
||||
/// Oscilloscope data samples
|
||||
OscilloscopeData(Result<Vec<f32>, String>),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user