|
| 1 | +/** |
| 2 | + * Bounded one-way Labanotation → Posecode translator prototype. |
| 3 | + * |
| 4 | + * Implements a bounded conversion for the supported subset of Labanotation fundamentals: |
| 5 | + * - Direction symbols: Place, Forward, Backward, Left, Right, Diagonals |
| 6 | + * - Level indicators: Low (plié / flexion), Middle (neutral / straight), High (relevé / plantarflexion) |
| 7 | + * - Basic support changes (weight transfer, steps, stances) |
| 8 | + * - Basic arm and leg gestures |
| 9 | + * - Simple turns |
| 10 | + * |
| 11 | + * Emits explicit diagnostics for unsupported/lossy constructs (polyphonic timing, |
| 12 | + * fractional weight distribution, continuous effort/dynamics) rather than fabricating motion. |
| 13 | + */ |
| 14 | + |
| 15 | +export type LabanDirection = |
| 16 | + | "place" |
| 17 | + | "forward" |
| 18 | + | "backward" |
| 19 | + | "left" |
| 20 | + | "right" |
| 21 | + | "forward-left" |
| 22 | + | "forward-right" |
| 23 | + | "backward-left" |
| 24 | + | "backward-right"; |
| 25 | + |
| 26 | +export type LabanLevel = "low" | "middle" | "high"; |
| 27 | + |
| 28 | +export interface LabanSupportAction { |
| 29 | + side: "left" | "right" | "both"; |
| 30 | + direction: LabanDirection; |
| 31 | + level: LabanLevel; |
| 32 | + durationBeats?: number; |
| 33 | +} |
| 34 | + |
| 35 | +export interface LabanGestureAction { |
| 36 | + bodyPart: "left-leg" | "right-leg" | "left-arm" | "right-arm" | "head" | "torso"; |
| 37 | + direction: LabanDirection; |
| 38 | + level: LabanLevel; |
| 39 | + durationBeats?: number; |
| 40 | +} |
| 41 | + |
| 42 | +export interface LabanTurnAction { |
| 43 | + direction: "clockwise" | "counter-clockwise"; |
| 44 | + degrees: number; |
| 45 | + support: "left" | "right" | "both"; |
| 46 | + durationBeats?: number; |
| 47 | +} |
| 48 | + |
| 49 | +export interface LabanScoreEvent { |
| 50 | + beat: number; |
| 51 | + durationBeats: number; |
| 52 | + name?: string; |
| 53 | + supports?: LabanSupportAction[]; |
| 54 | + gestures?: LabanGestureAction[]; |
| 55 | + turn?: LabanTurnAction; |
| 56 | + unsupportedFeatures?: string[]; |
| 57 | +} |
| 58 | + |
| 59 | +export interface LabanScore { |
| 60 | + title: string; |
| 61 | + tempoBpm?: number; |
| 62 | + startPose?: string; |
| 63 | + events: LabanScoreEvent[]; |
| 64 | +} |
| 65 | + |
| 66 | +export interface TranslationDiagnostic { |
| 67 | + severity: "warning" | "error" | "info"; |
| 68 | + beat: number; |
| 69 | + message: string; |
| 70 | +} |
| 71 | + |
| 72 | +export interface TranslationResult { |
| 73 | + posecode: string; |
| 74 | + diagnostics: TranslationDiagnostic[]; |
| 75 | + lossyFeatureCount: number; |
| 76 | +} |
| 77 | + |
| 78 | +export function translateLabanToPosecode(score: LabanScore): TranslationResult { |
| 79 | + const bpm = score.tempoBpm ?? 60; |
| 80 | + const secPerBeat = 60 / bpm; |
| 81 | + const diagnostics: TranslationDiagnostic[] = []; |
| 82 | + let lossyFeatureCount = 0; |
| 83 | + |
| 84 | + const lines: string[] = [ |
| 85 | + `# Translated from Labanotation score: "${score.title}"`, |
| 86 | + `# Generator: Posecode Labanotation Bounded Prototype v0.1`, |
| 87 | + `# Note: Review diagnostics for lossy or simplified constructs.`, |
| 88 | + "", |
| 89 | + `posecode exercise "${score.title}"`, |
| 90 | + " rig humanoid", |
| 91 | + ` pose start = ${score.startPose ?? "standing"}`, |
| 92 | + "", |
| 93 | + ]; |
| 94 | + |
| 95 | + let cumulativeTravelX = 0; |
| 96 | + let cumulativeTravelZ = 0; |
| 97 | + let currentYawDeg = 0; |
| 98 | + |
| 99 | + score.events.forEach((event, idx) => { |
| 100 | + const durationSec = (event.durationBeats * secPerBeat).toFixed(2); |
| 101 | + const stepName = event.name ?? `Phase ${idx + 1} (Beat ${event.beat})`; |
| 102 | + const stepLines: string[] = []; |
| 103 | + |
| 104 | + // Flag explicit unsupported features |
| 105 | + if (event.unsupportedFeatures && event.unsupportedFeatures.length > 0) { |
| 106 | + for (const feat of event.unsupportedFeatures) { |
| 107 | + diagnostics.push({ |
| 108 | + severity: "warning", |
| 109 | + beat: event.beat, |
| 110 | + message: `Unsupported Labanotation feature: "${feat}". Motion approximated or omitted.`, |
| 111 | + }); |
| 112 | + lossyFeatureCount++; |
| 113 | + stepLines.push(` # [Lossy conversion] Labanotation feature omitted: ${feat}`); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Process Supports |
| 118 | + if (event.supports && event.supports.length > 0) { |
| 119 | + for (const sup of event.supports) { |
| 120 | + // Level mapping |
| 121 | + if (sup.level === "low") { |
| 122 | + stepLines.push(" knees: flex 45"); |
| 123 | + stepLines.push(" ankles: dorsiflex 12"); |
| 124 | + } else if (sup.level === "high") { |
| 125 | + stepLines.push(" ankles: plantarflex 30"); |
| 126 | + stepLines.push(" knees: extend 0"); |
| 127 | + } else { |
| 128 | + stepLines.push(" knees: extend 0"); |
| 129 | + stepLines.push(" ankles: dorsiflex 0"); |
| 130 | + } |
| 131 | + |
| 132 | + // Direction / Travel mapping for support steps |
| 133 | + const stepSize = 0.35; |
| 134 | + if (sup.direction === "forward") { |
| 135 | + cumulativeTravelZ += stepSize; |
| 136 | + stepLines.push(` travel: ${cumulativeTravelX.toFixed(2)} ${cumulativeTravelZ.toFixed(2)}`); |
| 137 | + } else if (sup.direction === "backward") { |
| 138 | + cumulativeTravelZ -= stepSize; |
| 139 | + stepLines.push(` travel: ${cumulativeTravelX.toFixed(2)} ${cumulativeTravelZ.toFixed(2)}`); |
| 140 | + } else if (sup.direction === "left") { |
| 141 | + cumulativeTravelX += stepSize; |
| 142 | + stepLines.push(` travel: ${cumulativeTravelX.toFixed(2)} ${cumulativeTravelZ.toFixed(2)}`); |
| 143 | + } else if (sup.direction === "right") { |
| 144 | + cumulativeTravelX -= stepSize; |
| 145 | + stepLines.push(` travel: ${cumulativeTravelX.toFixed(2)} ${cumulativeTravelZ.toFixed(2)}`); |
| 146 | + } |
| 147 | + |
| 148 | + // Ground contact constraints |
| 149 | + if (sup.side === "both") { |
| 150 | + stepLines.push(" ground-lock: feet"); |
| 151 | + } else if (sup.side === "left") { |
| 152 | + stepLines.push(" pin: foot_left floor"); |
| 153 | + } else if (sup.side === "right") { |
| 154 | + stepLines.push(" pin: foot_right floor"); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Process Turn |
| 160 | + if (event.turn) { |
| 161 | + const delta = event.turn.direction === "clockwise" ? event.turn.degrees : -event.turn.degrees; |
| 162 | + currentYawDeg += delta; |
| 163 | + stepLines.push(` turn: ${currentYawDeg}`); |
| 164 | + if (event.turn.support === "left") { |
| 165 | + stepLines.push(" pin: foot_left floor"); |
| 166 | + } else if (event.turn.support === "right") { |
| 167 | + stepLines.push(" pin: foot_right floor"); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + // Process Gestures |
| 172 | + if (event.gestures && event.gestures.length > 0) { |
| 173 | + for (const g of event.gestures) { |
| 174 | + if (g.bodyPart === "right-arm" || g.bodyPart === "left-arm") { |
| 175 | + const side = g.bodyPart === "left-arm" ? "left" : "right"; |
| 176 | + if (g.direction === "forward") { |
| 177 | + stepLines.push(` shoulder_${side}: flex ${g.level === "high" ? 140 : g.level === "low" ? 30 : 85}`); |
| 178 | + } else if (g.direction === "left" || g.direction === "right") { |
| 179 | + stepLines.push(` shoulder_${side}: abduct ${g.level === "high" ? 130 : g.level === "low" ? 40 : 85}`); |
| 180 | + } else if (g.direction === "place") { |
| 181 | + stepLines.push(` shoulder_${side}: flex 0`); |
| 182 | + stepLines.push(` shoulder_${side}: abduct 0`); |
| 183 | + } |
| 184 | + stepLines.push(` elbow_${side}: flex 15`); |
| 185 | + } else if (g.bodyPart === "right-leg" || g.bodyPart === "left-leg") { |
| 186 | + const side = g.bodyPart === "left-leg" ? "left" : "right"; |
| 187 | + if (g.direction === "forward") { |
| 188 | + stepLines.push(` hip_${side}: flex 45`); |
| 189 | + if (g.level === "high") stepLines.push(` ankle_${side}: plantarflex 35`); |
| 190 | + } else if (g.direction === "place" && g.level === "high") { |
| 191 | + // Retiré/passé or knee lift |
| 192 | + stepLines.push(` hip_${side}: flex 50`); |
| 193 | + stepLines.push(` knee_${side}: flex 90`); |
| 194 | + stepLines.push(` ankle_${side}: plantarflex 30`); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + stepLines.push(` cue "Beat ${event.beat}: ${stepName}"`); |
| 201 | + |
| 202 | + lines.push(` step "${stepName}" ${durationSec}s flow:`); |
| 203 | + lines.push(...stepLines); |
| 204 | + lines.push(""); |
| 205 | + }); |
| 206 | + |
| 207 | + return { |
| 208 | + posecode: lines.join("\n"), |
| 209 | + diagnostics, |
| 210 | + lossyFeatureCount, |
| 211 | + }; |
| 212 | +} |
0 commit comments