Modern Apps git

← Modern-Apps.git

musicbrainz: convert at most two downloads at once

Commit
f022fab6c3b48971fec932cb4caabbc3f11350eb
Author
vayun-mathur <[email protected]>
When
2026-08-20 20:45 UTC (3 months ago)
Parents
a2fb6be

Diff

musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.ktindex 28eec39689..45b4ad51b6 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt@@ -23,7 +23,9 @@ import com.vayunmathur.musicbrainz.platform.SafTree import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.first import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.withLock+import kotlinx.coroutines.sync.withPermit import kotlinx.coroutines.withContext import java.io.ByteArrayOutputStream @@ -66,14 +68,16 @@ class DownloadWorker(context: Context, params: WorkerParameters) :             // Every download is filed as a tagged `.opus`. A stream that is already 48 kHz             // Opus is only rewrapped into Ogg; everything else is re-encoded. A failure here             // fails the download rather than writing one of the formats being replaced.-            val ogg = if (audio.isOpusPassthrough) {-                OpusRemuxer.remux(applicationContext, raw)-            } else {-                // Re-encoding is the slowest step in the download by a wide margin, so it-                // reports progress of its own; without it the row sits still long enough to-                // look like a hang and invite the user to cancel a working download.-                OpusTranscoder.transcode(raw, { isStopped }) { progress ->-                    DownloadQueue.update(DownloadState.Tagging, key, progress)+            val ogg = ConvertLimit.withPermit {+                if (audio.isOpusPassthrough) {+                    OpusRemuxer.remux(applicationContext, raw)+                } else {+                    // Re-encoding is the slowest step in the download by a wide margin, so it+                    // reports progress of its own; without it the row sits still long enough+                    // to look like a hang and invite the user to cancel a working download.+                    OpusTranscoder.transcode(raw, { isStopped }) { progress ->+                        DownloadQueue.update(DownloadState.Tagging, key, progress)+                    }                 }             } ?: return@withContext fail(key, "Could not convert the download to Opus") @@ -270,6 +274,22 @@ private fun DownloadRequest.toVorbisTags(cover: ByteArray?, lyrics: String?) = V private fun ByteArray?.isPng(): Boolean =     this != null && size > 8 && this[1] == 'P'.code.toByte() +/**+ * Bounds how many tracks convert to Opus at once.+ *+ * An album is one worker per track and they all reach the convert step within a second of+ * each other, so without this a twelve-track album runs twelve AAC decoders and twelve Opus+ * encoders together. All of those are software, so they contend for the same cores and every+ * track lands later than it would have done taking turns. Downloading is deliberately left+ * parallel: that part is network-bound, and overlapping it is what makes an album quick.+ */+private object ConvertLimit {+    /** Two, so one track's decode can overlap another's encode without a stampede. */+    private val permits = Semaphore(2)++    suspend fun <T> withPermit(block: () -> T): T = permits.withPermit(block)+}+ /**  * Holds the last few covers fetched from the Cover Art Archive.  *
musicbrainz/src/main/res/values/strings.xml
diff --git a/musicbrainz/src/main/res/values/strings.xml b/musicbrainz/src/main/res/values/strings.xmlindex 81a61207b1..e83052a3ed 100644--- a/musicbrainz/src/main/res/values/strings.xml+++ b/musicbrainz/src/main/res/values/strings.xml@@ -62,7 +62,7 @@     <!-- Download status: transferring the song. -->     <string name="state_downloading">Downloading</string>     <!-- Download status: writing song details into the file. -->-    <string name="state_tagging">Adding details</string>+    <string name="state_tagging">Converting audio</string>     <!-- Download status: finished successfully. -->     <string name="state_done">Saved</string>     <!-- Download status: did not finish. -->