Wire legal idle variants and validate dance media offline

This commit is contained in:
zenord
2026-10-13 05:33:27 +08:00
parent 04d73b4361
commit a113bd4bb3
8 changed files with 283 additions and 11 deletions
@@ -25,7 +25,8 @@ interface MusicMediaPort {
class ConversationMediaDriver(private val index: EmotionActionIndex,
private val create: (Int)->CompositeMediaPort,private val music: MusicMediaPort,
private val reveal: (Int?)->Unit,private val report: (String,BodyFrameKey?)->Unit = { _,_-> },
private val allowSilentEdges: Boolean=false) : ActionPlayer {
private val allowSilentEdges: Boolean=false,
private val chooseIdle: (Int)->Int = { kotlin.random.Random.nextInt(it) }) : ActionPlayer {
private data class Slot(val number: Int,val port: CompositeMediaPort,val pack: ContinuousAvatarPack,
val entry: Int,val packageId: String?,var key: BodyFrameKey?=null,var live: Boolean=true,var closedMouth: Boolean=false)
private val slots=mutableListOf<Slot>()
@@ -35,6 +36,8 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
private var bridge: Slot?=null
private var edge: ConversationEdge?=null
private var selectedId=index.fallbackId
private var idleTarget: String?=null
private var lastIdle: String?=null
var selection: RoleVisualSelection?=null;private set
val currentPackageId: String? get() = if(inBridge || actionStarted) null else visible?.packageId
val visualStatus: String get() = when {
@@ -42,6 +45,7 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
actionStarted -> "action_mouth_disabled"
inBridge -> "bridge_own_mouth"
edge!=null || revealing!=null -> "queued_emotion_safe_exit_or_prepared_surface"
idleTarget==visible?.packageId && idleTarget!=null -> "neutral_idle_variant"
visible?.packageId!=selectedId -> "queued_emotion_path_or_layer_unavailable"
else -> selection?.reason ?: "no_model_expression"
}
@@ -63,7 +67,7 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
private val releaseWaiters=mutableListOf<()->Unit>()
fun beginTurn(id: Long?) {
if(id==turn) return
turn=id;seq=-1;generation++;revision=0;talk=false;selection=null
turn=id;seq=-1;generation++;revision=0;talk=false;selection=null;idleTarget=null
selectedId=visible?.packageId ?: index.fallbackId
if(!inBridge && revealing==null) discardRoute()
applyMouth()
@@ -90,8 +94,10 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
seq=reply.sequence
selection=RoleVisualSelector.select(reply.emotion,reply.intensity,index.visuals,index.fallbackId)
selectedId=checkNotNull(selection).packageId
idleTarget=null
}
talk=talking && reply?.turnId==turn
if(talk) idleTarget=null
applyMouth()
if(active && !inBridge && !actionStarted) plan()
}
@@ -116,7 +122,7 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
slot.key=key;slot.closedMouth=closedMouth
if(slot===visible) {
reveal(slot.number);report(if(inBridge) "bridge" else "body_${slot.packageId}",key)
if(!inBridge) { applyMouth();plan() }
if(!inBridge) { chooseIdleAtLoopEnd(slot,key,previous);applyMouth();plan() }
if(slot===visible && !actionStarted && !holdExit(slot)) slot.port.advance()
} else if(returning) restoreAction(slot) else plan()
}
@@ -130,11 +136,32 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
private fun holdExit(slot: Slot): Boolean = !inBridge && (
slot.key?.index==edge?.fromFrame && edge!=null ||
action?.media?.let { slot.packageId==it.sourcePackage && slot.key?.index==it.sourceFrame }==true)
/** Only a completed neutral cycle may schedule variation. Speech/emotion/action
* remains authoritative, and every departure still goes through plan's own-mouth edge. */
private fun chooseIdleAtLoopEnd(slot: Slot,key: BodyFrameKey,previous: BodyFrameKey?) {
if(talk || action!=null || edge!=null || revealing!=null || target!=null || closing!=0 ||
key==previous || key.index!=slot.pack.body.frameCount-1 ||
selection?.visualEmotion?.let { it!=com.gorios.lanvoice.dialogue.RoleEmotion.NEUTRAL }==true ||
index.visuals.none { it.id==slot.packageId && it.emotion==com.gorios.lanvoice.dialogue.RoleEmotion.NEUTRAL }) return
if(idleTarget!=null && slot.packageId==idleTarget && slot.packageId!=index.fallbackId) {
idleTarget=index.fallbackId;report("idle_variant_return_queued",key);return
}
if(slot.packageId!=index.fallbackId || selectedId!=index.fallbackId) return
val candidates=index.visuals.filter { p->p.emotion==com.gorios.lanvoice.dialogue.RoleEmotion.NEUTRAL &&
p.id!=index.fallbackId && p.approvedEdges &&
index.edges.any { it.from==index.fallbackId && it.to==p.id && it.mouthRuntime!=null } &&
index.edges.any { it.from==p.id && it.to==index.fallbackId && it.mouthRuntime!=null }
}.map { it.id }
val fresh=candidates.filter { it!=lastIdle }.ifEmpty { candidates }
if(fresh.isEmpty()) return
val picked=chooseIdle(fresh.size);check(picked in fresh.indices)
idleTarget=fresh[picked];lastIdle=idleTarget;report("idle_variant_queued",key)
}
private fun plan() {
val source=visible ?: return
val key=source.key ?: return
if(inBridge || revealing!=null || !active || returning || actionStarted) return
val wanted=if(action!=null) checkNotNull(action?.media).sourcePackage else selectedId
val wanted=if(action!=null) checkNotNull(action?.media).sourcePackage else idleTarget ?: selectedId
var reviewedLoop: ConversationEdge?=null
if(source.packageId==wanted) {
if(edge!=null && edge?.from!=edge?.to) { discardRoute();return }
@@ -204,7 +231,7 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
override fun play(asset: ActionAsset,finished: (ActionEnd)->Unit,failed: ()->Unit) {
val media=asset.media
if(!active || action!=null || media==null || index.actionPack?.assets?.contains(asset)!=true) { failed();return }
action=asset;actionFinished=finished;actionFailed=failed;musicReady=false;talk=false;applyMouth()
action=asset;actionFinished=finished;actionFailed=failed;musicReady=false;talk=false;idleTarget=null;applyMouth()
val epoch=++musicEpoch
music.prepare(media,{ if(active && epoch==musicEpoch && action==asset) { musicReady=true;plan() } },
{ if(active && epoch==musicEpoch && actionStarted) reveal(3) },{
@@ -232,7 +259,7 @@ class ConversationMediaDriver(private val index: EmotionActionIndex,
completed?.invoke(ActionEnd(true,true,true))
}
override fun stop(released: ()->Unit) {
musicEpoch++;action=null;actionStarted=false;returning=false;musicReady=false
musicEpoch++;action=null;actionStarted=false;returning=false;musicReady=false;idleTarget=null
actionFinished=null;actionFailed=null;talk=false;generation++;revision=0
reveal(null);releaseWaiters.add(released)
active=false;visible=null;revealing=null;target=null;bridge=null;edge=null;inBridge=false