gomu/playingbar.go

234 lines
4.9 KiB
Go
Raw Normal View History

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 (
"errors"
2020-06-21 23:47:02 +08:00
"fmt"
"strconv"
"strings"
"time"
2020-06-19 16:22:20 +08:00
2021-02-21 15:18:19 +08:00
"github.com/bogem/id3v2"
"github.com/martinlindhe/subtitles"
2020-06-21 23:47:02 +08:00
"github.com/rivo/tview"
2020-07-21 12:22:00 +08:00
"github.com/ztrue/tracerr"
2020-06-21 23:47:02 +08:00
)
2020-06-26 12:54:48 +08:00
type PlayingBar struct {
*tview.Frame
full int
progress chan int
2020-06-21 23:47:02 +08:00
_progress int
2020-06-24 12:05:30 +08:00
skip bool
2020-06-26 12:54:48 +08:00
text *tview.TextView
2021-02-21 15:18:19 +08:00
hasTag bool
tag *id3v2.Tag
2021-02-21 23:08:16 +08:00
subtitle *subtitles.Subtitle
subtitles []*gomuSubtitle
langLyric string
}
type gomuSubtitle struct {
langExt string
subtitle *subtitles.Subtitle
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{
Frame: frame,
text: textView,
progress: make(chan int),
}
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
// 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
if p._progress > p.full || p.skip {
p.skip = false
p._progress = 0
break
}
p._progress += <-p.progress
2020-06-23 18:42:18 +08:00
p.text.Clear()
2020-07-21 12:22:00 +08:00
start, err := time.ParseDuration(strconv.Itoa(p._progress) + "s")
2020-06-23 18:42:18 +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-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()
progressBar := progresStr(p._progress, p.full, width/2, "█", "━")
// our progress bar
if p.hasTag && p.subtitles != nil {
for i := range p.subtitles {
2021-02-23 17:37:27 +08:00
// First we check if the lyric language prefered is presented
if strings.Contains(p.langLyric, p.subtitles[i].langExt) {
p.subtitle = p.subtitles[i].subtitle
break
}
}
2021-02-23 17:37:27 +08:00
// Secondly we check if english lyric is available
if p.subtitle == nil {
for i := range p.subtitles {
if strings.Contains(p.langLyric, "en") {
p.subtitle = p.subtitles[i].subtitle
p.langLyric = "en"
break
}
}
}
2021-02-24 10:32:29 +08:00
// Finally we display the first lyric
2021-02-23 17:37:27 +08:00
if p.subtitle == nil {
p.subtitle = p.subtitles[0].subtitle
p.langLyric = p.subtitles[0].langExt
}
2021-02-21 23:08:16 +08:00
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)
2021-02-23 17:12:02 +08:00
if currentTime.After(startTime.Add(-1*time.Second)) && currentTime.Before(endTime) {
lyricText = strings.Join(p.subtitle.Captions[i].Text, " ")
break
} else {
lyricText = ""
}
2021-02-21 23:08:16 +08:00
}
}
2021-02-21 23:08:16 +08:00
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n%v",
2021-02-21 15:18:19 +08:00
fmtDuration(start),
progressBar,
fmtDuration(end),
2021-02-21 23:08:16 +08:00
lyricText,
2021-02-21 15:18:19 +08:00
))
} else {
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s",
fmtDuration(start),
progressBar,
fmtDuration(end),
))
}
2021-02-19 15:07:44 +08:00
gomu.app.Draw()
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
}
// 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
}
// 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
p._progress = 0
2021-02-22 00:02:08 +08:00
p.setSongTitle(currentSong.name)
p.hasTag = false
p.subtitles = nil
2021-02-22 00:02:08 +08:00
p.tag = nil
2021-02-21 15:18:19 +08:00
var tag *id3v2.Tag
var err error
2021-02-22 00:02:08 +08:00
tag, err = id3v2.Open(currentSong.path, id3v2.Options{Parse: true})
2021-02-21 15:18:19 +08:00
if tag == nil || err != nil {
logError(err)
} else {
p.hasTag = true
p.tag = tag
usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))
for _, f := range usltFrames {
uslf, ok := f.(id3v2.UnsynchronisedLyricsFrame)
if !ok {
die(errors.New("USLT error!"))
2021-02-21 15:18:19 +08:00
}
res, err := subtitles.NewFromSRT(uslf.Lyrics)
if err != nil {
logError(err)
}
subtitle := &gomuSubtitle{
langExt: uslf.ContentDescriptor,
subtitle: &res,
}
p.subtitles = append(p.subtitles, subtitle)
p.langLyric = gomu.anko.GetString("General.lang_lyric")
if p.langLyric == "" {
p.langLyric = "en"
}
2021-02-21 15:18:19 +08:00
}
}
defer tag.Close()
2020-06-21 23:47:02 +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-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
}
func (p *PlayingBar) switchLyrics() {
if len(p.subtitles) == 0 {
return
}
var langIndex int
for i := range p.subtitles {
if p.subtitles[i].langExt == p.langLyric {
langIndex = i + 1
break
}
}
if langIndex >= len(p.subtitles) {
langIndex = 0
}
p.langLyric = p.subtitles[langIndex].langExt
defaultTimedPopup(" Success ", p.langLyric+" lyric switched successfully.")
}