From 9d5f3d1ec0eaea00c700c224c2e284a4fc491f13 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Mon, 4 Dec 2023 16:08:55 +0000 Subject: [PATCH] Various performance tweaks --- src/main.rs | 20 ++++++++++++++------ src/vis.rs | 7 ++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 92392d8..9139798 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ const SERVICE_TYPE: &str = "_nanoleafapi._tcp.local."; fn update_lights(panels: NanoleafLayoutResponse, nanoleaf: NanoleafClient, buffer_manager: Arc>, color_channel: Receiver>, intensity: f32) { // Needs to be over a sliding window. let mut window = SlidingWindow::new(64); - let mut color_set = Vec::new(); + let mut color_set: Vec = Vec::new(); let mut sorted_panels = panels.position_data.to_vec(); sorted_panels.sort_by(|a,b| { let v = a.x as i32 - b.x as i32; @@ -49,9 +49,11 @@ fn update_lights(panels: NanoleafLayoutResponse, nanoleaf: NanoleafClient, buffe loop { let process_start = Instant::now(); { - color_set = color_channel.recv_timeout(Duration::from_millis(30)).unwrap_or( color_set); + if let Ok(v) = color_channel.try_recv() { + color_set = v; + } - if let Some(data) = buffer_manager.write().unwrap().fft_interval::<10>(LIGHT_INTERVAL) { + if let Some(data) = buffer_manager.write().unwrap().fft_interval(LIGHT_INTERVAL, panels.num_panels) { let mut effect = NanoleafEffectPayload::new(panels.num_panels); for (panel_index, panel) in sorted_panels.iter().enumerate() { if let Some(color) = color_set.get(panel_index) { @@ -74,7 +76,7 @@ fn update_lights(panels: NanoleafLayoutResponse, nanoleaf: NanoleafClient, buffe if LIGHT_INTERVAL.ge(&process_start.elapsed()) { let sleep_duration = LIGHT_INTERVAL.sub(process_start.elapsed()); if sleep_duration.ge(&Duration::ZERO) { - thread::sleep(LIGHT_INTERVAL); + thread::sleep(sleep_duration); } } } @@ -158,6 +160,7 @@ fn configure_display(pause_duration:time::Duration, panel_count: usize, output_n let mut last_value = 0.0f32; let mut heatmap = vec![vec![vec![vec![0u32; 21]; 21]; 37]; panel_count]; loop { + let start = Instant::now(); let frame_copy = backend::capture_output_frame( &globals, &conn, @@ -167,11 +170,16 @@ fn configure_display(pause_duration:time::Duration, panel_count: usize, output_n let hsl = visual::prominent_color::determine_prominent_color(frame_copy, &mut heatmap); let value_hash: f32 = hsl.iter().map(|f| f.get_hue() + f.get_lightness() + f.get_saturation()).sum(); if value_hash != last_value { - log::debug!("Sending new hsl {:?}", hsl); tx.send(hsl).unwrap(); last_value = value_hash; } - thread::sleep(pause_duration); + if pause_duration.ge(&start.elapsed()) { + let sleep_duration = LIGHT_INTERVAL.sub(start.elapsed()); + if sleep_duration.ge(&Duration::ZERO) { + thread::sleep(sleep_duration); + } + } + } }); rx diff --git a/src/vis.rs b/src/vis.rs index 7e58938..018082b 100644 --- a/src/vis.rs +++ b/src/vis.rs @@ -90,10 +90,11 @@ impl BufferManager { // TODO: would be nice to have constant_q and/or variable_q intervals - pub fn fft_interval( + pub fn fft_interval( &mut self, interval: Duration, - ) -> Option> { + out_size: usize, + ) -> Option> { let BufferSlice { values, rate } = self.take_next(interval); if values.len() < 2 { @@ -147,7 +148,7 @@ impl BufferManager { //.normalized() .build() .unwrap() - .take(T) + .take(out_size) .map(|Complex { re, im }| { let power = f32::sqrt(re * re + im * im); let value = power / fft.scaling_factor;