Passagen, Übungsmodus, Stimmfinder-Überarbeitung, Gerätedesign, Metronom und Akkorde

- Passagenwahl, Original-Kurve, Spulen/Üben und Pause
- Startseite, Klartext-Oberfläche, Stimmfinder mit Stimmtyp-Vorschlag
- Gerätedesign (Regler, Schalter, LED) und Metronom (frei/songsynchron, Tap)
- Akkorderkennung aus der Begleitung, Akkordleiste und -karte
- zweiter Song als Chart

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 13:38:27 +01:00
parent a5fdab8b49
commit 4112f88e54
29 changed files with 6367 additions and 622 deletions

View File

@ -1,6 +1,6 @@
//! Piano-Roll-Ansicht: scrollende Tonhöhenkurve. Abbildung und Segmentierung sind rein und getestet.
use crate::chart::{NoteChart, NoteKind};
use crate::chart::{ChordSpan, NoteChart, NoteKind};
use crate::config::UiConfig;
use crate::pitch::{HistoryPoint, note_name};
use crate::score::ChartScore;
@ -62,6 +62,216 @@ pub fn segments<'a>(points: impl Iterator<Item = &'a HistoryPoint>) -> Vec<Vec<C
out
}
/// Kurvenstücke des Original-Gesangs für das Zeitfenster [t_from, t_to] (Sekunden), um `shift` Halbtöne
/// verschoben. Mit `fold_to` werden Oktavfehler der Erkennung auf die Oktave der Zielnote gefaltet.
pub fn original_segments(
frames: &[(f64, Option<f32>)],
t_from: f64,
t_to: f64,
shift: f32,
fold_to: Option<&NoteChart>,
) -> Vec<Vec<CurvePoint>> {
let lo = frames.partition_point(|f| f.0 < t_from);
let hi = frames.partition_point(|f| f.0 <= t_to);
let mut out: Vec<Vec<CurvePoint>> = Vec::new();
let mut open = false;
for &(t, m) in &frames[lo..hi.max(lo)] {
let Some(mut midi) = m else {
open = false;
continue;
};
midi += shift;
if let Some(c) = fold_to
&& let Some(i) = c.note_at(t * 1000.0)
{
midi = fold_to_target(midi, c.notes[i].midi_pitch);
}
let p = CurvePoint {
t,
midi,
cents: 0.0,
};
if open {
out.last_mut().expect("offenes Segment").push(p);
} else {
out.push(vec![p]);
open = true;
}
}
out
}
/// Eine Tonhöhen-Karte (Ziel, Original oder Du): großer Notenname, Zusatzzeilen und Cent-Anzeige.
pub struct PitchCard {
pub title: String,
pub color: Color32,
/// Kontinuierliche MIDI-Zahl; `None` = kein Ton (Karte gedämpft).
pub midi: Option<f32>,
/// Ersetzt den großen Notennamen durch einen anderen Text (z. B. Status).
pub big: Option<String>,
pub lines: Vec<(String, Color32)>,
/// Abweichung von der nächsten Halbtonstufe in Cent für die Anzeige unten.
pub meter: Option<f32>,
/// Pegel 0..=1 als LED-Kette (statt der Cent-Anzeige), z. B. für das Mikrofon.
pub level: Option<f32>,
}
/// Feste Höhe der Karten: die Anordnung darunter bleibt stabil, egal was angezeigt wird.
pub const CARD_H: f32 = 88.0;
/// Zeichnet die Karten nebeneinander als Geräte-Displays (dunkles Glas, leuchtende Schrift, LED-Anzeige).
pub fn draw_pitch_cards(ui: &mut egui::Ui, cards: &[PitchCard]) {
use super::gear;
// Etwas schmaler als die Fläche: die Bildlaufleiste schneidet sonst die rechte Karte an.
let (rect, _) = ui.allocate_exact_size(
egui::vec2(ui.available_width() - 14.0, CARD_H),
egui::Sense::hover(),
);
let painter = ui.painter_at(rect);
let gap = 8.0;
let w = (rect.width() - gap * (cards.len() as f32 - 1.0)) / cards.len() as f32;
for (i, c) in cards.iter().enumerate() {
let r = Rect::from_min_size(
pos2(rect.left() + i as f32 * (w + gap), rect.top()),
egui::vec2(w, CARD_H),
);
let active = c.midi.is_some();
// Gehäuse mit Rand, darin das dunkle Display.
painter.rect_filled(r, 7.0, gear::PLATE_BG);
painter.rect_stroke(
r,
7.0,
Stroke::new(1.5, gear::PLATE_EDGE),
egui::StrokeKind::Inside,
);
let lcd = r.shrink(5.0);
painter.rect_filled(lcd, 4.0, Color32::from_rgb(10, 13, 11));
painter.rect_stroke(
lcd,
4.0,
Stroke::new(
1.0,
if active {
c.color.gamma_multiply(0.45)
} else {
Color32::from_rgb(34, 38, 35)
},
),
egui::StrokeKind::Inside,
);
painter.text(
pos2(lcd.left() + 9.0, lcd.top() + 10.0),
Align2::LEFT_CENTER,
c.title.to_uppercase(),
FontId::proportional(11.5),
c.color.gamma_multiply(if active { 0.95 } else { 0.7 }),
);
let letter = c.big.clone().unwrap_or_else(|| {
c.midi
.map_or("–".to_string(), |m| note_name(m.round() as i32))
});
let glow = if active {
c.color
} else {
Color32::from_gray(70)
};
let letter_pos = pos2(lcd.left() + 9.0, lcd.top() + 38.0);
// Kurze große Texte (Notenname, Akkord) groß; lange Statustexte kleiner.
let font = FontId::monospace(if c.big.as_ref().is_some_and(|b| b.chars().count() > 4) {
20.0
} else {
38.0
});
// Schein hinter der Schrift.
painter.text(
letter_pos + egui::vec2(0.0, 0.0),
Align2::LEFT_CENTER,
&letter,
font.clone(),
glow.gamma_multiply(0.35),
);
painter.text(
letter_pos,
Align2::LEFT_CENTER,
letter,
font,
if active {
Color32::WHITE.gamma_multiply(0.92)
} else {
glow
},
);
for (k, (text, color)) in c.lines.iter().take(2).enumerate() {
if c.big.as_ref().is_some_and(|b| b.chars().count() > 4) {
// Statuskarten: Zusatzzeile rechts oben, damit sie den langen Statustext nicht überdeckt.
painter.text(
pos2(lcd.right() - 9.0, lcd.top() + 10.0 + k as f32 * 14.0),
Align2::RIGHT_CENTER,
text,
FontId::monospace(11.5),
*color,
);
} else {
painter.text(
pos2(
lcd.left() + lcd.width() * 0.46,
lcd.top() + 28.0 + k as f32 * 16.0,
),
Align2::LEFT_CENTER,
text,
FontId::monospace(12.5),
*color,
);
}
}
// Anzeige unten: LED-Pegelkette oder Cent-Skala (−50 … +50, Mitte = genau auf dem Ton).
let (ml, mw) = (lcd.left() + 9.0, lcd.width() - 18.0);
let my = lcd.bottom() - 10.0;
if let Some(level) = c.level {
let n = 28;
let lit = gear::lit_segments(level, n);
let step = mw / n as f32;
for k in 0..n {
gear::led(
&painter,
pos2(ml + (k as f32 + 0.5) * step, my),
k < lit,
gear::segment_color(k, n),
(step * 0.32).clamp(1.8, 3.6),
);
}
} else {
const N: usize = 21; // alle 5 Cent eine LED
let active_idx = c.meter.filter(|_| active).map(|cents| {
(((cents.clamp(-50.0, 50.0) + 50.0) / 5.0).round() as usize).min(N - 1)
});
let step = mw / N as f32;
for k in 0..N {
let cents = -50.0 + k as f32 * 5.0;
let color = if cents.abs() <= 15.0 {
gear::LED_GREEN
} else if cents.abs() <= 35.0 {
gear::LED_YELLOW
} else {
gear::LED_RED
};
let cx = ml + (k as f32 + 0.5) * step;
gear::led(
&painter,
pos2(cx, my),
Some(k) == active_idx,
color,
if k == N / 2 { 3.8 } else { 2.8 },
);
}
}
}
}
/// Farben im Chart-Modus: Original-Gesang und Gesang des Nutzers.
pub const ORIGINAL_COLOR: Color32 = Color32::from_rgb(80, 200, 235);
pub const USER_COLOR: Color32 = Color32::from_rgb(245, 110, 200);
/// Gewünschte Mitte des Ausschnitts: unverändert, solange das Ziel im Innenbereich liegt.
pub fn follow(center: f32, target: f32, half_range: f32, margin: f32) -> f32 {
let inner = half_range - margin;
@ -82,6 +292,11 @@ pub fn fold_to_target(midi: f32, target: i32) -> f32 {
/// Zusatzdaten für den Chart-Modus.
pub struct ChartOverlay<'a> {
pub chart: &'a NoteChart,
/// Kurve des Original-Gesangs (leer = ausgeblendet).
pub original: &'a [Vec<CurvePoint>],
/// Akkordleiste (leer = ausgeblendet) und ob mit Been geschrieben wird.
pub chords: &'a [ChordSpan],
pub chord_flats: bool,
pub now_ms: f64,
pub result: Option<&'a ChartScore>,
}
@ -169,7 +384,36 @@ pub fn draw(
}
if let Some(o) = overlay {
draw_chord_lane(&painter, vp, o);
draw_notes(&painter, vp, o);
for seg in o.original {
for w in seg.windows(2) {
painter.line_segment(
[
pos2(vp.x(w[0].t), vp.y(w[0].midi)),
pos2(vp.x(w[1].t), vp.y(w[1].midi)),
],
Stroke::new(2.5, ORIGINAL_COLOR.gamma_multiply(0.9)),
);
}
}
// Legende.
let lx = vp.rect.right() - 150.0;
for (i, (color, label)) in [(ORIGINAL_COLOR, "Original"), (USER_COLOR, "Du")]
.into_iter()
.enumerate()
{
let lane = if o.chords.is_empty() { 0.0 } else { 26.0 };
let y = vp.rect.top() + 12.0 + lane + i as f32 * 16.0;
painter.line_segment([pos2(lx, y), pos2(lx + 22.0, y)], Stroke::new(3.0, color));
painter.text(
pos2(lx + 28.0, y),
Align2::LEFT_CENTER,
label,
FontId::proportional(12.0),
Color32::from_gray(210),
);
}
// „Jetzt“-Linie.
let x = vp.x(o.now_ms / 1000.0);
painter.line_segment(
@ -194,7 +438,14 @@ pub fn draw(
let (a, b) = (&w[0], &w[1]);
painter.line_segment(
[pos2(vp.x(a.t), vp.y(a.midi)), pos2(vp.x(b.t), vp.y(b.midi))],
Stroke::new(3.0, cents_color(b.cents, cfg)),
Stroke::new(
3.0,
if overlay.is_some() {
USER_COLOR
} else {
cents_color(b.cents, cfg)
},
),
);
}
}
@ -204,7 +455,11 @@ pub fn draw(
painter.circle_filled(
pos2(vp.x(last.t), vp.y(last.midi)),
6.0,
cents_color(last.cents, cfg),
if overlay.is_some() {
USER_COLOR
} else {
cents_color(last.cents, cfg)
},
);
}
}
@ -215,6 +470,49 @@ fn score_color(total: f32) -> Color32 {
Color32::from_rgb((220.0 - 130.0 * t) as u8, (70.0 + 140.0 * t) as u8, 90)
}
/// Akkordleiste am oberen Rand: ein Feld je Akkord, der gerade klingende ist hervorgehoben.
fn draw_chord_lane(painter: &egui::Painter, vp: &Viewport, o: &ChartOverlay<'_>) {
const H: f32 = 22.0;
let (t_left, t_right) = (vp.t_now + vp.lead_s - vp.window_s, vp.t_now + vp.lead_s);
for c in o.chords {
let (a, b) = (c.start_ms / 1000.0, c.end_ms / 1000.0);
if b < t_left || a > t_right {
continue;
}
let (x0, x1) = (vp.x(a).max(vp.rect.left()), vp.x(b).min(vp.rect.right()));
if x1 - x0 < 2.0 {
continue;
}
let minor = c.name.ends_with('m');
let active = o.now_ms >= c.start_ms && o.now_ms < c.end_ms;
let fill = if active {
Color32::from_rgb(255, 176, 48)
} else if minor {
Color32::from_rgb(78, 62, 96)
} else {
Color32::from_rgb(58, 70, 92)
};
let r = Rect::from_min_max(
pos2(x0 + 1.0, vp.rect.top() + 2.0),
pos2(x1 - 1.0, vp.rect.top() + H),
);
painter.rect_filled(r, 4.0, fill);
if r.width() > 22.0 {
painter.text(
pos2(r.left() + 6.0, r.center().y),
Align2::LEFT_CENTER,
crate::import::chords::display_chord(&c.name, o.chord_flats),
FontId::proportional(14.0),
if active {
Color32::from_rgb(34, 22, 4)
} else {
Color32::WHITE
},
);
}
}
}
/// Zielbalken mit Silben; nach der Wertung nach Notenscore eingefärbt.
fn draw_notes(painter: &egui::Painter, vp: &Viewport, o: &ChartOverlay<'_>) {
for (i, n) in o.chart.notes.iter().enumerate() {
@ -249,6 +547,16 @@ fn draw_notes(painter: &egui::Painter, vp: &Viewport, o: &ChartOverlay<'_>) {
egui::StrokeKind::Outside,
);
}
// Notenname im Balken, wenn Platz ist.
if rect.width() >= 26.0 && rect.height() >= 11.0 {
painter.text(
rect.center(),
Align2::CENTER_CENTER,
note_name(n.midi_pitch),
FontId::monospace((rect.height() * 0.62).clamp(9.0, 13.0)),
Color32::from_white_alpha(if active { 255 } else { 190 }),
);
}
}
}
@ -374,6 +682,39 @@ mod tests {
assert_eq!(follow(60.0, 40.0, 12.0, 3.0), 49.0);
}
#[test]
fn original_segments_window_shift_fold_and_gaps() {
let frames: Vec<(f64, Option<f32>)> = (0..100)
.map(|i| {
(
i as f64 * 0.01,
if (40..50).contains(&i) {
None
} else {
Some(48.0)
},
)
})
.collect();
// Fenster 0,2–0,7 s: Lücke bei 0,40–0,50 teilt in zwei Stücke.
let s = original_segments(&frames, 0.2, 0.7, 0.0, None);
assert_eq!(s.len(), 2, "{s:?}");
assert!((s[0][0].t - 0.2).abs() < 1e-9 && s[1].last().unwrap().t <= 0.7 + 1e-9);
// Verschiebung.
assert!((original_segments(&frames, 0.0, 0.1, 3.0, None)[0][0].midi - 51.0).abs() < 1e-6);
// Oktavfaltung auf die Zielnote (60): 48 → 60.
let c = NoteChart::from_toml_str(
"title=\"t\"\n[[notes]]\nstart_ms=0\ndauer_ms=1000\nmidi_pitch=60",
None,
)
.unwrap();
let f = original_segments(&frames, 0.0, 0.3, 0.0, Some(&c));
assert!((f[0][0].midi - 60.0).abs() < 1e-6);
// Außerhalb der Daten: leer.
assert!(original_segments(&frames, 5.0, 6.0, 0.0, None).is_empty());
assert!(original_segments(&[], 0.0, 1.0, 0.0, None).is_empty());
}
#[test]
fn black_keys() {
assert!(is_black_key(61) && is_black_key(70) && is_black_key(-2));