Skip to content

Native Android client for Spotify, built with Jetpack Compose

automix: add echo and rising-filter shapes, keep more of both songs

kota-zo6683a08a03a8
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 11fef529..3ac0e195 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
@@ -20,6 +20,10 @@ enum class SegueShape {
CUT_ON_BEAT,
/** Locked: the incoming song opens from a closed filter up to the bass hand-over. */
FILTER_OPEN,
+ /** Locked: after the bass swap, the outgoing song thins from below until only its top is left. */
+ RISE_OUT,
+ /** Locked: the outgoing song's last beats repeat in time over the incoming one. */
+ ECHO_TAIL,
/** 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. */
@@ -28,6 +32,10 @@ enum class SegueShape {
QUICK_OUT,
/** Fade: the incoming song is up early and the outgoing one stays until late. */
LATE_OUT,
+ /** Fade: the outgoing song is thrown into an echo on a downbeat, and leaves. */
+ ECHO_OUT,
+ /** Fade: the outgoing song thins from below and dissolves into a room. */
+ WASH_OUT,
}
/** Writes each class's treatment as lanes: every curve the engine plays is data in the plan. */
@@ -72,6 +80,8 @@ internal class SegueCompiler(private val t: SegueTuning) {
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.RISE_OUT -> riseOut(swap, beatShare, matchA, matchB)
+ SegueShape.ECHO_TAIL -> echoTail(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
}
@@ -95,6 +105,28 @@ internal class SegueCompiler(private val t: SegueTuning) {
return out
}
+ /** The bass swap, the outgoing song's top kept while a high-pass climbs under it. */
+ private fun riseOut(swap: Double, beatShare: Double, matchA: Double, matchB: Double): List<SeguePlan.Lane> =
+ bassSwap(swap, beatShare, matchA, matchB, true).filterNot { it.side == LaneSide.OUTGOING && it.target == Target.SHELF_TOP } +
+ lane(LaneSide.OUTGOING, Target.RISE_HZ, listOf(knot(0.0, null), knot(swap, null), knot(1.0, t.riseClosedHz, KnotForm.Octave)))
+
+ /** The bass swap, and the outgoing song's fader closing early under a send that repeats its last beats. */
+ private fun echoTail(swap: Double, beatShare: Double, matchA: Double, matchB: Double): List<SeguePlan.Lane> {
+ val closed = (1 - beatShare).coerceAtLeast(swap + 0.01)
+ val opens = (closed - 2 * beatShare).coerceAtLeast(swap)
+ val closes = (closed + beatShare).coerceAtMost(1.0)
+ val out = ArrayList(levels(positions(swap, opens, closed)) { x ->
+ val after = ((x - swap) / (closed - swap)).coerceIn(0.0, 1.0)
+ val ga = if (x < swap) 1.0 else cos(PI / 2 * after)
+ val gb = if (x < swap) t.incomingBedGain * sin(PI / 2 * (x / swap)) else t.incomingBedGain + (1 - t.incomingBedGain) * ((x - swap) / (1 - swap)).coerceIn(0.0, 1.0)
+ matched(x, swap, matchA, matchB, ga, gb)
+ })
+ out += bassHandOver(swap, (swap + beatShare * t.bassSwapBeats).coerceAtMost(1.0))
+ out += lane(LaneSide.INCOMING, Target.SHELF_TOP, listOf(knot(0.0, t.incomingTopDb), knot(swap, 0.0), knot(1.0, 0.0)))
+ out += lane(LaneSide.OUTGOING, Target.SEND_ECHO, listOf(knot(0.0, 0.0), knot(opens, 0.0), knot(closed, t.echoSend), knot(closes, 0.0), knot(1.0, 0.0)))
+ 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 ->
@@ -129,14 +161,17 @@ internal class SegueCompiler(private val t: SegueTuning) {
/** Where a fade's bass hand-over sits, 0..1 of the window. */
fun swapOf(shape: SegueShape): Double = when (shape) {
+ SegueShape.ECHO_OUT -> t.echoThrowShare
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> {
+ fun filtered(shape: SegueShape, beatShare: Double, matchA: Double, matchB: Double, eq: Boolean, at: Double = swapOf(shape), beat: Double = beatShare): List<SeguePlan.Lane> {
val swap = swapOf(shape)
if (shape == SegueShape.SWEEP_OUT) return sweepOut(swap, beatShare, matchA, matchB, eq)
+ if (shape == SegueShape.ECHO_OUT) return echoOut(at, beat, matchA, matchB)
+ if (shape == SegueShape.WASH_OUT) return washOut(swap, beatShare, matchA, matchB)
// 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)
@@ -172,6 +207,36 @@ internal class SegueCompiler(private val t: SegueTuning) {
return out
}
+ /** The incoming song rising without its bass under the outgoing one, which leaves in a beat at [at], thrown into the echo. */
+ private fun echoOut(at: Double, beat: Double, matchA: Double, matchB: Double): List<SeguePlan.Lane> {
+ val gone = (at + beat).coerceAtMost(0.98)
+ val opens = (at - beat).coerceAtLeast(0.01)
+ val closes = (gone + beat).coerceAtMost(0.99)
+ val levels = levels(positions(at, gone)) { x ->
+ val ga = cos(PI / 2 * ((x - at) / (gone - at)).coerceIn(0.0, 1.0))
+ val gb = sin(PI / 2 * (x / gone).coerceIn(0.0, 1.0))
+ matched(x, at, matchA, matchB, ga, gb)
+ }
+ return levels + listOf(
+ lane(LaneSide.OUTGOING, Target.SEND_ECHO, listOf(knot(0.0, 0.0), knot(opens, 0.0), knot(at, t.echoSend), knot(closes, 0.0), knot(1.0, 0.0))),
+ lane(LaneSide.INCOMING, Target.SHELF_BOTTOM, listOf(knot(0.0, t.bassKillDb), knot(at, t.bassKillDb), knot(gone, 0.0), knot(1.0, 0.0))),
+ )
+ }
+
+ /** The outgoing song loses its low end, then its level, into a room; the incoming one takes the bass at the swap. */
+ private fun washOut(swap: Double, beatShare: Double, matchA: Double, matchB: Double): List<SeguePlan.Lane> {
+ val from = swap * 0.5
+ 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)
+ })
+ out += bassHandOver(swap, (swap + beatShare * t.bassSwapBeats).coerceAtMost(1.0)).filter { it.side == LaneSide.INCOMING }
+ out += lane(LaneSide.OUTGOING, Target.RISE_HZ, listOf(knot(0.0, null), knot(from, null), knot(1.0, t.riseClosedHz, KnotForm.Octave)))
+ out += lane(LaneSide.OUTGOING, Target.SEND_ROOM, listOf(knot(0.0, 0.0), knot(from, 0.0), knot(0.9, t.roomSend), knot(1.0, 0.0)))
+ return out
+ }
+
/** A cut: an anti-click fade and nothing else. */
fun hard(): List<SeguePlan.Lane> = listOf(
lane(LaneSide.OUTGOING, Target.LEVEL, listOf(knot(0.0, 0.0), knot(1.0, null))),
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueDeck.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueDeck.kt
index e5de61bf..d509485e 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueDeck.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueDeck.kt
@@ -24,6 +24,7 @@ class SegueDeck(
private val level = curve(Target.LEVEL)
private val bottom = curve(Target.SHELF_BOTTOM); private val core = curve(Target.BAND_CORE); private val top = curve(Target.SHELF_TOP)
private val sweep = curve(Target.SWEEP_HZ); private val peak = curve(Target.SWEEP_PEAK)
+ private val rise = curve(Target.RISE_HZ)
private val room = curve(Target.SEND_ROOM); private val echo = curve(Target.SEND_ECHO)
private val usesSends = room != null || echo != null
@@ -33,12 +34,15 @@ class SegueDeck(
private val bellCore = Array(channels) { Shape() }
private val shelfTop = Array(channels) { Shape() }
private val svf = Array(channels) { Svf() }
- private val echoes = if (echo != null) Array(channels) { Delay(frames(tuning.echoDelayUs)) } else null
+ private val riseSvf = Array(channels) { Svf() }
+ private val sendCut = Array(channels) { LowCut(tuning.sendLowCutHz, sampleRate) }
+ private val echoes = if (echo != null) Array(channels) { Delay(frames(plan.echoUs ?: tuning.echoDelayUs)) } else null
private val rooms = if (room != null) Array(channels) { Room(tuning, sampleRate) } else null
private var lastGain = Double.NaN
private val coreHz = sqrt(tuning.shelfLowHz * tuning.shelfHighHz)
private var filterMix = 0.0
+ private var riseMix = 0.0
private var holdMix = 0.0
private val holdStep = 1.0 / max(1.0, tuning.holdRampUs * sampleRate / 1e6)
@@ -85,12 +89,16 @@ class SegueDeck(
val wantFilter = if (hz < SegueFormat.SWEEP_OPEN_HZ * 0.999) 1.0 else 0.0
val q = sqrt(0.5) * (peak?.value(u1) ?: 1.0)
if (sweep != null) for (f in svf) f.tune(hz, q, sampleRate)
+ val riseHz = rise?.value(u1) ?: SegueFormat.RISE_OPEN_HZ
+ val wantRise = if (riseHz > SegueFormat.RISE_OPEN_HZ * 1.001) 1.0 else 0.0
+ if (rise != null) for (f in riseSvf) f.tune(riseHz, sqrt(0.5), sampleRate)
val roomSend0 = room?.value(u0) ?: 0.0; val roomSend1 = room?.value(u1) ?: 0.0
val echoSend0 = echo?.value(u0) ?: 0.0; val echoSend1 = echo?.value(u1) ?: 0.0
for (i in 0 until n) {
val p = (i + 1.0) / n
val gain = g0 + (g1 - g0) * p
val mix = filterMix + (wantFilter - filterMix) * p
+ val riseNow = riseMix + (wantRise - riseMix) * p
holdMix = if (hold) min(1.0, holdMix + holdStep) else max(0.0, holdMix - holdStep)
for (c in 0 until channels) {
val k = (from + i) * channels + c
@@ -101,16 +109,21 @@ class SegueDeck(
val filtered = svf[c].run(y)
y += (filtered - y) * mix
}
+ if (rise != null) {
+ riseSvf[c].run(y)
+ y += (riseSvf[c].high - y) * riseNow
+ }
var out = y * gain
if (usesSends) {
// Sends tap the signal before the fader: a tail outlives the song's level.
- echoes?.let { out += it[c].run(y * (echoSend0 + (echoSend1 - echoSend0) * p), tuning.echoFeedback) }
- rooms?.let { out += it[c].run(y * (roomSend0 + (roomSend1 - roomSend0) * p)) }
+ val send = sendCut[c].run(y)
+ echoes?.let { out += it[c].run(send * (echoSend0 + (echoSend1 - echoSend0) * p), tuning.echoFeedback) }
+ rooms?.let { out += it[c].run(send * (roomSend0 + (roomSend1 - roomSend0) * p)) }
}
- x[k] = (if (holdMix > 0) dry * holdMix + out * (1 - holdMix) else if (gain == 1.0 && eqNeutral && mix == 0.0 && !usesSends) dry else out).toFloat()
+ x[k] = (if (holdMix > 0) dry * holdMix + out * (1 - holdMix) else if (gain == 1.0 && eqNeutral && mix == 0.0 && riseNow == 0.0 && !usesSends) dry else out).toFloat()
}
}
- lastGain = g1; filterMix = wantFilter
+ lastGain = g1; filterMix = wantFilter; riseMix = wantRise
}
/** Shelves and a bell on one state-variable filter: stable while they move, and the identity at 0 dB. */
@@ -145,8 +158,9 @@ class SegueDeck(
}
}
- /** A low-pass that stays stable while its cutoff moves. */
+ /** A low-pass that stays stable while its cutoff moves; the same pass's high-pass is in [high]. */
private class Svf {
+ var high = 0.0; private set
private var g = 1.0; private var r = 1.414; private var h = 0.0
private var s1 = 0.0; private var s2 = 0.0
fun tune(hz: Double, q: Double, rate: Int) {
@@ -154,12 +168,20 @@ class SegueDeck(
}
fun run(x: Double): Double {
val hp = (x - (r + g) * s1 - s2) * h
+ high = hp
val bp = g * hp + s1; s1 = g * hp + bp
val lp = g * bp + s2; s2 = g * bp + lp
return lp
}
}
+ /** One pole: enough to keep a send's bass from building up. */
+ private class LowCut(hz: Double, rate: Int) {
+ private val k = kotlin.math.exp(-2 * PI * hz / rate)
+ private var low = 0.0
+ fun run(x: Double): Double { low = x + (low - x) * k; return x - low }
+ }
+
private class Delay(size: Int) {
private val line = DoubleArray(size); private var i = 0
fun run(x: Double, feedback: Double): Double {
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueFormat.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueFormat.kt
index 55de44ff..c04ea7df 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueFormat.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueFormat.kt
@@ -27,4 +27,10 @@ object SegueFormat {
/** The cutoff a null sweep knot stands for: the filter is fully open. */
const val SWEEP_OPEN_HZ = 20_000.0
+
+ /** The cutoff a null rise knot stands for: the high-pass lets everything through. */
+ const val RISE_OPEN_HZ = 20.0
+
+ /** The longest echo a plan may ask for. */
+ const val ECHO_MAX_US = 2_000_000L
}
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueJson.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueJson.kt
index 7235729d..cecb9840 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueJson.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueJson.kt
@@ -143,6 +143,7 @@ object SegueCodec {
"knots" to l.knots.map { k -> listOf(k.t, k.valueMilli?.let { BigDecimal.valueOf(it, 3) }, k.form.wire) })
})
put("gestures", p.gestures.map { mapOf("kind" to it.kind.wire, "atUs" to it.atUs, "units" to it.units) })
+ p.echoUs?.let { put("echoUs", it) }
}
private fun side(s: SeguePlan.Side) = mapOf("audioKey" to s.audioKey, "gridKey" to s.gridKey, "anchorUs" to s.anchorUs,
@@ -174,6 +175,7 @@ object SegueCodec {
SeguePlan.Gesture(enum(m, "kind", GestureKind.values()) { it.wire }, long(m, "atUs"), toInt(long(m, "units")))
},
schema,
+ longOrNull(root, "echoUs"),
)
}
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePlan.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePlan.kt
index 9fa2672f..0d8fd927 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePlan.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SeguePlan.kt
@@ -15,6 +15,8 @@ data class SeguePlan(
val lanes: List<Lane>,
val gestures: List<Gesture> = emptyList(),
val schema: String = SegueFormat.SCHEMA,
+ /** The echo send's delay, set from the song's pulse; null leaves it to the engine. */
+ val echoUs: Long? = null,
) {
data class Origin(val engine: String, val corpus: String)
@@ -64,6 +66,7 @@ enum class Target(val wire: String, val unit: SegueUnit, val nullMeaning: String
SHELF_TOP("shelfTop", SegueUnit.DB, "neutral", 0.0),
SWEEP_HZ("sweepHz", SegueUnit.HZ, "filter open", SegueFormat.SWEEP_OPEN_HZ),
SWEEP_PEAK("sweepPeak", SegueUnit.RATIO, "neutral", 1.0),
+ RISE_HZ("riseHz", SegueUnit.HZ, "filter open", SegueFormat.RISE_OPEN_HZ),
SEND_ROOM("sendRoom", SegueUnit.RATIO, "no send", 0.0),
SEND_ECHO("sendEcho", SegueUnit.RATIO, "no send", 0.0),
}
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 135b43e6..663a21b4 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
@@ -105,7 +105,8 @@ object SeguePolicy {
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, val shape: SegueShape? = null, val restoreSec: Double = 0.0)
+ /** [score] decides whether a level is good enough; [pick] decides between candidates of one level. */
+ private class Found(val plan: SeguePlan, val score: Double, val swap: Double, val shape: SegueShape? = null, val restoreSec: Double = 0.0, val pick: Double = score)
private class Run(val r: SegueRequest) {
val a = r.a; val b = r.b; val t = r.tuning
@@ -167,11 +168,13 @@ object SeguePolicy {
/** A's points where a window of [lengthUs] fits, instrumental endings first. */
private fun exits(times: Sequence<Double>, lengthUs: Long, limitUs: Long, phrases: Boolean): List<Point> {
val zone = max(zoneStartUs, r.earliestExitUs)
- return times.map { it to us(it) }.filter { (_, u) -> u >= zone && u + lengthUs <= limitUs }.mapNotNull { (time, u) ->
+ val all = times.map { it to us(it) }.filter { (_, u) -> u >= zone && u + lengthUs <= limitUs }.mapNotNull { (time, u) ->
val bar = a.barAt(time + 0.05)
val on = boundary(a, bar)
if (phrases && !on) null else Point(u, bar, instrumentalAfter(bar) + u.toDouble() / max(1L, limitUs) * 1e-3, on)
- }.sortedByDescending { it.rank }.take(t.candidatesPerSide).toList()
+ }.toList()
+ // The latest ones stay in the running even when a voice lasts to the end: the score weighs that against what leaving early loses.
+ return (all.sortedByDescending { it.rank }.take(t.candidatesPerSide) + all.sortedByDescending { it.us }.take(t.lateCandidates)).distinctBy { it.us }
}
private fun instrumentalAfter(bar: Int): Double {
@@ -254,8 +257,10 @@ object SeguePolicy {
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)
+ .copy(echoUs = if (shape == SegueShape.ECHO_TAIL) echoOf(ga.period) else null)
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))
+ val pick = score.total - t.lostPickLocked * skipped(e.point, n.point, lengthUs)
+ if (best == null || pick > best.pick) best = Found(plan, score.total, swap, shape, restore(ratePpm, gb.period), pick)
}
}
// The longest length that works: shorter only when it has to be.
@@ -294,7 +299,7 @@ object SeguePolicy {
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)
+ return (out.sortedByDescending { it.point.rank }.take(t.candidatesPerSide) + out.sortedByDescending { it.point.us }.take(t.lateCandidates)).distinctBy { it.point.us }
}
/** B's stretches from its useful attack; leaving out a start must be worth its note. */
@@ -334,27 +339,54 @@ object SeguePolicy {
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
+ val tail = if (rings(e.us, lengthUs)) listOf(SegueShape.ECHO_TAIL) else emptyList()
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)
+ bars <= t.lockedShortBars -> listOf(SegueShape.BASS_SWAP, SegueShape.CUT_ON_BEAT, SegueShape.FILTER_OPEN) + tail
+ bars <= t.lockedMiddleBars -> listOf(SegueShape.BASS_SWAP, SegueShape.THREE_BAND, SegueShape.CUT_ON_BEAT, SegueShape.FILTER_OPEN, SegueShape.RISE_OUT) + tail
+ else -> listOf(SegueShape.BASS_SWAP, SegueShape.THREE_BAND, SegueShape.FILTER_OPEN, SegueShape.RISE_OUT) + tail
}, 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)
+ val ring = rings(e.us, lengthUs)
+ val all = listOf(SegueShape.SWEEP_OUT, SegueShape.EVEN_FADE, SegueShape.QUICK_OUT, SegueShape.LATE_OUT) +
+ (if (ring && r.eq && ga != null) listOf(SegueShape.ECHO_OUT) else emptyList()) +
+ (if (ring && r.eq) listOf(SegueShape.WASH_OUT) else emptyList())
// 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
+ lateVoice -> if (ring && r.eq && ga != null) seeded(listOf(SegueShape.QUICK_OUT, SegueShape.ECHO_OUT), e.us, n.us) else SegueShape.QUICK_OUT
earlyVoice -> SegueShape.LATE_OUT
else -> seeded(all, e.us, n.us)
}
}
+ /** Music the listener never hears, the end of one song and the start of the other, 0..1. */
+ private fun skipped(e: Point, n: Point, lengthUs: Long): Double =
+ ((max(0L, us(a.contentEnd) - (e.us + lengthUs)) + max(0L, n.us - us(b.contentStart))).toDouble() / t.lostFullUs).coerceAtMost(1.0)
+
+ /** The outgoing song runs long enough past the window for a send to ring out. */
+ private fun rings(exitUs: Long, lengthUs: Long) = us(a.duration) - (exitUs + lengthUs) >= t.sendTailUs
+
+ /** A delay in time with [period] seconds a beat: three quarters of a beat, or as near as stays usable. */
+ private fun echoOf(period: Double): Long {
+ var d = us(period * 0.75)
+ while (d > t.echoMaxUs) d /= 2
+ while (d < t.echoMinUs) d *= 2
+ return d.coerceAtMost(SegueFormat.ECHO_MAX_US)
+ }
+
+ /** The downbeat nearest the usual hand-over, 0..1 of the window, not too near either end. */
+ private fun throwAt(e: Point, lengthUs: Long, usual: Double): Double {
+ val want = e.us + usual * lengthUs
+ val bar = a.barStarts.minByOrNull { abs(us(it) - want) } ?: return usual
+ val at = (us(bar) - e.us).toDouble() / lengthUs
+ return if (at in 0.3..0.85) at else usual
+ }
+
/** 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
@@ -383,10 +415,14 @@ object SeguePolicy {
tally.seen++
if (tally.refused(SegueGuards.crossed(SegueClass.FILTERED, measures(e, n, lengthUs, lengthUs), t))) continue
val shape = filteredShape(e, n, lengthUs)
- val lanes = compiler.filtered(shape, min(1.0, 0.4 * 1e6 / lengthUs), matchA(), matchB(), r.eq)
+ val at = if (shape == SegueShape.ECHO_OUT) throwAt(e, lengthUs, compiler.swapOf(shape)) else compiler.swapOf(shape)
+ val beat = ga?.let { min(0.25, it.period * 1e6 / lengthUs) } ?: 0.1
+ val lanes = compiler.filtered(shape, min(1.0, 0.4 * 1e6 / lengthUs), matchA(), matchB(), r.eq, at, beat)
val plan = plan(e.us, n.us, ga, gb, SegueFormat.RATE_NOMINAL, lengthUs, null, PulseState.FREE, null, null, lanes)
+ .copy(echoUs = if (shape == SegueShape.ECHO_OUT) ga?.let { echoOf(it.period) } else null)
val score = score(e, n, lengthUs, lengthUs, null, 0, lanes)
- if (best == null || score.total > best.score) best = Found(plan, score.total, compiler.swapOf(shape), shape)
+ val pick = score.total - t.lostPickFade * skipped(e, n, lengthUs)
+ if (best == null || pick > best.pick) best = Found(plan, score.total, at, shape, pick = pick)
}
if (best != null && best.score >= t.thresholdFiltered) break
}
@@ -475,7 +511,7 @@ object SeguePolicy {
SegueNote("key", if (r.harmonic) t.weightKey else 0.0, keyReliability(), 1 - keyDistance / 2),
)
val overlap = if (sa.maxVoice == 0f || sb.maxVoice == 0f) 0.0 else MixPlanner.vocalOverlap(a, a0, a1, b, b0, b1)
- val effects = lanes.count { it.target == Target.SWEEP_HZ || it.target == Target.SEND_ROOM || it.target == Target.SEND_ECHO }
+ val effects = lanes.count { it.target == Target.SWEEP_HZ || it.target == Target.RISE_HZ || it.target == Target.SEND_ROOM || it.target == Target.SEND_ECHO }
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),
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 350242f4..4a0c611c 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
@@ -51,6 +51,10 @@ data class SegueTuning(
val penaltyBass: Double = 0.5,
val weightKey: Double = 0.2,
val penaltyLost: Double = 0.35,
+ /** Choosing within a level: this much music skipped, both songs together [I], costs this much [I], locked or faded. */
+ val lostFullUs: Long = 20_000_000,
+ val lostPickLocked: Double = 0.4,
+ val lostPickFade: Double = 1.0,
val penaltySobriety: Double = 0.2,
// ── Shapes of the score's terms ──
@@ -99,8 +103,8 @@ data class SegueTuning(
/** 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]. */
- val filteredLongUs: Long = 12_000_000,
- val filteredMiddleUs: Long = 8_000_000,
+ val filteredLongUs: Long = 8_000_000,
+ val filteredMiddleUs: Long = 6_000_000,
val filteredShortUs: Long = 4_000_000,
/** Exits lie in this last share of the song. */
val exitZoneShare: Double = 1.0 / 3.0,
@@ -108,6 +112,8 @@ data class SegueTuning(
val entryZoneUs: Long = 40_000_000,
/** Best candidates kept per side [I] range 3–8. */
val candidatesPerSide: Int = 8,
+ /** The latest exits always tried besides the best-ranked [I]. */
+ val lateCandidates: Int = 4,
/** Anti-click fade of a cut [U] range 5–30 ms. */
val microFadeUs: Long = 30_000,
/** Plain short fade [R]. */
@@ -132,6 +138,16 @@ 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,
+ /** The outgoing song's high-pass rises to this cutoff [I]. */
+ val riseClosedHz: Double = 1_200.0,
+ /** Send levels at their height [I]: echo, room. */
+ val echoSend: Double = 0.5,
+ val roomSend: Double = 1.5,
+ /** Where in a fade the outgoing song is thrown into the echo [I]: late, so the repeats have the end of the window to themselves. */
+ val echoThrowShare: Double = 0.72,
+ /** An echo's delay stays between these [I]: shorter is a flam, longer loses the pulse. */
+ val echoMinUs: Long = 240_000,
+ val echoMaxUs: Long = 620_000,
/** 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,
@@ -140,7 +156,7 @@ data class SegueTuning(
/** A cut on the beat lasts this share of a beat [I]: quick, without a click. */
val cutBeatShare: Double = 0.25,
/** The two faders' largest sum [I]: 1 can never clip two peaks that meet, and dips by 3 dB at equal gains. */
- val overlapMaxSum: Double = 1.25,
+ val overlapMaxSum: Double = 1.1,
/** 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]. */
@@ -161,13 +177,15 @@ data class SegueTuning(
/** Slope of the shelves [U], width of the core bell between them [I]. */
val shelfQ: Double = 0.7071,
val coreQ: Double = 0.5,
- /** Echo send: delay [I], feedback [I]. */
+ /** Echo send: delay when the plan sets none [I], feedback [I]. */
val echoDelayUs: Long = 375_000,
- val echoFeedback: Double = 0.35,
+ val echoFeedback: Double = 0.5,
+ /** Sends leave out what is below this [I]: bass that rings on muddies the song coming in. */
+ val sendLowCutHz: Double = 250.0,
/** Room send: comb delays and all-pass delays of a small Schroeder reverb [U], comb feedback [I], all-pass gain [U]. */
val roomCombUs: List<Long> = listOf(29_700, 37_100, 41_100, 43_700),
val roomAllPassUs: List<Long> = listOf(5_000, 1_700),
- val roomCombFeedback: Double = 0.75,
+ val roomCombFeedback: Double = 0.87,
val roomAllPassGain: Double = 0.7,
/** How long the outgoing player keeps running for a send's tail [I]. */
val sendTailUs: Long = 3_000_000,
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueValidator.kt b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueValidator.kt
index 117c03d6..87390c06 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueValidator.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/segue/SegueValidator.kt
@@ -20,6 +20,7 @@ enum class SegueViolation {
FREQUENCY_NOT_POSITIVE,
EASE_NOT_POSITIVE,
GESTURE_INVALID,
+ ECHO_INVALID,
WINDOW_PAST_OUTGOING,
WINDOW_PAST_INCOMING,
STALE_OUTGOING_AUDIO,
@@ -53,6 +54,7 @@ object SegueValidator {
if (p.lanes.map { it.side to it.target }.toSet().size != p.lanes.size) out += SegueViolation.DUPLICATE_LANE
for (lane in p.lanes) lane(lane, out)
for (g in p.gestures) if (g.atUs < 0 || g.units <= 0) out += SegueViolation.GESTURE_INVALID
+ p.echoUs?.let { if (it !in 1..SegueFormat.ECHO_MAX_US) out += SegueViolation.ECHO_INVALID }
return out.toList()
}
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 f8ab4e5a..60ad4e48 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
@@ -36,6 +36,8 @@ object SegueBench {
} ?: SegueTuning()
when (rest.firstOrNull()) {
"split" -> printSplit(dir)
+ "tempos" -> tempos(dir, fold)
+ "tails" -> tails(dir, fold, limit ?: 12)
"matrix" -> matrix(dir, fold, rest.contains("fresh"))
"measure" -> measure(dir, fold, limit ?: 100, args.firstOrNull { it.startsWith("only=") }?.removePrefix("only="))
"stability" -> stability(dir, fold, limit ?: 60)
@@ -77,6 +79,49 @@ object SegueBench {
// ── Every pair: which level, why ──
+ /** For each shape that rings: how loud the outgoing song's tail is against the incoming one, and whether the mix clips. */
+ private fun tails(dir: File, fold: String?, limit: Int) {
+ val names = names(dir, fold)
+ val songs = analyses(dir, names)
+ val tmp = File(dir, ".segue-tmp").apply { mkdirs() }
+ val cache = HashMap<String, FloatArray>()
+ val shapes = setOf(SegueShape.ECHO_OUT, SegueShape.WASH_OUT, SegueShape.ECHO_TAIL, SegueShape.RISE_OUT)
+ val seen = HashMap<SegueShape, Int>()
+ println("pair\tshape\tseconds\ttail_db\tb_db\ttail_minus_b\tpeak\tclipped_ms")
+ for ((x, y) in pairs(names, null).shuffled(java.util.Random(7))) {
+ val d = SeguePolicy.decide(request(songs.getValue(x), songs.getValue(y)))
+ val shape = d.shape ?: continue
+ val p = d.plan ?: continue
+ if (shape !in shapes || seen.getOrDefault(shape, 0) >= limit) continue
+ seen.merge(shape, 1, Int::plus)
+ cache.clear()
+ val st = stems(dir, x, y, p, 2.0, 4.0, cache, tmp)
+ val end = st.preFrames + (p.span.lengthUs / 1e6 * RATE).toInt()
+ fun rms(v: FloatArray, from: Int, to: Int): Double {
+ var e = 0.0; var n = 0
+ for (i in 2 * from until min(2 * to, v.size)) { e += v[i] * v[i]; n++ }
+ return 10 * log10(max(1e-12, e / max(1, n)))
+ }
+ // From where the send is at its height, or the end of the window when there is none.
+ val height = p.lanes.firstOrNull { it.side == LaneSide.OUTGOING && (it.target == Target.SEND_ECHO || it.target == Target.SEND_ROOM) }?.knots?.maxByOrNull { it.valueMilli ?: 0 }?.t
+ val from = if (height == null) end else st.preFrames + (height.toDouble() / SegueFormat.T_MAX * p.span.lengthUs / 1e6 * RATE).toInt() + RATE / 3
+ val to = from + RATE
+ val ma = mix(st)
+ val peak = ma.maxOf { abs(it) }
+ val clipped = ma.count { abs(it) > 1f } / 2 * 1000 / RATE
+ println(String.format(L, "%s,%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.2f\t%d", x, y, shape, p.span.lengthUs / 1e6, rms(st.a, from, to), rms(st.b, from, to), rms(st.a, from, to) - rms(st.b, from, to), peak, clipped))
+ }
+ tmp.deleteRecursively()
+ }
+
+ /** Each song's tempo as the grid has it, at both ends. */
+ private fun tempos(dir: File, fold: String?) {
+ val names = names(dir, fold)
+ for ((name, s) in analyses(dir, names, false)) println(String.format(L, "%s\t%.2f\t%.2f\t%.2f\t%.2f\t%d", name,
+ s.tail?.bpm ?: 0.0, s.tail?.steadiness ?: 0.0, s.head?.bpm ?: 0.0, s.head?.steadiness ?: 0.0, s.modelBeats.size) +
+ String.format(L, "\t%.1f\t%.1f\t%.1f\t%.1f", s.duration, s.contentStart, s.contentEnd, s.barStarts.lastOrNull() ?: 0.0))
+ }
+
private fun matrix(dir: File, fold: String?, fresh: Boolean) {
val names = names(dir, fold)
val songs = analyses(dir, names, fresh)
diff --git a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueDeckTest.kt b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueDeckTest.kt
index b031857f..5db15137 100644
--- a/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueDeckTest.kt
+++ b/base/automix/src/test/kotlin/app/myspoti/automix/segue/SegueDeckTest.kt
@@ -183,6 +183,44 @@ class SegueDeckTest {
assertFalse(SegueDeck.playable(fine.copy(span = fine.span.copy(lengthUs = 0))))
}
+ @Test fun theRiseCutsLowNotesAndPassesHighOnes() {
+ val closed = plan(lane(LaneSide.OUTGOING, Target.RISE_HZ, SeguePlan.Knot(0, 1_200_000, step), SeguePlan.Knot(m, 1_200_000, step)))
+ val low = run(closed, LaneSide.OUTGOING, tone(60.0, 1.0))
+ val high = run(closed, LaneSide.OUTGOING, tone(6_000.0, 1.0))
+ assertTrue(db(rms(low, 0.5, 1.0) / rms(tone(60.0, 1.0), 0.5, 1.0)) < -30)
+ assertTrue(abs(db(rms(high, 0.5, 1.0) / rms(tone(6_000.0, 1.0), 0.5, 1.0))) < 1)
+ }
+
+ @Test fun anOpenRiseLeavesTheSongAlone() {
+ val open = plan(lane(LaneSide.OUTGOING, Target.RISE_HZ, SeguePlan.Knot(0, null, step), SeguePlan.Knot(m, null, step)))
+ val x = tone(60.0, 0.5)
+ val out = run(open, LaneSide.OUTGOING, x)
+ for (i in x.indices) assertEquals(x[i], out[i], 1e-6f)
+ }
+
+ @Test fun theEchoRepeatsAtThePlansDelay() {
+ // Silence with one click: the fader is shut, so all that comes out is the send.
+ val shut = lane(LaneSide.OUTGOING, Target.LEVEL, SeguePlan.Knot(0, null, step), SeguePlan.Knot(m, null, step))
+ val send = lane(LaneSide.OUTGOING, Target.SEND_ECHO, SeguePlan.Knot(0, 1000, step), SeguePlan.Knot(m, 1000, step))
+ val at = rate / 10
+ val delays = listOf(300_000L, 450_000L)
+ for (us in delays) {
+ val x = FloatArray(2 * rate); x[at] = 1f
+ val out = run(plan(shut, send).copy(echoUs = us), LaneSide.OUTGOING, x)
+ val first = out.indices.first { abs(out[it]) > 0.2f }
+ assertEquals("$us", at + (us * rate / 1_000_000).toInt(), first.toLong().toInt())
+ val second = out.indices.first { it > first + 100 && abs(out[it]) > 0.05f }
+ assertEquals("$us", (us * rate / 1_000_000).toDouble(), (second - first).toDouble(), 2.0)
+ }
+ }
+
+ @Test fun anEchoOutsideItsBoundsIsRefused() {
+ val p = plan(lane(LaneSide.OUTGOING, Target.LEVEL, SeguePlan.Knot(0, 0, step), SeguePlan.Knot(m, 0, step)))
+ assertTrue(SegueViolation.ECHO_INVALID in SegueValidator.structural(p.copy(echoUs = 0)))
+ assertTrue(SegueViolation.ECHO_INVALID in SegueValidator.structural(p.copy(echoUs = SegueFormat.ECHO_MAX_US + 1)))
+ assertEquals(emptyList<SegueViolation>(), SegueValidator.structural(p.copy(echoUs = 400_000)))
+ }
+
@Test fun onlySendsAskForATail() {
val fader = lane(LaneSide.OUTGOING, Target.LEVEL, SeguePlan.Knot(0, 0, step), SeguePlan.Knot(m, null, ramp))
assertEquals(0L, SegueDeck.tailUs(plan(fader)))
diff --git a/docs/automix/segue-design.md b/docs/automix/segue-design.md
index f6a0a847..025ed1bf 100644
--- a/docs/automix/segue-design.md
+++ b/docs/automix/segue-design.md
@@ -291,3 +291,13 @@ marked `[R]`, reused from this repository.
| `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 |
| `roomCombUs`, `roomAllPassUs`, `roomCombFeedback`, `roomAllPassGain` | Schroeder delays `[U]`, 0.75, 0.7 `[U]` | Same |
+
+## Sends and the rising filter
+
+Fades and locked mixes can now ring out. `riseHz` is a high-pass on a side, the mirror of `sweepHz`; a null knot leaves it open at 20 Hz. `echoUs` on the plan sets the echo send's delay from the outgoing song's beat, three quarters of a beat brought into 240-620 ms; a plan without it uses the engine's default. Sends leave out everything below 250 Hz so the tail does not sit on the incoming bass.
+
+Four shapes use them. `ECHO_OUT` throws the outgoing song into the echo on the downbeat nearest 72 % of a fade. `WASH_OUT` thins it from below into the room. `RISE_OUT` and `ECHO_TAIL` are the same two ideas on a locked mix. A shape with a tail is offered only when the outgoing song runs on past the window for at least `sendTailUs`: its player has to keep feeding the deck for the tail to sound.
+
+## What is skipped
+
+The level a pair reaches is still judged on `score`, but the candidate within a level is chosen on `pick`: the score less a cost for the music nobody hears, the end of one song and the start of the other. Taking this out of the score keeps a locked mix from losing its level because it skips an intro. Fades pay more for it than locked mixes, which have a reason to skip. The latest exits always stay in the running, including those with a voice that lasts to the end, so the choice between the two is the score's. Fades run 4, 6 or 8 s.