Skip to content

Commit

Permalink
Propagate ID3 TCON frame to MediaMetada.genre
Browse files Browse the repository at this point in the history
This change also includes mapping the numeric ID3v1 codes to their
string equivalents before setting them into `MediaMetadata`. This
mapping already existed, but it was previously only used when parsing
MP4 `gnre` atoms.

Issue: #1305
PiperOrigin-RevId: 629113480
  • Loading branch information
icbaker authored and Copybara-Service committed Apr 29, 2024
1 parent c6492e0 commit 96bc9e9
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 216 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
([#1302](https://github.com/androidx/media/issues/1302)).
* Fix reading of MP4 (/iTunes) numeric `gnre` (genre) and `tmpo` (tempo)
tags when the value is more than one byte long.
* Propagate ID3 `TCON` frame to `MediaMetadata.genre`
([#1305](https://github.com/androidx/media/issues/1305)).
* Image:
* DRM:
* Allow setting a `LoadErrorHandlingPolicy` on
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.extractor.metadata.id3;

import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi;
import com.google.common.collect.ImmutableList;

/** Utility methods for working with ID3 metadata. */
@UnstableApi
public final class Id3Util {

private static final ImmutableList<String> STANDARD_GENRES =
ImmutableList.of(
// These are the official ID3v1 genres.
"Blues",
"Classic Rock",
"Country",
"Dance",
"Disco",
"Funk",
"Grunge",
"Hip-Hop",
"Jazz",
"Metal",
"New Age",
"Oldies",
"Other",
"Pop",
"R&B",
"Rap",
"Reggae",
"Rock",
"Techno",
"Industrial",
"Alternative",
"Ska",
"Death Metal",
"Pranks",
"Soundtrack",
"Euro-Techno",
"Ambient",
"Trip-Hop",
"Vocal",
"Jazz+Funk",
"Fusion",
"Trance",
"Classical",
"Instrumental",
"Acid",
"House",
"Game",
"Sound Clip",
"Gospel",
"Noise",
"AlternRock",
"Bass",
"Soul",
"Punk",
"Space",
"Meditative",
"Instrumental Pop",
"Instrumental Rock",
"Ethnic",
"Gothic",
"Darkwave",
"Techno-Industrial",
"Electronic",
"Pop-Folk",
"Eurodance",
"Dream",
"Southern Rock",
"Comedy",
"Cult",
"Gangsta",
"Top 40",
"Christian Rap",
"Pop/Funk",
"Jungle",
"Native American",
"Cabaret",
"New Wave",
"Psychadelic",
"Rave",
"Showtunes",
"Trailer",
"Lo-Fi",
"Tribal",
"Acid Punk",
"Acid Jazz",
"Polka",
"Retro",
"Musical",
"Rock & Roll",
"Hard Rock",
// Genres made up by the authors of Winamp (v1.91) and later added to the ID3 spec.
"Folk",
"Folk-Rock",
"National Folk",
"Swing",
"Fast Fusion",
"Bebob",
"Latin",
"Revival",
"Celtic",
"Bluegrass",
"Avantgarde",
"Gothic Rock",
"Progressive Rock",
"Psychedelic Rock",
"Symphonic Rock",
"Slow Rock",
"Big Band",
"Chorus",
"Easy Listening",
"Acoustic",
"Humour",
"Speech",
"Chanson",
"Opera",
"Chamber Music",
"Sonata",
"Symphony",
"Booty Bass",
"Primus",
"Porn Groove",
"Satire",
"Slow Jam",
"Club",
"Tango",
"Samba",
"Folklore",
"Ballad",
"Power Ballad",
"Rhythmic Soul",
"Freestyle",
"Duet",
"Punk Rock",
"Drum Solo",
"A capella",
"Euro-House",
"Dance Hall",
// Genres made up by the authors of Winamp (v1.91) but have not been added to the ID3
// spec.
"Goa",
"Drum & Bass",
"Club-House",
"Hardcore",
"Terror",
"Indie",
"BritPop",
"Afro-Punk",
"Polsk Punk",
"Beat",
"Christian Gangsta Rap",
"Heavy Metal",
"Black Metal",
"Crossover",
"Contemporary Christian",
"Christian Rock",
"Merengue",
"Salsa",
"Thrash Metal",
"Anime",
"Jpop",
"Synthpop",
// Genres made up by the authors of Winamp (v5.6) but have not been added to the ID3 spec.
"Abstract",
"Art Rock",
"Baroque",
"Bhangra",
"Big beat",
"Breakbeat",
"Chillout",
"Downtempo",
"Dub",
"EBM",
"Eclectic",
"Electro",
"Electroclash",
"Emo",
"Experimental",
"Garage",
"Global",
"IDM",
"Illbient",
"Industro-Goth",
"Jam Band",
"Krautrock",
"Leftfield",
"Lounge",
"Math Rock",
"New Romantic",
"Nu-Breakz",
"Post-Punk",
"Post-Rock",
"Psytrance",
"Shoegaze",
"Space Rock",
"Trop Rock",
"World Music",
"Neoclassical",
"Audiobook",
"Audio theatre",
"Neue Deutsche Welle",
"Podcast",
"Indie-Rock",
"G-Funk",
"Dubstep",
"Garage Rock",
"Psybient");

/**
* Resolves an ID3v1 numeric genre code to its string form, or {@code null} if the code isn't
* recognized.
*
* <p>Includes codes that were added later by various versions of Winamp. See this Wikipedia <a
* href="https://en.wikipedia.org/wiki/List_of_ID3v1_genres">list of official and unofficial ID3v1
* genres</a>.
*/
@Nullable
public static String resolveV1Genre(int id3v1GenreCode) {
return id3v1GenreCode >= 0 && id3v1GenreCode < STANDARD_GENRES.size()
? STANDARD_GENRES.get(id3v1GenreCode)
: null;
}

private Id3Util() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import com.google.errorprone.annotations.InlineMe;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -175,6 +176,18 @@ public void populateMediaMetadata(MediaMetadata.Builder builder) {
case "TEXT":
builder.setWriter(values.get(0));
break;
case "TCON":
@Nullable Integer genreCode = Ints.tryParse(values.get(0));
if (genreCode == null) {
builder.setGenre(values.get(0));
break;
}
@Nullable String genre = Id3Util.resolveV1Genre(genreCode);
if (genre != null) {
builder.setGenre(genre);
}
// Don't set a numeric genre that we don't recognize.
break;
default:
break;
}
Expand Down
Loading

0 comments on commit 96bc9e9

Please sign in to comment.