forked from wlkns-dev/stimmwerk
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:
204
src/pitch/track.rs
Normal file
204
src/pitch/track.rs
Normal file
@ -0,0 +1,204 @@
|
||||
//! Glättung (Median), Voiced/Unvoiced-Entscheidung und Oktavsprung-Schutz.
|
||||
|
||||
use super::note::{cents_from_midi, midi_from_hz};
|
||||
use super::yin::RawPitch;
|
||||
use crate::config::PitchConfig;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct PitchEstimate {
|
||||
pub freq_hz: f32,
|
||||
/// Kontinuierliche MIDI-Zahl (A4 = 69.0).
|
||||
pub midi: f32,
|
||||
/// Nächste Halbtonstufe.
|
||||
pub note: i32,
|
||||
/// Abweichung von `note` in Cent (-50..=50).
|
||||
pub cents: f32,
|
||||
pub confidence: f32,
|
||||
/// Überbrückter Aussetzer: der Wert stammt aus dem Verlauf, der aktuelle Frame war stimmlos.
|
||||
pub held: bool,
|
||||
}
|
||||
|
||||
impl PitchEstimate {
|
||||
/// Baut einen Schätzwert aus einer kontinuierlichen MIDI-Zahl.
|
||||
pub fn from_midi(midi: f32, confidence: f32) -> Self {
|
||||
Self {
|
||||
freq_hz: 440.0 * 2f32.powf((midi - 69.0) / 12.0),
|
||||
midi,
|
||||
note: midi.round() as i32,
|
||||
cents: cents_from_midi(midi),
|
||||
confidence,
|
||||
held: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Tracker {
|
||||
cfg: PitchConfig,
|
||||
history: VecDeque<f32>,
|
||||
unvoiced_run: usize,
|
||||
/// Anzahl aufeinanderfolgender Frames mit gleichem, noch unbestätigtem Oktavsprung (Vorzeichen, Zähler).
|
||||
pending_jump: Option<(f32, usize)>,
|
||||
last_conf: f32,
|
||||
}
|
||||
|
||||
impl Tracker {
|
||||
pub fn new(cfg: PitchConfig) -> Self {
|
||||
Self {
|
||||
cfg,
|
||||
history: VecDeque::new(),
|
||||
unvoiced_run: 0,
|
||||
pending_jump: None,
|
||||
last_conf: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, raw: Option<RawPitch>) -> Option<PitchEstimate> {
|
||||
match raw.filter(|r| r.confidence >= self.cfg.min_confidence) {
|
||||
None => {
|
||||
self.unvoiced_run += 1;
|
||||
self.pending_jump = None;
|
||||
if self.unvoiced_run > self.cfg.release_frames {
|
||||
self.history.clear();
|
||||
}
|
||||
// Kurze Aussetzer überbrücken, damit die Anzeige nicht flackert.
|
||||
return self.smoothed();
|
||||
}
|
||||
Some(r) => {
|
||||
self.unvoiced_run = 0;
|
||||
self.last_conf = r.confidence;
|
||||
self.accept(midi_from_hz(r.freq_hz));
|
||||
}
|
||||
}
|
||||
self.smoothed()
|
||||
}
|
||||
|
||||
fn accept(&mut self, midi: f32) {
|
||||
if let Some(reference) = self.median() {
|
||||
let d = midi - reference;
|
||||
let octaves = (d / 12.0).round();
|
||||
let is_octave =
|
||||
octaves != 0.0 && (d - octaves * 12.0).abs() <= self.cfg.octave_tolerance_semitones;
|
||||
if is_octave {
|
||||
let sign = octaves.signum();
|
||||
let count = match self.pending_jump {
|
||||
Some((s, n)) if s == sign => n + 1,
|
||||
_ => 1,
|
||||
};
|
||||
if count < self.cfg.octave_confirm_frames {
|
||||
self.pending_jump = Some((sign, count));
|
||||
return; // Ausreißer verwerfen, alter Wert bleibt
|
||||
}
|
||||
// Sprung bestätigt: neu beginnen.
|
||||
self.history.clear();
|
||||
}
|
||||
}
|
||||
self.pending_jump = None;
|
||||
self.history.push_back(midi);
|
||||
while self.history.len() > self.cfg.median_len.max(1) {
|
||||
self.history.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
fn median(&self) -> Option<f32> {
|
||||
if self.history.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut v: Vec<f32> = self.history.iter().copied().collect();
|
||||
v.sort_by(f32::total_cmp);
|
||||
Some(v[v.len() / 2])
|
||||
}
|
||||
|
||||
fn smoothed(&self) -> Option<PitchEstimate> {
|
||||
// Während eines längeren stimmlosen Abschnitts gibt es nichts anzuzeigen.
|
||||
if self.unvoiced_run > self.cfg.release_frames {
|
||||
return None;
|
||||
}
|
||||
let midi = self.median()?;
|
||||
Some(PitchEstimate {
|
||||
held: self.unvoiced_run > 0,
|
||||
..PitchEstimate::from_midi(midi, self.last_conf)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn raw(hz: f32) -> Option<RawPitch> {
|
||||
Some(RawPitch {
|
||||
freq_hz: hz,
|
||||
confidence: 0.95,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_octave_outlier_is_suppressed() {
|
||||
let mut t = Tracker::new(PitchConfig::default());
|
||||
for _ in 0..5 {
|
||||
t.push(raw(220.0));
|
||||
}
|
||||
let e = t.push(raw(440.0)).unwrap();
|
||||
assert!((e.freq_hz - 220.0).abs() < 0.5, "{e:?}");
|
||||
let e = t.push(raw(110.0)).unwrap();
|
||||
assert!((e.freq_hz - 220.0).abs() < 0.5, "{e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sustained_octave_change_is_accepted() {
|
||||
let mut t = Tracker::new(PitchConfig::default());
|
||||
for _ in 0..5 {
|
||||
t.push(raw(220.0));
|
||||
}
|
||||
let mut last = None;
|
||||
for _ in 0..PitchConfig::default().octave_confirm_frames {
|
||||
last = t.push(raw(440.0));
|
||||
}
|
||||
assert!((last.unwrap().freq_hz - 440.0).abs() < 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normal_interval_is_not_delayed() {
|
||||
let mut t = Tracker::new(PitchConfig::default());
|
||||
for _ in 0..5 {
|
||||
t.push(raw(220.0));
|
||||
}
|
||||
// Quinte aufwärts: kein Oktavsprung, Median folgt nach wenigen Frames.
|
||||
let mut e = None;
|
||||
for _ in 0..3 {
|
||||
e = t.push(raw(330.0));
|
||||
}
|
||||
assert!((e.unwrap().freq_hz - 330.0).abs() < 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bridged_gaps_are_marked_held() {
|
||||
let mut t = Tracker::new(PitchConfig::default());
|
||||
assert!(!t.push(raw(220.0)).unwrap().held);
|
||||
let weak = Some(RawPitch {
|
||||
freq_hz: 500.0,
|
||||
confidence: 0.3,
|
||||
});
|
||||
assert!(t.push(weak).unwrap().held);
|
||||
assert!(!t.push(raw(220.0)).unwrap().held);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn low_confidence_is_unvoiced_and_releases() {
|
||||
let cfg = PitchConfig::default();
|
||||
let mut t = Tracker::new(cfg.clone());
|
||||
for _ in 0..5 {
|
||||
t.push(raw(220.0));
|
||||
}
|
||||
let weak = Some(RawPitch {
|
||||
freq_hz: 500.0,
|
||||
confidence: 0.3,
|
||||
});
|
||||
assert!(t.push(weak).is_some(), "kurzer Aussetzer wird überbrückt");
|
||||
for _ in 0..=cfg.release_frames {
|
||||
t.push(weak);
|
||||
}
|
||||
assert!(t.push(weak).is_none());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user