mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
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)
145 lines
3.8 KiB
Go
145 lines
3.8 KiB
Go
package subsonic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/resources"
|
|
"github.com/navidrome/navidrome/server/subsonic/responses"
|
|
"github.com/navidrome/navidrome/utils/gravatar"
|
|
"github.com/navidrome/navidrome/utils/req"
|
|
)
|
|
|
|
func (api *Router) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
|
if !conf.Server.EnableGravatar {
|
|
return api.getPlaceHolderAvatar(w, r)
|
|
}
|
|
p := req.Params(r)
|
|
username, err := p.String("username")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx := r.Context()
|
|
u, err := api.ds.User(ctx).FindByUsername(username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if u.Email == "" {
|
|
log.Warn(ctx, "User needs an email for gravatar to work", "username", username)
|
|
return api.getPlaceHolderAvatar(w, r)
|
|
}
|
|
http.Redirect(w, r, gravatar.Url(u.Email, 0), http.StatusFound) //nolint:gosec // URL is not constructed from user input
|
|
return nil, nil
|
|
}
|
|
|
|
func (api *Router) getPlaceHolderAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
|
f, err := resources.FS().Open(consts.PlaceholderAvatar)
|
|
if err != nil {
|
|
log.Error(r, "Image not found", err)
|
|
return nil, newError(responses.ErrorDataNotFound, "Avatar image not found")
|
|
}
|
|
defer f.Close()
|
|
_, _ = io.Copy(w, f)
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (api *Router) GetCoverArt(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
|
// If context is already canceled, discard request without further processing
|
|
if r.Context().Err() != nil {
|
|
return nil, nil //nolint:nilerr
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
p := req.Params(r)
|
|
id, _ := p.String("id")
|
|
size := p.IntOr("size", 0)
|
|
square := p.BoolOr("square", false)
|
|
|
|
imgReader, lastUpdate, err := api.artwork.GetOrPlaceholder(ctx, id, size, square)
|
|
switch {
|
|
case errors.Is(err, context.Canceled):
|
|
return nil, nil
|
|
case errors.Is(err, model.ErrNotFound):
|
|
log.Warn(r, "Couldn't find coverArt", "id", id, err)
|
|
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
|
|
case err != nil:
|
|
log.Error(r, "Error retrieving coverArt", "id", id, err)
|
|
return nil, err
|
|
}
|
|
|
|
defer imgReader.Close()
|
|
w.Header().Set("cache-control", "public, max-age=315360000")
|
|
w.Header().Set("last-modified", lastUpdate.Format(http.TimeFormat))
|
|
|
|
cnt, err := io.Copy(w, imgReader)
|
|
if err != nil {
|
|
log.Warn(ctx, "Error sending image", "count", cnt, err)
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
func (api *Router) GetLyrics(r *http.Request) (*responses.Subsonic, error) {
|
|
p := req.Params(r)
|
|
artist, _ := p.String("artist")
|
|
title, _ := p.String("title")
|
|
response := newResponse()
|
|
lyricsResponse := responses.Lyrics{}
|
|
response.Lyrics = &lyricsResponse
|
|
structuredLyrics, err := api.lyrics.GetLyricsByArtistTitle(r.Context(), artist, title)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
mainLyric, ok := structuredLyrics.Main()
|
|
if !ok {
|
|
return response, nil
|
|
}
|
|
|
|
lyricsResponse.Artist = artist
|
|
lyricsResponse.Title = title
|
|
|
|
var lyricsText strings.Builder
|
|
for _, line := range mainLyric.Line {
|
|
lyricsText.WriteString(line.Value + "\n")
|
|
}
|
|
lyricsResponse.Value = lyricsText.String()
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetLyricsBySongId(r *http.Request) (*responses.Subsonic, error) {
|
|
id, err := req.Params(r).String("id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
mediaFile, err := api.ds.MediaFile(r.Context()).Get(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
structuredLyrics, err := api.lyrics.GetLyrics(r.Context(), mediaFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
enhanced, _ := req.Params(r).Bool("enhanced")
|
|
|
|
response := newResponse()
|
|
response.LyricsList = buildLyricsList(mediaFile, structuredLyrics, enhanced)
|
|
|
|
return response, nil
|
|
}
|