Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
AutoMix lab (unfinished): recoverable preparation, plan invalidation, analysis retries and identity, DJ session mode, local sections and vocal risk, bench matrix
kota-zobbeebdfcd69e
diff --git a/base/app/build.gradle.kts b/base/app/build.gradle.ktsindex 0565f4db..958152e7 100644--- a/base/app/build.gradle.kts+++ b/base/app/build.gradle.kts@@ -13,6 +13,9 @@ val keystoreProperties = Properties().apply {}}+// -PautomixLab: a test app with its own data, installed alongside every regular/dev build.+val automixLab = findProperty("automixLab") != null+android {namespace = "app.myspoti.engine"@@ -26,8 +29,8 @@ android {// "-beta" as in UpdateChecker.isBeta, one predicate for the app id, the label and the log stripping.val beta = findProperty("appVersionName") == null || version.contains("-beta")defaultConfig {- applicationId = if (beta) "app.myspoti" else "app.myspoti.stable"- manifestPlaceholders["appLabel"] = if (beta) "Myspoti Beta" else "Myspoti"+ applicationId = if (automixLab) "app.myspoti.automixlab" else if (beta) "app.myspoti" else "app.myspoti.stable"+ manifestPlaceholders["appLabel"] = if (automixLab) "Myspoti Mix Lab" else if (beta) "Myspoti Beta" else "Myspoti"// The adb command line (com.music.spotui.cli): betas and debug builds only.manifestPlaceholders["cliEnabled"] = beta.toString()minSdk = 29@@ -78,8 +81,8 @@ android {buildTypes {debug {// Installs next to the release apps (own data, own sign-in).- applicationIdSuffix = ".dev"- manifestPlaceholders["appLabel"] = "Myspoti dev"+ if (!automixLab) applicationIdSuffix = ".dev"+ manifestPlaceholders["appLabel"] = if (automixLab) "Myspoti Mix Lab" else "Myspoti dev"manifestPlaceholders["cliEnabled"] = "true"}release {@@ -88,7 +91,7 @@ android {signingConfig = signingConfigs.getByName(if (hasReleaseKey) "release" else "debug")// -PoverDev: the optimised build under the dev app's id, to measure the speed of a// beta on the test phone without replacing the signed beta installed there.- if (findProperty("overDev") != null) {+ if (findProperty("overDev") != null && !automixLab) {applicationIdSuffix = ".dev"manifestPlaceholders["appLabel"] = "Myspoti dev"}@@ -176,4 +179,4 @@ dependencies {-}\ No newline at end of file+}diff --git a/base/app/src/main/java/com/music/spotui/automix/AnalysisIdentity.kt b/base/app/src/main/java/com/music/spotui/automix/AnalysisIdentity.ktnew file mode 100644index 00000000..4b01bfcc--- /dev/null+++ b/base/app/src/main/java/com/music/spotui/automix/AnalysisIdentity.kt@@ -0,0 +1,19 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.automix++/** Cache the recording, not the expiring access token or just its catalogue track id. */+internal object AnalysisIdentity {+ fun stream(track: String, provider: String?, durationMs: Long?, codec: String?, source: String?, url: String, manifest: String?): String =+ "$track|$provider|$durationMs|$codec|$source|${manifest?.let(::manifestIdentity) ?: stableAddress(url)}"++ fun file(uri: String, size: Long, modifiedAt: Long): String = "$uri|$size|$modifiedAt"++ private val signedParameter = Regex("([?&](?:amp;)?(?:token|sig|signature|expires|policy|key-pair-id|hdnts|X-Amz-[^=\\s]+)=)[^&\\s\"<>]*", RegexOption.IGNORE_CASE)++ private fun stableAddress(value: String): String = signedParameter.replace(value) { it.groupValues[1] }++ /** Read once on the worker, never for every player tick. Preserve media paths and non-auth parameters. */+ private fun manifestIdentity(encoded: String): String = runCatching {+ stableAddress(String(java.util.Base64.getMimeDecoder().decode(encoded), Charsets.UTF_8))+ }.getOrElse { stableAddress(encoded) }+}diff --git a/base/app/src/main/java/com/music/spotui/automix/AnalysisRetry.kt b/base/app/src/main/java/com/music/spotui/automix/AnalysisRetry.ktnew file mode 100644index 00000000..71a21e9d--- /dev/null+++ b/base/app/src/main/java/com/music/spotui/automix/AnalysisRetry.kt@@ -0,0 +1,19 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.automix++/** Worker-owned retry deadlines. A cancelled ask is never a failure of the audio. */+internal class AnalysisRetry {+ private data class Failure(val attempts: Int, val retryAt: Long)+ private val failures = HashMap<String, Failure>()++ fun allowed(key: String, nowMs: Long): Boolean = failures[key]?.let { nowMs >= it.retryAt } ?: true++ fun failed(key: String, nowMs: Long, cancelled: Boolean = false) {+ if (cancelled) return+ val attempts = ((failures[key]?.attempts ?: 0) + 1).coerceAtMost(5)+ failures[key] = Failure(attempts, nowMs + minOf(120_000L, 5_000L shl (attempts - 1)))+ }++ fun succeeded(key: String) { failures.remove(key) }+ fun clear() { failures.clear() }+}diff --git a/base/app/src/main/java/com/music/spotui/automix/AutoMix.kt b/base/app/src/main/java/com/music/spotui/automix/AutoMix.ktindex 8ced4cb1..1c7cc2c3 100644--- a/base/app/src/main/java/com/music/spotui/automix/AutoMix.kt+++ b/base/app/src/main/java/com/music/spotui/automix/AutoMix.kt@@ -33,6 +33,7 @@ object AutoMix {fun set(value: Boolean) {on = value+ if (!value) cancelRequests()Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean(KEY, value).apply()// The Spotify songs' fade follows: a short one with AutoMix on (see TransitionPref).com.music.spotui.data.preferences.applyCrossfade(com.music.spotui.data.preferences.CrossfadePref.seconds)@@ -47,7 +48,7 @@ object AutoMix {/** No mix between two songs that follow each other on their album. On by default. */var albumOrder by mutableStateOf(Graph.app.getSharedPreferences(PREFS, 0).getBoolean("automix_album_order", true))private set- fun changeAlbumOrder(value: Boolean) { albumOrder = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean("automix_album_order", value).apply() }+ fun changeAlbumOrder(value: Boolean) { albumOrder = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean("automix_album_order", value).apply(); com.music.spotui.di.LocalPlayer.autoMixSettingsChanged() }/** The mix shown in the player: its stretch on the seek bar, the cover and its glow pulsing with it. On by default. */var showInPlayer by mutableStateOf(Graph.app.getSharedPreferences(PREFS, 0).getBoolean("automix_show", true))@@ -57,6 +58,11 @@ object AutoMix {private setfun changeMixLength(value: Int) { mixLength = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putInt("automix_length", value).apply(); com.music.spotui.di.LocalPlayer.autoMixSettingsChanged() }+ /** A DJ session may leave at an earlier musical section; independent of overlap length. */+ var sessionDj by mutableStateOf(Graph.app.getSharedPreferences(PREFS, 0).getBoolean("automix_session_dj", true))+ private set+ fun changeSessionDj(value: Boolean) { sessionDj = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean("automix_session_dj", value).apply(); com.music.spotui.di.LocalPlayer.autoMixSettingsChanged() }+/** How far the next song's tempo may be bent to match: 0 strict (4 %), 1 normal (8 %), 2 wide (12 %). */var tempoRange by mutableStateOf(Graph.app.getSharedPreferences(PREFS, 0).getInt("automix_tempo", 1).coerceIn(0, 2))private set@@ -65,7 +71,7 @@ object AutoMix {/** The DJ EQ: the bass swap and the filters (each player's DynamicsProcessing); off, only the volumes move. */var djEq by mutableStateOf(Graph.app.getSharedPreferences(PREFS, 0).getBoolean("automix_eq", true))private set- fun changeDjEq(value: Boolean) { djEq = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean("automix_eq", value).apply() }+ fun changeDjEq(value: Boolean) { djEq = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean("automix_eq", value).apply(); com.music.spotui.di.LocalPlayer.autoMixEqChanged() }/** How much the player moves with the mix: 0 not at all (the stretch on the bar stays), 1 subtle, 2 strong. */var pulseLevel by mutableStateOf(Graph.app.getSharedPreferences(PREFS, 0).getInt("automix_pulse", 2).coerceIn(0, 2))@@ -90,7 +96,7 @@ object AutoMix {private setfun changeMixPodcasts(value: Boolean) { mixPodcasts = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean("automix_podcasts", value).apply(); com.music.spotui.di.LocalPlayer.autoMixSettingsChanged() }- /** Extension streams analysed on mobile data too (each is read a second time: about the song's size again). Off by default. */+ /** Extension streams analysed on mobile data too (each is read a second time: about the song's size again). On by default. */var analyzeOnMobileData by mutableStateOf(Graph.app.getSharedPreferences(PREFS, 0).getBoolean("automix_mobile_data", true))private setfun changeAnalyzeOnMobileData(value: Boolean) {@@ -102,19 +108,20 @@ object AutoMix {}/**- * The planner's settings from the ones above. Mix length in bars: short 4–8, balanced 2–16 (as- * the music says), long 12–32 (up to a minute); the lower end is a preference (a shorter mix only+ * The planner's settings from the ones above. Mix length in bars: short 4–8, balanced 8–16 (as+ * the music says), long 12–32 (up to 90 seconds); the lower end is a preference (a shorter mix only* when no longer one fits the songs), the upper one a limit. A plain fade lasts 4, 8 or 14 s.*/fun settings() = MixPlanner.Settings(maxStretch = doubleArrayOf(0.04, 0.08, 0.12)[tempoRange],- maxSec = if (mixLength == 2) 64.0 else 32.0,+ maxSec = if (mixLength == 2) 90.0 else 48.0,lengthBias = mixLength - 1,style = arrayOf(MixPlan.Style.BLEND, MixPlan.Style.CUT, MixPlan.Style.FADE)[style],- minBars = intArrayOf(4, 2, 12)[mixLength],+ minBars = intArrayOf(4, 8, 12)[mixLength],maxBars = intArrayOf(8, 16, 32)[mixLength],keyWeight = if (harmonic) 2.0 else 0.0,smoothSec = doubleArrayOf(4.0, 8.0, 14.0)[mixLength],+ djMode = sessionDj,)// ── Why a song is not mixed (for the screens) ──@@ -154,7 +161,7 @@ object AutoMix {}/** Why [uri] has no analysis and will get none for now (null: ready, or under way). */- fun missing(uri: Uri): Reason? = synchronized(memory) { refused[key(uri)] }+ fun missing(uri: Uri): Reason? = synchronized(memory) { refused[requestId(uri)] }fun changeShowInPlayer(value: Boolean) { showInPlayer = value; Graph.app.getSharedPreferences(PREFS, 0).edit().putBoolean("automix_show", value).apply() }@@ -174,7 +181,11 @@ object AutoMix {override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, TrackAnalysis>?) = size > 24}private val asked = HashSet<String>()- private val failed = HashSet<String>()+ private val retry = AnalysisRetry()+ private val fileVersions = java.util.concurrent.ConcurrentHashMap<String, String>()+ private val streamKeys = ArrayDeque<Pair<app.myspoti.extensions.AudioStream, String>>()+ private val baseKeys = java.util.concurrent.ConcurrentHashMap<String, String>()+ @Volatile private var cacheEpoch = 0L/** Songs without an analysis for now, and why (shown through [missing]). */private val refused = HashMap<String, Reason>()@Volatile private var wanted: Set<String> = emptySet()@@ -191,52 +202,75 @@ object AutoMix {* any other song; [ready] runs on the worker when one is done.*/fun request(uris: List<Uri>, ready: () -> Unit) {- // Saving energy (see [frugal]): the song playing and the next only.- val songs = if (frugal()) uris.take(2) else uris- wanted = songs.map(::key).toSet()- for (uri in songs) {- val key = key(uri)- synchronized(memory) { if (memory.containsKey(key) || key in failed || !asked.add(key)) continue }- worker.execute {- try {- if (key !in wanted) return@execute- val a = load(key) ?: analyse(uri, key)- if (a != null) { synchronized(memory) { memory[key] = a; refused -= key }; ready() }- } finally { synchronized(memory) { asked -= key } }- }+ // Three songs at most; cached answers for B/C are loaded before A's expensive analysis.+ val songs = uris.distinctBy(::requestId).take(if (frugal()) 2 else 3)+ wanted = songs.map(::requestId).toSet()+ val epoch = cacheEpoch+ val jobs = songs.filter { uri ->+ val id = requestId(uri)+ synchronized(memory) { retry.allowed(key(uri), android.os.SystemClock.elapsedRealtime()) && asked.add(id) }+ }+ if (jobs.isEmpty()) return+ worker.execute {+ val pending = ArrayList<Uri>()+ try {+ for (uri in jobs) {+ if (epoch != cacheEpoch || requestId(uri) !in wanted) continue+ refreshFileVersion(uri)+ prepareStreamKey(uri)+ val k = key(uri)+ if (synchronized(memory) { memory.containsKey(k) }) continue+ val cached = if (uri.scheme != ExtensionAudio.SCHEME || ExtensionAudio.streamOf(uri.lastPathSegment.orEmpty()) != null) load(k) else null+ if (cached != null) { synchronized(memory) { memory[k] = cached; refused -= requestId(uri) }; ready() }+ else pending += uri+ }+ for (uri in pending) {+ val id = requestId(uri)+ if (epoch != cacheEpoch || id !in wanted) continue+ val result = analyse(uri, epoch)+ if (result != null && epoch == cacheEpoch && id in wanted && result.first == key(uri)) {+ synchronized(memory) { memory[result.first] = result.second; refused -= id; retry.succeeded(result.first) }+ ready()+ }+ }+ } finally { synchronized(memory) { jobs.forEach { asked -= requestId(it) } } }}}- private fun analyse(uri: Uri, key: String): TrackAnalysis? {- // A stream is read a second time for the analysis: on unmetered networks only, unless the- // listener allows mobile data (never under the data saver, which refuses every connection).- if (uri.scheme == ExtensionAudio.SCHEME) {- val saver = com.music.spotui.data.DataSaver.active- if (com.music.spotui.data.DataSaver.metered && (saver || !analyzeOnMobileData)) {- synchronized(memory) { refused[key] = Reason.MobileData }- journal("not analysed (${if (saver) "data saver" else "metered network"}): ${uri.lastPathSegment}"); return null+ private fun analyse(uri: Uri, epoch: Long): Pair<String, TrackAnalysis>? {+ val id = requestId(uri)+ val cancelled = { epoch != cacheEpoch || id !in wanted }+ var cacheKey = key(uri)+ try {+ if (uri.scheme == ExtensionAudio.SCHEME) {+ val saver = com.music.spotui.data.DataSaver.active+ if (com.music.spotui.data.DataSaver.metered && (saver || !analyzeOnMobileData)) {+ synchronized(memory) { refused[id] = Reason.MobileData }+ return null+ }+ if (ExtensionAudio.resolve(uri, ahead = true) == null) throw java.io.IOException("no stream")+ prepareStreamKey(uri)+ cacheKey = key(uri)+ // A source had not answered at the cache pass: its old analysis may now be usable.+ load(cacheKey)?.let { return cacheKey to it }}- // DASH answers too: the data source reads them as one file, as the player does.- val s = runCatching { ExtensionAudio.resolve(uri, ahead = true) }.getOrNull()- if (s == null) { synchronized(memory) { failed += key; refused[key] = Reason.NotAnalyzed }; journal("not analysed (no stream): ${uri.lastPathSegment}"); return null }- }- val t0 = System.currentTimeMillis()- return try {- val cancelled = { key !in wanted }+ if (cancelled()) return null+ val t0 = System.currentTimeMillis()val pcm = TrackDecoder.decode(uri, cancelled)val t1 = System.currentTimeMillis()val a = Analyzer.analyze(pcm.mid, pcm.side, model(), cancelled, if (frugal()) 2 else if (charging()) THREADS else minOf(THREADS, 3))- save(key, a)+ if (cancelled() || cacheKey != key(uri)) return null+ save(cacheKey, a)journal(String.format("analysed %s: %.1f BPM%s, %d bars, key %d, decode %d ms, analysis %d ms", label(uri), a.bpm,if (a.steady) " steady" else (if (a.tail != null) " (tail grid)" else " (no grid)"), a.bars, a.key, t1 - t0, System.currentTimeMillis() - t1) +(if (a.note.isNotEmpty()) " | grids refused: ${a.note}" else ""))- a- } catch (e: InterruptedException) {- null- } catch (e: Throwable) {- synchronized(memory) { failed += key; refused[key] = Reason.NotAnalyzed }- journal("analysis failed for ${label(uri)}: ${e.javaClass.simpleName} ${e.message?.take(120)}")- null+ return cacheKey to a+ } catch (e: Exception) {+ // MediaExtractor can wrap the data source's cancellation IOException.+ if (cancelled() || e is InterruptedException) return null+ synchronized(memory) { retry.failed(cacheKey, android.os.SystemClock.elapsedRealtime()); refused[id] = Reason.NotAnalyzed }+ journal("analysis deferred for ${label(uri)}: ${e.javaClass.simpleName} ${e.message?.take(120)}")+ return null}}@@ -274,6 +308,7 @@ object AutoMix {// ── Plans ──fun planFor(a: TrackAnalysis, b: TrackAnalysis): MixPlan = MixPlanner.plan(a, b, settings())+ fun planAfter(a: TrackAnalysis, b: TrackAnalysis, earliestExit: Double): MixPlan? = MixPlanner.planAfter(a, b, earliestExit, settings())// ── Cache ──@@ -291,17 +326,63 @@ object AutoMix {}}- /** An extension stream is keyed by its track (its address changes); a file by its address. */+ private fun requestId(uri: Uri): String =+ if (uri.scheme == ExtensionAudio.SCHEME) "ext:" + uri.lastPathSegment.orEmpty() else uri.toString()++ /** No file I/O or large manifest hashing on the player/UI thread. */fun key(uri: Uri): String {- val id = if (uri.scheme == ExtensionAudio.SCHEME) "ext:" + uri.lastPathSegment.orEmpty() else uri.toString()- return MessageDigest.getInstance("SHA-1").digest(id.toByteArray()).joinToString("") { "%02x".format(it) }.take(24)+ val id = requestId(uri)+ if (uri.scheme == ExtensionAudio.SCHEME) {+ val stream = ExtensionAudio.streamOf(uri.lastPathSegment.orEmpty())+ if (stream != null) return synchronized(streamKeys) {+ streamKeys.firstOrNull { it.first === stream }?.second ?: "$id:pending:${System.identityHashCode(stream)}"+ }+ }+ return fileVersions[id] ?: baseKeys.getOrPut(id) { digest(id) }+ }++ private fun prepareStreamKey(uri: Uri) {+ if (uri.scheme != ExtensionAudio.SCHEME) return+ val stream = ExtensionAudio.streamOf(uri.lastPathSegment.orEmpty()) ?: return+ synchronized(streamKeys) { if (streamKeys.any { it.first === stream }) return }+ // Signed query strings expire; provider, recording path and audio metadata do not.+ val k = digest(AnalysisIdentity.stream(requestId(uri), stream.providerId, stream.durationMs, stream.codec, stream.source, stream.url, stream.manifest))+ synchronized(streamKeys) {+ streamKeys.addLast(stream to k)+ while (streamKeys.size > 24) streamKeys.removeFirst()+ }+ }++ private fun digest(id: String): String = MessageDigest.getInstance("SHA-1").digest(id.toByteArray())+ .joinToString("") { "%02x".format(it) }.take(24)++ /** Only the worker stats files; replacing an existing file invalidates its analysis. */+ private fun refreshFileVersion(uri: Uri) {+ if (uri.scheme == "file") {+ val file = File(uri.path ?: return)+ fileVersions[requestId(uri)] = digest(AnalysisIdentity.file(uri.toString(), file.length(), file.lastModified()))+ } else if (uri.scheme == "content") {+ runCatching {+ Graph.app.contentResolver.query(uri, arrayOf(android.provider.MediaStore.MediaColumns.SIZE, android.provider.MediaStore.MediaColumns.DATE_MODIFIED), null, null, null)?.use { cursor ->+ if (cursor.moveToFirst()) fileVersions[requestId(uri)] = digest(AnalysisIdentity.file(uri.toString(), cursor.getLong(0), cursor.getLong(1)))+ }+ }+ }+ }++ fun cancelRequests() { wanted = emptySet() }++ fun sourcesChanged() {+ synchronized(memory) { retry.clear(); refused.clear() }}private fun label(uri: Uri) = uri.lastPathSegment?.takeLast(48).orEmpty()/** Off the main thread: every song analysed is dropped (they are analysed again when played). */fun clearCache() {- synchronized(memory) { memory.clear(); failed.clear(); refused.clear() }+ cacheEpoch+++ synchronized(memory) { memory.clear(); retry.clear(); refused.clear() }+ fileVersions.clear()dir().listFiles()?.forEach { it.delete() }}diff --git a/base/app/src/main/java/com/music/spotui/automix/MixPreparation.kt b/base/app/src/main/java/com/music/spotui/automix/MixPreparation.ktnew file mode 100644index 00000000..7d8e2339--- /dev/null+++ b/base/app/src/main/java/com/music/spotui/automix/MixPreparation.kt@@ -0,0 +1,18 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.automix++/** Decisions independent of ExoPlayer, so late analysis and buffering can be tested without audio. */+internal object MixPreparation {+ const val MIN_LEAD_MS = 850L+ const val BUFFER_MS = 750L++ fun mayReplacePlan(committed: Boolean, provisional: Boolean, analysesReady: Boolean): Boolean =+ !committed && provisional && analysesReady++ fun readyToTakeOver(playing: Boolean, bufferedMs: Long, remainingMs: Long, phaseMs: Double?, synced: Boolean): Boolean =+ playing && bufferedMs >= minOf(BUFFER_MS, remainingMs.coerceAtLeast(0)) &&+ (!synced || phaseMs != null && kotlin.math.abs(phaseMs) <= 80.0)++ /** Audio lost on B must not make the still-playing A fade away. */+ fun preserveOutgoing(incomingReady: Boolean, outgoingPlaying: Boolean): Boolean = !incomingReady && outgoingPlaying+}diff --git a/base/app/src/main/java/com/music/spotui/automix/TrackDecoder.kt b/base/app/src/main/java/com/music/spotui/automix/TrackDecoder.ktindex bd7e60b3..f30e33b6 100644--- a/base/app/src/main/java/com/music/spotui/automix/TrackDecoder.kt+++ b/base/app/src/main/java/com/music/spotui/automix/TrackDecoder.kt@@ -167,7 +167,7 @@ object TrackDecoder {private var sizeKnown = false@Synchronized override fun readAt(position: Long, buffer: ByteArray, offset: Int, length: Int): Int {- if (cancelled()) throw IOException("cancelled")+ if (cancelled()) throw InterruptedException("cancelled")if (length == 0) return 0if (position < 0) return -1if (!open || cursor != position) {diff --git a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.ktindex c8261e85..0bcc50e2 100644--- a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt+++ b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt@@ -12,6 +12,7 @@ import androidx.media3.exoplayer.ExoPlayerimport com.music.spotui.data.local.LocalTrackimport com.music.spotui.data.preferences.CrossfadePrefimport com.music.spotui.cast.CastPlayback+import com.music.spotui.automix.MixPreparation/*** Plays the device's own audio files (Local Files). While it plays, the Spotify engine is@@ -131,6 +132,12 @@ object LocalPlayer {private fun listenerFor(p: ExoPlayer): Player.Listener = object : Player.Listener {override fun onEvents(player: Player, events: Player.Events) = publish()+ override fun onAudioSessionIdChanged(audioSessionId: Int) {+ if (exo !== p || !com.music.spotui.automix.AutoMix.on || !com.music.spotui.automix.AutoMix.djEq) return+ deckEq?.release()+ deckEq = com.music.spotui.automix.DeckEq.attach(audioSessionId)+ mix?.takeIf { it.deck === p }?.eq = deckEq+ }// Each change of what the player does, one line each, so a song that never starts says why.override fun onPlaybackStateChanged(state: Int) {@@ -178,7 +185,7 @@ object LocalPlayer {override fun onMediaItemTransition(mediaItem: androidx.media3.common.MediaItem?, reason: Int) {// Anything but the transition the fade itself makes (a skip, a tap in the queue) ends the fade.if (!startingFade) endFade()- planned = null; dropped = null+ planned = null; preparationFailures = 0; planningPair = null// From a station to a song or back: a station's repeat is off (see applyRepeat).applyRepeat(p)// The song kept from fading out is left behind: the next one fades as usual.@@ -292,6 +299,8 @@ object LocalPlayer {log("${current()?.title}: the audio sources changed, asked again")p.prepare(); p.play()}+ com.music.spotui.automix.AutoMix.sourcesChanged()+ autoMixSettingsChanged()prefetchNext(p)}@@ -305,6 +314,7 @@ object LocalPlayer {if (CastPlayback.active || !active.value) return falseif (p.currentMediaItem?.localConfiguration?.uri?.scheme != com.music.spotui.extensions.ExtensionAudio.SCHEME) return falseif (outgoing != null) endFade()+ autoMixSettingsChanged()val at = p.currentPositionlog("${current()?.title}: another source picked, opened again at $at ms")errors.played()@@ -456,6 +466,7 @@ object LocalPlayer {CastPlayback.stop(back = false)endFade()active.value = false+ com.music.spotui.automix.AutoMix.cancelRequests()// A page left muted by an extension's mirror must not stay silent once Spotify plays again.SongPlayer.unmuteMirror()forgetRadio(); continues = false; deletedDownloads.clear()@@ -483,9 +494,9 @@ object LocalPlayer {val toIndex: Int,val toId: String,val deck: ExoPlayer,- val eq: com.music.spotui.automix.DeckEq?,+ var eq: com.music.spotui.automix.DeckEq?,/** Where the deck starts (B's time, ms): the entry point less the muted run-up. */- val startMs: Long,+ var startMs: Long,/** A mix for Next: started from A's next downbeat once the deck is ready (see [mixOnSkip]). */val skip: Boolean = false,) {@@ -506,6 +517,9 @@ object LocalPlayer {var locked = falsevar worstMs = 0.0var loggedAt = 0L+ var bufferedHold = false+ var recoveryGainA = 1f+ var recoveryStartedAt = 0L/** Progress of the mix from the outgoing song's position (1 once it ended). */fun progress(): Double = a?.let {if (it.playbackState == Player.STATE_ENDED || it.playbackState == Player.STATE_IDLE) 1.0@@ -514,14 +528,20 @@ object LocalPlayer {}/** The plan for the song playing → the next one, once both are analysed. */- private class Planned(val fromIndex: Int, val toId: String, val plan: app.myspoti.automix.MixPlan)+ private class Planned(+ val fromIndex: Int, val toId: String, val plan: app.myspoti.automix.MixPlan,+ val analysisA: app.myspoti.automix.TrackAnalysis? = null,+ val analysisB: app.myspoti.automix.TrackAnalysis? = null,+ ) { val provisional get() = analysisA == null || analysisB == null }@Volatile private var planned: Planned? = nullprivate var mix: MixRun? = null/** The main player's EQ (a deck's once it took over). */private var deckEq: com.music.spotui.automix.DeckEq? = nullprivate var asking: String? = null- /** The pair whose deck was dropped: no new deck for it. */- private var dropped: Pair<Int, String>? = null+ private var planningPair: Pair<Int, String>? = null+ private var preparationFailures = 0+ private var retryDeckAt = 0L+ private var askedAt = 0Lprivate const val PREPARE_LEAD_MS = 15_000L/**@@ -546,20 +566,21 @@ object LocalPlayer {if (m != null && (m.toIndex != next || m.toId != nextId)) cancelMix("the next song changed")val pos = p.currentPositionval run = mix+ if (run != null && !run.skip && run.state < MixState.PREROLL && planned?.provisional == true) upgradePreparedFallback(run, p, next, nextId)if (run == null) {val plan = planFor(p, next, nextId) ?: return trueval exitMs = (plan.exitA * 1000).toLong()- if (pos >= exitMs - RUN_UP_MS - PREPARE_LEAD_MS && pos < exitMs - RUN_UP_MS / 2 && dropped != next to nextId) prepareDeck(p, next, nextId, plan)+ if (preparationFailures < 3 && android.os.SystemClock.uptimeMillis() >= retryDeckAt && pos >= exitMs - RUN_UP_MS - PREPARE_LEAD_MS && pos < exitMs - MixPreparation.MIN_LEAD_MS) prepareDeck(p, next, nextId, plan)return true}val exitMs = (run.plan.exitA * 1000).toLong()val startAt = exitMs - run.runUpA - startLeadwhen (run.state) {MixState.PREPARING -> {- // Too late for the deck: the song ends as usual (a skip that waited too long: the plain skip).+ // A late natural deck replans a future cue; a skip keeps its short overall deadline.if (run.skip && android.os.SystemClock.uptimeMillis() - run.createdAt > SKIP_WAIT_MS) {cancelMix("not ready in time for the skip"); p.seekToNextMediaItem()- } else if (!run.skip && pos > startAt) cancelMix("the next song was not ready in time")+ } else if (!run.skip && pos > exitMs - MixPreparation.MIN_LEAD_MS) cancelMix("the next song was not ready in time; replanning")}MixState.READY -> {// A skip's deck ready later than its plan: the mix moves to A's next downbeat.@@ -568,11 +589,17 @@ object LocalPlayer {if (++run.moves > 2) { cancelMix("the skip could not start"); p.seekToNextMediaItem(); return true }run.plan = skipPlan(p, next, pos)com.music.spotui.automix.AutoMix.journal("skip moved: ${run.plan}")- } else if (pos >= startAt) startRunUp(run)+ } else if (!run.skip && pos > exitMs + 250) cancelMix("cue passed before run-up; replanning")+ else if (pos >= startAt) startRunUp(run)}MixState.PREROLL -> {lock(run, p, preroll = true)- if (pos >= exitMs) takeOver(run, p)+ if (pos >= exitMs) {+ val d = run.deck+ val remaining = (d.duration.takeIf { it > 0 } ?: Long.MAX_VALUE) - d.currentPosition+ if (MixPreparation.readyToTakeOver(d.isPlaying, d.totalBufferedDuration, remaining, run.errors.lastOrNull(), run.plan.synced)) takeOver(run, p)+ else if (pos > exitMs + 1_500) cancelMix("deck not buffered or aligned at the cue; replanning")+ }}else -> Unit}@@ -603,44 +630,78 @@ object LocalPlayer {/** The plan for this pair: the analysed one, else (once the end is near) a fade on the songs' lengths. */private fun planFor(p: ExoPlayer, next: Int, nextId: String): app.myspoti.automix.MixPlan? {val cur = p.currentMediaItemIndex- planned?.takeIf { it.fromIndex == cur && it.toId == nextId }?.let { return it.plan }val a = tracks.getOrNull(cur) ?: return nullval b = tracks.getOrNull(next) ?: return null- // Spoken word starts and ends whole (and is not analysed).- if (spoken(a, b)) { whyNot(com.music.spotui.automix.AutoMix.Reason.Podcast); return null }- // Asked again whenever the songs ahead change (a queue edit): the next one and the one after it.- val ahead = "$cur|$nextId|${p.getMediaItemAt(next).mediaId}|${if (next + 1 < p.mediaItemCount) p.getMediaItemAt(next + 1).mediaId else ""}"- if (asking != ahead) {- asking = ahead- // The song after the next one too: a skip then finds both of its songs analysed.- com.music.spotui.automix.AutoMix.request(listOfNotNull(a.uri, b.uri, tracks.getOrNull(next + 1)?.uri)) {}- if (followsOnAlbum(a, b)) com.music.spotui.automix.AutoMix.journal("${a.title} → ${b.title}: next on the album, no mix")+ val pair = cur to nextId+ if (planningPair != pair) {+ planningPair = pair; planned = null; preparationFailures = 0; retryDeckAt = 0}- // B right after A on their album: they run into each other as the album does (a skip still mixes).- if (followsOnAlbum(a, b)) { whyNot(com.music.spotui.automix.AutoMix.Reason.Album); return null }- val ra = com.music.spotui.automix.AutoMix.analysis(a.uri); val rb = com.music.spotui.automix.AutoMix.analysis(b.uri)- if (ra != null && rb != null) {- val plan = com.music.spotui.automix.AutoMix.planFor(ra, rb)- planned = Planned(cur, nextId, plan)- whyNot(null)- com.music.spotui.automix.AutoMix.journal("plan ${a.title} → ${b.title}: $plan")- return plan+ // Policies are checked before a cached plan: a setting must apply immediately.+ if (spoken(a, b)) { planned = null; whyNot(com.music.spotui.automix.AutoMix.Reason.Podcast); return null }+ if (followsOnAlbum(a, b)) { planned = null; whyNot(com.music.spotui.automix.AutoMix.Reason.Album); return null }+ val ahead = "$cur|$nextId|${a.uri}|${b.uri}|${if (next + 1 < p.mediaItemCount) p.getMediaItemAt(next + 1).mediaId else ""}"+ val now = android.os.SystemClock.uptimeMillis()+ if (asking != ahead || now - askedAt >= 5_000) {+ asking = ahead; askedAt = now+ com.music.spotui.automix.AutoMix.request(listOfNotNull(a.uri, b.uri, tracks.getOrNull(next + 1)?.uri)) {+ handler.post {+ if (active.value && com.music.spotui.automix.AutoMix.on) {+ cuePair = ""+ if (exo?.isPlaying == true) { handler.removeCallbacks(tick); handler.post(tick) }+ }+ }+ }}- // A song refused for now (mobile data, a failed analysis) says so at once; one still under way, once the end is near.+ val ra = com.music.spotui.automix.AutoMix.analysis(a.uri)+ val rb = com.music.spotui.automix.AutoMix.analysis(b.uri)+ val old = planned?.takeIf { it.fromIndex == cur && it.toId == nextId }+ val enoughLead = old != null && old.plan.exitA * 1000 > p.currentPosition + MixPreparation.MIN_LEAD_MS+ val upgraded = preparationFailures < 2 && old != null && MixPreparation.mayReplacePlan(mix?.state?.let { it >= MixState.PREROLL } == true, old.provisional, ra != null && rb != null)+ if (enoughLead && !upgraded && (old!!.provisional || old.analysisA === ra && old.analysisB === rb)) return old.planval refused = (if (ra == null) com.music.spotui.automix.AutoMix.missing(a.uri) else null) ?: (if (rb == null) com.music.spotui.automix.AutoMix.missing(b.uri) else null)whyNot(refused)- // Not analysed yet with the end near: an equal-power fade on the lengths (6 s, or the smooth fade's own).+ if (ra != null && rb != null && preparationFailures < 2) {+ val earliest = (p.currentPosition + RUN_UP_MS + startLead + 500) / 1000.0+ com.music.spotui.automix.AutoMix.planAfter(ra, rb, earliest)?.let { plan ->+ planned = Planned(cur, nextId, plan, ra, rb)+ whyNot(null)+ com.music.spotui.automix.AutoMix.journal("plan ${a.title} → ${b.title}: $plan")+ return plan+ }+ }val dur = p.duration.takeIf { it > 0 } ?: return null- if (dur - p.currentPosition > PREPARE_LEAD_MS + RUN_UP_MS + 1000) return null+ if (dur - p.currentPosition > PREPARE_LEAD_MS + RUN_UP_MS + 1000 && preparationFailures < 2) return nullval settings = com.music.spotui.automix.AutoMix.settings()- val len = minOf(if (settings.style == app.myspoti.automix.MixPlan.Style.FADE) settings.smoothSec else 6.0, dur / 4000.0)- val plan = app.myspoti.automix.MixPlan(app.myspoti.automix.MixPlan.Style.FADE, dur / 1000.0 - len, 0.0, len, 1.0, 0, 0.5, 0.1, 1.0, 0.0, "fade: not analysed in time")+ val wantedLength = if (settings.style == app.myspoti.automix.MixPlan.Style.FADE) settings.smoothSec else 6.0+ val exit = maxOf(dur / 1000.0 - wantedLength, (p.currentPosition + MixPreparation.MIN_LEAD_MS + 300) / 1000.0)+ val length = minOf(wantedLength, dur / 1000.0 - exit)+ if (length < 0.35) { planned = null; return null }+ val plan = app.myspoti.automix.MixPlan(app.myspoti.automix.MixPlan.Style.FADE, exit, rb?.contentStart ?: 0.0, length, 1.0, 0, 0.5, 0.1, 1.0, 0.0,+ if (preparationFailures > 0) "fade: recovering a late deck" else "fade: analysis pending")planned = Planned(cur, nextId, plan)whyNot(refused ?: if (com.music.spotui.automix.AutoMix.frugal()) com.music.spotui.automix.AutoMix.Reason.Frugal else com.music.spotui.automix.AutoMix.Reason.NotAnalyzed)- com.music.spotui.automix.AutoMix.journal("plan ${a.title} → ${b.title}: $plan")+ com.music.spotui.automix.AutoMix.journal("provisional plan ${a.title} → ${b.title}: $plan")return plan}+ /** A prepared but silent fallback can still become a real mix when its analysis finishes. */+ private fun upgradePreparedFallback(run: MixRun, p: ExoPlayer, next: Int, nextId: String) {+ if (preparationFailures >= 2) return+ val a = tracks.getOrNull(p.currentMediaItemIndex)?.let { com.music.spotui.automix.AutoMix.analysis(it.uri) } ?: return+ val b = tracks.getOrNull(next)?.let { com.music.spotui.automix.AutoMix.analysis(it.uri) } ?: return+ // Once per completed analysis pair, including pairs with no future musical cue.+ planned = Planned(p.currentMediaItemIndex, nextId, run.plan, a, b)+ val plan = com.music.spotui.automix.AutoMix.planAfter(a, b, (p.currentPosition + RUN_UP_MS + startLead + 500) / 1000.0) ?: return+ run.plan = plan+ run.startMs = (plan.entryB * 1000 - minOf(plan.entryB * 1000, RUN_UP_MS * plan.speedB)).toLong().coerceAtLeast(0L)+ run.deck.playbackParameters = androidx.media3.common.PlaybackParameters((plan.speedB * rate()).toFloat())+ run.lastSpeed = run.deck.playbackParameters.speed+ run.deck.seekTo(run.startMs)+ run.state = if (run.deck.playbackState == Player.STATE_READY) MixState.READY else MixState.PREPARING+ planned = Planned(p.currentMediaItemIndex, nextId, plan, a, b)+ com.music.spotui.automix.AutoMix.journal("analysis ready: prepared fallback upgraded to $plan")+ }+/*** [b] is the song after [a] on their album: by the files' track numbers (the next track, or the* first of the next disc); without numbers (streams, downloads), when the album itself is the@@ -710,8 +771,14 @@ object LocalPlayer {run.a = p; run.eqA = deckEqmix = rundeck.addListener(object : Player.Listener {+ override fun onAudioSessionIdChanged(audioSessionId: Int) {+ if (mix !== run || run.state >= MixState.MIXING || !com.music.spotui.automix.AutoMix.djEq) return+ run.eq?.release()+ run.eq = com.music.spotui.automix.DeckEq.attach(audioSessionId)+ run.eq?.set(app.myspoti.automix.MixPlan.KILL, 0.0)+ }override fun onPlaybackStateChanged(state: Int) {- if (state == Player.STATE_READY && run.state == MixState.PREPARING) {+ if (mix === run && state == Player.STATE_READY && run.state == MixState.PREPARING) {run.state = MixState.READYcom.music.spotui.automix.AutoMix.journal("deck ready on ${t.title} at $start ms")}@@ -796,6 +863,8 @@ object LocalPlayer {@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)private fun takeOver(run: MixRun, a: ExoPlayer) {val d = run.deck+ if (!d.isPlaying || d.playbackState != Player.STATE_READY) return+ if (run.toIndex !in 0 until a.mediaItemCount) return cancelMix("the queue moved")if (a.getMediaItemAt(run.toIndex).mediaId != run.toId) return cancelMix("the queue moved")val err = run.errors.lastOrNull() ?: 0.0val items = (0 until a.mediaItemCount).map(a::getMediaItemAt)@@ -831,9 +900,29 @@ object LocalPlayer {private fun mixing(run: MixRun): Boolean {val d = run.deckif (exo !== d) { endMix(); return false }- val plan = run.plan+ var plan = run.planval a = run.aif (run.state == MixState.MIXING && a != null) {+ if (d.playWhenReady && MixPreparation.preserveOutgoing(d.isPlaying, a.isPlaying)) {+ if (!run.bufferedHold) com.music.spotui.automix.AutoMix.journal("incoming deck buffering: keeping the outgoing song")+ run.bufferedHold = true+ a.volume = (a.volume + 0.04f).coerceAtMost(1f); d.volume = 0f+ run.eqA?.neutral()+ return true+ }+ if (run.bufferedHold && d.isPlaying) {+ run.bufferedHold = false+ val left = (a.duration.takeIf { it > 0 } ?: a.currentPosition) - a.currentPosition+ if (left > 500 && a.isPlaying) {+ // Re-enter gently from the audio actually heard; do not chase an obsolete grid after a stall.+ plan = app.myspoti.automix.MixPlan(app.myspoti.automix.MixPlan.Style.FADE, a.currentPosition / 1000.0,+ d.currentPosition / 1000.0, minOf(4.0, left / 1000.0), 1.0, 0, 0.5, 0.1, run.plan.matchB, 0.0, "fade: incoming buffer recovered")+ run.plan = plan; run.lastSpeed = 0f+ run.recoveryGainA = a.volume; run.recoveryStartedAt = android.os.SystemClock.uptimeMillis()+ d.playbackParameters = androidx.media3.common.PlaybackParameters(rate())+ com.music.spotui.automix.AutoMix.journal("incoming deck recovered: gentle fade")+ }+ }val x = run.progress()if (x >= 1.0) {// A is gone.@@ -846,8 +935,10 @@ object LocalPlayer {run.state = MixState.RESTORING} else {val lv = plan.levels(x)- a.volume = lv.gainA.toFloat(); d.volume = lv.gainB.toFloat()- run.eqA?.set(lv.lowA, lv.highA); run.eq?.set(lv.lowB, lv.highB)+ val recovery = if (run.recoveryStartedAt == 0L) 1f else ((android.os.SystemClock.uptimeMillis() - run.recoveryStartedAt) / 400f).coerceIn(0f, 1f)+ a.volume = lv.gainA.toFloat() * (run.recoveryGainA + (1f - run.recoveryGainA) * recovery)+ d.volume = lv.gainB.toFloat()+ if (com.music.spotui.automix.AutoMix.djEq) { run.eqA?.set(lv.lowA, lv.highA); run.eq?.set(lv.lowB, lv.highB) }// B low in the mix, or a cut (B whole at once, A out within two beats): corrected fast.if (a.isPlaying) lock(run, a, preroll = false, quiet = lv.gainB < 0.35 || plan.style == app.myspoti.automix.MixPlan.Style.CUT)// Paused: the ticks start again with the play (onIsPlayingChanged).@@ -880,6 +971,7 @@ object LocalPlayer {/** The user moved in the song before the mix: the deck goes (prepared again if the end is still ahead). */private fun mixMoved() {+ planned = null; preparationFailures = 0; retryDeckAt = 0val run = mix ?: returnif (run.state < MixState.MIXING) cancelMix("seek", again = true)}@@ -888,8 +980,9 @@ object LocalPlayer {val run = mix ?: returnif (run.state >= MixState.MIXING) returnmix = null- // Not prepared again for this pair (a deck that failed would fail again), unless the user moved.- if (!again) dropped = run.toIndex to run.toId+ planned = null+ if (!again) { preparationFailures++; retryDeckAt = android.os.SystemClock.uptimeMillis() + 500 }+ // Retry a future cue; after two failures a plain fade is prepared instead. Three attempts at most.run.eq?.release()run.deck.release()com.music.spotui.automix.AutoMix.journal("deck dropped: $why")@@ -916,7 +1009,25 @@ object LocalPlayer {/** A planner setting changed (length, tempo range): the next mix is planned again unless it already started. */fun autoMixSettingsChanged() {- if (mix == null) { planned = null; asking = null; updateCue() }+ if (mix?.state?.let { it < MixState.MIXING } == true) cancelMix("settings changed", again = true)+ if (mix == null) {+ planned = null; asking = null; preparationFailures = 0; retryDeckAt = 0+ updateCue()+ if (exo?.isPlaying == true) { handler.removeCallbacks(tick); handler.post(tick) }+ }+ }++ fun autoMixEqChanged() {+ if (!com.music.spotui.automix.AutoMix.djEq) {+ deckEq?.neutral(); mix?.eqA?.neutral(); mix?.eq?.neutral()+ } else {+ exo?.let { if (deckEq == null) deckEq = com.music.spotui.automix.DeckEq.attach(it.audioSessionId) }+ mix?.let { run ->+ if (run.deck === exo) run.eq = deckEq+ else if (run.eq == null) run.eq = com.music.spotui.automix.DeckEq.attach(run.deck.audioSessionId)+ if (run.a !== exo && run.eqA == null) run.a?.let { run.eqA = com.music.spotui.automix.DeckEq.attach(it.audioSessionId) }+ }+ }}/** For the command line: where the mix stands. */diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.ktindex 4cdc85af..33f9ab85 100644--- a/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.kt+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.kt@@ -463,6 +463,10 @@ private fun CrossfadePage(back: () -> Unit) {}}if (mode == TransitionMode.AUTOMIX) immersiveGroup("automix", headerRes = R.string.settings_automix_title, footerRes = R.string.settings_automix_detail) {+ row {+ ImmersiveSwitchRow(stringResource(R.string.settings_automix_session), checked = automix.sessionDj,+ onChecked = automix::changeSessionDj, detail = stringResource(R.string.settings_automix_session_detail))+ }row {ImmersiveSwitchRow(stringResource(R.string.settings_automix_on_next), checked = automix.mixOnNext,onChecked = automix::changeMixOnNext, detail = stringResource(R.string.settings_automix_on_next_detail))diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.ktindex 6d6acaec..dbdd5d35 100644--- a/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.kt+++ b/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.kt@@ -598,6 +598,8 @@ private fun TransitionsRow() {com.music.spotui.ui.theme.AppAccent, AppText, AppMuted, Modifier.padding(top = 12.dp))}if (mode == TransitionMode.AUTOMIX) {+ OptionRow(stringResource(R.string.settings_automix_session), stringResource(R.string.settings_automix_session_detail),+ checked = automix.sessionDj, onChecked = automix::changeSessionDj)OptionRow(stringResource(R.string.settings_automix_on_next), stringResource(R.string.settings_automix_on_next_detail),checked = automix.mixOnNext, onChecked = automix::changeMixOnNext)OptionRow(stringResource(R.string.settings_automix_album_order), stringResource(R.string.settings_automix_album_order_detail),diff --git a/base/app/src/main/res/values-de/myspoti_strings.xml b/base/app/src/main/res/values-de/myspoti_strings.xmlindex 66eb7d60..6214e60b 100644--- a/base/app/src/main/res/values-de/myspoti_strings.xml+++ b/base/app/src/main/res/values-de/myspoti_strings.xml@@ -726,7 +726,7 @@<string name="settings_automix_style_cut">Schnitt auf dem Beat</string><string name="settings_automix_style_fade">Sanfte Blende</string><string name="settings_automix_style_detail">Der Beat-Mix gleicht die Tempos an, wo es geht, sonst schneidet er auf dem Beat oder blendet. Der Schnitt auf dem Beat wechselt auf einer Eins. Die sanfte Blende blendet, ohne das Tempo zu ändern.</string>- <string name="settings_automix_length_detail">Kurz: 4 bis 8 Takte. Ausgewogen: 2 bis 16, wie die Musik es zulässt. Lang: 12 bis 32 Takte, bis etwa eine Minute.</string>+ <string name="settings_automix_length_detail">Kurz: 4 bis 8 Takte. Ausgewogen: vorzugsweise 8 bis 16. Lang: vorzugsweise 12 bis 32, bis zu 90 Sekunden. Kürzer, wenn es die Musik erfordert.</string><string name="settings_automix_harmonic">Harmonisches Mixen</string><string name="settings_automix_harmonic_detail">Songs in unpassenden Tonarten bekommen kurze Mixe, passende lange. Aus: Tonarten werden ignoriert.</string><string name="settings_automix_podcasts">Podcasts mixen</string>diff --git a/base/app/src/main/res/values-el/myspoti_strings.xml b/base/app/src/main/res/values-el/myspoti_strings.xmlindex 140a859b..982950bf 100644--- a/base/app/src/main/res/values-el/myspoti_strings.xml+++ b/base/app/src/main/res/values-el/myspoti_strings.xml@@ -726,7 +726,7 @@<string name="settings_automix_style_cut">Κοπή στον χρόνο</string><string name="settings_automix_style_fade">Ομαλό σβήσιμο</string><string name="settings_automix_style_detail">Η μίξη στον ρυθμό ταιριάζει τα τέμπο όταν μπορεί, αλλιώς κόβει στον χρόνο ή σβήνει. Η κοπή στον χρόνο αλλάζει τραγούδι σε ισχυρό χρόνο. Το ομαλό σβήσιμο δεν αλλάζει το τέμπο.</string>- <string name="settings_automix_length_detail">Σύντομη: 4 έως 8 μέτρα. Ισορροπημένη: 2 έως 16, όπως επιτρέπει η μουσική. Μεγάλη: 12 έως 32 μέτρα, έως περίπου ένα λεπτό.</string>+ <string name="settings_automix_length_detail">Σύντομη: 4 έως 8 μέτρα. Ισορροπημένη: κατά προτίμηση 8 έως 16. Μεγάλη: κατά προτίμηση 12 έως 32, έως 90 δευτερόλεπτα. Συντομότερη όταν το απαιτεί η μουσική.</string><string name="settings_automix_harmonic">Αρμονική μίξη</string><string name="settings_automix_harmonic_detail">Τραγούδια σε τονικότητες που συγκρούονται παίρνουν σύντομες μίξεις, σε συμβατές μεγάλες. Ανενεργό: η τονικότητα αγνοείται.</string><string name="settings_automix_podcasts">Μίξη podcast</string>diff --git a/base/app/src/main/res/values-es/myspoti_strings.xml b/base/app/src/main/res/values-es/myspoti_strings.xmlindex e540653b..5b55b194 100644--- a/base/app/src/main/res/values-es/myspoti_strings.xml+++ b/base/app/src/main/res/values-es/myspoti_strings.xml@@ -726,7 +726,7 @@<string name="settings_automix_style_cut">Corte en el tiempo</string><string name="settings_automix_style_fade">Fundido suave</string><string name="settings_automix_style_detail">La mezcla al ritmo iguala los tempos cuando puede; si no, corta en el tiempo o hace un fundido. El corte en el tiempo cambia de canción en un primer tiempo. El fundido suave funde sin tocar el tempo.</string>- <string name="settings_automix_length_detail">Corta: 4 a 8 compases. Equilibrada: 2 a 16, según la música. Larga: 12 a 32 compases, hasta un minuto aproximadamente.</string>+ <string name="settings_automix_length_detail">Corta: 4 a 8 compases. Equilibrada: preferiblemente 8 a 16. Larga: preferiblemente 12 a 32, hasta 90 segundos. Más corta cuando la música lo necesita.</string><string name="settings_automix_harmonic">Mezcla armónica</string><string name="settings_automix_harmonic_detail">Las canciones en tonalidades que chocan tienen mezclas cortas; las compatibles, largas. Desactivada, se ignora la tonalidad.</string><string name="settings_automix_podcasts">Mezclar pódcast</string>diff --git a/base/app/src/main/res/values-fr/automix_session_strings.xml b/base/app/src/main/res/values-fr/automix_session_strings.xmlnew file mode 100644index 00000000..9b189ba0--- /dev/null+++ b/base/app/src/main/res/values-fr/automix_session_strings.xml@@ -0,0 +1,5 @@+<?xml version="1.0" encoding="utf-8"?>+<resources>+ <string name="settings_automix_session">Session DJ</string>+ <string name="settings_automix_session_detail">Autoriser les transitions sur un changement de section avant la fin. Désactivez pour garder davantage de chaque morceau.</string>+</resources>diff --git a/base/app/src/main/res/values-fr/myspoti_strings.xml b/base/app/src/main/res/values-fr/myspoti_strings.xmlindex f83fa4d3..69de704b 100644--- a/base/app/src/main/res/values-fr/myspoti_strings.xml+++ b/base/app/src/main/res/values-fr/myspoti_strings.xml@@ -731,7 +731,7 @@<string name="settings_automix_style_cut">Coupe sur le temps</string><string name="settings_automix_style_fade">Fondu doux</string><string name="settings_automix_style_detail">Le mix sur le temps cale les tempos quand il le peut, sinon il coupe sur le temps ou fait un fondu. La coupe sur le temps change de morceau sur un premier temps. Le fondu doux fond sans toucher au tempo.</string>- <string name="settings_automix_length_detail">Court : 4 à 8 mesures. Équilibré : 2 à 16, selon la musique. Long : 12 à 32 mesures, jusqu\'à une minute environ.</string>+ <string name="settings_automix_length_detail">Court : 4 à 8 mesures. Équilibré : de préférence 8 à 16. Long : de préférence 12 à 32, jusqu\'à 90 secondes. Plus court lorsque la musique le demande.</string><string name="settings_automix_harmonic">Mix harmonique</string><string name="settings_automix_harmonic_detail">Les morceaux de tonalités qui se heurtent ont des mix courts, les tonalités proches des mix longs. Désactivé, la tonalité est ignorée.</string><string name="settings_automix_podcasts">Mixer les podcasts</string>diff --git a/base/app/src/main/res/values-it/myspoti_strings.xml b/base/app/src/main/res/values-it/myspoti_strings.xmlindex e43cceaa..44e425ea 100644--- a/base/app/src/main/res/values-it/myspoti_strings.xml+++ b/base/app/src/main/res/values-it/myspoti_strings.xml@@ -726,7 +726,7 @@<string name="settings_automix_style_cut">Taglio sul battere</string><string name="settings_automix_style_fade">Dissolvenza morbida</string><string name="settings_automix_style_detail">Il mix a tempo allinea i tempi quando può, altrimenti taglia sul battere o sfuma. Il taglio sul battere cambia brano su un primo tempo. La dissolvenza morbida sfuma senza toccare il tempo.</string>- <string name="settings_automix_length_detail">Breve: da 4 a 8 battute. Bilanciato: da 2 a 16, come la musica permette. Lungo: da 12 a 32 battute, fino a circa un minuto.</string>+ <string name="settings_automix_length_detail">Breve: da 4 a 8 battute. Bilanciato: preferibilmente da 8 a 16. Lungo: preferibilmente da 12 a 32, fino a 90 secondi. Più breve quando lo richiede la musica.</string><string name="settings_automix_harmonic">Mix armonico</string><string name="settings_automix_harmonic_detail">I brani in tonalità che stridono hanno mix brevi, quelli in tonalità compatibili mix lunghi. Disattivato, la tonalità è ignorata.</string><string name="settings_automix_podcasts">Mixa i podcast</string>diff --git a/base/app/src/main/res/values-pl/myspoti_strings.xml b/base/app/src/main/res/values-pl/myspoti_strings.xmlindex a687478d..10896f10 100644--- a/base/app/src/main/res/values-pl/myspoti_strings.xml+++ b/base/app/src/main/res/values-pl/myspoti_strings.xml@@ -741,7 +741,7 @@<string name="settings_automix_style_cut">Cięcie na bit</string><string name="settings_automix_style_fade">Płynne przenikanie</string><string name="settings_automix_style_detail">Miks w rytm dopasowuje tempa, gdy może, a w przeciwnym razie tnie na bit lub przenika. Cięcie na bit zmienia utwór na raz taktu. Płynne przenikanie nie zmienia tempa.</string>- <string name="settings_automix_length_detail">Krótki: 4–8 taktów. Zrównoważony: 2–16, jak pozwala muzyka. Długi: 12–32 takty, do około minuty.</string>+ <string name="settings_automix_length_detail">Krótki: 4–8 taktów. Zrównoważony: najlepiej 8–16. Długi: najlepiej 12–32, do 90 sekund. Krócej, gdy wymaga tego muzyka.</string><string name="settings_automix_harmonic">Miksowanie harmoniczne</string><string name="settings_automix_harmonic_detail">Utwory w kłócących się tonacjach dostają krótkie miksy, w zgodnych długie. Wyłączone: tonacje są pomijane.</string><string name="settings_automix_podcasts">Miksuj podcasty</string>diff --git a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xmlindex d9529d66..1422e82b 100644--- a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml+++ b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml@@ -705,7 +705,7 @@<string name="settings_automix_style_cut">Corte no tempo</string><string name="settings_automix_style_fade">Fade suave</string><string name="settings_automix_style_detail">A mixagem no ritmo acerta os andamentos quando pode; senão, corta no tempo ou faz fade. O corte no tempo troca de música num primeiro tempo. O fade suave não mexe no andamento.</string>- <string name="settings_automix_length_detail">Curta: 4 a 8 compassos. Equilibrada: 2 a 16, conforme a música. Longa: 12 a 32 compassos, até cerca de um minuto.</string>+ <string name="settings_automix_length_detail">Curta: 4 a 8 compassos. Equilibrada: de preferência 8 a 16. Longa: de preferência 12 a 32, até 90 segundos. Mais curta quando a música pede.</string><string name="settings_automix_harmonic">Mixagem harmônica</string><string name="settings_automix_harmonic_detail">Músicas em tons que não combinam têm mixagens curtas; em tons compatíveis, longas. Desativada, o tom é ignorado.</string><string name="settings_automix_podcasts">Mixar podcasts</string>diff --git a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xmlindex c20c74aa..071e78c5 100644--- a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml+++ b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml@@ -672,7 +672,7 @@<string name="settings_automix_style_cut">Corte no tempo</string><string name="settings_automix_style_fade">Fade suave</string><string name="settings_automix_style_detail">A mistura no ritmo acerta os andamentos quando pode; senão, corta no tempo ou faz fade. O corte no tempo troca de música num primeiro tempo. O fade suave não mexe no andamento.</string>- <string name="settings_automix_length_detail">Curta: 4 a 8 compassos. Equilibrada: 2 a 16, conforme a música. Longa: 12 a 32 compassos, até cerca de um minuto.</string>+ <string name="settings_automix_length_detail">Curta: 4 a 8 compassos. Equilibrada: de preferência 8 a 16. Longa: de preferência 12 a 32, até 90 segundos. Mais curta quando a música o exige.</string><string name="settings_automix_harmonic">Mistura harmónica</string><string name="settings_automix_harmonic_detail">Músicas em tons que chocam têm misturas curtas; em tons compatíveis, longas. Desativada, o tom é ignorado.</string><string name="settings_automix_podcasts">Misturar podcasts</string>diff --git a/base/app/src/main/res/values-pt/myspoti_strings.xml b/base/app/src/main/res/values-pt/myspoti_strings.xmlindex e1685b79..9ef243c7 100644--- a/base/app/src/main/res/values-pt/myspoti_strings.xml+++ b/base/app/src/main/res/values-pt/myspoti_strings.xml@@ -726,7 +726,7 @@<string name="settings_automix_style_cut">Corte no tempo</string><string name="settings_automix_style_fade">Fade suave</string><string name="settings_automix_style_detail">A mistura no ritmo acerta os andamentos quando pode; senão, corta no tempo ou faz fade. O corte no tempo troca de música num primeiro tempo. O fade suave não mexe no andamento.</string>- <string name="settings_automix_length_detail">Curta: 4 a 8 compassos. Equilibrada: 2 a 16, conforme a música. Longa: 12 a 32 compassos, até cerca de um minuto.</string>+ <string name="settings_automix_length_detail">Curta: 4 a 8 compassos. Equilibrada: de preferência 8 a 16. Longa: de preferência 12 a 32, até 90 segundos. Mais curta quando a música o exige.</string><string name="settings_automix_harmonic">Mistura harmónica</string><string name="settings_automix_harmonic_detail">Músicas em tons que chocam têm misturas curtas; em tons compatíveis, longas. Desativada, o tom é ignorado.</string><string name="settings_automix_podcasts">Misturar podcasts</string>diff --git a/base/app/src/main/res/values-ru/myspoti_strings.xml b/base/app/src/main/res/values-ru/myspoti_strings.xmlindex 9347d9f2..78463e25 100644--- a/base/app/src/main/res/values-ru/myspoti_strings.xml+++ b/base/app/src/main/res/values-ru/myspoti_strings.xml@@ -741,7 +741,7 @@<string name="settings_automix_style_cut">Склейка на сильную долю</string><string name="settings_automix_style_fade">Плавное затухание</string><string name="settings_automix_style_detail">Сведение в такт подгоняет темпы, когда может, иначе склеивает на сильную долю или делает затухание. Склейка меняет трек на первой доле такта. Плавное затухание не меняет темп.</string>- <string name="settings_automix_length_detail">Короткий: 4–8 тактов. Сбалансированный: 2–16, как позволяет музыка. Длинный: 12–32 такта, примерно до минуты.</string>+ <string name="settings_automix_length_detail">Короткий: 4–8 тактов. Сбалансированный: предпочтительно 8–16. Длинный: предпочтительно 12–32, до 90 секунд. Короче, если этого требует музыка.</string><string name="settings_automix_harmonic">Гармоническое сведение</string><string name="settings_automix_harmonic_detail">Треки в несовместимых тональностях сводятся коротко, в совместимых — долго. Выключено: тональность не учитывается.</string><string name="settings_automix_podcasts">Сводить подкасты</string>diff --git a/base/app/src/main/res/values-zh/myspoti_strings.xml b/base/app/src/main/res/values-zh/myspoti_strings.xmlindex d3a871a3..0c7bb179 100644--- a/base/app/src/main/res/values-zh/myspoti_strings.xml+++ b/base/app/src/main/res/values-zh/myspoti_strings.xml@@ -726,7 +726,7 @@<string name="settings_automix_style_cut">踩拍切换</string><string name="settings_automix_style_fade">平滑淡入淡出</string><string name="settings_automix_style_detail">节拍混音在可行时对齐速度,否则踩拍切换或淡入淡出。踩拍切换在强拍上换歌。平滑淡入淡出不改变速度。</string>- <string name="settings_automix_length_detail">短:4 到 8 小节。均衡:2 到 16 小节,视音乐而定。长:12 到 32 小节,最长约一分钟。</string>+ <string name="settings_automix_length_detail">短:4 到 8 小节。均衡:优先 8 到 16 小节。长:优先 12 到 32 小节,最长 90 秒。音乐需要时会缩短。</string><string name="settings_automix_harmonic">和声混音</string><string name="settings_automix_harmonic_detail">调性冲突的歌曲混音较短,调性相合的较长。关闭时忽略调性。</string><string name="settings_automix_podcasts">混音播客</string>diff --git a/base/app/src/main/res/values/automix_session_strings.xml b/base/app/src/main/res/values/automix_session_strings.xmlnew file mode 100644index 00000000..305b69fa--- /dev/null+++ b/base/app/src/main/res/values/automix_session_strings.xml@@ -0,0 +1,5 @@+<?xml version="1.0" encoding="utf-8"?>+<resources>+ <string name="settings_automix_session">DJ session</string>+ <string name="settings_automix_session_detail">Let transitions begin at a musical section before the end. Turn off to keep more of each song.</string>+</resources>diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xmlindex 53fccc9b..66e3f3e8 100644--- a/base/app/src/main/res/values/myspoti_strings.xml+++ b/base/app/src/main/res/values/myspoti_strings.xml@@ -733,7 +733,7 @@<string name="settings_automix_style_cut">Cut on the beat</string><string name="settings_automix_style_fade">Smooth fade</string><string name="settings_automix_style_detail">Beat blend matches the tempos where it can, else cuts on the beat or fades. Cut on the beat switches songs on a downbeat. Smooth fade fades without touching the tempo.</string>- <string name="settings_automix_length_detail">Short: 4 to 8 bars. Balanced: 2 to 16, as the music allows. Long: 12 to 32 bars, up to about a minute.</string>+ <string name="settings_automix_length_detail">Short: 4 to 8 bars. Balanced: preferably 8 to 16. Long: preferably 12 to 32, up to 90 seconds. Shorter when the music needs it.</string><string name="settings_automix_harmonic">Harmonic mixing</string><string name="settings_automix_harmonic_detail">Songs in clashing keys get short mixes, matching keys long ones. Off, keys are ignored.</string><string name="settings_automix_podcasts">Mix podcasts</string>diff --git a/base/app/src/test/java/com/music/spotui/automix/AnalysisIdentityTest.kt b/base/app/src/test/java/com/music/spotui/automix/AnalysisIdentityTest.ktnew file mode 100644index 00000000..79e7f101--- /dev/null+++ b/base/app/src/test/java/com/music/spotui/automix/AnalysisIdentityTest.kt@@ -0,0 +1,42 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.automix++import org.junit.Assert.assertEquals+import org.junit.Assert.assertNotEquals+import org.junit.Test++class AnalysisIdentityTest {+ private fun stream(provider: String = "a", duration: Long = 200_000, url: String = "https://audio.test/recording?token=old") =+ AnalysisIdentity.stream("track", provider, duration, "flac", null, url, null)++ @Test fun replacingSourceOrEditionCannotUseOldBeatGrid() {+ assertNotEquals(stream(), stream(provider = "b"))+ assertNotEquals(stream(), stream(duration = 210_000))+ assertNotEquals(stream(), stream(url = "https://audio.test/other-recording?token=old"))+ }++ @Test fun renewingAnAccessTokenDoesNotReanalyseSameRecording() {+ assertEquals(stream(), stream(url = "https://audio.test/recording?token=new"))+ }++ @Test fun queryParametersSelectingAnotherRecordingRemainPartOfTheIdentity() {+ assertNotEquals(stream(url = "https://audio.test/recording?edition=studio&token=old"),+ stream(url = "https://audio.test/recording?edition=live&token=old"))+ assertEquals(stream(url = "https://audio.test/recording?edition=studio&token=old"),+ stream(url = "https://audio.test/recording?edition=studio&token=new"))+ }++ @Test fun replacingAFileAtSamePathInvalidatesItsAnalysis() {+ assertNotEquals(AnalysisIdentity.file("file:///song.flac", 100, 20), AnalysisIdentity.file("file:///song.flac", 101, 20))+ assertNotEquals(AnalysisIdentity.file("file:///song.flac", 100, 20), AnalysisIdentity.file("file:///song.flac", 100, 21))+ }++ @Test fun refreshingDashAccessTokensKeepsAnalysisButAnotherMediaPathDoesNot() {+ fun dash(path: String, token: String): String {+ val xml = "<MPD><BaseURL>https://audio.test/$path?token=$token&Expires=$token</BaseURL></MPD>"+ return AnalysisIdentity.stream("track", "a", 200_000, "flac", null, "", java.util.Base64.getEncoder().encodeToString(xml.toByteArray()))+ }+ assertEquals(dash("same", "old"), dash("same", "new"))+ assertNotEquals(dash("same", "old"), dash("different", "old"))+ }+}diff --git a/base/app/src/test/java/com/music/spotui/automix/AnalysisRetryTest.kt b/base/app/src/test/java/com/music/spotui/automix/AnalysisRetryTest.ktnew file mode 100644index 00000000..25ee0d10--- /dev/null+++ b/base/app/src/test/java/com/music/spotui/automix/AnalysisRetryTest.kt@@ -0,0 +1,34 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.automix++import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Test++class AnalysisRetryTest {+ @Test fun temporaryNetworkFailureCanRecoverWithoutRestart() {+ val retry = AnalysisRetry()+ retry.failed("song", 1_000)+ assertFalse(retry.allowed("song", 5_999))+ assertTrue(retry.allowed("song", 6_000))+ retry.failed("song", 6_000)+ assertFalse(retry.allowed("song", 15_999))+ assertTrue(retry.allowed("song", 16_000))+ retry.succeeded("song")+ assertTrue(retry.allowed("song", 16_001))+ }++ @Test fun skippingAnAnalysisDoesNotBlacklistTheSong() {+ val retry = AnalysisRetry()+ retry.failed("song", 1_000, cancelled = true)+ assertTrue(retry.allowed("song", 1_001))+ }++ @Test fun oneUnavailableRecordingDoesNotBlockAnotherSource() {+ val retry = AnalysisRetry()+ retry.failed("song:sourceA", 1_000)+ assertTrue(retry.allowed("song:sourceB", 1_001))+ retry.clear()+ assertTrue(retry.allowed("song:sourceA", 1_001))+ }+}diff --git a/base/app/src/test/java/com/music/spotui/automix/MixPreparationTest.kt b/base/app/src/test/java/com/music/spotui/automix/MixPreparationTest.ktnew file mode 100644index 00000000..3014b2ad--- /dev/null+++ b/base/app/src/test/java/com/music/spotui/automix/MixPreparationTest.kt@@ -0,0 +1,28 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package com.music.spotui.automix++import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Test++class MixPreparationTest {+ @Test fun lateAnalysisUpgradesFallbackUntilTheMutedRunUpStarted() {+ assertTrue(MixPreparation.mayReplacePlan(false, true, true))+ assertFalse(MixPreparation.mayReplacePlan(true, true, true))+ assertFalse(MixPreparation.mayReplacePlan(false, true, false))+ }++ @Test fun bufferingOrUnalignedDeckCannotReplaceAudibleSong() {+ assertFalse(MixPreparation.readyToTakeOver(false, 5_000, 100_000, 0.0, true))+ assertFalse(MixPreparation.readyToTakeOver(true, 100, 100_000, 0.0, true))+ assertFalse(MixPreparation.readyToTakeOver(true, 5_000, 100_000, 180.0, true))+ assertTrue(MixPreparation.readyToTakeOver(true, 5_000, 100_000, 12.0, true))+ assertTrue(MixPreparation.readyToTakeOver(true, 5_000, 100_000, null, false))+ }++ @Test fun incomingStallPreservesTheSongThatStillPlays() {+ assertTrue(MixPreparation.preserveOutgoing(false, true))+ assertFalse(MixPreparation.preserveOutgoing(true, true))+ assertFalse(MixPreparation.preserveOutgoing(false, false))+ }+}diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt b/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.ktindex 850395dc..ac30de09 100644--- a/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt+++ b/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt@@ -18,13 +18,9 @@ object Analyzer {const val HEAD_SEC = 75.0const val TAIL_SEC = 90.0- /** Seconds of the start and of the end that Beat This reads. */- const val MODEL_HEAD_SEC = 30.0- const val MODEL_TAIL_SEC = 40.0-/**- * [mid] and [side] (or null) at [Features.RATE] Hz. With [model], Beat This reads the song's first- * and last seconds for the metrical level and the downbeats; without it (or when it finds no+ * [mid] and [side] (or null) at [Features.RATE] Hz. With [model], Beat This reads the whole song+ * for the metrical level and the downbeats; without it (or when it finds no* beats) the onsets alone decide.*/fun analyze(mid: FloatArray, side: FloatArray?, model: BeatThis? = null, cancelled: () -> Boolean = { false }, threads: Int = 1): TrackAnalysis {@@ -32,11 +28,13 @@ object Analyzer {// outside the audible part are dropped below).var feats: Features? = nullvar act: BeatThis.Activations? = null- val split = max(1, threads / 2)- parallel(if (model != null) 2 else 1, 2) { task ->- if (task == 0) feats = Features.of(mid, side, cancelled, if (model != null) split else threads)- else act = model!!.run(mid, 0, mid.size, cancelled, max(1, threads - split))+ val budget = max(1, threads)+ val split = max(1, budget / 2)+ parallel(if (model != null) 2 else 1, if (model != null && budget > 1) 2 else 1) { task ->+ if (task == 0) feats = Features.of(mid, side, cancelled, if (model != null) split else budget)+ else act = model!!.run(mid, 0, mid.size, cancelled, max(1, budget - split))}+ if (cancelled()) throw InterruptedException("analysis cancelled")val f = feats!!val n = f.framesval env = BeatTracker.envelope(f)@@ -120,7 +118,7 @@ object Analyzer {val barStarts = starts.toDoubleArray()val bars = barStarts.sizeval barDb = FloatArray(bars); val barLow = FloatArray(bars); val barHigh = FloatArray(bars); val barMid = FloatArray(bars)- val barOnset = FloatArray(bars); val barCentre = FloatArray(bars)+ val barOnset = FloatArray(bars); val barCentre = FloatArray(bars); val absoluteCentre = FloatArray(bars)val barChroma = Array(bars) { FloatArray(12) }val centreMedian = if (f.stereo) percentile(f.centre, 0.5) else 0ffor (i in 0 until bars) {@@ -135,7 +133,8 @@ object Analyzer {var c = 0.0for (j in ca until cb) { for (p in 0 until 12) barChroma[i][p] += f.chroma[j][p]; c += f.centre[j] }for (p in 0 until 12) barChroma[i][p] /= (cb - ca)- barCentre[i] = if (f.stereo) (c / (cb - ca)).toFloat() - centreMedian else 0f+ absoluteCentre[i] = if (f.stereo) (c / (cb - ca)).toFloat() else 0f+ barCentre[i] = if (f.stereo) absoluteCentre[i] - centreMedian else 0f}// A downbeat on the head and on the tail grids: the model's downbeats voted on grid beats, else// the bar start nearest to the grid.@@ -152,11 +151,12 @@ object Analyzer {}val headDown = downOn(head, headSpan); val tailDown = downOn(tail, tailSpan)val novelty = novelty(arrayOf(barDb, barLow, barMid, barHigh, barOnset), barChroma)- val phraseOffset = phraseOffset(novelty, barDb)+ val phraseOffset = phraseOffset(novelty)+ val vocalRisk = VocalRisk.estimate(barDb, barMid, absoluteCentre, barCentre, f.stereo)val (key, keyStrength) = key(f, level, gate)val loud = loudness(f.rmsDb, peak - 20f)return TrackAnalysis(f.durationSec, contentStart, contentEnd, loud, key, keyStrength, bpm, steady, head, tail,- beats, barStarts, barDb, barLow, barHigh, barCentre, novelty, phraseOffset, f.stereo, headDown, tailDown, why.toString().trimEnd(' ', ';'))+ beats, barStarts, barDb, barLow, barHigh, barCentre, novelty, phraseOffset, f.stereo, headDown, tailDown, why.toString().trimEnd(' ', ';'), vocalRisk)}/**@@ -322,7 +322,7 @@ object Analyzer {}/** The bar offset of 8-bar phrases: where the novelty peaks recur every 8 bars. */- private fun phraseOffset(novelty: FloatArray, barDb: FloatArray): Int {+ private fun phraseOffset(novelty: FloatArray): Int {if (novelty.size < 16) return 0val per8 = DoubleArray(8)for (i in novelty.indices) per8[i % 8] += novelty[i].toDouble()diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/BeatThis.kt b/base/automix/src/main/kotlin/app/myspoti/automix/BeatThis.ktindex 98879f0f..6eed42c9 100644--- a/base/automix/src/main/kotlin/app/myspoti/automix/BeatThis.kt+++ b/base/automix/src/main/kotlin/app/myspoti/automix/BeatThis.kt@@ -31,7 +31,7 @@ class BeatThis(model: ByteArray, private val defaultThreads: Int = 4) : AutoClos}fun run(mono: FloatArray, from: Int, to: Int, cancelled: () -> Boolean = { false }, threads: Int = defaultThreads): Activations {- val mel = MelFrontend.spectrogram(mono, from, to, threads)+ val mel = MelFrontend.spectrogram(mono, from, to, threads, cancelled)val n = mel.sizeval beat = FloatArray(n); val down = FloatArray(n)// Chunks of 768 frames that overlap by 12; each keeps all but its 6 edge frames.@@ -106,6 +106,7 @@ object MelFrontend {private val filters = Array(MELS) { DoubleArray(FFT_N / 2 + 1) }private val first = IntArray(MELS); private val last = IntArray(MELS)private val window = hann(FFT_N)+ private val fft = Fft(FFT_N)init {val lo = mel(30.0); val hi = mel(11000.0)@@ -123,20 +124,20 @@ object MelFrontend {private fun mel(f: Double) = if (f < 1000) f / (200.0 / 3) else 15 + ln(f / 1000) / (ln(6.4) / 27)private fun hz(m: Double) = if (m < 15) m * (200.0 / 3) else 1000 * Math.exp((m - 15) * (ln(6.4) / 27))- fun spectrogram(mono: FloatArray, from: Int, to: Int, threads: Int = 1): Array<FloatArray> {+ fun spectrogram(mono: FloatArray, from: Int, to: Int, threads: Int = 1, cancelled: () -> Boolean = { false }): Array<FloatArray> {val n = to - fromrequire(n >= FFT_N) { "audio window too short" }val frames = 1 + n / HOPval out = Array(frames) { FloatArray(MELS) }val parts = max(1, threads)- parallel(parts, parts) { part -> slice(mono, from, n, out, frames * part / parts, frames * (part + 1) / parts) }+ parallel(parts, parts) { part -> slice(mono, from, n, out, frames * part / parts, frames * (part + 1) / parts, cancelled) }return out}- private fun slice(mono: FloatArray, from: Int, n: Int, out: Array<FloatArray>, t0: Int, t1: Int) {- val fft = Fft(FFT_N)+ private fun slice(mono: FloatArray, from: Int, n: Int, out: Array<FloatArray>, t0: Int, t1: Int, cancelled: () -> Boolean) {val re = DoubleArray(FFT_N); val im = DoubleArray(FFT_N); val mag = DoubleArray(FFT_N / 2 + 1)for (t in t0 until t1) {+ if (t and 63 == 0 && cancelled()) throw InterruptedException("analysis cancelled")for (i in 0 until FFT_N) {var at = t * HOP + i - FFT_N / 2while (at < 0 || at >= n) at = if (at < 0) -at else 2 * n - 2 - atdiff --git a/base/automix/src/main/kotlin/app/myspoti/automix/Features.kt b/base/automix/src/main/kotlin/app/myspoti/automix/Features.ktindex af24e68f..6c8ab421 100644--- a/base/automix/src/main/kotlin/app/myspoti/automix/Features.kt+++ b/base/automix/src/main/kotlin/app/myspoti/automix/Features.kt@@ -40,10 +40,17 @@ class Features private constructor(const val LOW_HZ = 180.0const val HIGH_HZ = 2500.0private const val BANDS = 40+ private const val POWER_NORM = N * N / 32.0+ // FFT tables and windows are immutable; every worker owns only its scratch buffers.+ private val onsetFft = Fft(N)+ private val chromaFft = Fft(CHROMA_N)+ private val onsetWindow = hann(N)+ private val chromaWindow = hann(CHROMA_N)- /** [threads]: the onset frames are cut into that many slices, computed side by side (and the chroma beside them). */+ /** [threads] is the total worker budget, including the chroma task and the calling thread. */fun of(mid: FloatArray, side: FloatArray?, cancelled: () -> Boolean = { false }, threads: Int = 1): Features {- val win = hann(N)+ if (cancelled()) throw InterruptedException("analysis cancelled")+ val win = onsetWindowval frames = max(1, (mid.size - N) / HOP + 1)val binHz = RATE.toDouble() / N// Log-spaced bands, 30 Hz to 10 kHz, each at least one bin wide.@@ -60,12 +67,13 @@ class Features private constructor(val bassOnset = FloatArray(frames)val lowDb = FloatArray(frames); val midDb = FloatArray(frames); val highDb = FloatArray(frames); val rmsDb = FloatArray(frames)var chromaResult: Triple<Array<FloatArray>, FloatArray, Boolean>? = null- val slices = max(1, threads)+ val budget = max(1, threads)+ val slices = max(1, budget - 1)// Task 0: the chroma; tasks 1..slices: the onset frames, a slice each (from the frame before it, for the flux).- parallel(slices + 1, slices + 1) { task ->+ parallel(slices + 1, budget) { task ->if (task == 0) chromaResult = chromaAndCentre(mid, side, cancelled) else {val f0 = frames.toLong() * (task - 1) / slices; val f1 = frames.toLong() * task / slices- val fft = Fft(N)+ val fft = onsetFftval frame = DoubleArray(N); val mag = DoubleArray(N / 2 + 1)val re = DoubleArray(N); val im = DoubleArray(N)var prev = DoubleArray(BANDS); var cur = DoubleArray(BANDS)@@ -80,8 +88,7 @@ class Features private constructor(when { k < lowBin -> low += p; k < highBin -> mi += p; else -> hi += p }}// Power to dB of a full-scale sine ~ 0 dB (Hann window sum N/2, one-sided spectrum).- val norm = (N / 4.0).pow(2) / 2- lowDb[f] = db(low / norm); midDb[f] = db(mi / norm); highDb[f] = db(hi / norm); rmsDb[f] = db((low + mi + hi) / norm)+ lowDb[f] = db(low / POWER_NORM); midDb[f] = db(mi / POWER_NORM); highDb[f] = db(hi / POWER_NORM); rmsDb[f] = db((low + mi + hi) / POWER_NORM)for (b in 0 until BANDS) {var s = 0.0for (k in edges[b] until edges[b + 1]) s += mag[k]@@ -105,8 +112,8 @@ class Features private constructor(}private fun chromaAndCentre(mid: FloatArray, side: FloatArray?, cancelled: () -> Boolean): Triple<Array<FloatArray>, FloatArray, Boolean> {- val fft = Fft(CHROMA_N)- val win = hann(CHROMA_N)+ val fft = chromaFft+ val win = chromaWindowval frames = max(1, (mid.size - CHROMA_N) / CHROMA_HOP + 1)val binHz = RATE.toDouble() / CHROMA_N// Pitch class of each bin from 65 Hz (C2) to 2.1 kHz (C7); weights favour the bins near a semitone centre.@@ -117,6 +124,8 @@ class Features private constructor(val centre = FloatArray(frames)val frame = DoubleArray(CHROMA_N); val mag = DoubleArray(CHROMA_N / 2 + 1); val smag = DoubleArray(CHROMA_N / 2 + 1)val re = DoubleArray(CHROMA_N); val im = DoubleArray(CHROMA_N)+ // A decoder can supply a zero side buffer for a mono file: no second FFT is needed.+ val sideSignal = side?.takeIf { samples -> samples.any { it != 0f } }var sideEnergy = 0.0; var midEnergy = 0.0for (f in 0 until frames) {if (f and 255 == 0 && cancelled()) throw InterruptedException("analysis cancelled")@@ -128,8 +137,8 @@ class Features private constructor(var m = 0.0for (k in voiceLo..voiceHi) m += mag[k] * mag[k]midEnergy += m- if (side != null) {- for (i in 0 until CHROMA_N) frame[i] = side.getOrElse(start + i) { 0f } * win[i]+ if (sideSignal != null) {+ for (i in 0 until CHROMA_N) frame[i] = sideSignal.getOrElse(start + i) { 0f } * win[i]fft.magnitudes(frame, smag, re, im)var s = 0.0for (k in voiceLo..voiceHi) s += smag[k] * smag[k]@@ -140,7 +149,7 @@ class Features private constructor(if (norm > 0) for (i in 0 until 12) c[i] /= norm}// A side signal 30 dB under the mid one is a mono master: no centre measure.- val stereo = side != null && sideEnergy > midEnergy * 1e-3+ val stereo = sideSignal != null && sideEnergy > midEnergy * 1e-3return Triple(chroma, centre, stereo)}diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlan.kt b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlan.ktindex 5cb25002..d2b910a0 100644--- a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlan.kt+++ b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlan.kt@@ -5,6 +5,7 @@ import kotlin.math.PIimport kotlin.math.cosimport kotlin.math.powimport kotlin.math.sin+import kotlin.math.sqrt/*** A transition from song A (going out) to song B (coming in).@@ -55,17 +56,24 @@ class MixPlan(if (x <= 0) return Levels(1.0, 0.0, 0.0, KILL, 0.0, 0.0)if (x >= 1) return Levels(0.0, matchB, KILL, 0.0, 0.0, 0.0)val raw = rawLevels(x)- if (matchA >= 0.999) return raw// A eased down to B's level over the first half, so the loudness holds when A leaves.val down = 1 - (1 - matchA) * smooth((x / 0.5).coerceIn(0.0, 1.0))- return Levels(raw.gainA * down, raw.gainB, raw.lowA, raw.lowB, raw.highA, raw.highB)+ val a = raw.gainA * down+ val b = raw.gainB+ // Keep the energy envelope bounded even when the phone cannot attach the DJ EQ.+ // This preserves the fader ratio and the bass hand-off without the +2 dB swell of two+ // full-level decks. It is an equal-power bound, not a limiter for correlated sample peaks.+ val power = a * a + b * b+ val scale = if (power > 1.0) 1.0 / sqrt(power) else 1.0+ if (scale == 1.0 && down == 1.0) return raw+ return Levels(a * scale, b * scale, raw.lowA, raw.lowB, raw.highA, raw.highB)}private fun rawLevels(x: Double): Levels {return when (style) {Style.BLEND -> {val s = swapAt- val ramp = beatShare.coerceAtLeast(0.01)+ val ramp = beatShare.coerceAtLeast(0.0001)// B's fader rises over the first half with its bass cut, sitting under A.val gB = if (x < s) 0.8 * sin(PI / 2 * (x / s)).pow(0.8) else 0.8 + 0.2 * ((x - s) / (1 - s)).coerceIn(0.0, 1.0)// A stays up until the swap, then fades out (equal power) over the second half.diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.ktindex fcb84417..4d0f2609 100644--- a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt+++ b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt@@ -1,6 +1,7 @@/* SPDX-License-Identifier: GPL-3.0-only */package app.myspoti.automix+import java.util.WeakHashMapimport kotlin.math.absimport kotlin.math.ceilimport kotlin.math.floor@@ -10,64 +11,59 @@ import kotlin.math.minimport kotlin.math.powimport kotlin.math.roundToInt-/**- * Picks the transition between two analysed songs: where A goes out, where B comes in, how long the- * mix lasts and its style.- *- * Rules (from DJ practice, Mixxx's intro/outro model and Zehren et al. 2020 on cue points):- * - Cues sit on downbeats, preferably on 8-bar phrase starts; the mix lasts 2 to 32 bars- * ([Settings.minBars] to [Settings.maxBars]).- * - A goes out in its outro: late, where its level drops, without cutting a loud part short.- * - B comes in at its first downbeat (or a later phrase start within its intro), and its first big- * change (the drop, the first verse) should land when A is gone.- * - Beat-matching only with clear grids on both sides and a tempo change within [Settings.maxStretch]- * (half and double time count); keys a fifth apart or more and two voices at once shorten the mix.- * - Otherwise a cut on A's downbeat (A has a grid) or a plain fade.- * The listener may ask for one style ([Settings.style]): always a cut on the beat, or always a fade.- */+/** Plans transitions on checked beat grids, local section boundaries and aligned voice-like activity. */object MixPlanner {class Settings(/** Largest tempo change applied to B, as a share (0.08 = 8 %). */val maxStretch: Double = 0.08,- /** Longest mix, seconds. */+ /** Longest mix, seconds, in addition to [maxBars]. */val maxSec: Double = 32.0,/** Fade length without a beat grid, seconds. */val fadeSec: Double = 6.0,- /** The listener's taste for mix length: -1 short, 0 as scored, 1 long (a lean within [minBars]..[maxBars]). */+ /** -1 short, 0 balanced, 1 long. */val lengthBias: Int = 0,- /**- * The style asked for: [MixPlan.Style.BLEND] beat-matched where it can (a cut or a fade- * otherwise), [MixPlan.Style.CUT] always a cut on A's downbeat (a fade when A has no grid),- * [MixPlan.Style.FADE] always a plain equal-power fade of [smoothSec], tempos untouched.- */+ /** BLEND chooses a safe beat blend, then a cut or fade; CUT and FADE request those styles. */val style: MixPlan.Style = MixPlan.Style.BLEND,- /** Shortest blend wanted, bars: a shorter one only when no longer one fits (each halving costs a lot). */+ /** Prefer at least these bars when a musically safe blend of that length fits. */val minBars: Int = 2,- /** Longest blend, bars (2 to 32). */val maxBars: Int = 16,- /** Weight of the key distance (Camelot) in a blend's score: 0 ignores the keys, 2 harmonic mixing. */+ /** 0 ignores keys, 2 strongly discourages known harmonic clashes. Unknown is not a clash. */val keyWeight: Double = 1.0,- /** Length of the fade when the listener asks for fades ([style] FADE), seconds. */val smoothSec: Double = 8.0,+ /** Allow a structural exit earlier in the second half, rather than preserving every late bar. */+ val djMode: Boolean = false,)- fun plan(a: TrackAnalysis, b: TrackAnalysis, s: Settings = Settings()): MixPlan {- if (s.style == MixPlan.Style.FADE) return fade(a, b, "smooth fade asked", s.smoothSec)+ fun plan(a: TrackAnalysis, b: TrackAnalysis, s: Settings = Settings()): MixPlan =+ planAfter(a, b, a.contentStart, s) ?: MixPlan(MixPlan.Style.FADE, a.contentStart, b.contentStart,+ 0.05, 1.0, 0, 0.5, 0.1, matchGain(a, b), 0.0, "no remaining audio")++ /**+ * A plan whose exit is at or after [earliestExit] (A seconds). A missed cue is never returned+ * again. The caller includes its preparation/run-up lead; null means A has no useful time left.+ */+ fun planAfter(a: TrackAnalysis, b: TrackAnalysis, earliestExit: Double, s: Settings = Settings()): MixPlan? {+ if (!earliestExit.isFinite() || a.contentEnd <= max(a.contentStart, earliestExit) + 0.05 || b.contentEnd <= b.contentStart) return null+ val sa = summary(a); val sb = summary(b)+ if (s.style == MixPlan.Style.FADE) return fade(a, b, sa, "smooth fade asked", s.smoothSec, earliestExit)val ga = a.tail; val gb = b.headval match = if (ga != null && gb != null && !a.tailDown.isNaN() && !b.headDown.isNaN()) tempoMatch(ga.bpm, gb.bpm) else nullif (s.style == MixPlan.Style.BLEND && match != null && abs(match.speed - 1) <= s.maxStretch) {- blend(a, b, ga!!, gb!!, match, s)?.let { return it }+ blend(a, b, sa, sb, ga!!, gb!!, match, s, earliestExit)?.let { return it }}- if (ga != null && !a.tailDown.isNaN()) cut(a, b, ga, gb, match, s)?.let { return it }- return fade(a, b, if (match == null) "no beat grid" else String.format("tempo %.1f → %.1f too far", ga!!.bpm, gb!!.bpm), s.fadeSec)+ // Quiet low/high bands and little voice-like activity can support a longer unsynced+ // handover. Do not reduce such a pair to a one-second cut merely for its BPM gap.+ if (s.style == MixPlan.Style.BLEND && (s.keyWeight == 0.0 || keyDistance(a, b) < 1.5)) {+ val seconds = max(s.fadeSec, s.smoothSec)+ val soft = fade(a, b, sa, "soft passages, tempos untouched", seconds, earliestExit)+ if (soft != null && sa.softTexture(a, soft.exitA, soft.endA) &&+ sb.softTexture(b, soft.entryB, soft.entryB + soft.length)) return soft+ }+ if (ga != null && !a.tailDown.isNaN()) cut(a, b, sa, sb, ga, gb, match, s, earliestExit)?.let { return it }+ return fade(a, b, sa, if (match == null) "no checked beat pair" else "no safe blend ahead", s.fadeSec, earliestExit)}- /**- * The mix for a skip (the user pressed Next at A's time [now]; the next song can be heard [lead]- * seconds later at the earliest): short, from A's next downbeat. Two bars beat-matched (one when- * two are over 5.5 s), else a cut on the downbeat, else a 2.5 s fade. Either analysis may be missing.- * The style asked for holds: no blend with CUT, a 2.5 s fade with FADE.- */+ /** Next: a short mix from the next downbeat; either analysis may be unavailable. */fun planSkip(a: TrackAnalysis?, b: TrackAnalysis?, now: Double, lead: Double, s: Settings = Settings()): MixPlan {val from = now + leadval ga = a?.let { gridAt(it, from) }@@ -90,7 +86,6 @@ object MixPlanner {if (s.style == MixPlan.Style.FADE) "skip: fade" else "skip: fade (no beat grid here)")}- /** The grid (and one of its downbeats) holding at A's time [t]: the tail's near the end, the head's near the start, the one grid of a steady song. */private fun gridAt(a: TrackAnalysis, t: Double): Pair<Grid, Double>? {val tail = a.tail?.takeIf { !a.tailDown.isNaN() && t >= max(it.first, a.contentEnd - Analyzer.TAIL_SEC) && t < it.last }if (tail != null) return tail to a.tailDown@@ -98,268 +93,323 @@ object MixPlanner {return head?.let { it to a.headDown }}- /** B's speed so its beat (or half or double beat) falls on A's; [beatsPerB] A beats per B beat. */class TempoMatch(val speed: Double, val beatsPerB: Double)fun tempoMatch(bpmA: Double, bpmB: Double): TempoMatch =listOf(0.5, 1.0, 2.0).map { m -> TempoMatch(bpmA / (bpmB * m), m) }.minBy { abs(ln(it.speed)) }- private class Cue(val time: Double, val score: Double, val why: String)+ private class Cue(val time: Double, val score: Double, val label: String, val strongBeat: Double = Double.NaN) {+ // Formatting every discarded cue costs more than its score on a cold Android runtime.+ val why: String get() = if (strongBeat.isNaN()) String.format("%s at %.1f", label, time)+ else String.format("B from %.1f for its strong beat at %.1f", time, strongBeat)+ }+ private class Entry(val cue: Cue, val end: Double, val score: Double, val relativeLevel: Double)+ private class Choice(val exit: Cue, val entry: Cue, val bars: Int, val score: Double)+ private const val MIN_BLEND_SCORE = 0.5+ private val BARS = intArrayOf(32, 24, 16, 12, 8, 6, 4, 2)- private fun blend(a: TrackAnalysis, b: TrackAnalysis, ga: Grid, gb: Grid, m: TempoMatch, s: Settings): MixPlan? {+ private fun blend(a: TrackAnalysis, b: TrackAnalysis, sa: Summary, sb: Summary, ga: Grid, gb: Grid,+ m: TempoMatch, s: Settings, earliest: Double): MixPlan? {val barA = 4 * ga.period- val exits = exitCues(a, ga)- val entries = entryCues(b, gb)+ val exits = exitCues(a, sa, ga, s, earliest)+ val entries = entryCues(b, sb, gb)if (exits.isEmpty() || entries.isEmpty()) return nullval keyPenalty = keyDistance(a, b)- val lengths = BARS.filter { it <= max(2, s.maxBars) }- var best: MixPlan? = null; var bestScore = Double.NEGATIVE_INFINITY- val drops = drops(b, gb)- for (e in exits) for (bars in lengths) for (n in entries + dropEntries(b, gb, drops, bars * barA * m.speed)) {+ val strong = drops(b, sb, gb)+ var best: Choice? = null+ var preferred: Choice? = null+ for (bars in BARS) {+ if (bars > max(2, s.maxBars)) continueval len = bars * barAif (len > s.maxSec + 0.01 && bars > 2) continue- // A must still be playing (its audible part) for most of the mix.- if (e.time + len > musicEnd(a) + barA) continue- val bEnd = n.time + len * m.speed- if (bEnd > b.contentEnd - 10) continue- // The whole mix where both grids were checked (a grid of a song's first 25 s holds there only).- if (bEnd > gb.last + gb.period || n.time < gb.first - gb.period || e.time < ga.first - ga.period) continue- var score = e.score + n.score- // Long mixes suit songs in compatible keys with steady grids; clashing keys favour short ones- // (as much as the listener weighs the keys: not at all without harmonic mixing).- score += when (bars) { 32 -> 0.6; 24 -> 0.3; 16 -> 1.0; 12 -> 0.6; 8 -> 1.5; 6 -> 0.4; 4 -> 0.8; else -> 0.0 } - keyPenalty * s.keyWeight * bars / 8.0- // The listener's taste: a lean towards short or long mixes (strong enough to win over the defaults),- // and below the shortest length asked for, a cost that only a mix that fits nothing longer pays.- score += s.lengthBias * ((min(bars, 16) - 8) / 8.0 + max(0, bars - 16) / 16.0) * 1.6- if (bars < s.minBars) score -= 6.0 * ln(s.minBars.toDouble() / bars) / ln(2.0)- // Strong tempo changes are heard: shorter mixes then.- score -= abs(m.speed - 1) / 0.08 * bars / 16.0- // A's loud bars after the mix (cut short) and a long silence of A inside the mix.- score -= loudCut(a, e.time + len) * 0.6- score -= max(0.0, e.time + len - musicEnd(a)) / barA * 0.5- // B's strong beat (the drop, the first verse: where its level and bass jump) when A is gone,- // the mix's best moment; a lesser change there still counts.- score += dropAt(b, bEnd) * 2.5 + changeNear(b, bEnd, 4 * gb.period * m.beatsPerB) * 0.6- // Two voices at once.- score -= voiceClash(a, e.time, e.time + len, b, n.time, bEnd) * bars / 8.0- // Two loud songs over each other for long: worse than a loud A over a quiet intro.- score -= max(0.0, levelAt(a, e.time, e.time + len) + levelAt(b, n.time, bEnd)) * 0.08 * bars / 8.0- if (score > bestScore) {- bestScore = score- val swapBar = (bars / 2.0).let { if (bars >= 4) floor(it) else it }- best = MixPlan(MixPlan.Style.BLEND, e.time, n.time, len, m.speed, bars * 4, swapBar / bars, 1.0 / (bars * 4),- matchGain(a, b), restore(m.speed, gb.period), matchA = matchGainA(a, b), note = String.format("%s, %s, %.1f→%.1f BPM, key %s, score %.1f", e.why, n.why, ga.bpm, gb.bpm, keyText(keyPenalty), score))+ val spanB = len * m.speed+ // Each B candidate and its structural/level scores are shared by all A exit candidates.+ val distinctEntries = (entries + dropEntries(b, gb, strong, spanB))+ .groupBy { (it.time * 1000).roundToInt() }.values.map { same -> same.maxBy { it.score } }+ val prepared = distinctEntries.mapNotNull { n ->+ val end = n.time + spanB+ if (end > b.contentEnd - 10 || end > gb.last + gb.period || n.time < gb.first - gb.period) null+ else Entry(n, end, n.score + sb.dropAt(b, end) * 2.5 + changeNear(b, sb, end, 4 * gb.period * m.beatsPerB) * 0.6,+ sb.levelAt(b, n.time, end))+ }+ if (prepared.isEmpty()) continue+ val lengthScore = lengthPreference(bars, s.lengthBias)+ val harmonicCost = keyPenalty * s.keyWeight * bars / 8.0+ val stretchCost = abs(m.speed - 1) / max(0.01, s.maxStretch) * bars / 20.0+ for (e in exits) {+ val endA = e.time + len+ if (endA > sa.musicEnd + barA || endA > a.contentEnd + 0.01 || endA > ga.last + ga.period + 0.01) continue+ val lost = sa.loudCut(a, endA) * if (s.djMode) 0.08 else 0.6+ val endBar = a.barAt(endA + 0.05)+ val structure = if (s.djMode) a.sectionStrength(endBar) * 2.0 else 0.0+ val scoreA = e.score + lengthScore - harmonicCost - stretchCost - lost + structure -+ max(0.0, endA - sa.musicEnd) / barA * 0.5+ val relativeA = sa.levelAt(a, e.time, endA)+ for (n in prepared) {+ val voices = if (sa.maxVoice == 0f || sb.maxVoice == 0f) 0.0 else vocalOverlap(a, e.time, endA, b, n.cue.time, n.end)+ // Disallow a long overlap dominated by two voices, even when Long was requested.+ if (bars >= 8 && voices > 0.65) continue+ val score = scoreA + n.score - voices * bars / 4.0 -+ max(0.0, relativeA + n.relativeLevel) * 0.08 * bars / 8.0+ if (score < MIN_BLEND_SCORE) continue+ if (best == null || score > best.score) best = Choice(e, n.cue, bars, score)+ // Length is a real preference among safe candidates, not a penalty that a drop+ // can accidentally outweigh. Known clashes/high vocal risk may still shorten it.+ val safeLong = (s.keyWeight == 0.0 || keyPenalty < 1.5) && voices < 0.4 &&+ abs(m.speed - 1) <= max(0.01, s.maxStretch * 0.85)+ if (bars >= s.minBars && safeLong && (preferred == null || score > preferred.score)) {+ preferred = Choice(e, n.cue, bars, score)+ }+ }}}- return best+ val c = preferred ?: best ?: return null+ val swapBar = bassSwapBar(b, sb, gb, c.entry.time, c.bars, barA, m.speed)+ val keyNote = if (knownKey(a) && knownKey(b)) keyText(keyPenalty) else "unknown (not penalised)"+ return MixPlan(MixPlan.Style.BLEND, c.exit.time, c.entry.time, c.bars * barA, m.speed, c.bars * 4,+ swapBar / c.bars, 1.0 / (c.bars * 4), matchGain(a, b), restore(m.speed, gb.period), matchA = matchGainA(a, b),+ note = String.format("%s, %s, %.1f→%.1f BPM, key %s, score %.1f", c.exit.why, c.entry.why, ga.bpm, gb.bpm, keyNote, c.score))}- /** Blend lengths tried, bars (longest first). */- private val BARS = intArrayOf(32, 24, 16, 12, 8, 6, 4, 2)+ private fun lengthPreference(bars: Int, bias: Int): Double {+ val base = when (bars) { 32 -> 2.2; 24 -> 2.0; 16 -> 1.8; 12 -> 1.4; 8 -> 1.2; 6 -> 0.6; 4 -> 0.4; else -> 0.0 }+ return base + bias * ((min(bars, 16) - 8) / 8.0 + max(0, bars - 16) / 16.0) * 1.6+ }- private fun cut(a: TrackAnalysis, b: TrackAnalysis, ga: Grid, gb: Grid?, m: TempoMatch?, s: Settings): MixPlan? {- val exits = exitCues(a, ga)+ /** Bass changes on a shared downbeat, near a local arrival when one exists, otherwise halfway. */+ private fun bassSwapBar(b: TrackAnalysis, sb: Summary, gb: Grid, entry: Double, bars: Int, barA: Double, speed: Double): Double {+ var chosen = if (bars >= 4) floor(bars / 2.0) else bars / 2.0+ var score = 0.0+ for (k in max(1, ceil(bars * 0.25).toInt())..floor(bars * 0.75).toInt()) {+ val t = entry + k * barA * speed+ if (abs(firstDown(gb, b.headDown, t - 0.1 * gb.period) - t) > 0.1 * gb.period) continue+ val value = sb.dropAt(b, t) * 1.5 + b.sectionStrength(b.barAt(t + 0.05)) - abs(k.toDouble() / bars - 0.5) * 0.35+ if (value > score) { score = value; chosen = k.toDouble() }+ }+ return chosen+ }++ private fun cut(a: TrackAnalysis, b: TrackAnalysis, sa: Summary, sb: Summary, ga: Grid, gb: Grid?, m: TempoMatch?,+ s: Settings, earliest: Double): MixPlan? {+ val exits = exitCues(a, sa, ga, s, earliest).filter { it.time + 2 * ga.period <= a.contentEnd + 0.01 }if (exits.isEmpty()) return null- // B starts on its first downbeat when it has one, else where its audio starts.- val entry = if (gb != null && !b.headDown.isNaN()) entryCues(b, gb).maxBy { it.score }.time else b.contentStart- val e = exits.maxBy { it.score - loudCut(a, it.time + 2 * ga.period) * 0.6 }+ val entry = if (gb != null && !b.headDown.isNaN()) entryCues(b, sb, gb).maxBy { it.score }.time else b.contentStart+ val e = exits.maxBy { it.score - sa.loudCut(a, it.time + 2 * ga.period) * if (s.djMode) 0.08 else 0.6 }val synced = m != null && abs(m.speed - 1) <= s.maxStretchval speed = if (synced) m!!.speed else 1.0return MixPlan(MixPlan.Style.CUT, e.time, entry, 2 * ga.period, speed, 2, 0.0, 0.5, matchGain(a, b),- if (synced) restore(speed, gb!!.period) else 0.0, "cut on ${e.why}" + if (synced) "" else ", tempos apart")+ if (synced) restore(speed, gb!!.period) else 0.0, "cut on ${e.why}" + if (synced) "" else ", no common tempo")}- private fun fade(a: TrackAnalysis, b: TrackAnalysis, why: String, seconds: Double): MixPlan {- val len = min(seconds, (a.contentEnd - a.contentStart) / 4)- // On a bar of A (its bars come from the model's beats even without a steady grid), with B's- // first downbeat on it when B has one.- val target = max(a.contentStart, musicEnd(a) - len * 0.6)- val exit = a.barStarts.lastOrNull { it <= target && it >= target - 6 } ?: target+ private fun fade(a: TrackAnalysis, b: TrackAnalysis, sa: Summary, why: String, seconds: Double, earliest: Double): MixPlan? {+ val minExit = max(a.contentStart, earliest)+ val wanted = min(seconds, (a.contentEnd - a.contentStart) / 4).coerceAtLeast(0.05)+ val target = max(minExit, sa.musicEnd - wanted)+ val exit = a.barStarts.lastOrNull { it <= target && it >= max(minExit, target - 6) } ?: targetval gb = b.head?.takeIf { !b.headDown.isNaN() }val entry = if (gb != null) firstDown(gb, b.headDown, b.contentStart - 0.08) else b.contentStart- return MixPlan(MixPlan.Style.FADE, exit, entry, len, 1.0, 0, 0.5, 0.1, matchGain(a, b), 0.0, "fade: $why", matchA = matchGainA(a, b))+ val len = minOf(wanted, a.contentEnd - exit, b.contentEnd - entry)+ if (len <= 0.05) return null+ return MixPlan(MixPlan.Style.FADE, exit, entry, len, 1.0, 0, 0.5, 0.1, matchGain(a, b), 0.0,+ "fade: $why", matchA = matchGainA(a, b))}- /**- * A's exit cues: downbeats of its tail grid in its last minute and a half, scored for phrase starts,- * a level drop after them (the outro) and being late in the song.- */- private fun exitCues(a: TrackAnalysis, g: Grid): List<Cue> {- val end = musicEnd(a)- // Within the grid's own span (a grid of the last 25 s holds there only).- val from = maxOf(a.contentStart + (end - a.contentStart) * 0.5, end - 90, g.first)+ private fun exitCues(a: TrackAnalysis, sa: Summary, g: Grid, s: Settings, earliest: Double): List<Cue> {+ val end = sa.musicEnd+ val from = maxOf(a.contentStart + (end - a.contentStart) * 0.5, end - if (s.djMode) 180 else 90, g.first, earliest)val to = min(end, g.last) - 2 * g.periodval out = ArrayList<Cue>()- val loud = loudLevel(a)var t = firstDown(g, a.tailDown, from)while (t <= to) {val bar = a.barAt(t + 0.05)- var score = 0.0; var why = ""+ var score = 0.0+ var label = "A bar"if (bar >= 0) {val pos = a.phrasePos(bar)- score += when (pos) { 0 -> 2.0; 4 -> 1.0; else -> 0.0 }- score += (a.barNovelty.getOrElse(bar) { 0f }).toDouble().coerceAtMost(3.0) * 0.4- val before = levelBars(a, bar - 4, bar); val after = levelBars(a, bar, bar + 4)- if (before - after > 3) { score += 1.5; why = "outro" }- why = (if (pos == 0) "A phrase" else if (pos == 4) "A half phrase" else "A bar") + (if (why.isEmpty()) "" else " ($why)") +- String.format(" at %.1f", t)- // Already quiet (the song's end fading): fine, but no later than needed.- if (after < loud - 12) score -= 0.5- } else why = String.format("A bar at %.1f", t)- // Later is better: less of A is lost.- score += (t - from) / max(1.0, to - from) * 1.5- out.add(Cue(t, score, why))+ score += when (pos) { 0 -> 1.4; 4 -> 0.6; else -> 0.0 }+ val section = a.sectionStrength(bar)+ score += section * if (s.djMode) 2.0 else 0.8+ score += a.barNovelty.getOrElse(bar) { 0f }.toDouble().coerceIn(0.0, 3.0) * 0.25+ val before = sa.levelBars(bar - 4, bar); val after = sa.levelBars(bar, bar + 4)+ if (before - after > 3) { score += 1.5; label = "A outro" }+ else if (section > 0) label = "A section"+ else if (pos == 0) label = "A phrase"+ else if (pos == 4) label = "A half phrase"+ if (after < sa.loud - 12) score -= 0.5+ }+ score += (t - from) / max(1.0, to - from) * if (s.djMode) 0.25 else 1.5+ out.add(Cue(t, score, label))t += 4 * g.period}return out}- /** B's strong beats in its first minute: downbeats of its grid where it comes in (see [dropAt]). */- private fun drops(b: TrackAnalysis, g: Grid): List<Double> {+ private fun drops(b: TrackAnalysis, sb: Summary, g: Grid): List<Double> {val out = ArrayList<Double>()var t = firstDown(g, b.headDown, b.contentStart + 4 * g.period)- while (t < b.contentStart + 60 && t < g.last) { if (dropAt(b, t) >= 0.5) out.add(t); t += 4 * g.period }+ while (t < b.contentStart + 96 && t < g.last) { if (sb.dropAt(b, t) >= 0.5) out.add(t); t += 4 * g.period }return out}- /** Entries that put one of B's strong beats [drops] right at the end of a mix lasting [lengthB] of B's seconds. */private fun dropEntries(b: TrackAnalysis, g: Grid, drops: List<Double>, lengthB: Double): List<Cue> =drops.mapNotNull { d ->val n = d - lengthBif (n < b.contentStart - 0.1) return@mapNotNull null- // On B's own downbeat (the mix lasts whole bars of B when the tempos match beat for beat).val down = firstDown(g, b.headDown, n - 2 * g.period)if (abs(down - n) > 0.1 * g.period) null- else Cue(down, 1.5 - (down - b.contentStart) / 25.0, String.format("B from %.1f for its strong beat at %.1f", down, d))+ else Cue(down, 1.5 - (down - b.contentStart) / 25.0, "B from", d)}- /** B's entry cues: its first downbeat, and phrase starts in its first 32 s (each skip costs). */- private fun entryCues(b: TrackAnalysis, g: Grid): List<Cue> {+ private fun entryCues(b: TrackAnalysis, sb: Summary, g: Grid): List<Cue> {val first = firstDown(g, b.headDown, b.contentStart - 0.08)- val out = arrayListOf(Cue(first, 1.0, String.format("B first downbeat %.1f", first)))+ val out = arrayListOf(Cue(first, 1.0, "B first downbeat"))var t = first + 4 * g.period- while (t < b.contentStart + 32 && t < b.contentEnd / 3 && t < g.last - 8 * g.period) {+ while (t < b.contentStart + 40 && t < b.contentEnd / 3 && t < g.last - 8 * g.period) {val bar = b.barAt(t + 0.05)- if (bar >= 0 && b.phrasePos(bar) == 0) {- // Skipping a quiet intro is fine, skipping music costs.- val skipped = t - b.contentStart- val quiet = levelBars(b, 0, bar) < loudLevel(b) - 9- out.add(Cue(t, 1.5 - skipped / (if (quiet) 30.0 else 12.0), String.format("B phrase %.1f", t)))+ if (bar >= 0 && (b.phrasePos(bar) == 0 || b.sectionStrength(bar) > 0)) {+ val quiet = sb.levelBars(0, bar) < sb.loud - 9+ out.add(Cue(t, 1.5 + b.sectionStrength(bar) * 0.5 - (t - b.contentStart) / (if (quiet) 30.0 else 12.0), "B phrase"))}t += 4 * g.period}return out}- /**- * Where A's music really ends: the end of its last bar within 15 dB of its loud level (a long- * fade-out or a quiet tail is left to the mix).- */- fun musicEnd(a: TrackAnalysis): Double {- val loud = loudLevel(a)- val last = (a.bars - 1 downTo 0).firstOrNull { a.barDb[it] > loud - 15 } ?: return a.contentEnd- return min(a.contentEnd, a.barEnd(last))- }+ fun musicEnd(a: TrackAnalysis): Double = summary(a).musicEnd- /** The first downbeat of the grid at or after [from]. */fun firstDown(g: Grid, down: Double, from: Double): Double {val bar = 4 * g.periodreturn down + ceil((from - down) / bar - 1e-6) * bar}- private fun loudLevel(a: TrackAnalysis): Double = a.barDb.sorted().let { if (it.isEmpty()) -20.0 else it[(it.size * 0.7).toInt()].toDouble() }-- private fun levelBars(a: TrackAnalysis, from: Int, to: Int): Double {- var s = 0.0; var c = 0- for (i in max(0, from) until min(a.bars, to)) { s += 10.0.pow(a.barDb[i] / 10.0); c++ }- return if (c == 0) -60.0 else 10 * Math.log10(s / c)- }-- private fun levelAt(a: TrackAnalysis, from: Double, to: Double): Double {- val i = max(0, a.barAt(from)); val j = max(i + 1, a.barAt(to))- return levelBars(a, i, j) - loudLevel(a)- }-- /** Bars of A at full level that the mix ending at [end] leaves out. */- private fun loudCut(a: TrackAnalysis, end: Double): Double {- val loud = loudLevel(a)- var n = 0.0- for (i in max(0, a.barAt(end) + 1) until a.bars) if (a.barDb[i] > loud - 4) n += 1.0- return n- }-/**- * How strongly B "comes in" at [t]: the rise of level and of bass from the 4 bars before the bar- * starting nearest [t] (within half a bar) to the 4 after, 0 (none) to 2 (a big drop).+ * Cheap per-song quantities, once per immutable analysis: no sorting, powers, whole-song scans+ * or level windows in the candidate loop. Values do not reference the weak map's key.*/- private fun dropAt(b: TrackAnalysis, t: Double): Double {- val i = (0 until b.bars).minByOrNull { abs(b.barStarts[it] - t) } ?: return 0.0- val bar = if (i + 1 < b.bars) b.barStarts[i + 1] - b.barStarts[i] else 2.0- if (abs(b.barStarts[i] - t) > bar / 2 || i < 2) return 0.0- fun mean(x: FloatArray, from: Int, to: Int): Double {- var s = 0.0; var c = 0- for (k in max(0, from) until min(b.bars, to)) { s += 10.0.pow(x[k] / 10.0); c++ }- return if (c == 0) -60.0 else 10 * Math.log10(s / c)+ private class Summary(a: TrackAnalysis) {+ val loud = a.barDb.sortedArray().let { if (it.isEmpty()) -20.0 else it[(it.size * 0.7).toInt()].toDouble() }+ private val level = prefix(a.barDb)+ private val low = prefix(a.barLowDb)+ private val high = prefix(a.barHighDb)+ private val loudTail = IntArray(a.bars + 1)+ private val drops = DoubleArray(a.bars)+ val maxVoice = a.barVocalRisk.maxOrNull() ?: 0f+ val noveltyMean = a.barNovelty.average().takeIf { it > 0 } ?: 0.0+ val musicEnd: Double+ init {+ var last = -1+ for (i in a.bars - 1 downTo 0) {+ loudTail[i] = loudTail[i + 1] + if (a.barDb[i] > loud - 4) 1 else 0+ if (last < 0 && a.barDb[i] > loud - 15) last = i+ }+ musicEnd = if (last < 0) a.contentEnd else min(a.contentEnd, a.barEnd(last))+ for (i in 2 until a.bars) {+ val db = levelBars(i, i + 4) - levelBars(i - 4, i)+ val bass = mean(low, i, i + 4) - mean(low, i - 4, i)+ drops[i] = ((max(0.0, db) + 0.5 * max(0.0, bass)) / 4).coerceIn(0.0, 2.0)+ }+ }+ fun levelBars(from: Int, to: Int): Double = mean(level, from, to)+ fun levelAt(a: TrackAnalysis, from: Double, to: Double): Double {+ val i = max(0, a.barAt(from)); val j = max(i + 1, a.barAt(to))+ return levelBars(i, j) - loud+ }+ fun loudCut(a: TrackAnalysis, end: Double): Int = loudTail[(a.barAt(end) + 1).coerceIn(0, a.bars)]+ fun softTexture(a: TrackAnalysis, from: Double, to: Double): Boolean {+ val lo = max(0, a.barAt(from)); val hi = max(lo + 1, a.barAt(to)).coerceAtMost(a.bars)+ if (hi <= lo) return false+ val db = levelBars(lo, hi)+ if (mean(low, lo, hi) - db > -15 || mean(high, lo, hi) - db > -12) return false+ var voice = 0.0+ for (i in lo until hi) voice += a.barVocalRisk.getOrElse(i) { 0f }+ return voice / (hi - lo) < 0.35+ }+ fun dropAt(a: TrackAnalysis, time: Double): Double {+ val i = nearestBar(a, time)+ if (i < 2) return 0.0+ val bar = if (i + 1 < a.bars) a.barStarts[i + 1] - a.barStarts[i] else 2.0+ return if (abs(a.barStarts[i] - time) > bar / 2) 0.0 else drops[i]+ }+ private fun prefix(x: FloatArray): DoubleArray = DoubleArray(x.size + 1).also { out ->+ for (i in x.indices) out[i + 1] = out[i] + 10.0.pow(x[i] / 10.0)+ }+ private fun mean(p: DoubleArray, from: Int, to: Int): Double {+ val lo = from.coerceIn(0, p.lastIndex); val hi = to.coerceIn(lo, p.lastIndex)+ return if (hi == lo) -60.0 else 10 * Math.log10(((p[hi] - p[lo]) / (hi - lo)).coerceAtLeast(1e-12))}- val level = mean(b.barDb, i, i + 4) - mean(b.barDb, i - 4, i)- val bass = mean(b.barLowDb, i, i + 4) - mean(b.barLowDb, i - 4, i)- return ((max(0.0, level) + 0.5 * max(0.0, bass)) / 4.0).coerceIn(0.0, 2.0)}+ private val summaries = WeakHashMap<TrackAnalysis, Summary>()+ private fun summary(a: TrackAnalysis): Summary = synchronized(summaries) { summaries.getOrPut(a) { Summary(a) } }- /** A large novelty (a new section) within a bar of [t] in B. */- private fun changeNear(b: TrackAnalysis, t: Double, bar: Double): Double {+ private fun nearestBar(a: TrackAnalysis, t: Double): Int {+ val i = a.barAt(t)+ if (i < 0) return if (a.bars == 0) -1 else 0+ return if (i + 1 < a.bars && t - a.barStarts[i] > a.barStarts[i + 1] - t) i + 1 else i+ }++ private fun changeNear(b: TrackAnalysis, sb: Summary, t: Double, bar: Double): Double {val i = b.barAt(t + 0.05)- if (i < 0) return 0.0- val mean = b.barNovelty.average().takeIf { it > 0 } ?: return 0.0+ if (i < 0 || sb.noveltyMean <= 0) return 0.0var top = 0.0for (j in max(0, i - 1)..min(b.bars - 1, i + 1)) {- if (abs(b.barStarts[j] - t) <= bar * 1.01) top = max(top, b.barNovelty[j] / mean)+ if (abs(b.barStarts[j] - t) <= bar * 1.01) top = max(top, b.barNovelty[j] / sb.noveltyMean)}return (top - 1).coerceIn(0.0, 2.0)}- /** Both songs' voice bands high at once (stereo songs only): 0 (none) to 1 (all along). */- private fun voiceClash(a: TrackAnalysis, a0: Double, a1: Double, b: TrackAnalysis, b0: Double, b1: Double): Double {- if (!a.stereo || !b.stereo) return 0.0- fun share(x: TrackAnalysis, t0: Double, t1: Double): Double {- val i = max(0, x.barAt(t0)); val j = max(i + 1, x.barAt(t1))- var c = 0; var v = 0- for (k in i until min(x.bars, j)) { c++; if (x.barCentre[k] > 3f) v++ }- return if (c == 0) 0.0 else v.toDouble() / c+ /**+ * Voice-like activity at the same audible moments, after B's time mapping. Integrates over bar+ * boundaries, with less weight at the very quiet ends of the blend. Mono uses its own risk map.+ */+ internal fun vocalOverlap(a: TrackAnalysis, a0: Double, a1: Double, b: TrackAnalysis, b0: Double, b1: Double): Double {+ if (a1 <= a0 || b1 <= b0 || a.bars == 0 || b.bars == 0) return 0.0+ val speed = (b1 - b0) / (a1 - a0)+ var t = a0+ var overlap = 0.0; var weight = 0.0+ while (t < a1 - 1e-6) {+ val ia = a.barAt(t + 1e-6); val ib = b.barAt(b0 + (t - a0) * speed + 1e-6)+ if (ia < 0 || ib < 0) return 0.0+ val to = minOf(a1, a.barEnd(ia), a0 + (b.barEnd(ib) - b0) / speed)+ if (to <= t + 1e-7) break+ val x = ((t + to) * 0.5 - a0) / (a1 - a0)+ val audible = 4 * x * (1 - x)+ val w = (to - t) * audible+ val va = a.barVocalRisk.getOrElse(ia) { 0f }.toDouble().coerceIn(0.0, 1.0)+ val vb = b.barVocalRisk.getOrElse(ib) { 0f }.toDouble().coerceIn(0.0, 1.0)+ overlap += va * vb * w; weight += w+ t = to}- return share(a, a0, a1) * share(b, b0, b1)+ return if (weight > 0) overlap / weight else 0.0}- /** 0: same or neighbouring Camelot keys; 1: two steps; 2: further. Unknown or weak keys: 0.5. */+ private fun knownKey(a: TrackAnalysis): Boolean = a.key >= 0 && a.keyStrength >= 0.5++ /** Same/neighbouring Camelot keys: 0; two steps: 1; further: 2. Unknown carries no clash evidence. */fun keyDistance(a: TrackAnalysis, b: TrackAnalysis): Double {- if (a.key < 0 || b.key < 0 || a.keyStrength < 0.5 || b.keyStrength < 0.5) return 0.5+ if (!knownKey(a) || !knownKey(b)) return 0.0val (na, la) = camelot(a.key); val (nb, lb) = camelot(b.key)val d = min(Math.floorMod(na - nb, 12), Math.floorMod(nb - na, 12)) + if (la != lb) 1 else 0return when { d <= 1 -> 0.0; d == 2 -> 1.0; else -> 2.0 }}- /** Camelot number (1..12) and letter (true = B, major). */fun camelot(key: Int): Pair<Int, Boolean> {val major = key < 12val tonic = if (major) key else Math.floorMod(key - 12 + 3, 12)return (Math.floorMod(tonic * 7 + 7, 12) + 1) to major}- private fun keyText(d: Double) = when (d) { 0.0 -> "compatible"; 0.5 -> "unsure"; 1.0 -> "near"; else -> "clash" }+ private fun keyText(d: Double) = when (d) { 0.0 -> "compatible"; 1.0 -> "near"; else -> "clash" }- /** B louder than A by more than 1.5 dB is brought down to A's level during the mix (down to −6 dB). */private fun matchGain(a: TrackAnalysis, b: TrackAnalysis): Double {val d = a.loudnessDb - b.loudnessDbreturn if (d < -1.5) 10.0.pow(max(d, -6.0) / 20) else 1.0}- /** The other way: A louder than B is brought down towards B's level as the mix goes (no dip when A leaves). */private fun matchGainA(a: TrackAnalysis, b: TrackAnalysis): Double {val d = a.loudnessDb - b.loudnessDbreturn if (d > 1.5) 10.0.pow(-min(d, 6.0) / 20) else 1.0}- /** B's speed back to 1: about 0.3 % per beat, at least 8 beats. */private fun restore(speed: Double, period: Double): Double {val beats = max(8.0, abs(speed - 1) / 0.003)return if (abs(speed - 1) < 1e-4) 0.0 else (beats * period).let { (it * 10).roundToInt() / 10.0 }diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/Sections.kt b/base/automix/src/main/kotlin/app/myspoti/automix/Sections.ktnew file mode 100644index 00000000..3ba687a5--- /dev/null+++ b/base/automix/src/main/kotlin/app/myspoti/automix/Sections.kt@@ -0,0 +1,37 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.automix++import kotlin.math.abs+import kotlin.math.max+import kotlin.math.min++/** Local section boundaries from the features already measured for the beat grid; no extra model. */+internal object Sections {+ class Map(val starts: IntArray, val strengths: FloatArray)++ fun detect(novelty: FloatArray): Map {+ val strength = FloatArray(novelty.size)+ if (novelty.size < 12) return Map(IntArray(0), strength)+ val finite = novelty.filter { it.isFinite() }.sorted()+ if (finite.isEmpty()) return Map(IntArray(0), strength)+ val median = finite[finite.size / 2]+ val deviations = finite.map { abs(it - median) }.sorted()+ // Sustained change across the surrounding bars, clearly above the song's novelty floor.+ val threshold = median + max(0.65f, deviations[deviations.size / 2] * 2.5f)+ val scale = max(0.9f, finite[(finite.size * 0.9).toInt().coerceAtMost(finite.lastIndex)] - median)+ val peaks = (4 until novelty.size - 4).filter { bar ->+ val value = novelty[bar]+ value.isFinite() && value >= threshold && (max(0, bar - 2)..min(novelty.lastIndex, bar + 2)).all { other ->+ novelty[other] < value || (novelty[other] == value && other >= bar)+ }+ }.sortedByDescending { novelty[it] }+ val kept = ArrayList<Int>()+ for (bar in peaks) {+ // One boundary for a broad change, never several adjacent "new sections".+ if (kept.any { abs(it - bar) < 4 }) continue+ kept.add(bar)+ strength[bar] = ((novelty[bar] - median) / scale * 0.75f).coerceIn(0.0f, 1.0f)+ }+ return Map(kept.sorted().toIntArray(), strength)+ }+}diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt b/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.ktindex 1700ce42..9ffebefb 100644--- a/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt+++ b/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt@@ -11,8 +11,8 @@ import java.io.OutputStream** Bars are 4 beats: bar i starts at [barStarts][i]. Per bar: level (dB), low-band level (dB),* high-band level (dB), centre (voice-band mid over side, dB above the song's median) and novelty- * (how much the 4 bars from here differ from the 4 before). [phraseOffset]: bars i with- * (i - phraseOffset) % 8 == 0 start 8-bar phrases.+ * (how much the 4 bars from here differ from the 4 before). Strong local changes can restart a+ * phrase; [phraseOffset] is the fallback when no local boundary was found.*/class TrackAnalysis(val duration: Double,@@ -42,8 +42,20 @@ class TrackAnalysis(val tailDown: Double,/** Why a start or end got no grid (for the journal), "" otherwise. */val note: String = "",+ /**+ * Local risk of a prominent voice-like mid band, 0..1. A cheap spectral heuristic, not a vocal+ * model: centred instruments may also score. Mono is uncertain rather than assumed voice-free.+ */+ val barVocalRisk: FloatArray = FloatArray(barStarts.size),) {val bars get() = barStarts.size+ private val sections by lazy(LazyThreadSafetyMode.PUBLICATION) { Sections.detect(barNovelty) }++ /** Strong local structural changes in bars; no named verse/chorus labels are inferred. */+ val sectionStarts: IntArray get() = sections.starts++ /** Confidence of a local structural change, 0..1 (0 away from the selected boundaries). */+ fun sectionStrength(bar: Int): Double = sections.strengths.getOrElse(bar) { 0f }.toDouble()/** The bar containing [t], or -1 before the first. */fun barAt(t: Double): Int {@@ -54,8 +66,14 @@ class TrackAnalysis(fun barEnd(i: Int): Double = if (i + 1 < bars) barStarts[i + 1] else contentEnd- /** 0 on an 8-bar phrase start, 4 on its middle (a 4-bar start), else other. */- fun phrasePos(i: Int): Int = Math.floorMod(i - phraseOffset, 8)+ /** 0 at a local phrase start, 4 at its middle; a global 8-bar offset is the fallback. */+ fun phrasePos(i: Int): Int {+ val starts = sectionStarts+ val found = starts.binarySearch(i)+ val before = if (found >= 0) found else -found - 2+ val anchor = if (before >= 0) starts[before] else phraseOffset+ return Math.floorMod(i - anchor, 8)+ }fun write(out: OutputStream) {val o = DataOutputStream(out)@@ -67,11 +85,12 @@ class TrackAnalysis(o.writeInt(bars); barStarts.forEach(o::writeDouble)for (a in listOf(barDb, barLowDb, barHighDb, barCentre, barNovelty)) a.forEach(o::writeFloat)o.writeInt(phraseOffset); o.writeBoolean(stereo); o.writeDouble(headDown); o.writeDouble(tailDown); o.writeUTF(note.take(2000))+ for (bar in 0 until bars) o.writeFloat(barVocalRisk.getOrElse(bar) { 0f })o.flush()}companion object {- const val VERSION = 4_07+ const val VERSION = 4_08fun read(input: InputStream): TrackAnalysis? {val i = DataInputStream(input)@@ -83,8 +102,10 @@ class TrackAnalysis(val n = i.readInt()val starts = DoubleArray(n) { i.readDouble() }val arrays = List(5) { FloatArray(n) { i.readFloat() } }+ val phrase = i.readInt(); val stereo = i.readBoolean(); val headDown = i.readDouble(); val tailDown = i.readDouble(); val note = i.readUTF()+ val vocal = FloatArray(n) { i.readFloat() }return TrackAnalysis(duration, cs, ce, loud, key, ks, bpm, steady, head, tail, beats, starts,- arrays[0], arrays[1], arrays[2], arrays[3], arrays[4], i.readInt(), i.readBoolean(), i.readDouble(), i.readDouble(), i.readUTF())+ arrays[0], arrays[1], arrays[2], arrays[3], arrays[4], phrase, stereo, headDown, tailDown, note, vocal)}private fun writeGrid(o: DataOutputStream, g: Grid?) {diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/VocalRisk.kt b/base/automix/src/main/kotlin/app/myspoti/automix/VocalRisk.ktnew file mode 100644index 00000000..47a18b45--- /dev/null+++ b/base/automix/src/main/kotlin/app/myspoti/automix/VocalRisk.kt@@ -0,0 +1,26 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.automix++import kotlin.math.max++/**+ * A conservative cue-selection hint from existing spectral measurements, not source separation.+ * Absolute centre energy matters too: a voice continuing through most of a song has no excess+ * above its own median. Mono has no useful stereo centre, so it retains uncertainty.+ */+internal object VocalRisk {+ fun estimate(level: FloatArray, midBand: FloatArray, centre: FloatArray, relative: FloatArray, stereo: Boolean): FloatArray =+ FloatArray(level.size) { i ->+ val active = ((level[i] + 60f) / 25f).coerceIn(0f, 1f)+ val prominence = ((midBand.getOrElse(i) { level[i] } - level[i] + 12f) / 10f).coerceIn(0f, 1f)+ val risk = if (stereo) {+ val absolute = ((centre.getOrElse(i) { 0f } + 1f) / 11f).coerceIn(0f, 1f)+ val excess = ((relative.getOrElse(i) { 0f } + 1f) / 7f).coerceIn(0f, 1f)+ max(absolute, excess) * prominence+ } else {+ // Do not declare a mono song voice-free or pretend to detect a voice from it.+ 0.2f + prominence * 0.35f+ }+ (risk * active).coerceIn(0f, 1f)+ }+}diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/AnalysisStructureTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/AnalysisStructureTest.ktnew file mode 100644index 00000000..81cf8829--- /dev/null+++ b/base/automix/src/test/kotlin/app/myspoti/automix/AnalysisStructureTest.kt@@ -0,0 +1,95 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.automix++import org.junit.Assert.*+import org.junit.Test+import java.io.ByteArrayInputStream+import java.io.ByteArrayOutputStream+import java.util.Collections+import kotlin.math.PI+import kotlin.math.sin++class AnalysisStructureTest {+ private fun analysis(novelty: FloatArray, voice: FloatArray = FloatArray(novelty.size)): TrackAnalysis {+ val n = novelty.size+ val db = FloatArray(n) { -12f }+ return TrackAnalysis(n * 2.0, 0.0, n * 2.0, -12.0, -1, 0.0, 120.0, true,+ null, null, DoubleArray(n * 4) { it * 0.5 }, DoubleArray(n) { it * 2.0 },+ db, db, db, FloatArray(n), novelty, 1, false, Double.NaN, Double.NaN, barVocalRisk = voice)+ }++ @Test fun structuralChangesRestartPhrasesAfterAnIrregularSection() {+ val novelty = FloatArray(64) { 0.2f }.also { it[13] = 5f; it[37] = 6f }+ val a = analysis(novelty)+ assertArrayEquals(intArrayOf(13, 37), a.sectionStarts)+ assertEquals(0, a.phrasePos(13))+ assertEquals(4, a.phrasePos(17))+ assertEquals(0, a.phrasePos(37))+ assertEquals(0, a.phrasePos(1)) // Global phase still applies before the first strong boundary.+ assertTrue(a.sectionStrength(13) > 0.65)+ assertEquals(0.0, a.sectionStrength(14), 0.0)+ }++ @Test fun aSteadyTextureDoesNotInventSections() {+ val novelty = FloatArray(64) { 0.2f + (it % 3) * 0.01f }+ assertTrue(analysis(novelty).sectionStarts.isEmpty())+ }++ @Test fun aBroadChangeHasOneBoundary() {+ val novelty = FloatArray(48) { 0.2f }.also { it[15] = 3f; it[16] = 5f; it[17] = 5f; it[18] = 3f }+ assertArrayEquals(intArrayOf(16), analysis(novelty).sectionStarts)+ }++ @Test fun aContinuingCentralVoiceLikeBandIsNotHiddenByItsOwnMedian() {+ val risk = VocalRisk.estimate(floatArrayOf(-12f), floatArrayOf(-14f), floatArrayOf(12f), floatArrayOf(0f), true)+ assertTrue("absolute centre should retain the risk", risk[0] > 0.8f)+ val mono = VocalRisk.estimate(floatArrayOf(-12f), floatArrayOf(-14f), floatArrayOf(0f), floatArrayOf(0f), false)+ assertTrue("mono is uncertain, not voice-free", mono[0] in 0.2f..0.56f)+ val silent = VocalRisk.estimate(floatArrayOf(-120f), floatArrayOf(-120f), floatArrayOf(30f), floatArrayOf(10f), true)+ assertEquals(0f, silent[0], 0f)+ }++ @Test fun cachedAnalysisKeepsLocalRiskAndSectionAnchors() {+ val novelty = FloatArray(32) { 0.2f }.also { it[13] = 5f }+ val a = analysis(novelty, FloatArray(32) { if (it in 8..15) 0.9f else 0.1f })+ val bytes = ByteArrayOutputStream().also(a::write).toByteArray()+ val b = TrackAnalysis.read(ByteArrayInputStream(bytes))!!+ assertArrayEquals(a.barVocalRisk, b.barVocalRisk, 0f)+ assertArrayEquals(a.sectionStarts, b.sectionStarts)+ assertEquals(a.phrasePos(17), b.phrasePos(17))+ }++ @Test fun oneWorkerBudgetDoesNotSpawnASecondFeatureWorker() {+ val threads = Collections.synchronizedSet(HashSet<Long>())+ val mid = FloatArray(Features.RATE * 2) { (sin(2 * PI * 440 * it / Features.RATE) * 0.2).toFloat() }+ Features.of(mid, FloatArray(mid.size), cancelled = { threads.add(Thread.currentThread().id); false }, threads = 1)+ assertEquals("the decoder supplies zero side for mono; chroma and onsets must share the worker", 1, threads.size)+ }++ @Test fun featureSlicesPreserveResultsWithinTheirThreadBudget() {+ val mid = FloatArray(Features.RATE * 2) { (sin(2 * PI * (220 + it % 7) * it / Features.RATE) * 0.2).toFloat() }+ val one = Features.of(mid, null, threads = 1)+ val workers = Collections.synchronizedSet(HashSet<Long>())+ val two = Features.of(mid, null, cancelled = { workers.add(Thread.currentThread().id); false }, threads = 2)+ assertTrue(workers.size <= 2)+ assertArrayEquals(one.onset, two.onset, 0f)+ assertArrayEquals(one.rmsDb, two.rmsDb, 0f)+ assertArrayEquals(one.chroma[4], two.chroma[4], 0f)+ }++ @Test(expected = InterruptedException::class)+ fun melPreparationCanBeCancelledBeforeRunningTheModel() {+ MelFrontend.spectrogram(FloatArray(Features.RATE * 3), 0, Features.RATE * 3, cancelled = { true })+ }++ @Test fun melPreparationStopsWhenTheAskIsCancelledDuringItsWork() {+ var checks = 0+ try {+ MelFrontend.spectrogram(FloatArray(Features.RATE * 10), 0, Features.RATE * 10,+ cancelled = { ++checks >= 3 })+ fail("an obsolete ask must not finish its full spectrogram")+ } catch (_: InterruptedException) {+ assertTrue("cancellation happened after work began", checks >= 3)+ }+ }+}diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/AutoMixTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/AutoMixTest.ktindex b4c3a217..ec3be62f 100644--- a/base/automix/src/test/kotlin/app/myspoti/automix/AutoMixTest.kt+++ b/base/automix/src/test/kotlin/app/myspoti/automix/AutoMixTest.kt@@ -166,7 +166,7 @@ class AutoMixTest {/** [a] with a clear [key] (the loops' own keys are too weak to count). */private fun withKey(a: TrackAnalysis, key: Int) = TrackAnalysis(a.duration, a.contentStart, a.contentEnd, a.loudnessDb, key, 1.0, a.bpm,a.steady, a.head, a.tail, a.beats, a.barStarts, a.barDb, a.barLowDb, a.barHighDb, a.barCentre, a.barNovelty, a.phraseOffset,- a.stereo, a.headDown, a.tailDown, a.note)+ a.stereo, a.headDown, a.tailDown, a.note, a.barVocalRisk)@Test fun harmonicMixingShortensClashingKeys() {val a = withKey(Analyzer.analyze(loop(124.0, 120.0, seed = 1), null), 0) // C major, 8Bdiff --git a/base/automix/src/test/kotlin/app/myspoti/automix/Bench.kt b/base/automix/src/test/kotlin/app/myspoti/automix/Bench.ktindex 1e78b0a0..def2a642 100644--- a/base/automix/src/test/kotlin/app/myspoti/automix/Bench.kt+++ b/base/automix/src/test/kotlin/app/myspoti/automix/Bench.kt@@ -12,8 +12,8 @@ import kotlin.math.abs* reference. Commands: `analyze <dir>` prints tempo, beat and downbeat agreement with the reference.*/fun main(args: Array<String>) {- val cmd = args.getOrNull(0) ?: return println("usage: analyze <dir> | plan <dir> <a> <b> [out.f32]")- val dir = File(args[1])+ val cmd = args.getOrNull(0) ?: return println("usage: analyze <dir> | plan <dir> a,b [render] | matrix <dir> [long] [whole] [fresh]")+ val dir = args.getOrNull(1)?.let(::File) ?: return println("missing corpus directory")when (cmd) {"analyze" -> {val names = dir.listFiles { f -> f.name.endsWith(".f32") }!!.map { it.nameWithoutExtension }.sorted()@@ -32,6 +32,7 @@ fun main(args: Array<String>) {if (render) Render.render(File(dir, "$x.f32"), File(dir, "$y.f32"), plan, File(dir, "mix-$x-$y.wav"))}}+ "matrix" -> matrix(dir, args.contains("long"), args.contains("fresh"), !args.contains("whole"))"model" -> dir.listFiles { f -> f.name.endsWith(".f32") }!!.map { it.nameWithoutExtension }.sorted().forEach { modelDebug(dir, it) }"profile" -> args.drop(2).forEach { profile(dir, it) }"grids" -> args.drop(2).forEach { gridDebug(dir, it) }@@ -58,8 +59,42 @@ fun loadPcm(file: File): Pair<FloatArray, FloatArray> {val model: BeatThis? by lazy { System.getenv("AUTOMIX_MODEL")?.let { BeatThis(File(it).readBytes(), defaultThreads = System.getenv("AUTOMIX_THREADS")?.toInt() ?: 2) } }fun analysisOf(dir: File, name: String): TrackAnalysis {- val (mid, side) = loadPcm(File(dir, "$name.f32"))- return Analyzer.analyze(mid, side, model, threads = System.getenv("AUTOMIX_THREADS")?.toInt() ?: 2)+ return BenchCache.analysis(dir, name).analysis+}++/** Replans every ordered pair cheaply from persistent analyses: no audio rendering or rereading on a warm run. */+private fun matrix(dir: File, long: Boolean, fresh: Boolean, dj: Boolean) {+ val names = dir.listFiles { f -> f.name.endsWith(".f32") && !f.name.contains(".tmp.") && !f.name.startsWith("mix-") }+ .orEmpty().map { it.nameWithoutExtension }.sorted()+ require(names.size >= 2) { "matrix needs at least two .f32 songs" }+ val analyses = names.associateWith { name ->+ val result = BenchCache.analysis(dir, name, fresh)+ println(String.format(java.util.Locale.ROOT, "track\t%s\t%s\t%.1f ms\t%.2f BPM\t%d sections", name,+ if (result.cached) "cached" else "analysed", result.elapsedMs, result.analysis.bpm, result.analysis.sectionStarts.size))+ result.analysis+ }+ // Same balanced/long policy as AutoMix.settings(), with harmonic mixing and Session DJ on.+ val settings = MixPlanner.Settings(maxSec = if (long) 90.0 else 48.0, lengthBias = if (long) 1 else 0,+ minBars = if (long) 12 else 8, maxBars = if (long) 32 else 16, keyWeight = 2.0, djMode = dj)+ val counts = MixPlan.Style.values().associateWith { 0 }.toMutableMap()+ val costs = ArrayList<Double>(); val lengths = ArrayList<Double>()+ var longBlends = 0+ println("pair\tstyle\tseconds\tbars\texitA\tentryB\tspeedB\tplan_ms\treason")+ for (a in names) for (b in names) if (a != b) {+ val t0 = System.nanoTime()+ val plan = MixPlanner.plan(analyses.getValue(a), analyses.getValue(b), settings)+ val ms = (System.nanoTime() - t0) / 1e6+ costs.add(ms); lengths.add(plan.length)+ counts[plan.style] = counts.getValue(plan.style) + 1+ if (plan.style == MixPlan.Style.BLEND && plan.beats >= 48) longBlends+++ println(String.format(java.util.Locale.ROOT, "%s,%s\t%s\t%.3f\t%.1f\t%.3f\t%.3f\t%.5f\t%.3f\t%s",+ a, b, plan.style, plan.length, plan.beats / 4.0, plan.exitA, plan.entryB, plan.speedB, ms, plan.note))+ }+ costs.sort(); lengths.sort()+ fun percentile(x: List<Double>, q: Double) = x[((x.size - 1) * q).toInt()]+ println(String.format(java.util.Locale.ROOT, "summary\tpairs=%d\tBLEND=%d\tCUT=%d\tFADE=%d\tblend_12plus_bars=%d\tmedian_seconds=%.2f\tplan_median_ms=%.3f\tplan_p95_ms=%.3f",+ costs.size, counts.getValue(MixPlan.Style.BLEND), counts.getValue(MixPlan.Style.CUT), counts.getValue(MixPlan.Style.FADE), longBlends,+ percentile(lengths, 0.5), percentile(costs, 0.5), percentile(costs, 0.95)))}private fun analyzeOne(dir: File, name: String) {diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/BenchCache.kt b/base/automix/src/test/kotlin/app/myspoti/automix/BenchCache.ktnew file mode 100644index 00000000..cc69c923--- /dev/null+++ b/base/automix/src/test/kotlin/app/myspoti/automix/BenchCache.kt@@ -0,0 +1,31 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.automix++import java.io.File++/** Desktop-only cache. Timing comparisons of planners must not re-run the neural model for every pair. */+internal object BenchCache {+ class Result(val analysis: TrackAnalysis, val cached: Boolean, val elapsedMs: Double)++ fun analysis(dir: File, name: String, fresh: Boolean = false): Result {+ val t0 = System.nanoTime()+ val source = File(dir, "$name.f32")+ require(source.isFile) { "missing audio: ${source.name}" }+ val modelFile = System.getenv("AUTOMIX_MODEL")?.let(::File)+ val stamp = "${TrackAnalysis.VERSION}|${source.length()}|${source.lastModified()}|${modelFile?.length()}|${modelFile?.lastModified()}"+ val cache = File(dir, ".automix-bench").apply { mkdirs() }+ val data = File(cache, "$name.analysis")+ val identity = File(cache, "$name.stamp")+ if (!fresh && identity.isFile && runCatching { identity.readText() == stamp }.getOrDefault(false)) {+ val a = runCatching { data.inputStream().buffered().use { TrackAnalysis.read(it) } }.getOrNull()+ if (a != null) return Result(a, true, (System.nanoTime() - t0) / 1e6)+ }+ val (mid, side) = loadPcm(source)+ val a = Analyzer.analyze(mid, side, model, threads = System.getenv("AUTOMIX_THREADS")?.toInt() ?: 2)+ val tmp = File(cache, "$name.tmp")+ tmp.outputStream().buffered().use(a::write)+ check(tmp.renameTo(data)) { "cannot save bench analysis" }+ identity.writeText(stamp)+ return Result(a, false, (System.nanoTime() - t0) / 1e6)+ }+}diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/MixLevelsTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/MixLevelsTest.ktnew file mode 100644index 00000000..5ac9df7f--- /dev/null+++ b/base/automix/src/test/kotlin/app/myspoti/automix/MixLevelsTest.kt@@ -0,0 +1,41 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.automix++import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test+import kotlin.math.abs++class MixLevelsTest {+ @Test fun longBlendStillHandsOverBassWithinOneBeat() {+ val beat = 1.0 / 128+ val plan = MixPlan(MixPlan.Style.BLEND, 100.0, 0.0, 64.0, 1.0, 128, 0.5, beat, 1.0, 0.0, "")+ assertEquals(0.0, plan.levels(0.5).lowA, 1e-9)+ assertEquals(MixPlan.KILL, plan.levels(0.5).lowB, 1e-9)+ assertEquals(MixPlan.KILL, plan.levels(0.5 + beat).lowA, 1e-9)+ assertEquals(0.0, plan.levels(0.5 + beat).lowB, 1e-9)+ }++ @Test fun overlapNeverAddsAnEnergySwellEvenWithoutEq() {+ for (style in MixPlan.Style.entries) for (swap in listOf(0.25, 0.5, 0.75)) {+ for (gainA in listOf(0.5, 1.0)) for (gainB in listOf(0.5, 1.0)) {+ val plan = MixPlan(style, 100.0, 2.0, 32.0, 1.0, 64, swap, 1.0 / 64,+ gainB, 0.0, "", matchA = gainA)+ var previous = plan.levels(0.0)+ for (step in 0..1000) {+ val at = step / 1000.0+ val levels = plan.levels(at)+ assertTrue("$style at $at", levels.gainA.isFinite() && levels.gainB.isFinite())+ assertTrue("$style energy at $at", levels.gainA * levels.gainA + levels.gainB * levels.gainB <= 1.000001)+ assertTrue("$style continuity A at $at", abs(levels.gainA - previous.gainA) < 0.07)+ assertTrue("$style continuity B at $at", abs(levels.gainB - previous.gainB) < 0.07)+ previous = levels+ }+ assertEquals(1.0, plan.levels(0.0).gainA, 0.0)+ assertEquals(0.0, plan.levels(0.0).gainB, 0.0)+ assertEquals(0.0, plan.levels(1.0).gainA, 0.0)+ assertEquals(gainB, plan.levels(1.0).gainB, 0.0)+ }+ }+ }+}diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/MixPlannerPolicyTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/MixPlannerPolicyTest.ktnew file mode 100644index 00000000..f50ced92--- /dev/null+++ b/base/automix/src/test/kotlin/app/myspoti/automix/MixPlannerPolicyTest.kt@@ -0,0 +1,147 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.automix++import org.junit.Assert.assertEquals+import org.junit.Assert.assertNotNull+import org.junit.Assert.assertNull+import org.junit.Assert.assertTrue+import org.junit.Test++/** Controlled analyses isolate scheduling and musical decisions from model accuracy. */+class MixPlannerPolicyTest {+ private fun track(key: Int = 0, risk: (Int) -> Float = { 0f }, tail: Grid? = null): TrackAnalysis {+ val duration = 240.0+ val beat = 0.5+ val grid = Grid(0.0, beat, 480, 1.0, 0.0, 1.0)+ return TrackAnalysis(duration, 0.0, duration, -10.0, key, if (key < 0) 0.0 else 1.0, 120.0,+ true, grid, tail ?: grid, DoubleArray(480) { it * beat }, DoubleArray(120) { it * 2.0 },+ FloatArray(120) { -10f }, FloatArray(120) { -16f }, FloatArray(120) { -20f },+ FloatArray(120), FloatArray(120), 0, false, 0.0, 0.0, barVocalRisk = FloatArray(120, risk))+ }++ private fun long() = MixPlanner.Settings(maxSec = 64.0, lengthBias = 1, minBars = 12, maxBars = 32, keyWeight = 2.0)++ @Test fun unknownKeyDoesNotShortenAnOtherwiseIdenticalLongMix() {+ val known = MixPlanner.plan(track(), track(), long())+ val unknown = MixPlanner.plan(track(-1), track(-1), long())+ assertEquals(MixPlan.Style.BLEND, unknown.style)+ assertEquals(known.length, unknown.length, 1e-9)+ assertEquals(64.0, unknown.length, 1e-9)+ }++ @Test fun safeMinimumLengthWinsOverAnEarlyDropWhenLongerMixesFit() {+ val b = track()+ for (i in 0 until 8) { b.barDb[i] = -25f; b.barLowDb[i] = -31f }+ b.barNovelty[8] = 3f+ val plan = MixPlanner.plan(track(), b, long())+ assertEquals(MixPlan.Style.BLEND, plan.style)+ assertTrue("${plan.beats / 4} bars: $plan", plan.beats >= 48)+ }++ @Test fun longRequestDoesNotForceTwoProminentVoicesTogether() {+ val plan = MixPlanner.plan(track(risk = { 1f }), track(risk = { 1f }), long())+ assertTrue("$plan", plan.style != MixPlan.Style.BLEND || plan.beats < 32)+ }++ @Test fun knownClashCanStillShortenTheMixDespiteLongPreference() {+ val match = MixPlanner.plan(track(), track(), long())+ val clash = MixPlanner.plan(track(), track(6), long())+ assertTrue("match=$match, clash=$clash", clash.beats < match.beats)+ }++ @Test fun voicesInDifferentPartsOfTheOverlapDoNotCollide() {+ val a = track(risk = { if (it < 4) 1f else 0f })+ val b = track(risk = { if (it in 4..7) 1f else 0f })+ assertEquals(0.0, MixPlanner.vocalOverlap(a, 0.0, 16.0, b, 0.0, 16.0), 1e-9)+ assertTrue(MixPlanner.vocalOverlap(a, 0.0, 16.0, a, 0.0, 16.0) > 0.4)+ }++ @Test fun voiceOverlapUsesIncomingEntryAndStretch() {+ val a = track(risk = { if (it < 4) 1f else 0f })+ val b = track(risk = { if (it in 8..15) 1f else 0f })+ assertEquals(0.0, MixPlanner.vocalOverlap(a, 0.0, 8.0, b, 0.0, 16.0), 1e-9)+ assertEquals(1.0, MixPlanner.vocalOverlap(a, 0.0, 8.0, b, 16.0, 32.0), 1e-9)+ }++ @Test fun missedCueReplansToOneThatIsStillAhead() {+ val a = track(); val b = track()+ val first = MixPlanner.plan(a, b, long())+ val earliest = first.exitA + 4.0+ val next = MixPlanner.planAfter(a, b, earliest, long())+ assertNotNull(next)+ assertTrue("$next", next!!.exitA >= earliest)+ assertTrue(next.endA <= a.contentEnd + 0.01)+ }++ @Test fun noPlanIsReturnedAfterTheSongHasEndedForAnyStyle() {+ for (style in MixPlan.Style.values()) {+ assertNull(MixPlanner.planAfter(track(), track(), 240.0, MixPlanner.Settings(style = style)))+ }+ }++ @Test fun lateFallbackStillStartsAheadAndFitsTheRemainingAudio() {+ val plan = MixPlanner.planAfter(track(), track(), 239.0,+ MixPlanner.Settings(style = MixPlan.Style.FADE, smoothSec = 14.0))!!+ assertEquals(239.0, plan.exitA, 1e-9)+ assertEquals(1.0, plan.length, 1e-9)+ assertEquals(240.0, plan.endA, 1e-9)+ }++ @Test fun blendNeverRunsPastTheCheckedOutgoingGrid() {+ val tail = Grid(210.0, 0.5, 40, 1.0, 0.0, 1.0)+ val plan = MixPlanner.plan(track(tail = tail), track(),+ MixPlanner.Settings(maxSec = 64.0, lengthBias = 1, minBars = 12, maxBars = 32, djMode = true))+ assertEquals(MixPlan.Style.BLEND, plan.style)+ assertTrue("$plan", plan.endA <= tail.last + tail.period + 0.01)+ }++ @Test fun djModeCanUseAnEarlierStructuralBreakWithoutChangingTempo() {+ fun outgoing(): TrackAnalysis = track().also { a ->+ for (i in 80 until 88) a.barDb[i] = -22f+ a.barNovelty[80] = 3f+ }+ val preserve = MixPlanner.plan(outgoing(), track())+ val dj = MixPlanner.plan(outgoing(), track(), MixPlanner.Settings(djMode = true))+ assertEquals(MixPlan.Style.BLEND, dj.style)+ assertTrue("preserve=$preserve, DJ=$dj", dj.exitA < preserve.exitA - 10)+ assertEquals(1.0, dj.speedB, 1e-9)+ }++ @Test fun bassHandoverFollowsAStrongArrivalWithinTheMix() {+ val b = track()+ // A bass-only arrival keeps B's earlier melody worth playing: the entry stays at 0.+ for (i in 0 until 8) b.barLowDb[i] = -31f+ b.barNovelty[8] = 3f+ val plan = MixPlanner.plan(track(), b, MixPlanner.Settings(maxSec = 64.0, minBars = 32, maxBars = 32, lengthBias = 1))+ assertEquals(MixPlan.Style.BLEND, plan.style)+ assertEquals(64.0, plan.length, 1e-9)+ assertEquals(0.25, plan.swapAt, 1e-9)+ }++ @Test fun softPassagesWithDifferentTemposGetAnUnstretchedMusicalFade() {+ fun soft(bpm: Double): TrackAnalysis {+ val original = track()+ val beat = 60 / bpm+ val g = Grid(0.0, beat, (240 / beat).toInt(), 1.0, 0.0, 1.0)+ original.barLowDb.fill(-40f); original.barHighDb.fill(-35f)+ return TrackAnalysis(original.duration, original.contentStart, original.contentEnd, original.loudnessDb,+ 0, 1.0, bpm, true, g, g, original.beats, original.barStarts, original.barDb, original.barLowDb,+ original.barHighDb, original.barCentre, original.barNovelty, 0, false, 0.0, 0.0)+ }+ val plan = MixPlanner.plan(soft(120.0), soft(100.0), MixPlanner.Settings(lengthBias = 1, smoothSec = 14.0))+ assertEquals(MixPlan.Style.FADE, plan.style)+ assertEquals(14.0, plan.length, 1e-9)+ assertEquals(1.0, plan.speedB, 1e-9)+ }++ @Test fun rhythmicPassagesWithDifferentTemposAreNotForcedIntoALongFade() {+ val b = track()+ val g = Grid(0.0, 0.6, 400, 1.0, 0.0, 1.0)+ val slower = TrackAnalysis(b.duration, b.contentStart, b.contentEnd, b.loudnessDb, b.key, b.keyStrength,+ 100.0, true, g, g, b.beats, b.barStarts, b.barDb, b.barLowDb, b.barHighDb, b.barCentre, b.barNovelty,+ 0, false, 0.0, 0.0)+ val plan = MixPlanner.plan(track(), slower, MixPlanner.Settings(lengthBias = 1, smoothSec = 14.0))+ assertEquals(MixPlan.Style.CUT, plan.style)+ assertEquals(1.0, plan.length, 1e-9)+ }+}diff --git a/docs/automix/README.md b/docs/automix/README.mdindex d1862f96..ad64f4cf 100644--- a/docs/automix/README.md+++ b/docs/automix/README.md@@ -42,9 +42,12 @@ mobile data" is on, never under the data saver), to mid and side at 22.05 kHz.fitted on the onsets first, and only then on the model's beats alone (its extra off-beat marksleft out; it can be wrong where the onsets are right). When none holds, the journal says why.- Bars (downbeats voted by the model near the start and near the end), per-bar level, low and- high band, voice-band centre (mid over side), novelty (4 bars against the 4 before) and the- 8-bar phrase offset; key (Krumhansl–Kessler on the chroma), loudness, where the sound starts and ends.-- The result is cached (`cache/automix4/`), about 10 KB a song.+ high band, voice-band centre (mid over side), novelty (4 bars against the 4 before), local+ structural boundaries and an 8-bar phrase fallback; key (Krumhansl–Kessler on the chroma),+ loudness, where the sound starts and ends. Voice-like activity uses existing spectral features,+ including absolute centre and mono uncertainty: it is a heuristic, not a vocal separation model.+- The result is cached (`cache/automix4/`, format 4_08), about 10 KB a song. Source metadata/path+ and local file size/modification time identify the recording; expiring access tokens do not.- Speed: on the S24, 5 to 11 s a song (decoding 3–6 s, analysis 3–5 s). The decoder never waits onthe codec while it has work; the onset features (in slices) and Beat This (its 15-s chunks side byside, one ONNX thread each) run at once on all cores but two. The song after the next one is@@ -54,14 +57,17 @@ mobile data" is on, never under the data saver), to mid and side at 22.05 kHz.**Settings** (Transitions → AutoMix, both looks; `MixPlanner.Settings` from `AutoMix.settings()`):- *Transition style*: Beat blend (the styles below, chosen per pair), Cut on the beat (always CUT whenA has a grid, else FADE; a skip cuts too), Smooth fade (always FADE, tempos untouched).-- *Mix length*, three levels mapped to bars: Short 4–8, Balanced 2–16, Long 12–32 (`maxSec` 64 s). The- upper end is a limit; the lower one a preference: a blend under it costs 6 points per halving, so a- pair that fits nothing longer still gets a shorter blend rather than a cut. A smooth fade lasts 4, 8 or 14 s.+- *DJ session* (on in the test branch): permits structural exits earlier in the second half,+ independent of overlap length. Off, late strong bars are preserved more strictly. Album order+ remains a separate setting and still takes precedence.+- *Mix length*: Short 4–8, Balanced preferably 8–16 (48 s cap), Long preferably 12–32 (90 s cap).+ A preferred-length blend must also satisfy vocal, tempo and harmonic checks; otherwise a shorter+ safe transition is allowed. A smooth fade lasts 4, 8 or 14 s.- *Harmonic mixing* (on by default): the key distance weighs ×2 in the blend's score (×1 before this- setting existed); off, ×0 (keys ignored).+ setting existed); off, ×0 (keys ignored). An unknown key carries no evidence of a clash.- *Mix podcasts* (off by default): a podcast episode or an audiobook at either end (`LocalTrack.speech`:downloaded episodes, MediaStore's IS_PODCAST/IS_AUDIOBOOK) is neither analysed nor mixed, and Next cuts.-- *Analyse on mobile data* (off by default): each extension stream is read a second time, about the+- *Analyse on mobile data* (on by default): each extension stream is read a second time, about thesong's size again.- Saving energy (`AutoMix.frugal`: the app's energy saver, its data saver applying, Android's batterysaver, or under 20 % unplugged): the song playing and the next one only, two threads.@@ -70,35 +76,49 @@ mobile data" is on, never under the data saver), to mid and side at 22.05 kHz.- `AutoMix.whyNot()` says why the next song is not mixed (Off, Cast, SpotifyStream, Podcast, Album,MobileData, Frugal, NotAnalyzed), for the screens. Casting: no deck (any is dropped when the cast starts).-**Planning** (`MixPlanner`): exit cues on A's downbeats in its last minute and a half (phrase starts,-a level drop after them, late; never past where its music really ends, before a long fade-out),+**Planning** (`MixPlanner`): exit cues on A's checked downbeats in the second half, up to three+minutes before the end in DJ session mode (local boundaries, phrase starts and level drops),entry cues on B's first downbeat or a phrase start in its intro (a skip costs, less over a quietintro, or the point that puts B's strong beat right at the end of the mix); 32, 24, 16, 12, 8, 6, 4 or 2bars (within the length setting), the whole mix inside the span where both grids were checked, scored first for B's strong beat(the drop, the first verse: where its level and bass jump) landing when A is gone, then for: A's loud bars-left out, two voices at once, two loud songs at once, key distance on the Camelot wheel, size of-the tempo change. Styles:+left out, aligned voice-like activity, key distance on the Camelot wheel and tempo change.+Per-song level prefixes, remaining loud bars and drop scores are prepared once, with B's candidate+scores shared across A's exits. `planAfter` excludes missed cues after a seek or late preparation.+Styles:- **BLEND** when both grids hold and the tempo change is at most 8 % (half and double time count):B comes in under A with its bass cut and its top end a little down, the bass moves from A to B- over one beat at a downbeat in the middle, A fades out (equal power) over the second half with+ over one beat at a shared structural downbeat between 25–75% of the mix, or its middle. A fades out withits top end going down. B louder than A is brought to A's level, then back to its own over 8 s.+ Summed squared fader gains are bounded by one, including when DJ EQ is unavailable. This bounds+ the energy envelope; it does not guarantee that correlated sample peaks cannot clip.- **CUT** when A has a grid but the tempos are too far apart: B starts whole on A's downbeat (onits own first downbeat or a later phrase), A's bass goes at once and A is out within two beats.- **FADE** without a grid: 6 s equal power, A's bass out halfway, from a bar of A (its bars come fromthe model's beats), B on its first downbeat when it has one.+ Two soft passages with little percussion and low vocal risk may get a longer unsynced fade+ across distant tempos; rhythmic passages retain a cut rather than false beat matching.**Playback** (`LocalPlayer`, "AutoMix" part): two decks, no song ever handed from one player to the other.- 15 s before the mix the next song is prepared on a second player at its entry point less a2.5 s run-up, its speed set (AudioTrack playback parameters) so its beats fall on A's, volume 0.- 2.5 s before the mix it starts, muted, and a phase-locked loop compares both positions every20 ms and nudges its speed (up to 8 % while muted, 2 % once heard; a seek if it is 400 ms off).-- At the exit point the deck becomes the main player: the queue around its song, the listener,+- At the exit point, once B is playing, sufficiently buffered and aligned, it becomes the main player: the queue around its song, the listener,the audio focus. The old main player plays the mix out as the outgoing one, then stops.- Gains are the two players' volumes; the bass swap and the top-end cuts are a three-bandDynamicsProcessing EQ on each player's audio session (low under 180 Hz, high over 2.5 kHz).The phone refusing the effect leaves the gains alone.- After the mix the deck's speed goes back to the song's own, about 0.3 % a beat.-- Not analysed in time: a 6 s fade on the songs' lengths, still on two decks.+- Not analysed in time: a provisional 6 s fade, still improvable while its deck is prepared but+ silent. The plan becomes fixed at the muted run-up. A late deck can retry at a future cue, then+ fall back to a safe fade, with at most three preparation attempts per pair.+- Analysis cache hits are loaded before expensive calculations, for at most three tracks (two+ while saving energy). Temporary failures back off; cancellations do not blacklist a song.+ Turning AutoMix or local playback off cancels obsolete analysis. Worker budgets include nested+ feature tasks; no new neural model is added.+- If B buffers during a mix, A stays audible while it can. Once B recovers the players re-enter+ gently; they do not keep chasing an obsolete beat grid through the stall.- **Album order kept**: at the end of a song, no mix into the next song of the same album (thephone's files: the next track number, or the first track of the next disc; streams and downloads,which carry no track number: the album played as the list, the next song in its own order). The@@ -122,6 +142,12 @@ the tempo change. Styles:## Checking it+The isolated test app builds with `./gradlew --offline :app:assembleDebug -Pabis=arm64-v8a+-PautomixLab -PappVersionCode=3115` from `base/`. It is `app.myspoti.automixlab`, labelled+"Myspoti Mix Lab", and installs alongside the regular and dev apps with separate data. No audio+files or private benchmark inputs are included in the repository. Test branch status and measured+results are recorded in [LAB.md](LAB.md).+Desktop bench (real songs, no phone): see [tools/automix/README.md](../../tools/automix/README.md).On the 12 songs it was tuned on (rap, trap, pop, an orchestral piece), against the Beat This final0model as the reference: the tempo level matches on all 12; 10 get one grid for the whole song; thediff --git a/tools/automix/README.md b/tools/automix/README.mdindex 84adcb8f..3e1c4dbf 100644--- a/tools/automix/README.md+++ b/tools/automix/README.md@@ -15,7 +15,16 @@ Then, from `base/`:./gradlew :automix:bench -Pargs="analyze <work folder>" # tempo, grids, downbeats; agreement with the references./gradlew :automix:bench -Pargs="plan <work folder> s01,s02 s03,s06" # plans; beat alignment on the references./gradlew :automix:bench -Pargs="plan <work folder> s01,s02 render" # also mix-s01-s02.wav (12 s before, 15 s after)+./gradlew :automix:bench -Pargs="matrix <work folder> long" # every ordered pair, current app's Long/Session DJ settings+./gradlew :automix:bench -Pargs="matrix <work folder> long whole" # same, preserving the whole-song exit policy```+`plan` and `matrix` keep analyses in `<work folder>/.automix-bench`. The cache is invalidated by+analysis version, source size/modification time and model size/modification time. A warm matrix+does not decode or run ONNX again. Add `fresh` to `matrix` to force a fresh analysis. Each track's+cache/read/analysis time and each pair's planning time are printed separately, followed by counts+of BLEND/CUT/FADE and blends of at least 12 bars. These are plans, not transitions heard on a phone.+The matrix does not render audio; `plan … render` remains explicit.+The bench reads the app's Beat This model (`app/src/main/assets/automix/`) through the JVM build ofONNX Runtime. References: `pip install beat_this` (PyTorch), then `beat_this <work folder>/*.wav`.