feat: Jam genauer synchron (Tempo-Feinabgleich, Messzeitpunkt, Spul-Vorhalt)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -13,7 +13,25 @@ import kotlin.math.max
|
||||
* paused ? position : position + (T - ref) / 1000. Negativ heisst "startet gleich".
|
||||
*/
|
||||
object JamSync {
|
||||
const val TOLERANCE_S = 1.5
|
||||
const val TOLERANCE_S = 1.0
|
||||
|
||||
/** Beim Spulen etwas vorhalten: bis das Video an der neuen Stelle spielt, vergeht Zeit. */
|
||||
const val SEEK_LEAD_S = 0.2
|
||||
private const val NUDGE_START_S = 0.08
|
||||
private const val NUDGE_STOP_S = 0.03
|
||||
private const val NUDGE_GAIN = 0.25
|
||||
private const val NUDGE_MAX = 0.08
|
||||
|
||||
/**
|
||||
* Wiedergabetempo, das eine kleine Abweichung [drift] (eigene minus Jam-Position, s) ausgleicht -
|
||||
* unhoerbar, die Tonhoehe bleibt. Einmal angefangen ([current] != 1) bis fast 0 ausgleichen.
|
||||
*/
|
||||
fun nudgeRate(drift: Double, current: Double): Double {
|
||||
val threshold = if (abs(current - 1.0) > 0.001) NUDGE_STOP_S else NUDGE_START_S
|
||||
if (!drift.isFinite() || abs(drift) < threshold) return 1.0
|
||||
val rate = (1.0 - NUDGE_GAIN * drift).coerceIn(1.0 - NUDGE_MAX, 1.0 + NUDGE_MAX)
|
||||
return Math.round(rate * 1000.0) / 1000.0
|
||||
}
|
||||
private const val CODE_ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"
|
||||
private val VIDEO_ID = Regex("^[A-Za-z0-9_-]{11}$")
|
||||
|
||||
@@ -207,13 +225,14 @@ object JamSync {
|
||||
return Target(item.videoId, item.id, positionAt(state.playback, serverNow), state.playback.paused, item.dur)
|
||||
}
|
||||
|
||||
data class Local(val videoId: String?, val pos: Double, val paused: Boolean)
|
||||
data class Local(val videoId: String?, val pos: Double, val paused: Boolean, val rate: Double = 1.0)
|
||||
|
||||
sealed class Step {
|
||||
data class Load(val videoId: String, val pos: Double) : Step()
|
||||
data class Seek(val pos: Double) : Step()
|
||||
data object Play : Step()
|
||||
data object Pause : Step()
|
||||
data class Rate(val rate: Double) : Step()
|
||||
}
|
||||
|
||||
/** Was der eigene Player tun soll, damit er zum Jam passt. */
|
||||
@@ -233,9 +252,14 @@ object JamSync {
|
||||
if (local.pos > 0.5) steps.add(Step.Seek(0.0))
|
||||
return steps
|
||||
}
|
||||
if (abs(local.pos - want.pos) > tolerance && !(nearEnd && local.pos > want.pos)) steps.add(Step.Seek(want.pos))
|
||||
val drift = local.pos - want.pos
|
||||
val seek = abs(drift) > tolerance && !(nearEnd && local.pos > want.pos)
|
||||
if (seek) steps.add(Step.Seek(if (want.paused) want.pos else want.pos + SEEK_LEAD_S))
|
||||
if (want.paused && !local.paused) steps.add(Step.Pause)
|
||||
if (!want.paused && local.paused) steps.add(Step.Play)
|
||||
// Feinabgleich ueber das Tempo, solange beide normal laufen.
|
||||
val desired = if (!seek && !want.paused && !local.paused && !nearEnd) nudgeRate(drift, local.rate) else 1.0
|
||||
if (abs(desired - local.rate) > 0.004) steps.add(Step.Rate(desired))
|
||||
return steps
|
||||
}
|
||||
|
||||
@@ -271,6 +295,8 @@ object JamSync {
|
||||
d: api('getDuration') && player.getDuration() > 0 ? player.getDuration() : ((video && isFinite(video.duration)) ? video.duration : 0),
|
||||
paused: paused,
|
||||
ended: state === 0 || (video ? video.ended : false),
|
||||
rate: video && video.playbackRate > 0 ? video.playbackRate : 1,
|
||||
now: Date.now(),
|
||||
ad: ad,
|
||||
error: error,
|
||||
title: meta && meta.title ? meta.title : null,
|
||||
@@ -288,6 +314,14 @@ object JamSync {
|
||||
"var v=p&&p.querySelector('video')||document.querySelector('video');if(v){v.$prop();}})();"
|
||||
}
|
||||
|
||||
/** Wiedergabetempo des Players (Feinabgleich, Tonhoehe bleibt). */
|
||||
fun rateScript(rate: Double): String {
|
||||
val r = if (rate.isFinite()) rate.coerceIn(0.5, 2.0) else 1.0
|
||||
return "(function(){var p=document.getElementById('movie_player');" +
|
||||
"var v=p&&p.querySelector('video')||document.querySelector('video');" +
|
||||
"if(v){v.preservesPitch=true;v.playbackRate=$r;}})();"
|
||||
}
|
||||
|
||||
fun seekScript(seconds: Double): String {
|
||||
val s = if (seconds.isFinite()) max(0.0, seconds) else 0.0
|
||||
return "(function(){var p=document.getElementById('movie_player');var s=$s;" +
|
||||
|
||||
Reference in New Issue
Block a user