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 }