Skip to content

Native Android client for Spotify, built with Jetpack Compose

AutoMix: locked mixes judged on stretches of bars instead of the song's whole ending: each run of bars fitted on the model's own beats and downbeats, phase measured between the two stretches at the plan's rate, 2-bar windows and up to 8 % of speed; a pair's locked plans go from 2.6 % to 25.4 % of the bench corpus. Eight shapes written as lanes (bass swap, three bands, cut on the beat, opening filter, four fades), chosen by the voices and loudness in the window then by a hash of the pair; fades as long as both songs' voices leave room; a voice left unheard costs score; plans worked out away from the main thread

kota-zoaa1ce2cf7cde
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 51d6a98c..9b59b2bc 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
@@ -195,7 +195,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; preparationFailures = 0; planningPair = null
+ planned = null; preparationFailures = 0; planningPair = null; segueWork = 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.
@@ -697,7 +697,8 @@ object LocalPlayer {
if (segue && ra != null && rb != null && preparationFailures < 2 && noMixFor?.first === ra && noMixFor?.second === rb) return null
if (ra != null && rb != null && preparationFailures < 2) {
val earliest = (p.currentPosition + RUN_UP_MS + startLead + 500) / 1000.0
- choosePlan(a, b, ra, rb, earliest).also { if (it == null && segue) { noMixFor = ra to rb; planned = null; whyNot(null); return null } }?.let { plan ->
+ if (!segueDecided(a, b, ra, rb, earliest)) return null
+ choosePlan(ra, rb, earliest).also { if (it == null && segue) { noMixFor = ra to rb; planned = null; whyNot(null); return null } }?.let { plan ->
planned = Planned(cur, nextId, plan, ra, rb)
whyNot(null)
com.music.spotui.automix.AutoMix.journal("plan ${a.title} → ${b.title}: $plan")
@@ -719,16 +720,45 @@ object LocalPlayer {
return plan
}
- /** The plan for two analysed songs. With the data plans on, null means the policy chose plain playback. */
- private fun choosePlan(a: LocalTrack, b: LocalTrack, ra: app.myspoti.automix.TrackAnalysis, rb: app.myspoti.automix.TrackAnalysis, earliest: Double): app.myspoti.automix.MixPlan? {
- if (!com.music.spotui.automix.AutoMix.segueActive) return com.music.spotui.automix.AutoMix.planAfter(ra, rb, earliest)
+ /** A data plan being worked out away from the main thread: weighing the candidates takes long enough to be felt there. */
+ private class SegueWork(val ra: app.myspoti.automix.TrackAnalysis, val rb: app.myspoti.automix.TrackAnalysis) {
+ @Volatile var done = false
+ var plan: app.myspoti.automix.MixPlan? = null
+ var note = ""
+ }
+ private val segueWorker by lazy { java.util.concurrent.Executors.newSingleThreadExecutor { r -> Thread(r, "segue-plan").apply { isDaemon = true } } }
+ private var segueWork: SegueWork? = null
+
+ /** Whether [choosePlan] has its answer for this pair; asked for the first time, it starts the work. */
+ private fun segueDecided(a: LocalTrack, b: LocalTrack, ra: app.myspoti.automix.TrackAnalysis, rb: app.myspoti.automix.TrackAnalysis, earliest: Double): Boolean {
+ if (!com.music.spotui.automix.AutoMix.segueActive) return true
+ segueWork?.takeIf { it.ra === ra && it.rb === rb }?.let { return it.done }
val album = a.album.trim()
val sameAlbum = com.music.spotui.automix.AutoMix.albumOrder && album.isNotEmpty() && !album.equals("<unknown>", true) && album.equals(b.album.trim(), true)
// Its lanes must be armed before the audio chain, ahead of the player, reaches the cue.
val chainAheadMs = exo?.let { p -> segueStages[p]?.takeIf { it.marked }?.let { (it.positionUs / 1000 - p.currentPosition).coerceAtLeast(0) } } ?: 0L
- val d = com.music.spotui.automix.AutoMix.decide(ra, rb, earliest + (chainAheadMs + MixPreparation.SEGUE_ARM_LEAD_MS) / 1000.0, sameAlbum, rate())
- com.music.spotui.automix.AutoMix.journal("segue ${d.level} ${d.fingerprint?.take(12) ?: ""}${if (d.attempts.isEmpty()) "" else ": ${d.why}"}")
- return app.myspoti.automix.segue.SeguePolicy.timing(d)
+ val from = earliest + (chainAheadMs + MixPreparation.SEGUE_ARM_LEAD_MS) / 1000.0
+ val listener = rate()
+ val work = SegueWork(ra, rb)
+ segueWork = work
+ segueWorker.execute {
+ runCatching {
+ val d = com.music.spotui.automix.AutoMix.decide(ra, rb, from, sameAlbum, listener)
+ work.note = "segue ${d.level} ${d.shape ?: ""} ${d.fingerprint?.take(12) ?: ""}${if (d.attempts.isEmpty()) "" else ": ${d.why}"}"
+ work.plan = app.myspoti.automix.segue.SeguePolicy.timing(d)
+ }.onFailure { work.note = "segue failed: $it"; work.plan = null }
+ work.done = true
+ }
+ return false
+ }
+
+ /** The plan for two analysed songs, once [segueDecided]. With the data plans on, null means the policy chose plain playback. */
+ private fun choosePlan(ra: app.myspoti.automix.TrackAnalysis, rb: app.myspoti.automix.TrackAnalysis, earliest: Double): app.myspoti.automix.MixPlan? {
+ if (!com.music.spotui.automix.AutoMix.segueActive) return com.music.spotui.automix.AutoMix.planAfter(ra, rb, earliest)
+ val work = segueWork ?: return null
+ segueWork = null
+ com.music.spotui.automix.AutoMix.journal(work.note)
+ return work.plan
}
/**
@@ -763,11 +793,13 @@ object LocalPlayer {
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 trackA = tracks.getOrNull(p.currentMediaItemIndex) ?: return
val trackB = tracks.getOrNull(next) ?: return
- val plan = choosePlan(trackA, trackB, a, b, (p.currentPosition + RUN_UP_MS + startLead + 500) / 1000.0)
+ val earliest = (p.currentPosition + RUN_UP_MS + startLead + 500) / 1000.0
+ if (!segueDecided(trackA, trackB, a, b, earliest)) return
+ // Once per completed analysis pair, including pairs with no future musical cue.
+ planned = Planned(p.currentMediaItemIndex, nextId, run.plan, a, b)
+ val plan = choosePlan(a, b, earliest)
if (plan == null) {
// The policy says no effect: the prepared fade goes too.
if (com.music.spotui.automix.AutoMix.segueActive) { noMixFor = a to b; cancelMix("no mix for this pair", again = true) }
@@ -1072,7 +1104,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 = 0; noMixFor = null
+ planned = null; preparationFailures = 0; retryDeckAt = 0; noMixFor = null; segueWork = null
val run = mix ?: return
if (run.state < MixState.MIXING) cancelMix("seek", again = 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 08fb6a21..68523b7a 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/Analyzer.kt
@@ -2,6 +2,7 @@
package app.myspoti.automix
import kotlin.math.abs
+import kotlin.math.exp
import kotlin.math.max
import kotlin.math.min
import kotlin.math.sqrt
@@ -15,6 +16,8 @@ object Analyzer {
private const val FPS = Features.FPS
/** Seconds of the song's start and end the head and tail grids are fitted on. */
+ /** How near a bar start the model's downbeat must be to speak for it [I]. */
+ private const val DOWN_NEAR_SEC = 0.07
const val HEAD_SEC = 75.0
const val TAIL_SEC = 90.0
@@ -161,6 +164,12 @@ object Analyzer {
val sum = vote.sum()
return if (sum <= 0) 0f else (((vote.max() / sum) - 0.25) / 0.75).toFloat().coerceIn(0f, 1f)
}
+ // The model's word on each bar start: its logit as a probability, 0 when it placed no downbeat there.
+ val barDownConfidence = if (act == null) FloatArray(0) else FloatArray(bars) { i ->
+ var best = 0f
+ for ((t, v) in modelDowns) if (abs(t - barStarts[i]) <= DOWN_NEAR_SEC) best = max(best, 1f / (1f + exp(-v)))
+ best
+ }
val novelty = novelty(arrayOf(barDb, barLow, barMid, barHigh, barOnset), barChroma)
val phraseOffset = phraseOffset(novelty)
val vocalRisk = VocalRisk.estimate(barDb, barMid, absoluteCentre, barCentre, f.stereo)
@@ -168,7 +177,8 @@ object Analyzer {
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(' ', ';'), vocalRisk,
- signalKey(mid, side), barBass, phaseStats(head, mb, headSpan), phaseStats(tail, mb, tailSpan), metre(head, headSpan), metre(tail, tailSpan))
+ signalKey(mid, side), barBass, phaseStats(head, mb, headSpan), phaseStats(tail, mb, tailSpan), metre(head, headSpan), metre(tail, tailSpan),
+ mb, barDownConfidence)
}
/** The model's beats against a grid: median and 95th percentile of their distance; null with too few to tell. */
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 c3be3a1c..f0d06197 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
@@ -197,12 +197,12 @@ object MixPlanner {
}
/** Bass changes on a shared downbeat, near a local arrival when one exists, otherwise halfway. */
- internal fun bassSwapBar(b: TrackAnalysis, sb: Summary, gb: Grid, entry: Double, bars: Int, barA: Double, speed: Double): Double {
+ internal fun bassSwapBar(b: TrackAnalysis, sb: Summary, gb: Grid, entry: Double, bars: Int, barA: Double, speed: Double, down: Double = b.headDown): 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
+ if (abs(firstDown(gb, down, 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() }
}
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 d661f890..54a17f5c 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/TrackAnalysis.kt
@@ -60,11 +60,22 @@ class TrackAnalysis(
/** How clearly the downbeats' votes chose one phase on each grid, 0..1 (0: no evidence). */
val headMetre: Float = 0f,
val tailMetre: Float = 0f,
+ /** The beat model's own beats, seconds; empty without the model. Stretches of bars are judged on these. */
+ val modelBeats: DoubleArray = DoubleArray(0),
+ /** How sure the model is of a downbeat at each bar start, 0..1; empty without the model. */
+ val barDownConfidence: FloatArray = FloatArray(0),
) {
val bars get() = barStarts.size
/** Identify the grids a plan relies on: a new analysis with another grid invalidates the plan. */
val headGridKey: String? by lazy(LazyThreadSafetyMode.PUBLICATION) { head?.let { gridKey(it, headDown) } }
val tailGridKey: String? by lazy(LazyThreadSafetyMode.PUBLICATION) { tail?.let { gridKey(it, tailDown) } }
+ /** Identifies the beats and bars a plan was placed on: another analysis of them invalidates the plan. */
+ val beatKey: String by lazy(LazyThreadSafetyMode.PUBLICATION) {
+ val text = StringBuilder()
+ for (t in barStarts) text.append(Math.round(t * 1e4)).append(',')
+ for (t in modelBeats) text.append(Math.round(t * 1e4)).append(',')
+ app.myspoti.automix.segue.SegueCodec.hex(text.toString().toByteArray())
+ }
private val sections by lazy(LazyThreadSafetyMode.PUBLICATION) { Sections.detect(barNovelty) }
/** Strong local structural changes in bars; no named verse/chorus labels are inferred. */
@@ -105,11 +116,13 @@ class TrackAnalysis(
o.writeUTF(audioKey)
for (bar in 0 until bars) o.writeFloat(barBassDb.getOrElse(bar) { -60f })
writePhase(o, headPhase); writePhase(o, tailPhase); o.writeFloat(headMetre); o.writeFloat(tailMetre)
+ o.writeInt(modelBeats.size); modelBeats.forEach(o::writeDouble)
+ o.writeInt(barDownConfidence.size); barDownConfidence.forEach(o::writeFloat)
o.flush()
}
companion object {
- const val VERSION = 4_09
+ const val VERSION = 4_10
fun read(input: InputStream): TrackAnalysis? {
val i = DataInputStream(input)
@@ -126,9 +139,11 @@ class TrackAnalysis(
val audioKey = i.readUTF()
val bass = FloatArray(n) { i.readFloat() }
val headPhase = readPhase(i); val tailPhase = readPhase(i); val headMetre = i.readFloat(); val tailMetre = i.readFloat()
+ val modelBeats = DoubleArray(i.readInt()) { i.readDouble() }
+ val barDown = FloatArray(i.readInt()) { 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], phrase, stereo, headDown, tailDown, note, vocal,
- audioKey, bass, headPhase, tailPhase, headMetre, tailMetre)
+ audioKey, bass, headPhase, tailPhase, headMetre, tailMetre, modelBeats, barDown)
}
private fun gridKey(g: Grid, down: Double): String = app.myspoti.automix.segue.SegueCodec.hex(
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueCompiler.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueCompiler.kt
index 9884ecac..6f271761 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueCompiler.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueCompiler.kt
@@ -9,6 +9,26 @@ import kotlin.math.roundToLong
import kotlin.math.sin
import kotlin.math.sqrt
+/** How a transition sounds: each is lanes in the plan, so the engine has nothing to know of them. */
+enum class SegueShape {
+ /** Locked: the bass changes hands at one point, over level ramps. */
+ BASS_SWAP,
+ /** Locked: bass, then middle, then top change hands one after the other. */
+ THREE_BAND,
+ /** Locked: one song, then the other, cut on a shared downbeat. */
+ CUT_ON_BEAT,
+ /** Locked: the incoming song opens from a closed filter up to the bass hand-over. */
+ FILTER_OPEN,
+ /** Fade: the outgoing song filtered away while the incoming one rises. */
+ SWEEP_OUT,
+ /** Fade: both levels equal-power over the window, the bass handed over. */
+ EVEN_FADE,
+ /** Fade: the outgoing song leaves early, for a voice in its way. */
+ QUICK_OUT,
+ /** Fade: the incoming song is up early and the outgoing one stays until late. */
+ LATE_OUT,
+}
+
/** Writes each class's treatment as lanes: every curve the engine plays is data in the plan. */
internal class SegueCompiler(private val t: SegueTuning) {
private fun knot(x: Double, value: Double?, form: KnotForm = KnotForm.Ramp) = SeguePlan.Knot(
@@ -47,8 +67,17 @@ internal class SegueCompiler(private val t: SegueTuning) {
return a * down to b * up
}
+ fun locked(shape: SegueShape, swap: Double, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean): List<SeguePlan.Lane> = when (shape) {
+ SegueShape.THREE_BAND -> threeBand(swap, beatShare, matchA, matchB, eq)
+ SegueShape.CUT_ON_BEAT -> cutOnBeat(swap, beatShare, matchA, matchB)
+ SegueShape.FILTER_OPEN -> bassSwap(swap, beatShare, matchA, matchB, eq).let { lanes ->
+ if (eq) lanes + lane(LaneSide.INCOMING, Target.SWEEP_HZ, listOf(knot(0.0, t.sweepClosedHz), knot(swap, null, KnotForm.Octave), knot(1.0, null))) else lanes
+ }
+ else -> bassSwap(swap, beatShare, matchA, matchB, eq)
+ }
+
/** Bass swapped at one point, level ramps; the top ends hand over too. */
- fun locked(swap: Double, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean): List<SeguePlan.Lane> {
+ private fun bassSwap(swap: Double, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean): List<SeguePlan.Lane> {
val out = ArrayList(levels(positions(swap)) { x ->
val after = ((x - swap) / (1 - swap)).coerceIn(0.0, 1.0)
val ga = if (x < swap) 1.0 else cos(PI / 2 * after)
@@ -64,13 +93,68 @@ internal class SegueCompiler(private val t: SegueTuning) {
return out
}
+ /** Each band leaves the outgoing song as it arrives from the incoming one, lowest first at the swap. */
+ private fun threeBand(swap: Double, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean): List<SeguePlan.Lane> {
+ val out = ArrayList(levels(positions(swap)) { x ->
+ val ga = cos(PI / 2 * ((x - swap) / (1 - swap)).coerceIn(0.0, 1.0))
+ val gb = sin(PI / 2 * (x / swap).coerceIn(0.0, 1.0))
+ matched(x, swap, matchA, matchB, ga, gb)
+ })
+ if (eq) {
+ out += bassHandOver(swap, (swap + beatShare * t.bassSwapBeats).coerceAtMost(1.0))
+ val coreFrom = swap * 0.8; val coreTo = swap + (1 - swap) * 0.5
+ out += lane(LaneSide.OUTGOING, Target.BAND_CORE, listOf(knot(0.0, 0.0), knot(coreFrom, 0.0), knot(coreTo, t.coreOutDb), knot(1.0, t.coreOutDb)))
+ out += lane(LaneSide.INCOMING, Target.BAND_CORE, listOf(knot(0.0, t.coreInDb), knot(coreFrom, t.coreInDb), knot(coreTo, 0.0), knot(1.0, 0.0)))
+ out += lane(LaneSide.OUTGOING, Target.SHELF_TOP, listOf(knot(0.0, 0.0), knot(coreTo, 0.0), knot(1.0, t.outgoingTopDb)))
+ out += lane(LaneSide.INCOMING, Target.SHELF_TOP, listOf(knot(0.0, t.topInDb), knot(swap, 0.0), knot(1.0, 0.0)))
+ }
+ return out
+ }
+
+ /** One song, then the other: the cut sits on the swap's downbeat, and the incoming song comes back to its level after. */
+ private fun cutOnBeat(swap: Double, beatShare: Double, matchA: Double, matchB: Double): List<SeguePlan.Lane> {
+ val end = (swap + beatShare * t.cutBeatShare).coerceAtMost(1.0)
+ return listOf(
+ lane(LaneSide.OUTGOING, Target.LEVEL, listOf(knot(0.0, db(matchA)), knot(swap, db(matchA)), knot(end, null), knot(1.0, null))),
+ lane(LaneSide.INCOMING, Target.LEVEL, listOf(knot(0.0, null), knot(swap, null), knot(end, db(matchB)), knot(1.0, 0.0))),
+ )
+ }
+
private fun bassHandOver(from: Double, to: Double): List<SeguePlan.Lane> = listOf(
lane(LaneSide.OUTGOING, Target.SHELF_BOTTOM, listOf(knot(0.0, 0.0), knot(from, 0.0), knot(to, t.bassKillDb), knot(1.0, t.bassKillDb))),
lane(LaneSide.INCOMING, Target.SHELF_BOTTOM, listOf(knot(0.0, t.bassKillDb), knot(from, t.bassKillDb), knot(to, 0.0), knot(1.0, 0.0))),
)
+ /** Where a fade's bass hand-over sits, 0..1 of the window. */
+ fun swapOf(shape: SegueShape): Double = when (shape) {
+ SegueShape.QUICK_OUT -> t.filteredSwapShare * 0.5
+ SegueShape.LATE_OUT -> (t.filteredSwapShare + 1) / 2
+ else -> t.filteredSwapShare
+ }
+
+ fun filtered(shape: SegueShape, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean): List<SeguePlan.Lane> {
+ val swap = swapOf(shape)
+ if (shape == SegueShape.SWEEP_OUT) return sweepOut(swap, beatShare, matchA, matchB, eq)
+ // The outgoing song leaves by here and the incoming one is up by there.
+ val (outFrom, outTo, inTo) = when (shape) {
+ SegueShape.QUICK_OUT -> Triple(0.0, swap * 2, 0.85)
+ SegueShape.LATE_OUT -> Triple(swap - 0.2, 1.0, swap)
+ else -> Triple(0.0, 1.0, 1.0)
+ }
+ val out = ArrayList(levels(positions(swap)) { x ->
+ val ga = cos(PI / 2 * ((x - outFrom) / (outTo - outFrom)).coerceIn(0.0, 1.0))
+ val gb = sin(PI / 2 * (x / inTo).coerceIn(0.0, 1.0))
+ matched(x, swap, matchA, matchB, ga, gb)
+ })
+ if (eq) {
+ out += bassHandOver(swap, (swap + beatShare * t.bassSwapBeats).coerceAtMost(1.0))
+ out += lane(LaneSide.INCOMING, Target.SHELF_TOP, listOf(knot(0.0, t.filteredIncomingTopDb), knot(swap, 0.0), knot(1.0, 0.0)))
+ }
+ return out
+ }
+
/** The outgoing song filtered away, a limited overlap, the incoming one rising without its bass. */
- fun filtered(swap: Double, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean): List<SeguePlan.Lane> {
+ private fun sweepOut(swap: Double, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean): List<SeguePlan.Lane> {
val out = ArrayList(levels(positions(swap)) { x ->
val after = ((x - swap) / (1 - swap)).coerceIn(0.0, 1.0)
val ga = if (x < swap) 1.0 else cos(PI / 2 * after)
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePolicy.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePolicy.kt
index e109bf58..135b43e6 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePolicy.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePolicy.kt
@@ -43,6 +43,8 @@ class SegueDecision(
val swapShare: Double = 0.5,
/** Seconds the incoming song takes to return to its own tempo. */
val restoreSec: Double = 0.0,
+ /** How the transition sounds, for the journal: the plan's lanes are what plays. */
+ val shape: SegueShape? = null,
) {
val why: String get() = attempts.joinToString("; ")
val fingerprint: String? get() = plan?.let(SegueCodec::fingerprint)
@@ -69,7 +71,7 @@ class SegueRequest(
* otherwise moves to the next level. Deterministic: the same request gives the same plan.
*/
object SeguePolicy {
- val ORIGIN = SeguePlan.Origin("myspoti-segue-1", "uncalibrated")
+ val ORIGIN = SeguePlan.Origin("myspoti-segue-2", "uncalibrated")
fun decide(r: SegueRequest): SegueDecision = Run(r).decide()
@@ -85,7 +87,7 @@ object SeguePolicy {
val beats = if (p.span.unitKind == UnitKind.BAR) (p.span.musicalUnits ?: 0) * 4 else p.span.musicalUnits ?: 0
return MixPlan(style, p.outgoing.anchorUs / 1e6, p.incoming.anchorUs / 1e6, p.span.lengthUs / 1e6, rate, beats,
d.swapShare, if (beats > 0) 1.0 / beats else 0.1, 1.0, d.restoreSec,
- "segue ${d.level}" + (d.score?.let { String.format(java.util.Locale.ROOT, " score %.2f", it) } ?: "") +
+ "segue ${d.level}" + (d.shape?.let { " $it" } ?: "") + (d.score?.let { String.format(java.util.Locale.ROOT, " score %.2f", it) } ?: "") +
(if (d.attempts.isEmpty()) "" else " | ${d.why}"),
filtered = d.level == SegueLevel.FILTERED, segue = p)
}
@@ -97,13 +99,13 @@ object SeguePolicy {
}
internal fun facts(a: TrackAnalysis, b: TrackAnalysis) =
- SegueSideFacts(a.audioKey, a.tailGridKey, us(a.duration)) to SegueSideFacts(b.audioKey, b.headGridKey, us(b.duration))
+ SegueSideFacts(a.audioKey, a.beatKey, us(a.duration)) to SegueSideFacts(b.audioKey, b.beatKey, us(b.duration))
private fun us(seconds: Double): Long = (seconds * 1e6).roundToLong()
private class Point(val us: Long, val bar: Int, val rank: Double, val boundary: Boolean)
- private class Found(val plan: SeguePlan, val score: Double, val swap: Double)
+ private class Found(val plan: SeguePlan, val score: Double, val swap: Double, val shape: SegueShape? = null, val restoreSec: Double = 0.0)
private class Run(val r: SegueRequest) {
val a = r.a; val b = r.b; val t = r.tuning
@@ -125,8 +127,7 @@ object SeguePolicy {
val found = attempt(level) ?: continue
val problems = SegueValidator.against(found.plan, facts(a, b).first, facts(a, b).second)
if (problems.isNotEmpty()) { attempts += SegueAttempt(level, SegueCause.INVALID_PLAN, problems.joinToString(",")); continue }
- return SegueDecision(level, found.plan, attempts.toList(), found.score, found.swap,
- if (level.cls == SegueClass.LOCKED) restore(found.plan.incoming.ratePpm, gb!!.period) else 0.0)
+ return SegueDecision(level, found.plan, attempts.toList(), found.score, found.swap, found.restoreSec, found.shape)
}
return SegueDecision(SegueLevel.NORMAL, null, attempts.toList())
}
@@ -144,7 +145,7 @@ object SeguePolicy {
private fun attempt(level: SegueLevel): Found? = when (level) {
SegueLevel.LOCKED_LONG -> locked(level, listOf(t.lockedLongBars), phrases = true)
SegueLevel.LOCKED_SHORT -> locked(level, listOf(t.lockedMiddleBars, t.lockedShortBars), phrases = true)
- SegueLevel.LOCKED_BEAT -> if (!r.eq) fail(level, SegueCause.BASS_NOT_MANAGED) else locked(level, listOf(t.lockedShortBars), phrases = false)
+ SegueLevel.LOCKED_BEAT -> if (!r.eq) fail(level, SegueCause.BASS_NOT_MANAGED) else locked(level, listOf(t.lockedShortBars, t.lockedTinyBars), phrases = false)
SegueLevel.FILTERED -> filtered(level)
SegueLevel.HARD -> hard(level)
SegueLevel.SIMPLE_FADE -> simple(level)
@@ -215,46 +216,157 @@ object SeguePolicy {
// ── Levels ──
private fun locked(level: SegueLevel, barsAsked: List<Int>, phrases: Boolean): Found? {
- if (ga == null || gb == null) return fail(level, SegueCause.NO_GRID, if (ga == null) "outgoing" else "incoming")
- val m = MixPlanner.tempoMatch(ga.bpm, gb.bpm)
- val ratePpm = (m.speed * SegueFormat.RATE_NOMINAL).roundToLong()
- val deviation = abs(ratePpm - SegueFormat.RATE_NOMINAL)
+ val allowed = barsAsked.filter { it <= (r.maxBars ?: Int.MAX_VALUE) }
+ if (allowed.isEmpty()) return fail(level, SegueCause.LENGTH_NOT_ALLOWED)
val limit = min(t.maxRateDeviationPpm, r.maxRatePpm ?: Long.MAX_VALUE)
- if (deviation > limit) return fail(level, SegueCause.TEMPO_OUT_OF_REACH, String.format(java.util.Locale.ROOT, "%.1f %%", deviation / 1e4))
- // The lower the reliability, the shorter the window.
- val rel = pulseReliability()
- val cap = if (rel >= t.reliabilityForLong) t.lockedLongBars else if (rel >= t.reliabilityForMiddle) t.lockedMiddleBars else t.lockedShortBars
- val allowed = barsAsked.filter { it <= cap && it <= (r.maxBars ?: Int.MAX_VALUE) && (m.beatsPerB == 1.0 || it <= t.halfDoubleMaxBars) }
- if (allowed.isEmpty()) return fail(level, SegueCause.LENGTH_NOT_ALLOWED, "reliability ${"%.2f".format(java.util.Locale.ROOT, rel)}")
- val barA = 4 * ga.period
- val phaseMedian = phase(a.tailPhase?.medianUs, b.headPhase?.medianUs)
- val phaseP95 = phase(a.tailPhase?.p95Us, b.headPhase?.p95Us)
- val drift = hypot(ga.driftMs, gb.driftMs) / 1000 / ga.period
val tally = Tally()
var best: Found? = null
+ var closestPpm = Long.MAX_VALUE
+ var tooLong = false
+ var stretches = false
for (bars in allowed) {
- val lengthUs = us(bars * barA)
- val spanUs = lengthUs * ratePpm / SegueFormat.RATE_NOMINAL
- val outs = exits(gridTimes(ga, a.tailDown, max(a.contentStart, ga.first), min(sa.musicEnd, ga.last)), lengthUs, min(us(a.contentEnd), us(ga.last + ga.period)), phrases)
- val ins = entries(gb, spanUs, phrases)
- for (e in outs) for (n in ins) {
- tally.seen++
- val base = measures(e, n, lengthUs, spanUs).copy(rateDeviationPpm = deviation, phaseMedianUs = phaseMedian, phaseP95Us = phaseP95,
- driftBeats = drift, gridReliable = reliable(ga) && reliable(gb))
- if (tally.refused(SegueGuards.crossed(SegueClass.LOCKED, base, t))) continue
- val swap = MixPlanner.bassSwapBar(b, sb, gb, n.us / 1e6, bars, barA, m.speed) / bars
- val beatShare = 1.0 / (bars * 4)
- val lanes = compiler.locked(swap, beatShare, matchA(), matchB(), r.eq)
- val plan = plan(e.us, n.us, ga, gb, ratePpm, lengthUs, bars, PulseState.LOCKED, us60(ga), phaseMedian, lanes)
- val score = score(e, n, lengthUs, spanUs, phaseMedian, deviation, lanes)
- if (best == null || score.total > best.score) best = Found(plan, score.total, swap)
+ val outs = stretchExits(bars, phrases, tally)
+ stretches = stretches || outs.isNotEmpty()
+ // The incoming bars of the same length of time: as many, or half or twice as many between half and double tempos.
+ for ((barsB, m) in listOf(bars to 1.0, bars / 2 to 2.0, bars * 2 to 0.5)) {
+ if (m != 1.0 && (bars > t.halfDoubleMaxBars || barsB < t.lockedTinyBars || barsB * 2 != bars * (if (m == 2.0) 1 else 4))) continue
+ val ins = stretchEntries(barsB, phrases, tally)
+ for (e in outs) for (n in ins) {
+ val rate = n.stretch.lengthSec / e.stretch.lengthSec
+ val ratePpm = (rate * SegueFormat.RATE_NOMINAL).roundToLong()
+ val deviation = abs(ratePpm - SegueFormat.RATE_NOMINAL)
+ closestPpm = min(closestPpm, deviation)
+ if (deviation > limit) continue
+ // The lower the reliability, the shorter the window.
+ val pulse = min(e.stretch.reliability, n.stretch.reliability)
+ val cap = if (pulse >= t.reliabilityForLong) t.lockedLongBars else if (pulse >= t.reliabilityForMiddle) t.lockedMiddleBars else t.lockedShortBars
+ if (bars > cap) { tooLong = true; continue }
+ tally.seen++
+ val lengthUs = us(e.stretch.lengthSec)
+ val spanUs = us(n.stretch.lengthSec)
+ val (phaseMedian, phaseP95) = Stretches.phase(e.stretch, n.stretch, rate) ?: (null to null)
+ val ga = e.stretch.grid; val gb = n.stretch.grid
+ val base = measures(e.point, n.point, lengthUs, spanUs).copy(rateDeviationPpm = deviation, phaseMedianUs = phaseMedian, phaseP95Us = phaseP95,
+ driftBeats = hypot(ga.driftMs, gb.driftMs) / 1000 / ga.period)
+ if (tally.refused(SegueGuards.crossed(SegueClass.LOCKED, base, t))) continue
+ val swap = MixPlanner.bassSwapBar(b, sb, gb, n.stretch.start, bars, 4 * ga.period, rate, gb.first) / bars
+ val beatShare = 1.0 / (bars * 4)
+ val shape = lockedShape(e.point, n.point, bars, barsB, swap, lengthUs, spanUs)
+ val lanes = compiler.locked(shape, swap, beatShare, matchA(), matchB(), r.eq)
+ val plan = plan(e.stretch.startUs, n.stretch.startUs, ga, gb, ratePpm, lengthUs, bars, PulseState.LOCKED, us60(ga), phaseMedian, lanes)
+ val score = score(e.point, n.point, lengthUs, spanUs, phaseMedian, deviation, lanes, pulse, bars, barsB)
+ if (best == null || score.total > best.score) best = Found(plan, score.total, swap, shape, restore(ratePpm, gb.period))
+ }
}
// The longest length that works: shorter only when it has to be.
if (best != null && best.score >= t.thresholdLocked) break
}
+ if (tally.seen == 0) return when {
+ !stretches -> fail(level, SegueCause.NO_GRID, tally.toString())
+ tooLong -> fail(level, SegueCause.LENGTH_NOT_ALLOWED, "reliability")
+ closestPpm == Long.MAX_VALUE -> fail(level, SegueCause.NO_CANDIDATE, tally.toString())
+ else -> fail(level, SegueCause.TEMPO_OUT_OF_REACH, String.format(java.util.Locale.ROOT, "%.1f %%", closestPpm / 1e4))
+ }
return settle(level, tally, best, t.thresholdLocked)
}
+ /** A stretch and its place among the candidates. */
+ private inner class Placed(val stretch: Stretch, val point: Point)
+
+ private val outgoingStretches = HashMap<Long, Stretch?>()
+ private val incomingStretches = HashMap<Long, Stretch?>()
+
+ private fun cached(cache: HashMap<Long, Stretch?>, s: TrackAnalysis, bar: Int, bars: Int, metre: Double): Stretch? =
+ cache.getOrPut(bar.toLong() * 1000 + bars) { Stretches.measure(s, bar, bars, metre, t) }
+
+ /** A's stretches where a window fits, instrumental endings first. */
+ private fun stretchExits(bars: Int, phrases: Boolean, tally: Tally): List<Placed> {
+ val zone = max(zoneStartUs, r.earliestExitUs)
+ val limit = us(a.contentEnd)
+ val out = ArrayList<Placed>()
+ for (bar in 0 until a.bars) {
+ val start = us(a.barStarts[bar])
+ if (start < zone - 50_000) continue
+ val on = boundary(a, bar)
+ if (phrases && !on) continue
+ val s = cached(outgoingStretches, a, bar, bars, a.tailMetre.toDouble())
+ if (s == null) { if (bar + bars < a.bars) tally.refused(listOf(SegueGuard.GRID_RELIABILITY)); continue }
+ if (s.startUs < zone || us(s.end) > limit) continue
+ out += Placed(s, Point(s.startUs, bar, instrumentalAfter(bar) + s.startUs.toDouble() / max(1L, limit) * 1e-3, on))
+ }
+ return out.sortedByDescending { it.point.rank }.take(t.candidatesPerSide)
+ }
+
+ /** B's stretches from its useful attack; leaving out a start must be worth its note. */
+ private fun stretchEntries(bars: Int, phrases: Boolean, tally: Tally): List<Placed> {
+ val startUs = us(b.contentStart)
+ val zone = min(startUs + t.entryZoneUs, us(b.contentEnd) / 3)
+ val firstBar = (0 until b.bars).firstOrNull { b.barStarts[it] >= b.contentStart - 0.08 } ?: return emptyList()
+ val out = ArrayList<Placed>()
+ for (bar in firstBar until b.bars) {
+ val at = us(b.barStarts[bar])
+ if (bar != firstBar && at >= zone) break
+ val on = boundary(b, bar)
+ if (phrases && !on && bar != firstBar) continue
+ val s = cached(incomingStretches, b, bar, bars, b.headMetre.toDouble())
+ if (s == null) { if (bar + bars < b.bars) tally.refused(listOf(SegueGuard.GRID_RELIABILITY)); continue }
+ val rank = if (bar == firstBar) 1.0
+ else (if (b.sectionStrength(bar) > 0) t.entryNoteSection else t.entryNoteOther) * (1 - (at - startUs).toDouble() / t.entryZoneUs)
+ out += Placed(s, Point(s.startUs, bar, rank, on || bar == firstBar))
+ }
+ return out.sortedByDescending { it.point.rank }.take(t.candidatesPerSide)
+ }
+
+ // ── Shapes: what the music asks for first, then a choice that stays the same for the same pair ──
+ private fun seeded(options: List<SegueShape>, exitUs: Long, entryUs: Long): SegueShape {
+ var h = -3750763034362895579L
+ for (c in "${a.audioKey}|${b.audioKey}|$exitUs|$entryUs") { h = (h xor c.code.toLong()) * 1099511628211L }
+ return options[Math.floorMod(h, options.size.toLong()).toInt()]
+ }
+
+ private fun lockedShape(e: Point, n: Point, bars: Int, barsB: Int, swap: Double, lengthUs: Long, spanUs: Long): SegueShape {
+ // A cut is only as good as what is on both sides of it: a much louder or quieter song is heard as a jump.
+ val barA = e.bar + (bars * swap).toInt(); val barB = n.bar + (barsB * swap).toInt()
+ val cuts = abs(sa.levelBars(max(0, barA - 2), barA) - sb.levelBars(barB, barB + 2)) <= t.loudnessJumpMaxDb
+ if (!cuts) return seeded(if (r.eq && bars > t.lockedTinyBars) listOf(SegueShape.BASS_SWAP, SegueShape.THREE_BAND) else listOf(SegueShape.BASS_SWAP), e.us, n.us)
+ if (!r.eq) return seeded(listOf(SegueShape.BASS_SWAP, SegueShape.CUT_ON_BEAT), e.us, n.us)
+ // Two voices in the window: they must not meet, so one takes over from the other.
+ val overlap = sa.maxVoice != 0f && sb.maxVoice != 0f &&
+ MixPlanner.coVocal(a, e.us / 1e6, (e.us + lengthUs) / 1e6, b, n.us / 1e6, (n.us + spanUs) / 1e6).total > 0
+ if (overlap && bars <= t.lockedMiddleBars) return SegueShape.CUT_ON_BEAT
+ return seeded(when {
+ bars <= t.lockedTinyBars -> listOf(SegueShape.CUT_ON_BEAT, SegueShape.BASS_SWAP)
+ bars <= t.lockedShortBars -> listOf(SegueShape.BASS_SWAP, SegueShape.CUT_ON_BEAT, SegueShape.FILTER_OPEN)
+ bars <= t.lockedMiddleBars -> listOf(SegueShape.BASS_SWAP, SegueShape.THREE_BAND, SegueShape.CUT_ON_BEAT, SegueShape.FILTER_OPEN)
+ else -> listOf(SegueShape.BASS_SWAP, SegueShape.THREE_BAND, SegueShape.FILTER_OPEN)
+ }, e.us, n.us)
+ }
+
+ private fun filteredShape(e: Point, n: Point, lengthUs: Long): SegueShape {
+ val all = listOf(SegueShape.SWEEP_OUT, SegueShape.EVEN_FADE, SegueShape.QUICK_OUT, SegueShape.LATE_OUT)
+ // A voice still there late in the window leaves early; one coming early in the incoming song gets in clear.
+ val endBar = a.barAt((e.us + lengthUs) / 1e6)
+ val lateVoice = ((e.bar + endBar + 1) / 2..endBar).any { a.barVocalRisk.getOrElse(it) { 0f } >= t.voiceBarRisk }
+ val earlyVoice = (n.bar until b.barAt((n.us + lengthUs / 2) / 1e6) + 1).any { b.barVocalRisk.getOrElse(it) { 0f } >= t.voiceBarRisk }
+ return when {
+ lateVoice -> SegueShape.QUICK_OUT
+ earlyVoice -> SegueShape.LATE_OUT
+ else -> seeded(all, e.us, n.us)
+ }
+ }
+
+ /** The lengths to try: the longest that stays clear of both voices, then the others from the longest. */
+ private fun fadeLengths(lengths: List<Long>): List<Long> {
+ if (lengths.size < 2 || sa.maxVoice == 0f || sb.maxVoice == 0f) return lengths
+ val lastA = (a.bars - 1 downTo 0).firstOrNull { a.barVocalRisk.getOrElse(it) { 0f } >= t.voiceBarRisk }
+ val firstB = (0 until b.bars).firstOrNull { b.barVocalRisk.getOrElse(it) { 0f } >= t.voiceBarRisk }
+ val freeA = lastA?.let { us(sa.musicEnd) - us(a.barEnd(it)) } ?: Long.MAX_VALUE
+ val freeB = firstB?.let { us(b.barStarts[it]) - us(b.contentStart) } ?: Long.MAX_VALUE
+ val free = max(0L, min(freeA, freeB))
+ val fit = lengths.filter { it <= free }.maxOrNull() ?: lengths.min()
+ return listOf(fit) + lengths.filter { it != fit }
+ }
+
private fun filtered(level: SegueLevel): Found? {
val rel = tempoReliability(ga)
val lengths = when {
@@ -264,16 +376,17 @@ object SeguePolicy {
}
val tally = Tally()
var best: Found? = null
- for (lengthUs in lengths) {
+ for (lengthUs in fadeLengths(lengths)) {
val outs = exits(a.barStarts.asSequence(), lengthUs, us(a.contentEnd), phrases = false)
val ins = entries(gb, lengthUs, phrases = false)
for (e in outs) for (n in ins) {
tally.seen++
if (tally.refused(SegueGuards.crossed(SegueClass.FILTERED, measures(e, n, lengthUs, lengthUs), t))) continue
- val lanes = compiler.filtered(t.filteredSwapShare, min(1.0, 0.4 * 1e6 / lengthUs), matchA(), matchB(), r.eq)
+ val shape = filteredShape(e, n, lengthUs)
+ val lanes = compiler.filtered(shape, min(1.0, 0.4 * 1e6 / lengthUs), matchA(), matchB(), r.eq)
val plan = plan(e.us, n.us, ga, gb, SegueFormat.RATE_NOMINAL, lengthUs, null, PulseState.FREE, null, null, lanes)
val score = score(e, n, lengthUs, lengthUs, null, 0, lanes)
- if (best == null || score.total > best.score) best = Found(plan, score.total, t.filteredSwapShare)
+ if (best == null || score.total > best.score) best = Found(plan, score.total, compiler.swapOf(shape), shape)
}
if (best != null && best.score >= t.thresholdFiltered) break
}
@@ -346,14 +459,15 @@ object SeguePolicy {
contentCutShare = if (musicUs > 0) cutUs.toDouble() / musicUs else 1.0, playedUs = e.us - us(a.contentStart))
}
- private fun score(e: Point, n: Point, lengthUs: Long, spanUs: Long, phaseUs: Long?, deviationPpm: Long, lanes: List<SeguePlan.Lane>): SegueScore {
+ private fun score(e: Point, n: Point, lengthUs: Long, spanUs: Long, phaseUs: Long?, deviationPpm: Long, lanes: List<SeguePlan.Lane>,
+ pulse: Double = pulseReliability(), barsA: Int? = null, barsB: Int? = null): SegueScore {
val a0 = e.us / 1e6; val a1 = (e.us + lengthUs) / 1e6; val b0 = n.us / 1e6; val b1 = (n.us + spanUs) / 1e6
val endBar = a.barAt(a1)
val energy = abs(sa.levelBars(max(0, endBar - 3), endBar + 1) - sb.levelBars(max(0, n.bar), max(0, n.bar) + 4))
val rhythm = phaseUs?.let { SegueScore.fallingFrom(it.toDouble(), t.rhythmFullUs.toDouble(), t.phaseMedianMaxUs.toDouble()) } ?: 0.0
- val phrase = (if (e.boundary) 0.5 else 0.0) + if (n.boundary) 0.5 else 0.0
+ val phrase = phraseNote(a, e, barsA) + phraseNote(b, n, barsB)
val notes = listOf(
- SegueNote("rhythm", t.weightRhythm, pulseReliability(), rhythm),
+ SegueNote("rhythm", t.weightRhythm, pulse, rhythm),
SegueNote("phrase", t.weightPhrase, min(structureReliability(a, e.bar), structureReliability(b, n.bar)), phrase),
SegueNote("energy", t.weightEnergy, 1.0, SegueScore.gaussian(energy, t.energySigmaDb)),
SegueNote("exit", t.weightExit, vocalReliability(), exitQuality(e, endBar)),
@@ -365,6 +479,7 @@ object SeguePolicy {
val defects = listOf(
SegueDefect("speed", t.penaltySpeed, SegueScore.risingFrom(deviationPpm.toDouble(), t.rateCostFreePpm.toDouble(), t.maxRateDeviationPpm.toDouble())),
SegueDefect("voice", t.penaltyVoice, overlap * lengthUs / t.voiceOverlapMaxUs),
+ SegueDefect("voice cut", t.penaltyVoiceCut, voiceCut(endBar, n.bar)),
SegueDefect("bass", t.penaltyBass, commonBass(e.us, n.us, lengthUs, spanUs, lanes)),
SegueDefect("lost", t.penaltyLost, max(0L, us(sa.musicEnd) - (e.us + lengthUs)).toDouble() / t.contentCutMaxUs),
SegueDefect("sobriety", t.penaltySobriety, effects.toDouble() / t.sobrietyFullEffects),
@@ -372,6 +487,25 @@ object SeguePolicy {
return SegueScore(notes, defects)
}
+ /** A boundary at the start of the window, or less at its middle or end, where the mix's hand-over can land. */
+ private fun phraseNote(s: TrackAnalysis, p: Point, bars: Int?): Double = when {
+ p.boundary -> 0.5
+ bars != null && p.bar >= 0 && (s.sectionStrength(p.bar + bars / 2) > 0 || s.sectionStrength(p.bar + bars) > 0) -> t.phraseNoteInside
+ else -> 0.0
+ }
+
+ /** The share of each song's voice nobody hears: A's after the window, B's before the entry. */
+ private fun voiceCut(endBar: Int, entryBar: Int): Double {
+ fun share(x: FloatArray, range: IntRange): Double {
+ val total = x.sumOf { it.toDouble() }
+ if (total <= 0) return 0.0
+ var part = 0.0
+ for (i in range) part += x.getOrElse(i) { 0f }
+ return part / total
+ }
+ return share(a.barVocalRisk, (endBar + 1) until a.bars) + share(b.barVocalRisk, 0 until entryBar)
+ }
+
/** The voice is over, or dies early in the window: leaving on it is what the listener hears. */
private fun exitQuality(e: Point, endBar: Int): Double {
if (e.bar < 0 || endBar < e.bar) return 0.0
@@ -403,8 +537,8 @@ object SeguePolicy {
private fun plan(exitUs: Long, entryUs: Long, g: Grid?, h: Grid?, ratePpm: Long, lengthUs: Long, bars: Int?, state: PulseState, tempo: Long?, residual: Long?, lanes: List<SeguePlan.Lane>) =
SeguePlan(ORIGIN,
- SeguePlan.Side(a.audioKey, if (g != null) a.tailGridKey else null, exitUs, SegueFormat.RATE_NOMINAL, r.listenerRate),
- SeguePlan.Side(b.audioKey, if (h != null) b.headGridKey else null, entryUs, ratePpm, r.listenerRate),
+ SeguePlan.Side(a.audioKey, if (g != null) a.beatKey else null, exitUs, SegueFormat.RATE_NOMINAL, r.listenerRate),
+ SeguePlan.Side(b.audioKey, if (h != null) b.beatKey else null, entryUs, ratePpm, r.listenerRate),
SeguePlan.Span(lengthUs, bars, UnitKind.BAR, Clock.ELAPSED),
SeguePlan.Pulse(state, tempo, residual), lanes)
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueStretch.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueStretch.kt
new file mode 100644
index 00000000..44cc2a82
--- /dev/null
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueStretch.kt
@@ -0,0 +1,103 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+package app.myspoti.automix.segue
+
+import app.myspoti.automix.BeatTracker
+import app.myspoti.automix.Grid
+import app.myspoti.automix.TrackAnalysis
+import kotlin.math.abs
+import kotlin.math.max
+import kotlin.math.min
+import kotlin.math.roundToInt
+import kotlin.math.roundToLong
+
+/**
+ * A run of whole bars judged on its own beats. A song whose ending as a whole holds no single grid
+ * (a tempo that wanders, a bridge without drums) can still have stretches that do, and a mix only
+ * needs one. [grid] is fitted on these beats alone: beat 0 on the first bar's start.
+ */
+internal class Stretch(
+ val bar: Int,
+ val bars: Int,
+ val grid: Grid,
+ /** The beats measured inside, seconds. */
+ val beats: DoubleArray,
+ /** How far it can be trusted, 0..1: the worst of its beats' share and its downbeats' confidence. */
+ val reliability: Double,
+) {
+ val start get() = grid.first
+ val end get() = grid.first + grid.period * 4 * bars
+ val startUs get() = (start * 1e6).roundToLong()
+ val lengthSec get() = end - start
+}
+
+internal object Stretches {
+ private fun lowerBound(x: DoubleArray, value: Double): Int {
+ var lo = 0; var hi = x.size
+ while (lo < hi) { val m = (lo + hi) ushr 1; if (x[m] < value) lo = m + 1 else hi = m }
+ return lo
+ }
+
+ /**
+ * The stretch of [bars] bars from [bar], or null when its beats cannot be relied on. [metre] stands
+ * in for the downbeats' confidence on an analysis that has none.
+ */
+ fun measure(s: TrackAnalysis, bar: Int, bars: Int, metre: Double, t: SegueTuning): Stretch? {
+ if (bar < 0 || bar + bars >= s.bars) return null
+ val from = s.barStarts[bar]; val to = s.barStarts[bar + bars]
+ val beatCount = 4 * bars
+ val rough = (to - from) / beatCount
+ if (rough <= 0 || 60 / rough !in BeatTracker.MIN_BPM..BeatTracker.MAX_BPM) return null
+ val source = if (s.modelBeats.isNotEmpty()) s.modelBeats else s.beats
+ val i0 = lowerBound(source, from - rough / 4); val i1 = lowerBound(source, to - rough / 4)
+ val n = i1 - i0
+ if (n < t.stretchMinBeatShare * beatCount || n > beatCount + 1 || n < 2) return null
+ val beats = source.copyOfRange(i0, i1)
+ // A straight line through the beats by their place in the stretch.
+ var sk = 0.0; var st = 0.0; var skk = 0.0; var skt = 0.0
+ val ks = IntArray(n) { ((beats[it] - from) / rough).roundToInt() }
+ for (i in 0 until n) { val k = ks[i].toDouble(); sk += k; st += beats[i]; skk += k * k; skt += k * beats[i] }
+ val den = n * skk - sk * sk
+ if (den <= 0) return null
+ val period = (n * skt - sk * st) / den
+ val first = (st - period * sk) / n
+ if (period <= 0 || abs(period / rough - 1) > 0.05) return null
+ val gaps = DoubleArray(n) { abs(beats[it] - (first + period * ks[it])) }.also { it.sort() }
+ if (gaps[((n - 1) * 0.95).toInt()] * 1e6 > t.phaseP95MaxUs) return null
+ // The worst slide of a bar's average off the line.
+ var drift = 0.0
+ var j = 0
+ while (j < n) {
+ val e = min(n, j + 4); var sum = 0.0
+ for (i in j until e) sum += beats[i] - (first + period * ks[i])
+ drift = max(drift, abs(sum / (e - j)))
+ j = e
+ }
+ if (drift * 1000 > t.gridMaxDriftMs) return null
+ val down = if (s.barDownConfidence.isEmpty()) metre else {
+ var sum = 0.0
+ for (b in bar until bar + bars) sum += s.barDownConfidence.getOrElse(b) { 0f }
+ sum / bars
+ }
+ if (down < t.stretchMinDownConfidence) return null
+ val share = min(1.0, n.toDouble() / beatCount)
+ return Stretch(bar, bars, Grid(first, period, beatCount, share, drift * 1000, 1.0), beats, min(share, down))
+ }
+
+ /**
+ * How far apart the two stretches' beats fall once the incoming one plays at [rate] from its start
+ * on the outgoing one's start: the sparser beats against the nearest of the denser. Median and
+ * 95th percentile, microseconds; null with too few to tell.
+ */
+ fun phase(a: Stretch, b: Stretch, rate: Double): Pair<Long, Long>? {
+ // Everything on the outgoing song's time, from the start of the window.
+ val xa = DoubleArray(a.beats.size) { a.beats[it] - a.start }
+ val xb = DoubleArray(b.beats.size) { (b.beats[it] - b.start) / rate }
+ val (sparse, dense) = if (xa.size <= xb.size) xa to xb else xb to xa
+ if (sparse.size < 2) return null
+ val gaps = DoubleArray(sparse.size) { i ->
+ val at = lowerBound(dense, sparse[i])
+ min(if (at < dense.size) dense[at] - sparse[i] else Double.MAX_VALUE, if (at > 0) sparse[i] - dense[at - 1] else Double.MAX_VALUE)
+ }.also { it.sort() }
+ return (gaps[gaps.size / 2] * 1e6).roundToLong() to (gaps[((gaps.size - 1) * 0.95).toInt()] * 1e6).roundToLong()
+ }
+}
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueTuning.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueTuning.kt
index 61b867db..19859653 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueTuning.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueTuning.kt
@@ -10,8 +10,8 @@ package app.myspoti.automix.segue
*/
data class SegueTuning(
// ── Guards: one crossing rejects a candidate, whatever its score ──
- /** Largest speed change in the locked class, ppm. [U] range 4–8 %; a semitone is about 6 %. */
- val maxRateDeviationPpm: Long = 40_000,
+ /** Largest speed change in the locked class, ppm. [U] range 4–8 %, the top of it: the stretch keeps the pitch, and the bench lost most pairs to this guard. */
+ val maxRateDeviationPpm: Long = 80_000,
/** Expected phase error: median [U], against double attacks. */
val phaseMedianMaxUs: Long = 20_000,
/** Expected phase error: 95th percentile [U]. */
@@ -22,6 +22,10 @@ data class SegueTuning(
val gridMinBeats: Int = 16,
val gridMinSteadiness: Double = 0.6,
val gridMaxDriftMs: Double = 30.0,
+ /** A stretch of bars must have this share of its beats from the model [I]. */
+ val stretchMinBeatShare: Double = 0.85,
+ /** And the model must be this sure of its downbeats, on average [I]. */
+ val stretchMinDownConfidence: Double = 0.5,
/** Two voices at comparable levels, tolerated total [I] range 1–2 s. */
val voiceOverlapMaxUs: Long = 1_000_000,
/** Content removed at the end of a song: share of it [I] range 20–30 %, and a cap [I]. */
@@ -40,6 +44,8 @@ data class SegueTuning(
val penaltySpeed: Double = 0.5,
val weightEnergy: Double = 0.5,
val penaltyVoice: Double = 1.0,
+ /** Voice left unheard: after the window on the way out, before the entry on the way in [I]. */
+ val penaltyVoiceCut: Double = 0.5,
val weightExit: Double = 0.5,
val weightEntry: Double = 0.5,
val penaltyBass: Double = 0.5,
@@ -57,6 +63,10 @@ data class SegueTuning(
/** Entry note on a later section, and anywhere else [I]; the first attack scores 1. */
val entryNoteSection: Double = 0.6,
val entryNoteOther: Double = 0.3,
+ /** Phrase note per side for a boundary at the middle or end of a stretch, against 0.5 at its start [I]. */
+ val phraseNoteInside: Double = 0.25,
+ /** Per-bar voice-like risk from which a bar counts as sung or rapped [R]. */
+ val voiceBarRisk: Double = 0.6,
/** Effects at which the sobriety defect is full [I]. */
val sobrietyFullEffects: Int = 3,
/** Points sampled along the window for the common bass energy [I]. */
@@ -85,6 +95,7 @@ data class SegueTuning(
val lockedLongBars: Int = 16,
val lockedMiddleBars: Int = 8,
val lockedShortBars: Int = 4,
+ val lockedTinyBars: Int = 2,
/** Half or double time: two grooves meeting every other beat, kept short [R]. */
val halfDoubleMaxBars: Int = 8,
/** Filtered fade lengths [U] range 4–12 s; the middle one is [I]. */
@@ -121,6 +132,13 @@ data class SegueTuning(
/** Filtered: the outgoing song's filter closes to this cutoff [I] with this accent [I]. */
val sweepClosedHz: Double = 400.0,
val sweepPeakRatio: Double = 1.2,
+ /** Middle band handed over in a three-band shape: outgoing down to, incoming up from [I]. */
+ val coreOutDb: Double = -24.0,
+ val coreInDb: Double = -12.0,
+ /** Top band of the incoming song held back in a three-band shape [I]. */
+ val topInDb: Double = -12.0,
+ /** A cut on the beat lasts this share of a beat [I]: quick, without a click. */
+ val cutBeatShare: Double = 0.25,
/** Knots per equal-power curve [I]: enough that the steps between them are not heard. */
val curveKnots: Int = 16,
/** Loudness gap left alone, and the largest one corrected, dB [R]. */
diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBench.kt b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBench.kt
index 78bc1c5b..f8ab4e5a 100644
--- a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBench.kt
+++ b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBench.kt
@@ -86,7 +86,7 @@ object SegueBench {
val causes = HashMap<String, Int>(); val refused = HashMap<String, Int>()
val ms = ArrayList<Double>(); var unstable = 0; var invalid = 0
val lengths = ArrayList<Double>(); val cuts = ArrayList<Double>()
- println("pair\tlevel\tseconds\texit\tentry\trate\tscore\tms\twhy")
+ println("pair\tlevel\tseconds\texit\tentry\trate\tscore\tms\tshape\twhy")
for ((x, y) in pairs(names, null)) {
val a = songs.getValue(x); val b = songs.getValue(y)
val t0 = System.nanoTime()
@@ -104,8 +104,8 @@ object SegueBench {
cuts += max(0.0, app.myspoti.automix.MixPlanner.musicEnd(a) - (p.outgoing.anchorUs + p.span.lengthUs) / 1e6)
}
val p = d.plan
- println(String.format(L, "%s,%s\t%s\t%.3f\t%.3f\t%.3f\t%d\t%s\t%.2f\t%s", x, y, d.level, (p?.span?.lengthUs ?: 0) / 1e6, (p?.outgoing?.anchorUs ?: 0) / 1e6,
- (p?.incoming?.anchorUs ?: 0) / 1e6, p?.incoming?.ratePpm ?: 0, d.score?.let { String.format(L, "%.3f", it) } ?: "-", ms.last(), d.why))
+ println(String.format(L, "%s,%s\t%s\t%.3f\t%.3f\t%.3f\t%d\t%s\t%.2f\t%s\t%s", x, y, d.level, (p?.span?.lengthUs ?: 0) / 1e6, (p?.outgoing?.anchorUs ?: 0) / 1e6,
+ (p?.incoming?.anchorUs ?: 0) / 1e6, p?.incoming?.ratePpm ?: 0, d.score?.let { String.format(L, "%.3f", it) } ?: "-", ms.last(), d.shape ?: "-", d.why))
}
ms.sort(); lengths.sort(); cuts.sort()
println("summary\tpairs=${ms.size}\t" + levels.entries.joinToString("\t") { "${it.key}=${it.value}" } + "\tnot_reproducible=$unstable\tinvalid=$invalid")
diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBenchTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBenchTest.kt
index 776acecd..be7c4656 100644
--- a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBenchTest.kt
+++ b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueBenchTest.kt
@@ -97,8 +97,8 @@ class SegueBenchTest {
val all = SegueCorpus.variations(base)
assertTrue(all.size > 80)
val v = all.first { it.first == "maxRateDeviationPpm\t1.2" }.second
- assertEquals(48_000L, v.maxRateDeviationPpm)
- assertEquals(base.copy(maxRateDeviationPpm = 48_000), v)
+ assertEquals(96_000L, v.maxRateDeviationPpm)
+ assertEquals(base.copy(maxRateDeviationPpm = 96_000), v)
assertNotNull(all.first { it.first == "energySigmaDb\t0.8" })
for ((_, t) in all) assertNotEquals(base, t)
}
diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SeguePolicyTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SeguePolicyTest.kt
index 30cb3b1f..17467d29 100644
--- a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SeguePolicyTest.kt
+++ b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SeguePolicyTest.kt
@@ -35,15 +35,17 @@ class SeguePolicyTest {
barDb: Float = -10f,
down: Double = 0.0,
risk: (Int) -> Float = { 0f },
+ modelBeats: DoubleArray? = null,
): TrackAnalysis {
val beat = 60 / bpm
- val n = if (bars) (duration / 2).toInt() else 0
+ val n = if (bars) (duration / (4 * beat)).toInt() else 0
val g = Grid(0.0, beat, (duration / beat).toInt(), steadiness, 0.0, 1.0)
return TrackAnalysis(duration, contentStart, contentEnd, loudness, musicKey, 1.0, bpm, grid,
- if (grid) g else null, if (grid) tail ?: g else null, DoubleArray(0), DoubleArray(n) { it * 2.0 },
+ if (grid) g else null, if (grid) tail ?: g else null, if (grid && bars) DoubleArray(g.count) { g.beat(it) } else DoubleArray(0), DoubleArray(n) { it * 4 * beat },
FloatArray(n) { barDb }, FloatArray(n) { -16f }, FloatArray(n) { -20f }, FloatArray(n), FloatArray(n), 0, stereo,
if (grid) down else Double.NaN, if (grid) tail?.first ?: 0.0 else Double.NaN, barVocalRisk = FloatArray(n, risk),
- audioKey = key, barBassDb = FloatArray(n) { -18f }, headPhase = phase, tailPhase = phase, headMetre = metre, tailMetre = metre)
+ audioKey = key, barBassDb = FloatArray(n) { -18f }, headPhase = phase, tailPhase = phase, headMetre = metre, tailMetre = metre,
+ modelBeats = modelBeats ?: DoubleArray(0))
}
private fun decide(a: TrackAnalysis, b: TrackAnalysis, earliest: Long = 0, sameAlbum: Boolean = false, eq: Boolean = true, tuning: SegueTuning = SegueTuning()) =
@@ -137,18 +139,16 @@ class SeguePolicyTest {
}
@Test fun level3LockedOnADownbeatWhenNoPhraseBoundaryFits() {
- // Phrase boundaries fall at 176 s and 192 s; this checked grid lies strictly between them.
- val tail = Grid(178.0, 0.5, 26, 1.0, 0.0, 1.0)
- val d = decide(song("a", duration = 200.0, tail = tail), song("b"))
+ // Phrase boundaries fall at 176 s and 192 s; the only beats to rely on lie strictly between them.
+ val d = decide(song("a", duration = 200.0, modelBeats = DoubleArray(26) { 178.0 + it * 0.5 }), song("b"))
assertEquals(d.why, SegueLevel.LOCKED_BEAT, d.level)
- assertCause(d, SegueLevel.LOCKED_LONG, SegueCause.NO_CANDIDATE)
- assertCause(d, SegueLevel.LOCKED_SHORT, SegueCause.NO_CANDIDATE)
+ assertCause(d, SegueLevel.LOCKED_LONG, SegueCause.NO_GRID)
+ assertCause(d, SegueLevel.LOCKED_SHORT, SegueCause.NO_GRID)
assertEquals(t.lockedShortBars, d.plan!!.span.musicalUnits)
}
@Test fun level3NeedsTheBassManaged() {
- val tail = Grid(178.0, 0.5, 26, 1.0, 0.0, 1.0)
- val d = decide(song("a", duration = 200.0, tail = tail), song("b"), eq = false)
+ val d = decide(song("a", duration = 200.0, modelBeats = DoubleArray(26) { 178.0 + it * 0.5 }), song("b"), eq = false)
assertCause(d, SegueLevel.LOCKED_BEAT, SegueCause.BASS_NOT_MANAGED)
}
@@ -160,7 +160,7 @@ class SeguePolicyTest {
assertEquals(PulseState.FREE, p.pulse.state)
assertEquals(SegueFormat.RATE_NOMINAL, p.incoming.ratePpm)
assertTrue(p.span.lengthUs in t.filteredShortUs..t.filteredLongUs)
- assertTrue(p.lanes.any { it.target == Target.SWEEP_HZ && it.side == LaneSide.OUTGOING })
+ assertTrue(p.lanes.any { it.target == Target.LEVEL && it.side == LaneSide.OUTGOING })
}
@Test fun level5HardWhenAnyOverlapWouldHaveTwoVoices() {
@@ -227,10 +227,10 @@ class SeguePolicyTest {
}
@Test fun anEntryIsNeverBeforeTheSongsStart() {
- // The grid's downbeats fall at -0.05 s, 1.95 s...: the first one in the song is the one to use.
- val d = decide(song("a"), song("b", down = 1.95))
+ // The bars start at 0 s, 2 s...: the first one once the song has begun is the one to use.
+ val d = decide(song("a"), song("b", contentStart = 1.95))
assertEquals(d.why, SegueLevel.LOCKED_LONG, d.level)
- assertEquals(1_950_000L, d.plan!!.incoming.anchorUs)
+ assertEquals(2_000_000L, d.plan!!.incoming.anchorUs)
val far = decide(song("a"), song("b", bpm = 100.0, down = 2.35))
assertEquals(far.why, SegueLevel.FILTERED, far.level)
assertTrue(far.plan!!.incoming.anchorUs >= 0)
@@ -337,7 +337,7 @@ class SeguePolicyTest {
assertEquals(emptyList<SegueViolation>(), SeguePolicy.stale(p, a, b))
assertEquals(listOf(SegueViolation.STALE_OUTGOING_AUDIO), SeguePolicy.stale(p, song("other"), b))
assertEquals(listOf(SegueViolation.STALE_INCOMING_AUDIO), SeguePolicy.stale(p, a, song("other")))
- val moved = song("a", tail = Grid(0.01, 0.5, 480, 1.0, 0.0, 1.0))
+ val moved = song("a", modelBeats = DoubleArray(480) { it * 0.5 + 0.01 })
assertEquals(listOf(SegueViolation.STALE_OUTGOING_GRID), SeguePolicy.stale(p, moved, b))
assertNotEquals(p.outgoing.gridKey, decide(moved, b).plan!!.outgoing.gridKey)
}
diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueStretchTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueStretchTest.kt
new file mode 100644
index 00000000..4e18999a
--- /dev/null
+++ b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueStretchTest.kt
@@ -0,0 +1,163 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+package app.myspoti.automix.segue
+
+import app.myspoti.automix.TrackAnalysis
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import kotlin.math.abs
+
+class SegueStretchTest {
+ private val t = SegueTuning()
+
+ /** Bars every 2 s, no grid for the whole song, only the beats given. */
+ private fun song(
+ key: String,
+ beats: DoubleArray = DoubleArray(480) { it * 0.5 },
+ down: FloatArray = FloatArray(0),
+ duration: Double = 240.0,
+ barSeconds: Double = 2.0,
+ risk: (Int) -> Float = { 0f },
+ ): TrackAnalysis {
+ val n = (duration / barSeconds).toInt()
+ return TrackAnalysis(duration, 0.0, duration, -10.0, 0, 1.0, 120.0, false, null, null, beats, DoubleArray(n) { it * barSeconds },
+ FloatArray(n) { -10f }, FloatArray(n) { -16f }, FloatArray(n) { -20f }, FloatArray(n), FloatArray(n), 0, true,
+ Double.NaN, Double.NaN, barVocalRisk = FloatArray(n, risk), audioKey = key, barBassDb = FloatArray(n) { -18f },
+ headMetre = 1f, tailMetre = 1f, modelBeats = beats, barDownConfidence = down)
+ }
+
+ private fun lockedLevels() = setOf(SegueLevel.LOCKED_LONG, SegueLevel.LOCKED_SHORT, SegueLevel.LOCKED_BEAT)
+
+ @Test fun aSteadyStretchIsFittedOnItsOwnBeats() {
+ val s = Stretches.measure(song("a"), 10, 4, 1.0, t)!!
+ assertEquals(20.0, s.start, 1e-9)
+ assertEquals(28.0, s.end, 1e-9)
+ assertEquals(0.5, s.grid.period, 1e-9)
+ assertEquals(1.0, s.reliability, 1e-9)
+ }
+
+ @Test fun scatteredBeatsAreRefused() {
+ val beats = DoubleArray(480) { it * 0.5 + if (it % 2 == 0) 0.06 else -0.06 }
+ assertNull(Stretches.measure(song("a", beats), 10, 4, 1.0, t))
+ }
+
+ @Test fun missingBeatsAreRefused() {
+ val beats = DoubleArray(480) { it * 0.5 }.filterIndexed { i, _ -> i !in 40..49 }.toDoubleArray()
+ assertNull(Stretches.measure(song("a", beats), 10, 4, 1.0, t))
+ }
+
+ @Test fun unsureDownbeatsAreRefused() {
+ val unsure = FloatArray(120) { 0.2f }
+ assertNull(Stretches.measure(song("a", down = unsure), 10, 4, 1.0, t))
+ assertNotNull(Stretches.measure(song("a", down = FloatArray(120) { 0.8f }), 10, 4, 1.0, t))
+ }
+
+ @Test fun beatsAlignedAtTheRightRateHaveNoPhaseError() {
+ val a = Stretches.measure(song("a"), 100, 4, 1.0, t)!!
+ val b = Stretches.measure(song("b"), 0, 4, 1.0, t)!!
+ val (median, p95) = Stretches.phase(a, b, 1.0)!!
+ assertTrue(median < 1_000 && p95 < 1_000)
+ // Off by a tenth of a beat, from the first beat to the last.
+ val (_, off) = Stretches.phase(a, b, 1.05)!!
+ assertTrue("$off", off > t.phaseMedianMaxUs)
+ }
+
+ @Test fun halfTempoIsMeasuredOnTheSparserBeats() {
+ val a = Stretches.measure(song("a"), 100, 4, 1.0, t)!!
+ val slow = song("b", DoubleArray(240) { it * 1.0 }, barSeconds = 4.0)
+ val b = Stretches.measure(slow, 0, 2, 1.0, t)!!
+ val (median, _) = Stretches.phase(a, b, 1.0)!!
+ assertTrue("$median", median < 1_000)
+ }
+
+ @Test fun aSongWithoutAWholeGridStillLocksOnAStretch() {
+ val d = SeguePolicy.decide(SegueRequest(song("a"), song("b")))
+ assertTrue(d.why, d.level in lockedLevels())
+ assertEquals(PulseState.LOCKED, d.plan!!.pulse.state)
+ }
+
+ @Test fun onlyAReliableStretchIsUsed() {
+ // Steady only from 180 s to 196 s: whatever is chosen lies there.
+ val beats = DoubleArray(480) { i -> val at = i * 0.5; if (at in 180.0..196.5) at else at + (if (i % 2 == 0) 0.07 else -0.07) }
+ val d = SeguePolicy.decide(SegueRequest(song("a", beats), song("b")))
+ assertTrue(d.why, d.level in lockedLevels())
+ val exit = d.plan!!.outgoing.anchorUs / 1e6
+ assertTrue("$exit", exit in 180.0..196.0)
+ }
+
+ @Test fun theRateIsTheRatioOfTheStretchesAndOnlyTheIncomingSongMoves() {
+ val quick = song("b", DoubleArray(500) { it * 0.48 }, barSeconds = 1.92)
+ val d = SeguePolicy.decide(SegueRequest(song("a"), quick))
+ assertTrue(d.why, d.level in lockedLevels())
+ assertEquals(SegueFormat.RATE_NOMINAL, d.plan!!.outgoing.ratePpm)
+ assertEquals(960_000.0, d.plan!!.incoming.ratePpm.toDouble(), 2_000.0)
+ }
+
+ @Test fun twoVoicesInAShortWindowMeetOnACut() {
+ val sung = { _: Int -> 1f }
+ // Voices all along: a window where both sing must hand over, never overlap.
+ val a = song("a", risk = sung); val b = song("b", risk = { if (it < 60) 1f else 0f })
+ val d = SeguePolicy.decide(SegueRequest(a, b, tuning = t.copy(voiceOverlapMaxUs = 60_000_000)))
+ if (d.level in lockedLevels() && (d.plan!!.span.musicalUnits ?: 0) <= t.lockedMiddleBars) assertEquals(SegueShape.CUT_ON_BEAT, d.shape)
+ }
+
+ @Test fun everyShapeIsAValidPlanAndPlayable() {
+ val compiler = SegueCompiler(t)
+ val keys = "a".repeat(64)
+ fun plan(lanes: List<SeguePlan.Lane>, length: Long = 16_000_000) = SeguePlan(SeguePolicy.ORIGIN,
+ SeguePlan.Side(keys, keys, 0, SegueFormat.RATE_NOMINAL, SegueFormat.RATE_NOMINAL), SeguePlan.Side(keys, keys, 0, SegueFormat.RATE_NOMINAL, SegueFormat.RATE_NOMINAL),
+ SeguePlan.Span(length, 8, UnitKind.BAR, Clock.ELAPSED), SeguePlan.Pulse(PulseState.LOCKED, 120_000_000, 0), lanes)
+ for (eq in listOf(true, false)) {
+ for (shape in listOf(SegueShape.BASS_SWAP, SegueShape.THREE_BAND, SegueShape.CUT_ON_BEAT, SegueShape.FILTER_OPEN)) {
+ val p = plan(compiler.locked(shape, 0.5, 1.0 / 32, 0.8, 1.0, eq))
+ assertEquals("$shape", emptyList<SegueViolation>(), SegueValidator.structural(p))
+ assertTrue("$shape", SegueDeck.playable(p))
+ }
+ for (shape in listOf(SegueShape.SWEEP_OUT, SegueShape.EVEN_FADE, SegueShape.QUICK_OUT, SegueShape.LATE_OUT)) {
+ val p = plan(compiler.filtered(shape, 0.05, 1.0, 0.8, eq))
+ assertEquals("$shape", emptyList<SegueViolation>(), SegueValidator.structural(p))
+ assertTrue("$shape", SegueDeck.playable(p))
+ }
+ }
+ }
+
+ @Test fun eachShapeHandsOverCompletely() {
+ val compiler = SegueCompiler(t)
+ fun level(lanes: List<SeguePlan.Lane>, side: LaneSide, x: Double) =
+ LaneCurve(lanes.first { it.side == side && it.target == Target.LEVEL }).value(x * SegueFormat.T_MAX)
+ for (shape in listOf(SegueShape.BASS_SWAP, SegueShape.THREE_BAND, SegueShape.CUT_ON_BEAT, SegueShape.FILTER_OPEN)) {
+ val lanes = compiler.locked(shape, 0.5, 1.0 / 32, 1.0, 1.0, true)
+ assertTrue("$shape", abs(level(lanes, LaneSide.OUTGOING, 0.0)) < 0.5)
+ assertTrue("$shape", level(lanes, LaneSide.OUTGOING, 1.0) < -40)
+ assertTrue("$shape", level(lanes, LaneSide.INCOMING, 0.0) < -40)
+ assertTrue("$shape", abs(level(lanes, LaneSide.INCOMING, 1.0)) < 0.5)
+ }
+ }
+
+ @Test fun theSameSongsGiveTheSameShape() {
+ val first = SeguePolicy.decide(SegueRequest(song("a"), song("b")))
+ val again = SeguePolicy.decide(SegueRequest(song("a"), song("b")))
+ assertEquals(first.shape, again.shape)
+ assertEquals(first.fingerprint, again.fingerprint)
+ }
+
+ @Test fun anotherAnalysisOfTheBeatsInvalidatesThePlan() {
+ val a = song("a"); val b = song("b")
+ val p = SeguePolicy.decide(SegueRequest(a, b)).plan!!
+ assertTrue(SeguePolicy.stale(p, a, b).isEmpty())
+ val moved = song("a", DoubleArray(480) { it * 0.5 + 0.01 })
+ assertTrue(SeguePolicy.stale(p, moved, b).isNotEmpty())
+ }
+
+ @Test fun aNewFormatKeepsTheModelsBeats() {
+ val s = song("a", down = FloatArray(120) { 0.7f })
+ val out = java.io.ByteArrayOutputStream(); s.write(out)
+ val back = TrackAnalysis.read(out.toByteArray().inputStream())!!
+ assertTrue(s.modelBeats.contentEquals(back.modelBeats))
+ assertTrue(s.barDownConfidence.contentEquals(back.barDownConfidence))
+ assertEquals(s.beatKey, back.beatKey)
+ }
+
+}
diff --git a/docs/automix/segue-design.md b/docs/automix/segue-design.md
index 7c22dd9d..dc20bae5 100644
--- a/docs/automix/segue-design.md
+++ b/docs/automix/segue-design.md
@@ -159,6 +159,44 @@ Each quantity carries a reliability: an uncertain measure weighs less, it is not
`audioKey` hashes the decoded analysis signal. A different decoder may give a different hash: the
plan is then recomputed, which costs nothing but a calculation.
+## Stretches
+
+A mix needs one good run of bars, not a good song. Locked candidates are therefore judged on their
+own beats (`SegueStretch.kt`), not on a grid fitted to the song's whole ending: a tempo that wanders
+elsewhere, or a bridge without drums, no longer rules out the part that holds.
+
+A stretch is a whole number of bars. It needs most of its beats from the model, a straight line
+through them that they stay close to, bars that do not slide off that line, and downbeats the model
+is sure of. The line gives the stretch its own tempo and start. Only the incoming song changes speed:
+the rate is the ratio of the two stretches' lengths in time, so their bar lines meet at both ends, and
+between half and double tempos one has half as many bars as the other.
+
+The expected phase error is no longer a mix of two songs' averages: the beats of the sparser stretch
+are measured against the nearest beats of the denser one, at the rate the plan will use. Plans carry
+`beatKey`, which covers the bars and the model's beats, so a new analysis of them invalidates the plan.
+
+## Shapes
+
+How a transition sounds is its lanes, so the engine has nothing to know of a shape (`SegueShape`).
+
+| Shape | Class | What is heard |
+|---|---|---|
+| `BASS_SWAP` | locked | The bass changes hands at one point, over level ramps |
+| `THREE_BAND` | locked | Bass, then middle, then top change hands |
+| `CUT_ON_BEAT` | locked | One song, then the other, on a shared downbeat |
+| `FILTER_OPEN` | locked | The incoming song opens from a closed filter up to the bass hand-over |
+| `SWEEP_OUT` | filtered | The outgoing song filtered away while the incoming one rises |
+| `EVEN_FADE` | filtered | Both levels equal-power, the bass handed over |
+| `QUICK_OUT` | filtered | The outgoing song leaves early, for a voice in its way |
+| `LATE_OUT` | filtered | The incoming song is up early, for a voice coming in it |
+
+What the music asks for comes first: two voices in a short window meet on a cut, and a cut is refused
+across a loudness jump. Otherwise the shape comes from a hash of the two audio keys and the anchors,
+so a pair always sounds the same and a playlist does not repeat one effect.
+
+A fade's length starts from the longest that stays clear of both songs' voices, instead of always
+the longest allowed.
+
## Policy
Order: generate candidates, reject by guards, score, keep the best if it clears its class's
@@ -170,7 +208,7 @@ and the same request always gives the same plan.
|---|---|---|
| `LOCKED_LONG` | locked | Reachable tempos, reliable grids, phrases on both sides, the longest length |
| `LOCKED_SHORT` | locked | The same, shorter |
-| `LOCKED_BEAT` | locked | Any shared downbeat, and only if the bass is managed |
+| `LOCKED_BEAT` | locked | Any shared downbeat, 4 or 2 bars, and only if the bass is managed |
| `FILTERED` | filtered | No alignment; filtered exit, limited overlap, progressive entry, without two voices |
| `HARD` | hard | Clear boundaries, close loudness, on a downbeat; an anti-click fade alone |
| `SIMPLE_FADE` | fade | A short fade from a safe point |
@@ -185,13 +223,14 @@ Scores compare only within a class; between classes the fallback order decides.
| Term | Kind | Role | Form |
|---|---|---|---|
| Rhythm | Note, strong | Makes the passage forgettable | Falls from `rhythmFullUs` to `phaseMedianMaxUs` |
-| Phrase | Note, strong | Makes the sections coincide | 0.5 per side on a boundary |
+| Phrase | Note, strong | Makes the sections coincide | 0.5 per side on a boundary at the start, `phraseNoteInside` at the middle or end |
| Energy | Note, medium | Avoids holes in the tension | Gaussian on the level gap, `energySigmaDb` |
| Exit | Note, medium | Leaves at the end of a phrase or on a calm section | Share without voice in the window's second half |
| Entry | Note, medium | Avoids entering mid-phrase | 1 on the first attack, less on a later section, less with the time skipped |
| Key | Note, weak | Reduces clashes | Distance on the wheel of fifths |
| Speed | Defect, medium | Prefers the least alteration | Rises from `rateCostFreePpm` to the guard |
| Voice | Defect, strong | Goes beyond the guard | Overlap weighted by audibility, over `voiceOverlapMaxUs` |
+| Voice cut | Defect, medium | Leaves no voice unheard | A's voice after the window and B's before the entry, as shares of each song's |
| Bass | Defect, medium | Limits masking | Common energy over total energy, 20–150 Hz, after levels and shelves |
| Lost | Defect, weak to medium | Prefers keeping the music | Seconds removed over `contentCutMaxUs` |
| Sobriety | Defect, weak | Avoids gratuitous effects | Effect lanes over `sobrietyFullEffects` |
@@ -243,6 +282,10 @@ marked `[R]`, reused from this repository.
| `simpleFadeUs`, `entryZoneUs`, `halfDoubleMaxBars` | 6 s, 40 s, 8 bars `[R]` | Values the player already uses |
| Lane shapes (`bassKillDb`, `outgoingTopDb`, `incomingBedGain`…) | see `SegueTuning` `[R]` | The curves the player already plays |
| `sweepClosedHz`, `sweepPeakRatio` | 400 Hz, 1.2 | The design asks for a filtered exit without values |
+| `stretchMinBeatShare`, `stretchMinDownConfidence` | 0.85, 0.5 | What a stretch must have to be relied on |
+| `penaltyVoiceCut`, `phraseNoteInside` | 0.5, 0.25 | The two score terms added with stretches |
+| `voiceBarRisk`, `lockedTinyBars` | 0.6 `[R]`, 2 | A bar counted as sung; the shortest locked length |
+| `coreOutDb`, `coreInDb`, `topInDb`, `cutBeatShare` | −24, −12, −12 dB, 0.25 | The three-band and cut shapes |
| `blockFrames`, `shelfLowHz`, `shelfHighHz`, `holdRampUs` | 64, 180 Hz, 2.5 kHz, 400 ms `[R]` | Values the player and the render already use |
| `shelfQ`, `coreQ` | 0.707 `[U]`, 0.5 | The slope of a shelf, and a bell wide enough to span the middle band |
| `echoDelayUs`, `echoFeedback`, `sendTailUs` | 375 ms, 0.35, 3 s | The sends need a sound; the policy does not use them yet |