musikr: remove musictype auxio dependency

This commit is contained in:
Alexander Capehart 2024-12-13 19:50:45 -07:00
parent c70c27a7b4
commit 3da9e6c5b3
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
5 changed files with 32 additions and 25 deletions

View file

@ -81,23 +81,34 @@ sealed interface Music : Item {
class UID class UID
private constructor( private constructor(
private val format: Format, private val format: Format,
private val type: MusicType, private val item: Item,
private val uuid: UUID private val uuid: UUID
) : Parcelable { ) : Parcelable {
// Cache the hashCode for HashMap efficiency. // Cache the hashCode for HashMap efficiency.
@IgnoredOnParcel private var hashCode = format.hashCode() @IgnoredOnParcel private var hashCode = format.hashCode()
init { init {
hashCode = 31 * hashCode + type.hashCode() hashCode = 31 * hashCode + item.hashCode()
hashCode = 31 * hashCode + uuid.hashCode() hashCode = 31 * hashCode + uuid.hashCode()
} }
override fun hashCode() = hashCode override fun hashCode() = hashCode
override fun equals(other: Any?) = override fun equals(other: Any?) =
other is UID && format == other.format && type == other.type && uuid == other.uuid other is UID && format == other.format && item == other.item && uuid == other.uuid
override fun toString() = "${format.namespace}:${type.intCode.toString(16)}-$uuid" override fun toString() = "${format.namespace}:${item.intCode.toString(16)}-$uuid"
enum class Item(val intCode: Int) {
// Item used to be MusicType back when the music module was
// part of Auxio, so these old integer codes remain.
// TODO: Introduce new UID format that removes these.
SONG(0xA10B),
ALBUM(0xA10A),
ARTIST(0xA109),
GENRE(0xA108),
PLAYLIST(0xA107)
}
/** /**
* Internal marker of [Music.UID] format type. * Internal marker of [Music.UID] format type.
@ -125,23 +136,23 @@ sealed interface Music : Item {
* Creates an Auxio-style [UID] of random composition. Used if there is no * Creates an Auxio-style [UID] of random composition. Used if there is no
* non-subjective, unlikely-to-change metadata of the music. * non-subjective, unlikely-to-change metadata of the music.
* *
* @param type The analogous [MusicType] of the item that created this [UID]. * @param item The type of [Item] that created this [UID].
*/ */
fun auxio(type: MusicType): UID { fun auxio(item: Item): UID {
return UID(Format.AUXIO, type, UUID.randomUUID()) return UID(Format.AUXIO, item, UUID.randomUUID())
} }
/** /**
* Creates an Auxio-style [UID] with a [UUID] composed of a hash of the non-subjective, * Creates an Auxio-style [UID] with a [UUID] composed of a hash of the non-subjective,
* unlikely-to-change metadata of the music. * unlikely-to-change metadata of the music.
* *
* @param type The analogous [MusicType] of the item that created this [UID]. * @param item The type of [Item] that created this [UID].
* @param updates Block to update the [MessageDigest] hash with the metadata of the * @param updates Block to update the [MessageDigest] hash with the metadata of the
* item. Make sure the metadata hashed semantically aligns with the format * item. Make sure the metadata hashed semantically aligns with the format
* specification. * specification.
* @return A new auxio-style [UID]. * @return A new auxio-style [UID].
*/ */
fun auxio(type: MusicType, updates: MessageDigest.() -> Unit): UID { fun auxio(item: Item, updates: MessageDigest.() -> Unit): UID {
val digest = val digest =
MessageDigest.getInstance("SHA-256").run { MessageDigest.getInstance("SHA-256").run {
updates() updates()
@ -171,19 +182,19 @@ sealed interface Music : Item {
.or(digest[13].toLong().and(0xFF).shl(16)) .or(digest[13].toLong().and(0xFF).shl(16))
.or(digest[14].toLong().and(0xFF).shl(8)) .or(digest[14].toLong().and(0xFF).shl(8))
.or(digest[15].toLong().and(0xFF))) .or(digest[15].toLong().and(0xFF)))
return UID(Format.AUXIO, type, uuid) return UID(Format.AUXIO, item, uuid)
} }
/** /**
* Creates a MusicBrainz-style [UID] with a [UUID] derived from the MusicBrainz ID * Creates a MusicBrainz-style [UID] with a [UUID] derived from the MusicBrainz ID
* extracted from a file. * extracted from a file.
* *
* @param type The analogous [MusicType] of the item that created this [UID]. * @param item The analogous [MusicType] of the item that created this [UID].
* @param mbid The analogous MusicBrainz ID for this item that was extracted from a * @param mbid The analogous MusicBrainz ID for this item that was extracted from a
* file. * file.
* @return A new MusicBrainz-style [UID]. * @return A new MusicBrainz-style [UID].
*/ */
fun musicBrainz(type: MusicType, mbid: UUID) = UID(Format.MUSICBRAINZ, type, mbid) fun musicBrainz(item: Item, mbid: UUID) = UID(Format.MUSICBRAINZ, item, mbid)
/** /**
* Convert a [UID]'s string representation back into a concrete [UID] instance. * Convert a [UID]'s string representation back into a concrete [UID] instance.
@ -211,8 +222,8 @@ sealed interface Music : Item {
return null return null
} }
val type = val intCode = ids[0].toIntOrNull(16) ?: return null
MusicType.fromIntCode(ids[0].toIntOrNull(16) ?: return null) ?: return null val type = Item.entries.firstOrNull { it.intCode == intCode } ?: return null
val uuid = ids[1].toUuidOrNull() ?: return null val uuid = ids[1].toUuidOrNull() ?: return null
return UID(format, type, uuid) return UID(format, type, uuid)
} }

View file

@ -18,7 +18,6 @@
package org.oxycblt.musikr.model package org.oxycblt.musikr.model
import org.oxycblt.auxio.music.MusicType
import org.oxycblt.auxio.util.update import org.oxycblt.auxio.util.update
import org.oxycblt.musikr.Album import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist import org.oxycblt.musikr.Artist
@ -45,8 +44,8 @@ class AlbumImpl(private val core: AlbumCore) : Album {
override val uid = override val uid =
// Attempt to use a MusicBrainz ID first before falling back to a hashed UID. // Attempt to use a MusicBrainz ID first before falling back to a hashed UID.
preAlbum.musicBrainzId?.let { Music.UID.musicBrainz(MusicType.ALBUMS, it) } preAlbum.musicBrainzId?.let { Music.UID.musicBrainz(Music.UID.Item.ALBUM, it) }
?: Music.UID.auxio(MusicType.ALBUMS) { ?: Music.UID.auxio(Music.UID.Item.ALBUM) {
// Hash based on only names despite the presence of a date to increase stability. // Hash based on only names despite the presence of a date to increase stability.
// I don't know if there is any situation where an artist will have two albums with // I don't know if there is any situation where an artist will have two albums with
// the exact same name, but if there is, I would love to know. // the exact same name, but if there is, I would love to know.

View file

@ -18,7 +18,6 @@
package org.oxycblt.musikr.model package org.oxycblt.musikr.model
import org.oxycblt.auxio.music.MusicType
import org.oxycblt.auxio.util.update import org.oxycblt.auxio.util.update
import org.oxycblt.musikr.Album import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist import org.oxycblt.musikr.Artist
@ -44,8 +43,8 @@ interface ArtistCore {
class ArtistImpl(private val core: ArtistCore) : Artist { class ArtistImpl(private val core: ArtistCore) : Artist {
override val uid = override val uid =
// Attempt to use a MusicBrainz ID first before falling back to a hashed UID. // Attempt to use a MusicBrainz ID first before falling back to a hashed UID.
core.preArtist.musicBrainzId?.let { Music.UID.musicBrainz(MusicType.ARTISTS, it) } core.preArtist.musicBrainzId?.let { Music.UID.musicBrainz(Music.UID.Item.ARTIST, it) }
?: Music.UID.auxio(MusicType.ARTISTS) { update(core.preArtist.rawName) } ?: Music.UID.auxio(Music.UID.Item.ARTIST) { update(core.preArtist.rawName) }
override val name = core.preArtist.name override val name = core.preArtist.name
override val songs = core.songs override val songs = core.songs

View file

@ -18,7 +18,6 @@
package org.oxycblt.musikr.model package org.oxycblt.musikr.model
import org.oxycblt.auxio.music.MusicType
import org.oxycblt.auxio.util.update import org.oxycblt.auxio.util.update
import org.oxycblt.musikr.Artist import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre import org.oxycblt.musikr.Genre
@ -39,7 +38,7 @@ interface GenreCore {
* @author Alexander Capehart (OxygenCobalt) * @author Alexander Capehart (OxygenCobalt)
*/ */
class GenreImpl(private val core: GenreCore) : Genre { class GenreImpl(private val core: GenreCore) : Genre {
override val uid = Music.UID.auxio(MusicType.GENRES) { update(core.preGenre.rawName) } override val uid = Music.UID.auxio(Music.UID.Item.GENRE) { update(core.preGenre.rawName) }
override val name = core.preGenre.name override val name = core.preGenre.name
override val songs = mutableSetOf<Song>() override val songs = mutableSetOf<Song>()

View file

@ -20,7 +20,6 @@ package org.oxycblt.musikr.tag.interpret
import android.net.Uri import android.net.Uri
import java.util.UUID import java.util.UUID
import org.oxycblt.auxio.music.MusicType
import org.oxycblt.auxio.playback.replaygain.ReplayGainAdjustment import org.oxycblt.auxio.playback.replaygain.ReplayGainAdjustment
import org.oxycblt.auxio.util.update import org.oxycblt.auxio.util.update
import org.oxycblt.musikr.Music import org.oxycblt.musikr.Music
@ -54,8 +53,8 @@ data class PreSong(
val preGenres: List<PreGenre> val preGenres: List<PreGenre>
) { ) {
fun computeUid() = fun computeUid() =
musicBrainzId?.let { Music.UID.musicBrainz(MusicType.SONGS, it) } musicBrainzId?.let { Music.UID.musicBrainz(Music.UID.Item.SONG, it) }
?: Music.UID.auxio(MusicType.SONGS) { ?: Music.UID.auxio(Music.UID.Item.SONG) {
// Song UIDs are based on the raw data without parsing so that they remain // Song UIDs are based on the raw data without parsing so that they remain
// consistent across music setting changes. Parents are not held up to the // consistent across music setting changes. Parents are not held up to the
// same standard since grouping is already inherently linked to settings. // same standard since grouping is already inherently linked to settings.