Singtrainer: Phasen 0-6, Stimmfinder und Latenzkalibrierung
Rust-Desktop-App (egui/eframe, cpal) zum Singüben. - Audio: Geräteauswahl, Pegelanzeige, Mixer ohne Locks im Callback (Backing, Original-Gesang, Führungston, Aufnahme, Klick), Dekodierung mit symphonia, Resampling mit rubato, Mikroaufnahme als WAV - Pitch: YIN mit Konfidenz, Median-Glättung, Oktavsprung-Schutz - Live-Ansicht: scrollende Tonhöhenkurve, Referenzton - Chart: NoteChart (TOML), Scoring (Tonhöhe oktavtolerant, Timing, Stabilität), Session mit Countdown, Testchart - Aufnahme und Review: Session-Datei (JSON) mit Mikrospur, Verlauf, Wiedergabe solo oder gemischt - Stimmfinder: Sprechlage, Legato, Sirene, Haltetöne, Tessitura, Profil-Verlauf, Transponierung und Warnung für Charts - Latenzkalibrierung: Klickzug, Einsatzerkennung, Offset in der Config - Simulationsmodus (--sim) und Tests ohne Audiogerät Manuelle Abnahmen am Gerät stehen noch aus (siehe README). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
382
src/ui/roll.rs
Normal file
382
src/ui/roll.rs
Normal file
@ -0,0 +1,382 @@
|
||||
//! Piano-Roll-Ansicht: scrollende Tonhöhenkurve. Abbildung und Segmentierung sind rein und getestet.
|
||||
|
||||
use crate::chart::{NoteChart, NoteKind};
|
||||
use crate::config::UiConfig;
|
||||
use crate::pitch::{HistoryPoint, note_name};
|
||||
use crate::score::ChartScore;
|
||||
use eframe::egui::{self, Align2, Color32, FontId, Rect, Stroke, pos2};
|
||||
|
||||
/// Abbildung Zeit/Tonhöhe → Bildschirm. Der rechte Rand liegt `lead_s` hinter „jetzt“
|
||||
/// (0 = Tuner, > 0 = Vorschau auf kommende Noten im Chart-Modus).
|
||||
pub struct Viewport {
|
||||
pub rect: Rect,
|
||||
pub t_now: f64,
|
||||
pub window_s: f64,
|
||||
pub lead_s: f64,
|
||||
/// Tonhöhe (MIDI, kontinuierlich) am unteren und oberen Rand.
|
||||
pub lo: f32,
|
||||
pub hi: f32,
|
||||
}
|
||||
|
||||
impl Viewport {
|
||||
pub fn x(&self, t: f64) -> f32 {
|
||||
let frac = ((t - (self.t_now + self.lead_s - self.window_s)) / self.window_s) as f32;
|
||||
self.rect.left() + frac * self.rect.width()
|
||||
}
|
||||
|
||||
pub fn y(&self, midi: f32) -> f32 {
|
||||
let frac = (midi - self.lo) / (self.hi - self.lo);
|
||||
self.rect.bottom() - frac * self.rect.height()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct CurvePoint {
|
||||
pub t: f64,
|
||||
pub midi: f32,
|
||||
pub cents: f32,
|
||||
}
|
||||
|
||||
/// Teilt den Verlauf an stimmlosen Stellen in zusammenhängende Kurvenstücke.
|
||||
pub fn segments<'a>(points: impl Iterator<Item = &'a HistoryPoint>) -> Vec<Vec<CurvePoint>> {
|
||||
let mut out: Vec<Vec<CurvePoint>> = Vec::new();
|
||||
let mut open = false;
|
||||
for p in points {
|
||||
match p.est {
|
||||
Some(e) => {
|
||||
let cp = CurvePoint {
|
||||
t: p.t,
|
||||
midi: e.midi,
|
||||
cents: e.cents,
|
||||
};
|
||||
if open {
|
||||
out.last_mut().expect("offenes Segment").push(cp);
|
||||
} else {
|
||||
out.push(vec![cp]);
|
||||
open = true;
|
||||
}
|
||||
}
|
||||
None => open = false,
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// 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;
|
||||
if target > center + inner {
|
||||
target - inner
|
||||
} else if target < center - inner {
|
||||
target + inner
|
||||
} else {
|
||||
center
|
||||
}
|
||||
}
|
||||
|
||||
/// Verschiebt `midi` um ganze Oktaven in die Nähe von `target` (für die oktavtolerante Anzeige).
|
||||
pub fn fold_to_target(midi: f32, target: i32) -> f32 {
|
||||
midi + 12.0 * ((target as f32 - midi) / 12.0).round()
|
||||
}
|
||||
|
||||
/// Zusatzdaten für den Chart-Modus.
|
||||
pub struct ChartOverlay<'a> {
|
||||
pub chart: &'a NoteChart,
|
||||
pub now_ms: f64,
|
||||
pub result: Option<&'a ChartScore>,
|
||||
}
|
||||
|
||||
pub fn is_black_key(note: i32) -> bool {
|
||||
matches!(note.rem_euclid(12), 1 | 3 | 6 | 8 | 10)
|
||||
}
|
||||
|
||||
fn cents_color(cents: f32, cfg: &UiConfig) -> Color32 {
|
||||
let c = cents.abs();
|
||||
if c <= cfg.good_cents {
|
||||
Color32::from_rgb(90, 210, 120)
|
||||
} else if c <= cfg.ok_cents {
|
||||
Color32::from_rgb(235, 200, 70)
|
||||
} else {
|
||||
Color32::from_rgb(240, 130, 60)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(
|
||||
ui: &egui::Ui,
|
||||
vp: &Viewport,
|
||||
segs: &[Vec<CurvePoint>],
|
||||
reference: Option<i32>,
|
||||
cfg: &UiConfig,
|
||||
overlay: Option<&ChartOverlay<'_>>,
|
||||
) {
|
||||
let painter = ui.painter_at(vp.rect);
|
||||
painter.rect_filled(vp.rect, 4.0, Color32::from_rgb(24, 26, 32));
|
||||
|
||||
// Halbtonzeilen, zentriert auf ganzzahlige MIDI-Werte.
|
||||
let row_h = vp.y(0.0) - vp.y(1.0);
|
||||
for n in (vp.lo - 0.5).ceil() as i32..=(vp.hi + 0.5).floor() as i32 {
|
||||
let band = Rect::from_min_max(
|
||||
pos2(vp.rect.left(), vp.y(n as f32 + 0.5)),
|
||||
pos2(vp.rect.right(), vp.y(n as f32 - 0.5)),
|
||||
);
|
||||
let fill = if is_black_key(n) {
|
||||
Color32::from_rgb(18, 19, 24)
|
||||
} else {
|
||||
Color32::from_rgb(30, 33, 41)
|
||||
};
|
||||
painter.rect_filled(band, 0.0, fill);
|
||||
if n.rem_euclid(12) == 0 {
|
||||
let y = vp.y(n as f32 - 0.5);
|
||||
painter.line_segment(
|
||||
[pos2(vp.rect.left(), y), pos2(vp.rect.right(), y)],
|
||||
Stroke::new(1.0, Color32::from_gray(70)),
|
||||
);
|
||||
}
|
||||
if row_h >= 11.0 {
|
||||
painter.text(
|
||||
pos2(vp.rect.left() + 6.0, vp.y(n as f32)),
|
||||
Align2::LEFT_CENTER,
|
||||
note_name(n),
|
||||
FontId::monospace((row_h * 0.7).min(13.0)),
|
||||
Color32::from_gray(if is_black_key(n) { 110 } else { 150 }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Sekundenlinien.
|
||||
let first = (vp.t_now - vp.window_s).ceil() as i64;
|
||||
for s in first..=vp.t_now.floor() as i64 {
|
||||
let x = vp.x(s as f64);
|
||||
painter.line_segment(
|
||||
[pos2(x, vp.rect.top()), pos2(x, vp.rect.bottom())],
|
||||
Stroke::new(1.0, Color32::from_white_alpha(14)),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(n) = reference {
|
||||
let y = vp.y(n as f32);
|
||||
painter.line_segment(
|
||||
[pos2(vp.rect.left() + 40.0, y), pos2(vp.rect.right(), y)],
|
||||
Stroke::new(2.0, Color32::from_rgb(90, 150, 255)),
|
||||
);
|
||||
painter.text(
|
||||
pos2(vp.rect.right() - 6.0, y - 3.0),
|
||||
Align2::RIGHT_BOTTOM,
|
||||
format!("Ref {}", note_name(n)),
|
||||
FontId::proportional(12.0),
|
||||
Color32::from_rgb(130, 175, 255),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(o) = overlay {
|
||||
draw_notes(&painter, vp, o);
|
||||
// „Jetzt“-Linie.
|
||||
let x = vp.x(o.now_ms / 1000.0);
|
||||
painter.line_segment(
|
||||
[pos2(x, vp.rect.top()), pos2(x, vp.rect.bottom())],
|
||||
Stroke::new(1.5, Color32::from_white_alpha(70)),
|
||||
);
|
||||
}
|
||||
|
||||
// Aktuelle Zielnote hervorheben und Kurve zeichnen.
|
||||
if overlay.is_none()
|
||||
&& let Some(last) = segs.last().and_then(|s| s.last())
|
||||
{
|
||||
let n = last.midi.round();
|
||||
let band = Rect::from_min_max(
|
||||
pos2(vp.rect.left(), vp.y(n + 0.5)),
|
||||
pos2(vp.rect.right(), vp.y(n - 0.5)),
|
||||
);
|
||||
painter.rect_filled(band, 0.0, Color32::from_white_alpha(14));
|
||||
}
|
||||
for seg in segs {
|
||||
for w in seg.windows(2) {
|
||||
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)),
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(last) = segs.last().and_then(|s| s.last()) {
|
||||
// Markierung nur, wenn der letzte Punkt noch „frisch“ ist.
|
||||
if (vp.t_now - last.t).abs() < 0.1 {
|
||||
painter.circle_filled(
|
||||
pos2(vp.x(last.t), vp.y(last.midi)),
|
||||
6.0,
|
||||
cents_color(last.cents, cfg),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn score_color(total: f32) -> Color32 {
|
||||
let t = total.clamp(0.0, 1.0);
|
||||
Color32::from_rgb((220.0 - 130.0 * t) as u8, (70.0 + 140.0 * t) as u8, 90)
|
||||
}
|
||||
|
||||
/// 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() {
|
||||
let (x0, x1) = (vp.x(n.start_ms / 1000.0), vp.x(n.end_ms() / 1000.0));
|
||||
if x1 < vp.rect.left() || x0 > vp.rect.right() {
|
||||
continue;
|
||||
}
|
||||
let rect = Rect::from_min_max(
|
||||
pos2(x0 + 1.0, vp.y(n.midi_pitch as f32 + 0.42)),
|
||||
pos2(x1 - 1.0, vp.y(n.midi_pitch as f32 - 0.42)),
|
||||
);
|
||||
let base = match n.typ {
|
||||
NoteKind::Normal => Color32::from_rgb(70, 110, 190),
|
||||
NoteKind::Golden => Color32::from_rgb(210, 170, 50),
|
||||
NoteKind::Frei => Color32::from_rgb(140, 90, 180),
|
||||
};
|
||||
let fill = match o.result.and_then(|r| r.notes.get(i)) {
|
||||
Some(s) => score_color(s.total),
|
||||
None => base,
|
||||
};
|
||||
let active = o.now_ms >= n.start_ms && o.now_ms < n.end_ms();
|
||||
painter.rect_filled(
|
||||
rect,
|
||||
5.0,
|
||||
fill.gamma_multiply(if active { 1.0 } else { 0.75 }),
|
||||
);
|
||||
if active {
|
||||
painter.rect_stroke(
|
||||
rect,
|
||||
5.0,
|
||||
Stroke::new(2.0, Color32::WHITE),
|
||||
egui::StrokeKind::Outside,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Textzeile am unteren Rand: gesungene Silben gedämpft, aktuelle hervorgehoben.
|
||||
pub fn draw_lyrics(ui: &egui::Ui, rect: Rect, chart: &NoteChart, now_ms: f64) {
|
||||
let painter = ui.painter_at(rect);
|
||||
let line = chart.lyric_line_at(now_ms);
|
||||
let active = chart.note_at(now_ms);
|
||||
let font = FontId::proportional(22.0);
|
||||
let parts: Vec<(usize, String)> = line
|
||||
.clone()
|
||||
.filter_map(|i| {
|
||||
let syl = chart.notes[i].silbe.as_deref()?;
|
||||
let joined = match syl.strip_suffix('-') {
|
||||
Some(stem) => stem.to_string(),
|
||||
None => format!("{syl} "),
|
||||
};
|
||||
Some((i, joined))
|
||||
})
|
||||
.collect();
|
||||
let galleys: Vec<_> = parts
|
||||
.iter()
|
||||
.map(|(i, text)| {
|
||||
let color = if Some(*i) == active {
|
||||
Color32::from_rgb(255, 215, 80)
|
||||
} else if chart.notes[*i].end_ms() <= now_ms {
|
||||
Color32::from_rgb(120, 200, 140)
|
||||
} else {
|
||||
Color32::from_gray(225)
|
||||
};
|
||||
painter.layout_no_wrap(text.clone(), font.clone(), color)
|
||||
})
|
||||
.collect();
|
||||
let total: f32 = galleys.iter().map(|g| g.size().x).sum();
|
||||
let mut x = rect.center().x - total / 2.0;
|
||||
for g in galleys {
|
||||
let w = g.size().x;
|
||||
painter.galley(
|
||||
pos2(x, rect.center().y - g.size().y / 2.0),
|
||||
g,
|
||||
Color32::WHITE,
|
||||
);
|
||||
x += w;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::pitch::PitchEstimate;
|
||||
|
||||
fn vp() -> Viewport {
|
||||
Viewport {
|
||||
rect: Rect::from_min_max(pos2(100.0, 50.0), pos2(900.0, 650.0)),
|
||||
t_now: 20.0,
|
||||
window_s: 8.0,
|
||||
lead_s: 0.0,
|
||||
lo: 48.0,
|
||||
hi: 72.0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn time_mapping() {
|
||||
let v = vp();
|
||||
assert!((v.x(20.0) - 900.0).abs() < 1e-3);
|
||||
assert!((v.x(12.0) - 100.0).abs() < 1e-3);
|
||||
assert!((v.x(16.0) - 500.0).abs() < 1e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lead_shifts_now_leftwards() {
|
||||
let v = Viewport {
|
||||
lead_s: 6.0,
|
||||
..vp()
|
||||
};
|
||||
// „Jetzt“ bei 25 % der Breite, Zukunft rechts davon.
|
||||
assert!((v.x(20.0) - 300.0).abs() < 1e-3);
|
||||
assert!((v.x(26.0) - 900.0).abs() < 1e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn octave_folding() {
|
||||
assert!((fold_to_target(48.3, 60) - 60.3).abs() < 1e-4);
|
||||
assert!((fold_to_target(75.0, 60) - 63.0).abs() < 1e-4);
|
||||
assert!((fold_to_target(60.4, 60) - 60.4).abs() < 1e-4);
|
||||
assert!(
|
||||
(fold_to_target(65.9, 60) - 65.9).abs() < 1e-4,
|
||||
"knapp unter Halboktave bleibt"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pitch_mapping() {
|
||||
let v = vp();
|
||||
assert!((v.y(72.0) - 50.0).abs() < 1e-3);
|
||||
assert!((v.y(48.0) - 650.0).abs() < 1e-3);
|
||||
assert!((v.y(60.0) - 350.0).abs() < 1e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn segments_split_at_unvoiced() {
|
||||
let e = |m| Some(PitchEstimate::from_midi(m, 0.9));
|
||||
let pts: Vec<HistoryPoint> = [e(60.0), e(60.2), None, None, e(62.0), None, e(64.0)]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(i, est)| HistoryPoint {
|
||||
t: i as f64 * 0.01,
|
||||
est,
|
||||
})
|
||||
.collect();
|
||||
let s = segments(pts.iter());
|
||||
assert_eq!(s.iter().map(Vec::len).collect::<Vec<_>>(), vec![2, 1, 1]);
|
||||
assert!((s[0][1].cents - 20.0).abs() < 0.1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn follow_keeps_center_inside_margin() {
|
||||
// Bereich 24 → half 12, Rand 3 → innen ±9.
|
||||
assert_eq!(follow(60.0, 65.0, 12.0, 3.0), 60.0);
|
||||
assert_eq!(follow(60.0, 69.0, 12.0, 3.0), 60.0);
|
||||
assert_eq!(follow(60.0, 73.0, 12.0, 3.0), 64.0);
|
||||
assert_eq!(follow(60.0, 40.0, 12.0, 3.0), 49.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn black_keys() {
|
||||
assert!(is_black_key(61) && is_black_key(70) && is_black_key(-2));
|
||||
assert!(!is_black_key(60) && !is_black_key(64));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user