-
-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for lyrics #519
Open
danielepintore
wants to merge
5
commits into
sentriz:master
Choose a base branch
from
danielepintore:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0131d1
Add songLyrics extension since the getLyricsBySongId method is implem…
danielepintore 3b6ff61
Add support for lyrics
danielepintore 033fee8
Add check for getLyrics function
danielepintore 786786b
fix typo
danielepintore 2029a6e
chore: fix tests
danielepintore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,12 @@ import ( | |
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"time" | ||
"unicode" | ||
|
||
"github.com/env25/mpdlrc/lrc" | ||
"github.com/google/uuid" | ||
"github.com/jinzhu/gorm" | ||
|
||
|
@@ -41,6 +43,7 @@ func (c *Controller) ServeGetOpenSubsonicExtensions(_ *http.Request) *spec.Respo | |
sub.OpenSubsonicExtensions = &spec.OpenSubsonicExtensions{ | ||
{Name: "transcodeOffset", Versions: []int{1}}, | ||
{Name: "formPost", Versions: []int{1}}, | ||
{Name: "songLyrics", Versions: []int{1}}, | ||
} | ||
return sub | ||
} | ||
|
@@ -468,12 +471,154 @@ func (c *Controller) ServeJukebox(r *http.Request) *spec.Response { // nolint:go | |
return sub | ||
} | ||
|
||
func (c *Controller) ServeGetLyrics(_ *http.Request) *spec.Response { | ||
func (c *Controller) ServeGetLyrics(r *http.Request) *spec.Response { | ||
params := r.Context().Value(CtxParams).(params.Params) | ||
artist, _ := params.Get("artist") | ||
title, _ := params.Get("title") | ||
|
||
var track db.Track | ||
q := c.dbc. | ||
Preload("Album"). | ||
Joins("JOIN track_artists ON track_artists.track_id = tracks.id"). | ||
Joins("JOIN artists ON artists.id = track_artists.artist_id"). | ||
Where("tracks.tag_title LIKE ? AND artists.name LIKE ?", title, artist). | ||
First(&track) | ||
if err := q.Error; err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return spec.NewError(70, "couldn't find a track with that id") | ||
} else { | ||
return spec.NewError(0, "lyrics: %v", err) | ||
} | ||
} | ||
|
||
sub := spec.NewResponse() | ||
|
||
if track.Lyrics != "" { | ||
sub.Lyrics = &spec.Lyrics{ | ||
Value: track.Lyrics, | ||
Artist: track.TagTrackArtist, | ||
Title: track.TagTitle, | ||
} | ||
return sub | ||
} | ||
|
||
_, text, err := lyricsFile(&track) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return sub | ||
} | ||
return spec.NewError(0, fmt.Sprintf("lyricsFile: %v", err.Error())) | ||
} | ||
|
||
contents := strings.Join(text, "\n") | ||
|
||
sub.Lyrics = &spec.Lyrics{ | ||
Value: contents, | ||
Artist: track.TagTrackArtist, | ||
Title: track.TagTitle, | ||
} | ||
return sub | ||
} | ||
|
||
func (c *Controller) ServeGetLyricsBySongID(r *http.Request) *spec.Response { | ||
params := r.Context().Value(CtxParams).(params.Params) | ||
id, err := params.GetID("id") | ||
if err != nil { | ||
return spec.NewError(10, "provide an `id` parameter") | ||
} | ||
|
||
var track db.Track | ||
q := c.dbc. | ||
Preload("Album"). | ||
Preload("Album.Artists"). | ||
Preload("Artists"). | ||
Where("id=?", id.Value). | ||
First(&track) | ||
if err := q.Error; err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return spec.NewError(70, "couldn't find a track with that id") | ||
} else { | ||
return spec.NewError(0, "lyrics: %v", err) | ||
} | ||
} | ||
|
||
sub := spec.NewResponse() | ||
sub.Lyrics = &spec.Lyrics{} | ||
|
||
if track.Lyrics != "" { | ||
times, lrc, err := lrc.ParseString(track.Lyrics) | ||
if err != nil { | ||
return spec.NewError(0, fmt.Sprintf("lyricsFile: %v", err.Error())) | ||
} | ||
|
||
lines := make([]spec.Lyric, len(times)) | ||
for i, time := range times { | ||
lines[i] = spec.Lyric{ | ||
Start: time.Milliseconds(), | ||
Value: lrc[i], | ||
} | ||
} | ||
|
||
structured := spec.StructuredLyrics{ | ||
Lang: "xxx", | ||
Synced: true, | ||
Lines: lines, | ||
DisplayArtist: track.TagTrackArtist, | ||
DisplayTitle: track.TagTitle, | ||
Offset: 0, | ||
} | ||
|
||
sub.LyricsList = &spec.LyricsList{ | ||
StructuredLyrics: []spec.StructuredLyrics{structured}, | ||
} | ||
return sub | ||
} | ||
|
||
times, lrc, err := lyricsFile(&track) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
sub.LyricsList = &spec.LyricsList{ | ||
StructuredLyrics: []spec.StructuredLyrics{}, | ||
} | ||
return sub | ||
} | ||
return spec.NewError(0, fmt.Sprintf("lyricsFile: %v", err.Error())) | ||
} | ||
|
||
lines := make([]spec.Lyric, len(times)) | ||
for i, time := range times { | ||
lines[i] = spec.Lyric{ | ||
Start: time.Milliseconds(), | ||
Value: lrc[i], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here :) |
||
} | ||
} | ||
|
||
structured := spec.StructuredLyrics{ | ||
Lang: "xxx", | ||
Synced: true, | ||
Lines: lines, | ||
DisplayArtist: track.TagTrackArtist, | ||
DisplayTitle: track.TagTitle, | ||
Offset: 0, | ||
} | ||
|
||
sub.LyricsList = &spec.LyricsList{ | ||
StructuredLyrics: []spec.StructuredLyrics{structured}, | ||
} | ||
return sub | ||
} | ||
|
||
func lyricsFile(file *db.Track) ([]lrc.Duration, []lrc.Text, error) { | ||
dir := filepath.Dir(file.AbsPath()) | ||
filename := strings.TrimSuffix(filepath.Base(file.AbsPath()), filepath.Ext(file.AbsPath())) | ||
|
||
lrcContent, err := os.ReadFile(filepath.Join(dir, filename+".lrc")) | ||
if err != nil { | ||
return []lrc.Duration{}, []lrc.Text{}, err | ||
} | ||
|
||
return lrc.Parse(lrcContent) | ||
} | ||
|
||
func scrobbleStatsUpdateTrack(dbc *db.DB, track *db.Track, userID int, playTime time.Time) error { | ||
var play db.Play | ||
if err := dbc.Where("album_id=? AND user_id=?", track.AlbumID, userID).First(&play).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it is standard or not, but the lrc source files I have have a space between the time tag and a string.
[mm:ss] Line
I'm using Tempo as my player on Android and there the lyrics highliter would shift by a letter with each new line.
I checkd with the api and noticed that the
value
would start with a leading space"value": " Line"
I added a
string.TrimSpace
around thelrc[i]
and that fixed the space offset issue with Tempo