Add library snapshot song record codec
This commit is contained in:
@@ -18,9 +18,11 @@
|
||||
|
||||
package org.oxycblt.musikr.snapshot
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import java.io.DataInputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
@@ -43,6 +45,11 @@ import org.oxycblt.musikr.tag.Placeholder
|
||||
import org.oxycblt.musikr.tag.ReleaseType
|
||||
import org.oxycblt.musikr.tag.ReplayGainAdjustment
|
||||
import org.oxycblt.musikr.tag.Token
|
||||
import org.oxycblt.musikr.tag.interpret.PreAlbum
|
||||
import org.oxycblt.musikr.tag.interpret.PreArtist
|
||||
import org.oxycblt.musikr.tag.interpret.PreArtistsFrom
|
||||
import org.oxycblt.musikr.tag.interpret.PreGenre
|
||||
import org.oxycblt.musikr.tag.interpret.PreSong
|
||||
|
||||
object LibrarySnapshotCodec {
|
||||
const val MAGIC = "AUXSNAP"
|
||||
@@ -50,6 +57,8 @@ object LibrarySnapshotCodec {
|
||||
private const val MAX_STRING_BYTES = 1024 * 1024
|
||||
private const val MAX_FORMAT_DEPTH = 8
|
||||
private const val MAX_NAME_TOKENS = 10_000
|
||||
private const val MAX_SONG_ARTISTS = 10_000
|
||||
private const val MAX_SONG_GENRES = 10_000
|
||||
private val MAGIC_BYTES = MAGIC.toByteArray(Charsets.US_ASCII)
|
||||
|
||||
data class Header(val magic: String, val schemaVersion: Int)
|
||||
@@ -507,4 +516,155 @@ object LibrarySnapshotCodec {
|
||||
}
|
||||
return KnownName(raw, sort, tokens)
|
||||
}
|
||||
|
||||
internal fun writePreSong(output: OutputStream, value: PreSong) {
|
||||
writeUid(output, value.v363Uid)
|
||||
writeUid(output, value.v400Uid)
|
||||
writeUid(output, value.v401Uid)
|
||||
writeMusicBrainzId(output, value.musicBrainzId)
|
||||
writeName(output, value.name)
|
||||
writeString(output, value.rawName)
|
||||
writeNullableInt(output, value.track)
|
||||
writeBoolean(output, value.disc != null)
|
||||
value.disc?.let { writeDisc(output, it) }
|
||||
writeBoolean(output, value.date != null)
|
||||
value.date?.let { writeDate(output, it) }
|
||||
writeFileProperties(
|
||||
output,
|
||||
FileProperties(
|
||||
value.uri,
|
||||
value.path,
|
||||
value.format,
|
||||
value.size,
|
||||
value.modifiedMs,
|
||||
value.addedMs,
|
||||
),
|
||||
)
|
||||
writeLong(output, value.durationMs)
|
||||
writeInt(output, value.bitrateKbps)
|
||||
writeInt(output, value.sampleRateHz)
|
||||
writeReplayGainAdjustment(output, value.replayGainAdjustment)
|
||||
writeNullableString(output, value.cover?.id)
|
||||
writePreAlbum(output, value.preAlbum)
|
||||
writePreArtists(output, value.preArtists)
|
||||
writeCount(output, value.preGenres.size, MAX_SONG_GENRES)
|
||||
value.preGenres.forEach { writePreGenre(output, it) }
|
||||
}
|
||||
|
||||
internal fun readPreSong(
|
||||
input: InputStream,
|
||||
storedCoverDirectory: File,
|
||||
context: Context,
|
||||
): PreSong {
|
||||
val v363Uid = readUid(input)
|
||||
val v400Uid = readUid(input)
|
||||
val v401Uid = readUid(input)
|
||||
val musicBrainzId = readMusicBrainzId(input)
|
||||
val name = readName(input) as? Name.Known ?: throw IOException("Invalid snapshot song name")
|
||||
val rawName = readString(input)
|
||||
val track = readNullableInt(input)
|
||||
val disc = if (readBoolean(input)) readDisc(input) else null
|
||||
val date = if (readBoolean(input)) readDate(input) else null
|
||||
val fileProperties = readFileProperties(input)
|
||||
val durationMs = readLong(input)
|
||||
val bitrateKbps = readInt(input)
|
||||
val sampleRateHz = readInt(input)
|
||||
val replayGainAdjustment = readReplayGainAdjustment(input)
|
||||
val coverId = readNullableString(input)
|
||||
val cover = LibrarySnapshotCovers.restore(coverId, storedCoverDirectory, context)
|
||||
val preAlbum = readPreAlbum(input)
|
||||
val preArtists = readPreArtists(input)
|
||||
val preGenres = List(readCount(input, MAX_SONG_GENRES)) { readPreGenre(input) }
|
||||
return PreSong(
|
||||
v363Uid = v363Uid,
|
||||
v400Uid = v400Uid,
|
||||
v401Uid = v401Uid,
|
||||
musicBrainzId = musicBrainzId,
|
||||
name = name,
|
||||
rawName = rawName,
|
||||
track = track,
|
||||
disc = disc,
|
||||
date = date,
|
||||
uri = fileProperties.uri,
|
||||
path = fileProperties.path,
|
||||
format = fileProperties.format,
|
||||
size = fileProperties.size,
|
||||
durationMs = durationMs,
|
||||
bitrateKbps = bitrateKbps,
|
||||
sampleRateHz = sampleRateHz,
|
||||
replayGainAdjustment = replayGainAdjustment,
|
||||
modifiedMs = fileProperties.modifiedMs,
|
||||
addedMs = fileProperties.addedMs,
|
||||
cover = cover,
|
||||
preAlbum = preAlbum,
|
||||
preArtists = preArtists,
|
||||
preGenres = preGenres,
|
||||
)
|
||||
}
|
||||
|
||||
private fun writeNullableInt(output: OutputStream, value: Int?) {
|
||||
writeBoolean(output, value != null)
|
||||
value?.let { writeInt(output, it) }
|
||||
}
|
||||
|
||||
private fun readNullableInt(input: InputStream): Int? {
|
||||
return if (readBoolean(input)) readInt(input) else null
|
||||
}
|
||||
|
||||
private fun writePreAlbum(output: OutputStream, value: PreAlbum) {
|
||||
writeMusicBrainzId(output, value.musicBrainzId)
|
||||
writeName(output, value.name)
|
||||
writeNullableString(output, value.rawName)
|
||||
writeReleaseType(output, value.releaseType)
|
||||
when (val artists = value.preArtists) {
|
||||
is PreArtistsFrom.Individual -> {
|
||||
writeInt(output, 0)
|
||||
writePreArtists(output, artists.preArtists)
|
||||
}
|
||||
is PreArtistsFrom.Album -> {
|
||||
writeInt(output, 1)
|
||||
writePreArtists(output, artists.preArtists)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun readPreAlbum(input: InputStream): PreAlbum {
|
||||
val musicBrainzId = readMusicBrainzId(input)
|
||||
val name = readName(input)
|
||||
val rawName = readNullableString(input)
|
||||
val releaseType = readReleaseType(input)
|
||||
val artistsTag = readInt(input)
|
||||
val preArtists = readPreArtists(input)
|
||||
val artists =
|
||||
when (artistsTag) {
|
||||
0 -> PreArtistsFrom.Individual(preArtists)
|
||||
1 -> PreArtistsFrom.Album(preArtists)
|
||||
else -> throw IOException("Invalid snapshot album artist source")
|
||||
}
|
||||
return PreAlbum(musicBrainzId, name, rawName, releaseType, artists)
|
||||
}
|
||||
|
||||
private fun writePreArtists(output: OutputStream, value: List<PreArtist>) {
|
||||
writeCount(output, value.size, MAX_SONG_ARTISTS)
|
||||
for (artist in value) {
|
||||
writeMusicBrainzId(output, artist.musicBrainzId)
|
||||
writeName(output, artist.name)
|
||||
writeNullableString(output, artist.rawName)
|
||||
}
|
||||
}
|
||||
|
||||
private fun readPreArtists(input: InputStream): List<PreArtist> {
|
||||
return List(readCount(input, MAX_SONG_ARTISTS)) {
|
||||
PreArtist(readMusicBrainzId(input), readName(input), readNullableString(input))
|
||||
}
|
||||
}
|
||||
|
||||
private fun writePreGenre(output: OutputStream, value: PreGenre) {
|
||||
writeName(output, value.name)
|
||||
writeNullableString(output, value.rawName)
|
||||
}
|
||||
|
||||
private fun readPreGenre(input: InputStream): PreGenre {
|
||||
return PreGenre(readName(input), readNullableString(input))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* LibrarySnapshotSongCodecTest.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.snapshot
|
||||
|
||||
import android.net.Uri
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.nio.file.Files
|
||||
import java.util.UUID
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
import org.oxycblt.musikr.fs.Format
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.path.ExternalVolumeValue
|
||||
import org.oxycblt.musikr.fs.path.InternalVolumeValue
|
||||
import org.oxycblt.musikr.tag.Date
|
||||
import org.oxycblt.musikr.tag.Disc
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
import org.oxycblt.musikr.tag.Placeholder
|
||||
import org.oxycblt.musikr.tag.ReleaseType
|
||||
import org.oxycblt.musikr.tag.ReplayGainAdjustment
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
import org.oxycblt.musikr.tag.interpret.PreAlbum
|
||||
import org.oxycblt.musikr.tag.interpret.PreArtist
|
||||
import org.oxycblt.musikr.tag.interpret.PreArtistsFrom
|
||||
import org.oxycblt.musikr.tag.interpret.PreGenre
|
||||
import org.oxycblt.musikr.tag.interpret.PreSong
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [30])
|
||||
class LibrarySnapshotSongCodecTest {
|
||||
@Test
|
||||
fun preSong_roundTrip_preservesEveryCapturedField() {
|
||||
val storedCoverDirectory =
|
||||
Files.createTempDirectory("auxio-song-cover").toFile().apply { deleteOnExit() }
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val value =
|
||||
PreSong(
|
||||
v363Uid =
|
||||
requireNotNull(
|
||||
Music.UID.fromString("uas00112233-4455-6677-8899-aabbccddeeff")
|
||||
),
|
||||
v400Uid =
|
||||
requireNotNull(
|
||||
Music.UID.fromString("ums11111111-2222-4333-8444-555566667777")
|
||||
),
|
||||
v401Uid =
|
||||
requireNotNull(
|
||||
Music.UID.fromString("uas88888888-9999-4aaa-bbbb-ccccddddeeee")
|
||||
),
|
||||
musicBrainzId = UUID.fromString("01234567-89ab-4cde-8fab-0123456789ab"),
|
||||
name = Naming.simple().name("15-9", "nine"),
|
||||
rawName = "15-9",
|
||||
track = 9,
|
||||
disc = Disc(2, "Bonus 界"),
|
||||
date = requireNotNull(Date.from("2016-08-16T00:01:02Z")),
|
||||
uri =
|
||||
Uri.parse(
|
||||
"content://media/7AFE-060A/audio/media/42/Music/%E5%88%9D%E9%9F%B3.ogg"
|
||||
),
|
||||
path =
|
||||
Path(
|
||||
ExternalVolumeValue(
|
||||
"7AFE-060A",
|
||||
"7afe-060a",
|
||||
Components.parseUnix("/storage/7AFE-060A"),
|
||||
"SD card",
|
||||
),
|
||||
Components.parseUnix("Development/Music/Library/初音/15-9.ogg"),
|
||||
),
|
||||
format = Format.Ogg(Format.Opus),
|
||||
size = 17_130_325L,
|
||||
durationMs = 256_008L,
|
||||
bitrateKbps = 256,
|
||||
sampleRateHz = 48_000,
|
||||
replayGainAdjustment = ReplayGainAdjustment(track = -6.5f, album = -9.0f),
|
||||
modifiedMs = 1_794_589_521_000L,
|
||||
addedMs = 1_794_619_280_000L,
|
||||
cover =
|
||||
requireNotNull(
|
||||
LibrarySnapshotCovers.restore("mcs:cover.webp", storedCoverDirectory, context)
|
||||
),
|
||||
preAlbum =
|
||||
PreAlbum(
|
||||
musicBrainzId =
|
||||
UUID.fromString("12345678-9abc-4def-8123-456789abcdef"),
|
||||
name = Naming.simple().name("The Silent Force", "Silent Force"),
|
||||
rawName = "The Silent Force",
|
||||
releaseType = ReleaseType.Compilation(ReleaseType.Refinement.LIVE),
|
||||
preArtists =
|
||||
PreArtistsFrom.Individual(
|
||||
listOf(
|
||||
PreArtist(
|
||||
UUID.fromString(
|
||||
"abcdef01-2345-4678-9abc-def012345678"
|
||||
),
|
||||
Naming.simple().name("初音ミク", "Hatsune Miku"),
|
||||
"初音ミク",
|
||||
)
|
||||
)
|
||||
),
|
||||
),
|
||||
preArtists =
|
||||
listOf(
|
||||
PreArtist(
|
||||
UUID.fromString("abcdef01-2345-4678-9abc-def012345678"),
|
||||
Naming.simple().name("初音ミク", "Hatsune Miku"),
|
||||
"初音ミク",
|
||||
),
|
||||
PreArtist(null, Naming.simple().name("Within Temptation", null), "Within Temptation"),
|
||||
),
|
||||
preGenres =
|
||||
listOf(
|
||||
PreGenre(Naming.simple().name("J-pop", null), "J-pop"),
|
||||
PreGenre(Naming.simple().name("Symphonic 0032", null), "Symphonic 0032"),
|
||||
),
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writePreSong(output, value)
|
||||
val decoded =
|
||||
LibrarySnapshotCodec.readPreSong(
|
||||
ByteArrayInputStream(output.toByteArray()),
|
||||
storedCoverDirectory,
|
||||
context,
|
||||
)
|
||||
|
||||
assertEquals(value, decoded)
|
||||
assertEquals(value.disc?.name, decoded.disc?.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun preSong_optionalAndCompatibilityFields_roundTripWithoutSubstitutingDefaults() {
|
||||
val storedCoverDirectory =
|
||||
Files.createTempDirectory("auxio-optional-song").toFile().apply { deleteOnExit() }
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val unknownArtist = PreArtist(null, Name.Unknown(Placeholder.ARTIST), null)
|
||||
val value =
|
||||
PreSong(
|
||||
v363Uid =
|
||||
requireNotNull(
|
||||
Music.UID.fromString("uas11111111-2222-4333-8444-555566667777")
|
||||
),
|
||||
v400Uid =
|
||||
requireNotNull(
|
||||
Music.UID.fromString("uas22222222-3333-4444-8555-666677778888")
|
||||
),
|
||||
v401Uid =
|
||||
requireNotNull(
|
||||
Music.UID.fromString("ums33333333-4444-4555-8666-777788889999")
|
||||
),
|
||||
musicBrainzId = null,
|
||||
name = Naming.simple().name("Loose file", null),
|
||||
rawName = "Loose file",
|
||||
track = null,
|
||||
disc = null,
|
||||
date = null,
|
||||
uri = Uri.parse("content://media/internal/audio/media/7"),
|
||||
path =
|
||||
Path(
|
||||
InternalVolumeValue(
|
||||
null,
|
||||
Components.parseUnix("/storage/emulated/0"),
|
||||
"Internal shared storage",
|
||||
),
|
||||
Components.parseUnix("Download/Loose file.mp3"),
|
||||
),
|
||||
format = Format.MPEG3,
|
||||
size = 8_192L,
|
||||
durationMs = 1_000L,
|
||||
bitrateKbps = 128,
|
||||
sampleRateHz = 44_100,
|
||||
replayGainAdjustment = ReplayGainAdjustment(track = null, album = null),
|
||||
modifiedMs = 0L,
|
||||
addedMs = 1L,
|
||||
cover = null,
|
||||
preAlbum =
|
||||
PreAlbum(
|
||||
musicBrainzId = null,
|
||||
name = Name.Unknown(Placeholder.ALBUM),
|
||||
rawName = null,
|
||||
releaseType = ReleaseType.Album(null),
|
||||
preArtists = PreArtistsFrom.Album(listOf(unknownArtist)),
|
||||
),
|
||||
preArtists = listOf(unknownArtist),
|
||||
preGenres = listOf(PreGenre(Name.Unknown(Placeholder.GENRE), null)),
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writePreSong(output, value)
|
||||
val decoded =
|
||||
LibrarySnapshotCodec.readPreSong(
|
||||
ByteArrayInputStream(output.toByteArray()),
|
||||
storedCoverDirectory,
|
||||
context,
|
||||
)
|
||||
val decodedUids = listOf(decoded.v363Uid, decoded.v400Uid, decoded.v401Uid)
|
||||
|
||||
assertEquals(value, decoded)
|
||||
assertEquals(listOf(value.v363Uid, value.v400Uid, value.v401Uid), decodedUids)
|
||||
assertEquals(3, decodedUids.toSet().size)
|
||||
assertNull(decoded.musicBrainzId)
|
||||
assertNull(decoded.track)
|
||||
assertNull(decoded.disc)
|
||||
assertNull(decoded.date)
|
||||
assertNull(decoded.cover)
|
||||
assertNull(decoded.preAlbum.musicBrainzId)
|
||||
assertNull(decoded.preAlbum.rawName)
|
||||
assertTrue(decoded.preAlbum.preArtists is PreArtistsFrom.Album)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user