Skip to content

Commit

Permalink
Refactor: move some playback-related functions to backend package
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jun 25, 2024
1 parent 1ddc6dc commit 7793fd8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package helpers

import (
"fmt"

"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/sharedutil"
)

func GetSimilarSongsFallback(mp mediaprovider.MediaProvider, track *mediaprovider.Track, count int) []*mediaprovider.Track {
var tracks []*mediaprovider.Track
if len(track.ArtistIDs) > 0 {
tracks, _ = mp.GetSimilarTracks(track.ArtistIDs[0], count)
}
if len(tracks) == 0 {
tracks, _ = mp.GetRandomTracks(track.Genre, count)
Expand All @@ -19,3 +20,19 @@ func GetSimilarSongsFallback(mp mediaprovider.MediaProvider, track *mediaprovide
return t.ID != track.ID
})
}

func GetArtistTracks(mp mediaprovider.MediaProvider, artistID string) ([]*mediaprovider.Track, error) {
artist, err := mp.GetArtist(artistID)
if err != nil {
return nil, fmt.Errorf("error getting artist tracks: %v", err.Error())
}
var allTracks []*mediaprovider.Track
for _, album := range artist.Albums {
album, err := mp.GetAlbum(album.ID)
if err != nil {
return nil, fmt.Errorf("error loading album tracks: %v", err.Error())
}
allTracks = append(allTracks, album.Tracks...)
}
return allTracks, nil
}
4 changes: 4 additions & 0 deletions backend/mediaprovider/jellyfin/jellyfinmediaprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func (j *jellyfinMediaProvider) GetArtist(artistID string) (*mediaprovider.Artis
return artist, nil
}

func (j *jellyfinMediaProvider) GetArtistTracks(artistID string) ([]*mediaprovider.Track, error) {
return helpers.GetArtistTracks(j, artistID)
}

