aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Garrelou <simon.garrelou@gmail.com>2022-12-10 22:35:46 +0100
committerSimon Garrelou <simon.garrelou@gmail.com>2022-12-10 22:35:46 +0100
commit3a815dcf23323d2f1d0b830c5cb5037d09156677 (patch)
tree2aa9739ad934fe287b13e4c1f0a9d6b5bd0e4ef6
parent08182123b8165c8c8dc55d1fefc643f5c5c47035 (diff)
downloadtermsonic-3a815dcf23323d2f1d0b830c5cb5037d09156677.tar.gz
termsonic-3a815dcf23323d2f1d0b830c5cb5037d09156677.zip
Add shuffle + fix deadlocks
-rw-r--r--music/playqueue.go7
-rw-r--r--src/app.go12
-rw-r--r--src/footer.go4
-rw-r--r--src/keybinds.go25
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() {
57} 57}
58 58
59func (q *Queue) PlaySong(s *subsonic.Child) error { 59func (q *Queue) PlaySong(s *subsonic.Child) error {
60 if q.isPaused {
61 q.TogglePause()
62 }
63
60 rc, err := Download2(q.sub, s.ID) 64 rc, err := Download2(q.sub, s.ID)
61 if err != nil { 65 if err != nil {
62 return err 66 return err
@@ -125,6 +129,9 @@ func (q *Queue) Next() error {
125} 129}
126 130
127func (q *Queue) Stop() { 131func (q *Queue) Stop() {
132 if q.isPaused {
133 q.TogglePause()
134 }
128 speaker.Clear() 135 speaker.Clear()
129} 136}
130 137
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
2 2
3import ( 3import (
4 "fmt" 4 "fmt"
5 "math/rand"
5 "os" 6 "os"
7 "time"
6 8
7 "git.sixfoisneuf.fr/termsonic/music" 9 "git.sixfoisneuf.fr/termsonic/music"
8 "github.com/delucks/go-subsonic" 10 "github.com/delucks/go-subsonic"
@@ -158,3 +160,13 @@ func (a *app) switchToPage(name string) {
158 160
159 a.updateFooter() 161 a.updateFooter()
160} 162}
163
164func randomize(t []*subsonic.Child) []*subsonic.Child {
165 t2 := make([]*subsonic.Child, len(t))
166 copy(t2, t)
167
168 rand.Seed(time.Now().UnixNano())
169 rand.Shuffle(len(t2), func(i, j int) { t2[i], t2[j] = t2[j], t2[i] })
170
171 return t2
172}
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() {
9 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") 9 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")
10 } 10 }
11 case "playqueue": 11 case "playqueue":
12 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") 12 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")
13 case "playlists": 13 case "playlists":
14 if a.tv.GetFocus() == a.playlistsList { 14 if a.tv.GetFocus() == a.playlistsList {
15 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") 15 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")
16 } else if a.tv.GetFocus() == a.playlistSongs { 16 } else if a.tv.GetFocus() == a.playlistSongs {
17 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") 17 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")
18 } 18 }
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) {
51 a.playQueueList.SetCurrentItem(sel - 1) 51 a.playQueueList.SetCurrentItem(sel - 1)
52 52
53 return nil 53 return nil
54 } else if event.Rune() == 'r' {
55 a.playQueue.Stop()
56 songs := a.playQueue.GetSongs()
57 a.playQueue.Clear()
58 for _, s := range randomize(songs) {
59 a.playQueue.Append(s)
60 }
61
62 a.playQueue.Play()
63 a.updatePageQueue()
54 } 64 }
55 } 65 }
56 66
@@ -147,6 +157,21 @@ func (a *app) setupMusicControlKeys(p *tview.Box) {
147 } 157 }
148 158
149 a.updatePageQueue() 159 a.updatePageQueue()
160 } else if event.Rune() == 'r' {
161 sel := a.playlistsList.GetCurrentItem()
162 pl, err := a.sub.GetPlaylist(a.allPlaylists[sel].ID)
163 if err != nil {
164 a.alert("Error: %v", err)
165 return nil
166 }
167
168 a.playQueue.Clear()
169 for _, s := range randomize(pl.Entry) {
170 a.playQueue.Append(s)
171 }
172 a.playQueue.Play()
173
174 a.updatePageQueue()
150 } 175 }
151 } 176 }
152 177