Skip to content

Commit

Permalink
feat: add timestamp in xesam:asText
Browse files Browse the repository at this point in the history
  • Loading branch information
imxyy1soope1 committed Dec 9, 2023
1 parent 4692ee4 commit 83f694a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 12 additions & 5 deletions internal/lyric/lrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@ type LRCFile struct {
fragments []LRCFragment
}

func (f *LRCFile) AsText() string {
func AsText(f *LRCFile, t *TranslateLRCFile) string {
var builder strings.Builder

if f == nil || len(f.fragments) == 0 {
return "暂无歌词~"
return "[00:00.00]暂无歌词~"
}

for _, line := range f.fragments {
flt := time.Duration(line.StartTimeMs*1e6) % (time.Second * 60)
it := time.Duration(line.StartTimeMs * 1e6)
builder.WriteString(fmt.Sprintf("[%02.0f:%05.2f]", it.Minutes(), flt.Seconds()))
builder.WriteString(line.Content)
if t != nil && t.fragments[line.StartTimeMs] != "" {
builder.WriteByte(' ')
builder.WriteByte('[')
builder.WriteString(t.fragments[line.StartTimeMs])
builder.WriteByte(']')
}
builder.WriteByte('\n')
}

res := builder.String()

return res[:len(res)-1]
return builder.String()[:builder.Len()-1]
}

type TranslateLRCFile struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func (p *Player) lyricListener(_ int64, content, transContent string, _ bool, in

// getLyric 获取歌曲歌词
func (p *Player) getLyric(songId int64) {
p.lrcFile, _ = lyric.ReadLRC(strings.NewReader("[00:00.00] 暂无歌词~"))
p.lrcFile, _ = lyric.ReadLRC(strings.NewReader("[00:00.00]暂无歌词~"))
p.transLrcFile, _ = lyric.ReadTranslateLRC(strings.NewReader("[00:00.00]"))
lrcService := service.LyricService{
ID: strconv.FormatInt(songId, 10),
Expand Down Expand Up @@ -838,6 +838,6 @@ func (p *Player) PlayingInfo() state_handler.PlayingInfo {
Album: music.Album.Name,
Artist: music.ArtistName(),
AlbumArtist: music.Album.ArtistName(),
AsText: p.lrcFile.AsText(),
AsText: lyric.AsText(p.lrcFile, p.transLrcFile),
}
}

0 comments on commit 83f694a

Please sign in to comment.