feat(schema): implement pydantic schemas and tempo conversion logic
This commit is contained in:
29
src/songsplit/schema/__init__.py
Normal file
29
src/songsplit/schema/__init__.py
Normal file
@ -0,0 +1,29 @@
|
||||
"""Song-Paket data schemas."""
|
||||
|
||||
from songsplit.schema.base import DataSource, GeneratedBy, SongPackageBase
|
||||
from songsplit.schema.chords import ChordEvent, ChordsDocument
|
||||
from songsplit.schema.meta import MetaDocument, ModelInfo, SourceFileInfo
|
||||
from songsplit.schema.strumming import StrumDirection, StrumEvent, StrummingDocument, StrumPattern
|
||||
from songsplit.schema.tempo import TempoAnchor, TempoDocument, TimeSignature
|
||||
from songsplit.schema.vocals import VocalLine, VocalsDocument, VocalSyllable
|
||||
|
||||
__all__ = [
|
||||
"DataSource",
|
||||
"GeneratedBy",
|
||||
"SongPackageBase",
|
||||
"SourceFileInfo",
|
||||
"ModelInfo",
|
||||
"MetaDocument",
|
||||
"VocalSyllable",
|
||||
"VocalLine",
|
||||
"VocalsDocument",
|
||||
"TempoAnchor",
|
||||
"TimeSignature",
|
||||
"TempoDocument",
|
||||
"ChordEvent",
|
||||
"ChordsDocument",
|
||||
"StrumDirection",
|
||||
"StrumEvent",
|
||||
"StrumPattern",
|
||||
"StrummingDocument",
|
||||
]
|
||||
27
src/songsplit/schema/base.py
Normal file
27
src/songsplit/schema/base.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""Base classes and shared types for Song-Paket schemas."""
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
DataSource = Literal["auto", "manual", "import"]
|
||||
|
||||
|
||||
class GeneratedBy(BaseModel):
|
||||
"""Information about the stage, model and parameters used to generate the data."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
stage: str
|
||||
model: str
|
||||
model_version: str = "1.0"
|
||||
params: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class SongPackageBase(BaseModel):
|
||||
"""Base schema for all Song-Paket JSON files with schema version and generation info."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
schema_version: str = "1.0"
|
||||
generated_by: GeneratedBy | None = None
|
||||
28
src/songsplit/schema/chords.py
Normal file
28
src/songsplit/schema/chords.py
Normal file
@ -0,0 +1,28 @@
|
||||
"""Schema for chords.json."""
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from songsplit.schema.base import DataSource, SongPackageBase
|
||||
|
||||
|
||||
class ChordEvent(BaseModel):
|
||||
"""A chord segment defined in beats."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
start_beat: float
|
||||
end_beat: float
|
||||
label: str
|
||||
root: str | None = None
|
||||
quality: str | None = None
|
||||
bass: str | None = None
|
||||
confidence: float = Field(default=1.0, ge=0.0, le=1.0)
|
||||
source: DataSource = "auto"
|
||||
|
||||
|
||||
class ChordsDocument(SongPackageBase):
|
||||
"""Song-Paket chords document (chords.json)."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
chords: list[ChordEvent] = Field(default_factory=list)
|
||||
43
src/songsplit/schema/meta.py
Normal file
43
src/songsplit/schema/meta.py
Normal file
@ -0,0 +1,43 @@
|
||||
"""Schema for meta.json."""
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from songsplit.schema.base import SongPackageBase
|
||||
|
||||
|
||||
class SourceFileInfo(BaseModel):
|
||||
"""Information about the original source audio/video file."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
path: str
|
||||
sha256: str
|
||||
size_bytes: int
|
||||
|
||||
|
||||
class ModelInfo(BaseModel):
|
||||
"""Information about an AI model used during processing."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
stage: str
|
||||
name: str
|
||||
version: str
|
||||
|
||||
|
||||
class MetaDocument(SongPackageBase):
|
||||
"""Song-Paket metadata document (meta.json)."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
title: str | None = None
|
||||
artist: str | None = None
|
||||
language: str | None = None
|
||||
duration_sec: float = Field(ge=0.0)
|
||||
source_file: SourceFileInfo
|
||||
key: str | None = None
|
||||
capo: int | None = None
|
||||
models_used: list[ModelInfo] = Field(default_factory=list)
|
||||
created_at: str
|
||||
stages_done: list[str] = Field(default_factory=list)
|
||||
stages_failed: dict[str, str] = Field(default_factory=dict)
|
||||
41
src/songsplit/schema/strumming.py
Normal file
41
src/songsplit/schema/strumming.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""Schema for strumming.json."""
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from songsplit.schema.base import DataSource, SongPackageBase
|
||||
|
||||
StrumDirection = Literal["down", "up", "unknown"]
|
||||
|
||||
|
||||
class StrumEvent(BaseModel):
|
||||
"""A single strumming event."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
beat: float
|
||||
direction: StrumDirection
|
||||
strength: float = Field(default=1.0, ge=0.0, le=1.0)
|
||||
confidence: float = Field(default=1.0, ge=0.0, le=1.0)
|
||||
source: DataSource = "auto"
|
||||
|
||||
|
||||
class StrumPattern(BaseModel):
|
||||
"""An aggregated strumming pattern over a beat range."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
start_beat: float
|
||||
end_beat: float
|
||||
pattern: str
|
||||
grid: str = "8th"
|
||||
|
||||
|
||||
class StrummingDocument(SongPackageBase):
|
||||
"""Song-Paket strumming document (strumming.json)."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
events: list[StrumEvent] = Field(default_factory=list)
|
||||
patterns: list[StrumPattern] = Field(default_factory=list)
|
||||
34
src/songsplit/schema/tempo.py
Normal file
34
src/songsplit/schema/tempo.py
Normal file
@ -0,0 +1,34 @@
|
||||
"""Schema for tempo.json."""
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from songsplit.schema.base import SongPackageBase
|
||||
|
||||
|
||||
class TempoAnchor(BaseModel):
|
||||
"""A tempo anchor defining constant BPM starting from a given time/beat."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
sec: float
|
||||
beat: float
|
||||
bpm: float = Field(gt=0.0)
|
||||
|
||||
|
||||
class TimeSignature(BaseModel):
|
||||
"""Musical time signature, e.g. 4/4."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
numerator: int = Field(default=4, gt=0)
|
||||
denominator: int = Field(default=4, gt=0)
|
||||
|
||||
|
||||
class TempoDocument(SongPackageBase):
|
||||
"""Song-Paket tempo document (tempo.json)."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
anchors: list[TempoAnchor] = Field(default_factory=list)
|
||||
time_signature: TimeSignature = Field(default_factory=TimeSignature)
|
||||
downbeat_offset: int = 0
|
||||
37
src/songsplit/schema/vocals.py
Normal file
37
src/songsplit/schema/vocals.py
Normal file
@ -0,0 +1,37 @@
|
||||
"""Schema for vocals.json."""
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from songsplit.schema.base import DataSource, SongPackageBase
|
||||
|
||||
|
||||
class VocalSyllable(BaseModel):
|
||||
"""A single syllable with timing, pitch, and confidence."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
text: str
|
||||
start: float = Field(ge=0.0)
|
||||
end: float = Field(ge=0.0)
|
||||
midi: float
|
||||
golden: bool = False
|
||||
confidence: float = Field(default=1.0, ge=0.0, le=1.0)
|
||||
source: DataSource = "auto"
|
||||
|
||||
|
||||
class VocalLine(BaseModel):
|
||||
"""A line of lyrics consisting of one or more syllables."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
start: float = Field(ge=0.0)
|
||||
end: float = Field(ge=0.0)
|
||||
syllables: list[VocalSyllable] = Field(default_factory=list)
|
||||
|
||||
|
||||
class VocalsDocument(SongPackageBase):
|
||||
"""Song-Paket vocals document (vocals.json)."""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
lines: list[VocalLine] = Field(default_factory=list)
|
||||
64
src/songsplit/tempo.py
Normal file
64
src/songsplit/tempo.py
Normal file
@ -0,0 +1,64 @@
|
||||
"""Tempo-Map conversion between seconds and musical beats.
|
||||
|
||||
As defined in the concept:
|
||||
- Vocals are in seconds
|
||||
- Chords and strumming are in beats
|
||||
- Conversion ONLY through sec_to_beat() and beat_to_sec().
|
||||
"""
|
||||
|
||||
from bisect import bisect_right
|
||||
|
||||
from songsplit.schema.tempo import TempoAnchor, TempoDocument
|
||||
|
||||
|
||||
def _get_sorted_anchors(tempo_map: TempoDocument) -> list[TempoAnchor]:
|
||||
"""Return anchors sorted by time/beat, raising ValueError if empty."""
|
||||
if not tempo_map.anchors:
|
||||
raise ValueError("Tempo map has no anchors defined.")
|
||||
return sorted(tempo_map.anchors, key=lambda a: a.sec)
|
||||
|
||||
|
||||
def sec_to_beat(sec: float, tempo_map: TempoDocument) -> float:
|
||||
"""Convert time in seconds to musical beat (float quarter notes).
|
||||
|
||||
Constant tempo is assumed between consecutive anchors.
|
||||
The last anchor applies until the end of the song.
|
||||
Extrapolation is used before the first anchor.
|
||||
"""
|
||||
anchors = _get_sorted_anchors(tempo_map)
|
||||
sec_points = [a.sec for a in anchors]
|
||||
|
||||
idx = bisect_right(sec_points, sec) - 1
|
||||
|
||||
if idx < 0:
|
||||
# Before the first anchor -> extrapolate using the first anchor's tempo
|
||||
anchor = anchors[0]
|
||||
delta_sec = sec - anchor.sec
|
||||
return anchor.beat + delta_sec * (anchor.bpm / 60.0)
|
||||
|
||||
anchor = anchors[idx]
|
||||
delta_sec = sec - anchor.sec
|
||||
return anchor.beat + delta_sec * (anchor.bpm / 60.0)
|
||||
|
||||
|
||||
def beat_to_sec(beat: float, tempo_map: TempoDocument) -> float:
|
||||
"""Convert musical beat (float quarter notes) to time in seconds.
|
||||
|
||||
Constant tempo is assumed between consecutive anchors.
|
||||
The last anchor applies until the end of the song.
|
||||
Extrapolation is used before the first anchor.
|
||||
"""
|
||||
anchors = _get_sorted_anchors(tempo_map)
|
||||
beat_points = [a.beat for a in anchors]
|
||||
|
||||
idx = bisect_right(beat_points, beat) - 1
|
||||
|
||||
if idx < 0:
|
||||
# Before the first anchor -> extrapolate using the first anchor's tempo
|
||||
anchor = anchors[0]
|
||||
delta_beat = beat - anchor.beat
|
||||
return anchor.sec + delta_beat * (60.0 / anchor.bpm)
|
||||
|
||||
anchor = anchors[idx]
|
||||
delta_beat = beat - anchor.beat
|
||||
return anchor.sec + delta_beat * (60.0 / anchor.bpm)
|
||||
Reference in New Issue
Block a user