navidrome/server/subsonic/lyrics.go
Yuuta 3a14faa033
feat(subsonic): add structured sidecar lyrics support with OpenSubsonic v2 karaoke cues and agent layers (#5076)
Expand backend lyrics support with richer sidecar formats and upgrade the
OpenSubsonic songLyrics implementation to the version 2 structured karaoke
contract, while preserving version 1 behavior by default.

Sidecar formats and parsing:
- Add a TTML parser (core/lyrics/ttml.go): clock time, offset time, bare
  decimal seconds, nested timing contexts, and token-level <span> timing for
  word/syllable karaoke. Parses Apple Music-style metadata tracks (translation
  and pronunciation/transliteration) and agent metadata into per-track agents[]
  plus per-cue-line agentId. Hydrates missing line timing from cue timing.
- Add an SRT parser (core/lyrics/srt.go).
- Add a LRCLIB Lyricsfile (.yaml/.yml) parser (model/lyricsfile.go): maps
  per-word lines[].words[] to cues with inclusive UTF-8 byte offsets and
  attributes overlapping lines to synthetic voice agents so parallel vocals
  split correctly in the enhanced response.
- Extend LRC parsing for Enhanced LRC inline <mm:ss.xx> word-timing markers.
- Add UTF-8 BOM and UTF-16 LE support for TTML/LRC sidecars.
- Parse the above formats from embedded tags as well as sidecar files.

Source resolution:
- Default lyricspriority is now
  ".ttml,.yaml,.yml,.elrc,.lrc,.srt,.txt,embedded" so the new formats are
  discoverable without manual configuration.
- Preserve configured source priority across duplicate media-file candidates
  instead of only checking the first DB match, so higher-priority sidecar
  lyrics on older duplicates can still win.
- Raise the embedded-lyrics tag maxLength to 1 MB to fit word-timed
  TTML/Enhanced-LRC karaoke for a full song.

OpenSubsonic songLyrics v2:
- Advertise songLyrics versions [1, 2].
- With enhanced=true, getLyricsBySongId may return structuredLyrics.kind
  (main/translation/pronunciation), cueLine[] line-level karaoke groupings,
  cueLine.cue[] timed words/syllables with required UTF-8 byteStart/byteEnd,
  reusable structuredLyrics.agents[], and cueLine.agentId references.
- Without enhanced=true, the response stays v1-compatible: no kind, no cueLine,
  no agents, no non-main tracks; the existing line[] payload is always
  populated so legacy clients keep working.

Contract details:
- cueLine is emitted only for synced lyrics with cue data.
- Within a cueLine, cue.end is normalized all-or-none and overlaps are removed;
  overlaps across separate cueLines remain valid for parallel vocal layers.
- Missing cue end-times are filled from the next cue or the parent line.
- When cueLines share an index, the one whose agent has role "main" is first.
- LyricCue.Value is serialized as XML chardata; cues with nil start are skipped
  rather than serialized as 0.

Refactoring:
- Move pure format parsers into model/ (lyrics.go, lyrics_ttml.go,
  lyrics_srt.go, lyrics_embedded.go, lyricsfile.go) and extract Subsonic
  response building into server/subsonic/lyrics.go.
- Centralize lyric-kind constants and add Lyrics.EffectiveKind/IsMainKind.
- Add gg.Clone helper.

Spec references:
  https://github.com/opensubsonic/open-subsonic-api/discussions/213
  https://github.com/opensubsonic/open-subsonic-api/pull/218 (songLyrics v2)
  https://github.com/opensubsonic/open-subsonic-api/pull/228 (cue byte offsets)
2026-06-19 12:00:58 -04:00

181 lines
5 KiB
Go

package subsonic
import (
"slices"
"sort"
"strings"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/subsonic/responses"
)
// agentRoleMain is the OpenSubsonic agent role that marks the primary vocal
// layer; its cue line is emitted before other agents sharing the same index.
const agentRoleMain = "main"
func buildLyricsList(mf *model.MediaFile, lyricsList model.LyricList, enhanced bool) *responses.LyricsList {
filtered := lyricsList
if !enhanced {
// Without enhanced, only return main-kind entries (a blank kind is main).
filtered = nil
for _, l := range lyricsList {
if l.IsMainKind() {
filtered = append(filtered, l)
}
}
}
lyricList := make(responses.StructuredLyrics, len(filtered))
for i, lyrics := range filtered {
lyricList[i] = buildStructuredLyric(mf, lyrics, enhanced)
}
return &responses.LyricsList{StructuredLyrics: lyricList}
}
func buildStructuredLyric(mf *model.MediaFile, lyrics model.Lyrics, enhanced bool) responses.StructuredLyric {
agents := newLyricAgents(lyrics.Agents)
lines := make([]responses.Line, len(lyrics.Line))
var cueLines []responses.CueLine
for i, line := range lyrics.Line {
lines[i] = responses.Line{Start: line.Start, Value: line.Value}
if enhanced && len(line.Cue) > 0 {
cueLines = append(cueLines, buildCueLines(line, int32(i), agents)...)
}
}
structured := responses.StructuredLyric{
DisplayArtist: lyrics.DisplayArtist,
DisplayTitle: lyrics.DisplayTitle,
Lang: lyrics.Lang,
Line: lines,
CueLine: cueLines,
Offset: lyrics.Offset,
Synced: lyrics.Synced,
}
if enhanced {
structured.Kind = lyrics.EffectiveKind()
if len(cueLines) > 0 && len(agents.response) > 0 {
structured.Agents = agents.response
}
}
if structured.DisplayArtist == "" {
structured.DisplayArtist = mf.Artist
}
if structured.DisplayTitle == "" {
structured.DisplayTitle = mf.Title
}
return structured
}
// lyricAgents indexes a lyric's agents by ID so cue lines can be ordered and
// the response agent list reused without rescanning the slice per line.
type lyricAgents struct {
orderByID map[string]int
roleByID map[string]string
response []responses.Agent
}
func newLyricAgents(agents []model.Agent) lyricAgents {
a := lyricAgents{
orderByID: make(map[string]int, len(agents)),
roleByID: make(map[string]string, len(agents)),
response: make([]responses.Agent, 0, len(agents)),
}
for i, agent := range agents {
a.orderByID[agent.ID] = i
a.roleByID[agent.ID] = agent.Role
a.response = append(a.response, responses.Agent{ID: agent.ID, Role: agent.Role, Name: agent.Name})
}
return a
}
// buildCueLines splits a line's cues by agent and emits one CueLine per agent,
// ordered main-role first then by the agent's declared order.
func buildCueLines(line model.Line, index int32, agents lyricAgents) []responses.CueLine {
agentOrder := make([]string, 0, 2)
cuesByAgent := make(map[string][]model.Cue)
for _, cue := range line.Cue {
if cue.Start == nil {
continue
}
agentID := strings.TrimSpace(cue.AgentID)
if _, exists := cuesByAgent[agentID]; !exists {
agentOrder = append(agentOrder, agentID)
}
cuesByAgent[agentID] = append(cuesByAgent[agentID], cue)
}
sort.SliceStable(agentOrder, func(i, j int) bool {
return agents.less(agentOrder[i], agentOrder[j], i, j)
})
cueLines := make([]responses.CueLine, 0, len(agentOrder))
for _, agentID := range agentOrder {
cueLine := responses.CueLine{
Index: index,
Start: line.Start,
End: line.End,
Value: line.Value,
Cue: buildLyricCues(cuesByAgent[agentID], line.End),
}
if agentID != "" {
cueLine.AgentID = agentID
}
cueLines = append(cueLines, cueLine)
}
return cueLines
}
// less orders two agent IDs: the main role wins, then the declared agent order,
// then known-before-unknown, then the original encounter order (origI/origJ).
func (a lyricAgents) less(left, right string, origI, origJ int) bool {
leftMain := a.roleByID[left] == agentRoleMain
rightMain := a.roleByID[right] == agentRoleMain
if leftMain != rightMain {
return leftMain
}
leftOrder, leftOK := a.orderByID[left]
rightOrder, rightOK := a.orderByID[right]
if leftOK && rightOK && leftOrder != rightOrder {
return leftOrder < rightOrder
}
if leftOK != rightOK {
return leftOK
}
return origI < origJ
}
func buildLyricCues(cues []model.Cue, lineEnd *int64) []responses.LyricCue {
if len(cues) == 0 {
return nil
}
// Only resolve end times when at least one cue carries one; otherwise the
// group is start-only and must stay that way.
hasAnyEnd := slices.ContainsFunc(cues, func(c model.Cue) bool { return c.End != nil })
if hasAnyEnd {
cues = model.NormalizeCueEnds(cues, lineEnd)
}
out := make([]responses.LyricCue, 0, len(cues))
for i := range cues {
if cues[i].Start == nil {
continue
}
cue := responses.LyricCue{
Start: *cues[i].Start,
Value: cues[i].Value,
ByteStart: cues[i].ByteStart,
ByteEnd: cues[i].ByteEnd,
}
if hasAnyEnd {
cue.End = cues[i].End
}
out = append(out, cue)
}
return out
}