@margoengine/cutscene 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,229 @@
1
+ import { MargoError as e } from "@margoengine/math/errors";
2
+ import { Label as t, Panel as n } from "@margoengine/ui";
3
+ //#region ../shared/errors.js
4
+ var r = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, i = (e, t) => TypeError(r(e, t));
5
+ //#endregion
6
+ //#region src/film-titles.ts
7
+ function a(e) {
8
+ return [
9
+ {
10
+ text: "Made with Margo Engine",
11
+ size: 36,
12
+ color: "#edbd84"
13
+ },
14
+ {
15
+ text: e,
16
+ size: 74,
17
+ color: "#fff5e8"
18
+ },
19
+ {
20
+ text: "Please subscribe",
21
+ size: 41,
22
+ color: "#e4edf1"
23
+ }
24
+ ];
25
+ }
26
+ var o = (e) => {
27
+ throw i(e, "film-titles");
28
+ }, s = (e, t) => ((typeof e != "number" || !Number.isFinite(e)) && o(`${t} is a finite number, got ${String(e)}`), e), c = (e) => {
29
+ let t = Math.max(0, Math.min(1, e));
30
+ return t * t * (3 - 2 * t);
31
+ }, l = (e, t, n) => n > t ? c((e - t) / (n - t)) : +(e >= t), u = (e, t, n, r, i) => {
32
+ if (!(e >= t && e < n)) return 0;
33
+ let a = (n - t) / 2;
34
+ return l(e, t, t + Math.min(r, a)) * (1 - l(e, n - Math.min(i, a), n));
35
+ }, d = /* @__PURE__ */ new WeakMap();
36
+ function f(e, t) {
37
+ let n, r, i;
38
+ if (Array.isArray(e)) [n, r, i] = e;
39
+ else if (e && typeof e == "object") {
40
+ let t = e;
41
+ ({start: n, end: r} = t), i = Array.isArray(t.lines) ? t.lines.join("\n") : t.text;
42
+ } else o(`cues[${t}] is a cue, a [start, end, text] tuple or a captions cue`);
43
+ return s(n, `cues[${t}] start`), s(r, `cues[${t}] end`), r > n || o(`cues[${t}] ends after it starts (${n} to ${r})`), typeof i != "string" && o(`cues[${t}] text is a string`), {
44
+ start: n,
45
+ end: r,
46
+ text: i
47
+ };
48
+ }
49
+ function p(e) {
50
+ m(e);
51
+ }
52
+ function m(e) {
53
+ let t = d.get(e);
54
+ if (t) return t;
55
+ (!e || typeof e != "object") && o("the film titles spec is an object"), typeof e.title != "string" && o("title is a string");
56
+ let n = s(e.fadeIn ?? .22, "fadeIn"), r = s(e.fadeOut ?? .24, "fadeOut"), i = s(e.closingFade ?? 1, "closingFade"), a = s(e.chapterHold ?? 4, "chapterHold"), c = s(e.firstNumber ?? 0, "firstNumber");
57
+ (n < 0 || r < 0 || i < 0 || a <= 0) && o("fades are 0 or more and chapterHold above 0");
58
+ let l = (e.cues ?? []).map(f).sort((e, t) => e.start - t.start || e.end - t.end), u = e.chapters ?? [], p = u.map((e, t) => e.number === void 0 ? c + t : s(e.number, `chapters[${t}] number`)), m = e.chapterCount === void 0 ? Math.max(0, ...p) : s(e.chapterCount, "chapterCount"), h = Math.max(2, String(Math.round(m)).length), g = [];
59
+ u.forEach((e, t) => {
60
+ let n = s(e.start, `chapters[${t}] start`);
61
+ if (t && !(n > u[t - 1].start) && o(`chapters start in film order; chapters[${t}] starts at ${n}`), typeof e.title != "string" && o(`chapters[${t}] title is a string`), e.card === !1) return;
62
+ let r = s(e.hold ?? a, `chapters[${t}] hold`);
63
+ r <= 0 && o(`chapters[${t}] hold is above 0`);
64
+ let i = u[t + 1]?.start ?? Infinity;
65
+ g.push({
66
+ start: n,
67
+ end: Math.min(n + r, i),
68
+ index: t,
69
+ label: `${String(p[t]).padStart(h, "0")} / ${String(m).padStart(h, "0")}`,
70
+ title: e.title,
71
+ place: e.place ?? null
72
+ });
73
+ });
74
+ let _ = {
75
+ cues: l,
76
+ cards: g,
77
+ closing: e.closing ? s(e.closing.start, "closing start") : null,
78
+ fadeIn: n,
79
+ fadeOut: r,
80
+ closingFade: i
81
+ };
82
+ return d.set(e, _), _;
83
+ }
84
+ function h(e, t) {
85
+ let n = 0, r = e.length;
86
+ for (; n < r;) {
87
+ let i = n + r >> 1;
88
+ e[i].start <= t ? n = i + 1 : r = i;
89
+ }
90
+ let i = null;
91
+ for (let r = n - 1; r >= 0; r--) e[r].end > t && (i = e[r]);
92
+ return i;
93
+ }
94
+ function g(e, t) {
95
+ let n = m(e);
96
+ s(t, "film time");
97
+ let r = h(n.cues, t), i = n.cards.find((e) => t >= e.start && t < e.end) ?? null;
98
+ return {
99
+ caption: r ? {
100
+ text: r.text,
101
+ opacity: u(t, r.start, r.end, n.fadeIn, n.fadeOut)
102
+ } : null,
103
+ chapter: i ? {
104
+ index: i.index,
105
+ label: i.label,
106
+ title: i.title,
107
+ place: i.place,
108
+ opacity: u(t, i.start, i.end, n.fadeIn, n.fadeOut)
109
+ } : null,
110
+ closing: n.closing === null ? 0 : l(t, n.closing, n.closing + n.closingFade)
111
+ };
112
+ }
113
+ var _ = class extends n {
114
+ constructor(e) {
115
+ super({
116
+ ...e,
117
+ visible: !1
118
+ }), this.alpha = 0;
119
+ }
120
+ get opacity() {
121
+ return this.alpha;
122
+ }
123
+ fade(e) {
124
+ e !== this.alpha && (this.alpha = e, this.visible = e > 0, this.invalidate(!1));
125
+ }
126
+ }, v = class {
127
+ constructor(e, n, r = {}) {
128
+ this.state = {
129
+ caption: null,
130
+ chapter: null,
131
+ closing: 0
132
+ }, this.disposed = !1, (!e || typeof e.add != "function") && o("FilmTitles needs a Ui from @margoengine/ui"), m(n), this.ui = e, this.spec = n;
133
+ let i = r.background ?? "#111a23cf", s = r.cardMargin ?? [
134
+ 0,
135
+ 0,
136
+ 200,
137
+ 98
138
+ ];
139
+ this.caption = new t({
140
+ text: "",
141
+ size: r.captionSize ?? 39,
142
+ color: r.captionColor ?? "#faf4e7",
143
+ outline: !0,
144
+ wrap: !1
145
+ }), this.captionPanel = new _({
146
+ anchor: "bottom-left",
147
+ margin: r.captionMargin ?? [
148
+ 0,
149
+ 0,
150
+ 74,
151
+ 97
152
+ ],
153
+ padding: [
154
+ 11,
155
+ 18,
156
+ 11,
157
+ 18
158
+ ],
159
+ radius: 7,
160
+ background: i,
161
+ pointerEvents: "none",
162
+ children: [this.caption]
163
+ }), this.number = new t({
164
+ text: "",
165
+ size: r.numberSize ?? 30,
166
+ color: "#edbd84",
167
+ wrap: !1
168
+ }), this.title = new t({
169
+ text: "",
170
+ size: r.titleSize ?? 64,
171
+ color: "#fff5e8",
172
+ wrap: !1
173
+ }), this.place = new t({
174
+ text: "",
175
+ size: r.placeSize ?? 32,
176
+ color: "#e4edf1",
177
+ wrap: !1
178
+ }), this.card = new _({
179
+ anchor: "bottom-left",
180
+ margin: s,
181
+ padding: 25,
182
+ radius: 8,
183
+ gap: 7,
184
+ background: i,
185
+ pointerEvents: "none",
186
+ children: [
187
+ this.number,
188
+ this.title,
189
+ this.place
190
+ ]
191
+ });
192
+ let c = n.closing ? n.closing.lines ?? a(n.title) : null;
193
+ this.closing = c ? new _({
194
+ anchor: "bottom-left",
195
+ margin: s,
196
+ padding: 25,
197
+ radius: 8,
198
+ gap: 7,
199
+ background: "#101920e8",
200
+ pointerEvents: "none",
201
+ children: c.map((e) => new t({
202
+ text: e.text,
203
+ size: e.size ?? "body",
204
+ color: e.color ?? "#fff5e8",
205
+ wrap: !1
206
+ }))
207
+ }) : null, this.widgets = [
208
+ this.captionPanel,
209
+ this.card,
210
+ ...this.closing ? [this.closing] : []
211
+ ], e.add(...this.widgets);
212
+ }
213
+ update(t) {
214
+ if (this.disposed) throw new e("disposed", "FilmTitles is disposed; create a new FilmTitles; see https://margoengine.com/errors#film-titles");
215
+ let n = g(this.spec, t);
216
+ return n.caption && (this.caption.text = n.caption.text), this.captionPanel.fade(n.caption?.opacity ?? 0), n.chapter && (this.number.text = n.chapter.label, this.title.text = n.chapter.title, this.place.text = n.chapter.place ?? "", this.place.visible = n.chapter.place !== null), this.card.fade(n.chapter?.opacity ?? 0), this.closing?.fade(n.closing), this.state = n, n;
217
+ }
218
+ dispose() {
219
+ if (!this.disposed) {
220
+ this.disposed = !0;
221
+ for (let e of this.widgets) this.ui.remove(e);
222
+ }
223
+ }
224
+ [Symbol.dispose]() {
225
+ this.dispose();
226
+ }
227
+ };
228
+ //#endregion
229
+ export { v as FilmTitles, p as checkFilmTitles, a as defaultClosingLines, g as filmTitlesAt };
@@ -0,0 +1,3 @@
1
+ export { BASIC_EASINGS, MAX_CUTSCENE_ITEMS, MAX_TIMELINE_DEPTH, parseCutscene, validateCutscene, type AnimationClip, type AnimationTrack, type AudioClip, type AudioTrack, type CameraKey, type CameraTrack, type ClipBase, type CueEvent, type CutsceneDocument, type CutsceneIssue, type CustomTrackTypes, type EaseSpec, type EventTrack, type Marker, type PropertyKey, type PropertyTrack, type Quat, type SubtitleClip, type SubtitleTrack, type TimelineClip, type TimelineSpec, type TimelineTrack, type Track, type TrackCheck, type TrackValidator, type TransformKey, type TransformTrack, type Vec3, } from './document.js';
2
+ export { Cutscene, EASING_NAMES, evaluate, type AnimationState, type AudioState, type CameraState, type CutsceneEvent, type CutsceneOptions, type CutsceneState, type CutsceneTrackType, type SubtitleState, type TrackInstant, type TransformState, } from './cutscene.js';
3
+ export { CutscenePlayer, type CutscenePlayerEvent, type CutscenePlayerOptions } from './player.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import { a as e, c as t, d as n, l as r, n as i, s as a, t as o, u as s } from "./cutscene-DAWl8liP.js";
2
+ import { t as c } from "./player-DtIkr2sQ.js";
3
+ export { a as BASIC_EASINGS, o as Cutscene, c as CutscenePlayer, i as EASING_NAMES, t as MAX_CUTSCENE_ITEMS, r as MAX_TIMELINE_DEPTH, e as evaluate, s as parseCutscene, n as validateCutscene };
@@ -0,0 +1,72 @@
1
+ import type { CutsceneState, CutsceneTrackType } from './cutscene.js';
2
+ import type { ClipBase } from './document.js';
3
+ /** The 15 viseme names people know from game and avatar tools. */
4
+ export declare const VISEMES: readonly ['sil', 'PP', 'FF', 'TH', 'DD', 'kk', 'CH', 'SS', 'nn', 'RR', 'aa', 'E', 'ih', 'oh', 'ou'];
5
+ /** A mouth position for a group of sounds. */
6
+ export type Viseme = (typeof VISEMES)[number];
7
+ /**
8
+ * The viseme of a phoneme: an ARPAbet symbol in either case, with or without its stress digit (`AH0`, `ey`), or a
9
+ * viseme name. Unknown symbols give null.
10
+ */
11
+ export declare function visemeOf(phoneme: string): Viseme | null;
12
+ /** A sound in dialogue. */
13
+ export interface PhonemeKey {
14
+ /** Seconds from the start of the clip. */
15
+ time: number;
16
+ /** Seconds from the start of the clip; default the next key's time. */
17
+ end?: number;
18
+ /** ARPAbet symbol or viseme name. */
19
+ phoneme: string;
20
+ /** How far the mouth goes, 0 to 1 (default 1). */
21
+ weight?: number;
22
+ }
23
+ /** A line of dialogue. */
24
+ export interface LipSyncClip extends ClipBase {
25
+ phonemes: PhonemeKey[];
26
+ /** Scale on every weight (default 1). */
27
+ gain?: number;
28
+ }
29
+ /** Mouth shapes for one character. */
30
+ export interface LipSyncTrack {
31
+ type: 'lipsync';
32
+ id: string;
33
+ /** The character's name in your scene. */
34
+ target: string;
35
+ clips: LipSyncClip[];
36
+ /** Seconds a mouth takes to move into and out of a sound (default 0.06). */
37
+ blend?: number;
38
+ muted?: boolean;
39
+ }
40
+ /** Weights of the visemes that are not 0. They sum to 1 or less, and what is left is a closed, resting mouth. */
41
+ export type VisemeWeights = Partial<Record<Viseme, number>>;
42
+ /** The viseme weights of a clip at `t` seconds into it. */
43
+ export declare function visemesAt(clip: LipSyncClip, t: number, blend?: number): VisemeWeights;
44
+ /**
45
+ * The `lipsync` track type: pass it in the `tracks` option of `Cutscene` or `CutscenePlayer`. A track sets the
46
+ * character's weights, read with {@link mouthOf}. Clips fire `enter` and `exit`, named after the character.
47
+ */
48
+ export declare function lipSyncTrack(): CutsceneTrackType;
49
+ /** The viseme weights a state gives a character; empty when its mouth is at rest. */
50
+ export declare function mouthOf(state: CutsceneState, target: string): VisemeWeights;
51
+ /**
52
+ * Weights as blend shapes to set: `'visemes'` (default) names them `viseme_aa` and so on, as avatar tools do, `'vrm'`
53
+ * gives the five VRM 1.0 mouth presets, `'arkit'` Apple's face shape names. A shape absent from the result is 0.
54
+ */
55
+ export declare function mouthShapes(weights: VisemeWeights, set?: 'visemes' | 'vrm' | 'arkit'): Record<string, number>;
56
+ /** Options of {@link analyseSpeech}. */
57
+ export interface SpeechOptions {
58
+ /** Seconds of the frame step (default 0.01). */
59
+ step?: number;
60
+ /** Quietest sound counted, in dB under the loudest frame (default 35). */
61
+ floor?: number;
62
+ /** Shortest sound kept, seconds (default 0.04). */
63
+ shortest?: number;
64
+ }
65
+ /**
66
+ * Reads timed visemes out of recorded speech: silence, hiss, and each vowel's mouth from its formants. It needs no
67
+ * words or transcript and works on any voice in any language, as a rough mouth, not a transcript: it does not tell
68
+ * consonants apart beyond closed, hiss and open. Use timed phonemes from a forced aligner when the mouth must be exact.
69
+ * @returns keys ready for a clip's `phonemes`, with `weight` following how loud each sound is.
70
+ * @throws RangeError for a bad rate or option.
71
+ */
72
+ export declare function analyseSpeech(samples: Float32Array, sampleRate: number, options?: SpeechOptions): PhonemeKey[];
@@ -0,0 +1,377 @@
1
+ //#region src/lipsync.ts
2
+ var e = [
3
+ "sil",
4
+ "PP",
5
+ "FF",
6
+ "TH",
7
+ "DD",
8
+ "kk",
9
+ "CH",
10
+ "SS",
11
+ "nn",
12
+ "RR",
13
+ "aa",
14
+ "E",
15
+ "ih",
16
+ "oh",
17
+ "ou"
18
+ ], t = new Map(Object.entries({
19
+ sil: "pau sil sp spn ssil",
20
+ PP: "p b m",
21
+ FF: "f v",
22
+ TH: "th dh",
23
+ DD: "t d",
24
+ kk: "k g hh ng",
25
+ CH: "ch jh sh zh",
26
+ SS: "s z",
27
+ nn: "n l",
28
+ RR: "r er axr",
29
+ aa: "aa ae ah ax ay aw",
30
+ E: "eh ey",
31
+ ih: "ih iy y ix",
32
+ oh: "ao ow oy",
33
+ ou: "uh uw w"
34
+ }).flatMap(([e, t]) => t.split(" ").map((t) => [t, e]))), n = new Set(e);
35
+ function r(e) {
36
+ return n.has(e) ? e : t.get(e.toLowerCase().replace(/[0-2]$/, "")) ?? null;
37
+ }
38
+ function i(e, t) {
39
+ let { path: n, add: i } = t;
40
+ t.target(), "blend" in e && !(typeof e.blend == "number" && e.blend >= 0 && Number.isFinite(e.blend)) && i(`${n}.blend`, "must be seconds, 0 or more"), t.clips((e, n) => {
41
+ t.positive(e, "gain", n);
42
+ let a = t.list(e.phonemes, `${n}.phonemes`);
43
+ a?.forEach((t, a) => {
44
+ let o = `${n}.phonemes[${a}]`;
45
+ typeof t.time == "number" && t.time >= 0 && t.time <= e.duration || i(`${o}.time`, "must be seconds inside the clip"), "end" in t && !(typeof t.end == "number" && t.end >= t.time) && i(`${o}.end`, "must not come before its time"), (typeof t.phoneme != "string" || r(t.phoneme) === null) && i(`${o}.phoneme`, `unknown phoneme ${JSON.stringify(t.phoneme)}; use ARPAbet or a viseme name`), "weight" in t && !(typeof t.weight == "number" && t.weight >= 0 && t.weight <= 1) && i(`${o}.weight`, "must be from 0 to 1");
46
+ }), a && t.ordered(a, `${n}.phonemes`, "time");
47
+ });
48
+ }
49
+ var a = /* @__PURE__ */ new WeakMap();
50
+ function o(e) {
51
+ let t = a.get(e);
52
+ if (t) return t;
53
+ let n = 0;
54
+ return t = e.phonemes.map((t, i) => {
55
+ let a = t.end ?? e.phonemes[i + 1]?.time ?? e.duration;
56
+ return n = Math.max(n, a), {
57
+ from: t.time,
58
+ to: a,
59
+ viseme: r(t.phoneme),
60
+ weight: t.weight ?? 1,
61
+ latest: n
62
+ };
63
+ }), a.set(e, t), t;
64
+ }
65
+ var s = .06;
66
+ function c({ from: e, to: t }, n, r) {
67
+ if (!r) return +(n >= e && n < t);
68
+ let i = r / 2;
69
+ return Math.max(0, Math.min(1, (n - e + i) / r, (t + i - n) / r));
70
+ }
71
+ function l(e, t, n = s) {
72
+ let r = o(e), i = n / 2, a = {}, l = r.length, u = 0;
73
+ for (; u < l;) {
74
+ let e = u + l >> 1;
75
+ r[e].from - i <= t ? u = e + 1 : l = e;
76
+ }
77
+ let d = 0;
78
+ for (let e = u - 1; e >= 0 && r[e].latest + i > t; e--) {
79
+ let i = r[e], o = c(i, t, n);
80
+ o <= 0 || (d += o, i.viseme !== "sil" && (a[i.viseme] = (a[i.viseme] ?? 0) + o * i.weight));
81
+ }
82
+ let f = (e.gain ?? 1) / Math.max(1, d);
83
+ for (let e of Object.keys(a)) a[e] = Math.min(1, a[e] * f);
84
+ return a;
85
+ }
86
+ function u() {
87
+ return {
88
+ type: "lipsync",
89
+ validate: i,
90
+ instants(e) {
91
+ let { clips: t, target: n } = e;
92
+ return t.flatMap((e) => [{
93
+ time: e.start,
94
+ kind: "enter",
95
+ name: n,
96
+ clip: e
97
+ }, {
98
+ time: e.start + e.duration,
99
+ kind: "exit",
100
+ name: n,
101
+ clip: e
102
+ }]);
103
+ },
104
+ evaluate(e, t, n) {
105
+ let { clips: r, target: i, blend: a = s } = e, o = (n.extras ??= {}).mouths ??= {}, c = o[i] ??= {}, u = a / 2;
106
+ for (let e of r) if (t >= e.start - u && t < e.start + e.duration + u) for (let [n, r] of Object.entries(l(e, t - e.start, a))) c[n] = Math.min(1, (c[n] ?? 0) + r);
107
+ }
108
+ };
109
+ }
110
+ function d(e, t) {
111
+ return e.extras?.mouths?.[t] ?? {};
112
+ }
113
+ var f = {
114
+ vrm: {
115
+ FF: { ih: .2 },
116
+ TH: {
117
+ ih: .3,
118
+ aa: .1
119
+ },
120
+ DD: {
121
+ ih: .3,
122
+ aa: .15
123
+ },
124
+ kk: {
125
+ aa: .25,
126
+ ih: .15
127
+ },
128
+ CH: {
129
+ ou: .4,
130
+ ih: .3
131
+ },
132
+ SS: { ih: .5 },
133
+ nn: {
134
+ ih: .25,
135
+ aa: .15
136
+ },
137
+ RR: {
138
+ ou: .4,
139
+ oh: .2
140
+ },
141
+ aa: { aa: 1 },
142
+ E: {
143
+ ee: .8,
144
+ aa: .2
145
+ },
146
+ ih: { ih: 1 },
147
+ oh: { oh: 1 },
148
+ ou: { ou: 1 }
149
+ },
150
+ arkit: {
151
+ PP: {
152
+ mouthClose: .3,
153
+ mouthPressLeft: .6,
154
+ mouthPressRight: .6
155
+ },
156
+ FF: {
157
+ mouthRollLower: .6,
158
+ mouthUpperUpLeft: .3,
159
+ mouthUpperUpRight: .3,
160
+ jawOpen: .05
161
+ },
162
+ TH: {
163
+ jawOpen: .15,
164
+ mouthUpperUpLeft: .1,
165
+ mouthUpperUpRight: .1
166
+ },
167
+ DD: {
168
+ jawOpen: .15,
169
+ mouthStretchLeft: .2,
170
+ mouthStretchRight: .2
171
+ },
172
+ kk: {
173
+ jawOpen: .2,
174
+ mouthStretchLeft: .1,
175
+ mouthStretchRight: .1
176
+ },
177
+ CH: {
178
+ jawOpen: .1,
179
+ mouthFunnel: .6,
180
+ mouthPucker: .3
181
+ },
182
+ SS: {
183
+ jawOpen: .05,
184
+ mouthStretchLeft: .5,
185
+ mouthStretchRight: .5
186
+ },
187
+ nn: {
188
+ jawOpen: .15,
189
+ mouthStretchLeft: .15,
190
+ mouthStretchRight: .15
191
+ },
192
+ RR: {
193
+ jawOpen: .15,
194
+ mouthFunnel: .4,
195
+ mouthPucker: .2
196
+ },
197
+ aa: { jawOpen: .7 },
198
+ E: {
199
+ jawOpen: .35,
200
+ mouthStretchLeft: .5,
201
+ mouthStretchRight: .5
202
+ },
203
+ ih: {
204
+ jawOpen: .2,
205
+ mouthSmileLeft: .4,
206
+ mouthSmileRight: .4
207
+ },
208
+ oh: {
209
+ jawOpen: .45,
210
+ mouthFunnel: .6
211
+ },
212
+ ou: {
213
+ jawOpen: .15,
214
+ mouthPucker: .9
215
+ }
216
+ }
217
+ };
218
+ function p(e, t = "visemes") {
219
+ let n = {};
220
+ for (let [r, i] of Object.entries(e)) if (t === "visemes") n[`viseme_${r}`] = i;
221
+ else for (let [e, a] of Object.entries(f[t][r] ?? {})) n[e] = Math.min(1, (n[e] ?? 0) + a * i);
222
+ return n;
223
+ }
224
+ var m = 11025, h = 14;
225
+ function g(e, t) {
226
+ let n = new Float64Array(t + 1);
227
+ for (let r = 0; r <= t; r++) {
228
+ let t = 0;
229
+ for (let n = r; n < e.length; n++) t += e[n] * e[n - r];
230
+ n[r] = t;
231
+ }
232
+ if (!(n[0] > 0)) return null;
233
+ n[0] *= 1 + 1e-5;
234
+ let r = new Float64Array(t + 1), i = new Float64Array(t + 1);
235
+ r[0] = 1;
236
+ let a = n[0];
237
+ for (let e = 1; e <= t; e++) {
238
+ let t = n[e];
239
+ for (let i = 1; i < e; i++) t += r[i] * n[e - i];
240
+ let o = -t / a;
241
+ i.set(r);
242
+ for (let t = 1; t < e; t++) i[t] = r[t] + o * r[e - t];
243
+ if (i[e] = o, r.set(i), a *= 1 - o * o, !(a > 0)) return null;
244
+ }
245
+ return r;
246
+ }
247
+ function _(e, t) {
248
+ let n = /* @__PURE__ */ new Float64Array(257);
249
+ for (let t = 0; t <= 256; t++) {
250
+ let r = Math.PI * t / 256, i = 0, a = 0;
251
+ for (let t = 0; t < e.length; t++) i += e[t] * Math.cos(r * t), a -= e[t] * Math.sin(r * t);
252
+ n[t] = 1 / (i * i + a * a);
253
+ }
254
+ let r = [];
255
+ for (let e = 1; e < 256; e++) if (n[e] > n[e - 1] && n[e] >= n[e + 1]) {
256
+ let i = Math.log(n[e - 1]), a = Math.log(n[e]), o = Math.log(n[e + 1]), s = .5 * (i - o) / (i - 2 * a + o || 1), c = (e + s) / 256 * (t / 2);
257
+ c > 150 && r.push(c);
258
+ }
259
+ return r;
260
+ }
261
+ var v = [
262
+ [
263
+ "ih",
264
+ 300,
265
+ 2400
266
+ ],
267
+ [
268
+ "ih",
269
+ 420,
270
+ 2050
271
+ ],
272
+ [
273
+ "E",
274
+ 560,
275
+ 1900
276
+ ],
277
+ [
278
+ "aa",
279
+ 800,
280
+ 1750
281
+ ],
282
+ [
283
+ "aa",
284
+ 800,
285
+ 1250
286
+ ],
287
+ [
288
+ "oh",
289
+ 620,
290
+ 950
291
+ ],
292
+ [
293
+ "ou",
294
+ 470,
295
+ 1050
296
+ ],
297
+ [
298
+ "ou",
299
+ 320,
300
+ 900
301
+ ]
302
+ ], y = (e) => {
303
+ let t = [...e].sort((e, t) => e - t);
304
+ return t[t.length >> 1] ?? 0;
305
+ };
306
+ function b(e, t, n = {}) {
307
+ let { step: r = .01, floor: i = 35, shortest: a = .04 } = n;
308
+ if (!(t >= 8e3) || !Number.isFinite(t)) throw RangeError("analyseSpeech needs a sampleRate of 8000 or more");
309
+ if (!(r > 0) || !(i > 0) || !(a >= 0)) throw RangeError("analyseSpeech options must be above 0");
310
+ let o = Math.max(1, Math.floor(t / m)), s = t / o, c = new Float64Array(Math.floor(e.length / o));
311
+ for (let t = 0; t < c.length; t++) {
312
+ let n = 0;
313
+ for (let r = 0; r < o; r++) n += e[t * o + r];
314
+ c[t] = n / o;
315
+ }
316
+ let l = Math.round(.03 * s), u = Math.max(1, Math.round(r * s)), d = Math.max(0, Math.floor((c.length - l) / u) + 1), f = Float64Array.from({ length: l }, (e, t) => .54 - .46 * Math.cos(2 * Math.PI * t / (l - 1))), p = new Float64Array(d), b = new Float64Array(d), x = [], S = new Float64Array(l), C = 0;
317
+ for (let e = 0; e < d; e++) {
318
+ let t = e * u, n = 0, r = 0;
319
+ for (let e = 0; e < l; e++) {
320
+ let i = c[t + e], a = t + e > 0 ? c[t + e - 1] : 0;
321
+ n += i * i, r += (i - a) ** 2, S[e] = (i - .97 * a) * f[e];
322
+ }
323
+ p[e] = n / l, b[e] = n > 0 ? r / n : 0, C = Math.max(C, p[e]);
324
+ let i = g(S, h);
325
+ x.push(i ? _(i, s) : []);
326
+ }
327
+ let w = C * 10 ** (-i / 10), T = [], E = [];
328
+ for (let e = 0; e < d; e++) p[e] <= w ? T.push("sil") : b[e] > 1.1 ? T.push("SS") : (T.push("aa"), p[e] > C * .03 && x[e].length >= 2 && E.push(x[e][1]));
329
+ let D = E.length ? y(E) / 1500 : 1;
330
+ for (let e = 0; e < d; e++) {
331
+ if (T[e] !== "aa") continue;
332
+ let t = x[e];
333
+ if (t.length < 2 || p[e] < C * .003) {
334
+ T[e] = "nn";
335
+ continue;
336
+ }
337
+ let n = Math.log(t[0] / D), r = Math.log(t[1] / D), i = Infinity;
338
+ for (let [t, a, o] of v) {
339
+ let s = (n - Math.log(a)) ** 2 + (r - Math.log(o)) ** 2;
340
+ s < i && (i = s, T[e] = t);
341
+ }
342
+ }
343
+ let O = T.map((e, t) => {
344
+ let n = /* @__PURE__ */ new Map();
345
+ for (let e = Math.max(0, t - 2); e <= Math.min(d - 1, t + 2); e++) n.set(T[e], (n.get(T[e]) ?? 0) + 1);
346
+ let r = e, i = 0;
347
+ for (let [e, t] of n) t > i && (i = t, r = e);
348
+ return r;
349
+ }), k = (e) => (e * u + l / 2) / s, A = [];
350
+ for (let e = 0; e < d; e++) {
351
+ let t = A.at(-1);
352
+ t?.viseme === O[e] ? t.to = e + 1 : A.push({
353
+ from: e,
354
+ to: e + 1,
355
+ viseme: O[e]
356
+ });
357
+ }
358
+ let j = [];
359
+ for (let e of A) {
360
+ let t = j.at(-1);
361
+ t && (k(e.to) - k(e.from) < a || t.viseme === e.viseme) ? t.to = e.to : j.push({ ...e });
362
+ }
363
+ let M = Math.sqrt(C) || 1;
364
+ return j.map(({ from: e, to: t, viseme: n }) => {
365
+ let r = 0;
366
+ for (let n = e; n < t; n++) r += p[n];
367
+ let i = Math.sqrt(r / (t - e)) / M;
368
+ return {
369
+ time: k(e),
370
+ end: k(t),
371
+ phoneme: n,
372
+ weight: n === "sil" ? 1 : Math.min(1, .4 + i * 1.2)
373
+ };
374
+ });
375
+ }
376
+ //#endregion
377
+ export { e as VISEMES, b as analyseSpeech, u as lipSyncTrack, d as mouthOf, p as mouthShapes, r as visemeOf, l as visemesAt };