sampler improvements, live waveform preview

This commit is contained in:
Skyler Lehmkuhl
2026-02-17 10:08:49 -05:00
parent c10f42da8f
commit 21a49235fc
11 changed files with 210 additions and 53 deletions
+24 -1
View File
@@ -53,6 +53,7 @@ pub struct Engine {
// Recording state
recording_state: Option<RecordingState>,
input_rx: Option<rtrb::Consumer<f32>>,
recording_mirror_tx: Option<rtrb::Producer<f32>>,
recording_progress_counter: usize,
// MIDI recording state
@@ -130,6 +131,7 @@ impl Engine {
next_clip_id: 0,
recording_state: None,
input_rx: None,
recording_mirror_tx: None,
recording_progress_counter: 0,
midi_recording_state: None,
midi_input_manager: None,
@@ -151,6 +153,11 @@ impl Engine {
self.input_rx = Some(input_rx);
}
/// Set the recording mirror producer for streaming audio to UI during recording
pub fn set_recording_mirror_tx(&mut self, tx: rtrb::Producer<f32>) {
self.recording_mirror_tx = Some(tx);
}
/// Set the MIDI input manager for external MIDI devices
pub fn set_midi_input_manager(&mut self, manager: MidiInputManager) {
self.midi_input_manager = Some(manager);
@@ -393,8 +400,24 @@ impl Engine {
// Add samples to recording
if !self.recording_sample_buffer.is_empty() {
// Calculate how many samples will be skipped (stale buffer data)
let skip = if recording.paused {
self.recording_sample_buffer.len()
} else {
recording.samples_to_skip.min(self.recording_sample_buffer.len())
};
match recording.add_samples(&self.recording_sample_buffer) {
Ok(_flushed) => {
// Mirror non-skipped samples to UI for live waveform display
if skip < self.recording_sample_buffer.len() {
if let Some(ref mut mirror_tx) = self.recording_mirror_tx {
for &sample in &self.recording_sample_buffer[skip..] {
let _ = mirror_tx.push(sample);
}
}
}
// Update clip duration every callback for sample-accurate timing
let duration = recording.duration();
let clip_id = recording.clip_id;
@@ -2540,7 +2563,7 @@ impl Engine {
}
// Notify UI that recording has started
let _ = self.event_tx.push(AudioEvent::RecordingStarted(track_id, clip_id));
let _ = self.event_tx.push(AudioEvent::RecordingStarted(track_id, clip_id, self.sample_rate, self.channels));
}
Err(e) => {
// Send error event to UI
+2 -2
View File
@@ -235,8 +235,8 @@ pub enum AudioEvent {
BufferPoolStats(BufferPoolStats),
/// Automation lane created (track_id, lane_id, parameter_id)
AutomationLaneCreated(TrackId, AutomationLaneId, ParameterId),
/// Recording started (track_id, clip_id)
RecordingStarted(TrackId, ClipId),
/// Recording started (track_id, clip_id, sample_rate, channels)
RecordingStarted(TrackId, ClipId, u32, u32),
/// Recording progress update (clip_id, current_duration)
RecordingProgress(ClipId, f64),
/// Recording stopped (clip_id, pool_index, waveform)
+15
View File
@@ -39,6 +39,8 @@ pub struct AudioSystem {
pub channels: u32,
/// Event receiver for polling audio events (only present when no EventEmitter is provided)
pub event_rx: Option<rtrb::Consumer<AudioEvent>>,
/// Consumer for recording audio mirror (streams recorded samples to UI for live waveform)
recording_mirror_rx: Option<rtrb::Consumer<f32>>,
}
impl AudioSystem {
@@ -85,9 +87,13 @@ impl AudioSystem {
let input_buffer_size = (sample_rate * channels * 10) as usize;
let (mut input_tx, input_rx) = rtrb::RingBuffer::new(input_buffer_size);
// Create mirror ringbuffer for streaming recorded audio to UI (live waveform)
let (mirror_tx, mirror_rx) = rtrb::RingBuffer::new(input_buffer_size);
// Create engine
let mut engine = Engine::new(sample_rate, channels, command_rx, event_tx, query_rx, query_response_tx);
engine.set_input_rx(input_rx);
engine.set_recording_mirror_tx(mirror_tx);
let controller = engine.get_controller(command_tx, query_tx, query_response_rx);
// Initialize MIDI input manager for external MIDI devices
@@ -151,6 +157,7 @@ impl AudioSystem {
sample_rate,
channels,
event_rx: None, // No event receiver when audio device unavailable
recording_mirror_rx: None,
});
}
};
@@ -176,6 +183,7 @@ impl AudioSystem {
sample_rate,
channels,
event_rx: None,
recording_mirror_rx: None,
});
}
};
@@ -207,6 +215,7 @@ impl AudioSystem {
sample_rate,
channels,
event_rx: None,
recording_mirror_rx: None,
});
}
};
@@ -232,9 +241,15 @@ impl AudioSystem {
sample_rate,
channels,
event_rx: event_rx_option,
recording_mirror_rx: Some(mirror_rx),
})
}
/// Take the recording mirror consumer for streaming recorded audio to UI
pub fn take_recording_mirror_rx(&mut self) -> Option<rtrb::Consumer<f32>> {
self.recording_mirror_rx.take()
}
/// Spawn a background thread to emit events from the ringbuffer
fn spawn_emitter_thread(mut event_rx: rtrb::Consumer<AudioEvent>, emitter: std::sync::Arc<dyn EventEmitter>) {
std::thread::spawn(move || {