func (j *jellyfinMediaProvider) GetArtistInfo(artistID string) (*mediaprovider.ArtistInfo, error) {
ar, err := j.client.GetArtist(artistID)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions backend/mediaprovider/mediaprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ type MediaProvider interface {

GetArtist(artistID string) (*ArtistWithAlbums, error)

GetArtistTracks(artistID string) ([]*Track, error)

GetArtistInfo(artistID string) (*ArtistInfo, error)

GetPlaylist(playlistID string) (*PlaylistWithTracks, error)
Expand Down
4 changes: 4 additions & 0 deletions backend/mediaprovider/subsonic/subsonicmediaprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (s *subsonicMediaProvider) GetArtist(artistID string) (*mediaprovider.Artis
}, nil
}

func (s *subsonicMediaProvider) GetArtistTracks(artistID string) ([]*mediaprovider.Track, error) {
return helpers.GetArtistTracks(s, artistID)
}

func (s *subsonicMediaProvider) GetArtistInfo(artistID string) (*mediaprovider.ArtistInfo, error) {
info, err := s.client.GetArtistInfo2(artistID, map[string]string{})
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions backend/playbackmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"log"
"math/rand"

"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/backend/player"
Expand Down Expand Up @@ -168,6 +169,47 @@ func (p *PlaybackManager) PlayTrack(trackID string) error {
return p.PlayFromBeginning()
}

func (p *PlaybackManager) ShuffleArtistAlbums(artistID string) {
artist, err := p.engine.sm.Server.GetArtist(artistID)
if err != nil {
log.Printf(err.Error())
return
}
if len(artist.Albums) == 0 {
return
}

rand.Shuffle(len(artist.Albums), func(i, j int) {
artist.Albums[i], artist.Albums[j] = artist.Albums[j], artist.Albums[i]
})
p.StopAndClearPlayQueue()
for _, al := range artist.Albums {
p.LoadAlbum(al.ID, Append, false)
}

if p.engine.replayGainCfg.Mode == ReplayGainAuto {
p.SetReplayGainMode(player.ReplayGainAlbum)
}
p.PlayFromBeginning()
}

func (p *PlaybackManager) PlayArtistDiscography(artistID string, shuffleTracks bool) {
tr, err := p.engine.sm.Server.GetArtistTracks(artistID)
if err != nil {
log.Printf(err.Error())
return
}
p.LoadTracks(tr, Replace, shuffleTracks)
if p.engine.replayGainCfg.Mode == ReplayGainAuto {
if shuffleTracks {
p.SetReplayGainMode(player.ReplayGainTrack)
} else {
p.SetReplayGainMode(player.ReplayGainAlbum)
}
}
p.PlayFromBeginning()
}

func (p *PlaybackManager) PlayFromBeginning() error {
return p.engine.PlayTrackAt(0)
}
Expand Down
6 changes: 3 additions & 3 deletions ui/browsing/artistpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func NewArtistPageHeader(page *ArtistPage) *ArtistPageHeader {
}
a.favoriteBtn = widgets.NewFavoriteButton(func() { go a.toggleFavorited() })
a.playBtn = widget.NewButtonWithIcon("Play Discography", theme.MediaPlayIcon(), func() {
go a.artistPage.contr.PlayArtistDiscography(a.artistID, false /*shuffle*/)
go a.artistPage.pm.PlayArtistDiscography(a.artistID, false /*shuffle*/)
})
a.playRadioBtn = widget.NewButtonWithIcon("Play Artist Radio", myTheme.ShuffleIcon, func() {
// must not pass playArtistRadio func directly to NewButton
Expand All @@ -332,11 +332,11 @@ func NewArtistPageHeader(page *ArtistPage) *ArtistPageHeader {
a.menuBtn.OnTapped = func() {
if pop == nil {
shuffleTracks := fyne.NewMenuItem("Shuffle tracks", func() {
go a.artistPage.contr.PlayArtistDiscography(a.artistID, true /*shuffle*/)
go a.artistPage.pm.PlayArtistDiscography(a.artistID, true /*shuffle*/)
})
shuffleTracks.Icon = myTheme.TracksIcon
shuffleAlbums := fyne.NewMenuItem("Shuffle albums", func() {
go a.artistPage.contr.ShuffleArtistAlbums(a.artistID)
go a.artistPage.pm.ShuffleArtistAlbums(a.artistID)
})
shuffleAlbums.Icon = myTheme.AlbumIcon
menu := fyne.NewMenu("", shuffleTracks, shuffleAlbums)
Expand Down
60 changes: 6 additions & 54 deletions ui/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"image"
"io"
"log"
"math/rand"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -210,7 +209,7 @@ func (m *Controller) ConnectArtistGridActions(grid *widgets.GridView) {
grid.OnPlayNext = func(artistID string) {
go m.App.PlaybackManager.LoadTracks(m.GetArtistTracks(artistID), backend.InsertNext, false)
}
grid.OnPlay = func(artistID string, shuffle bool) { go m.PlayArtistDiscography(artistID, shuffle) }
grid.OnPlay = func(artistID string, shuffle bool) { go m.App.PlaybackManager.PlayArtistDiscography(artistID, shuffle) }
grid.OnAddToQueue = func(artistID string) {
go m.App.PlaybackManager.LoadTracks(m.GetArtistTracks(artistID), backend.Append, false)
}
Expand All @@ -235,62 +234,15 @@ func (m *Controller) ConnectArtistGridActions(grid *widgets.GridView) {
}

func (m *Controller) GetArtistTracks(artistID string) []*mediaprovider.Track {
server := m.App.ServerManager.Server
if server == nil {
log.Println("error playing artist discography: logged out")
return nil
}

artist, err := server.GetArtist(artistID)
if err != nil {
log.Printf("error getting artist discography: %v", err.Error())
return nil
}
var allTracks []*mediaprovider.Track
for _, album := range artist.Albums {
album, err := server.GetAlbum(album.ID)
if err != nil {
log.Printf("error loading album tracks: %v", err.Error())
if mp := m.App.ServerManager.Server; mp != nil {
if tr, err := mp.GetArtistTracks(artistID); err != nil {
log.Println(err.Error())
return nil
}
allTracks = append(allTracks, album.Tracks...)
}
return allTracks
}

func (m *Controller) PlayArtistDiscography(artistID string, shuffleTracks bool) {
m.App.PlaybackManager.LoadTracks(m.GetArtistTracks(artistID), backend.Replace, shuffleTracks)
if m.App.Config.ReplayGain.Mode == backend.ReplayGainAuto {
if shuffleTracks {
m.App.PlaybackManager.SetReplayGainMode(player.ReplayGainTrack)
} else {
m.App.PlaybackManager.SetReplayGainMode(player.ReplayGainAlbum)
return tr
}
}
m.App.PlaybackManager.PlayFromBeginning()
}

func (m *Controller) ShuffleArtistAlbums(artistID string) {
artist, err := m.App.ServerManager.Server.GetArtist(artistID)
if err != nil {
log.Printf("error getting artist discography: %v", err.Error())
}
if len(artist.Albums) == 0 {
return
}

rand.Shuffle(len(artist.Albums), func(i, j int) {
artist.Albums[i], artist.Albums[j] = artist.Albums[j], artist.Albums[i]
})
m.App.PlaybackManager.StopAndClearPlayQueue()
for _, al := range artist.Albums {
m.App.PlaybackManager.LoadAlbum(al.ID, backend.Append, false)
}

if m.App.Config.ReplayGain.Mode == backend.ReplayGainAuto {
m.App.PlaybackManager.SetReplayGainMode(player.ReplayGainAlbum)
}
m.App.PlaybackManager.PlayFromBeginning()
return nil
}

func (m *Controller) PromptForFirstServer() {
Expand Down

0 comments on commit 7793fd8

Please sign in to comment.