//! Versions 1 and 2 of the song-analysis contract, with seconds relative to the source origin. 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, #[serde(default)] pub bpm_source: Option, pub key: Option, pub duration: f64, #[serde(default)] pub source_offset_s: Option, pub schema_version: u32, pub errors: BTreeMap, } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Pitch { pub hop_ms: u32, pub hz: Vec, pub conf: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Chord { pub chord: String, pub start: f64, pub end: f64, pub conf: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Beats { pub beats: Vec, pub downbeats: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Word { pub w: String, pub start: f64, pub end: f64, pub conf: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Line { pub start: Option, pub end: Option, #[serde(default)] pub source_line: Option, #[serde(default)] pub section: Option, #[serde(default)] pub alignment_conf: Option, pub words: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Lyrics { pub verified: bool, pub lines: Vec, } #[derive(Debug)] pub struct Analysis { pub meta: Meta, pub pitch: Option, pub chords: Option>, pub beats: Option, pub lyrics: Option, pub vocals: Option, pub drums: Option, pub bass: Option, pub other: Option, pub melody_midi: Option, pub guitar_midi: Option, } fn existing(dir: &Path, name: &str) -> Option { let path = dir.join(name); path.is_file().then_some(path) } fn read_optional(dir: &Path, name: &str) -> Result> { 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 { 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 && meta.schema_version != 2 { bail!( "Unbekannte song-analysis schema_version {}", meta.schema_version ); } if meta.schema_version == 2 && (meta.source_offset_s != Some(0.0) || !matches!(meta.bpm_source.as_deref(), Some("automatic" | "manual"))) { bail!("Ungültige v2-Metadaten: bpm_source oder source_offset_s fehlt"); } let pitch: Option = 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"), }) }