fix(scanner): a featured guest or a listed collaborator is a credit on the track, not an artist of their own
release / web (push) Successful in 1m13s
release / go (push) Successful in 1m46s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m52s
release / android (push) Successful in 4m11s
release / Build signed APK (push) Successful in 4m42s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
release / web (push) Successful in 1m13s
release / go (push) Successful in 1m46s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m52s
release / android (push) Successful in 4m11s
release / Build signed APK (push) Successful in 4m42s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
A track tagged "Low Tide Choir feat. Ruby Calloway" was filed under an artist of that whole name, with its own page, its own place in the artist list and its own "Songs like" mix. It is now filed under Low Tide Choir and keeps "Low Tide Choir feat. Ruby Calloway" as its credit, shown wherever the track is listed, sent to Subsonic apps and scrobbled. A collaboration is split only when the file lists its artists one by one (Picard's ARTISTS tag, read from FLAC, Ogg, Opus, MP3 and M4A). "A & B" on its own may be one band, so it stays whole. A Vorbis file's artist id is now its first, not the guest's. Tag-read version 10 re-reads the library once; the scan's tidy-up removes the credit-named artists it leaves empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/dhowden/tag"
|
||||
)
|
||||
|
||||
// artistCredit is who a track is filed under, and the credit shown beside it.
|
||||
//
|
||||
// A track's ARTIST tag is a credit, not always one artist: "Low Tide Choir
|
||||
// feat. Ruby Calloway" names a guest, and a collaboration names two artists
|
||||
// equally. Filed under the whole string, each such credit became an artist of
|
||||
// its own, with its own page, its own place in the artist list and its own
|
||||
// "Songs like" mix, holding one or two tracks. The track is filed under its
|
||||
// first artist instead, and the full credit is kept to show with it.
|
||||
type artistCredit struct {
|
||||
// Name is the artist the track is filed under.
|
||||
Name string
|
||||
// MBID is Name's MusicBrainz id, or "".
|
||||
MBID string
|
||||
// Credit is the full credit when it names more than Name, else "".
|
||||
Credit string
|
||||
}
|
||||
|
||||
// featuring finds where a guest credit starts: "X feat. Y", "X ft. Y",
|
||||
// "X featuring Y", "X (feat. Y)". Outside brackets the abbreviations need
|
||||
// their full stop, so a band called "Little Feat" is not cut short; inside
|
||||
// brackets the stop is optional.
|
||||
var featuring = regexp.MustCompile(`(?i)\s+(?:[(\[]\s*(?:feat\.?|ft\.?|featuring)|feat\.|ft\.|featuring)\s`)
|
||||
|
||||
// readArtistCredit works out a track's artist credit from its tags.
|
||||
//
|
||||
// - A file that lists its artists one by one (Picard's ARTISTS tag, two or
|
||||
// more values) is filed under the first. This is the only way a
|
||||
// collaboration is split: "A & B" may be two artists or one band
|
||||
// ("Simon & Garfunkel", "St. Paul & The Broken Bones"), and only the
|
||||
// list says which.
|
||||
// - Otherwise a featured guest is cut from the credit: "X feat. Y" is filed
|
||||
// under X.
|
||||
// - Otherwise the credit is the artist.
|
||||
//
|
||||
// The MusicBrainz id is the first of the file's artist ids, which Picard
|
||||
// writes in credit order, so it belongs to the first artist.
|
||||
func readArtistCredit(meta tag.Metadata, rs io.ReadSeeker, ids TagMBIDs) artistCredit {
|
||||
credit := strings.TrimSpace(meta.Artist())
|
||||
listed := readTagList(meta, rs, "ARTISTS", "ARTISTS")
|
||||
mbid := ids.Artist
|
||||
if isVorbis(meta) {
|
||||
// dhowden/tag keeps the last of a repeated Vorbis field, which on a
|
||||
// credit of two artists is the second artist's id.
|
||||
if all := readTagList(meta, rs, "MUSICBRAINZ_ARTISTID", ""); len(all) > 0 {
|
||||
mbid = cleanMBID(all[0])
|
||||
}
|
||||
}
|
||||
|
||||
name := credit
|
||||
switch {
|
||||
case len(listed) >= 2:
|
||||
name = listed[0]
|
||||
if credit == "" {
|
||||
credit = strings.Join(listed, ", ")
|
||||
}
|
||||
case credit != "":
|
||||
name = withoutGuests(credit)
|
||||
}
|
||||
if name == "" {
|
||||
return artistCredit{Name: "Unknown Artist"}
|
||||
}
|
||||
out := artistCredit{Name: name, MBID: mbid}
|
||||
if credit != name {
|
||||
out.Credit = credit
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// withoutGuests cuts a featured guest from a credit: "X feat. Y" gives X.
|
||||
// Album artists go through it too, as a single's release credit often names
|
||||
// its guest.
|
||||
func withoutGuests(credit string) string {
|
||||
if loc := featuring.FindStringIndex(credit); loc != nil && loc[0] > 0 {
|
||||
return strings.TrimSpace(credit[:loc[0]])
|
||||
}
|
||||
return credit
|
||||
}
|
||||
|
||||
func isVorbis(meta tag.Metadata) bool { return meta.Format() == tag.VORBIS }
|
||||
|
||||
// readTagList returns every value of a field that can repeat, in file order:
|
||||
// the Vorbis comment vorbisKey (FLAC, Ogg Vorbis, Opus), or the user-defined
|
||||
// text field called textName (ID3v2 TXXX, MP4 freeform). An empty textName
|
||||
// reads Vorbis only. A file that cannot be read for it gives nil.
|
||||
func readTagList(meta tag.Metadata, rs io.ReadSeeker, vorbisKey, textName string) []string {
|
||||
var values []string
|
||||
switch meta.Format() {
|
||||
case tag.VORBIS:
|
||||
if meta.FileType() == tag.OGG {
|
||||
values, _ = readOggValues(rs, vorbisKey)
|
||||
} else {
|
||||
values, _ = readFLACValues(rs, vorbisKey)
|
||||
}
|
||||
case tag.MP4:
|
||||
if textName != "" {
|
||||
values, _ = readMP4FreeformValues(rs, textName)
|
||||
}
|
||||
case tag.ID3v2_2, tag.ID3v2_3, tag.ID3v2_4:
|
||||
if textName != "" {
|
||||
values = id3TextList(meta, textName)
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// id3TextList reads the TXXX field called name. dhowden/tag files each TXXX
|
||||
// frame under "TXXX", "TXXX_0", … as a *tag.Comm. ID3v2.4 separates the
|
||||
// values of one frame with NUL. ID3v2.3 has no separator, and a "/" there
|
||||
// may be part of a name (AC/DC), so a v2.3 frame is one value.
|
||||
func id3TextList(meta tag.Metadata, name string) []string {
|
||||
var out []string
|
||||
for key, v := range meta.Raw() {
|
||||
if !strings.HasPrefix(key, "TXXX") {
|
||||
continue
|
||||
}
|
||||
c, ok := v.(*tag.Comm)
|
||||
if !ok || !strings.EqualFold(c.Description, name) {
|
||||
continue
|
||||
}
|
||||
for _, part := range strings.Split(c.Text, "\x00") {
|
||||
if p := strings.TrimSpace(part); p != "" {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
// One frame per field name: a tagger that wrote two would leave the
|
||||
// order between them to map iteration.
|
||||
break
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/dhowden/tag"
|
||||
)
|
||||
|
||||
func TestWithoutGuests(t *testing.T) {
|
||||
tests := []struct{ credit, want string }{
|
||||
{"Low Tide Choir feat. Ruby Calloway", "Low Tide Choir"},
|
||||
{"Low Tide Choir ft. Ruby Calloway", "Low Tide Choir"},
|
||||
{"Low Tide Choir featuring Ruby Calloway", "Low Tide Choir"},
|
||||
{"Low Tide Choir (feat. Ruby Calloway)", "Low Tide Choir"},
|
||||
{"Low Tide Choir [ft Ruby Calloway]", "Low Tide Choir"},
|
||||
{"Low Tide Choir FEAT. Ruby Calloway & Mae", "Low Tide Choir"},
|
||||
// Names that only look like a credit stay whole.
|
||||
{"Little Feat", "Little Feat"},
|
||||
{"Little Feat featuring Bonnie Raitt", "Little Feat"},
|
||||
{"Daft Punk", "Daft Punk"},
|
||||
{"Soft Cell", "Soft Cell"},
|
||||
{"Featuring Fools", "Featuring Fools"},
|
||||
{"Simon & Garfunkel", "Simon & Garfunkel"},
|
||||
{"St. Paul & The Broken Bones", "St. Paul & The Broken Bones"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
if got := withoutGuests(tc.credit); got != tc.want {
|
||||
t.Errorf("withoutGuests(%q) = %q, want %q", tc.credit, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// id3Meta is an ID3v2 file's tags as dhowden/tag presents them.
|
||||
type id3Meta struct {
|
||||
tag.Metadata
|
||||
artist string
|
||||
raw map[string]interface{}
|
||||
}
|
||||
|
||||
func (m id3Meta) Format() tag.Format { return tag.ID3v2_4 }
|
||||
func (m id3Meta) FileType() tag.FileType { return tag.MP3 }
|
||||
func (m id3Meta) Artist() string { return m.artist }
|
||||
func (m id3Meta) Raw() map[string]interface{} { return m.raw }
|
||||
|
||||
func TestReadArtistCredit_ID3(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
artist string
|
||||
raw map[string]interface{}
|
||||
mbid string
|
||||
want artistCredit
|
||||
}{
|
||||
{
|
||||
name: "a guest is cut from the credit",
|
||||
artist: "Low Tide Choir feat. Ruby Calloway",
|
||||
mbid: "id-choir",
|
||||
want: artistCredit{Name: "Low Tide Choir", MBID: "id-choir", Credit: "Low Tide Choir feat. Ruby Calloway"},
|
||||
},
|
||||
{
|
||||
name: "a collaboration the file lists is filed under its first artist",
|
||||
artist: "Paper Lanterns & Ruby Calloway",
|
||||
raw: map[string]interface{}{"TXXX": &tag.Comm{Description: "ARTISTS", Text: "Paper Lanterns\x00Ruby Calloway"}},
|
||||
want: artistCredit{Name: "Paper Lanterns", Credit: "Paper Lanterns & Ruby Calloway"},
|
||||
},
|
||||
{
|
||||
name: "an & the file does not list is one artist",
|
||||
artist: "St. Paul & The Broken Bones",
|
||||
raw: map[string]interface{}{"TXXX": &tag.Comm{Description: "ARTISTS", Text: "St. Paul & The Broken Bones"}},
|
||||
want: artistCredit{Name: "St. Paul & The Broken Bones"},
|
||||
},
|
||||
{
|
||||
name: "a v2.3 list has no separator, so a slash stays in the name",
|
||||
artist: "AC/DC",
|
||||
raw: map[string]interface{}{"TXXX_0": &tag.Comm{Description: "Artists", Text: "AC/DC"}},
|
||||
want: artistCredit{Name: "AC/DC"},
|
||||
},
|
||||
{
|
||||
name: "no artist at all",
|
||||
want: artistCredit{Name: "Unknown Artist"},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := readArtistCredit(id3Meta{artist: tc.artist, raw: tc.raw}, nil, TagMBIDs{Artist: tc.mbid})
|
||||
if got != tc.want {
|
||||
t.Errorf("readArtistCredit = %+v, want %+v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A FLAC credited to two artists, as Picard writes it: ARTIST is the credit,
|
||||
// and ARTISTS and MUSICBRAINZ_ARTISTID list the artists in credit order.
|
||||
// dhowden/tag keeps the last of each repeated field, so it alone would file
|
||||
// the track under the second artist's id.
|
||||
func TestReadArtistCredit_FLACListsItsArtists(t *testing.T) {
|
||||
data := buildFLAC(false,
|
||||
"TITLE=Undertow",
|
||||
"ARTIST=Paper Lanterns & Ruby Calloway",
|
||||
"ARTISTS=Paper Lanterns",
|
||||
"ARTISTS=Ruby Calloway",
|
||||
"MUSICBRAINZ_ARTISTID=id-lanterns",
|
||||
"MUSICBRAINZ_ARTISTID=id-ruby",
|
||||
)
|
||||
rs := bytes.NewReader(data)
|
||||
meta, err := tag.ReadFrom(rs)
|
||||
if err != nil {
|
||||
t.Fatalf("tag.ReadFrom: %v", err)
|
||||
}
|
||||
got := readArtistCredit(meta, rs, ReadTagMBIDs(meta))
|
||||
want := artistCredit{Name: "Paper Lanterns", MBID: "id-lanterns", Credit: "Paper Lanterns & Ruby Calloway"}
|
||||
if got != want {
|
||||
t.Errorf("readArtistCredit = %+v, want %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadArtistCredit_FLACWithOneArtist(t *testing.T) {
|
||||
data := buildFLAC(false, "ARTIST=Marina & the Kats", "ARTISTS=Marina & the Kats", "MUSICBRAINZ_ARTISTID=id-kats")
|
||||
rs := bytes.NewReader(data)
|
||||
meta, err := tag.ReadFrom(rs)
|
||||
if err != nil {
|
||||
t.Fatalf("tag.ReadFrom: %v", err)
|
||||
}
|
||||
got := readArtistCredit(meta, rs, ReadTagMBIDs(meta))
|
||||
if want := (artistCredit{Name: "Marina & the Kats", MBID: "id-kats"}); got != want {
|
||||
t.Errorf("readArtistCredit = %+v, want %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadMP4FreeformValues(t *testing.T) {
|
||||
freeform := func(name string, values ...string) []byte {
|
||||
children := [][]byte{
|
||||
mp4Box("mean", []byte{0, 0, 0, 0}, []byte("com.apple.iTunes")),
|
||||
mp4Box("name", []byte{0, 0, 0, 0}, []byte(name)),
|
||||
}
|
||||
for _, v := range values {
|
||||
children = append(children, mp4Text(v))
|
||||
}
|
||||
return mp4Box("----", children...)
|
||||
}
|
||||
data := buildMP4(true,
|
||||
freeform("MusicBrainz Artist Id", "id-lanterns"),
|
||||
freeform("ARTISTS", "Paper Lanterns", "Ruby Calloway"),
|
||||
)
|
||||
got, err := readMP4FreeformValues(bytes.NewReader(data), "artists")
|
||||
if err != nil {
|
||||
t.Fatalf("readMP4FreeformValues: %v", err)
|
||||
}
|
||||
if want := []string{"Paper Lanterns", "Ruby Calloway"}; !equalStrings(got, want) {
|
||||
t.Errorf("ARTISTS = %q, want %q", got, want)
|
||||
}
|
||||
if got, err := readMP4FreeformValues(bytes.NewReader(buildMP4(true)), "ARTISTS"); err != nil || got != nil {
|
||||
t.Errorf("no ARTISTS item: got %q, %v; want nil, nil", got, err)
|
||||
}
|
||||
}
|
||||
@@ -74,9 +74,92 @@ func findMP4Child(rs io.ReadSeeker, start, end int64, kind string) (mp4Atom, boo
|
||||
// readMP4GenreValues returns every ©gen value, in file order. rs is seeked
|
||||
// as needed, so it is safe to call after dhowden/tag has consumed the reader.
|
||||
func readMP4GenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
ilst, found, err := findMP4Ilst(rs)
|
||||
if err != nil || !found {
|
||||
return nil, err
|
||||
}
|
||||
var out []string
|
||||
err = eachMP4Item(rs, ilst, func(kind string, body []byte) bool {
|
||||
if kind != "\xa9gen" {
|
||||
return true
|
||||
}
|
||||
values, ok := mp4DataValues(body)
|
||||
out = append(out, values...)
|
||||
return ok
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// readMP4FreeformValues returns the values of the freeform "----" item called
|
||||
// name (com.apple.iTunes:ARTISTS is Picard's), in file order. Names compare
|
||||
// case-insensitively, as taggers differ in case.
|
||||
func readMP4FreeformValues(rs io.ReadSeeker, name string) ([]string, error) {
|
||||
ilst, found, err := findMP4Ilst(rs)
|
||||
if err != nil || !found {
|
||||
return nil, err
|
||||
}
|
||||
var out []string
|
||||
err = eachMP4Item(rs, ilst, func(kind string, body []byte) bool {
|
||||
if kind != "----" || !strings.EqualFold(mp4FreeformName(body), name) {
|
||||
return true
|
||||
}
|
||||
values, ok := mp4DataValues(body)
|
||||
out = append(out, values...)
|
||||
return ok
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// mp4FreeformName is the text of the "name" atom in a freeform item's body:
|
||||
// size, "name", four bytes of version and flags, then the name.
|
||||
func mp4FreeformName(b []byte) string {
|
||||
for len(b) >= 8 {
|
||||
size := binary.BigEndian.Uint32(b[0:4])
|
||||
if size < 8 || uint64(size) > uint64(len(b)) {
|
||||
return ""
|
||||
}
|
||||
if string(b[4:8]) == "name" && size >= 12 {
|
||||
return string(b[12:size])
|
||||
}
|
||||
b = b[size:]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// eachMP4Item calls fn with the kind and body of each item in ilst, in file
|
||||
// order, until fn returns false (a malformed body). Each body is capped at
|
||||
// maxMP4GenreAtomSize.
|
||||
func eachMP4Item(rs io.ReadSeeker, ilst mp4Atom, fn func(kind string, body []byte) bool) error {
|
||||
for pos := ilst.body; pos+8 <= ilst.limit; {
|
||||
a, err := readMP4Atom(rs, pos, ilst.limit)
|
||||
if err != nil {
|
||||
return errNoGenreFrame
|
||||
}
|
||||
pos = a.limit
|
||||
n := a.limit - a.body
|
||||
if n > maxMP4GenreAtomSize {
|
||||
return errNoGenreFrame
|
||||
}
|
||||
if _, err := rs.Seek(a.body, io.SeekStart); err != nil {
|
||||
return errNoGenreFrame
|
||||
}
|
||||
body := make([]byte, n)
|
||||
if _, err := io.ReadFull(rs, body); err != nil {
|
||||
return errNoGenreFrame
|
||||
}
|
||||
if !fn(a.kind, body) {
|
||||
return errNoGenreFrame
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// findMP4Ilst finds the iTunes item list, moov > udta > meta > ilst.
|
||||
// found is false for a file with no iTunes metadata.
|
||||
func findMP4Ilst(rs io.ReadSeeker) (ilst mp4Atom, found bool, err error) {
|
||||
end, err := rs.Seek(0, io.SeekEnd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return mp4Atom{}, false, err
|
||||
}
|
||||
// moov may sit after mdat; walking headers and seeking past bodies finds
|
||||
// it either way without reading the audio.
|
||||
@@ -84,10 +167,10 @@ func readMP4GenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
for _, kind := range []string{"moov", "udta", "meta"} {
|
||||
next, found, err := findMP4Child(rs, cur.body, cur.limit, kind)
|
||||
if err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
return mp4Atom{}, false, errNoGenreFrame
|
||||
}
|
||||
if !found {
|
||||
return nil, nil // no iTunes metadata at all
|
||||
return mp4Atom{}, false, nil // no iTunes metadata at all
|
||||
}
|
||||
cur = next
|
||||
}
|
||||
@@ -95,54 +178,24 @@ func readMP4GenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
// children in the ISO layout. QuickTime-style files omit them, so the
|
||||
// first child (hdlr) starts straight away. Look before skipping.
|
||||
if _, err := rs.Seek(cur.body, io.SeekStart); err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
return mp4Atom{}, false, errNoGenreFrame
|
||||
}
|
||||
var peek [8]byte
|
||||
if _, err := io.ReadFull(rs, peek[:]); err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
return mp4Atom{}, false, errNoGenreFrame
|
||||
}
|
||||
if string(peek[4:8]) != "hdlr" {
|
||||
cur.body += 4
|
||||
}
|
||||
ilst, found, err := findMP4Child(rs, cur.body, cur.limit, "ilst")
|
||||
ilst, found, err = findMP4Child(rs, cur.body, cur.limit, "ilst")
|
||||
if err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
return mp4Atom{}, false, errNoGenreFrame
|
||||
}
|
||||
if !found {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var out []string
|
||||
for pos := ilst.body; pos+8 <= ilst.limit; {
|
||||
a, err := readMP4Atom(rs, pos, ilst.limit)
|
||||
if err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
pos = a.limit
|
||||
if a.kind != "\xa9gen" {
|
||||
continue
|
||||
}
|
||||
n := a.limit - a.body
|
||||
if n > maxMP4GenreAtomSize {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
if _, err := rs.Seek(a.body, io.SeekStart); err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
body := make([]byte, n)
|
||||
if _, err := io.ReadFull(rs, body); err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
values, ok := mp4DataValues(body)
|
||||
if !ok {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
out = append(out, values...)
|
||||
}
|
||||
return out, nil
|
||||
return ilst, found, nil
|
||||
}
|
||||
|
||||
// mp4DataValues reads the "data" atoms in a ©gen body. Each is: size, "data",
|
||||
// mp4DataValues reads the "data" atoms in a ©gen or freeform item's body;
|
||||
// a freeform item's "mean" and "name" atoms are skipped. Each is: size, "data",
|
||||
// a 4-byte type indicator (1 = UTF-8 text), a 4-byte locale, then the value.
|
||||
// Atoms of other types carry no text and are skipped.
|
||||
func mp4DataValues(b []byte) ([]string, bool) {
|
||||
|
||||
@@ -84,7 +84,12 @@ var audioExtensions = map[string]bool{
|
||||
// track, was split into an album per track artist; a re-read files each
|
||||
// track back on the one album, and the scan's tidy-up removes the rows
|
||||
// it leaves empty.
|
||||
const tagReadVersion int16 = 9
|
||||
// 10: a credit that names a guest ("X feat. Y") or lists several artists
|
||||
// (Picard's ARTISTS) is filed under its first artist and kept as the
|
||||
// track's credit, rather than becoming an artist of its own; the scan's
|
||||
// tidy-up removes the credit-named artists it leaves empty. A Vorbis
|
||||
// file's artist id is its first, not its last.
|
||||
const tagReadVersion int16 = 10
|
||||
|
||||
type Stats struct {
|
||||
Scanned int `json:"scanned"`
|
||||
@@ -367,13 +372,14 @@ func (s *Scanner) scanFile(
|
||||
return pgtype.UUID{}, false, fmt.Errorf("tag read: %w", err)
|
||||
}
|
||||
ids := ReadTagMBIDs(meta)
|
||||
albumMBID, artistMBID := ids.Release, ids.Artist
|
||||
albumMBID := ids.Release
|
||||
recordingMBID, releaseGroupMBID := ids.Recording, ids.ReleaseGroup
|
||||
|
||||
artistName := meta.Artist()
|
||||
if artistName == "" {
|
||||
artistName = "Unknown Artist"
|
||||
}
|
||||
// A guest or a second artist in the credit does not get an artist of
|
||||
// their own: the track is filed under the first, and the full credit is
|
||||
// kept to show with it (artistcredit.go).
|
||||
credit := readArtistCredit(meta, f, ids)
|
||||
artistName := credit.Name
|
||||
albumTitle := meta.Album()
|
||||
if albumTitle == "" {
|
||||
albumTitle = "Unknown Album"
|
||||
@@ -383,7 +389,7 @@ func (s *Scanner) scanFile(
|
||||
trackTitle = strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
|
||||
}
|
||||
|
||||
artist, err := s.resolveArtist(ctx, q, artistName, artistMBID)
|
||||
artist, err := s.resolveArtist(ctx, q, artistName, credit.MBID)
|
||||
if err != nil {
|
||||
return pgtype.UUID{}, false, fmt.Errorf("artist: %w", err)
|
||||
}
|
||||
@@ -392,7 +398,7 @@ func (s *Scanner) scanFile(
|
||||
// lands on one album instead of one per track artist. The track keeps
|
||||
// its own artist.
|
||||
albumArtist := artist
|
||||
if name := strings.TrimSpace(meta.AlbumArtist()); name != "" && name != artistName {
|
||||
if name := withoutGuests(strings.TrimSpace(meta.AlbumArtist())); name != "" && name != artistName {
|
||||
albumArtist, err = s.resolveArtist(ctx, q, name, ids.AlbumArtist)
|
||||
if err != nil {
|
||||
return pgtype.UUID{}, false, fmt.Errorf("album artist: %w", err)
|
||||
@@ -479,6 +485,10 @@ func (s *Scanner) scanFile(
|
||||
// Stamped so a future extraction fix can find this row again.
|
||||
TagReadVersion: tagReadVersion,
|
||||
}
|
||||
if credit.Credit != "" {
|
||||
c := credit.Credit
|
||||
params.ArtistCredit = &c
|
||||
}
|
||||
if trackNum > 0 {
|
||||
v := int32(trackNum)
|
||||
params.TrackNumber = &v
|
||||
|
||||
@@ -630,25 +630,60 @@ func TestScanner_AlbumArtistKeepsAnAlbumTogether_Integration(t *testing.T) {
|
||||
if got["Summer Hits"].tracks != 2 || got["Summer Hits"].trackArtists != 2 {
|
||||
t.Errorf("Summer Hits = %+v, want 2 tracks by 2 artists on one album", got["Summer Hits"])
|
||||
}
|
||||
if got["Night Drive"].tracks != 2 || got["Night Drive"].trackArtists != 2 {
|
||||
t.Errorf("Night Drive = %+v, want 2 tracks by 2 artists on one album", got["Night Drive"])
|
||||
// The guest is a credit on the track, not an artist of their own.
|
||||
if got["Night Drive"].tracks != 2 || got["Night Drive"].trackArtists != 1 {
|
||||
t.Errorf("Night Drive = %+v, want 2 tracks, both filed under Paper Lanterns", got["Night Drive"])
|
||||
}
|
||||
const guestCredit = "Paper Lanterns feat. Low Tide Choir"
|
||||
creditOf := func() (artist string, credit *string) {
|
||||
t.Helper()
|
||||
if err := pool.QueryRow(ctx, `
|
||||
SELECT ar.name, t.artist_credit FROM tracks t JOIN artists ar ON ar.id = t.artist_id
|
||||
WHERE t.file_path = $1`, guest).Scan(&artist, &credit); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return artist, credit
|
||||
}
|
||||
if artist, credit := creditOf(); artist != "Paper Lanterns" || credit == nil || *credit != guestCredit {
|
||||
t.Errorf("guest track filed under %q with credit %v, want Paper Lanterns with credit %q", artist, credit, guestCredit)
|
||||
}
|
||||
creditArtists := func() int {
|
||||
t.Helper()
|
||||
var n int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM artists WHERE name = $1`, guestCredit).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
if n := creditArtists(); n != 0 {
|
||||
t.Errorf("%d artists named %q, want none", n, guestCredit)
|
||||
}
|
||||
|
||||
// The guest track as the old filing left it: on an album of its own,
|
||||
// made long enough ago for the tidy-up, under an older tag-read version.
|
||||
// The guest track as the old filing left it: under an artist named for
|
||||
// the whole credit, on an album of its own, both made long enough ago for
|
||||
// the tidy-up, under an older tag-read version.
|
||||
if _, err := pool.Exec(ctx, `
|
||||
WITH g AS (SELECT artist_id FROM tracks WHERE file_path = $1),
|
||||
WITH ar AS (INSERT INTO artists (name, sort_name, created_at)
|
||||
VALUES ($2, lower($2), now() - interval '1 day') RETURNING id),
|
||||
a AS (INSERT INTO albums (title, sort_title, artist_id, created_at)
|
||||
SELECT 'Night Drive', 'night drive', artist_id, now() - interval '1 day' FROM g
|
||||
SELECT 'Night Drive', 'night drive', id, now() - interval '1 day' FROM ar
|
||||
RETURNING id)
|
||||
UPDATE tracks SET album_id = (SELECT id FROM a), tag_read_version = 8 WHERE file_path = $1`, guest); err != nil {
|
||||
UPDATE tracks SET album_id = (SELECT id FROM a), artist_id = (SELECT id FROM ar),
|
||||
artist_credit = NULL, tag_read_version = 8
|
||||
WHERE file_path = $1`, guest, guestCredit); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := scanner.Scan(ctx, nil); err != nil {
|
||||
t.Fatalf("rescan: %v", err)
|
||||
}
|
||||
if got := albums(); got["Night Drive"].tracks != 2 {
|
||||
t.Errorf("after the re-read, Night Drive = %+v, want both tracks back on one album", got["Night Drive"])
|
||||
if got := albums(); got["Night Drive"].tracks != 2 || got["Night Drive"].trackArtists != 1 {
|
||||
t.Errorf("after the re-read, Night Drive = %+v, want both tracks back on one album under Paper Lanterns", got["Night Drive"])
|
||||
}
|
||||
if artist, credit := creditOf(); artist != "Paper Lanterns" || credit == nil || *credit != guestCredit {
|
||||
t.Errorf("after the re-read, guest track filed under %q with credit %v, want Paper Lanterns with credit %q", artist, credit, guestCredit)
|
||||
}
|
||||
if n := creditArtists(); n != 0 {
|
||||
t.Errorf("after the re-read, %d artists named %q, want the tidy-up to remove it", n, guestCredit)
|
||||
}
|
||||
var empty int
|
||||
if err := pool.QueryRow(ctx,
|
||||
|
||||
@@ -22,7 +22,9 @@ import (
|
||||
// other seven would never be seen by browse or the taste profile.
|
||||
//
|
||||
// The comment block is read here directly, as the ID3v2 genre frame is in
|
||||
// id3v2genre.go. Every other field still comes from dhowden/tag.
|
||||
// id3v2genre.go, for the fields where a repeat matters: GENRE, and the
|
||||
// ARTISTS and MUSICBRAINZ_ARTISTID lists an artist credit is read from
|
||||
// (artistcredit.go). Every other field still comes from dhowden/tag.
|
||||
|
||||
// maxVorbisCommentSize caps a comment block or packet. Comments are kilobytes;
|
||||
// an embedded METADATA_BLOCK_PICTURE can take them to a few megabytes. The cap
|
||||
@@ -32,9 +34,17 @@ const maxVorbisCommentSize = 16 << 20
|
||||
var errMalformedComment = errors.New("library: malformed Vorbis comment")
|
||||
|
||||
// readFLACGenreValues returns every GENRE value in a FLAC file's comment block.
|
||||
// (nil, nil) means the file is readable and declares no genre. rs is seeked to
|
||||
// the start, so it is safe to call after dhowden/tag has consumed the reader.
|
||||
func readFLACGenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
// (nil, nil) means the file is readable and declares no genre.
|
||||
func readFLACGenreValues(rs io.ReadSeeker) ([]string, error) { return readFLACValues(rs, "GENRE") }
|
||||
|
||||
// readOggGenreValues returns every GENRE value in an Ogg Vorbis or Opus file.
|
||||
func readOggGenreValues(rs io.ReadSeeker) ([]string, error) { return readOggValues(rs, "GENRE") }
|
||||
|
||||
// readFLACValues returns every value of the comment field key in a FLAC
|
||||
// file's comment block, in file order. (nil, nil) means the file is readable
|
||||
// and has no such field. rs is seeked to the start, so it is safe to call after
|
||||
// dhowden/tag has consumed the reader.
|
||||
func readFLACValues(rs io.ReadSeeker, key string) ([]string, error) {
|
||||
if _, err := rs.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -82,7 +92,7 @@ func readFLACGenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
if _, err := io.ReadFull(rs, body); err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
return vorbisCommentGenres(body)
|
||||
return vorbisCommentValues(body, key)
|
||||
case 127: // invalid by spec; nothing after it can be trusted
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
@@ -95,11 +105,12 @@ func readFLACGenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// readOggGenreValues returns every GENRE value in an Ogg Vorbis or Opus file.
|
||||
// The comments are the stream's second packet, which can span several pages
|
||||
// when it carries cover art, so pages are reassembled until it is complete.
|
||||
// Only the first logical stream is read; pages of any other are skipped.
|
||||
func readOggGenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
// readOggValues returns every value of the comment field key in an Ogg Vorbis
|
||||
// or Opus file. The comments are the stream's second packet, which can span
|
||||
// several pages when it carries cover art, so pages are reassembled until it
|
||||
// is complete. Only the first logical stream is read; pages of any other are
|
||||
// skipped.
|
||||
func readOggValues(rs io.ReadSeeker, key string) ([]string, error) {
|
||||
if _, err := rs.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -149,7 +160,7 @@ func readOggGenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
continue // the packet carries on in the next segment
|
||||
}
|
||||
if packetIdx == 1 {
|
||||
return oggCommentGenres(packet)
|
||||
return oggCommentValues(packet, key)
|
||||
}
|
||||
packetIdx++
|
||||
packet = packet[:0]
|
||||
@@ -157,24 +168,24 @@ func readOggGenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// oggCommentGenres strips the codec's comment-packet prefix. Ogg FLAC and Speex
|
||||
// oggCommentValues strips the codec's comment-packet prefix. Ogg FLAC and Speex
|
||||
// are rare enough to leave to dhowden/tag.
|
||||
func oggCommentGenres(packet []byte) ([]string, error) {
|
||||
func oggCommentValues(packet []byte, key string) ([]string, error) {
|
||||
switch {
|
||||
case bytes.HasPrefix(packet, []byte("\x03vorbis")):
|
||||
return vorbisCommentGenres(packet[7:])
|
||||
return vorbisCommentValues(packet[7:], key)
|
||||
case bytes.HasPrefix(packet, []byte("OpusTags")):
|
||||
return vorbisCommentGenres(packet[8:])
|
||||
return vorbisCommentValues(packet[8:], key)
|
||||
}
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
|
||||
// vorbisCommentGenres reads a comment block (vendor string, then a count of
|
||||
// vorbisCommentValues reads a comment block (vendor string, then a count of
|
||||
// length-prefixed "KEY=value" entries, little-endian lengths throughout) and
|
||||
// returns the GENRE values in file order. Field names are case-insensitive by
|
||||
// spec. Anything after the last entry, such as Vorbis's framing bit, is
|
||||
// ignored.
|
||||
func vorbisCommentGenres(b []byte) ([]string, error) {
|
||||
// returns the values of field key in file order. Field names are
|
||||
// case-insensitive by spec. Anything after the last entry, such as Vorbis's
|
||||
// framing bit, is ignored.
|
||||
func vorbisCommentValues(b []byte, key string) ([]string, error) {
|
||||
next := func() ([]byte, bool) {
|
||||
if len(b) < 4 {
|
||||
return nil, false
|
||||
@@ -203,8 +214,8 @@ func vorbisCommentGenres(b []byte) ([]string, error) {
|
||||
if !ok {
|
||||
return nil, errMalformedComment
|
||||
}
|
||||
key, value, found := strings.Cut(string(field), "=")
|
||||
if !found || !strings.EqualFold(key, "GENRE") {
|
||||
name, value, found := strings.Cut(string(field), "=")
|
||||
if !found || !strings.EqualFold(name, key) {
|
||||
continue
|
||||
}
|
||||
if value = strings.TrimSpace(value); value != "" {
|
||||
|
||||
Reference in New Issue
Block a user