Skip to content

Commit

Permalink
feat: impl xesam:asText (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
anhoderai committed May 12, 2024
1 parent c71521e commit 0edfcf8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
43 changes: 32 additions & 11 deletions internal/lyric/lrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/pkg/errors"
Expand All @@ -19,23 +20,43 @@ import (

type LRCFile struct {
fragments []LRCFragment
}

func (f *LRCFile) AsText() string {
var builder strings.Builder
text string
l sync.Mutex
}

func (f *LRCFile) AsText(t ...*TranslateLRCFile) string {
if f == nil || len(f.fragments) == 0 {
return "暂无歌词~"
return "[00:00.00]暂无歌词~"
}

f.l.Lock()
defer f.l.Unlock()

if f.text != "" {
return f.text
}

var trans *TranslateLRCFile
if len(t) > 0 {
trans = t[0]
}

var builder strings.Builder
for _, line := range f.fragments {
at := time.Duration(line.StartTimeMs) * time.Millisecond
builder.WriteString(fmt.Sprintf("[%02d:%05.2f]", int(at.Minutes()), at.Seconds()))
builder.WriteString(line.Content)
builder.WriteByte('\n')
if trans != nil && trans.fragments[line.StartTimeMs] != "" {
builder.WriteString(" [")
builder.WriteString(trans.fragments[line.StartTimeMs])
builder.WriteString("]")
}
builder.WriteString("\n")
}
f.text = builder.String()

res := builder.String()

return res[:len(res)-1]
return f.text
}

type TranslateLRCFile struct {
Expand Down Expand Up @@ -153,8 +174,8 @@ func parseLRCTime(line, openChar, closeChar string) (tm time.Duration, err error
}
}()

var left = strings.Index(line, openChar)
var right = strings.Index(line, closeChar)
left := strings.Index(line, openChar)
right := strings.Index(line, closeChar)
if left < 0 || right < 0 {
err = errors.New("brackets missing")
return
Expand All @@ -173,7 +194,7 @@ func parseLRCTime(line, openChar, closeChar string) (tm time.Duration, err error
err = errors.New("format error")
return
}
var milliseconds = minutes*60000 + int(math.Floor(seconds*1000))
milliseconds := minutes*60000 + int(math.Floor(seconds*1000))
tm = time.Duration(milliseconds) * time.Millisecond
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ func (p *Player) PlayingInfo() control.PlayingInfo {
Album: music.Album.Name,
Artist: music.ArtistName(),
AlbumArtist: music.Album.ArtistName(),
AsText: p.lrcFile.AsText(),
AsText: p.lrcFile.AsText(p.transLrcFile),
}
}

Expand Down

0 comments on commit 0edfcf8

Please sign in to comment.