Skip to content

Commit f19fdb8

Browse files
perf: quadrature rewrite in spectrogram, other smaller optimizations
Signed-off-by: Maika Namuo <httpworldview@gmail.com>
1 parent 2e699ec commit f19fdb8

10 files changed

Lines changed: 229 additions & 138 deletions

File tree

‎src/util/audio.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ mod frequency;
77
mod level;
88
mod window;
99

10+
#[cfg(test)]
11+
pub(crate) use self::level::LN_TO_DB;
1012
pub use self::{
1113
frequency::{DISPLAY_MIN_HZ, FrequencyScale},
12-
level::{DB_FLOOR, LN_TO_DB, db_to_power, power_to_db},
14+
level::{DB_FLOOR, db_to_power, power_to_db},
1315
window::{DcHistory, HopQueue, WindowKind, WindowedFft, dc_power_scale},
1416
};
1517
pub(crate) use self::{

‎src/util/audio/window.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ pub struct HopQueue {
172172
dc: DcHistory,
173173
debt: usize,
174174
last_nonzero: Option<usize>,
175+
position: u64,
175176
}
176177

177178
impl HopQueue {
@@ -183,6 +184,7 @@ impl HopQueue {
183184
}
184185

185186
pub fn clear(&mut self) {
187+
self.position += self.samples.len() as u64;
186188
self.samples.clear();
187189
self.dc.clear();
188190
self.debt = 0;
@@ -237,6 +239,11 @@ impl HopQueue {
237239
.last_nonzero
238240
.and_then(|index| index.checked_sub(drained));
239241
self.debt = self.debt.saturating_add(count - drained);
242+
self.position += count as u64;
243+
}
244+
245+
pub fn position(&self) -> u64 {
246+
self.position
240247
}
241248

242249
pub fn samples(&mut self) -> &[f32] {

‎src/util/color.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ pub fn lerp_rgba(a: [f32; 4], b: [f32; 4], t: f32) -> [f32; 4] {
100100
pub fn ramp_segment(positions: &[f32], spreads: &[f32], t: f32) -> (usize, usize, f32) {
101101
let count = positions.len();
102102
let t = t.clamp(0.0, 1.0);
103-
let hi = positions
104-
.partition_point(|&pos| pos < t)
105-
.clamp(1, count - 1);
103+
let below = positions.iter().filter(|&&pos| pos < t).count();
104+
let hi = below.clamp(1, count - 1);
106105
let lo = hi - 1;
107106
let linear =
108107
((t - positions[lo]) / (positions[hi] - positions[lo]).max(f32::EPSILON)).clamp(0.0, 1.0);

‎src/visuals/render/common.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ const _: () = assert!(SdfInstance::RADIAL_DOT < SdfInstance::TURN);
182182
// primitive after expanding it, so fills and gradients keep their
183183
// frame axes.
184184
pub(in crate::visuals) fn turn_instances(instances: &mut [SdfInstance], turn: QuarterTurn) {
185+
if turn.0 == 0 {
186+
return;
187+
}
185188
let tag = SdfInstance::TURN * f32::from(turn.0);
186189
for instance in instances {
187190
instance.params[3] = instance.params[3] % SdfInstance::TURN + tag;

‎src/visuals/spectrogram/processor.rs‎

Lines changed: 146 additions & 88 deletions
Large diffs are not rendered by default.

‎src/visuals/spectrogram/render.rs‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ use crate::visuals::render::common::{
1515
QuarterTurn, RenderPipelineSpec, begin_pass, create_buffer, create_render_pipeline,
1616
};
1717

18-
use super::processor::{ColumnKind, SpectrogramColumn, SpectrogramPoint, col_byte_stride};
19-
use crate::util::audio::FrequencyScale;
18+
use super::processor::{
19+
ColumnKind, SpectrogramColumn, SpectrogramPoint, col_byte_stride, pack_classic_db,
20+
};
21+
use crate::util::audio::{DB_FLOOR, FrequencyScale};
2022

2123
pub const SPECTROGRAM_PALETTE_SIZE: usize = crate::visuals::palettes::spectrogram::SIZE;
2224

@@ -1072,8 +1074,7 @@ impl Resources {
10721074
}
10731075
for (slot, column) in p.newest_slots(uploads.len()).zip(uploads) {
10741076
if let SpectrogramColumn::Reassigned(points) = column {
1075-
let times = points.iter().map(|point| point.time_offset);
1076-
let [lo, hi] = times.fold(NO_SPAN, |[lo, hi], t| [lo.min(t), hi.max(t)]);
1077+
let [lo, hi] = time_span(points);
10771078
self.spans[slot] = [lo - 0.5, hi + 0.5];
10781079
self.reach = self.reach.max(hi + 0.5);
10791080
}
@@ -1087,13 +1088,11 @@ impl Resources {
10871088
page.write(queue, slot % columns, points);
10881089
}
10891090
}
1090-
(RingStorage::Classic { buf, .. }, SpectrogramColumn::Classic(mags))
1091-
if !mags.is_empty() =>
1092-
{
1091+
(RingStorage::Classic { buf, .. }, SpectrogramColumn::Classic(mags)) => {
10931092
let packed = &mut self.classic_upload_scratch;
10941093
let written = mags.len().min(packed.len());
10951094
packed[..written].copy_from_slice(&mags[..written]);
1096-
packed[written..].fill(0);
1095+
packed[written..].fill(pack_classic_db(DB_FLOOR));
10971096
let offset = slot as u64 * self.ring.layout.stride;
10981097
queue.write_buffer(buf, offset, bytemuck::cast_slice(packed));
10991098
}
@@ -1103,6 +1102,18 @@ impl Resources {
11031102
}
11041103
}
11051104

1105+
// Lanes keep the comparisons off one long dependency chain.
1106+
fn time_span(points: &[SpectrogramPoint]) -> [f32; 2] {
1107+
let merge = |[lo, hi]: [f32; 2], [a, b]: [f32; 2]| [lo.min(a), hi.max(b)];
1108+
let (chunks, rest) = points.as_chunks::<8>();
1109+
let mut lanes = [NO_SPAN; 8];
1110+
for chunk in chunks {
1111+
lanes = std::array::from_fn(|lane| merge(lanes[lane], [chunk[lane].time_offset; 2]));
1112+
}
1113+
let times = rest.iter().map(|point| [point.time_offset; 2]);
1114+
lanes.into_iter().chain(times).fold(NO_SPAN, merge)
1115+
}
1116+
11061117
// First frame column of the image, where the resolve starts showing it: evicted columns splat
11071118
// only left of it. It is past zero only when the history budget keeps fewer columns than the
11081119
// view spans, and it never decreases between redraws, as the reach only grows.

‎src/visuals/spectrum/processor.rs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use crate::dsp::{AudioBlock, SampleRate};
55
use crate::util::Finite;
66
use crate::util::audio::{
7-
Channel, DcHistory, FftSize, HopQueue, LN_TO_DB, NegativeDb, Seconds, WindowKind, WindowedFft,
8-
db_to_power, smoothing_weight,
7+
Channel, DcHistory, FftSize, HopQueue, NegativeDb, Seconds, WindowKind, WindowedFft,
8+
db_to_power, power_to_db, smoothing_weight,
99
};
1010
use serde::{Deserialize, Serialize};
1111
use std::num::NonZeroUsize;
@@ -327,13 +327,8 @@ impl SpectrumLevelBuffers {
327327
.zip(raw_out)
328328
.zip(weighted_out);
329329
for (((&power, &weight), raw), weighted) in bins {
330-
if power < self.state_floor {
331-
(*raw, *weighted) = (floor, floor);
332-
continue;
333-
}
334-
let db = power.ln() * LN_TO_DB;
335-
*raw = db.max(floor);
336-
*weighted = (db + weight).max(floor);
330+
let db = power_to_db(power, f32::NEG_INFINITY);
331+
(*raw, *weighted) = (db.max(floor), (db + weight).max(floor));
337332
}
338333
}
339334
}

‎src/visuals/spectrum/render.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ impl SpectrumParams {
118118
project(points, &self.normalized_points);
119119
project(points2, &self.secondary_points);
120120

121+
let ramp = |point: (f32, f32)| self.ramp_color((baseline - point.1) / bounds.height);
122+
let mut c0 = points.first().copied().map_or([0.0; 4], ramp);
121123
for &[p0, p1] in points.array_windows() {
122-
let c0 = self.ramp_color((baseline - p0.1) / bounds.height);
123-
let c1 = self.ramp_color((baseline - p1.1) / bounds.height);
124+
let c1 = ramp(p1);
124125
if c0[3] > 0.0 || c1[3] > 0.0 {
125126
vertices.push(baseline_segment_instance(
126127
p0,
@@ -131,6 +132,7 @@ impl SpectrumParams {
131132
false,
132133
));
133134
}
135+
c0 = c1;
134136
}
135137
extend_aa_line_list(
136138
vertices,

‎src/visuals/waveform/render.rs‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::visuals::render::common::{
1515
ClipTransform, GeometryScratch, amplitude_y, extend_filled_line, quad_instance,
1616
};
1717
use crate::visuals::waveform::processor::{
18-
BAND_COUNT, MAX_COLUMN_CAPACITY, WAVEFORM_SILENCE_AMPLITUDE, WaveColumn, WaveFrame,
19-
WaveformPreview,
18+
BAND_COUNT, DERIVED_CHANNELS, MAX_COLUMN_CAPACITY, WAVEFORM_SILENCE_AMPLITUDE, WaveColumn,
19+
WaveFrame, WaveformPreview,
2020
};
2121

2222
const COLUMN_WIDTH_PIXELS: f32 = 1.0;
@@ -32,7 +32,7 @@ const LOUDNESS_STOPS: [f32; BAND_COUNT] = [0.0, 0.5, 1.0];
3232
pub struct WaveformParams {
3333
pub bounds: Rectangle,
3434
pub lane: usize,
35-
pub data: Arc<Mutex<VecDeque<WaveFrame>>>,
35+
pub data: Arc<Mutex<WaveHistory>>,
3636
pub preview: WaveformPreview,
3737
pub color_mode: WaveformColorMode,
3838
pub history_mode: WaveformHistoryMode,
@@ -41,6 +41,14 @@ pub struct WaveformParams {
4141
pub key: u64,
4242
}
4343

44+
// Frames, oldest first, each with its lanes' column colors once drawn. A column's color depends
45+
// only on it, the palette and the color mode, so it is kept until they change.
46+
#[derive(Debug, Default)]
47+
pub struct WaveHistory {
48+
pub(super) frames: VecDeque<(WaveFrame, Option<[[f32; 4]; DERIVED_CHANNELS]>)>,
49+
colored_with: Option<(WaveformColorMode, [[f32; 4]; BAND_COUNT])>,
50+
}
51+
4452
impl WaveformParams {
4553
fn column_color(&self, column: WaveColumn) -> [f32; 4] {
4654
match self.color_mode {
@@ -98,7 +106,14 @@ fn sample_y_span(lane: Rectangle, min: f32, max: f32) -> Option<(f32, f32)> {
98106

99107
impl WaveformParams {
100108
fn build_vertices(&self, scratch: &mut GeometryScratch) {
101-
let data = crate::util::unpoison(self.data.lock());
109+
let mut stored = crate::util::unpoison(self.data.lock());
110+
let colored_with = Some((self.color_mode, self.palette));
111+
if std::mem::replace(&mut stored.colored_with, colored_with) != colored_with {
112+
for (_, colors) in &mut stored.frames {
113+
*colors = None;
114+
}
115+
}
116+
let data = &mut stored.frames;
102117
let columns = column_count(self.bounds.width).min(data.len());
103118
let start = data.len().saturating_sub(columns);
104119
let preview_columns = self.preview.columns.filter(|_| self.preview.progress > 0.0);
@@ -123,22 +138,25 @@ impl WaveformParams {
123138
let dist_steps = (columns - 1 - i) as f32;
124139
right_edge - dist_steps * col_width - scroll_offset - col_width
125140
};
126-
let push_column = |vertices: &mut Vec<_>, x0, x1, column: WaveColumn| {
141+
let push_column = |vertices: &mut Vec<_>, x0, x1, column: WaveColumn, color| {
127142
if let Some((y0, y1)) = sample_y_span(self.bounds, column.min, column.max) {
128-
let color = self.column_color(column);
129143
vertices.push(quad_instance(x0, y0, x1, y1, clip, color));
130144
}
131145
};
132146

133-
for (i, frame) in data.range(start..start + columns).enumerate() {
147+
for (i, (frame, colors)) in data.range_mut(start..start + columns).enumerate() {
148+
let colors =
149+
colors.get_or_insert_with(|| frame.map(|column| self.column_color(column)));
134150
let column = frame[self.lane];
135151
let x = column_x(i);
136-
push_column(vertices, x, x + col_width, column);
152+
push_column(vertices, x, x + col_width, column, colors[self.lane]);
137153
}
138154

139155
if let Some(preview_columns) = preview_columns {
140156
let start_x = right_edge - scroll_offset;
141-
push_column(vertices, start_x, right_edge, preview_columns[self.lane]);
157+
let column = preview_columns[self.lane];
158+
let color = self.column_color(column);
159+
push_column(vertices, start_x, right_edge, column, color);
142160
}
143161

144162
if let Some(history) = history {
@@ -150,16 +168,14 @@ impl WaveformParams {
150168
let fill_color = rgba_scale_alpha(color, BAND_FILL_ALPHA);
151169

152170
pts.clear();
153-
pts.extend(
154-
data.range(start..start + columns)
155-
.enumerate()
156-
.map(|(i, frame)| {
157-
let column = frame[self.lane];
158-
let db = column.rms_db[history][band];
159-
let level = ((db - floor) / -floor).clamp(0.0, 1.0);
160-
(column_x(i), baseline - level * band_height)
161-
}),
162-
);
171+
pts.extend(data.range(start..start + columns).enumerate().map(
172+
|(i, (frame, _))| {
173+
let column = frame[self.lane];
174+
let db = column.rms_db[history][band];
175+
let level = ((db - floor) / -floor).clamp(0.0, 1.0);
176+
(column_x(i), baseline - level * band_height)
177+
},
178+
));
163179
let last_y = pts.last().expect("nonempty waveform history").1;
164180
pts.push((right_edge, last_y));
165181
extend_filled_line(

‎src/visuals/waveform/state.rs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
// Copyright (C) 2026 Maika Namuo
33

44
use super::processor::{BAND_COUNT, WAVEFORM_CHANNELS, WaveFrame, WaveformPreview, WaveformUpdate};
5-
use super::render::{WaveformParams, column_count};
5+
use super::render::{WaveHistory, WaveformParams, column_count};
66
use crate::persistence::settings::WaveformSettings;
77
use crate::util::{color::color_to_rgba, unpoison};
88
use crate::visuals::palettes;
99
use crate::visuals::render::common::lane_bounds;
1010
use iced::Color;
11-
use std::collections::VecDeque;
1211
use std::sync::{Arc, Mutex};
1312
use std::time::{Duration, Instant};
1413

@@ -17,8 +16,7 @@ const SCROLL_CLOCK_TIMEOUT: Duration = Duration::from_millis(100);
1716

1817
crate::macros::default_struct! {
1918
pub(crate) struct WaveformState {
20-
data: Arc<Mutex<VecDeque<WaveFrame>>> =
21-
Arc::new(Mutex::new(VecDeque::with_capacity(INITIAL_VIEW_COLUMNS))),
19+
data: Arc<Mutex<WaveHistory>> = Arc::default(),
2220
preview: WaveformPreview = WaveformPreview::default(),
2321
scroll: (Instant, f32) = (Instant::now(), 0.0),
2422
snapshot_at: Instant = Instant::now(),
@@ -32,7 +30,7 @@ crate::macros::default_struct! {
3230

3331
impl WaveformState {
3432
pub fn reset_audio(&mut self) {
35-
unpoison(self.data.lock()).clear();
33+
unpoison(self.data.lock()).frames.clear();
3634
self.preview = WaveformPreview::default();
3735
let now = Instant::now();
3836
self.scroll = (now, 0.0);
@@ -53,7 +51,7 @@ impl WaveformState {
5351
return;
5452
}
5553
let max_columns = self.view_columns;
56-
let data = &mut *unpoison(self.data.lock());
54+
let data = &mut unpoison(self.data.lock()).frames;
5755
if update.reset {
5856
data.clear();
5957
self.quiescent_columns = 0;
@@ -73,7 +71,7 @@ impl WaveformState {
7371
if data.len() == max_columns {
7472
data.pop_front();
7573
}
76-
data.push_back(columns);
74+
data.push_back((columns, None));
7775
}
7876
}
7977

@@ -85,7 +83,7 @@ impl WaveformState {
8583
pub(in crate::visuals) fn is_quiescent(&self) -> bool {
8684
let needed = self.view_columns;
8785
self.quiescent_columns >= needed
88-
&& unpoison(self.data.lock()).len() >= needed
86+
&& unpoison(self.data.lock()).frames.len() >= needed
8987
&& self.preview.columns.unwrap_or_default() == WaveFrame::default()
9088
}
9189

0 commit comments

Comments
 (0)