gomu/playingbar.go

420 lines
9.5 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 (
2021-04-08 15:14:37 +08:00
"bytes"
"errors"
2020-06-21 23:47:02 +08:00
"fmt"
2021-04-19 16:18:38 +08:00
"image"
2020-06-21 23:47:02 +08:00
"strconv"
"strings"
"sync/atomic"
"syscall"
2020-06-21 23:47:02 +08:00
"time"
"unsafe"
2020-06-19 16:22:20 +08:00
2021-04-08 15:14:37 +08:00
"github.com/disintegration/imaging"
2020-06-21 23:47:02 +08:00
"github.com/rivo/tview"
"github.com/tramhao/id3v2"
2020-07-21 12:22:00 +08:00
"github.com/ztrue/tracerr"
2021-04-08 12:05:48 +08:00
ugo "gitlab.com/diamondburned/ueberzug-go"
2021-04-09 10:33:29 +08:00
"github.com/issadarkthing/gomu/lyric"
"github.com/issadarkthing/gomu/player"
2021-04-08 22:54:22 +08:00
)
2020-06-21 23:47:02 +08:00
2021-03-25 12:01:12 +08:00
// PlayingBar shows song name, progress and lyric
2020-06-26 12:54:48 +08:00
type PlayingBar struct {
*tview.Frame
2021-04-19 16:18:38 +08:00
full int32
update chan struct{}
progress int32
skip bool
text *tview.TextView
hasTag bool
tag *id3v2.Tag
subtitle *lyric.Lyric
subtitles []*lyric.Lyric
albumPhoto *ugo.Image
albumPhotoSource image.Image
colrowPixel int32
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)
2021-03-14 18:20:48 +08:00
textView.SetBackgroundColor(gomu.colors.background)
2021-03-16 11:47:35 +08:00
textView.SetDynamicColors(true)
2021-03-14 18:20:48 +08:00
2020-07-15 21:38:34 +08:00
frame := tview.NewFrame(textView).SetBorders(1, 1, 1, 1, 1, 1)
frame.SetBorder(true).SetTitle(" Now Playing ")
2021-03-14 18:20:48 +08:00
frame.SetBackgroundColor(gomu.colors.background)
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,
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
// 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
progress := p.getProgress()
full := p.getFull()
if progress > full || p.skip {
2020-07-21 12:22:00 +08:00
p.skip = false
p.setProgress(0)
2020-07-21 12:22:00 +08:00
break
}
if gomu.player.IsPaused() {
2021-03-10 13:42:42 +08:00
time.Sleep(1 * time.Second)
continue
}
// p.progress = int(gomu.player.GetPosition().Seconds())
p.setProgress(int(gomu.player.GetPosition().Seconds()))
2020-06-23 18:42:18 +08:00
start, err := time.ParseDuration(strconv.Itoa(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
end, err := time.ParseDuration(strconv.Itoa(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)
}
var width, colrowPixel int
gomu.app.QueueUpdate(func() {
_, _, width, _ = p.GetInnerRect()
cols, rows, windowWidth, windowHeight := getConsoleSize()
rowPixel := windowHeight / rows
colPixel := windowWidth / cols
colrowPixel = rowPixel + colPixel
})
2020-06-21 23:47:02 +08:00
progressBar := progresStr(progress, full, width/2, "█", "━")
if p.getColRowPixel() != colrowPixel {
2021-04-19 16:18:38 +08:00
p.updatePhoto()
p.setColRowPixel(colrowPixel)
2021-04-19 16:18:38 +08:00
}
// our progress bar
var lyricText string
if p.subtitle != nil {
lyricText, err = p.subtitle.GetText(progress)
if err != nil {
return tracerr.Wrap(err)
}
2021-02-21 15:18:19 +08:00
}
2021-02-26 15:26:23 +08:00
gomu.app.QueueUpdateDraw(func() {
2021-03-16 11:47:35 +08:00
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n\n[%s]%v[-]",
fmtDuration(start),
progressBar,
fmtDuration(end),
2021-03-16 11:47:35 +08:00
gomu.colors.subtitle,
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
}
// 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)
2021-04-08 12:05:48 +08:00
2020-06-21 23:47:02 +08:00
}
// Resets progress bar, ready for execution
func (p *PlayingBar) newProgress(currentSong *player.AudioFile, full int) {
2021-03-30 14:01:24 +08:00
p.setFull(full)
p.setProgress(0)
p.hasTag = false
p.tag = nil
p.subtitles = nil
p.subtitle = nil
2021-04-08 15:14:37 +08:00
if p.albumPhoto != nil {
p.albumPhoto.Clear()
p.albumPhoto.Destroy()
p.albumPhoto = nil
2021-04-08 15:14:37 +08:00
}
2021-02-22 00:02:08 +08:00
err := p.loadLyrics(currentSong.Path())
if err != nil {
errorPopup(err)
return
}
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 {
if strings.Contains(langLyricFromConfig, v.LangExt) {
p.subtitle = v
break
}
}
// 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 {
if v.LangExt == "en" {
p.subtitle = v
break
}
}
2021-02-21 15:18:19 +08:00
}
// Finally we display the first lyric
if p.subtitle == nil {
p.subtitle = p.subtitles[0]
}
2021-02-21 15:18:19 +08:00
}
p.setSongTitle(currentSong.Name())
2021-04-08 15:14:37 +08:00
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)
if p.albumPhoto != nil {
p.albumPhoto.Clear()
}
}
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
}
// When switch lyrics, we reload the lyrics from mp3 to reflect changes
func (p *PlayingBar) switchLyrics() {
2021-03-16 00:39:13 +08:00
err := p.loadLyrics(gomu.player.GetCurrentSong().Path())
if err != nil {
errorPopup(err)
return
2021-03-16 00:39:13 +08:00
}
// no subtitle just ignore
if len(p.subtitles) == 0 {
defaultTimedPopup(" Warning ", "No embed lyric found")
p.subtitle = nil
return
}
// only 1 subtitle, prompt to the user and select this one
if len(p.subtitles) == 1 {
p.subtitle = p.subtitles[0]
2021-03-30 14:01:24 +08:00
defaultTimedPopup(" Warning ", p.subtitle.LangExt+" lyric is the only lyric available")
return
}
2021-03-16 00:35:47 +08:00
// more than 1 subtitle, cycle through them and select next
var langIndex int
for i, v := range p.subtitles {
if p.subtitle.LangExt == v.LangExt {
2021-03-16 00:35:47 +08:00
langIndex = i + 1
break
}
}
if langIndex >= len(p.subtitles) {
langIndex = 0
}
p.subtitle = p.subtitles[langIndex]
defaultTimedPopup(" Success ", p.subtitle.LangExt+" lyric switched successfully.")
}
func (p *PlayingBar) delayLyric(lyricDelay int) (err error) {
2021-03-13 01:13:45 +08:00
if p.subtitle != nil {
p.subtitle.Offset -= int32(lyricDelay)
err = embedLyric(gomu.player.GetCurrentSong().Path(), p.subtitle, false)
2021-03-13 01:13:45 +08:00
if err != nil {
return tracerr.Wrap(err)
}
err = p.loadLyrics(gomu.player.GetCurrentSong().Path())
if err != nil {
return tracerr.Wrap(err)
}
for _, v := range p.subtitles {
if strings.Contains(v.LangExt, p.subtitle.LangExt) {
p.subtitle = v
break
}
}
}
return nil
}
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()
if tag == nil {
return nil
}
p.hasTag = true
p.tag = tag
if p.albumPhoto != nil {
p.albumPhoto.Clear()
p.albumPhoto.Destroy()
p.albumPhoto = nil
}
2021-03-13 01:13:45 +08:00
syltFrames := tag.GetFrames(tag.CommonID("Synchronised lyrics/text"))
usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))
2021-03-13 01:13:45 +08:00
for _, f := range syltFrames {
sylf, ok := f.(id3v2.SynchronisedLyricsFrame)
if !ok {
return fmt.Errorf("sylt error")
}
for _, u := range usltFrames {
uslf, ok := u.(id3v2.UnsynchronisedLyricsFrame)
if !ok {
return errors.New("USLT error")
}
if sylf.ContentDescriptor == uslf.ContentDescriptor {
2021-03-25 16:57:44 +08:00
var lyric lyric.Lyric
err := lyric.NewFromLRC(uslf.Lyrics)
if err != nil {
return tracerr.Wrap(err)
}
lyric.SyncedCaptions = sylf.SynchronizedTexts
lyric.LangExt = sylf.ContentDescriptor
p.subtitles = append(p.subtitles, &lyric)
}
2021-03-13 01:13:45 +08:00
}
}
2021-04-08 12:05:48 +08:00
pictures := tag.GetFrames(tag.CommonID("Attached picture"))
for _, f := range pictures {
pic, ok := f.(id3v2.PictureFrame)
if !ok {
return errors.New("picture frame error")
}
// Do something with picture frame.
2021-04-19 16:18:38 +08:00
imgTmp, err := imaging.Decode(bytes.NewReader(pic.Picture))
2021-04-08 15:14:37 +08:00
if err != nil {
return tracerr.Wrap(err)
}
2021-04-19 16:18:38 +08:00
p.albumPhotoSource = imgTmp
p.setColRowPixel(0)
2021-04-08 12:05:48 +08:00
}
2021-04-08 15:14:37 +08:00
return nil
}
func (p *PlayingBar) getProgress() int {
2021-04-19 16:18:38 +08:00
return int(atomic.LoadInt32(&p.progress))
}
func (p *PlayingBar) setProgress(progress int) {
2021-04-19 16:18:38 +08:00
atomic.StoreInt32(&p.progress, int32(progress))
}
2021-03-30 14:01:24 +08:00
func (p *PlayingBar) getFull() int {
2021-04-19 16:18:38 +08:00
return int(atomic.LoadInt32(&p.full))
2021-03-30 14:01:24 +08:00
}
func (p *PlayingBar) setFull(full int) {
2021-04-19 16:18:38 +08:00
atomic.StoreInt32(&p.full, int32(full))
}
func (p *PlayingBar) getColRowPixel() int {
return int(atomic.LoadInt32(&p.colrowPixel))
2021-04-19 16:18:38 +08:00
}
func (p *PlayingBar) setColRowPixel(colrowPixel int) {
atomic.StoreInt32(&p.colrowPixel, int32(colrowPixel))
}
2021-04-08 22:54:22 +08:00
func getConsoleSize() (int, int, int, int) {
var sz struct {
rows uint16
cols uint16
xpixels uint16
ypixels uint16
}
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdout), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
return int(sz.cols), int(sz.rows), int(sz.xpixels), int(sz.ypixels)
}
2021-04-19 16:18:38 +08:00
// updatePhoto finish two tasks: 1. resize photo based on room left for photo
// 2. register photo in the correct position
2021-04-19 16:18:38 +08:00
func (p *PlayingBar) updatePhoto() {
// Put the whole block in goroutine, in order not to block the whole apps
// also to avoid data race by adding QueueUpdateDraw
2021-04-19 16:18:38 +08:00
go gomu.app.QueueUpdateDraw(func() {
if p.albumPhotoSource == nil {
return
}
if p.albumPhoto != nil {
p.albumPhoto.Clear()
p.albumPhoto.Destroy()
p.albumPhoto = nil
2021-04-19 16:18:38 +08:00
}
x, y, width, height := gomu.queue.GetInnerRect()
2021-04-19 16:18:38 +08:00
cols, rows, windowWidth, windowHeight := getConsoleSize()
colPixel := windowWidth / cols
rowPixel := windowHeight / rows
imageWidth := width * colPixel / 3
// resize the photo according to space left for x and y axis
dstImage := imaging.Resize(p.albumPhotoSource, imageWidth, 0, imaging.Lanczos)
2021-04-19 16:18:38 +08:00
var err error
positionX := x*colPixel + width*colPixel - dstImage.Rect.Dx()
positionY := y*rowPixel + height*rowPixel - dstImage.Rect.Dy()
// register new image
2021-04-19 16:18:38 +08:00
p.albumPhoto, err = ugo.NewImage(dstImage, positionX, positionY)
if err != nil {
errorPopup(err)
}
p.albumPhoto.Show()
})
}