Files
stimmwerk/src/song_analysis.rs

147 lines
4.1 KiB
Rust

//! 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<f64>,
#[serde(default)]
pub bpm_source: Option<String>,
pub key: Option<String>,
pub duration: f64,
#[serde(default)]
pub source_offset_s: Option<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: Option<f64>,
pub end: Option<f64>,
#[serde(default)]
pub source_line: Option<u32>,
#[serde(default)]
pub section: Option<String>,
#[serde(default)]
pub alignment_conf: Option<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 && 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<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"),
})
}