From 3a815dcf23323d2f1d0b830c5cb5037d09156677 Mon Sep 17 00:00:00 2001 From: Simon Garrelou Date: Sat, 10 Dec 2022 22:35:46 +0100 Subject: Add shuffle + fix deadlocks --- music/playqueue.go | 7 +++++++ src/app.go | 12 ++++++++++++ src/footer.go | 4 ++-- src/keybinds.go | 25 +++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/music/playqueue.go b/music/playqueue.go index 235007e..6bdd5b1 100644 --- a/music/playqueue.go +++ b/music/playqueue.go @@ -57,6 +57,10 @@ func (q *Queue) Clear() { } func (q *Queue) PlaySong(s *subsonic.Child) error { + if q.isPaused { + q.TogglePause() + } + rc, err := Download2(q.sub, s.ID) if err != nil { return err @@ -125,6 +129,9 @@ func (q *Queue) Next() error { } func (q *Queue) Stop() { + if q.isPaused { + q.TogglePause() + } speaker.Clear() } diff --git a/src/app.go b/src/app.go index ee1b342..b998a50 100644 --- a/src/app.go +++ b/src/app.go @@ -2,7 +2,9 @@ package src import ( "fmt" + "math/rand" "os" + "time" "git.sixfoisneuf.fr/termsonic/music" "github.com/delucks/go-subsonic" @@ -158,3 +160,13 @@ func (a *app) switchToPage(name string) { a.updateFooter() } + +func randomize(t []*subsonic.Child) []*subsonic.Child { + t2 := make([]*subsonic.Child, len(t)) + copy(t2, t) + + rand.Seed(time.Now().UnixNano()) + rand.Shuffle(len(t2), func(i, j int) { t2[i], t2[j] = t2[j], t2[i] }) + + return t2 +} diff --git a/src/footer.go b/src/footer.go index ab6181d..f75f954 100644 --- a/src/footer.go +++ b/src/footer.go @@ -9,10 +9,10 @@ func (a *app) updateFooter() { a.footer.SetText("[blue]l:[yellow] Next song [blue]p:[yellow] Toggle pause [blue]e:[yellow] Play song last [blue]n:[yellow] Play song next") } case "playqueue": - a.footer.SetText("[blue]l:[yellow] Next song [blue]p:[yellow] Toggle pause [blue]d:[yellow] Remove [blue]j:[yellow] Move up [blue]k:[yellow] Move down") + a.footer.SetText("[blue]l:[yellow] Next song [blue]p:[yellow] Toggle pause [blue]d:[yellow] Remove [blue]j:[yellow] Move up [blue]k:[yellow] Move down [blue]r:[yellow] Shuffle") case "playlists": if a.tv.GetFocus() == a.playlistsList { - a.footer.SetText("[blue]l:[yellow] Next song [blue]p:[yellow] Toggle pause [blue]e:[yellow] Play playlist last [blue]n:[yellow] Play playlist next") + a.footer.SetText("[blue]l:[yellow] Next song [blue]p:[yellow] Toggle pause [blue]e:[yellow] Play playlist last [blue]n:[yellow] Play playlist next [blue]r:[yellow] Random") } else if a.tv.GetFocus() == a.playlistSongs { a.footer.SetText("[blue]l:[yellow] Next song [blue]p:[yellow] Toggle pause [blue]e:[yellow] Play song last [blue]n:[yellow] Play song next") } diff --git a/src/keybinds.go b/src/keybinds.go index 74f7953..e3b80a1 100644 --- a/src/keybinds.go +++ b/src/keybinds.go @@ -51,6 +51,16 @@ func (a *app) setupMusicControlKeys(p *tview.Box) { a.playQueueList.SetCurrentItem(sel - 1) return nil + } else if event.Rune() == 'r' { + a.playQueue.Stop() + songs := a.playQueue.GetSongs() + a.playQueue.Clear() + for _, s := range randomize(songs) { + a.playQueue.Append(s) + } + + a.playQueue.Play() + a.updatePageQueue() } } @@ -146,6 +156,21 @@ func (a *app) setupMusicControlKeys(p *tview.Box) { a.playQueue.Insert(1, pl.Entry[i]) } + a.updatePageQueue() + } else if event.Rune() == 'r' { + sel := a.playlistsList.GetCurrentItem() + pl, err := a.sub.GetPlaylist(a.allPlaylists[sel].ID) + if err != nil { + a.alert("Error: %v", err) + return nil + } + + a.playQueue.Clear() + for _, s := range randomize(pl.Entry) { + a.playQueue.Append(s) + } + a.playQueue.Play() + a.updatePageQueue() } } -- cgit v1.2.3