File size: 18,849 Bytes
b563b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
 * PersonaFlow - LipSync Engine
 * Ported from TalkingHead (MIT License, Mika Suominen)
 * Converts English text to Oculus LipSync Visemes with timing data
 * 
 * Viseme set: aa, E, I, O, U, PP, SS, TH, DD, FF, kk, nn, RR, CH, sil
 * These map directly to RPM model morph targets: viseme_aa, viseme_E, etc.
 */

const LipSyncEngine = {

    // ─── Letter-to-Sound Rules (NRL Report 7948) ───
    // Each rule: { regex, move, visemes[] }
    _rulesCompiled: false,
    _rules: {},

    // Viseme durations in relative units (1 = average)
    visemeDurations: {
        'aa': 0.95, 'E': 0.90, 'I': 0.92, 'O': 0.96, 'U': 0.95, 'PP': 1.08,
        'SS': 1.23, 'TH': 1, 'DD': 1.05, 'FF': 1.00, 'kk': 1.21, 'nn': 0.88,
        'RR': 0.88, 'CH': 1.1, 'sil': 1
    },

    // Pauses in relative units
    specialDurations: { ' ': 1, ',': 3, '-': 0.5, "'": 0.5 },

    // All viseme names (Oculus LipSync)
    visemeNames: ['aa', 'E', 'I', 'O', 'U', 'PP', 'SS', 'TH', 'DD', 'FF', 'kk', 'nn', 'RR', 'CH', 'sil'],

    // Viseme-to-blendshape intensity mapping for non-viseme morph targets
    // This adds jaw movement and supplementary expressions to make lip sync more visible
    visemeBlendshapes: {
        'aa': { jawOpen: 0.35 },
        'E': { jawOpen: 0.20 },
        'I': { jawOpen: 0.12 },
        'O': { jawOpen: 0.30 },
        'U': { jawOpen: 0.15 },
        'PP': { jawOpen: 0.02 },
        'SS': { jawOpen: 0.05 },
        'TH': { jawOpen: 0.08 },
        'DD': { jawOpen: 0.10 },
        'FF': { jawOpen: 0.05 },
        'kk': { jawOpen: 0.12 },
        'nn': { jawOpen: 0.08 },
        'RR': { jawOpen: 0.10 },
        'CH': { jawOpen: 0.08 },
        'sil': { jawOpen: 0.0 }
    },

    /**
     * Initialize the rules engine. Called once on first use.
     */
    init() {
        if (this._rulesCompiled) return;

        // Raw letter-to-sound rules from NRL Report 7948
        const rawRules = {
            'A': [
                "[A] =aa", " [ARE] =aa RR", " [AR]O=aa RR", "[AR]#=E RR",
                " ^[AS]#=E SS", "[A]WA=aa", "[AW]=aa", " :[ANY]=E nn I",
                "[A]^+#=E", "#:[ALLY]=aa nn I", " [AL]#=aa nn", "[AGAIN]=aa kk E nn",
                "#:[AG]E=I kk", "[A]^+:#=aa", ":[A]^+ =E", "[A]^%=E",
                " [ARR]=aa RR", "[ARR]=aa RR", " :[AR] =aa RR", "[AR] =E",
                "[AR]=aa RR", "[AIR]=E RR", "[AI]=E", "[AY]=E", "[AU]=aa",
                "#:[AL] =aa nn", "#:[ALS] =aa nn SS", "[ALK]=aa kk", "[AL]^=aa nn",
                " :[ABLE]=E PP aa nn", "[ABLE]=aa PP aa nn", "[ANG]+=E nn kk", "[A]=aa"
            ],
            'B': [
                " [BE]^#=PP I", "[BEING]=PP I I nn", " [BOTH] =PP O TH",
                " [BUS]#=PP I SS", "[BUIL]=PP I nn", "[B]=PP"
            ],
            'C': [
                " [CH]^=kk", "^E[CH]=kk", "[CH]=CH", " S[CI]#=SS aa",
                "[CI]A=SS", "[CI]O=SS", "[CI]EN=SS", "[C]+=SS",
                "[CK]=kk", "[COM]%=kk aa PP", "[C]=kk"
            ],
            'D': [
                "#:[DED] =DD I DD", ".E[D] =DD", "#^:E[D] =DD", " [DE]^#=DD I",
                " [DO] =DD U", " [DOES]=DD aa SS", " [DOING]=DD U I nn",
                " [DOW]=DD aa", "[DU]A=kk U", "[D]=DD"
            ],
            'E': [
                "#:[E] =", "'^:[E] =", " :[E] =I", "#[ED] =DD", "#:[E]D =",
                "[EV]ER=E FF", "[E]^%=I", "[ERI]#=I RR I", "[ERI]=E RR I",
                "#:[ER]#=E", "[ER]#=E RR", "[ER]=E", " [EVEN]=I FF E nn",
                "#:[E]W=", "@[EW]=U", "[EW]=I U", "[E]O=I", "#:&[ES] =I SS",
                "#:[E]S =", "#:[ELY] =nn I", "#:[EMENT]=PP E nn DD", "[EFUL]=FF U nn",
                "[EE]=I", "[EARN]=E nn", " [EAR]^=E", "[EAD]=E DD", "#:[EA] =I aa",
                "[EA]SU=E", "[EA]=I", "[EIGH]=E", "[EI]=I", " [EYE]=aa", "[EY]=I",
                "[EU]=I U", "[E]=E"
            ],
            'F': [
                "[FUL]=FF U nn", "[F]=FF"
            ],
            'G': [
                "[GIV]=kk I FF", " [G]I^=kk", "[GE]T=kk E", "SU[GGES]=kk kk E SS",
                "[GG]=kk", " B#[G]=kk", "[G]+=kk", "[GREAT]=kk RR E DD",
                "#[GH]=", "[G]=kk"
            ],
            'H': [
                " [HAV]=I aa FF", " [HERE]=I I RR", " [HOUR]=aa EE", "[HOW]=I aa",
                "[H]#=I", "[H]="
            ],
            'I': [
                " [IN]=I nn", " [I] =aa", "[IN]D=aa nn", "[IER]=I E",
                "#:R[IED] =I DD", "[IED] =aa DD", "[IEN]=I E nn", "[IE]T=aa E",
                " :[I]%=aa", "[I]%=I", "[IE]=I", "[I]^+:#=I", "[IR]#=aa RR",
                "[IZ]%=aa SS", "[IS]%=aa SS", "[I]D%=aa", "+^[I]^+=I",
                "[I]T%=aa", "#^:[I]^+=I", "[I]^+=aa", "[IR]=E", "[IGH]=aa",
                "[ILD]=aa nn DD", "[IGN] =aa nn", "[IGN]^=aa nn", "[IGN]%=aa nn",
                "[IQUE]=I kk", "[I]=I"
            ],
            'J': ["[J]=kk"],
            'K': [" [K]N=", "[K]=kk"],
            'L': ["[LO]C#=nn O", "L[L]=", "#^:[L]%=aa nn", "[LEAD]=nn I DD", "[L]=nn"],
            'M': ["[MOV]=PP U FF", "[M]=PP"],
            'N': [
                "E[NG]+=nn kk", "[NG]R=nn kk", "[NG]#=nn kk", "[NGL]%=nn kk aa nn",
                "[NG]=nn", "[NK]=nn kk", " [NOW] =nn aa", "[N]=nn"
            ],
            'O': [
                "[OF] =aa FF", "[OROUGH]=E O", "#:[OR] =E", "#:[ORS] =E SS",
                "[OR]=aa RR", " [ONE]=FF aa nn", "[OW]=O", " [OVER]=O FF E",
                "[OV]=aa FF", "[O]^%=O", "[O]^EN=O", "[O]^I#=O", "[OL]D=O nn",
                "[OUGHT]=aa DD", "[OUGH]=aa FF", " [OU]=aa", "H[OU]S#=aa",
                "[OUS]=aa SS", "[OUR]=aa RR", "[OULD]=U DD", "^[OU]^L=aa",
                "[OUP]=U OO", "[OU]=aa", "[OY]=O", "[OING]=O I nn", "[OI]=O",
                "[OOR]=aa RR", "[OOK]=U kk", "[OOD]=U DD", "[OO]=U", "[O]E=O",
                "[O] =O", "[OA]=O", " [ONLY]=O nn nn I", " [ONCE]=FF aa nn SS",
                "[ON'T]=O nn DD", "C[O]N=aa", "[O]NG=aa", " ^:[O]N=aa",
                "I[ON]=aa nn", "#:[ON] =aa nn", "#^[ON]=aa nn", "[O]ST =O",
                "[OF]^=aa FF", "[OTHER]=aa TH E", "[OSS] =aa SS", "#^:[OM]=aa PP",
                "[O]=aa"
            ],
            'P': [
                "[PH]=FF", "[PEOP]=PP I PP", "[POW]=PP aa", "[PUT] =PP U DD",
                "[P]=PP"
            ],
            'Q': ["[QUAR]=kk FF aa RR", "[QU]=kk FF", "[Q]=kk"],
            'R': [" [RE]^#=RR I", "[R]=RR"],
            'S': [
                "[SH]=SS", "#[SION]=SS aa nn", "[SOME]=SS aa PP", "#[SUR]#=SS E",
                "[SUR]#=SS E", "#[SU]#=SS U", "#[SSU]#=SS U", "#[SED] =SS DD",
                "#[S]#=SS", "[SAID]=SS E DD", "^[SION]=SS aa nn", "[S]S=",
                ".[S] =SS", "#:.E[S] =SS", "#^:##[S] =SS", "#^:#[S] =SS",
                "U[S] =SS", " :#[S] =SS", " [SCH]=SS kk", "[S]C+=",
                "#[SM]=SS PP", "#[SN]'=SS aa nn", "[S]=SS"
            ],
            'T': [
                " [THE] =TH aa", "[TO] =DD U", "[THAT] =TH aa DD", " [THIS] =TH I SS",
                " [THEY]=TH E", " [THERE]=TH E RR", "[THER]=TH E", "[THEIR]=TH E RR",
                " [THAN] =TH aa nn", " [THEM] =TH E PP", "[THESE] =TH I SS",
                " [THEN]=TH E nn", "[THROUGH]=TH RR U", "[THOSE]=TH O SS",
                "[THOUGH] =TH O", " [THUS]=TH aa SS", "[TH]=TH", "#:[TED] =DD I DD",
                "S[TI]#N=CH", "[TI]O=SS", "[TI]A=SS", "[TIEN]=SS aa nn",
                "[TUR]#=CH E", "[TU]A=CH U", " [TWO]=DD U", "[T]=DD"
            ],
            'U': [
                " [UN]I=I U nn", " [UN]=aa nn", " [UPON]=aa PP aa nn",
                "@[UR]#=U RR", "[UR]#=I U RR", "[UR]=E", "[U]^ =aa",
                "[U]^^=aa", "[UY]=aa", " G[U]#=", "G[U]%=", "G[U]#=FF",
                "#N[U]=I U", "@[U]=I", "[U]=I U"
            ],
            'V': ["[VIEW]=FF I U", "[V]=FF"],
            'W': [
                " [WERE]=FF E", "[WA]S=FF aa", "[WA]T=FF aa", "[WHERE]=FF E RR",
                "[WHAT]=FF aa DD", "[WHOL]=I O nn", "[WHO]=I U", "[WH]=FF",
                "[WAR]=FF aa RR", "[WOR]^=FF E", "[WR]=RR", "[W]=FF"
            ],
            'X': [" [X]=SS", "[X]=kk SS"],
            'Y': [
                "[YOUNG]=I aa nn", " [YOU]=I U", " [YES]=I E SS", " [Y]=I",
                "#^:[Y] =I", "#^:[Y]I=I", " :[Y] =aa", " :[Y]#=aa",
                " :[Y]^+:#=I", " :[Y]^#=I", "[Y]=I"
            ],
            'Z': ["[Z]=SS"]
        };

        // Operator definitions for rule patterns
        const ops = {
            '#': '[AEIOUY]+',
            '.': '[BDVGJLMNRWZ]',
            '%': '(?:ER|E|ES|ED|ING|ELY)',
            '&': '(?:[SCGZXJ]|CH|SH)',
            '@': '(?:[TSRDLZNJ]|TH|CH|SH)',
            '^': '[BCDFGHJKLMNPQRSTVWXZ]',
            '+': '[EIY]',
            ':': '[BCDFGHJKLMNPQRSTVWXZ]*',
            ' ': '\\b'
        };

        // Compile rules to regex
        Object.keys(rawRules).forEach(key => {
            this._rules[key] = rawRules[key].map(rule => {
                const posL = rule.indexOf('[');
                const posR = rule.indexOf(']');
                const posE = rule.indexOf('=');
                const strLeft = rule.substring(0, posL);
                const strLetters = rule.substring(posL + 1, posR);
                const strRight = rule.substring(posR + 1, posE);
                const strVisemes = rule.substring(posE + 1);

                const o = { regex: '', move: 0, visemes: [] };

                let exp = '';
                exp += [...strLeft].map(x => ops[x] || x).join('');
                const ctxLetters = [...strLetters];
                ctxLetters[0] = ctxLetters[0].toLowerCase();
                exp += ctxLetters.join('');
                o.move = ctxLetters.length;
                exp += [...strRight].map(x => ops[x] || x).join('');
                o.regex = new RegExp(exp);

                if (strVisemes.length) {
                    strVisemes.split(' ').forEach(viseme => {
                        o.visemes.push(viseme);
                    });
                }

                return o;
            });
        });

        this._rulesCompiled = true;
        console.log('LipSyncEngine: Initialized with', Object.keys(this._rules).length, 'letter rules');
    },

    /**
     * Pre-process text: remove special chars, convert numbers to words, etc.
     * @param {string} s - Input text
     * @returns {string} Cleaned text
     */
    preProcessText(s) {
        // Symbols to words
        const symbols = { '%': 'percent', '€': 'euros', '&': 'and', '+': 'plus', '$': 'dollars' };
        let r = s.replace(/[#_*":;]/g, '');
        r = r.replace(/[%€&+$]/g, (sym) => ' ' + (symbols[sym] || '') + ' ');

        // Simple number-to-word conversion for common cases
        r = r.replace(/\b(\d+)\b/g, (match) => {
            const n = parseInt(match);
            if (n >= 0 && n <= 20) {
                const words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
                    'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen',
                    'eighteen', 'nineteen', 'twenty'];
                return words[n];
            }
            // For larger numbers, just spell out digits
            return match.split('').map(d => ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'][parseInt(d)] || d).join(' ');
        });

        r = r.replace(/(\D)\1\1+/g, '$1$1') // max 2 repeating chars
            .replace(/  +/g, ' ')
            .trim();

        return r;
    },

    /**
     * Convert a word to Oculus LipSync Visemes with timing
     * @param {string} w - Word to convert
     * @returns {Object} { visemes: string[], times: number[], durations: number[] }
     */
    wordToVisemes(w) {
        this.init();

        const o = { words: w.toUpperCase(), visemes: [], times: [], durations: [], i: 0 };
        let t = 0;

        const chars = [...o.words];
        while (o.i < chars.length) {
            const c = chars[o.i];
            const ruleset = this._rules[c];
            if (ruleset) {
                let matched = false;
                for (let i = 0; i < ruleset.length; i++) {
                    const rule = ruleset[i];
                    const test = o.words.substring(0, o.i) + c.toLowerCase() + o.words.substring(o.i + 1);
                    const matches = test.match(rule.regex);
                    if (matches) {
                        rule.visemes.forEach(viseme => {
                            if (o.visemes.length && o.visemes[o.visemes.length - 1] === viseme) {
                                const d = 0.7 * (this.visemeDurations[viseme] || 1);
                                o.durations[o.durations.length - 1] += d;
                                t += d;
                            } else {
                                const d = this.visemeDurations[viseme] || 1;
                                o.visemes.push(viseme);
                                o.times.push(t);
                                o.durations.push(d);
                                t += d;
                            }
                        });
                        o.i += rule.move;
                        matched = true;
                        break;
                    }
                }
                if (!matched) o.i++;
            } else {
                o.i++;
                t += this.specialDurations[c] || 0;
            }
        }

        return o;
    },

    /**
     * Convert full text to a viseme timeline scaled to a given audio duration
     * @param {string} text - Full text to convert
     * @param {number} audioDuration - Audio duration in seconds
     * @returns {Object[]} Array of { time, viseme, duration, intensity }
     */
    textToVisemes(text, audioDuration) {
        this.init();

        const processedText = this.preProcessText(text);
        const words = processedText.split(/\s+/).filter(w => w.length > 0);

        if (words.length === 0) return [];

        // Build the full viseme sequence with relative timing
        let allVisemes = [];
        let totalRelativeTime = 0;

        words.forEach((word, wi) => {
            // Strip trailing punctuation — keep it for pause calculation but don't feed to viseme rules
            const punctMatch = word.match(/[.,!?;:…]+$/);
            const cleanWord = punctMatch ? word.slice(0, -punctMatch[0].length) : word;
            const punct = punctMatch ? punctMatch[0] : '';

            if (cleanWord.length > 0) {
                const result = this.wordToVisemes(cleanWord);
                if (result.visemes.length > 0) {
                    const wordDuration = result.times[result.visemes.length - 1] + result.durations[result.visemes.length - 1];
                    result.visemes.forEach((viseme, vi) => {
                        allVisemes.push({
                            viseme: viseme,
                            relTime: totalRelativeTime + result.times[vi],
                            relDuration: result.durations[vi]
                        });
                    });
                    totalRelativeTime += wordDuration;
                }
            }

            // Punctuation-aware pauses — insert explicit silence viseme so mouth closes
            let pauseDuration;
            if (punct.includes('…') || punct.includes('...')) {
                pauseDuration = 4.0;
            } else if (punct.includes('.') || punct.includes('!') || punct.includes('?')) {
                pauseDuration = 3.0;
            } else if (punct.includes(',') || punct.includes(';') || punct.includes(':')) {
                pauseDuration = 1.8;
            } else {
                pauseDuration = 0.8;
            }

            // Insert a silence viseme at the pause start so the mouth closes
            allVisemes.push({
                viseme: 'sil',
                relTime: totalRelativeTime,
                relDuration: pauseDuration
            });
            totalRelativeTime += pauseDuration;
        });

        if (allVisemes.length === 0 || totalRelativeTime === 0) return [];

        // Scale relative times to actual audio duration
        // Leave small margin at start and end for natural feel
        const startMargin = 0.05; // 50ms
        const endMargin = 0.15;   // 150ms
        const effectiveDuration = audioDuration - startMargin - endMargin;
        const scale = effectiveDuration / totalRelativeTime;

        const timeline = allVisemes.map(v => ({
            time: startMargin + v.relTime * scale,
            viseme: v.viseme,
            duration: v.relDuration * scale,
            // PP and FF are bilabial/labiodental — more visible
            intensity: (v.viseme === 'PP' || v.viseme === 'FF') ? 0.9 : 0.7
        }));

        // Add initial and final silence
        timeline.unshift({ time: 0, viseme: 'sil', duration: startMargin, intensity: 0 });
        timeline.push({ time: audioDuration - endMargin, viseme: 'sil', duration: endMargin, intensity: 0 });

        return timeline;
    },

    /**
     * Get the active viseme at a given time from a timeline
     * @param {Object[]} timeline - Viseme timeline from textToVisemes()
     * @param {number} currentTime - Current audio playback time in seconds
     * @returns {Object} { viseme, intensity, progress } where progress is 0-1 within the viseme
     */
    getActiveViseme(timeline, currentTime) {
        if (!timeline || timeline.length === 0) return { viseme: 'sil', intensity: 0, progress: 0 };

        // Find the active viseme
        let active = timeline[0];
        for (let i = timeline.length - 1; i >= 0; i--) {
            if (currentTime >= timeline[i].time) {
                active = timeline[i];
                break;
            }
        }

        // Find the next viseme for blending
        let nextIdx = -1;
        for (let i = 0; i < timeline.length; i++) {
            if (timeline[i].time > currentTime) {
                nextIdx = i;
                break;
            }
        }

        const elapsed = currentTime - active.time;
        const progress = active.duration > 0 ? Math.min(1, elapsed / active.duration) : 1;

        // Envelope: quick attack, sustain, decay
        let envelope;
        if (progress < 0.15) {
            // Attack phase — ramp up quickly
            envelope = progress / 0.15;
        } else if (progress < 0.7) {
            // Sustain — full intensity
            envelope = 1.0;
        } else {
            // Decay — ramp down
            envelope = 1.0 - ((progress - 0.7) / 0.3);
        }
        envelope = Math.max(0, Math.min(1, envelope));

        return {
            viseme: active.viseme,
            intensity: active.intensity * envelope,
            progress: progress,
            nextViseme: nextIdx >= 0 ? timeline[nextIdx].viseme : 'sil'
        };
    }
};