From c954c13e16a768e2cb89d7ca6dcc433c11861a34 Mon Sep 17 00:00:00 2001 From: tramhao Date: Tue, 2 Mar 2021 00:19:41 +0800 Subject: [PATCH] 1. Change the logic of switch lyrics 2. Remove a module level variable playringBar.langConfigFromConfig --- lyric/lrc.go | 9 ++-- playingbar.go | 126 +++++++++++++++++++++++--------------------------- start.go | 5 +- 3 files changed, 66 insertions(+), 74 deletions(-) diff --git a/lyric/lrc.go b/lyric/lrc.go index 1c0db74..43c117e 100644 --- a/lyric/lrc.go +++ b/lyric/lrc.go @@ -76,10 +76,11 @@ func NewFromLRC(s string) (res subtitles.Subtitle, err error) { s2 := r2.ReplaceAllString(lines[i], "$1") s3 := strings.Trim(s2, "\r ") o.Text = append(o.Text, s3) - if len(o.Text) > 0 { - res.Captions = append(res.Captions, o) - outSeq++ - } + // Seems that empty lines are useful and shouldn't be deleted + // if len(o.Text) > 0 { + res.Captions = append(res.Captions, o) + outSeq++ + // } } return } diff --git a/playingbar.go b/playingbar.go index a6d90f2..012a3f2 100644 --- a/playingbar.go +++ b/playingbar.go @@ -26,7 +26,6 @@ type PlayingBar struct { tag *id3v2.Tag subtitle *subtitles.Subtitle subtitles []*gomuSubtitle - langConfigFromConfig string langLyricCurrentPlaying string } @@ -69,7 +68,6 @@ func (p *PlayingBar) run() error { p.progress = int(gomu.player.GetPosition().Seconds()) - start, err := time.ParseDuration(strconv.Itoa(p.progress) + "s") if err != nil { return tracerr.Wrap(err) @@ -84,71 +82,31 @@ func (p *PlayingBar) run() error { _, _, width, _ := p.GetInnerRect() progressBar := progresStr(p.progress, p.full, width/2, "█", "━") // our progress bar - p.subtitle = nil - if p.hasTag && p.subtitles != nil { - for i := range p.subtitles { - // First we check if the lyric language prefered is presented - if strings.Contains(p.langConfigFromConfig, p.subtitles[i].langExt) { - p.subtitle = p.subtitles[i].subtitle - p.langLyricCurrentPlaying = p.subtitles[i].langExt + var lyricText string + if p.subtitle != nil { + for i := range p.subtitle.Captions { + startTime := p.subtitle.Captions[i].Start + endTime := p.subtitle.Captions[i].End + currentTime := time.Date(0, 1, 1, 0, 0, p.progress, 0, time.UTC) + if currentTime.After(startTime.Add(-1*time.Second)) && currentTime.Before(endTime) { + lyricText = strings.Join(p.subtitle.Captions[i].Text, " ") break + } else { + lyricText = "" } } - - // Secondly we check if english lyric is available - if p.subtitle == nil { - for i := range p.subtitles { - if p.subtitles[i].langExt == "en" { - p.subtitle = p.subtitles[i].subtitle - p.langLyricCurrentPlaying = "en" - break - } - } - } - - // Finally we display the first lyric - if p.subtitle == nil { - p.subtitle = p.subtitles[0].subtitle - p.langLyricCurrentPlaying = p.subtitles[0].langExt - } - - var lyricText string - if p.subtitle != nil { - for i := range p.subtitle.Captions { - startTime := p.subtitle.Captions[i].Start - endTime := p.subtitle.Captions[i].End - currentTime := time.Date(0, 1, 1, 0, 0, p.progress, 0, time.UTC) - if currentTime.After(startTime.Add(-1*time.Second)) && currentTime.Before(endTime) { - lyricText = strings.Join(p.subtitle.Captions[i].Text, " ") - break - } else { - lyricText = "" - } - } - } - - gomu.app.QueueUpdateDraw(func() { - p.text.Clear() - p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n%v", - fmtDuration(start), - progressBar, - fmtDuration(end), - lyricText, - )) - }) - - } else { - - gomu.app.QueueUpdateDraw(func() { - p.text.Clear() - p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s", - fmtDuration(start), - progressBar, - fmtDuration(end), - )) - }) } + gomu.app.QueueUpdateDraw(func() { + p.text.Clear() + p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n%v", + fmtDuration(start), + progressBar, + fmtDuration(end), + lyricText, + )) + }) + <-time.After(time.Second) } @@ -198,9 +156,36 @@ func (p *PlayingBar) newProgress(currentSong *AudioFile, full int) { subtitle: &res, } p.subtitles = append(p.subtitles, subtitle) - p.langConfigFromConfig = gomu.anko.GetString("General.lang_lyric") - if p.langConfigFromConfig == "" { - p.langConfigFromConfig = "en" + langLyricFromConfig := gomu.anko.GetString("General.lang_lyric") + if langLyricFromConfig == "" { + langLyricFromConfig = "en" + } + if p.hasTag && p.subtitles != nil { + // First we check if the lyric language prefered is presented + for i := range p.subtitles { + if strings.Contains(langLyricFromConfig, p.subtitles[i].langExt) { + p.subtitle = p.subtitles[i].subtitle + p.langLyricCurrentPlaying = p.subtitles[i].langExt + break + } + } + + // Secondly we check if english lyric is available + if p.subtitle == nil { + for i := range p.subtitles { + if p.subtitles[i].langExt == "en" { + p.subtitle = p.subtitles[i].subtitle + p.langLyricCurrentPlaying = "en" + break + } + } + } + + // Finally we display the first lyric + if p.subtitle == nil { + p.subtitle = p.subtitles[0].subtitle + p.langLyricCurrentPlaying = p.subtitles[0].langExt + } } } } @@ -228,9 +213,13 @@ func (p *PlayingBar) switchLyrics() { return } + if len(p.subtitles) == 1 { + defaultTimedPopup(" Warning ", p.langLyricCurrentPlaying+" lyric is the only lyric available") + return + } var langIndex int for i := range p.subtitles { - if p.subtitles[i].langExt == p.langConfigFromConfig { + if p.subtitles[i].langExt == p.langLyricCurrentPlaying { langIndex = i + 1 break } @@ -240,8 +229,9 @@ func (p *PlayingBar) switchLyrics() { langIndex = 0 } - p.langConfigFromConfig = p.subtitles[langIndex].langExt - defaultTimedPopup(" Success ", p.langConfigFromConfig+" lyric switched successfully.") + p.langLyricCurrentPlaying = p.subtitles[langIndex].langExt + p.subtitle = p.subtitles[langIndex].subtitle + defaultTimedPopup(" Success ", p.langLyricCurrentPlaying+" lyric switched successfully.") } diff --git a/start.go b/start.go index d62c502..3ee6509 100644 --- a/start.go +++ b/start.go @@ -206,8 +206,9 @@ module General { # to another instance from this list: # https://github.com/iv-org/documentation/blob/master/Invidious-Instances.md invidious_instance = "https://vid.puffyan.us" - # Prefered language for lyrics to be embeded, if not available, english version will be embeded. - # Available tags: en,el,ko,es,th,vi,zh-Hans,zh-Hant but only set 1 tag is working + # Prefered language for lyrics to be displayed, if not available, english version + # will be displayed. + # Available tags: en,el,ko,es,th,vi,zh-Hans,zh-Hant, and can be separated with comma. # find more tags: youtube-dl --skip-download --list-subs "url" lang_lyric = "en" }