forked from wlkns-dev/stimmwerk
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:
246
src/voice/classify.rs
Normal file
246
src/voice/classify.rs
Normal file
@ -0,0 +1,246 @@
|
||||
//! Einordnung in einen Stimmtyp (Bass … Sopran) aus dem gemessenen Profil.
|
||||
//!
|
||||
//! Grundlage sind die Richtwerte der klassischen Stimmfächer. Verglichen wird die geschätzte Mitte der
|
||||
//! Stimme mit der typischen Mitte jedes Typs. Das ist eine Orientierung aus der Messung – das Stimmfach
|
||||
//! hängt auch von Klangfarbe und Registerübergängen ab und lässt sich nur mit Gehör sicher bestimmen.
|
||||
|
||||
use super::VoiceProfile;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum VoiceType {
|
||||
Bass,
|
||||
Bariton,
|
||||
Tenor,
|
||||
Alt,
|
||||
Mezzosopran,
|
||||
Sopran,
|
||||
}
|
||||
|
||||
impl VoiceType {
|
||||
pub const ALL: [VoiceType; 6] = [
|
||||
VoiceType::Bass,
|
||||
VoiceType::Bariton,
|
||||
VoiceType::Tenor,
|
||||
VoiceType::Alt,
|
||||
VoiceType::Mezzosopran,
|
||||
VoiceType::Sopran,
|
||||
];
|
||||
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
VoiceType::Bass => "Bass",
|
||||
VoiceType::Bariton => "Bariton",
|
||||
VoiceType::Tenor => "Tenor",
|
||||
VoiceType::Alt => "Alt",
|
||||
VoiceType::Mezzosopran => "Mezzosopran",
|
||||
VoiceType::Sopran => "Sopran",
|
||||
}
|
||||
}
|
||||
|
||||
/// Typischer Gesamtumfang (MIDI): Bass E2–E4, Bariton G2–G4, Tenor C3–C5, Alt F3–F5,
|
||||
/// Mezzosopran A3–A5, Sopran C4–C6.
|
||||
pub fn typical_range(self) -> (i32, i32) {
|
||||
match self {
|
||||
VoiceType::Bass => (40, 64),
|
||||
VoiceType::Bariton => (43, 67),
|
||||
VoiceType::Tenor => (48, 72),
|
||||
VoiceType::Alt => (53, 77),
|
||||
VoiceType::Mezzosopran => (57, 81),
|
||||
VoiceType::Sopran => (60, 84),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn center(self) -> f32 {
|
||||
let (lo, hi) = self.typical_range();
|
||||
(lo + hi) as f32 / 2.0
|
||||
}
|
||||
|
||||
/// Kurze Beschreibung in einfacher Sprache.
|
||||
pub fn description(self) -> &'static str {
|
||||
match self {
|
||||
VoiceType::Bass => "die tiefste Männerstimme, kräftig und dunkel",
|
||||
VoiceType::Bariton => "die mittlere Männerstimme, die häufigste – warm und vielseitig",
|
||||
VoiceType::Tenor => "die hohe Männerstimme, hell und strahlend",
|
||||
VoiceType::Alt => "die tiefe Frauenstimme, voll und samtig",
|
||||
VoiceType::Mezzosopran => "die mittlere Frauenstimme, warm mit guter Höhe",
|
||||
VoiceType::Sopran => "die hohe Frauenstimme, hell und leicht",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Confidence {
|
||||
Hoch,
|
||||
Mittel,
|
||||
Niedrig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Classification {
|
||||
pub best: VoiceType,
|
||||
/// Zweitbester Typ, wenn er fast genauso gut passt.
|
||||
pub runner_up: Option<VoiceType>,
|
||||
pub confidence: Confidence,
|
||||
/// Geschätzte Mitte der Stimme (MIDI).
|
||||
pub center: f32,
|
||||
pub explanation: String,
|
||||
}
|
||||
|
||||
/// Abstand zwischen Sprechlage und Stimmmitte (Halbtöne): die Sprechstimme liegt typischerweise etwa so weit
|
||||
/// unter der Mitte des Gesangsumfangs.
|
||||
const SPEECH_TO_CENTER: f32 = 10.0;
|
||||
|
||||
/// Schätzt die Mitte der Stimme aus sicherem Bereich (Gewicht 2), Umfang (1) und Sprechlage (1).
|
||||
fn estimate_center(p: &VoiceProfile) -> Option<(f32, &'static str)> {
|
||||
let mut sum = 0.0f32;
|
||||
let mut weight = 0.0f32;
|
||||
let mut basis = "Sprechlage";
|
||||
if let Some((lo, hi)) = p.safe_range {
|
||||
sum += 2.0 * (lo + hi) as f32 / 2.0;
|
||||
weight += 2.0;
|
||||
basis = "sicherer Bereich";
|
||||
}
|
||||
if let Some((lo, hi)) = p.range {
|
||||
sum += (lo + hi) / 2.0;
|
||||
weight += 1.0;
|
||||
if p.safe_range.is_none() {
|
||||
basis = "Stimmumfang";
|
||||
}
|
||||
}
|
||||
if let Some(s) = &p.speech {
|
||||
sum += s.median_midi + SPEECH_TO_CENTER;
|
||||
weight += 1.0;
|
||||
}
|
||||
(weight > 0.0).then(|| (sum / weight, basis))
|
||||
}
|
||||
|
||||
/// Ordnet das Profil dem nächstliegenden Stimmtyp zu; `None` ohne verwertbare Messung.
|
||||
pub fn classify(p: &VoiceProfile) -> Option<Classification> {
|
||||
let (center, basis) = estimate_center(p)?;
|
||||
let mut ranked: Vec<(VoiceType, f32)> = VoiceType::ALL
|
||||
.iter()
|
||||
.map(|&t| (t, (center - t.center()).abs()))
|
||||
.collect();
|
||||
ranked.sort_by(|a, b| a.1.total_cmp(&b.1));
|
||||
let (best, d1) = ranked[0];
|
||||
let (second, d2) = ranked[1];
|
||||
let margin = d2 - d1;
|
||||
// Nur die Sprechlage allein ist zu ungenau für mehr als „niedrig“.
|
||||
let only_speech = p.safe_range.is_none() && p.range.is_none();
|
||||
let confidence = if only_speech || margin < 1.0 {
|
||||
Confidence::Niedrig
|
||||
} else if margin < 2.0 {
|
||||
Confidence::Mittel
|
||||
} else {
|
||||
Confidence::Hoch
|
||||
};
|
||||
let (tlo, thi) = best.typical_range();
|
||||
let name = |m: i32| crate::pitch::note_name(m);
|
||||
let explanation = format!(
|
||||
"Die Mitte deiner Stimme liegt bei {} (aus {basis}); das passt am besten zum typischen {}-Bereich {}–{}.",
|
||||
name(center.round() as i32),
|
||||
best.name(),
|
||||
name(tlo),
|
||||
name(thi)
|
||||
);
|
||||
Some(Classification {
|
||||
best,
|
||||
runner_up: (margin < 2.0).then_some(second),
|
||||
confidence,
|
||||
center,
|
||||
explanation,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::voice::{SpeechResult, VoiceProfile};
|
||||
|
||||
fn profile(
|
||||
safe: Option<(i32, i32)>,
|
||||
range: Option<(f32, f32)>,
|
||||
speech: Option<f32>,
|
||||
) -> VoiceProfile {
|
||||
let mut p = VoiceProfile::assemble("x".into(), "y".into(), None, None, None, vec![], None);
|
||||
p.safe_range = safe;
|
||||
p.range = range;
|
||||
p.speech = speech.map(|m| SpeechResult {
|
||||
median_midi: m,
|
||||
lo_midi: m - 2.0,
|
||||
hi_midi: m + 2.0,
|
||||
voiced_s: 20.0,
|
||||
});
|
||||
p
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typical_centers_map_to_their_own_type() {
|
||||
for t in VoiceType::ALL {
|
||||
let (lo, hi) = t.typical_range();
|
||||
// Sicherer Bereich etwas enger als der Umfang, Sprechlage passend darunter.
|
||||
let p = profile(
|
||||
Some((lo + 3, hi - 3)),
|
||||
Some((lo as f32, hi as f32)),
|
||||
Some(t.center() - SPEECH_TO_CENTER),
|
||||
);
|
||||
let c = classify(&p).unwrap();
|
||||
assert_eq!(c.best, t, "{t:?} → {c:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn male_and_female_examples() {
|
||||
// Männerstimme mit Sprechlage ~ A2 (45), sicherem Bereich C3–E4 → Bariton/Tenor-Grenze, Bariton.
|
||||
let bar = classify(&profile(Some((48, 64)), Some((41.0, 70.0)), Some(45.0))).unwrap();
|
||||
assert!(
|
||||
matches!(bar.best, VoiceType::Bariton | VoiceType::Tenor),
|
||||
"{bar:?}"
|
||||
);
|
||||
// Hohe Frauenstimme.
|
||||
let sop = classify(&profile(Some((62, 81)), Some((59.0, 84.0)), Some(60.0))).unwrap();
|
||||
assert_eq!(sop.best, VoiceType::Sopran, "{sop:?}");
|
||||
// Tiefe Männerstimme.
|
||||
let bass = classify(&profile(Some((40, 57)), Some((38.0, 62.0)), Some(40.0))).unwrap();
|
||||
assert_eq!(bass.best, VoiceType::Bass, "{bass:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn borderline_cases_name_a_runner_up_and_low_confidence() {
|
||||
// Mitte genau zwischen Bariton (55) und Tenor (60).
|
||||
let c = classify(&profile(Some((50, 65)), None, None)).unwrap();
|
||||
assert_eq!(c.confidence, Confidence::Niedrig, "{c:?}");
|
||||
assert!(c.runner_up.is_some());
|
||||
assert!(matches!(
|
||||
(c.best, c.runner_up),
|
||||
(VoiceType::Bariton, Some(VoiceType::Tenor))
|
||||
| (VoiceType::Tenor, Some(VoiceType::Bariton))
|
||||
));
|
||||
// Eindeutig → keine Nennung eines Zweiten.
|
||||
let clear = classify(&profile(Some((42, 66)), Some((40.0, 68.0)), Some(44.0))).unwrap();
|
||||
assert!(clear.runner_up.is_none() || clear.confidence != Confidence::Hoch);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn speech_only_is_never_high_confidence_and_empty_gives_none() {
|
||||
let c = classify(&profile(None, None, Some(45.0))).unwrap();
|
||||
assert_eq!(c.confidence, Confidence::Niedrig);
|
||||
assert!(classify(&profile(None, None, None)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explanation_names_the_type_and_ranges() {
|
||||
let c = classify(&profile(Some((43, 67)), Some((41.0, 70.0)), Some(45.0))).unwrap();
|
||||
assert!(c.explanation.contains(c.best.name()), "{}", c.explanation);
|
||||
assert!(
|
||||
c.explanation.contains("sicherer Bereich"),
|
||||
"{}",
|
||||
c.explanation
|
||||
);
|
||||
assert!(
|
||||
c.explanation.contains("G2") || c.explanation.contains("C3"),
|
||||
"{}",
|
||||
c.explanation
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
//! Stimmfinder: Messung von Sprechlage, Umfang und Tessitura, Profil und Nutzung für Transponierung.
|
||||
|
||||
pub mod analysis;
|
||||
pub mod classify;
|
||||
pub mod sim;
|
||||
mod transpose;
|
||||
|
||||
|
||||
@ -18,7 +18,6 @@ const HOLD_PERIOD_S: f64 = 2.9;
|
||||
|
||||
impl SimStep {
|
||||
/// Dauer der simulierten Aufnahme in Sekunden.
|
||||
#[cfg(test)]
|
||||
pub fn duration_s(self) -> f64 {
|
||||
match self {
|
||||
SimStep::Speech => 30.0,
|
||||
|
||||
Reference in New Issue
Block a user