From c71dfb5905090a9591f24aba71f1f2b67f25d1f6 Mon Sep 17 00:00:00 2001 From: tramhao Date: Thu, 25 Feb 2021 19:50:52 +0800 Subject: [PATCH] 1. Fix a bug when embedLyric, need to delete the old frame with same language id and content descriptor 2. Add a function to adjust the delay time of lyrics --- README.md | 2 ++ command.go | 14 ++++++++++++++ playingbar.go | 52 +++++++++++++++++++++++++++++++-------------------- queue.go | 4 ++++ utils.go | 14 +++++++++++++- 5 files changed, 65 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a0cb600..825b36b 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,8 @@ Each panel has it's own additional keybinding. To view the available keybinding | z | toggle loop | | s | shuffle | | / | find in queue | +| t | lyric delay increase 1 second | +| r | lyric delay decrease 1 second | ### Scripting diff --git a/command.go b/command.go index 1f722ef..2fca3a1 100644 --- a/command.go +++ b/command.go @@ -404,6 +404,20 @@ func (c Command) defineCommands() { } }) + c.define("lyric_delay_increase", func() { + err := gomu.playingBar.delayLyric(1000) + if err != nil { + logError(err) + } + }) + + c.define("lyric_delay_decrease", func() { + err := gomu.playingBar.delayLyric(-1000) + if err != nil { + logError(err) + } + }) + for name, cmd := range c.commands { err := gomu.anko.Define(name, cmd) if err != nil { diff --git a/playingbar.go b/playingbar.go index f87f94d..376bbc1 100644 --- a/playingbar.go +++ b/playingbar.go @@ -17,16 +17,17 @@ import ( type PlayingBar struct { *tview.Frame - full int - progress chan int - _progress int - skip bool - text *tview.TextView - hasTag bool - tag *id3v2.Tag - subtitle *subtitles.Subtitle - subtitles []*gomuSubtitle - langLyric string + full int + progress chan int + _progress int + skip bool + text *tview.TextView + hasTag bool + tag *id3v2.Tag + subtitle *subtitles.Subtitle + subtitles []*gomuSubtitle + langConfigFromConfig string + langLyricCurrentPlaying string } type gomuSubtitle struct { @@ -87,8 +88,9 @@ func (p *PlayingBar) run() error { 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.langLyric, p.subtitles[i].langExt) { + if strings.Contains(p.langConfigFromConfig, p.subtitles[i].langExt) { p.subtitle = p.subtitles[i].subtitle + p.langLyricCurrentPlaying = p.subtitles[i].langExt break } } @@ -96,9 +98,9 @@ func (p *PlayingBar) run() error { // Secondly we check if english lyric is available if p.subtitle == nil { for i := range p.subtitles { - if strings.Contains(p.langLyric, "en") { + if p.subtitles[i].langExt == "en" { p.subtitle = p.subtitles[i].subtitle - p.langLyric = "en" + p.langLyricCurrentPlaying = "en" break } } @@ -107,7 +109,7 @@ func (p *PlayingBar) run() error { // Finally we display the first lyric if p.subtitle == nil { p.subtitle = p.subtitles[0].subtitle - p.langLyric = p.subtitles[0].langExt + p.langLyricCurrentPlaying = p.subtitles[0].langExt } var lyricText string @@ -185,9 +187,9 @@ func (p *PlayingBar) newProgress(currentSong *AudioFile, full int) { subtitle: &res, } p.subtitles = append(p.subtitles, subtitle) - p.langLyric = gomu.anko.GetString("General.lang_lyric") - if p.langLyric == "" { - p.langLyric = "en" + p.langConfigFromConfig = gomu.anko.GetString("General.lang_lyric") + if p.langConfigFromConfig == "" { + p.langConfigFromConfig = "en" } } } @@ -217,7 +219,7 @@ func (p *PlayingBar) switchLyrics() { var langIndex int for i := range p.subtitles { - if p.subtitles[i].langExt == p.langLyric { + if p.subtitles[i].langExt == p.langConfigFromConfig { langIndex = i + 1 break } @@ -227,7 +229,17 @@ func (p *PlayingBar) switchLyrics() { langIndex = 0 } - p.langLyric = p.subtitles[langIndex].langExt - defaultTimedPopup(" Success ", p.langLyric+" lyric switched successfully.") + p.langConfigFromConfig = p.subtitles[langIndex].langExt + defaultTimedPopup(" Success ", p.langConfigFromConfig+" lyric switched successfully.") } + +func (p *PlayingBar) delayLyric(lyricDelay int) (err error) { + + p.subtitle.ResyncSubs(lyricDelay) + err = embedLyric(gomu.player.currentSong.path, p.subtitle.AsSRT(), p.langLyricCurrentPlaying) + if err != nil { + return tracerr.Wrap(err) + } + return nil +} diff --git a/queue.go b/queue.go index 5a995f4..8dd8e9a 100644 --- a/queue.go +++ b/queue.go @@ -330,6 +330,8 @@ func (q *Queue) help() []string { "z toggle loop", "s shuffle", "/ find in queue", + "t lyric delay increase 1 second", + "r lyric delay decrease 1 second", } } @@ -390,6 +392,8 @@ func newQueue() *Queue { 'z': "toggle_loop", 's': "shuffle_queue", '/': "queue_search", + 't': "lyric_delay_increase", + 'r': "lyric_delay_decrease", } for key, cmd := range cmds { diff --git a/utils.go b/utils.go index e767fea..99234c2 100644 --- a/utils.go +++ b/utils.go @@ -249,7 +249,19 @@ func embedLyric(songPath string, lyricContent string, usltContentDescriptor stri return tracerr.Wrap(err) } defer tag.Close() - + usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription")) + tag.DeleteFrames(tag.CommonID("Unsynchronised lyrics/text transcription")) + // We delete the lyric frame with same language by delete all and add others back + for _, f := range usltFrames { + uslf, ok := f.(id3v2.UnsynchronisedLyricsFrame) + if !ok { + die(errors.New("USLT error!")) + } + if uslf.ContentDescriptor == usltContentDescriptor { + continue + } + tag.AddUnsynchronisedLyricsFrame(uslf) + } tag.AddUnsynchronisedLyricsFrame(id3v2.UnsynchronisedLyricsFrame{ Encoding: id3v2.EncodingUTF8, Language: "eng",