Handle composer fallback in parser

This commit is contained in:
Alexander Capehart
2025-11-03 12:45:06 -07:00
parent a72424487d
commit c29e40422a
3 changed files with 65 additions and 3 deletions
@@ -152,6 +152,43 @@ internal fun Metadata.artistSortNames() =
?: id3v2["TXXX:ARTISTSORT"] ?: id3v2["TXXX:ARTISTSORT"]
?: id3v2["TXXX:ARTIST SORT"]) ?: id3v2["TXXX:ARTIST SORT"])
internal fun Metadata.composerMusicBrainzIds() =
(xiph["MUSICBRAINZ_COMPOSERID"]
?: xiph["MUSICBRAINZ COMPOSER ID"]
?: mp4["----:COM.APPLE.ITUNES:MUSICBRAINZ COMPOSER ID"]
?: mp4["----:COM.APPLE.ITUNES:MUSICBRAINZ_COMPOSERID"]
?: id3v2["TXXX:MUSICBRAINZ COMPOSER ID"]
?: id3v2["TXXX:MUSICBRAINZ_COMPOSERID"])
internal fun Metadata.composerNames() =
(xiph["COMPOSERS"]
?: xiph["COMPOSER"]
?: mp4["----:COM.APPLE.ITUNES:COMPOSERS"]
?: mp4["----:COM.APPLE.ITUNES:COMPOSER"]
?: mp4["©wrt"]
?: id3v2["TXXX:COMPOSERS"]
?: id3v2["TCOM"]
?: id3v2["TXXX:COMPOSER"])
internal fun Metadata.composerSortNames() =
(xiph["COMPOSERSSORT"]
?: xiph["COMPOSERS_SORT"]
?: xiph["COMPOSERS SORT"]
?: xiph["COMPOSERSORT"]
?: xiph["COMPOSER SORT"]
?: mp4["----:COM.APPLE.ITUNES:COMPOSERSSORT"]
?: mp4["----:COM.APPLE.ITUNES:COMPOSERS_SORT"]
?: mp4["----:COM.APPLE.ITUNES:COMPOSERS SORT"]
?: mp4["----:COM.APPLE.ITUNES:COMPOSERSORT"]
?: mp4["soCo"]
?: mp4["----:COM.APPLE.ITUNES:COMPOSER SORT"]
?: id3v2["TXXX:COMPOSERSSORT"]
?: id3v2["TXXX:COMPOSERS_SORT"]
?: id3v2["TXXX:COMPOSERS SORT"]
?: id3v2["TXXX:COMPOSERSORT"]
?: id3v2["TSOC"]
?: id3v2["TXXX:COMPOSER SORT"])
internal fun Metadata.albumArtistMusicBrainzIds() = internal fun Metadata.albumArtistMusicBrainzIds() =
(xiph["MUSICBRAINZ_ALBUMARTISTID"] (xiph["MUSICBRAINZ_ALBUMARTISTID"]
?: xiph["MUSICBRAINZ ALBUM ARTIST ID"] ?: xiph["MUSICBRAINZ ALBUM ARTIST ID"]
@@ -31,6 +31,14 @@ internal interface TagParser {
private data object TagParserImpl : TagParser { private data object TagParserImpl : TagParser {
override fun parse(metadata: Metadata): ParsedTags { override fun parse(metadata: Metadata): ParsedTags {
val compilation = metadata.isCompilation() val compilation = metadata.isCompilation()
val artistMusicBrainzIds =
metadata.artistMusicBrainzIds()
?: metadata.composerMusicBrainzIds()
?: listOf()
val artistNames =
metadata.artistNames() ?: metadata.composerNames() ?: listOf()
val artistSortNames =
metadata.artistSortNames() ?: metadata.composerSortNames() ?: listOf()
return ParsedTags( return ParsedTags(
durationMs = metadata.properties.durationMs, durationMs = metadata.properties.durationMs,
replayGainTrackAdjustment = metadata.replayGainTrackAdjustment(), replayGainTrackAdjustment = metadata.replayGainTrackAdjustment(),
@@ -49,9 +57,9 @@ private data object TagParserImpl : TagParser {
// we don't have any other release types // we don't have any other release types
releaseTypes = releaseTypes =
metadata.releaseTypes() ?: listOf("compilation").takeIf { compilation } ?: listOf(), metadata.releaseTypes() ?: listOf("compilation").takeIf { compilation } ?: listOf(),
artistMusicBrainzIds = metadata.artistMusicBrainzIds() ?: listOf(), artistMusicBrainzIds = artistMusicBrainzIds,
artistNames = metadata.artistNames() ?: listOf(), artistNames = artistNames,
artistSortNames = metadata.artistSortNames() ?: listOf(), artistSortNames = artistSortNames,
albumArtistMusicBrainzIds = metadata.albumArtistMusicBrainzIds() ?: listOf(), albumArtistMusicBrainzIds = metadata.albumArtistMusicBrainzIds() ?: listOf(),
// Compilation pretty heavily implies various artists in the case that we don't // Compilation pretty heavily implies various artists in the case that we don't
// have any other album artists // have any other album artists
@@ -145,6 +145,23 @@ class TagParserTest {
assertEquals(-2.1f, tags.replayGainAlbumAdjustment) assertEquals(-2.1f, tags.replayGainAlbumAdjustment)
} }
@Test
fun tagParser_composerFallbackForArtistInfo() {
val metadata =
createTestMetadata(
id3v2Tags =
mapOf(
"TCOM" to listOf("Composer Artist"),
"TSOC" to listOf("Composer Artist Sort"),
"TXXX:MUSICBRAINZ COMPOSER ID" to listOf("composer-artist-mbid")))
val tags = tagParser.parse(metadata)
assertEquals(listOf("Composer Artist"), tags.artistNames)
assertEquals(listOf("Composer Artist Sort"), tags.artistSortNames)
assertEquals(listOf("composer-artist-mbid"), tags.artistMusicBrainzIds)
}
private fun createTestMetadata( private fun createTestMetadata(
id3v2Tags: Map<String, List<String>> = emptyMap(), id3v2Tags: Map<String, List<String>> = emptyMap(),
xiphTags: Map<String, List<String>> = emptyMap(), xiphTags: Map<String, List<String>> = emptyMap(),