Song-Analyse: eigenständiges Werkzeug und Lese-Schnittstelle in Rust
- tools/analyze + tools/song_analysis: Trennung in Stems, Takte, Akkorde, Tonhöhe und Text mit Wortzeiten (eigene Python-3.11-Umgebung, README, Tests) - schemas/song-analysis-v1: JSON-Schemas der Ausgabe - src/lib.rs, src/song_analysis.rs: liest die Ausgabe (Version 1); Integrationstest in tests/ - .gitignore: Umgebung, Modelle und Zwischenspeicher des Werkzeugs Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2
src/lib.rs
Normal file
2
src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
||||
//! Stable file contract for external song-analysis tools.
|
||||
pub mod song_analysis;
|
||||
130
src/song_analysis.rs
Normal file
130
src/song_analysis.rs
Normal file
@ -0,0 +1,130 @@
|
||||
//! Version 1 of `schemas/song-analysis-v1/`, with seconds relative to the normalized mix.
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Meta {
|
||||
pub title: String,
|
||||
pub bpm: Option<f64>,
|
||||
pub key: Option<String>,
|
||||
pub duration: f64,
|
||||
pub schema_version: u32,
|
||||
pub errors: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Pitch {
|
||||
pub hop_ms: u32,
|
||||
pub hz: Vec<f64>,
|
||||
pub conf: Vec<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Chord {
|
||||
pub chord: String,
|
||||
pub start: f64,
|
||||
pub end: f64,
|
||||
pub conf: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Beats {
|
||||
pub beats: Vec<f64>,
|
||||
pub downbeats: Vec<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Word {
|
||||
pub w: String,
|
||||
pub start: f64,
|
||||
pub end: f64,
|
||||
pub conf: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Line {
|
||||
pub start: f64,
|
||||
pub end: f64,
|
||||
pub words: Vec<Word>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Lyrics {
|
||||
pub verified: bool,
|
||||
pub lines: Vec<Line>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Analysis {
|
||||
pub meta: Meta,
|
||||
pub pitch: Option<Pitch>,
|
||||
pub chords: Option<Vec<Chord>>,
|
||||
pub beats: Option<Beats>,
|
||||
pub lyrics: Option<Lyrics>,
|
||||
pub vocals: Option<PathBuf>,
|
||||
pub drums: Option<PathBuf>,
|
||||
pub bass: Option<PathBuf>,
|
||||
pub other: Option<PathBuf>,
|
||||
pub melody_midi: Option<PathBuf>,
|
||||
pub guitar_midi: Option<PathBuf>,
|
||||
}
|
||||
|
||||
fn existing(dir: &Path, name: &str) -> Option<PathBuf> {
|
||||
let path = dir.join(name);
|
||||
path.is_file().then_some(path)
|
||||
}
|
||||
|
||||
fn read_optional<T: serde::de::DeserializeOwned>(dir: &Path, name: &str) -> Result<Option<T>> {
|
||||
let path = dir.join(name);
|
||||
if !path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
let bytes = std::fs::read(&path).with_context(|| format!("{} lesen", path.display()))?;
|
||||
let value =
|
||||
serde_json::from_slice(&bytes).with_context(|| format!("{} parsen", path.display()))?;
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
/// Reads only the analysis output directory. Missing outputs are allowed after a failed step.
|
||||
pub fn load_analysis(dir: &Path) -> Result<Analysis> {
|
||||
let path = dir.join("meta.json");
|
||||
let meta: Meta = serde_json::from_slice(
|
||||
&std::fs::read(&path).with_context(|| format!("{} lesen", path.display()))?,
|
||||
)?;
|
||||
if meta.schema_version != 1 {
|
||||
bail!(
|
||||
"Unbekannte song-analysis schema_version {}",
|
||||
meta.schema_version
|
||||
);
|
||||
}
|
||||
let pitch: Option<Pitch> = read_optional(dir, "pitch.json")?;
|
||||
if pitch
|
||||
.as_ref()
|
||||
.is_some_and(|p| p.hop_ms != 10 || p.hz.len() != p.conf.len())
|
||||
{
|
||||
bail!("Ungültige pitch.json");
|
||||
}
|
||||
Ok(Analysis {
|
||||
meta,
|
||||
pitch,
|
||||
chords: read_optional(dir, "chords.json")?,
|
||||
beats: read_optional(dir, "beats.json")?,
|
||||
lyrics: read_optional(dir, "lyrics.json")?,
|
||||
vocals: existing(dir, "stems/vocals.wav"),
|
||||
drums: existing(dir, "stems/drums.wav"),
|
||||
bass: existing(dir, "stems/bass.wav"),
|
||||
other: existing(dir, "stems/other.wav"),
|
||||
melody_midi: existing(dir, "melody.mid"),
|
||||
guitar_midi: existing(dir, "guitar.mid"),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user