Work on sampler nodes, fix slew limiter
This commit is contained in:
@@ -466,6 +466,66 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
/// Read audio from pool as mono f32 samples.
|
||||
/// Handles all storage types: InMemory/Mapped use read_samples(),
|
||||
/// Compressed falls back to decoding from the file path.
|
||||
fn read_mono_from_pool(pool: &crate::audio::pool::AudioClipPool, pool_index: usize) -> Option<(Vec<f32>, f32)> {
|
||||
let audio_file = pool.get_file(pool_index)?;
|
||||
let channels = audio_file.channels as usize;
|
||||
let frames = audio_file.frames as usize;
|
||||
let sample_rate = audio_file.sample_rate as f32;
|
||||
|
||||
// Try read_samples first (works for InMemory and Mapped)
|
||||
let mut mono_samples = vec![0.0f32; frames];
|
||||
let read_count = if channels == 1 {
|
||||
audio_file.read_samples(0, frames, 0, &mut mono_samples)
|
||||
} else {
|
||||
let mut channel_buf = vec![0.0f32; frames];
|
||||
let mut count = 0;
|
||||
for ch in 0..channels {
|
||||
count = audio_file.read_samples(0, frames, ch, &mut channel_buf);
|
||||
for (i, &s) in channel_buf.iter().enumerate() {
|
||||
mono_samples[i] += s;
|
||||
}
|
||||
}
|
||||
let scale = 1.0 / channels as f32;
|
||||
for s in &mut mono_samples {
|
||||
*s *= scale;
|
||||
}
|
||||
count
|
||||
};
|
||||
|
||||
if read_count > 0 {
|
||||
return Some((mono_samples, sample_rate));
|
||||
}
|
||||
|
||||
// Compressed storage: decode from file path using sample_loader
|
||||
let path = audio_file.path.to_string_lossy();
|
||||
if !path.starts_with("<embedded") {
|
||||
if let Ok(sample_data) = crate::audio::sample_loader::load_audio_file(&*path) {
|
||||
return Some((sample_data.samples, sample_data.sample_rate as f32));
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: try interleaved data() and mix down
|
||||
let data = audio_file.data();
|
||||
if !data.is_empty() && channels > 0 {
|
||||
let actual_frames = data.len() / channels;
|
||||
let mut mono = vec![0.0f32; actual_frames];
|
||||
for frame in 0..actual_frames {
|
||||
let mut sum = 0.0f32;
|
||||
for ch in 0..channels {
|
||||
sum += data[frame * channels + ch];
|
||||
}
|
||||
mono[frame] = sum / channels as f32;
|
||||
}
|
||||
return Some((mono, sample_rate));
|
||||
}
|
||||
|
||||
eprintln!("[read_mono_from_pool] Failed to read audio from pool_index={}", pool_index);
|
||||
None
|
||||
}
|
||||
|
||||
/// Handle a command from the UI thread
|
||||
fn handle_command(&mut self, cmd: Command) {
|
||||
match cmd {
|
||||
@@ -1586,6 +1646,38 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
Command::SamplerLoadFromPool(track_id, node_id, pool_index) => {
|
||||
use crate::audio::node_graph::nodes::SimpleSamplerNode;
|
||||
|
||||
let sample_result = Self::read_mono_from_pool(&self.audio_pool, pool_index);
|
||||
|
||||
if let Some((mono_samples, sample_rate)) = sample_result {
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
|
||||
let graph = &mut track.instrument_graph;
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
if let Some(graph_node) = graph.get_graph_node_mut(node_idx) {
|
||||
if let Some(sampler_node) = graph_node.node.as_any_mut().downcast_mut::<SimpleSamplerNode>() {
|
||||
sampler_node.set_sample(mono_samples, sample_rate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Command::SamplerSetRootNote(track_id, node_id, root_note) => {
|
||||
use crate::audio::node_graph::nodes::SimpleSamplerNode;
|
||||
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
|
||||
let graph = &mut track.instrument_graph;
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
if let Some(graph_node) = graph.get_graph_node_mut(node_idx) {
|
||||
if let Some(sampler_node) = graph_node.node.as_any_mut().downcast_mut::<SimpleSamplerNode>() {
|
||||
sampler_node.set_root_note(root_note);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Command::MultiSamplerAddLayer(track_id, node_id, file_path, key_min, key_max, root_key, velocity_min, velocity_max, loop_start, loop_end, loop_mode) => {
|
||||
use crate::audio::node_graph::nodes::MultiSamplerNode;
|
||||
|
||||
@@ -1604,6 +1696,29 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
Command::MultiSamplerAddLayerFromPool(track_id, node_id, pool_index, key_min, key_max, root_key) => {
|
||||
use crate::audio::node_graph::nodes::MultiSamplerNode;
|
||||
use crate::audio::node_graph::nodes::LoopMode;
|
||||
|
||||
let sample_result = Self::read_mono_from_pool(&self.audio_pool, pool_index);
|
||||
|
||||
if let Some((mono_samples, sample_rate)) = sample_result {
|
||||
if let Some(TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
|
||||
let graph = &mut track.instrument_graph;
|
||||
let node_idx = NodeIndex::new(node_id as usize);
|
||||
if let Some(graph_node) = graph.get_graph_node_mut(node_idx) {
|
||||
if let Some(multi_node) = graph_node.node.as_any_mut().downcast_mut::<MultiSamplerNode>() {
|
||||
multi_node.add_layer(
|
||||
mono_samples, sample_rate,
|
||||
key_min, key_max, root_key,
|
||||
0, 127, None, None, LoopMode::OneShot,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Command::MultiSamplerUpdateLayer(track_id, node_id, layer_index, key_min, key_max, root_key, velocity_min, velocity_max, loop_start, loop_end, loop_mode) => {
|
||||
use crate::audio::node_graph::nodes::MultiSamplerNode;
|
||||
|
||||
@@ -3099,11 +3214,26 @@ impl EngineController {
|
||||
let _ = self.command_tx.push(Command::SamplerLoadSample(track_id, node_id, file_path));
|
||||
}
|
||||
|
||||
/// Load a sample from the audio pool into a SimpleSampler node
|
||||
pub fn sampler_load_from_pool(&mut self, track_id: TrackId, node_id: u32, pool_index: usize) {
|
||||
let _ = self.command_tx.push(Command::SamplerLoadFromPool(track_id, node_id, pool_index));
|
||||
}
|
||||
|
||||
/// Set the root note for a SimpleSampler node
|
||||
pub fn sampler_set_root_note(&mut self, track_id: TrackId, node_id: u32, root_note: u8) {
|
||||
let _ = self.command_tx.push(Command::SamplerSetRootNote(track_id, node_id, root_note));
|
||||
}
|
||||
|
||||
/// Add a sample layer to a MultiSampler node
|
||||
pub fn multi_sampler_add_layer(&mut self, track_id: TrackId, node_id: u32, file_path: String, key_min: u8, key_max: u8, root_key: u8, velocity_min: u8, velocity_max: u8, loop_start: Option<usize>, loop_end: Option<usize>, loop_mode: crate::audio::node_graph::nodes::LoopMode) {
|
||||
let _ = self.command_tx.push(Command::MultiSamplerAddLayer(track_id, node_id, file_path, key_min, key_max, root_key, velocity_min, velocity_max, loop_start, loop_end, loop_mode));
|
||||
}
|
||||
|
||||
/// Add a sample layer from the audio pool to a MultiSampler node
|
||||
pub fn multi_sampler_add_layer_from_pool(&mut self, track_id: TrackId, node_id: u32, pool_index: usize, key_min: u8, key_max: u8, root_key: u8) {
|
||||
let _ = self.command_tx.push(Command::MultiSamplerAddLayerFromPool(track_id, node_id, pool_index, key_min, key_max, root_key));
|
||||
}
|
||||
|
||||
/// Update a MultiSampler layer's configuration
|
||||
pub fn multi_sampler_update_layer(&mut self, track_id: TrackId, node_id: u32, layer_index: usize, key_min: u8, key_max: u8, root_key: u8, velocity_min: u8, velocity_max: u8, loop_start: Option<usize>, loop_end: Option<usize>, loop_mode: crate::audio::node_graph::nodes::LoopMode) {
|
||||
let _ = self.command_tx.push(Command::MultiSamplerUpdateLayer(track_id, node_id, layer_index, key_min, key_max, root_key, velocity_min, velocity_max, loop_start, loop_end, loop_mode));
|
||||
|
||||
@@ -101,8 +101,7 @@ impl OscilloscopeNode {
|
||||
|
||||
let inputs = vec![
|
||||
NodePort::new("Audio In", SignalType::Audio, 0),
|
||||
NodePort::new("V/oct", SignalType::CV, 1),
|
||||
NodePort::new("CV In", SignalType::CV, 2),
|
||||
NodePort::new("CV In", SignalType::CV, 1),
|
||||
];
|
||||
|
||||
let outputs = vec![
|
||||
@@ -223,13 +222,24 @@ 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
|
||||
// Read CV input if available (port 1) — used for both display and V/Oct triggering
|
||||
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;
|
||||
let cv_input = inputs[1];
|
||||
let cv_len = len.min(cv_input.len());
|
||||
|
||||
// Check if connected (not NaN sentinel)
|
||||
if cv_len > 0 && !cv_input[0].is_nan() {
|
||||
// Update V/Oct trigger period from CV value
|
||||
self.voct_value = cv_input[0];
|
||||
let frequency = Self::voct_to_frequency(self.voct_value);
|
||||
let period_samples = (sample_rate as f32 / frequency).max(1.0);
|
||||
self.trigger_period = period_samples as usize;
|
||||
|
||||
// Capture CV samples to buffer
|
||||
if let Ok(mut cv_buffer) = self.cv_buffer.lock() {
|
||||
cv_buffer.write(&cv_input[..cv_len]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update sample counter for V/oct triggering
|
||||
@@ -245,14 +255,6 @@ impl AudioNode for OscilloscopeNode {
|
||||
buffer.write(&input[..len]);
|
||||
}
|
||||
|
||||
// Capture CV samples if CV input is connected (input 2)
|
||||
if inputs.len() > 2 && !inputs[2].is_empty() {
|
||||
let cv_input = inputs[2];
|
||||
if let Ok(mut cv_buffer) = self.cv_buffer.lock() {
|
||||
cv_buffer.write(&cv_input[..len.min(cv_input.len())]);
|
||||
}
|
||||
}
|
||||
|
||||
// Update last sample for trigger detection (use left channel, frame 0)
|
||||
if !input.is_empty() {
|
||||
self.last_sample = input[0];
|
||||
|
||||
@@ -25,6 +25,7 @@ pub struct SimpleSamplerNode {
|
||||
gain: f32,
|
||||
loop_enabled: bool,
|
||||
pitch_shift: f32, // Additional pitch shift in semitones
|
||||
root_note: u8, // MIDI note for original pitch playback (default 69 = A4)
|
||||
|
||||
inputs: Vec<NodePort>,
|
||||
outputs: Vec<NodePort>,
|
||||
@@ -61,6 +62,7 @@ impl SimpleSamplerNode {
|
||||
gain: 1.0,
|
||||
loop_enabled: false,
|
||||
pitch_shift: 0.0,
|
||||
root_note: 69, // A4 — V/Oct 0.0 from MIDI-to-CV
|
||||
inputs,
|
||||
outputs,
|
||||
parameters,
|
||||
@@ -101,13 +103,25 @@ impl SimpleSamplerNode {
|
||||
}
|
||||
|
||||
/// Convert V/oct CV to playback speed multiplier
|
||||
/// 0V = 1.0 (original speed), +1V = 2.0 (one octave up), -1V = 0.5 (one octave down)
|
||||
/// Accounts for root_note: when the incoming MIDI note matches root_note,
|
||||
/// the sample plays at original speed. V/Oct 0.0 = A4 (MIDI 69) by convention.
|
||||
fn voct_to_speed(&self, voct: f32) -> f32 {
|
||||
// Add pitch shift parameter
|
||||
let total_semitones = voct * 12.0 + self.pitch_shift;
|
||||
// Offset so root_note plays at original speed
|
||||
let root_offset = (self.root_note as f32 - 69.0) / 12.0;
|
||||
let total_semitones = (voct - root_offset) * 12.0 + self.pitch_shift;
|
||||
2.0_f32.powf(total_semitones / 12.0)
|
||||
}
|
||||
|
||||
/// Set the root note (MIDI note number for original-pitch playback)
|
||||
pub fn set_root_note(&mut self, note: u8) {
|
||||
self.root_note = note.min(127);
|
||||
}
|
||||
|
||||
/// Get the current root note
|
||||
pub fn root_note(&self) -> u8 {
|
||||
self.root_note
|
||||
}
|
||||
|
||||
/// Read sample at playhead with linear interpolation
|
||||
fn read_sample(&self, playhead: f32, sample: &[f32]) -> f32 {
|
||||
if sample.is_empty() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType};
|
||||
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType, cv_input_or_default};
|
||||
use crate::audio::midi::MidiEvent;
|
||||
|
||||
const PARAM_RISE_TIME: u32 = 0;
|
||||
@@ -90,9 +90,8 @@ impl AudioNode for SlewLimiterNode {
|
||||
return;
|
||||
}
|
||||
|
||||
let input = inputs[0];
|
||||
let output = &mut outputs[0];
|
||||
let length = input.len().min(output.len());
|
||||
let length = output.len();
|
||||
|
||||
// Calculate maximum change per sample
|
||||
let sample_duration = 1.0 / sample_rate as f32;
|
||||
@@ -111,7 +110,9 @@ impl AudioNode for SlewLimiterNode {
|
||||
};
|
||||
|
||||
for i in 0..length {
|
||||
let target = input[i];
|
||||
// Use cv_input_or_default to handle unconnected inputs (NaN sentinel)
|
||||
// Default to last_value so output holds steady when unconnected
|
||||
let target = cv_input_or_default(inputs, 0, i, self.last_value);
|
||||
let difference = target - self.last_value;
|
||||
|
||||
let max_change = if difference > 0.0 {
|
||||
|
||||
Reference in New Issue
Block a user