navidrome/plugins/lyrics_adapter.go
Deluan 0ec7a818fb feat(plugins): content-sniff plugin lyrics for all formats
Replace model.ToLyrics (LRC/plain only) with model.ParseEmbedded so plugin
responses are content-sniffed for TTML, SRT, YAML, LRC, and plain text.
ParseEmbedded returns a LyricList, so the loop now flattens multiple tracks
per response entry.

The test-lyrics WASM plugin gains a "ttml" format mode (configured via
pdk.GetConfig) that returns a minimal TTML document; rebuilt with the
standard Go wasip1 toolchain (GOOS=wasip1 GOARCH=wasm). A new Ginkgo test
asserts Synced==true and the exact cue value, which the old plain-text path
could not produce.

GetLyrics doc comment updated to reflect content-sniffing; a later task will
retarget it to ParseLyrics once that function is introduced.
2026-06-19 14:15:07 -04:00

66 lines
1.5 KiB
Go

package plugins
import (
"context"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/plugins/capabilities"
)
const CapabilityLyrics Capability = "Lyrics"
const (
FuncLyricsGetLyrics = "nd_lyrics_get_lyrics"
)
func init() {
registerCapability(
CapabilityLyrics,
FuncLyricsGetLyrics,
)
}
func newLyricsPlugin(p *plugin) *LyricsPlugin {
return &LyricsPlugin{name: p.name, plugin: p}
}
// LyricsPlugin adapts a WASM plugin with the Lyrics capability.
type LyricsPlugin struct {
name string
plugin *plugin
}
// GetLyrics calls the plugin to fetch lyrics, then content-sniffs each response
// via model.ParseEmbedded (TTML/SRT/YAML/LRC/plain). A later task retargets this
// to ParseLyrics once that function is introduced.
func (l *LyricsPlugin) GetLyrics(ctx context.Context, mf *model.MediaFile) (model.LyricList, error) {
req := capabilities.GetLyricsRequest{
Track: mediaFileToTrackInfo(l.plugin, mf),
}
resp, err := callPluginFunction[capabilities.GetLyricsRequest, capabilities.GetLyricsResponse](
ctx, l.plugin, FuncLyricsGetLyrics, req,
)
if err != nil {
return nil, err
}
var result model.LyricList
for _, lt := range resp.Lyrics {
lang := lt.Lang
if lang == "" {
lang = "xxx"
}
parsed, err := model.ParseEmbedded(lang, lt.Text)
if err != nil {
log.Warn(ctx, "Error parsing plugin lyrics", "plugin", l.name, err)
continue
}
for _, lyric := range parsed {
if !lyric.IsEmpty() {
result = append(result, lyric)
}
}
}
return result, nil
}