Skip to content

Native Android client for Spotify, built with Jetpack Compose

AutoMix fixes from listening on the phone: the outgoing player keeps its own song alone at the hand-over (it went on to the next song from 0:00, heard at full volume, and the mix never ended), DASH streams analysed, plans made again when the songs ahead change, B's strong beat aimed at the end of the mix, grids on shorter spans and on the model's beats

kota-zo142ada5e513a
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.kt
index d0e1c699..3bfdf9ee 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
@@ -86,8 +86,9 @@ object AutoMix {
// A stream is read a second time for the analysis: on unmetered networks only.
if (uri.scheme == ExtensionAudio.SCHEME) {
if (com.music.spotui.data.DataSaver.metered) { journal("not analysed (metered network): ${uri.lastPathSegment}"); return null }
+ // 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 || s.manifest != null) { synchronized(memory) { failed += key }; journal("not analysed (${if (s == null) "no stream" else "DASH stream"}): ${uri.lastPathSegment}"); return null }
+ if (s == null) { synchronized(memory) { failed += key }; journal("not analysed (no stream): ${uri.lastPathSegment}"); return null }
}
val t0 = System.currentTimeMillis()
return try {
@@ -97,7 +98,8 @@ object AutoMix {
val a = Analyzer.analyze(pcm.mid, pcm.side, model(), cancelled, THREADS)
save(key, 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.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
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.kt
index c7244bf3..bd7e60b3 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
@@ -19,7 +19,7 @@ import java.nio.ByteOrder
/**
* Decodes a whole song to the analysis signals: mid ((L+R)/2) and side ((L-R)/2) at 22.05 kHz.
* The bytes come through the player's own data source (downloads are decrypted there, extension
- * streams resolved), so a song is read the way it plays. Not for DASH answers (no single file).
+ * streams resolved, DASH answers read as one file), so a song is read the way it plays.
*/
@androidx.annotation.OptIn(UnstableApi::class)
object TrackDecoder {
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.kt
index b470c254..de5cabbc 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
@@ -482,8 +482,11 @@ object LocalPlayer {
var locked = false
var worstMs = 0.0
var loggedAt = 0L
- /** Progress of the mix from the outgoing song's position. */
- fun progress(): Double = a?.let { ((it.currentPosition / 1000.0) - plan.exitA) / plan.length } ?: 1.0
+ /** 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
+ else ((it.currentPosition / 1000.0) - plan.exitA) / plan.length
+ } ?: 1.0
}
/** The plan for the song playing → the next one, once both are analysed. */
@@ -492,7 +495,7 @@ object LocalPlayer {
private var mix: MixRun? = null
/** The main player's EQ (a deck's once it took over). */
private var deckEq: com.music.spotui.automix.DeckEq? = null
- private var asking: Pair<Int, String>? = null
+ private var asking: String? = null
/** The pair whose deck was dropped: no new deck for it. */
private var dropped: Pair<Int, String>? = null
@@ -556,8 +559,10 @@ object LocalPlayer {
planned?.takeIf { it.fromIndex == cur && it.toId == nextId }?.let { return it.plan }
val a = tracks.getOrNull(cur) ?: return null
val b = tracks.getOrNull(next) ?: return null
- if (asking != cur to nextId) {
- asking = cur to nextId
+ // 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")
@@ -741,6 +746,12 @@ object LocalPlayer {
d.repeatMode = a.repeatMode
d.shuffleModeEnabled = false
mainListener?.let(a::removeListener)
+ // The old player keeps its own song alone: at its end it stops there (with the queue it went
+ // on to the next song, the one the deck plays, from 0:00, heard at full volume).
+ val own = a.currentMediaItemIndex
+ if (own + 1 < a.mediaItemCount) a.removeMediaItems(own + 1, a.mediaItemCount)
+ if (own > 0) a.removeMediaItems(0, own)
+ a.repeatMode = Player.REPEAT_MODE_OFF
// The focus moves with the main role: given up by the old player first, so it does not pause.
a.setAudioAttributes(audioAttributes, false); a.setHandleAudioBecomingNoisy(false)
d.setAudioAttributes(audioAttributes, !mirrorActive); d.setHandleAudioBecomingNoisy(true)
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt b/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt
index 381f952a..850395dc 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt
@@ -65,16 +65,40 @@ object Analyzer {
val q = BeatTracker.quality(env, g, span.first, span.second)
return q.beats >= 16 && q.driftMs <= 40 && (q.steadiness >= 0.8 || (q.steadiness >= 0.6 && agreement(g, mb, span) >= 0.75))
}
- fun local(span: Pair<Double, Double>): Grid? =
- BeatTracker.fit(env, (span.first * FPS).toInt(), min(n, (span.second * FPS).toInt()), global?.period?.times(FPS) ?: p0)
- ?.let { onModel(it, mb) }?.takeIf { holds(it, span) }
+ // A grid of its own there: on the onsets, else on the model's beats alone (sparse or uneven
+ // onsets, as in much trap, where the model still hears a steady beat).
+ val why = StringBuilder()
+ /**
+ * The longest span from the song's start or end (60–90 s, then 45, then 25: a mix only needs
+ * that much) where a grid holds: the global one, else one fitted on the onsets there; only
+ * then one on the model's beats alone (it can be wrong where the onsets are right).
+ */
+ fun near(start: Boolean): Grid? {
+ val spans = doubleArrayOf(if (start) HEAD_SEC else TAIL_SEC, 45.0, 25.0).map { len ->
+ if (start) contentStart to min(contentEnd, contentStart + len) else max(contentStart, contentEnd - len) to contentEnd
+ }
+ val notes = StringBuilder()
+ for (span in spans) {
+ if (global != null && holds(global, span)) return global
+ val g = BeatTracker.fit(env, (span.first * FPS).toInt(), min(n, (span.second * FPS).toInt()), global?.period?.times(FPS) ?: p0)?.let { onModel(it, mb) }
+ if (g != null && holds(g, span)) return g
+ notes.append(String.format("%.0f s: onsets %s; ", span.second - span.first, g?.let { BeatTracker.quality(env, it, span.first, span.second).toString() } ?: "no fit"))
+ }
+ for (span in spans) {
+ val (fb, reason) = fromBeats(mb, span, global?.period ?: (p0 / FPS))
+ if (fb != null) return fb
+ notes.append(String.format("%.0f s: model %s; ", span.second - span.first, reason))
+ }
+ why.append(if (start) "start: " else "end: ").append(notes)
+ return null
+ }
// One grid for the whole song (made on a fixed tempo, as most are): it holds in quiet intros too.
val whole = global?.let { BeatTracker.quality(env, it, contentStart, contentEnd) }
lastWhole = whole
val steady = global != null && whole != null && whole.driftMs <= 40 &&
(whole.steadiness >= 0.75 || (whole.steadiness >= 0.6 && agreement(global, mb, contentStart to contentEnd) >= 0.75))
- val head = if (steady || (global != null && holds(global, headSpan))) global else local(headSpan)
- val tail = if (steady || (global != null && holds(global, tailSpan))) global else local(tailSpan)
+ val head = if (steady) global else near(start = true)
+ val tail = if (steady) global else near(start = false)
val period = global?.period?.times(FPS) ?: p0
val beats = (when {
steady -> DoubleArray(global!!.count) { global.beat(it) }
@@ -118,7 +142,7 @@ object Analyzer {
fun downOn(g: Grid?, span: Pair<Double, Double>): Double {
if (g == null) return Double.NaN
val vote = DoubleArray(4)
- for ((t, v) in modelDowns) if (t in span.first..span.second) {
+ for ((t, v) in modelDowns) if (t in max(span.first, g.first - g.period)..min(span.second, g.last + g.period)) {
val k = Math.round(g.index(t)).toInt()
if (abs(g.beat(k) - t) < g.period * 0.3) vote[Math.floorMod(k, 4)] += v.toDouble()
}
@@ -132,7 +156,7 @@ object Analyzer {
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)
+ beats, barStarts, barDb, barLow, barHigh, barCentre, novelty, phraseOffset, f.stereo, headDown, tailDown, why.toString().trimEnd(' ', ';'))
}
/**
@@ -204,6 +228,46 @@ object Analyzer {
return if (gaps.size < 8) g else g.copy(first = g.first + gaps[gaps.size / 2])
}
+ /**
+ * A constant grid fitted on the model's beats in [span] alone (least squares on their times, a
+ * missed beat counted as such), kept when they sit on it: median gap ≤ 15 ms, 90 % within 35 ms,
+ * no 8-beat stretch off by more than 30 ms, at least 16 beats.
+ */
+ private fun fromBeats(beats: DoubleArray, span: Pair<Double, Double>, period: Double): Pair<Grid?, String> {
+ val t = beats.filter { it >= span.first && it <= span.second }
+ if (t.size < 16) return null to "${t.size} beats"
+ val gaps = t.zipWithNext { a, b -> b - a }.filter { abs(it - period) < 0.15 * period }.sorted()
+ if (gaps.size < 12) return null to "${gaps.size} regular gaps of ${t.size}"
+ var per = gaps[gaps.size / 2]; var first = t[0]
+ val keep = BooleanArray(t.size) { true }
+ repeat(3) { pass ->
+ var n = 0.0; var sk = 0.0; var st = 0.0; var skk = 0.0; var skt = 0.0
+ for (i in t.indices) {
+ val k = Math.round((t[i] - first) / per).toDouble()
+ if (pass > 0) keep[i] = abs(t[i] - (first + k * per)) < 0.08 * per
+ if (!keep[i]) continue
+ n++; sk += k; st += t[i]; skk += k * k; skt += k * t[i]
+ }
+ val den = n * skk - sk * sk
+ if (n < 12 || den <= 0) return null to "too few beats on a grid"
+ per = (n * skt - sk * st) / den; first = (st - per * sk) / n
+ }
+ // Extra detections (off-beats the model also marked) are left out before judging: the beats
+ // that sit on the grid must be most of them, close to it, and not drift.
+ val all = t.map { x -> x - (first + Math.round((x - first) / per) * per) }
+ val res = all.filter { abs(it) <= 0.12 * per }
+ if (res.size < 16) return null to "${res.size} of ${t.size} beats on a grid"
+ val abs = res.map { abs(it) }.sorted()
+ var drift = 0.0
+ for (i in 0..res.size - 8) drift = max(drift, abs(res.subList(i, i + 8).average()))
+ val share = res.size.toDouble() / t.size
+ val text = String.format("%d beats, %.0f %% on the grid, median %.0f ms, 90%% %.0f ms, drift %.0f ms", t.size, share * 100,
+ abs[abs.size / 2] * 1000, abs[(abs.size * 0.9).toInt()] * 1000, drift * 1000)
+ if (share < 0.75 || abs[abs.size / 2] > 0.015 || abs[(abs.size * 0.9).toInt()] > 0.035 || drift > 0.030) return null to text
+ val count = Math.round((t.last() - first) / per).toInt() + 1
+ return Grid(first, per, count, res.count { abs(it) <= 0.035 }.toDouble() / res.size, drift * 1000, 1.0) to text
+ }
+
/** Share of the model's beats (in [span] and the grid's) within 50 ms of a grid beat; 1 without the model. */
private fun agreement(g: Grid, modelBeats: DoubleArray, span: Pair<Double, Double>): Double {
val inside = modelBeats.filter { it >= max(span.first, g.first - 0.05) && it <= min(span.second, g.last + 0.05) }
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
index f7c515fc..349bfc32 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
@@ -92,23 +92,27 @@ object MixPlanner {
if (exits.isEmpty() || entries.isEmpty()) return null
val keyPenalty = keyDistance(a, b)
var best: MixPlan? = null; var bestScore = Double.NEGATIVE_INFINITY
- for (e in exits) for (n in entries) for (bars in intArrayOf(16, 8, 4, 2)) {
+ val drops = drops(b, gb)
+ for (e in exits) for (bars in intArrayOf(16, 12, 8, 6, 4, 2)) for (n in entries + dropEntries(b, gb, drops, bars * barA * m.speed)) {
val len = bars * barA
if (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.
- score += when (bars) { 16 -> 1.0; 8 -> 1.5; 4 -> 0.8; else -> 0.0 } - keyPenalty * bars / 8.0
+ score += when (bars) { 16 -> 1.0; 12 -> 0.6; 8 -> 1.5; 6 -> 0.4; 4 -> 0.8; else -> 0.0 } - keyPenalty * bars / 8.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 change (drop, first verse) when A is gone: a novelty peak near the end of the mix.
- score += changeNear(b, bEnd, 4 * gb.period * m.beatsPerB) * 1.2
+ // 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.
@@ -137,8 +141,13 @@ object MixPlanner {
private fun fade(a: TrackAnalysis, b: TrackAnalysis, s: Settings, why: String): MixPlan {
val len = min(s.fadeSec, (a.contentEnd - a.contentStart) / 4)
- val exit = max(a.contentStart, musicEnd(a) - len * 0.6)
- return MixPlan(MixPlan.Style.FADE, exit, b.contentStart, len, 1.0, 0, 0.5, 0.1, matchGain(a, b), 0.0, "fade: $why")
+ // 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
+ val 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")
}
/**
@@ -147,8 +156,9 @@ object MixPlanner {
*/
private fun exitCues(a: TrackAnalysis, g: Grid): List<Cue> {
val end = musicEnd(a)
- val from = max(a.contentStart + (end - a.contentStart) * 0.5, end - 90)
- val to = end - 2 * g.period
+ // 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)
+ val to = min(end, g.last) - 2 * g.period
val out = ArrayList<Cue>()
val loud = loudLevel(a)
var t = firstDown(g, a.tailDown, from)
@@ -174,12 +184,31 @@ object MixPlanner {
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> {
+ 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 }
+ 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 - lengthB
+ if (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))
+ }
+
/** 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> {
val first = firstDown(g, b.headDown, b.contentStart - 0.08)
val out = arrayListOf(Cue(first, 1.0, String.format("B first downbeat %.1f", first)))
var t = first + 4 * g.period
- while (t < b.contentStart + 32 && t < b.contentEnd / 3) {
+ while (t < b.contentStart + 32 && 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.
@@ -229,6 +258,24 @@ object MixPlanner {
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).
+ */
+ 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)
+ }
+ 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)
+ }
+
/** A large novelty (a new section) within a bar of [t] in B. */
private fun changeNear(b: TrackAnalysis, t: Double, bar: Double): Double {
val i = b.barAt(t + 0.05)
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt b/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt
index fd48f2f0..1700ce42 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt
@@ -40,6 +40,8 @@ class TrackAnalysis(
/** A downbeat of the [head] and of the [tail] grid (NaN without the grid): bars there start every 4 beats from it. */
val headDown: Double,
val tailDown: Double,
+ /** Why a start or end got no grid (for the journal), "" otherwise. */
+ val note: String = "",
) {
val bars get() = barStarts.size
@@ -64,12 +66,12 @@ class TrackAnalysis(
o.writeInt(beats.size); beats.forEach(o::writeDouble)
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.writeInt(phraseOffset); o.writeBoolean(stereo); o.writeDouble(headDown); o.writeDouble(tailDown); o.writeUTF(note.take(2000))
o.flush()
}
companion object {
- const val VERSION = 4_02
+ const val VERSION = 4_07
fun read(input: InputStream): TrackAnalysis? {
val i = DataInputStream(input)
@@ -82,7 +84,7 @@ class TrackAnalysis(
val starts = DoubleArray(n) { i.readDouble() }
val arrays = List(5) { 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())
+ arrays[0], arrays[1], arrays[2], arrays[3], arrays[4], i.readInt(), i.readBoolean(), i.readDouble(), i.readDouble(), i.readUTF())
}
private fun writeGrid(o: DataOutputStream, g: Grid?) {
diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/Bench.kt b/base/automix/src/test/kotlin/app/myspoti/automix/Bench.kt
index fd2b3fb3..1e78b0a0 100644
--- a/base/automix/src/test/kotlin/app/myspoti/automix/Bench.kt
+++ b/base/automix/src/test/kotlin/app/myspoti/automix/Bench.kt
@@ -34,6 +34,7 @@ fun main(args: Array<String>) {
}
"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) }
"tempo" -> dir.listFiles { f -> f.name.endsWith(".f32") }!!.map { it.nameWithoutExtension }.sorted().forEach { tempoDebug(dir, it) }
else -> println("unknown $cmd")
}
@@ -193,3 +194,15 @@ fun profile(dir: File, name: String) {
println()
}
}
+
+/** One song's grids against the reference beats, by region. */
+fun gridDebug(dir: File, name: String) {
+ val a = analysisOf(dir, name)
+ val rb = File(dir, "$name.beats").readLines().filter { it.isNotBlank() }.map { it.trim().split(Regex("\\s+"))[0].toDouble() }.toDoubleArray()
+ for ((label, g) in listOf("head" to a.head, "tail" to a.tail)) {
+ if (g == null) { println("$name $label none"); continue }
+ val gb = DoubleArray(g.count) { g.beat(it) }
+ println(String.format("%s %s %.3f..%.1f s %.2f BPM: F vs ref %.2f, offset %+.0f ms | note: %s", name, label, g.first, g.last, g.bpm,
+ fMeasure(gb, rb.filter { it in g.first..g.last }.toDoubleArray()), meanOffset(gb, rb) * 1000, a.note))
+ }
+}
diff --git a/docs/automix/README.md b/docs/automix/README.md
index 51c43111..f078b6ea 100644
--- a/docs/automix/README.md
+++ b/docs/automix/README.md
@@ -28,7 +28,7 @@ Two Media3 facts shaped the new design (read in Media3 1.8.0, the version the ap
**Analysis** (`base/automix`, pure Kotlin, the same code on the phone and on the desktop bench).
Each song is decoded once through the player's own data source (downloads decrypted, extension
-streams resolved; streams only on unmetered networks, no DASH answers), to mid and side at 22.05 kHz.
+streams resolved, DASH answers read as one file; streams only on unmetered networks), to mid and side at 22.05 kHz.
- Onsets: spectral flux on 40 log bands, plus the bands under 150 Hz (kicks, 808s).
- Beat This! small0 (MIT, int8 ONNX, ONNX Runtime) on the whole song gives the metrical level
(pure onset autocorrelation hesitates between the beat and ¾ or 4⁄3 of it on trap hi-hats) and
@@ -37,7 +37,9 @@ streams resolved; streams only on unmetered networks, no DASH answers), to mid a
least squares on the onset peaks), moved onto the model's beats (same instant for every song),
and checked: the share of beats on the grid, the drift over 8 beats, and the agreement with the
model. A song made on a fixed tempo gets one grid; otherwise the start and the end get grids of
- their own where they hold.
+ their own where they hold: over 60–90 s, else 45, else 25 s from the edge (a mix needs no more),
+ fitted on the onsets first, and only then on the model's beats alone (its extra off-beat marks
+ left 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.
@@ -51,7 +53,9 @@ streams resolved; streams only on unmetered networks, no DASH answers), to mid a
**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),
entry cues on B's first downbeat or a phrase start in its intro (a skip costs, less over a quiet
-intro); 16, 8, 4 or 2 bars, scored for: B's next section starting when A is gone, A's loud bars
+intro, or the point that puts B's strong beat right at the end of the mix); 16, 12, 8, 6, 4 or 2
+bars, 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:
- **BLEND** when both grids hold and the tempo change is at most 8 % (half and double time count):
@@ -60,7 +64,8 @@ the tempo change. Styles:
its top end going down. B louder than A is brought to A's level, then back to its own over 8 s.
- **CUT** when A has a grid but the tempos are too far apart: B starts whole on A's downbeat (on
its 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.
+- **FADE** without a grid: 6 s equal power, A's bass out halfway, from a bar of A (its bars come from
+ the model's beats), B on its first downbeat when it has one.
**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 a
@@ -83,9 +88,18 @@ the tempo change. Styles:
downbeat: two bars beat-matched (one when two last over 5.5 s), else a cut on the beat, else a
2.5 s fade; this holds for songs of one album as well. Not ready within 2.5 s: the plain skip. A
second Next skips at once.
+- At the hand-over the old player keeps its own song alone: at its end it stops (with the queue it
+ went on to the next song by itself, the one the deck plays, from 0:00).
+- The songs ahead are checked every 200 ms: a queue edit (added, removed, moved, "play next")
+ plans the mix again for the new next song and asks for its analysis and the one after it.
- Seek, previous, a queue change or AutoMix off: the deck is dropped (before the mix) or the song
coming in plays on at its own speed (during the mix).
+## Known problems
+
+- The cover now changes after a mix (the stuck cover came from the hand-over bug fixed on
+ 2026-11-03), but its animation during a mix is to be redone.
+
## Checking it
Desktop bench (real songs, no phone): see [tools/automix/README.md](../../tools/automix/README.md).