diff --git a/src/visualizations/lyrics_in_motion.rhai b/src/visualizations/lyrics_in_motion.rhai
index 45d177c..95fc15e 100644
--- a/src/visualizations/lyrics_in_motion.rhai
+++ b/src/visualizations/lyrics_in_motion.rhai
@@ -1,9 +1,9 @@
// name: Lyrics in motion
// protocol: furumi-visualizer-v3
-// bundle-version: 1
+// bundle-version: 2
//
-// Incoming and outgoing text are driven by playback timestamps. Rhai caches
-// the two dot-matrix rasters across frames and only draws sparse accents.
+// Playback timestamps drive the entrance and highlight. The active glyphs
+// are cached across frames and rebuilt when the terminal changes size.
fn clamp(value, low, high) {
if value < low { low } else if value > high { high } else { value }
@@ -32,43 +32,68 @@ fn center(text, dx, y, w, h, fg) {
this.text_at(text, (w - cell_width(text)) / 2 + dx, y, w, h, fg);
}
-fn verse(text, glyph, dx, y, w, h, fg) {
- if glyph.len() == 2 {
- this.center(glyph[0], dx, y, w, h, fg);
- this.center(glyph[1], dx, y + 1, w, h, fg);
- } else {
- this.center(text, dx, y, w, h, fg);
+fn stage(input, w, h, tint, arrival) {
+ if w < 20 || h < 12 { return; }
+ let band = max(1, h / 40);
+ for y in range(3, h - 3, band) {
+ let distance = abs(to_float(2 * y - h)) / to_float(h);
+ let level = 0.04 + pow(1.0 - distance, 3.0) * (0.12 + input.bass * 0.12 + arrival * 0.08);
+ this.push(#{ op: "rect", x: 0, y: y, w: w, h: min(band, h - 3 - y), bg: glow(tint, level) });
+ }
+ let stars = [];
+ for i in 0..64 {
+ let angle = to_float(i) * 2.399963 + input.time * 0.09;
+ let radius = 0.48 + 0.46 * abs(sin(to_float(i) * 8.17 + input.time * 0.24));
+ let x = w + to_int(cos(angle) * to_float(w - 2) * radius);
+ let y = h * 2 + to_int(sin(angle) * to_float(h * 2 - 12) * radius);
+ if abs(2 * x - 2 * w) > w || abs(y - h * 2) > h {
+ stars.push(x); stars.push(y);
+ }
+ }
+ this.push(#{ op: "dots", points: stars, fg: glow(tint, 0.48 + input.treble * 0.40), bg: glow(tint, 0.09) });
+
+ let samples = min(24, w / 4);
+ for i in 0..samples {
+ let wave = 0.5 + 0.5 * sin(input.time * 5.0 + to_float(i) * 0.67);
+ let rise = min(max(1, h / 5 - 3), 1 + to_int(wave * (2.0 + 4.0 * input.bass + 3.0 * input.beat)));
+ let x = 2 + i * (w / 2 - 3) / samples;
+ let fg = glow(tint, 0.27 + wave * 0.25 + input.energy * 0.26);
+ this.push(#{ op: "vline", x: x, y: h - 3 - rise, h: rise, ch: "▌", fg: fg, bg: 0x080b18 });
+ this.push(#{ op: "vline", x: w - 1 - x, y: h - 3 - rise, h: rise, ch: "▐", fg: fg, bg: 0x080b18 });
}
}
-fn atmosphere(input, w, h, phase, tint) {
- if w < 20 || h < 8 { return; }
- let points = [];
- let stride = if w < 65 { 3 } else { 2 };
- let cx = w;
- let cy = h * 2;
- for i in 0..22 {
- let base = to_float(i) * 2.399963 + input.time * (0.13 + input.treble * 0.08);
- let spread = 0.66 + 0.26 * sin(to_float(i) * 18.67 + phase * 1.8);
- let x = cx + to_int(cos(base) * to_float(w - 6) * spread);
- let y = cy + to_int(sin(base) * to_float(h * 2 - 4) * spread);
- if abs(to_float(y - cy)) > 8.0 || abs(to_float(x - cx)) > to_float(w / 2) {
- points.push(x); points.push(y);
- }
+fn phrase(glyph, slot, total, mode, p, progress, y, w, h, tint, beat) {
+ let drift = pow(1.0 - p, 3.0);
+ let side = if slot % 2 == 0 { -1 } else { 1 };
+ let height = glyph.len();
+ let headline = height >= 10 || total == 1;
+ for i in 0..height {
+ let row = glyph[i];
+ if row == "" { continue; }
+ let width = cell_width(row);
+ let space = w - width;
+ let anchor = if total == 1 { space / 2 }
+ else if (slot + mode) % 2 == 0 { space / 5 }
+ else { space * 4 / 5 };
+ let dx = if mode == 0 { side * to_int(drift * to_float(min(22, w / 5))) }
+ else if mode == 2 { to_int(drift * to_float(i * 2 - height)) }
+ else { 0 };
+ let dy = if mode == 1 { to_int(drift * to_float(min(5, h / 6))) } else { 0 };
+ let x = anchor + dx;
+ let brightness = if headline { 1.0 } else { 0.72 };
+ let shade = brightness * (1.0 - 0.27 * to_float(i) / to_float(height));
+ // Reveal from opposing edges in the third entrance.
+ let shown = if mode == 2 { max(1, to_int(to_float(width) * (1.0 - drift))) } else { width };
+ let start = if mode == 2 && side > 0 { width - shown } else { 0 };
+ let visible = cut_cells(row, start, shown);
+ let lift = y + i + dy;
+ this.text_at(visible, x + start + 1, lift, w, h, glow(tint, 0.40 + beat * 0.22));
+ this.text_at(visible, x + start, lift, w, h, glow(tint, shade * (0.76 + 0.24 * p)));
+ let sweep = to_int(to_float(width) * progress);
+ this.text_at(cut_cells(row, start, max(0, min(shown, sweep - start))), x + start, lift,
+ w, h, glow(0xfff6ff, shade + 0.20));
}
- this.push(#{ op: "dots", points: points, fg: glow(tint, 0.25 + input.beat * 0.28), bg: 0x080b18 });
-
- let rails = [];
- for i in 0..18 {
- let x = i * stride;
- if x + 4 < w {
- let wave = sin(input.time * 3.0 + to_float(i) * 0.76);
- let rise = to_int((1.0 + wave) * (2.0 + 6.0 * input.bass));
- rails.push(4 + x); rails.push(h * 4 - 8 - rise);
- rails.push(w * 2 - 5 - x); rails.push(h * 4 - 8 - rise);
- }
- }
- this.push(#{ op: "dots", points: rails, fg: glow(tint, 0.34 + input.energy * 0.30), bg: 0x080b18 });
}
fn render(input) {
@@ -81,65 +106,73 @@ fn render(input) {
active: "", previous: "", next: "", index: -1, elapsed: 0.0, progress: 0.0, until_next: -1.0 }
};
let st = if type_of(input.state) == "map" { input.state } else { #{} };
- let tint = if l.index % 2 == 0 { 0xa8baff } else { 0xffb795 };
- let y = h / 2 - 1;
- let p = clamp(l.elapsed / 0.48, 0.0, 1.0);
- commands.atmosphere(input, w, h, p, tint);
+ let tint = if l.index % 2 == 0 { 0x93c5ff } else { 0xffa5da };
+ let p = clamp(l.elapsed / 0.40, 0.0, 1.0);
+ let arrival = if l.index >= 0 { pow(1.0 - p, 3.0) } else { 0.0 };
+ commands.stage(input, w, h, tint, arrival);
if h > 7 {
- commands.center(input.track_title, 0, 1, w, h, glow(tint, 0.62));
- if input.show_clock && w >= 24 {
- commands.text_at(input.clock, w - 10, 1, w, h, glow(tint, 0.55));
+ let title_width = if input.show_clock && w >= 32 { w - 24 } else { w };
+ let title = if cell_width(input.track_title) <= title_width { input.track_title }
+ else { cut_cells(input.track_title, 0, title_width - 1) + "…" };
+ commands.center(title, 0, 1, w, h, glow(tint, 0.78));
+ if input.show_clock && w >= 32 {
+ commands.text_at(input.clock, w - 10, 1, w, h, glow(tint, 0.72));
}
}
+
if l.index >= 0 {
- let font_width = if h >= 10 { w - 6 } else { 0 };
- if !("active" in st) || st.active != l.active || st.width != font_width {
+ let font_width = w - 8;
+ let font_height = if h >= 12 { min(32, h * 3 / 5) } else { 0 };
+ if !("active" in st) || st.active != l.active || st.width != font_width || st.height != font_height {
st.active = l.active;
st.width = font_width;
- st.glyph = matrix_text(l.active, font_width);
+ st.height = font_height;
+ st.phrases = lyric_layout(l.active, font_width, font_height);
}
- if p < 1.0 && l.previous != "" {
- if !("previous" in st) || st.previous != l.previous || st.prev_width != font_width {
- st.previous = l.previous;
- st.prev_width = font_width;
- st.prev_glyph = matrix_text(l.previous, font_width);
- }
- let exit = p * p;
- let side = if l.index % 2 == 0 { 1 } else { -1 };
- commands.verse(l.previous, st.prev_glyph, to_int(exit * to_float(w / 2)) * side,
- y - 2 - to_int(exit * to_float(h / 2)), w, h, glow(tint, (1.0 - p) * 0.55));
+ let total = st.phrases.len();
+ let rows = max(1, total - 1);
+ if total > 0 {
+ rows = total - 1;
+ for glyph in st.phrases { rows += glyph.len(); }
+ }
+ let top = (h - rows) / 2;
+ let y = top;
+ if total == 0 {
+ commands.center(l.active, 0, y, w, h, 0xf6f3ff);
+ }
+ let mode = l.index % 3;
+ let duration = if l.until_next >= 0.0 { l.elapsed + l.until_next } else { 2.0 };
+ let entrance = clamp(duration * 0.28, 0.08, 0.60);
+ for slot in 0..total {
+ let glyph = st.phrases[slot];
+ let phase = clamp(l.elapsed / entrance * 1.5 - 0.5 * to_float(slot), 0.0, 1.0);
+ let progress = clamp(l.progress * to_float(total) - to_float(slot), 0.0, 1.0);
+ commands.phrase(glyph, slot, total, mode, phase, progress, y, w, h, tint, input.beat);
+ y += glyph.len() + 1;
+ }
+ if h >= 12 {
+ let exit = 1.0 - arrival;
+ let side = if mode == 1 { -1 } else { 1 };
+ commands.center(l.previous, side * to_int(exit * to_float(w / 12)),
+ top - 2, w, h, glow(tint, 0.32 + arrival * 0.38));
+ commands.center(l.next, 0, min(h - 3, top + rows + 3), w, h, glow(tint, 0.66));
}
-
- // Alternate diagonal entries, a horizontal slide and a cut.
- let remaining = pow(1.0 - p, 3.0);
- let mode = l.index % 5;
- let dx = if mode == 0 || mode == 2 { -to_int(remaining * to_float(w / 3)) }
- else if mode == 1 || mode == 4 { to_int(remaining * to_float(w / 3)) }
- else { 0 };
- let dy = if mode == 0 { -to_int(remaining * to_float(h / 3)) }
- else if mode == 1 { to_int(remaining * to_float(h / 3)) }
- else if mode == 4 { -to_int(remaining * to_float(h / 4)) }
- else { 0 };
- let intensity = if mode == 3 { 1.0 } else { 0.72 + p * 0.28 };
- commands.verse(l.active, st.glyph, dx, y + dy, w, h, glow(tint, intensity));
} else {
let message = if l.instrumental { "Instrumental" }
else if l.timed { "Waiting for the first line" }
else if l.available { "Plain lyrics · l to read" }
else if l.loading { "Looking for lyrics…" }
else { "No lyrics found" };
- commands.center(message, 0, y, w, h, glow(tint, 0.72));
+ commands.center(message, 0, h / 2, w, h, glow(tint, 0.82));
}
- if h >= 10 {
- if l.next != "" { commands.center(l.next, 0, y + 4, w, h, glow(tint, 0.38)); }
- if l.index >= 0 && l.until_next >= 0.0 && w >= 14 {
- let count = clamp(to_int(to_float(w - 8) * l.progress), 0, w - 8);
- commands.push(#{ op: "hline", x: 4, y: h - 2, w: w - 8, ch: "─", fg: 0x26304b, bg: 0x080b18 });
- if count > 0 {
- commands.push(#{ op: "hline", x: 4, y: h - 2, w: count, ch: "━", fg: glow(tint, 0.70), bg: 0x080b18 });
- }
+ if h >= 10 && l.index >= 0 && l.until_next >= 0.0 && w >= 14 {
+ let count = clamp(to_int(to_float(w - 8) * l.progress), 0, w - 8);
+ commands.push(#{ op: "hline", x: 4, y: h - 2, w: w - 8, ch: "─", fg: 0x394568, bg: 0x080b18 });
+ if count > 0 {
+ commands.push(#{ op: "hline", x: 4, y: h - 2, w: count, ch: "━", fg: tint, bg: 0x080b18 });
+ commands.text_at("◆", min(w - 5, 4 + count), h - 2, w, h, 0xfff6ff);
}
}
#{ commands: commands, state: st }
diff --git a/src/visualizer.rs b/src/visualizer.rs
index 4eeabac..3a2c5db 100644
--- a/src/visualizer.rs
+++ b/src/visualizer.rs
@@ -66,7 +66,7 @@ const BUNDLED_SCRIPTS: &[BundledScript] = &[
id: "lyrics_in_motion",
file_name: "lyrics_in_motion.rhai",
display_name: "Lyrics in motion",
- version: 1,
+ version: 2,
source: include_str!("visualizations/lyrics_in_motion.rhai"),
},
];
@@ -701,6 +701,7 @@ impl Default for ScriptRuntime {
engine.register_fn("abs", |value: FLOAT| value.abs());
engine.register_fn("pow", |value: FLOAT, power: FLOAT| value.powf(power));
engine.register_fn("matrix_text", matrix::matrix_text);
+ engine.register_fn("lyric_layout", matrix::lyric_layout);
engine.register_fn("cut_cells", matrix::cut_cells);
engine.register_fn("cell_width", |text: &str| {
unicode_width::UnicodeWidthStr::width(text) as i64
@@ -1566,13 +1567,17 @@ mod tests {
.unwrap();
let mut runtime = ScriptRuntime::default();
let mut state = AppState::default();
- let version =
- crate::lyrics::manual_version("Timed", "[00:01]FIRST\n[00:05]SECOND\n[00:09]歌詞")
- .unwrap();
+ let version = crate::lyrics::manual_version(
+ "Timed",
+ "[00:01]FIRST\n[00:05]SECOND\n[00:09]歌詞\n[00:13]Снова над городом встаёт солнце",
+ )
+ .unwrap();
state.lyrics.document = crate::lyrics::Document::new(&version);
state.lyrics.versions.push(version);
- let mut render = |position| {
+ let mut render = |position, width, height| {
let mut input = test_input();
+ insert_int(&mut input, "width", width);
+ insert_int(&mut input, "height", height);
input.insert("lyrics".into(), lyrics_input(&state, position).into());
runtime.render(file.path(), input).unwrap()
};
@@ -1581,26 +1586,54 @@ mod tests {
.iter()
.find_map(|command| match command {
DrawCommand::Text { x, y, text, .. }
- if text.chars().any(|c| ('\u{2801}'..='\u{28ff}').contains(&c)) =>
+ if text.chars().any(|c| ('▀'..='▟').contains(&c)) =>
{
Some((*x, *y))
}
_ => None,
})
- .expect("Braille lyric spans")
+ .expect("block lyric spans")
};
- assert_ne!(matrix_start(&render(1.05)), matrix_start(&render(1.75)));
+ assert_ne!(
+ matrix_start(&render(1.05, 80, 24)),
+ matrix_start(&render(1.75, 80, 24))
+ );
assert!(
- render(5.1)
+ render(5.1, 80, 24)
.iter()
.any(|command| matches!(command, DrawCommand::Dots { .. }))
);
assert!(
- render(10.0)
+ render(10.0, 80, 24)
.iter()
.any(|command| matches!(command, DrawCommand::Text { text, .. } if text == "歌詞"))
);
- assert_ne!(matrix_start(&render(2.0)), (0, 0));
+ let wide = render(14.0, 180, 45);
+ let block_widths: std::collections::BTreeSet<_> = wide
+ .iter()
+ .filter_map(|command| match command {
+ DrawCommand::Text { text, .. }
+ if text.chars().any(|c| ('▀'..='▟').contains(&c)) =>
+ {
+ Some(text.chars().count())
+ }
+ _ => None,
+ })
+ .collect();
+ assert!(
+ block_widths.contains(&63),
+ "expected a large Cyrillic headline"
+ );
+ assert!(
+ block_widths.contains(&27),
+ "expected smaller supporting text"
+ );
+ assert!(
+ render(14.0, 34, 8)
+ .iter()
+ .any(|command| matches!(command, DrawCommand::Text { text, .. }
+ if text.starts_with("Снова над городом")))
+ );
}
#[test]
diff --git a/src/visualizer/matrix.rs b/src/visualizer/matrix.rs
index 9330e79..0fd8cf7 100644
--- a/src/visualizer/matrix.rs
+++ b/src/visualizer/matrix.rs
@@ -36,6 +36,129 @@ pub(super) fn matrix_text(text: &str, width: i64) -> Array {
vec![Dynamic::from(top), Dynamic::from(bottom)]
}
+/// Fit the largest block glyphs that leave room for the surrounding UI.
+pub(super) fn lyric_layout(text: &str, width: i64, height: i64) -> Array {
+ let words: Vec<_> = text.split_whitespace().collect();
+ if words.len() >= 3 && width >= 60 && height >= 16 {
+ // Pick a substantial word that fits the headline scale. Keep reading order.
+ let focus = words
+ .iter()
+ .enumerate()
+ .filter(|(_, word)| word.chars().count() as i64 * 9 <= width - 8)
+ .max_by_key(|(index, word)| (word.chars().count(), usize::MAX - index));
+ if let Some((index, word)) = focus {
+ let headline = lyric_blocks(word, width - 8, 11, 3);
+ let before = words[..index].join(" ");
+ let after = words[index + 1..].join(" ");
+ let supporting = |phrase: &str| lyric_blocks(phrase, width - 8, 9, 1);
+ let above = supporting(&before);
+ let below = supporting(&after);
+ let fits = !headline.is_empty()
+ && (before.is_empty() || !above.is_empty())
+ && (after.is_empty() || !below.is_empty())
+ && (headline.len() + above.len() + below.len() + 2) as i64 <= height;
+ if fits {
+ return [above, headline, below]
+ .into_iter()
+ .filter(|rows| !rows.is_empty())
+ .map(Dynamic::from)
+ .collect();
+ }
+ }
+ }
+ let rows = lyric_blocks(text, width, height, 3);
+ if rows.is_empty() {
+ Vec::new()
+ } else {
+ vec![Dynamic::from(rows)]
+ }
+}
+
+fn lyric_blocks(text: &str, width: i64, height: i64, max_scale: usize) -> Array {
+ let width = width.clamp(0, 512) as usize;
+ let height = height.clamp(0, 32) as usize;
+ let Some(words) = text
+ .split_whitespace()
+ .map(|word| {
+ word.chars()
+ .map(|ch| {
+ let mut uppercase = ch.to_uppercase();
+ let first = uppercase.next()?;
+ if uppercase.next().is_some() {
+ return None;
+ }
+ glyph(first)
+ })
+ .collect::