Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

feat(ui): a waveform's bars are one pitch on screen at every zoom

Jareerd982e116df42
diff --git a/src/crates/concat/src/format.rs b/src/crates/concat/src/format.rs
index 362e972e..ba380bc9 100644
--- a/src/crates/concat/src/format.rs
+++ b/src/crates/concat/src/format.rs
@@ -177,7 +177,7 @@ pub fn wave_path(
/// Fewest columns worth drawing, and the most: enough that a clip a
/// screen wide reads a column a pixel, few enough that the string stays
/// under a few hundred kilobytes.
- const COLUMNS: std::ops::RangeInclusive<usize> = 8..=512;
+ const COLUMNS: std::ops::RangeInclusive<usize> = 8..=4096;
/// Silence still draws a sliver: a hairline along the floor of a clip
/// rather than a gap in it.
const FLOOR: f32 = 0.024;
@@ -223,11 +223,12 @@ pub const WAVE_PITCH: f32 = 3.0;
/// own; see `media_bin`.
pub const WAVE_BAR: f32 = 2.0 / 3.0;
-/// How many bars a clip `seconds` long gets at `seconds_per_pixel`: one a
+/// How many bars a span of `seconds` gets at `seconds_per_pixel`: one a
/// [`WAVE_PITCH`], rounded up to the next sixteen so a zoom rebuilds the
/// path at each step of that and not at every pixel, held to what
-/// `wave_path` draws - past the cap a bar is a little wider than its
-/// pitch, on a clip already wider than a screen.
+/// `wave_path` draws. The span is the window of a clip on screen (see
+/// `Studio::wave`), so the cap is a screen's worth and a bar is never
+/// stretched past its pitch.
pub fn wave_columns(seconds: f32, seconds_per_pixel: f32) -> usize {
if seconds.is_nan() || seconds <= 0.0 || seconds_per_pixel.is_nan() || seconds_per_pixel <= 0.0
{
@@ -235,7 +236,7 @@ pub fn wave_columns(seconds: f32, seconds_per_pixel: f32) -> usize {
}
let pixels = (seconds / seconds_per_pixel).ceil().max(1.0);
let bars = (pixels / WAVE_PITCH).ceil() as usize;
- bars.div_ceil(16).max(1).saturating_mul(16).clamp(8, 512)
+ bars.div_ceil(16).max(1).saturating_mul(16).clamp(8, 4096)
}
/// A moment in the past, in the words a recents row wants: "just now",
@@ -403,7 +404,7 @@ mod tests {
wave_path(&peaks, 0.0, 2.0, 1_000_000, WAVE_BAR)
.matches('M')
.count(),
- 512
+ 4096
);
// A bar takes `bar` of its pitch; the rest is the gap.
let eight = wave_path(&peaks, 0.0, 2.0, 8, 0.75);
@@ -472,7 +473,7 @@ mod tests {
assert_eq!(wave_columns(10.0, 0.01), 336, "1000 px is 334 bars");
assert_eq!(
wave_columns(600.0, 0.01),
- 512,
+ 4096,
"held to the most worth drawing"
);
assert_eq!(wave_columns(0.1, 0.05), 16, "never under a step");
diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rs
index cf929822..08d06237 100644
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@ -47,8 +47,8 @@ use crate::dock::{
Dock, DockLayout, SEAT_GAP, default_dock, lay_out, nearest_row, row_at, row_top,
};
use crate::format::{
- WAVE_BAR, colour_of, frames_timecode, hex_of, hex_rgba, hex_with_alpha, wave_columns,
- wave_path, when_phrase,
+ WAVE_BAR, WAVE_PITCH, colour_of, frames_timecode, hex_of, hex_rgba, hex_with_alpha,
+ wave_columns, wave_path, when_phrase,
};
use crate::host::{
CachedStrip, Host, MediaArt, WindowArt, cached_media_art, cached_window_art, image_at,
@@ -2109,27 +2109,71 @@ impl Studio {
///
/// At unity: the clip's volume scales the drawing in the lane, so a
/// volume drag never comes here - see `format::wave_path`.
- fn wave(&self, clip: &Clip) -> SharedString {
+ ///
+ /// Only the window of the clip that is on screen, and a screen either
+ /// side of it, is built: what comes back is the path and where it
+ /// sits on the clip, as fractions of the clip's length. A clip zoomed
+ /// in far enough to be wider than the screen many times over would
+ /// otherwise spread its bars across all of that width - a bar the
+ /// width of a finger - or want tens of thousands of them. This way a
+ /// bar is one pitch on screen at every zoom, and the count is bounded
+ /// by the screen, not the clip. The window is quantised to strides
+ /// of sixty-four bars, so a scroll rebuilds at each stride and not at
+ /// every pixel, and the cache holds a few hundred windows before it
+ /// is emptied.
+ fn wave(&self, clip: &Clip) -> (SharedString, f32, f32) {
if clip.muted == Some(true) {
- return SharedString::new();
+ return Default::default();
}
// The stream this clip plays; its peaks come when they are decoded,
// and until then the lane is bare rather than showing another
// track's shape.
let art = art_key(&clip.media_id, clip.audio_stream);
let Some(peaks) = self.peaks.get(&art) else {
- return SharedString::new();
+ return Default::default();
};
let step = |seconds: f32| (seconds * WAVE_STEPS).round() / WAVE_STEPS;
let (source_start, duration) = (step(clip.source_start as f32), step(clip.duration as f32));
- let columns = wave_columns(clip.duration as f32, self.lanes.seconds_per_pixel);
- let key = format!("{art}|{source_start:.3}|{duration:.3}|{columns}");
+ if duration <= 0.0 {
+ return Default::default();
+ }
+ let seconds_per_pixel = self.lanes.seconds_per_pixel;
+ let stride = (WAVE_PITCH * seconds_per_pixel * 64.0).max(1.0 / WAVE_STEPS);
+ let (from, to) = match self.lanes.published_span() {
+ Some((left, right)) => {
+ let from = (left - clip.start as f32).max(0.0);
+ let to = (right - clip.start as f32).min(duration);
+ if to > from {
+ (
+ (from / stride).floor() * stride,
+ ((to / stride).ceil() * stride).min(duration),
+ )
+ } else {
+ (0.0, duration)
+ }
+ }
+ None => (0.0, duration),
+ };
+ let window = to - from;
+ let columns = wave_columns(window, seconds_per_pixel);
+ let placed = (from / duration, window / duration);
+ let key = format!("{art}|{source_start:.3}|{from:.3}|{window:.3}|{columns}");
if let Some(cached) = self.waves.borrow().get(&key) {
- return cached.clone();
+ return (cached.clone(), placed.0, placed.1);
+ }
+ let built = SharedString::from(wave_path(
+ peaks,
+ source_start + from,
+ window,
+ columns,
+ WAVE_BAR,
+ ));
+ let mut waves = self.waves.borrow_mut();
+ if waves.len() >= 512 {
+ waves.clear();
}
- let built = SharedString::from(wave_path(peaks, source_start, duration, columns, WAVE_BAR));
- self.waves.borrow_mut().insert(key, built.clone());
- built
+ waves.insert(key, built.clone());
+ (built, placed.0, placed.1)
}
// ── placing things ──
@@ -5006,7 +5050,9 @@ impl Studio {
.map(|text| text.content.as_str())
.unwrap_or_default()
.into(),
- wave,
+ wave: wave.0,
+ wave_from: wave.1,
+ wave_span: wave.2,
strip: self.strip_of(clip),
}
})
diff --git a/src/crates/concat/ui/timeline/lanes.slint b/src/crates/concat/ui/timeline/lanes.slint
index 0c5b0ef1..21d09978 100644
--- a/src/crates/concat/ui/timeline/lanes.slint
+++ b/src/crates/concat/ui/timeline/lanes.slint
@@ -213,9 +213,9 @@ component Clip inherits Rectangle {
// is at unity and the clip's volume scales its height here, so a
// volume drag moves the picture without a new path from Rust.
if root.item.wave != "" && !root.banded: Wave {
- x: 0;
+ x: root.item.wave-from * parent.width;
y: 0;
- width: parent.width;
+ width: root.item.wave-span * parent.width;
height: parent.height;
commands: root.item.wave;
gain: root.item.volume;
@@ -319,9 +319,9 @@ component Clip inherits Rectangle {
}
if root.item.wave != "": Wave {
- x: 0;
+ x: root.item.wave-from * parent.width;
y: 1px;
- width: parent.width;
+ width: root.item.wave-span * parent.width;
height: parent.height - 1px;
commands: root.item.wave;
gain: root.item.volume;
diff --git a/src/crates/concat/ui/timeline/model.slint b/src/crates/concat/ui/timeline/model.slint
index 9fb91c84..b4803f4b 100644
--- a/src/crates/concat/ui/timeline/model.slint
+++ b/src/crates/concat/ui/timeline/model.slint
@@ -112,6 +112,10 @@ export struct ClipData {
/// that draws it is `fit: fill`, which stretches the box onto whatever
/// width the clip currently has. Built once in Rust, where the peaks are.
wave: string,
+ /// Where on the clip `wave` sits: its start and its length as fractions
+ /// of the clip's, since only the window on screen is built.
+ wave-from: float,
+ wave-span: float,
/// The frames a picture clip tiles along its body; `frames` is 0 when
/// there are none yet.
strip: StripData,