Stations made from the music, random and without end, with nothing to set up: the
mixes the listener has (genres, favourites, rediscover, new to you), everything,
one for each decade that has 30 songs by 4 artists, and the radio of an artist (their
songs, four times as likely, and the songs of the two genres most of theirs have).
Every time a station is asked for it picks other songs, with the same weights as the
mixes (taste, hearts, nothing skipped lately).
- mixes.Service.Radios and Radio; Filter has All and years, the store has Decades
- GET /radio and GET /radio/{id}, in the OpenAPI spec
- OpenSubsonic: each station is a playlist (rd-<id>, "Radio · Rock") that has other
songs every time it is opened, for apps that cannot play a radio that never ends
152 lines
4.6 KiB
Go
152 lines
4.6 KiB
Go
package mixes
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/catalog"
|
|
)
|
|
|
|
// The radio is the mixes without a day to stay the same for: every time a station is asked for, it picks other songs,
|
|
// so that it never has to end. Its stations are the ones of the mixes (the genres, Your favourites, Rediscover, New to
|
|
// you) and three more that need no listening: everything, a decade, and the songs of an artist and of the genres they
|
|
// play.
|
|
|
|
// radioTitle is the title of the station that is every song.
|
|
const radioAll = "Everything"
|
|
|
|
// decades are the stations of the years: one for each decade that has songs enough for a mix, the oldest first.
|
|
func (s *Service) decades(ctx context.Context) ([]definition, error) {
|
|
list, err := s.store.Decades(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read decades: %w", err)
|
|
}
|
|
sort.Slice(list, func(i, j int) bool { return list[i].Start < list[j].Start })
|
|
var out []definition
|
|
for _, d := range list {
|
|
if d.Tracks < MinGenreTracks || d.Artists < MinGenreArtists {
|
|
continue
|
|
}
|
|
out = append(out, definition{
|
|
Mix: Mix{ID: "decade:" + strconv.Itoa(d.Start), Kind: KindDecade, Title: strconv.Itoa(d.Start) + "s", Decade: d.Start, Artists: []string{}},
|
|
minYear: d.Start, maxYear: d.Start + 9,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// radioDefinitions is every station there is for a listener.
|
|
func (s *Service) radioDefinitions(ctx context.Context, st state) ([]definition, error) {
|
|
defs := append([]definition{}, st.defs...)
|
|
defs = append(defs, definition{Mix: Mix{ID: "all", Kind: KindAll, Title: radioAll, Artists: []string{}}, all: true})
|
|
decades, err := s.decades(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append(defs, decades...), nil
|
|
}
|
|
|
|
// Radios returns the stations of the radio of a listener: the mixes they have, everything, and the decades.
|
|
func (s *Service) Radios(ctx context.Context, userID int64) ([]Mix, error) {
|
|
st, err := s.state(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defs, err := s.radioDefinitions(ctx, st)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]Mix, len(defs))
|
|
for i, d := range defs {
|
|
if out[i], err = s.withCovers(ctx, d); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Radio gives songs of a station: up to Size of them, other ones every time it is asked. The id is one of Radios, or
|
|
// "artist:<id>" for the station of an artist. It gives ErrNotFound for a station the listener does not have.
|
|
func (s *Service) Radio(ctx context.Context, userID int64, id string) (Mix, []catalog.Track, error) {
|
|
st, err := s.state(ctx, userID)
|
|
if err != nil {
|
|
return Mix{}, nil, err
|
|
}
|
|
var def *definition
|
|
if rest, ok := strings.CutPrefix(id, "artist:"); ok {
|
|
if def, err = s.artistStation(ctx, rest); err != nil {
|
|
return Mix{}, nil, err
|
|
}
|
|
} else {
|
|
defs, err := s.radioDefinitions(ctx, st)
|
|
if err != nil {
|
|
return Mix{}, nil, err
|
|
}
|
|
for i := range defs {
|
|
if defs[i].ID == id {
|
|
def = &defs[i]
|
|
}
|
|
}
|
|
}
|
|
if def == nil {
|
|
return Mix{}, nil, ErrNotFound
|
|
}
|
|
return s.make(ctx, userID, st, *def, s.seed())
|
|
}
|
|
|
|
// artistStation is the station of an artist: their songs, and the songs of the (up to two) genres most of their songs
|
|
// have. It is not found for an id that is not an artist, or an artist that has no song to play.
|
|
func (s *Service) artistStation(ctx context.Context, raw string) (*definition, error) {
|
|
artist, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil || artist < 1 {
|
|
return nil, ErrNotFound
|
|
}
|
|
names, err := s.store.ArtistNames(ctx, []int64{artist})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read artist names: %w", err)
|
|
}
|
|
name := names[artist]
|
|
if name == "" {
|
|
return nil, ErrNotFound
|
|
}
|
|
of, err := s.store.ArtistGenres(ctx, []int64{artist})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read artist genres: %w", err)
|
|
}
|
|
type count struct {
|
|
raw string
|
|
n int
|
|
}
|
|
var counts []count
|
|
for raw, n := range of[artist] {
|
|
if normalizeGenre(raw) != "" {
|
|
counts = append(counts, count{raw, n})
|
|
}
|
|
}
|
|
sort.Slice(counts, func(i, j int) bool {
|
|
if counts[i].n != counts[j].n {
|
|
return counts[i].n > counts[j].n
|
|
}
|
|
return counts[i].raw < counts[j].raw
|
|
})
|
|
var raws []string
|
|
for _, c := range counts[:min(2, len(counts))] {
|
|
raws = append(raws, c.raw)
|
|
}
|
|
pool, err := s.store.Pool(ctx, Filter{ArtistIDs: []int64{artist}})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read the pool: %w", err)
|
|
}
|
|
if len(pool) == 0 {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &definition{
|
|
Mix: Mix{ID: "artist:" + raw, Kind: KindArtist, Title: name, Artists: []string{name}},
|
|
raw: raws,
|
|
artists: []int64{artist},
|
|
focus: artist,
|
|
}, nil
|
|
}
|