2020-06-22 00:05:56 +08:00
|
|
|
// Copyright (C) 2020 Raziman
|
|
|
|
|
2020-06-19 16:22:20 +08:00
|
|
|
package main
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-06-19 16:22:20 +08:00
|
|
|
|
2021-02-21 15:18:19 +08:00
|
|
|
"github.com/bogem/id3v2"
|
2020-06-21 23:47:02 +08:00
|
|
|
"github.com/rivo/tview"
|
2020-07-21 12:22:00 +08:00
|
|
|
"github.com/ztrue/tracerr"
|
2021-03-02 23:34:38 +08:00
|
|
|
|
|
|
|
"github.com/issadarkthing/gomu/lyric"
|
2020-06-21 23:47:02 +08:00
|
|
|
)
|
|
|
|
|
2020-06-26 12:54:48 +08:00
|
|
|
type PlayingBar struct {
|
|
|
|
*tview.Frame
|
2021-03-16 00:12:33 +08:00
|
|
|
full int
|
|
|
|
update chan struct{}
|
|
|
|
progress int
|
|
|
|
skip bool
|
|
|
|
text *tview.TextView
|
|
|
|
hasTag bool
|
|
|
|
tag *id3v2.Tag
|
|
|
|
subtitle *lyric.Lyric
|
|
|
|
subtitles []*lyric.Lyric
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:15:39 +08:00
|
|
|
func (p *PlayingBar) help() []string {
|
2020-07-17 15:34:50 +08:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:38:34 +08:00
|
|
|
// Playing bar shows progress of the song and the title of the song
|
2020-07-23 15:15:39 +08:00
|
|
|
func newPlayingBar() *PlayingBar {
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-15 21:38:34 +08:00
|
|
|
textView := tview.NewTextView().SetTextAlign(tview.AlignCenter)
|
|
|
|
frame := tview.NewFrame(textView).SetBorders(1, 1, 1, 1, 1, 1)
|
|
|
|
frame.SetBorder(true).SetTitle(" Now Playing ")
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-17 15:34:50 +08:00
|
|
|
p := &PlayingBar{
|
2021-02-25 19:56:19 +08:00
|
|
|
Frame: frame,
|
|
|
|
text: textView,
|
2021-02-25 16:47:24 +08:00
|
|
|
update: make(chan struct{}),
|
2020-07-17 15:34:50 +08:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
return p
|
2020-06-19 16:22:20 +08:00
|
|
|
}
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-23 13:10:29 +08:00
|
|
|
// Start processing progress bar
|
2020-07-23 15:15:39 +08:00
|
|
|
func (p *PlayingBar) run() error {
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-21 12:22:00 +08:00
|
|
|
for {
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-21 12:22:00 +08:00
|
|
|
// stop progressing if song ends or skipped
|
2021-02-25 16:46:52 +08:00
|
|
|
if p.progress > p.full || p.skip {
|
2020-07-21 12:22:00 +08:00
|
|
|
p.skip = false
|
2021-02-25 16:46:52 +08:00
|
|
|
p.progress = 0
|
2020-07-21 12:22:00 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-03-08 00:29:51 +08:00
|
|
|
if gomu.player.IsPaused() {
|
2021-03-10 13:42:42 +08:00
|
|
|
time.Sleep(1 * time.Second)
|
2021-03-08 00:29:51 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:26:23 +08:00
|
|
|
p.progress = int(gomu.player.GetPosition().Seconds())
|
2020-06-23 18:42:18 +08:00
|
|
|
|
2021-02-27 09:56:13 +08:00
|
|
|
start, err := time.ParseDuration(strconv.Itoa(p.progress) + "s")
|
2020-07-21 12:22:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return tracerr.Wrap(err)
|
|
|
|
}
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-21 12:22:00 +08:00
|
|
|
end, err := time.ParseDuration(strconv.Itoa(p.full) + "s")
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-21 12:22:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return tracerr.Wrap(err)
|
|
|
|
}
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-08-22 14:51:16 +08:00
|
|
|
_, _, width, _ := p.GetInnerRect()
|
2021-02-25 16:46:52 +08:00
|
|
|
progressBar := progresStr(p.progress, p.full, width/2, "█", "━")
|
2020-07-23 13:10:29 +08:00
|
|
|
// our progress bar
|
2021-03-02 00:19:41 +08:00
|
|
|
var lyricText string
|
|
|
|
if p.subtitle != nil {
|
|
|
|
for i := range p.subtitle.Captions {
|
2021-03-15 02:29:41 +08:00
|
|
|
startTime := int32(p.subtitle.Captions[i].Timestamp) + p.subtitle.Offset
|
|
|
|
var endTime int32
|
2021-03-03 16:10:06 +08:00
|
|
|
if i < len(p.subtitle.Captions)-1 {
|
2021-03-15 02:29:41 +08:00
|
|
|
endTime = int32(p.subtitle.Captions[i+1].Timestamp) + p.subtitle.Offset
|
2021-03-03 16:10:06 +08:00
|
|
|
} else {
|
|
|
|
// Here we display the last lyric until the end of song
|
2021-03-15 02:29:41 +08:00
|
|
|
endTime = int32(p.full * 1000)
|
2021-03-03 16:10:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-15 02:29:41 +08:00
|
|
|
currentTime := int32(p.progress * 1000)
|
|
|
|
if currentTime >= startTime && currentTime <= endTime {
|
2021-03-13 01:13:45 +08:00
|
|
|
lyricText = p.subtitle.Captions[i].Text
|
2021-02-23 16:11:44 +08:00
|
|
|
break
|
2021-03-02 00:19:41 +08:00
|
|
|
} else {
|
|
|
|
lyricText = ""
|
2021-02-23 15:46:51 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-21 15:18:19 +08:00
|
|
|
}
|
2021-02-26 15:26:23 +08:00
|
|
|
|
2021-03-02 00:19:41 +08:00
|
|
|
gomu.app.QueueUpdateDraw(func() {
|
2021-03-03 02:13:47 +08:00
|
|
|
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n\n%v",
|
2021-03-02 00:19:41 +08:00
|
|
|
fmtDuration(start),
|
|
|
|
progressBar,
|
|
|
|
fmtDuration(end),
|
|
|
|
lyricText,
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2021-02-26 15:26:23 +08:00
|
|
|
<-time.After(time.Second)
|
2020-07-21 12:22:00 +08:00
|
|
|
}
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-21 12:22:00 +08:00
|
|
|
return nil
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
2020-07-23 13:10:29 +08:00
|
|
|
// Updates song title
|
2020-07-23 15:15:39 +08:00
|
|
|
func (p *PlayingBar) setSongTitle(title string) {
|
2020-06-26 12:54:48 +08:00
|
|
|
p.Clear()
|
2020-08-22 14:41:03 +08:00
|
|
|
titleColor := gomu.colors.title
|
|
|
|
p.AddText(title, true, tview.AlignCenter, titleColor)
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
2020-07-23 13:10:29 +08:00
|
|
|
// Resets progress bar, ready for execution
|
2021-02-22 00:02:08 +08:00
|
|
|
func (p *PlayingBar) newProgress(currentSong *AudioFile, full int) {
|
2020-06-21 23:47:02 +08:00
|
|
|
p.full = full
|
2021-02-25 16:46:52 +08:00
|
|
|
p.progress = 0
|
2021-02-22 00:02:08 +08:00
|
|
|
p.setSongTitle(currentSong.name)
|
2021-03-16 00:12:33 +08:00
|
|
|
p.hasTag = false
|
|
|
|
p.tag = nil
|
|
|
|
p.subtitles = nil
|
|
|
|
p.subtitle = nil
|
2021-02-22 00:02:08 +08:00
|
|
|
|
2021-03-04 13:39:56 +08:00
|
|
|
err := p.loadLyrics(currentSong.path)
|
|
|
|
if err != nil {
|
|
|
|
errorPopup(err)
|
|
|
|
}
|
|
|
|
langLyricFromConfig := gomu.anko.GetString("General.lang_lyric")
|
|
|
|
if langLyricFromConfig == "" {
|
|
|
|
langLyricFromConfig = "en"
|
|
|
|
}
|
|
|
|
if p.hasTag && p.subtitles != nil {
|
2021-03-15 03:03:12 +08:00
|
|
|
// First we check if the lyric language preferred is presented
|
|
|
|
for _, v := range p.subtitles {
|
2021-03-16 00:12:33 +08:00
|
|
|
if strings.Contains(langLyricFromConfig, v.LangExt) {
|
|
|
|
p.subtitle = v
|
|
|
|
if v.IsSync {
|
2021-03-15 03:03:12 +08:00
|
|
|
break
|
|
|
|
}
|
2021-03-02 00:19:41 +08:00
|
|
|
}
|
2021-03-04 13:39:56 +08:00
|
|
|
}
|
2021-03-02 00:19:41 +08:00
|
|
|
|
2021-03-04 13:39:56 +08:00
|
|
|
// Secondly we check if english lyric is available
|
|
|
|
if p.subtitle == nil {
|
2021-03-15 03:03:12 +08:00
|
|
|
for _, v := range p.subtitles {
|
2021-03-16 00:12:33 +08:00
|
|
|
if v.LangExt == "en" {
|
|
|
|
p.subtitle = v
|
|
|
|
if v.IsSync {
|
2021-03-15 03:03:12 +08:00
|
|
|
break
|
|
|
|
}
|
2021-03-02 00:19:41 +08:00
|
|
|
}
|
2021-02-23 16:50:20 +08:00
|
|
|
}
|
2021-02-21 15:18:19 +08:00
|
|
|
}
|
2021-03-04 13:39:56 +08:00
|
|
|
|
|
|
|
// Finally we display the first lyric
|
|
|
|
if p.subtitle == nil {
|
2021-03-16 00:12:33 +08:00
|
|
|
p.subtitle = p.subtitles[0]
|
2021-03-04 13:39:56 +08:00
|
|
|
}
|
2021-02-21 15:18:19 +08:00
|
|
|
}
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
2020-07-23 13:10:29 +08:00
|
|
|
// Sets default title and progress bar
|
2020-07-23 15:15:39 +08:00
|
|
|
func (p *PlayingBar) setDefault() {
|
|
|
|
p.setSongTitle("---------:---------")
|
2020-08-22 14:51:16 +08:00
|
|
|
_, _, width, _ := p.GetInnerRect()
|
2020-07-20 21:48:13 +08:00
|
|
|
text := fmt.Sprintf(
|
2020-08-22 14:51:16 +08:00
|
|
|
"%s ┣%s┫ %s", "00:00", strings.Repeat("━", width/2), "00:00",
|
2020-07-20 21:48:13 +08:00
|
|
|
)
|
|
|
|
p.text.SetText(text)
|
2020-06-22 21:18:36 +08:00
|
|
|
}
|
2020-06-24 12:05:30 +08:00
|
|
|
|
2020-07-23 15:34:56 +08:00
|
|
|
// Skips the current playing song
|
2020-07-23 15:15:39 +08:00
|
|
|
func (p *PlayingBar) stop() {
|
2020-06-24 12:05:30 +08:00
|
|
|
p.skip = true
|
|
|
|
}
|
2021-02-23 15:46:51 +08:00
|
|
|
|
2021-03-04 13:39:56 +08:00
|
|
|
// When switch lyrics, we reload the lyrics from mp3 to reflect changes
|
2021-02-23 15:46:51 +08:00
|
|
|
func (p *PlayingBar) switchLyrics() {
|
2021-02-23 21:34:33 +08:00
|
|
|
|
2021-03-16 00:35:47 +08:00
|
|
|
// err := p.loadLyrics(gomu.player.GetCurrentSong().Path())
|
|
|
|
// if err != nil {
|
|
|
|
// errorPopup(err)
|
|
|
|
// }
|
2021-03-04 13:39:56 +08:00
|
|
|
// no subtitle just ignore
|
2021-02-23 21:34:33 +08:00
|
|
|
if len(p.subtitles) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:39:56 +08:00
|
|
|
// only 1 subtitle, prompt to the user and select this one
|
2021-03-02 00:19:41 +08:00
|
|
|
if len(p.subtitles) == 1 {
|
2021-03-16 00:35:47 +08:00
|
|
|
sync := " unsynchronized"
|
|
|
|
if p.subtitles[0].IsSync {
|
|
|
|
sync = " synchronized"
|
|
|
|
}
|
|
|
|
defaultTimedPopup(" Warning ", p.subtitle.LangExt+sync+" lyric is the only lyric available")
|
2021-03-16 00:12:33 +08:00
|
|
|
p.subtitle = p.subtitles[0]
|
2021-03-02 00:19:41 +08:00
|
|
|
return
|
|
|
|
}
|
2021-03-04 13:39:56 +08:00
|
|
|
|
2021-03-16 00:35:47 +08:00
|
|
|
// more than 1 subtitle, cycle through them and select next
|
2021-02-23 15:46:51 +08:00
|
|
|
var langIndex int
|
2021-03-16 00:12:33 +08:00
|
|
|
for i, v := range p.subtitles {
|
2021-03-16 00:35:47 +08:00
|
|
|
if p.subtitle.LangExt == v.LangExt && p.subtitle.IsSync == v.IsSync {
|
|
|
|
langIndex = i + 1
|
2021-02-23 15:46:51 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if langIndex >= len(p.subtitles) {
|
|
|
|
langIndex = 0
|
|
|
|
}
|
|
|
|
|
2021-03-16 00:12:33 +08:00
|
|
|
p.subtitle = p.subtitles[langIndex]
|
2021-02-23 15:46:51 +08:00
|
|
|
|
2021-03-16 00:35:47 +08:00
|
|
|
sync := " unsynchronized"
|
2021-03-16 00:12:33 +08:00
|
|
|
if p.subtitle.IsSync {
|
2021-03-16 00:35:47 +08:00
|
|
|
sync = " synchronized"
|
2021-03-16 00:12:33 +08:00
|
|
|
}
|
2021-03-16 00:35:47 +08:00
|
|
|
defaultTimedPopup(" Success ", p.subtitle.LangExt+sync+" lyric switched successfully.")
|
2021-02-23 15:46:51 +08:00
|
|
|
}
|
2021-02-25 19:50:52 +08:00
|
|
|
|
|
|
|
func (p *PlayingBar) delayLyric(lyricDelay int) (err error) {
|
|
|
|
|
2021-03-13 01:13:45 +08:00
|
|
|
if p.subtitle != nil {
|
2021-03-15 02:29:41 +08:00
|
|
|
p.subtitle.Offset += int32(lyricDelay)
|
2021-03-16 00:12:33 +08:00
|
|
|
err = embedLyric(gomu.player.GetCurrentSong().Path(), p.subtitle, false)
|
2021-03-13 01:13:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return tracerr.Wrap(err)
|
|
|
|
}
|
2021-02-25 19:50:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-04 13:39:56 +08:00
|
|
|
|
|
|
|
func (p *PlayingBar) loadLyrics(currentSongPath string) error {
|
|
|
|
p.subtitles = nil
|
|
|
|
|
|
|
|
var tag *id3v2.Tag
|
|
|
|
var err error
|
|
|
|
tag, err = id3v2.Open(currentSongPath, id3v2.Options{Parse: true})
|
|
|
|
if err != nil {
|
|
|
|
return tracerr.Wrap(err)
|
|
|
|
}
|
2021-03-07 02:21:42 +08:00
|
|
|
defer tag.Close()
|
|
|
|
|
2021-03-04 13:39:56 +08:00
|
|
|
if tag == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p.hasTag = true
|
|
|
|
p.tag = tag
|
|
|
|
|
2021-03-16 00:12:33 +08:00
|
|
|
// load usltFrames if available
|
2021-03-04 13:39:56 +08:00
|
|
|
usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))
|
|
|
|
for _, f := range usltFrames {
|
|
|
|
uslf, ok := f.(id3v2.UnsynchronisedLyricsFrame)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("USLT error")
|
|
|
|
}
|
|
|
|
res, err := lyric.NewFromLRC(uslf.Lyrics)
|
|
|
|
if err != nil {
|
|
|
|
return tracerr.Wrap(err)
|
|
|
|
}
|
2021-03-16 00:12:33 +08:00
|
|
|
subtitle := &res
|
|
|
|
subtitle.LangExt = uslf.ContentDescriptor
|
|
|
|
subtitle.IsSync = false
|
2021-03-04 13:39:56 +08:00
|
|
|
p.subtitles = append(p.subtitles, subtitle)
|
|
|
|
}
|
2021-03-13 01:13:45 +08:00
|
|
|
|
|
|
|
// loading syltFrames if available
|
|
|
|
syltFrames := tag.GetFrames(tag.CommonID("Synchronised lyrics/text"))
|
|
|
|
|
|
|
|
for _, f := range syltFrames {
|
|
|
|
sylf, ok := f.(id3v2.SynchronisedLyricsFrame)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("sylt error")
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:29:41 +08:00
|
|
|
var caps []lyric.Caption
|
2021-03-13 01:13:45 +08:00
|
|
|
for _, v := range sylf.SynchronizedTexts {
|
|
|
|
var cap lyric.Caption
|
2021-03-15 02:29:41 +08:00
|
|
|
cap.Timestamp = v.Timestamp
|
2021-03-13 01:13:45 +08:00
|
|
|
cap.Text = v.Text
|
2021-03-15 02:29:41 +08:00
|
|
|
caps = append(caps, cap)
|
2021-03-13 01:13:45 +08:00
|
|
|
}
|
|
|
|
|
2021-03-16 00:12:33 +08:00
|
|
|
lyric := &lyric.Lyric{
|
|
|
|
LangExt: sylf.ContentDescriptor,
|
|
|
|
IsSync: true,
|
2021-03-15 02:29:41 +08:00
|
|
|
Captions: caps,
|
2021-03-13 01:13:45 +08:00
|
|
|
}
|
2021-03-16 00:12:33 +08:00
|
|
|
p.subtitles = append(p.subtitles, lyric)
|
2021-03-13 01:13:45 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 13:39:56 +08:00
|
|
|
return nil
|
|
|
|
}
|