Audacity trace, ribbon, fill — colour is a picker
Browse files at this commit70a02fba137e70da90e39523a4cbac452e1cc961Parent: dfcec06- feat
- Audacity trace, ribbon, fill — colour is a picker
3 changed files +458 -98
- crates/app/src/player/library.rs +40 -0
- crates/app/src/ui/dock_stance.rs +135 -16
- crates/engine/src/decode.rs +283 -82
crates/app/src/player/library.rsmodified+40 -0
| @@ -203,6 +203,46 @@ impl Player { | |||
| 203 | cx.notify(); | 203 | cx.notify(); |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | + pub(crate) fn set_viz_style(&mut self, style: u8, cx: &mut Context<Self>) { | ||
| 207 | + let Some((lane, idx)) = self.selected.anchor() else { | ||
| 208 | + return; | ||
| 209 | + }; | ||
| 210 | + let Some(session) = &mut self.session else { | ||
| 211 | + return; | ||
| 212 | + }; | ||
| 213 | + let Some(clip) = session.lane_clips(lane).get(idx).copied() else { | ||
| 214 | + return; | ||
| 215 | + }; | ||
| 216 | + if clip.visualizer & 1 == 0 { | ||
| 217 | + return; | ||
| 218 | + } | ||
| 219 | + let next = engine::decode::with_viz_style(clip.visualizer, style); | ||
| 220 | + if next != clip.visualizer && session.set_visualizer(lane, idx, next) { | ||
| 221 | + self.reset_after_reseek(); | ||
| 222 | + } | ||
| 223 | + cx.notify(); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + pub(crate) fn set_viz_hue(&mut self, hue: u8, cx: &mut Context<Self>) { | ||
| 227 | + let Some((lane, idx)) = self.selected.anchor() else { | ||
| 228 | + return; | ||
| 229 | + }; | ||
| 230 | + let Some(session) = &mut self.session else { | ||
| 231 | + return; | ||
| 232 | + }; | ||
| 233 | + let Some(clip) = session.lane_clips(lane).get(idx).copied() else { | ||
| 234 | + return; | ||
| 235 | + }; | ||
| 236 | + if clip.visualizer & 1 == 0 { | ||
| 237 | + return; | ||
| 238 | + } | ||
| 239 | + let next = engine::decode::with_viz_hue(clip.visualizer, hue); | ||
| 240 | + if next != clip.visualizer && session.set_visualizer(lane, idx, next) { | ||
| 241 | + self.reset_after_reseek(); | ||
| 242 | + } | ||
| 243 | + cx.notify(); | ||
| 244 | + } | ||
| 245 | + | ||
| 206 | /// Takes a library row's file out of the list, which is the one thing a row | 246 | /// Takes a library row's file out of the list, which is the one thing a row |
| 207 | /// can lose. Refused in the engine's own words while clips still play from | 247 | /// can lose. Refused in the engine's own words while clips still play from |
| 208 | /// it -- and those words name the lanes holding them, so the refusal says | 248 | /// it -- and those words name the lanes holding them, so the refusal says |
crates/app/src/ui/dock_stance.rsmodified+135 -16
| @@ -1187,6 +1187,25 @@ fn transition_row(player: &Player, cx: &mut Context<Player>) -> Option<impl Into | |||
| 1187 | /// these onto. Drag-while-playing and every other gesture on a row is | 1187 | /// these onto. Drag-while-playing and every other gesture on a row is |
| 1188 | /// whatever that card already does; nothing about the gesture is reimplemented | 1188 | /// whatever that card already does; nothing about the gesture is reimplemented |
| 1189 | /// here. | 1189 | /// here. |
| 1190 | +const VIZ_HUE_IDS: [&str; 16] = [ | ||
| 1191 | + "dock-viz-hue-0", | ||
| 1192 | + "dock-viz-hue-1", | ||
| 1193 | + "dock-viz-hue-2", | ||
| 1194 | + "dock-viz-hue-3", | ||
| 1195 | + "dock-viz-hue-4", | ||
| 1196 | + "dock-viz-hue-5", | ||
| 1197 | + "dock-viz-hue-6", | ||
| 1198 | + "dock-viz-hue-7", | ||
| 1199 | + "dock-viz-hue-8", | ||
| 1200 | + "dock-viz-hue-9", | ||
| 1201 | + "dock-viz-hue-10", | ||
| 1202 | + "dock-viz-hue-11", | ||
| 1203 | + "dock-viz-hue-12", | ||
| 1204 | + "dock-viz-hue-13", | ||
| 1205 | + "dock-viz-hue-14", | ||
| 1206 | + "dock-viz-hue-15", | ||
| 1207 | +]; | ||
| 1208 | + | ||
| 1190 | fn viz_style_verbs(player: &Player, cx: &mut Context<Player>) -> impl IntoElement { | 1209 | fn viz_style_verbs(player: &Player, cx: &mut Context<Player>) -> impl IntoElement { |
| 1191 | let flags = player.selected.anchor().and_then(|(lane, idx)| { | 1210 | let flags = player.selected.anchor().and_then(|(lane, idx)| { |
| 1192 | let session = player.session.as_ref()?; | 1211 | let session = player.session.as_ref()?; |
| @@ -1196,6 +1215,30 @@ fn viz_style_verbs(player: &Player, cx: &mut Context<Player>) -> impl IntoElemen | |||
| 1196 | let Some(flags) = flags else { | 1215 | let Some(flags) = flags else { |
| 1197 | return div().id("dock-viz-styles"); | 1216 | return div().id("dock-viz-styles"); |
| 1198 | }; | 1217 | }; |
| 1218 | + let pick = |id: &'static str, label_text: &'static str, style: u8, on: bool| { | ||
| 1219 | + let row = label(type_scale::LABEL_ROW_PX, FontWeight::MEDIUM); | ||
| 1220 | + div() | ||
| 1221 | + .id(id) | ||
| 1222 | + .flex_none() | ||
| 1223 | + .h(px(CONTROL_H)) | ||
| 1224 | + .px(px(8.)) | ||
| 1225 | + .flex() | ||
| 1226 | + .items_center() | ||
| 1227 | + .cursor_pointer() | ||
| 1228 | + .text_color(rgb(if on { INK1() } else { INK3() })) | ||
| 1229 | + .font(row.font) | ||
| 1230 | + .text_size(row.size) | ||
| 1231 | + .children(hitmap::control(id, label_text, true)) | ||
| 1232 | + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | ||
| 1233 | + this.set_viz_style(style, cx); | ||
| 1234 | + })) | ||
| 1235 | + .child(if on { | ||
| 1236 | + format!("· {label_text}") | ||
| 1237 | + } else { | ||
| 1238 | + label_text.to_string() | ||
| 1239 | + }) | ||
| 1240 | + .into_any_element() | ||
| 1241 | + }; | ||
| 1199 | let bit = |id: &'static str, label_text: &'static str, bit: u8, on: bool| { | 1242 | let bit = |id: &'static str, label_text: &'static str, bit: u8, on: bool| { |
| 1200 | let style = label(type_scale::LABEL_ROW_PX, FontWeight::MEDIUM); | 1243 | let style = label(type_scale::LABEL_ROW_PX, FontWeight::MEDIUM); |
| 1201 | div() | 1244 | div() |
| @@ -1226,17 +1269,23 @@ fn viz_style_verbs(player: &Player, cx: &mut Context<Player>) -> impl IntoElemen | |||
| 1226 | .flex() | 1269 | .flex() |
| 1227 | .flex_col() | 1270 | .flex_col() |
| 1228 | .gap(px(2.)) | 1271 | .gap(px(2.)) |
| 1229 | - .child(bit( | 1272 | + .child(pick( |
| 1230 | - "dock-viz-outline", | 1273 | + "dock-viz-wave", |
| 1231 | - "Outline", | 1274 | + "Waveform", |
| 1232 | - engine::decode::VIZ_OUTLINE, | 1275 | + engine::decode::VIZ_STYLE_WAVE, |
| 1233 | - flags & engine::decode::VIZ_OUTLINE != 0, | 1276 | + engine::decode::viz_style(flags) == engine::decode::VIZ_STYLE_WAVE, |
| 1234 | )) | 1277 | )) |
| 1235 | - .child(bit( | 1278 | + .child(pick( |
| 1236 | - "dock-viz-smooth", | 1279 | + "dock-viz-ribbon", |
| 1237 | - "Smooth", | 1280 | + "Ribbon", |
| 1238 | - engine::decode::VIZ_SMOOTH, | 1281 | + engine::decode::VIZ_STYLE_RIBBON, |
| 1239 | - flags & engine::decode::VIZ_SMOOTH != 0, | 1282 | + engine::decode::viz_style(flags) == engine::decode::VIZ_STYLE_RIBBON, |
| 1283 | + )) | ||
| 1284 | + .child(pick( | ||
| 1285 | + "dock-viz-fill", | ||
| 1286 | + "Fill", | ||
| 1287 | + engine::decode::VIZ_STYLE_FILL, | ||
| 1288 | + engine::decode::viz_style(flags) == engine::decode::VIZ_STYLE_FILL, | ||
| 1240 | )) | 1289 | )) |
| 1241 | .child(bit( | 1290 | .child(bit( |
| 1242 | "dock-viz-fast", | 1291 | "dock-viz-fast", |
| @@ -1244,12 +1293,82 @@ fn viz_style_verbs(player: &Player, cx: &mut Context<Player>) -> impl IntoElemen | |||
| 1244 | engine::decode::VIZ_FAST, | 1293 | engine::decode::VIZ_FAST, |
| 1245 | flags & engine::decode::VIZ_FAST != 0, | 1294 | flags & engine::decode::VIZ_FAST != 0, |
| 1246 | )) | 1295 | )) |
| 1247 | - .child(bit( | 1296 | + .child(viz_hue_row(flags, cx)) |
| 1248 | - "dock-viz-tint", | 1297 | +} |
| 1249 | - "Colour", | 1298 | + |
| 1250 | - engine::decode::VIZ_TINT, | 1299 | +fn viz_hue_row(flags: u8, cx: &mut Context<Player>) -> impl IntoElement { |
| 1251 | - flags & engine::decode::VIZ_TINT != 0, | 1300 | + let hue = engine::decode::viz_hue(flags); |
| 1252 | - )) | 1301 | + let style = label(type_scale::LABEL_ROW_PX, FontWeight::MEDIUM); |
| 1302 | + let step = |id: &'static str, glyph: &'static str, by: i8, cx: &mut Context<Player>| { | ||
| 1303 | + div() | ||
| 1304 | + .id(id) | ||
| 1305 | + .flex() | ||
| 1306 | + .w(px(HIT_MIN)) | ||
| 1307 | + .h(px(HIT_MIN)) | ||
| 1308 | + .items_center() | ||
| 1309 | + .justify_center() | ||
| 1310 | + .rounded(px(3.)) | ||
| 1311 | + .bg(rgb(DARK_RAISED())) | ||
| 1312 | + .cursor_pointer() | ||
| 1313 | + .children(hitmap::control(id, "Colour", true)) | ||
| 1314 | + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | ||
| 1315 | + let cur = this | ||
| 1316 | + .selected | ||
| 1317 | + .anchor() | ||
| 1318 | + .and_then(|(lane, idx)| { | ||
| 1319 | + this.session | ||
| 1320 | + .as_ref()? | ||
| 1321 | + .lane_clips(lane) | ||
| 1322 | + .get(idx) | ||
| 1323 | + .map(|c| engine::decode::viz_hue(c.visualizer)) | ||
| 1324 | + }) | ||
| 1325 | + .unwrap_or(0); | ||
| 1326 | + this.set_viz_hue((cur as i16 + i16::from(by)).rem_euclid(16) as u8, cx); | ||
| 1327 | + })) | ||
| 1328 | + .child(glyph) | ||
| 1329 | + }; | ||
| 1330 | + div() | ||
| 1331 | + .id("dock-viz-hue") | ||
| 1332 | + .flex_none() | ||
| 1333 | + .flex() | ||
| 1334 | + .items_center() | ||
| 1335 | + .gap(px(4.)) | ||
| 1336 | + .h(px(CONTROL_H)) | ||
| 1337 | + .px(px(8.)) | ||
| 1338 | + .child( | ||
| 1339 | + div() | ||
| 1340 | + .font(style.font) | ||
| 1341 | + .text_size(style.size) | ||
| 1342 | + .text_color(rgb(INK2())) | ||
| 1343 | + .child("Colour"), | ||
| 1344 | + ) | ||
| 1345 | + .child(step("dock-viz-hue-minus", "−", -1, cx)) | ||
| 1346 | + .child( | ||
| 1347 | + div() | ||
| 1348 | + .id("dock-viz-hue-swatches") | ||
| 1349 | + .flex_1() | ||
| 1350 | + .flex() | ||
| 1351 | + .gap(px(2.)) | ||
| 1352 | + .children((0u8..16).map(|i| { | ||
| 1353 | + let (r, g, b) = engine::decode::viz_ink_rgb(i); | ||
| 1354 | + let color = (u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b); | ||
| 1355 | + div() | ||
| 1356 | + .id(("dock-viz-hue", u64::from(i))) | ||
| 1357 | + .flex_1() | ||
| 1358 | + .h(px(14.)) | ||
| 1359 | + .min_w(px(8.)) | ||
| 1360 | + .rounded(px(2.)) | ||
| 1361 | + .bg(rgb(color)) | ||
| 1362 | + .when(i == hue, |d| d.border_1().border_color(rgb(INK1()))) | ||
| 1363 | + .cursor_pointer() | ||
| 1364 | + .children(hitmap::control(VIZ_HUE_IDS[i as usize], "Colour", true)) | ||
| 1365 | + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | ||
| 1366 | + this.set_viz_hue(i, cx); | ||
| 1367 | + })) | ||
| 1368 | + .into_any_element() | ||
| 1369 | + })), | ||
| 1370 | + ) | ||
| 1371 | + .child(step("dock-viz-hue-plus", "+", 1, cx)) | ||
| 1253 | } | 1372 | } |
| 1254 | 1373 | ||
| 1255 | fn clip_tab( | 1374 | fn clip_tab( |
crates/engine/src/decode.rsmodified+283 -82
| @@ -609,7 +609,7 @@ impl DecodeSession { | |||
| 609 | 609 | ||
| 610 | /// A worker for an audio file played as a *picture* ([`crate::is_audio`]): | 610 | /// A worker for an audio file played as a *picture* ([`crate::is_audio`]): |
| 611 | /// the file's peaks are decoded **once** on the worker, and every picture | 611 | /// the file's peaks are decoded **once** on the worker, and every picture |
| 612 | - /// after that is painted -- a sliding mirrored waveform | 612 | + /// after that is painted -- a sliding visualizer |
| 613 | /// ([`visualizer_i420`]) at the canvas's own size, then graded, placed and | 613 | /// ([`visualizer_i420`]) at the canvas's own size, then graded, placed and |
| 614 | /// converted exactly like a decoded picture, so a grade, a transform and a | 614 | /// converted exactly like a decoded picture, so a grade, a transform and a |
| 615 | /// fit policy reach it the way they reach anything else on the timeline. | 615 | /// fit policy reach it the way they reach anything else on the timeline. |
| @@ -1408,28 +1408,154 @@ fn rgb_to_i420(rgb: &[u8], width: usize, height: usize) -> (Vec<u8>, Vec<u8>, Ve | |||
| 1408 | /// Peak buckets per second the visualizer paints from, shared by the one | 1408 | /// Peak buckets per second the visualizer paints from, shared by the one |
| 1409 | /// caller that decodes them ([`DecodeSession::open_visualizer`], preview and | 1409 | /// caller that decodes them ([`DecodeSession::open_visualizer`], preview and |
| 1410 | /// export both) and the painter that reads them. | 1410 | /// export both) and the painter that reads them. |
| 1411 | -pub(crate) const VIZ_BUCKETS_PER_SEC: u32 = 60; | 1411 | +pub(crate) const VIZ_BUCKETS_PER_SEC: u32 = 4000; |
| 1412 | -pub const VIZ_OUTLINE: u8 = 1 << 1; | 1412 | +/// Bits 1-2: which picture this visualizer paints. Mutually exclusive — |
| 1413 | -pub const VIZ_SMOOTH: u8 = 1 << 2; | 1413 | +/// a picker, not independent toggles. |
| 1414 | +pub const VIZ_STYLE_WAVE: u8 = 0; | ||
| 1415 | +pub const VIZ_STYLE_RIBBON: u8 = 1; | ||
| 1416 | +pub const VIZ_STYLE_FILL: u8 = 2; | ||
| 1414 | pub const VIZ_FAST: u8 = 1 << 3; | 1417 | pub const VIZ_FAST: u8 = 1 << 3; |
| 1415 | -pub const VIZ_TINT: u8 = 1 << 4; | 1418 | +/// High nibble of [`crate::project::Clip::visualizer`]: 16 inks. Zero is gold, then around |
| 1419 | +/// the wheel in 22.5° steps so a picker — not a click-cycle — chooses the | ||
| 1420 | +/// strand colour. | ||
| 1421 | +pub const VIZ_HUE_SHIFT: u8 = 4; | ||
| 1422 | + | ||
| 1423 | +/// The ink nibble stored in `flags`. | ||
| 1424 | +pub fn viz_hue(flags: u8) -> u8 { | ||
| 1425 | + flags >> VIZ_HUE_SHIFT | ||
| 1426 | +} | ||
| 1427 | + | ||
| 1428 | +/// Keep style bits, replace the ink nibble. | ||
| 1429 | +pub fn with_viz_hue(flags: u8, hue: u8) -> u8 { | ||
| 1430 | + (flags & 0x0F) | ((hue & 15) << VIZ_HUE_SHIFT) | ||
| 1431 | +} | ||
| 1432 | + | ||
| 1433 | +pub fn viz_style(flags: u8) -> u8 { | ||
| 1434 | + (flags >> 1) & 3 | ||
| 1435 | +} | ||
| 1436 | + | ||
| 1437 | +pub fn with_viz_style(flags: u8, style: u8) -> u8 { | ||
| 1438 | + (flags & !0b0110) | ((style & 3) << 1) | ||
| 1439 | +} | ||
| 1440 | + | ||
| 1441 | +/// Ink colour for the CLIP swatches and the painter. Hue 0 is gold. | ||
| 1442 | +pub fn viz_ink_rgb(hue: u8) -> (u8, u8, u8) { | ||
| 1443 | + hsv_to_rgb(48.0 + f32::from(hue & 15) * 22.5, 0.68, 0.92) | ||
| 1444 | +} | ||
| 1445 | + | ||
| 1446 | +fn hsv_to_rgb(h: f32, s: f32, v: f32) -> (u8, u8, u8) { | ||
| 1447 | + let h = ((h % 360.0) + 360.0) % 360.0; | ||
| 1448 | + let c = v * s; | ||
| 1449 | + let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs()); | ||
| 1450 | + let m = v - c; | ||
| 1451 | + let (r, g, b) = match (h / 60.0) as i32 { | ||
| 1452 | + 0 => (c, x, 0.0), | ||
| 1453 | + 1 => (x, c, 0.0), | ||
| 1454 | + 2 => (0.0, c, x), | ||
| 1455 | + 3 => (0.0, x, c), | ||
| 1456 | + 4 => (x, 0.0, c), | ||
| 1457 | + _ => (c, 0.0, x), | ||
| 1458 | + }; | ||
| 1459 | + ( | ||
| 1460 | + ((r + m) * 255.0).round() as u8, | ||
| 1461 | + ((g + m) * 255.0).round() as u8, | ||
| 1462 | + ((b + m) * 255.0).round() as u8, | ||
| 1463 | + ) | ||
| 1464 | +} | ||
| 1465 | + | ||
| 1466 | +fn rgb_yuv(r: i32, g: i32, b: i32) -> (u8, u8, u8) { | ||
| 1467 | + let y = (((66 * r + 129 * g + 25 * b + 128) >> 8) + 16) as u8; | ||
| 1468 | + let u = (((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128) as u8; | ||
| 1469 | + let v = (((112 * r - 94 * g - 18 * b + 128) >> 8) + 128) as u8; | ||
| 1470 | + (y, u, v) | ||
| 1471 | +} | ||
| 1416 | 1472 | ||
| 1417 | /// How much sound one visualizer frame shows, centered on the frame's own | 1473 | /// How much sound one visualizer frame shows, centered on the frame's own |
| 1418 | -/// time: two seconds, so a glance reads the shape of the around-now rather | 1474 | +/// time. Two seconds by default; [`VIZ_FAST`] is a fifth of a second, the |
| 1419 | -/// than the whole file squeezed into one picture. | 1475 | +/// zoom that makes an Audacity-style trace readable as a line. |
| 1420 | const VIZ_WINDOW_SECS: f64 = 2.0; | 1476 | const VIZ_WINDOW_SECS: f64 = 2.0; |
| 1477 | +const VIZ_FAST_SECS: f64 = 0.2; | ||
| 1478 | + | ||
| 1479 | +fn viz_stamp( | ||
| 1480 | + y: &mut [u8], | ||
| 1481 | + u: &mut [u8], | ||
| 1482 | + v: &mut [u8], | ||
| 1483 | + w: usize, | ||
| 1484 | + h: usize, | ||
| 1485 | + cw: usize, | ||
| 1486 | + ch: usize, | ||
| 1487 | + x: i32, | ||
| 1488 | + row: i32, | ||
| 1489 | + fall: f32, | ||
| 1490 | + bg_y: u8, | ||
| 1491 | + ink_y: u8, | ||
| 1492 | + ink_u: u8, | ||
| 1493 | + ink_v: u8, | ||
| 1494 | +) { | ||
| 1495 | + if x < 0 || row < 0 || x >= w as i32 || row >= h as i32 { | ||
| 1496 | + return; | ||
| 1497 | + } | ||
| 1498 | + let lum = | ||
| 1499 | + (f32::from(bg_y) + (f32::from(ink_y) - f32::from(bg_y)) * fall.clamp(0.0, 1.0)) as u8; | ||
| 1500 | + let idx = row as usize * w + x as usize; | ||
| 1501 | + if lum > y[idx] { | ||
| 1502 | + y[idx] = lum; | ||
| 1503 | + } | ||
| 1504 | + if fall > 0.6 { | ||
| 1505 | + let cx = x as usize / 2; | ||
| 1506 | + let cy = row as usize / 2; | ||
| 1507 | + if cy < ch && cx < cw { | ||
| 1508 | + u[cy * cw + cx] = ink_u; | ||
| 1509 | + v[cy * cw + cx] = ink_v; | ||
| 1510 | + } | ||
| 1511 | + } | ||
| 1512 | +} | ||
| 1421 | 1513 | ||
| 1422 | -/// One visualizer picture, painted straight into tightly packed I420 in the | 1514 | +fn viz_line( |
| 1423 | -/// BT.601 limited range [`rgb_to_i420`] writes: dark (`16`, neutral chroma) | 1515 | + y: &mut [u8], |
| 1424 | -/// with the waveform bright (`220`), so what turns it into pixels on a screen | 1516 | + u: &mut [u8], |
| 1425 | -/// is the same conversion a still goes through. | 1517 | + v: &mut [u8], |
| 1426 | -/// | 1518 | + w: usize, |
| 1427 | -/// The columns are a **sliding** window of [`VIZ_WINDOW_SECS`] centered on | 1519 | + h: usize, |
| 1428 | -/// `t_secs`, mirrored around the vertical middle: the buckets' maxima reach | 1520 | + cw: usize, |
| 1429 | -/// up from the center line and their minima down, the standard symmetric | 1521 | + ch: usize, |
| 1430 | -/// envelope. `t_secs` outside the peaks -- and an empty envelope, silence | 1522 | + mut x0: i32, |
| 1431 | -/// included, whose buckets are `(0.0, 0.0)` -- paints the one-pixel center | 1523 | + mut y0: i32, |
| 1432 | -/// line: a visualizer frame always exists, whatever the sound did. | 1524 | + x1: i32, |
| 1525 | + y1: i32, | ||
| 1526 | + bg_y: u8, | ||
| 1527 | + ink_y: u8, | ||
| 1528 | + ink_u: u8, | ||
| 1529 | + ink_v: u8, | ||
| 1530 | +) { | ||
| 1531 | + let dx = (x1 - x0).abs(); | ||
| 1532 | + let dy = -(y1 - y0).abs(); | ||
| 1533 | + let sx = if x0 < x1 { 1 } else { -1 }; | ||
| 1534 | + let sy = if y0 < y1 { 1 } else { -1 }; | ||
| 1535 | + let mut err = dx + dy; | ||
| 1536 | + loop { | ||
| 1537 | + viz_stamp(y, u, v, w, h, cw, ch, x0, y0, 1.0, bg_y, ink_y, ink_u, ink_v); | ||
| 1538 | + viz_stamp(y, u, v, w, h, cw, ch, x0, y0 - 1, 0.45, bg_y, ink_y, ink_u, ink_v); | ||
| 1539 | + viz_stamp(y, u, v, w, h, cw, ch, x0, y0 + 1, 0.45, bg_y, ink_y, ink_u, ink_v); | ||
| 1540 | + if x0 == x1 && y0 == y1 { | ||
| 1541 | + break; | ||
| 1542 | + } | ||
| 1543 | + let e2 = 2 * err; | ||
| 1544 | + if e2 >= dy { | ||
| 1545 | + err += dy; | ||
| 1546 | + x0 += sx; | ||
| 1547 | + } | ||
| 1548 | + if e2 <= dx { | ||
| 1549 | + err += dx; | ||
| 1550 | + y0 += sy; | ||
| 1551 | + } | ||
| 1552 | + } | ||
| 1553 | +} | ||
| 1554 | + | ||
| 1555 | +/// One visualizer picture in tightly packed I420. Three looks, picked by | ||
| 1556 | +/// [`viz_style`]: a bipolar sample trace (Audacity), a filled min/max | ||
| 1557 | +/// envelope at pixel resolution, or a multi-strand ribbon. Ink is | ||
| 1558 | +/// [`viz_ink_rgb`]. A frame always exists; silence is the zero line. | ||
| 1433 | pub(crate) fn visualizer_i420( | 1559 | pub(crate) fn visualizer_i420( |
| 1434 | peaks: &[(f32, f32)], | 1560 | peaks: &[(f32, f32)], |
| 1435 | t_secs: f64, | 1561 | t_secs: f64, |
| @@ -1437,81 +1563,114 @@ pub(crate) fn visualizer_i420( | |||
| 1437 | height: u32, | 1563 | height: u32, |
| 1438 | flags: u8, | 1564 | flags: u8, |
| 1439 | ) -> (Vec<u8>, Vec<u8>, Vec<u8>) { | 1565 | ) -> (Vec<u8>, Vec<u8>, Vec<u8>) { |
| 1440 | - // Even dims, like every canvas this engine composes at: an odd caller is | ||
| 1441 | - // shaved rather than handed chroma math it did not ask about. | ||
| 1442 | let width = width & !1; | 1566 | let width = width & !1; |
| 1443 | let height = height & !1; | 1567 | let height = height & !1; |
| 1444 | let (w, h) = (width as usize, height as usize); | 1568 | let (w, h) = (width as usize, height as usize); |
| 1445 | let (cw, ch) = crate::scale::chroma_dims(w, h); | 1569 | let (cw, ch) = crate::scale::chroma_dims(w, h); |
| 1446 | - let mut y = vec![16u8; w * h]; | 1570 | + let (bg_y, bg_u, bg_v) = rgb_yuv(8, 12, 22); |
| 1447 | - let mut u = vec![128u8; cw * ch]; | 1571 | + let mut y = vec![bg_y; w * h]; |
| 1448 | - let mut v = vec![128u8; cw * ch]; | 1572 | + let mut u = vec![bg_u; cw * ch]; |
| 1449 | - let center = h / 2; | 1573 | + let mut v = vec![bg_v; cw * ch]; |
| 1450 | - let duration = peaks.len() as f64 / f64::from(VIZ_BUCKETS_PER_SEC); | 1574 | + let (ink_r, ink_g, ink_b) = viz_ink_rgb(viz_hue(flags)); |
| 1451 | - if peaks.is_empty() || !t_secs.is_finite() || !(0.0..=duration).contains(&t_secs) { | 1575 | + let (ink_y, ink_u, ink_v) = rgb_yuv(ink_r as i32, ink_g as i32, ink_b as i32); |
| 1452 | - for col in 0..w { | 1576 | + let center = h as f32 / 2.0; |
| 1453 | - y[center * w + col] = 220; | 1577 | + let half = h as f32 * 0.42; |
| 1454 | - } | 1578 | + let t_secs = if t_secs.is_finite() { t_secs } else { 0.0 }; |
| 1455 | - return (y, u, v); | 1579 | + |
| 1456 | - } | 1580 | + let z = (center.round() as isize).clamp(0, h as isize - 1) as usize; |
| 1457 | - let per_sec = f64::from(VIZ_BUCKETS_PER_SEC); | ||
| 1458 | - let window = if flags & VIZ_FAST != 0 { 0.4 } else { VIZ_WINDOW_SECS }; | ||
| 1459 | - // The window, clamped to the peaks that exist: a frame near either end | ||
| 1460 | - // looks past the file's edge, and draws the part of the window that is | ||
| 1461 | - // real rather than padding it out. | ||
| 1462 | - let first = (((t_secs - window / 2.0).max(0.0) * per_sec).floor() as usize) | ||
| 1463 | - .min(peaks.len() - 1); | ||
| 1464 | - let last = ((((t_secs + window / 2.0).min(duration) * per_sec).ceil() as usize) | ||
| 1465 | - .clamp(first + 1, peaks.len())) | ||
| 1466 | - .min(peaks.len()); | ||
| 1467 | - let span = (last - first) as f64; | ||
| 1468 | - let scale = center as f32; | ||
| 1469 | - let smooth = flags & VIZ_SMOOTH != 0; | ||
| 1470 | - let outline = flags & VIZ_OUTLINE != 0; | ||
| 1471 | for col in 0..w { | 1581 | for col in 0..w { |
| 1472 | - // The buckets this column draws: at least one, whatever the window's | 1582 | + y[z * w + col] = y[z * w + col].max(bg_y.saturating_add(18)); |
| 1473 | - // width is against the picture's -- a short clip stretches, a long | 1583 | + } |
| 1474 | - // one condenses, and neither leaves a column with nothing. | 1584 | + |
| 1475 | - let b0 = first + (col as f64 * span / w as f64).floor() as usize; | 1585 | + let mut lo = vec![0.0f32; w]; |
| 1476 | - let b1 = first | 1586 | + let mut hi = vec![0.0f32; w]; |
| 1477 | - + (((col + 1) as f64 * span / w as f64).ceil() as usize) | 1587 | + let mut sig = vec![0.0f32; w]; |
| 1478 | - .clamp(b0 - first + 1, last - first); | 1588 | + if !peaks.is_empty() { |
| 1479 | - let (s0, s1) = if smooth { | 1589 | + let duration = peaks.len() as f64 / f64::from(VIZ_BUCKETS_PER_SEC); |
| 1480 | - (b0.saturating_sub(1), (b1 + 1).min(peaks.len())) | 1590 | + let per_sec = f64::from(VIZ_BUCKETS_PER_SEC); |
| 1591 | + let window = if flags & VIZ_FAST != 0 { | ||
| 1592 | + VIZ_FAST_SECS | ||
| 1481 | } else { | 1593 | } else { |
| 1482 | - (b0, b1) | 1594 | + VIZ_WINDOW_SECS |
| 1483 | }; | 1595 | }; |
| 1484 | - let (mut lo, mut hi) = (f32::INFINITY, f32::NEG_INFINITY); | 1596 | + let first = (((t_secs - window / 2.0).max(0.0) * per_sec).floor() as usize) |
| 1485 | - for &(min, max) in &peaks[s0..s1] { | 1597 | + .min(peaks.len() - 1); |
| 1486 | - lo = lo.min(min); | 1598 | + let last = ((((t_secs + window / 2.0).min(duration) * per_sec).ceil() as usize) |
| 1487 | - hi = hi.max(max); | 1599 | + .clamp(first + 1, peaks.len())) |
| 1488 | - } | 1600 | + .min(peaks.len()); |
| 1489 | - // Mirrored around the center: maxima up, minima down, silence a line. | 1601 | + let span = (last - first) as f64; |
| 1490 | - let top = (scale - hi * scale).round(); | 1602 | + let pair = |i: usize| peaks[i.min(peaks.len() - 1)]; |
| 1491 | - let bottom = (scale - lo * scale).round(); | 1603 | + let signed = |i: usize| { |
| 1492 | - let top = (top as isize).clamp(0, h as isize - 1) as usize; | 1604 | + let (a, b) = pair(i); |
| 1493 | - let bottom = (bottom as isize).clamp(top as isize, h as isize - 1) as usize; | 1605 | + if b.abs() >= a.abs() { |
| 1494 | - if outline { | 1606 | + b |
| 1495 | - y[top * w + col] = 220; | 1607 | + } else { |
| 1496 | - y[bottom * w + col] = 220; | 1608 | + a |
| 1497 | - } else { | ||
| 1498 | - for row in top..=bottom { | ||
| 1499 | - y[row * w + col] = 220; | ||
| 1500 | } | 1609 | } |
| 1610 | + }; | ||
| 1611 | + for col in 0..w { | ||
| 1612 | + let pos = first as f64 + (col as f64 + 0.5) * span / w as f64; | ||
| 1613 | + let i = pos.floor() as usize; | ||
| 1614 | + let f = (pos - i as f64) as f32; | ||
| 1615 | + let (lo0, hi0) = pair(i); | ||
| 1616 | + let (lo1, hi1) = pair(i.saturating_add(1)); | ||
| 1617 | + lo[col] = (lo0 + (lo1 - lo0) * f).clamp(-1.0, 0.0); | ||
| 1618 | + hi[col] = (hi0 + (hi1 - hi0) * f).clamp(0.0, 1.0); | ||
| 1619 | + sig[col] = signed(i) + (signed(i.saturating_add(1)) - signed(i)) * f; | ||
| 1501 | } | 1620 | } |
| 1502 | } | 1621 | } |
| 1503 | - if flags & VIZ_TINT != 0 { | 1622 | + |
| 1504 | - // Cyan on the ink, black stays black: chroma follows luma so a | 1623 | + match viz_style(flags) { |
| 1505 | - // grade still reaches the bars around it. | 1624 | + VIZ_STYLE_RIBBON => { |
| 1506 | - for cy in 0..ch { | 1625 | + let tau = std::f64::consts::TAU; |
| 1507 | - for cx in 0..cw { | 1626 | + let travel = t_secs * 1.35; |
| 1508 | - let ink = y[cy * 2 * w + cx * 2] > 16 | 1627 | + for col in 0..w { |
| 1509 | - || y[cy * 2 * w + cx * 2 + 1] > 16 | 1628 | + let env = lo[col].abs().max(hi[col]).max(0.08); |
| 1510 | - || y[(cy * 2 + 1).min(h - 1) * w + cx * 2] > 16; | 1629 | + let x_phase = (col as f64 / w.max(1) as f64) * 2.15 * tau; |
| 1511 | - if ink { | 1630 | + for s in 0..16 { |
| 1512 | - u[cy * cw + cx] = 80; | 1631 | + let frac = s as f32 / 15.0; |
| 1513 | - v[cy * cw + cx] = 80; | 1632 | + let scale = 0.32 + 0.68 * frac; |
| 1633 | + let wave = (x_phase + travel + s as f64 * 0.07).sin() as f32; | ||
| 1634 | + let row = (center + half * env * scale * wave).round() as i32; | ||
| 1635 | + viz_stamp( | ||
| 1636 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row, 1.0, bg_y, ink_y, | ||
| 1637 | + ink_u, ink_v, | ||
| 1638 | + ); | ||
| 1639 | + viz_stamp( | ||
| 1640 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row - 1, 0.4, bg_y, | ||
| 1641 | + ink_y, ink_u, ink_v, | ||
| 1642 | + ); | ||
| 1643 | + viz_stamp( | ||
| 1644 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row + 1, 0.4, bg_y, | ||
| 1645 | + ink_y, ink_u, ink_v, | ||
| 1646 | + ); | ||
| 1647 | + } | ||
| 1648 | + } | ||
| 1649 | + } | ||
| 1650 | + VIZ_STYLE_FILL => { | ||
| 1651 | + for col in 0..w { | ||
| 1652 | + let top = (center - hi[col] * half).round() as i32; | ||
| 1653 | + let bot = (center - lo[col] * half).round() as i32; | ||
| 1654 | + viz_line( | ||
| 1655 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top, col as i32, bot, bg_y, | ||
| 1656 | + ink_y, ink_u, ink_v, | ||
| 1657 | + ); | ||
| 1658 | + } | ||
| 1659 | + } | ||
| 1660 | + _ => { | ||
| 1661 | + // Signed peak of each column, connected: the Audacity trace. | ||
| 1662 | + // Min/max bars are Fill; this is the sample path. | ||
| 1663 | + let mut prev: Option<(i32, i32)> = None; | ||
| 1664 | + for col in 0..w { | ||
| 1665 | + let y_pt = (center - sig[col] * half).round() as i32; | ||
| 1666 | + let x = col as i32; | ||
| 1667 | + if let Some((px, py)) = prev { | ||
| 1668 | + viz_line( | ||
| 1669 | + &mut y, &mut u, &mut v, w, h, cw, ch, px, py, x, y_pt, bg_y, ink_y, ink_u, | ||
| 1670 | + ink_v, | ||
| 1671 | + ); | ||
| 1514 | } | 1672 | } |
| 1673 | + prev = Some((x, y_pt)); | ||
| 1515 | } | 1674 | } |
| 1516 | } | 1675 | } |
| 1517 | } | 1676 | } |
| @@ -1833,6 +1992,48 @@ mod tests { | |||
| 1833 | while still holding the receiver that thread is waiting on" | 1992 | while still holding the receiver that thread is waiting on" |
| 1834 | ); | 1993 | ); |
| 1835 | } | 1994 | } |
| 1995 | + | ||
| 1996 | + #[test] | ||
| 1997 | + fn visualizer_paints_a_trace_not_a_filled_block() { | ||
| 1998 | + // 4000 buckets/sec * 2s window around t=1. | ||
| 1999 | + let mut peaks = vec![(0.0, 0.0); 12_000]; | ||
| 2000 | + for (i, p) in peaks.iter_mut().enumerate() { | ||
| 2001 | + let a = ((i as f32 / 9.0) * std::f32::consts::TAU).sin() * 0.8; | ||
| 2002 | + *p = (a.min(0.0), a.max(0.0)); | ||
| 2003 | + } | ||
| 2004 | + let wave = 1u8; | ||
| 2005 | + let (y, u, v) = visualizer_i420(&peaks, 1.0, 320, 180, wave); | ||
| 2006 | + let ink = y.iter().filter(|&&p| p > 40).count(); | ||
| 2007 | + assert!(ink > 80, "trace missing: {ink} ink pixels"); | ||
| 2008 | + assert!( | ||
| 2009 | + ink < y.len() / 4, | ||
| 2010 | + "filled block, not a trace: {ink}/{}", | ||
| 2011 | + y.len() | ||
| 2012 | + ); | ||
| 2013 | + let mut gold = 0usize; | ||
| 2014 | + for i in 0..u.len() { | ||
| 2015 | + if u[i] < 110 && v[i] > 135 { | ||
| 2016 | + gold += 1; | ||
| 2017 | + } | ||
| 2018 | + } | ||
| 2019 | + assert!(gold > 10, "default ink was not gold: {gold}"); | ||
| 2020 | + | ||
| 2021 | + let fill = with_viz_style(wave, VIZ_STYLE_FILL); | ||
| 2022 | + let (yf, _, _) = visualizer_i420(&peaks, 1.0, 320, 180, fill); | ||
| 2023 | + let fill_ink = yf.iter().filter(|&&p| p > 40).count(); | ||
| 2024 | + assert!(fill_ink > 80, "fill missing: {fill_ink}"); | ||
| 2025 | + | ||
| 2026 | + let hue = with_viz_hue(wave, 8); | ||
| 2027 | + let (_, u2, v2) = visualizer_i420(&peaks, 1.0, 64, 32, hue); | ||
| 2028 | + let mut shifted = 0usize; | ||
| 2029 | + for i in 0..u2.len() { | ||
| 2030 | + if u2[i] > 130 && v2[i] < 120 { | ||
| 2031 | + shifted += 1; | ||
| 2032 | + } | ||
| 2033 | + } | ||
| 2034 | + assert!(shifted > 2, "hue 8 did not move off gold"); | ||
| 2035 | + | ||
| 2036 | + } | ||
| 1836 | } | 2037 | } |
| 1837 | 2038 | ||
| 1838 | fn run( | 2039 | fn run( |