Improve idle performance

This commit is contained in:
Skyler Lehmkuhl
2026-03-10 02:41:44 -04:00
parent 26f06da5bf
commit ac2b4ff8ab
4 changed files with 211 additions and 133 deletions
@@ -796,6 +796,13 @@ struct EditorApp {
#[allow(dead_code)] // Must be kept alive to maintain audio output
audio_stream: Option<cpal::Stream>,
audio_controller: Option<std::sync::Arc<std::sync::Mutex<daw_backend::EngineController>>>,
/// Holds `input_tx` and device info needed to open the microphone stream on
/// demand (when the user selects an audio input track).
audio_input: Option<daw_backend::InputStreamOpener>,
/// Active microphone/line-in stream; kept alive while an audio input track is selected.
#[allow(dead_code)]
audio_input_stream: Option<cpal::Stream>,
audio_buffer_size: u32,
audio_event_rx: Option<rtrb::Consumer<daw_backend::AudioEvent>>,
audio_events_pending: std::sync::Arc<std::sync::atomic::AtomicBool>,
/// Count of in-flight graph preset loads — keeps the repaint loop alive
@@ -1004,13 +1011,16 @@ impl EditorApp {
let action_executor = lightningbeam_core::action::ActionExecutor::new(document);
// Initialize audio system and destructure it for sharing
let (audio_stream, audio_controller, audio_event_rx, audio_sample_rate, audio_channels, file_command_tx, recording_mirror_rx) =
let (audio_stream, audio_controller, audio_event_rx, audio_sample_rate, audio_channels, file_command_tx, recording_mirror_rx, audio_input) =
match daw_backend::AudioSystem::new(None, config.audio_buffer_size) {
Ok(mut audio_system) => {
println!("✅ Audio engine initialized successfully");
// Extract components
let mirror_rx = audio_system.take_recording_mirror_rx();
// take_input_opener pulls out input_tx + sample_rate/channels into
// a self-contained struct that can open the stream on demand.
let input_opener = audio_system.take_input_opener();
let stream = audio_system.stream;
let sample_rate = audio_system.sample_rate;
let channels = audio_system.channels;
@@ -1022,7 +1032,7 @@ impl EditorApp {
// Spawn file operations worker
let file_command_tx = FileOperationsWorker::spawn(controller.clone());
(Some(stream), Some(controller), event_rx, sample_rate, channels, file_command_tx, mirror_rx)
(Some(stream), Some(controller), event_rx, sample_rate, channels, file_command_tx, mirror_rx, input_opener)
}
Err(e) => {
eprintln!("❌ Failed to initialize audio engine: {}", e);
@@ -1030,7 +1040,7 @@ impl EditorApp {
// Create a dummy channel for file operations (won't be used)
let (tx, _rx) = std::sync::mpsc::channel();
(None, None, None, 48000, 2, tx, None)
(None, None, None, 48000, 2, tx, None, None)
}
};
@@ -1078,6 +1088,9 @@ impl EditorApp {
audio_stream,
audio_controller,
audio_event_rx,
audio_input,
audio_input_stream: None,
audio_buffer_size: config.audio_buffer_size,
audio_events_pending: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
pending_graph_loads: std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)),
commit_raster_floating_if_any: false,
@@ -5679,6 +5692,9 @@ impl eframe::App for EditorApp {
schneider_max_error: &mut self.schneider_max_error,
raster_settings: &mut self.raster_settings,
audio_controller: self.audio_controller.as_ref(),
audio_input_opener: &mut self.audio_input,
audio_input_stream: &mut self.audio_input_stream,
audio_buffer_size: self.audio_buffer_size,
video_manager: &self.video_manager,
playback_time: &mut self.playback_time,
is_playing: &mut self.is_playing,
@@ -192,6 +192,12 @@ pub struct SharedPaneState<'a> {
pub raster_settings: &'a mut crate::tools::RasterToolSettings,
/// Audio engine controller for playback control (wrapped in Arc<Mutex<>> for thread safety)
pub audio_controller: Option<&'a std::sync::Arc<std::sync::Mutex<daw_backend::EngineController>>>,
/// Opener for the microphone/line-in stream — consumed on first use.
pub audio_input_opener: &'a mut Option<daw_backend::InputStreamOpener>,
/// Live input stream handle; kept alive while recording is active.
pub audio_input_stream: &'a mut Option<cpal::Stream>,
/// Buffer size (frames) used for the output stream, passed to the input stream opener.
pub audio_buffer_size: u32,
/// Video manager for video decoding and frame caching
pub video_manager: &'a std::sync::Arc<std::sync::Mutex<lightningbeam_core::video::VideoManager>>,
/// Mapping from Document layer UUIDs to daw-backend TrackIds
@@ -657,6 +657,15 @@ impl TimelinePane {
}
RecordCandidate::AudioSampled => {
if let Some(&track_id) = shared.layer_to_track_map.get(&layer_id) {
// Open the input stream now if it hasn't been opened yet.
if shared.audio_input_stream.is_none() {
if let Some(opener) = shared.audio_input_opener.take() {
match opener.open(shared.audio_buffer_size) {
Ok(stream) => *shared.audio_input_stream = Some(stream),
Err(e) => eprintln!("⚠️ Could not open input stream: {e}"),
}
}
}
if let Some(controller_arc) = shared.audio_controller {
let mut controller = controller_arc.lock().unwrap();
controller.start_recording(track_id, start_time);