Skip to content

Commit

Permalink
refactor: extract audiofile.go from playlist.go
Browse files Browse the repository at this point in the history
  • Loading branch information
tramhao committed Apr 30, 2021
1 parent 05bf59e commit dcb8fda
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 239 deletions.
28 changes: 14 additions & 14 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (c Command) defineCommands() {

c.define("delete_playlist", func() {
audioFile := gomu.playlist.getCurrentFile()
if audioFile.isAudioFile {
if audioFile.IsAudioFile() {
return
}
err := confirmDeleteAllPopup(audioFile.node)
err := confirmDeleteAllPopup(audioFile.Node())
if err != nil {
errorPopup(err)
}
Expand All @@ -58,7 +58,7 @@ func (c Command) defineCommands() {
c.define("delete_file", func() {
audioFile := gomu.playlist.getCurrentFile()
// prevent from deleting a directory
if !audioFile.isAudioFile {
if !audioFile.IsAudioFile() {
return
}

Expand All @@ -81,8 +81,8 @@ func (c Command) defineCommands() {
}
// this ensures it downloads to
// the correct dir
if audioFile.isAudioFile {
downloadMusicPopup(audioFile.parent)
if audioFile.IsAudioFile() {
downloadMusicPopup(audioFile.ParentNode())
} else {
downloadMusicPopup(currNode)
}
Expand All @@ -91,7 +91,7 @@ func (c Command) defineCommands() {
c.define("add_queue", func() {
audioFile := gomu.playlist.getCurrentFile()
currNode := gomu.playlist.GetCurrentNode()
if audioFile.isAudioFile {
if audioFile.IsAudioFile() {
gomu.queue.pushFront(audioFile)
if len(gomu.queue.items) == 1 && !gomu.player.IsRunning() {
err := gomu.queue.playQueue()
Expand All @@ -111,8 +111,8 @@ func (c Command) defineCommands() {
// close the node's parent
// remove the color of the node

if audioFile.isAudioFile {
parent := audioFile.parent
if audioFile.IsAudioFile() {
parent := audioFile.ParentNode()
gomu.playlist.setHighlight(parent)
parent.SetExpanded(false)
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func (c Command) defineCommands() {
files := make([]string, len(gomu.playlist.getAudioFiles()))

for i, file := range gomu.playlist.getAudioFiles() {
files[i] = file.name
files[i] = file.Name()
}

searchPopup("Search", files, func(text string) {
Expand All @@ -175,7 +175,7 @@ func (c Command) defineCommands() {
logError(err)
}

gomu.playlist.setHighlight(audio.node)
gomu.playlist.setHighlight(audio.Node())
gomu.playlist.refresh()
})
})
Expand Down Expand Up @@ -245,14 +245,14 @@ func (c Command) defineCommands() {

audios := make([]string, 0, len(queue.items))
for _, file := range queue.items {
audios = append(audios, file.name)
audios = append(audios, file.Name())
}

searchPopup("Songs", audios, func(selected string) {

index := 0
for i, v := range queue.items {
if v.name == selected {
if v.Name() == selected {
index = i
}
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func (c Command) defineCommands() {

var wg sync.WaitGroup
wg.Add(1)
if audioFile.isAudioFile {
if audioFile.IsAudioFile() {
go func() {
err := lyricPopup(lang, audioFile, &wg)
if err != nil {
Expand All @@ -442,7 +442,7 @@ func (c Command) defineCommands() {

var wg sync.WaitGroup
wg.Add(1)
if audioFile.isAudioFile {
if audioFile.IsAudioFile() {
go func() {
err := lyricPopup(lang, audioFile, &wg)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/PuerkitoBio/goquery v1.6.1 // indirect
github.com/antchfx/htmlquery v1.2.3 // indirect
github.com/antchfx/xmlquery v1.3.4 // indirect
github.com/bogem/id3v2 v1.2.0 // indirect
github.com/bogem/id3v2 v1.2.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/disintegration/imaging v1.6.2
github.com/faiface/beep v1.0.2
Expand All @@ -29,7 +29,6 @@ require (
github.com/tramhao/id3v2 v1.2.1
github.com/ztrue/tracerr v0.3.0
gitlab.com/diamondburned/ueberzug-go v0.0.0-20190521043425-7c15a5f63b06
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/exp v0.0.0-20201229011636-eab1b5eb1a03 // indirect
golang.org/x/image v0.0.0-20201208152932-35266b937fa6 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ gitlab.com/diamondburned/ueberzug-go v0.0.0-20190521043425-7c15a5f63b06/go.mod h
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down
152 changes: 152 additions & 0 deletions player/audiofile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright (C) 2020 Raziman

package player

import (
"errors"
"fmt"
"sort"
"time"

"github.com/rivo/tview"
"github.com/tramhao/id3v2"
"github.com/ztrue/tracerr"
)

var _ Audio = (*AudioFile)(nil)

// AudioFile represents directories and mp3 files
// isAudioFile equals to false if it is a directory
type AudioFile struct {
name string
path string
isAudioFile bool
length time.Duration
node *tview.TreeNode
parent *tview.TreeNode
}

// NewAudioFile return a new instance of Audiofile
func NewAudioFile() *AudioFile {
return &AudioFile{}
}

// Name return the name of AudioFile
func (a *AudioFile) Name() string {
return a.name
}

// SetName set the name of AudioFile
func (a *AudioFile) SetName(name string) {
if name == "" {
return
}
a.name = name
}

// Path return the path of AudioFile
func (a *AudioFile) Path() string {
return a.path
}

// SetPath return the path of AudioFile
func (a *AudioFile) SetPath(path string) {
a.path = path
}

// IsAudioFile check if the file is song or directory
func (a *AudioFile) IsAudioFile() bool {
return a.isAudioFile
}

// SetIsAudioFile check if the file is song or directory
func (a *AudioFile) SetIsAudioFile(isAudioFile bool) {
a.isAudioFile = isAudioFile
}

// Len return the length of AudioFile
func (a *AudioFile) Len() time.Duration {
return a.length
}

// SetLen set the length of AudioFile
func (a *AudioFile) SetLen(length time.Duration) {
a.length = length
}

// Parent return the parent directory of AudioFile
func (a *AudioFile) Parent() *AudioFile {
if a.parent == nil {
return nil
}
return a.parent.GetReference().(*AudioFile)
}

// SetParentNode return the parent directory of AudioFile
func (a *AudioFile) SetParentNode(parentNode *tview.TreeNode) {
if parentNode == nil {
return
}
a.parent = parentNode
}

// ParentNode return the parent node of AudioFile
func (a *AudioFile) ParentNode() *tview.TreeNode {
if a.parent == nil {
return nil
}
return a.parent
}

// Node return the current node of AudioFile
func (a *AudioFile) Node() *tview.TreeNode {
if a.node == nil {
return nil
}
return a.node
}

// SetNode return the current node of AudioFile
func (a *AudioFile) SetNode(node *tview.TreeNode) {
a.node = node
}

// String return the string of AudioFile
func (a *AudioFile) String() string {
if a == nil {
return "nil"
}
return fmt.Sprintf("%#v", a)
}

// LoadTagMap will load from tag and return a map of langExt to lyrics
func (a *AudioFile) LoadTagMap() (tag *id3v2.Tag, popupLyricMap map[string]string, options []string, err error) {

popupLyricMap = make(map[string]string)

if a.isAudioFile {
tag, err = id3v2.Open(a.path, id3v2.Options{Parse: true})
if err != nil {
return nil, nil, nil, tracerr.Wrap(err)
}
defer tag.Close()
} else {
return nil, nil, nil, fmt.Errorf("not an audio file")
}
usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))

for _, f := range usltFrames {
uslf, ok := f.(id3v2.UnsynchronisedLyricsFrame)
if !ok {
return nil, nil, nil, errors.New("USLT error")
}
res := uslf.Lyrics
popupLyricMap[uslf.ContentDescriptor] = res
}
for option := range popupLyricMap {
options = append(options, option)
}
sort.Strings(options)

return tag, popupLyricMap, options, err
}
7 changes: 4 additions & 3 deletions playingbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ugo "gitlab.com/diamondburned/ueberzug-go"

"github.com/issadarkthing/gomu/lyric"
"github.com/issadarkthing/gomu/player"
)

// PlayingBar shows song name, progress and lyric
Expand Down Expand Up @@ -145,7 +146,7 @@ func (p *PlayingBar) setSongTitle(title string) {
}

// Resets progress bar, ready for execution
func (p *PlayingBar) newProgress(currentSong *AudioFile, full int) {
func (p *PlayingBar) newProgress(currentSong *player.AudioFile, full int) {
p.setFull(full)
p.setProgress(0)
p.hasTag = false
Expand All @@ -158,7 +159,7 @@ func (p *PlayingBar) newProgress(currentSong *AudioFile, full int) {
p.albumPhoto = nil
}

err := p.loadLyrics(currentSong.path)
err := p.loadLyrics(currentSong.Path())
if err != nil {
errorPopup(err)
return
Expand Down Expand Up @@ -191,7 +192,7 @@ func (p *PlayingBar) newProgress(currentSong *AudioFile, full int) {
p.subtitle = p.subtitles[0]
}
}
p.setSongTitle(currentSong.name)
p.setSongTitle(currentSong.Name())

}

Expand Down
Loading

0 comments on commit dcb8fda

Please sign in to comment.