feat: version song analysis contract and preserve lyric lines
This commit is contained in:
@ -6,7 +6,7 @@ import sys
|
||||
from pathlib import Path
|
||||
|
||||
from . import backends
|
||||
from .core import group_words, run_step, validate_json, validate_outputs, write_json
|
||||
from .core import bpm_from_beats, group_words, run_step, validate_json, validate_outputs, write_json
|
||||
|
||||
|
||||
def resolve_separator_bin(value: str) -> str:
|
||||
@ -18,6 +18,7 @@ def resolve_separator_bin(value: str) -> str:
|
||||
|
||||
|
||||
def analyze(source: Path, out: Path, lyrics: Path | None = None, *, force: bool = False,
|
||||
title: str | None = None,
|
||||
separator_bin: str = "audio-separator", model_dir: Path | None = None,
|
||||
roformer_model: str = "model_bs_roformer_ep_317_sdr_12.9755.ckpt",
|
||||
language: str = "en", whisper_model: str = "large-v3") -> dict:
|
||||
@ -91,14 +92,18 @@ def analyze(source: Path, out: Path, lyrics: Path | None = None, *, force: bool
|
||||
lambda: backends.transcribe_midi(vocals, out / "melody.mid"), errors, force)
|
||||
lyrics_path = out / "lyrics.json"
|
||||
def lyrics_step():
|
||||
words, verified = backends.lyrics_words(vocals, lyrics, language, whisper_model)
|
||||
downbeats = []
|
||||
if (out / "beats.json").exists():
|
||||
try:
|
||||
downbeats = json.loads((out / "beats.json").read_text(encoding="utf-8"))["downbeats"]
|
||||
except (OSError, ValueError, KeyError):
|
||||
pass
|
||||
value = {"verified": verified, "lines": group_words(words, downbeats)}
|
||||
if lyrics:
|
||||
value = {"verified": True,
|
||||
"lines": backends.align_lyric_lines(vocals, lyrics, language, whisper_model)}
|
||||
else:
|
||||
words = backends.lyrics_words(vocals, language, whisper_model)
|
||||
downbeats = []
|
||||
if (out / "beats.json").exists():
|
||||
try:
|
||||
downbeats = json.loads((out / "beats.json").read_text(encoding="utf-8"))["downbeats"]
|
||||
except (OSError, ValueError, KeyError):
|
||||
pass
|
||||
value = {"verified": False, "lines": group_words(words, downbeats)}
|
||||
validate_json("lyrics", value)
|
||||
write_json(lyrics_path, value)
|
||||
run_step("lyrics", [lyrics_path], lyrics_step, errors, force)
|
||||
@ -108,18 +113,20 @@ def analyze(source: Path, out: Path, lyrics: Path | None = None, *, force: bool
|
||||
lambda: backends.transcribe_midi(stems / "other.wav", out / "guitar.mid"),
|
||||
errors, force)
|
||||
|
||||
bpm = previous.get("bpm")
|
||||
bpm = None
|
||||
if (out / "beats.json").exists():
|
||||
try:
|
||||
beat_times = json.loads((out / "beats.json").read_text(encoding="utf-8"))["beats"]
|
||||
gaps = [b - a for a, b in zip(beat_times, beat_times[1:]) if 0.2 <= b - a <= 2]
|
||||
if gaps:
|
||||
import statistics
|
||||
bpm = round(60 / statistics.median(gaps), 2)
|
||||
bpm = bpm_from_beats(beat_times)
|
||||
except (OSError, ValueError, KeyError, TypeError):
|
||||
pass
|
||||
if not force and previous.get("bpm") is not None:
|
||||
bpm_source = previous.get("bpm_source", "automatic")
|
||||
if previous.get("schema_version") == 1 and previous.get("bpm") is not None:
|
||||
bpm_source = "manual" if previous["bpm"] != bpm else "automatic"
|
||||
if not force and bpm_source == "manual" and previous.get("bpm") is not None:
|
||||
bpm = previous["bpm"]
|
||||
elif force:
|
||||
bpm_source = "automatic"
|
||||
validate_outputs(out, errors)
|
||||
key = previous.get("key")
|
||||
if mix.exists() and (force or key is None):
|
||||
@ -127,10 +134,10 @@ def analyze(source: Path, out: Path, lyrics: Path | None = None, *, force: bool
|
||||
key = backends.detect_key(mix)
|
||||
except Exception as exc:
|
||||
errors["key"] = f"{type(exc).__name__}: {exc}"
|
||||
meta = {"title": previous.get("title", source.stem) if not force else source.stem,
|
||||
"bpm": bpm, "key": key,
|
||||
meta = {"title": title if title is not None else (previous.get("title", source.stem) if not force else source.stem),
|
||||
"bpm": bpm, "bpm_source": bpm_source, "key": key,
|
||||
"duration": previous.get("duration", duration) if not force and previous.get("duration", 0) > 0 else duration,
|
||||
"schema_version": 1, "errors": errors}
|
||||
"source_offset_s": 0.0, "schema_version": 2, "errors": errors}
|
||||
try:
|
||||
validate_json("meta", meta)
|
||||
except Exception as exc:
|
||||
@ -144,6 +151,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
parser.add_argument("audio", type=Path)
|
||||
parser.add_argument("--out", required=True, type=Path)
|
||||
parser.add_argument("--lyrics", type=Path)
|
||||
parser.add_argument("--title")
|
||||
parser.add_argument("--force", action="store_true")
|
||||
parser.add_argument("--separator-bin", default="audio-separator")
|
||||
parser.add_argument("--model-dir", type=Path)
|
||||
@ -152,7 +160,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
parser.add_argument("--whisper-model", default="large-v3")
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
meta = analyze(args.audio, args.out, args.lyrics, force=args.force,
|
||||
meta = analyze(args.audio, args.out, args.lyrics, force=args.force, title=args.title,
|
||||
separator_bin=args.separator_bin, model_dir=args.model_dir,
|
||||
roformer_model=args.roformer_model, language=args.language,
|
||||
whisper_model=args.whisper_model)
|
||||
|
||||
Reference in New Issue
Block a user